periph 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/examples/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -0
- package/examples/rtc/ds3231/complete.js +77 -0
- package/examples/rtc/ds3231/demo.js +60 -0
- package/examples/rtc/ds3231/minimal.js +23 -0
- package/examples/rtc/pcf8523/complete.js +66 -0
- package/examples/rtc/pcf8523/demo.js +65 -0
- package/examples/rtc/pcf8523/minimal.js +20 -0
- package/examples/temperature/mcp9808/complete.js +89 -0
- package/examples/temperature/mcp9808/demo.js +62 -0
- package/examples/temperature/mcp9808/minimal.js +21 -0
- package/examples/temperature/tmp117/complete.js +90 -0
- package/examples/temperature/tmp117/demo.js +73 -0
- package/examples/temperature/tmp117/minimal.js +21 -0
- package/examples/tof/vl53l0x/complete.js +118 -0
- package/examples/tof/vl53l0x/demo.js +83 -0
- package/examples/tof/vl53l0x/minimal.js +25 -0
- package/examples/tof/vl53l1x/complete.js +151 -0
- package/examples/tof/vl53l1x/demo.js +100 -0
- package/examples/tof/vl53l1x/minimal.js +25 -0
- package/package.json +1 -1
- package/src/chips/imu/mpu9250.js +4 -4
- package/src/chips/imu/mpu9255.js +5 -5
- package/src/chips/motor/drv8830.js +239 -0
- package/src/chips/rtc/ds3231.js +504 -0
- package/src/chips/rtc/pcf8523.js +539 -0
- package/src/chips/temperature/mcp9808.js +488 -0
- package/src/chips/temperature/tmp117.js +471 -0
- package/src/chips/tof/_vl53_base.js +160 -0
- package/src/chips/tof/vl53l0x.js +816 -0
- package/src/chips/tof/vl53l1x.js +816 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_CONTROL_1 = 0x00;
|
|
4
|
+
const _REG_CONTROL_2 = 0x01;
|
|
5
|
+
const _REG_CONTROL_3 = 0x02;
|
|
6
|
+
const _REG_SECONDS = 0x03;
|
|
7
|
+
const _REG_MINUTE_ALARM = 0x0A;
|
|
8
|
+
const _REG_OFFSET = 0x0E;
|
|
9
|
+
const _REG_TMR_CLKOUT_CTRL = 0x0F;
|
|
10
|
+
const _REG_TMR_A_FREQ_CTRL = 0x10;
|
|
11
|
+
const _REG_TMR_A_REG = 0x11;
|
|
12
|
+
const _REG_TMR_B_FREQ_CTRL = 0x12;
|
|
13
|
+
const _REG_TMR_B_REG = 0x13;
|
|
14
|
+
|
|
15
|
+
// CONTROL_1 (0x00) bits.
|
|
16
|
+
const _C1_T = 0x40;
|
|
17
|
+
const _C1_STOP = 0x20;
|
|
18
|
+
const _C1_SR = 0x10;
|
|
19
|
+
const _C1_12_24 = 0x08;
|
|
20
|
+
const _C1_SIE = 0x04;
|
|
21
|
+
const _C1_AIE = 0x02;
|
|
22
|
+
|
|
23
|
+
// CONTROL_2 (0x01) bits.
|
|
24
|
+
const _C2_WTAF = 0x80;
|
|
25
|
+
const _C2_CTAF = 0x40;
|
|
26
|
+
const _C2_CTBF = 0x20;
|
|
27
|
+
const _C2_SF = 0x10;
|
|
28
|
+
const _C2_AF = 0x08;
|
|
29
|
+
const _C2_WTAIE = 0x04;
|
|
30
|
+
const _C2_CTAIE = 0x02;
|
|
31
|
+
const _C2_CTBIE = 0x01;
|
|
32
|
+
const _C2_CLEARABLE = 0x78; // CTAF|CTBF|SF|AF: write 0 clears, 1 keeps
|
|
33
|
+
const _C2_ENABLES = 0x07;
|
|
34
|
+
|
|
35
|
+
// CONTROL_3 (0x02) bits.
|
|
36
|
+
const _C3_PM_MASK = 0xE0;
|
|
37
|
+
const _C3_BSF = 0x08;
|
|
38
|
+
const _C3_BLF = 0x04;
|
|
39
|
+
const _C3_BSIE = 0x02;
|
|
40
|
+
const _C3_BLIE = 0x01;
|
|
41
|
+
|
|
42
|
+
const _SECONDS_OS = 0x80;
|
|
43
|
+
|
|
44
|
+
// TMR_CLKOUT_CTRL (0x0F) fields.
|
|
45
|
+
const _TMR_TAM = 0x80;
|
|
46
|
+
const _TMR_TBM = 0x40;
|
|
47
|
+
const _TMR_COF_MASK = 0x38;
|
|
48
|
+
const _TMR_TAC_MASK = 0x06;
|
|
49
|
+
const _TMR_TAC_COUNTDOWN = 0x02;
|
|
50
|
+
const _TMR_TAC_WATCHDOG = 0x04;
|
|
51
|
+
const _TMR_TBC = 0x01;
|
|
52
|
+
|
|
53
|
+
const _SOURCE_CLOCKS = { '4096hz': 0x00, '64hz': 0x01, '1hz': 0x02, '1_60hz': 0x03, '1_3600hz': 0x07 };
|
|
54
|
+
|
|
55
|
+
// TBW[2:0] -> low-pulse width in ms (datasheet Table 36; not uniformly spaced).
|
|
56
|
+
const _TBW_WIDTHS_MS = [46.875, 62.5, 78.125, 93.75, 125, 156.25, 187.5, 218.75];
|
|
57
|
+
|
|
58
|
+
const _CLKOUT_COF = { 32768: 0, 16384: 1, 8192: 2, 4096: 3, 1024: 4, 32: 5, 1: 6 };
|
|
59
|
+
|
|
60
|
+
const _PM_MODES = {
|
|
61
|
+
standard: [0x00, 0x04], // [low detection on, off]
|
|
62
|
+
direct: [0x01, 0x05],
|
|
63
|
+
disabled: [0x02, 0x07],
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
function _bcdToInt(b) {
|
|
67
|
+
return ((b >> 4) & 0x0F) * 10 + (b & 0x0F);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function _intToBcd(v) {
|
|
71
|
+
return (Math.floor(v / 10) << 4) | (v % 10);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* PCF8523 low-power I²C real-time clock and calendar (NXP) — minimal
|
|
76
|
+
* interface.
|
|
77
|
+
*
|
|
78
|
+
* Reads/writes the battery-backed calendar clock with no configuration
|
|
79
|
+
* beyond the connection. `init()` enables battery switch-over in standard
|
|
80
|
+
* mode with battery-low detection (PM[2:0]=000) — a deliberate override of
|
|
81
|
+
* the chip's single-supply POR default. Fixed I²C address 0x68.
|
|
82
|
+
*
|
|
83
|
+
* This driver always operates the HOURS registers in 24-hour mode.
|
|
84
|
+
* `weekday` follows the datasheet's suggested assignment, 0=Sunday ..
|
|
85
|
+
* 6=Saturday — the WEEKDAYS register has no hardware-enforced meaning.
|
|
86
|
+
*/
|
|
87
|
+
class PCF8523Minimal {
|
|
88
|
+
/**
|
|
89
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection at address 0x68.
|
|
90
|
+
*/
|
|
91
|
+
constructor(connection) {
|
|
92
|
+
this._conn = connection;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async _writeReg(reg, value) {
|
|
96
|
+
await this._conn.write(Buffer.from([reg & 0xFF, value & 0xFF]));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async _readReg(reg) {
|
|
100
|
+
return (await this._conn.writeRead(Buffer.from([reg & 0xFF]), 1))[0];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async _readRegs(reg, n) {
|
|
104
|
+
return this._conn.writeRead(Buffer.from([reg & 0xFF]), n);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async _readControl1() {
|
|
108
|
+
// T must always be written 0 and SR always reads 0; mask both so a
|
|
109
|
+
// read-modify-write never triggers a reset.
|
|
110
|
+
return (await this._readReg(_REG_CONTROL_1)) & ~(_C1_T | _C1_SR) & 0xFF;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Confirm the device answers on the bus (plain CONTROL_1 read — the
|
|
115
|
+
* PCF8523 has no identity register) and enable battery switch-over
|
|
116
|
+
* standard mode with battery-low detection (PM[2:0]=000).
|
|
117
|
+
* @returns {Promise<void>}
|
|
118
|
+
*/
|
|
119
|
+
async init() {
|
|
120
|
+
await this._readReg(_REG_CONTROL_1);
|
|
121
|
+
await this._writeReg(_REG_CONTROL_3, 0x00);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Read the calendar clock.
|
|
126
|
+
* @returns {Promise<{year: number, month: number, day: number, weekday: number, hour: number, minute: number, second: number}>}
|
|
127
|
+
* `year` is 2000-2099, `hour` is 0-23, `weekday` is 0=Sunday..6=Saturday.
|
|
128
|
+
*/
|
|
129
|
+
async getDatetime() {
|
|
130
|
+
const raw = await this._readRegs(_REG_SECONDS, 7);
|
|
131
|
+
return {
|
|
132
|
+
year: 2000 + _bcdToInt(raw[6]),
|
|
133
|
+
month: _bcdToInt(raw[5] & 0x1F),
|
|
134
|
+
day: _bcdToInt(raw[3] & 0x3F),
|
|
135
|
+
weekday: raw[4] & 0x07,
|
|
136
|
+
hour: _bcdToInt(raw[2] & 0x3F),
|
|
137
|
+
minute: _bcdToInt(raw[1] & 0x7F),
|
|
138
|
+
second: _bcdToInt(raw[0] & 0x7F),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Set the calendar clock using the STOP-bit precision start: freeze the
|
|
144
|
+
* divider chain (STOP=1), write all seven time/date registers in one
|
|
145
|
+
* transaction, then release STOP. Forces 24-hour mode and clears the OS
|
|
146
|
+
* flag (the time is now known-good).
|
|
147
|
+
* @param {number} year - 2000-2099.
|
|
148
|
+
* @param {number} month - 1-12.
|
|
149
|
+
* @param {number} day - 1-31.
|
|
150
|
+
* @param {number} weekday - 0=Sunday..6=Saturday.
|
|
151
|
+
* @param {number} hour - 0-23.
|
|
152
|
+
* @param {number} minute - 0-59.
|
|
153
|
+
* @param {number} second - 0-59.
|
|
154
|
+
* @returns {Promise<void>}
|
|
155
|
+
*/
|
|
156
|
+
async setDatetime(year, month, day, weekday, hour, minute, second) {
|
|
157
|
+
const ctrl1 = (await this._readControl1()) & ~_C1_12_24 & 0xFF;
|
|
158
|
+
await this._writeReg(_REG_CONTROL_1, ctrl1 | _C1_STOP);
|
|
159
|
+
await this._conn.write(Buffer.from([
|
|
160
|
+
_REG_SECONDS,
|
|
161
|
+
_intToBcd(second) & 0x7F, // OS = 0
|
|
162
|
+
_intToBcd(minute),
|
|
163
|
+
_intToBcd(hour) & 0x3F,
|
|
164
|
+
_intToBcd(day),
|
|
165
|
+
weekday & 0x07,
|
|
166
|
+
_intToBcd(month),
|
|
167
|
+
_intToBcd(year - 2000),
|
|
168
|
+
]));
|
|
169
|
+
await this._writeReg(_REG_CONTROL_1, ctrl1 & ~_C1_STOP & 0xFF);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* PCF8523 full interface — extends Minimal with the alarm, Timer A
|
|
175
|
+
* (countdown or watchdog), Timer B, programmable CLKOUT, offset
|
|
176
|
+
* calibration, battery backup control/status, oscillator-stop detection,
|
|
177
|
+
* software reset, and the Level-3 interrupt API.
|
|
178
|
+
*
|
|
179
|
+
* INT1 is shared with CLKOUT: interrupts only reach INT1 once CLKOUT is
|
|
180
|
+
* disabled (`disableClockOutput()`); the driver never does that
|
|
181
|
+
* implicitly. Timer B additionally drives the dedicated INT2 pin.
|
|
182
|
+
*/
|
|
183
|
+
class PCF8523Full extends PCF8523Minimal {
|
|
184
|
+
constructor(connection) {
|
|
185
|
+
super(connection);
|
|
186
|
+
this._callback = null;
|
|
187
|
+
this._edgeHandler = null;
|
|
188
|
+
this._pollTimer = null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async _updateTmrClkout(clearMask, setBits) {
|
|
192
|
+
const reg = await this._readReg(_REG_TMR_CLKOUT_CTRL);
|
|
193
|
+
await this._writeReg(_REG_TMR_CLKOUT_CTRL, (reg & ~clearMask) | setBits);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async _writeControl2(enables) {
|
|
197
|
+
// Re-supply the enable bits; write 1 to every clearable flag so none
|
|
198
|
+
// is cleared by accident (AND semantics). WTAF is read-only.
|
|
199
|
+
await this._writeReg(_REG_CONTROL_2, _C2_CLEARABLE | (enables & _C2_ENABLES));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async _writeControl3(value) {
|
|
203
|
+
// Write 1 to BSF so it is left unchanged; BLF is read-only.
|
|
204
|
+
await this._writeReg(_REG_CONTROL_3, (value & (_C3_PM_MASK | _C3_BSIE | _C3_BLIE)) | _C3_BSF);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// -- Alarm ------------------------------------------------------------
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Decode the alarm registers (0x0A-0x0D).
|
|
211
|
+
* @returns {Promise<{minute: ?number, hour: ?number, day: ?number, weekday: ?number}>}
|
|
212
|
+
* Each field is `null` when disabled (AEN_x=1).
|
|
213
|
+
*/
|
|
214
|
+
async getAlarm() {
|
|
215
|
+
const raw = await this._readRegs(_REG_MINUTE_ALARM, 4);
|
|
216
|
+
return {
|
|
217
|
+
minute: raw[0] & 0x80 ? null : _bcdToInt(raw[0] & 0x7F),
|
|
218
|
+
hour: raw[1] & 0x80 ? null : _bcdToInt(raw[1] & 0x3F),
|
|
219
|
+
day: raw[2] & 0x80 ? null : _bcdToInt(raw[2] & 0x3F),
|
|
220
|
+
weekday: raw[3] & 0x80 ? null : raw[3] & 0x07,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Write the alarm registers (0x0A-0x0D). A field left `null`/undefined
|
|
226
|
+
* is disabled; the alarm fires when every enabled field matches.
|
|
227
|
+
* @param {{minute?: ?number, hour?: ?number, day?: ?number, weekday?: ?number}} [alarm={}]
|
|
228
|
+
* `weekday` is 0=Sunday..6=Saturday.
|
|
229
|
+
* @returns {Promise<void>}
|
|
230
|
+
*/
|
|
231
|
+
async setAlarm({ minute = null, hour = null, day = null, weekday = null } = {}) {
|
|
232
|
+
await this._conn.write(Buffer.from([
|
|
233
|
+
_REG_MINUTE_ALARM,
|
|
234
|
+
minute == null ? 0x80 : _intToBcd(minute) & 0x7F,
|
|
235
|
+
hour == null ? 0x80 : _intToBcd(hour) & 0x3F,
|
|
236
|
+
day == null ? 0x80 : _intToBcd(day) & 0x3F,
|
|
237
|
+
weekday == null ? 0x80 : weekday & 0x07,
|
|
238
|
+
]));
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// -- Timers -----------------------------------------------------------
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Configure and start Timer A.
|
|
245
|
+
* @param {'countdown'|'watchdog'} mode
|
|
246
|
+
* @param {number} value - Countdown value, 0-255.
|
|
247
|
+
* @param {'4096hz'|'64hz'|'1hz'|'1_60hz'|'1_3600hz'} sourceClock
|
|
248
|
+
* @param {boolean} [pulsed=false] - Pulsed (true) or permanently-active interrupt.
|
|
249
|
+
* @returns {Promise<void>}
|
|
250
|
+
*/
|
|
251
|
+
async configureTimerA(mode, value, sourceClock, pulsed = false) {
|
|
252
|
+
const tac = mode === 'watchdog' ? _TMR_TAC_WATCHDOG : _TMR_TAC_COUNTDOWN;
|
|
253
|
+
await this._writeReg(_REG_TMR_A_FREQ_CTRL, _SOURCE_CLOCKS[sourceClock]);
|
|
254
|
+
await this._writeReg(_REG_TMR_A_REG, value);
|
|
255
|
+
await this._updateTmrClkout(_TMR_TAM | _TMR_TAC_MASK, (pulsed ? _TMR_TAM : 0) | tac);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Stop Timer A (TAC=00).
|
|
260
|
+
* @returns {Promise<void>}
|
|
261
|
+
*/
|
|
262
|
+
async disableTimerA() {
|
|
263
|
+
await this._updateTmrClkout(_TMR_TAC_MASK, 0);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* @returns {Promise<number>} Timer A's live countdown value, 0-255 (not the loaded one).
|
|
268
|
+
*/
|
|
269
|
+
async readTimerA() {
|
|
270
|
+
return this._readReg(_REG_TMR_A_REG);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Configure and start Timer B (also drives INT2).
|
|
275
|
+
* @param {number} value - Countdown value, 0-255.
|
|
276
|
+
* @param {'4096hz'|'64hz'|'1hz'|'1_60hz'|'1_3600hz'} sourceClock
|
|
277
|
+
* @param {number} [pulseWidthMs=46.875] - Pulsed-mode low-pulse width in ms; the
|
|
278
|
+
* nearest of the eight hardware widths (46.875-218.75 ms) is used.
|
|
279
|
+
* @param {boolean} [pulsed=false] - Pulsed (true) or permanently-active interrupt.
|
|
280
|
+
* @returns {Promise<void>}
|
|
281
|
+
*/
|
|
282
|
+
async configureTimerB(value, sourceClock, pulseWidthMs = 46.875, pulsed = false) {
|
|
283
|
+
let tbw = 0;
|
|
284
|
+
_TBW_WIDTHS_MS.forEach((w, i) => {
|
|
285
|
+
if (Math.abs(w - pulseWidthMs) < Math.abs(_TBW_WIDTHS_MS[tbw] - pulseWidthMs)) tbw = i;
|
|
286
|
+
});
|
|
287
|
+
await this._writeReg(_REG_TMR_B_FREQ_CTRL, (tbw << 4) | _SOURCE_CLOCKS[sourceClock]);
|
|
288
|
+
await this._writeReg(_REG_TMR_B_REG, value);
|
|
289
|
+
await this._updateTmrClkout(_TMR_TBM | _TMR_TBC, (pulsed ? _TMR_TBM : 0) | _TMR_TBC);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Stop Timer B (TBC=0).
|
|
294
|
+
* @returns {Promise<void>}
|
|
295
|
+
*/
|
|
296
|
+
async disableTimerB() {
|
|
297
|
+
await this._updateTmrClkout(_TMR_TBC, 0);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* @returns {Promise<number>} Timer B's live countdown value, 0-255 (not the loaded one).
|
|
302
|
+
*/
|
|
303
|
+
async readTimerB() {
|
|
304
|
+
return this._readReg(_REG_TMR_B_REG);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// -- CLKOUT -----------------------------------------------------------
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Drive CLKOUT on the shared INT1/CLKOUT pin.
|
|
311
|
+
* @param {number} frequencyHz - One of 32768, 16384, 8192, 4096, 1024, 32, 1.
|
|
312
|
+
* @returns {Promise<void>}
|
|
313
|
+
*/
|
|
314
|
+
async setClockOutput(frequencyHz) {
|
|
315
|
+
const cof = frequencyHz in _CLKOUT_COF ? _CLKOUT_COF[frequencyHz] : 7;
|
|
316
|
+
await this._updateTmrClkout(_TMR_COF_MASK, cof << 3);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Disable CLKOUT (COF=111), freeing INT1 for interrupts.
|
|
321
|
+
* @returns {Promise<void>}
|
|
322
|
+
*/
|
|
323
|
+
async disableClockOutput() {
|
|
324
|
+
await this._updateTmrClkout(_TMR_COF_MASK, _TMR_COF_MASK);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// -- Offset -----------------------------------------------------------
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Read the clock-offset calibration register.
|
|
331
|
+
* @returns {Promise<{offset: number, mode: 'every_two_hours'|'every_minute'}>}
|
|
332
|
+
* `offset` is -64..+63 LSB (4.34 ppm/LSB every two hours, 4.069 ppm/LSB every minute).
|
|
333
|
+
*/
|
|
334
|
+
async getOffset() {
|
|
335
|
+
const raw = await this._readReg(_REG_OFFSET);
|
|
336
|
+
let offset = raw & 0x7F;
|
|
337
|
+
if (offset & 0x40) offset -= 128;
|
|
338
|
+
return { offset, mode: raw & 0x80 ? 'every_minute' : 'every_two_hours' };
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Write the clock-offset calibration register.
|
|
343
|
+
* @param {number} offset - Two's-complement correction, -64 to 63 LSB.
|
|
344
|
+
* @param {'every_two_hours'|'every_minute'} [mode='every_two_hours']
|
|
345
|
+
* @returns {Promise<void>}
|
|
346
|
+
*/
|
|
347
|
+
async setOffset(offset, mode = 'every_two_hours') {
|
|
348
|
+
await this._writeReg(_REG_OFFSET, (mode === 'every_minute' ? 0x80 : 0) | (offset & 0x7F));
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// -- Battery backup ---------------------------------------------------
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Select the battery switch-over mode (PM[2:0]).
|
|
355
|
+
* @param {'standard'|'direct'|'disabled'} mode - `disabled` means VDD only (tie VBAT to VDD).
|
|
356
|
+
* @param {boolean} [lowDetection=true] - Enable battery-low detection.
|
|
357
|
+
* @returns {Promise<void>}
|
|
358
|
+
*/
|
|
359
|
+
async configureBatteryBackup(mode, lowDetection = true) {
|
|
360
|
+
const pm = _PM_MODES[mode][lowDetection ? 0 : 1];
|
|
361
|
+
const ctrl3 = await this._readReg(_REG_CONTROL_3);
|
|
362
|
+
await this._writeControl3((ctrl3 & ~_C3_PM_MASK) | (pm << 5));
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* @returns {Promise<boolean>} BSF — a switch-over to VBAT occurred since it was last cleared.
|
|
367
|
+
*/
|
|
368
|
+
async isBatterySwitchedOver() {
|
|
369
|
+
return ((await this._readReg(_REG_CONTROL_3)) & _C3_BSF) !== 0;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Clear BSF only, leaving PM and the enable bits unchanged.
|
|
374
|
+
* @returns {Promise<void>}
|
|
375
|
+
*/
|
|
376
|
+
async clearBatterySwitchover() {
|
|
377
|
+
const ctrl3 = await this._readReg(_REG_CONTROL_3);
|
|
378
|
+
await this._writeReg(_REG_CONTROL_3, ctrl3 & (_C3_PM_MASK | _C3_BSIE | _C3_BLIE));
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* @returns {Promise<boolean>} BLF (read-only) — VBAT is below the detection threshold.
|
|
383
|
+
*/
|
|
384
|
+
async isBatteryLow() {
|
|
385
|
+
return ((await this._readReg(_REG_CONTROL_3)) & _C3_BLF) !== 0;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* @returns {Promise<boolean>} OS flag (bit 7 of SECONDS) — the time may be
|
|
390
|
+
* invalid; cleared by `setDatetime()`.
|
|
391
|
+
*/
|
|
392
|
+
async oscillatorStopped() {
|
|
393
|
+
return ((await this._readReg(_REG_SECONDS)) & _SECONDS_OS) !== 0;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Send the software-reset sequence (0x58 to CONTROL_1). Resets all
|
|
398
|
+
* control/configuration registers to POR defaults — including PM=111
|
|
399
|
+
* (battery backup disabled) — but keeps the time/date/alarm/timer values.
|
|
400
|
+
* @returns {Promise<void>}
|
|
401
|
+
*/
|
|
402
|
+
async softwareReset() {
|
|
403
|
+
await this._writeReg(_REG_CONTROL_1, 0x58);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// -- Interrupt API (Level 3) ------------------------------------------
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Read CONTROL_2/CONTROL_3, clear the set CTAF/CTBF/SF/AF/BSF flags
|
|
410
|
+
* (WTAF/BLF are read-only; enable bits untouched), and return the
|
|
411
|
+
* pre-clear status mask — test with the `PCF8523Full.SOURCE_*` constants.
|
|
412
|
+
* @returns {Promise<number>}
|
|
413
|
+
*/
|
|
414
|
+
async pollInterrupt() {
|
|
415
|
+
const raw = await this._readRegs(_REG_CONTROL_2, 2);
|
|
416
|
+
const ctrl2 = raw[0];
|
|
417
|
+
const ctrl3 = raw[1];
|
|
418
|
+
let status = 0;
|
|
419
|
+
if (ctrl2 & _C2_SF) status |= PCF8523Full.SOURCE_SECOND;
|
|
420
|
+
if (ctrl2 & (_C2_CTAF | _C2_WTAF)) status |= PCF8523Full.SOURCE_TIMER_A;
|
|
421
|
+
if (ctrl2 & _C2_CTBF) status |= PCF8523Full.SOURCE_TIMER_B;
|
|
422
|
+
if (ctrl2 & _C2_AF) status |= PCF8523Full.SOURCE_ALARM;
|
|
423
|
+
if (ctrl3 & _C3_BSF) status |= PCF8523Full.SOURCE_BATTERY_SWITCH;
|
|
424
|
+
if (ctrl3 & _C3_BLF) status |= PCF8523Full.SOURCE_BATTERY_LOW;
|
|
425
|
+
// Write 0 only to the flags seen set, 1 to the rest, so a flag that
|
|
426
|
+
// sets between the read and this write is not lost.
|
|
427
|
+
if (ctrl2 & _C2_CLEARABLE) {
|
|
428
|
+
await this._writeReg(_REG_CONTROL_2, (_C2_CLEARABLE & ~ctrl2) | (ctrl2 & _C2_ENABLES));
|
|
429
|
+
}
|
|
430
|
+
if (ctrl3 & _C3_BSF) {
|
|
431
|
+
await this._writeReg(_REG_CONTROL_3, ctrl3 & (_C3_PM_MASK | _C3_BSIE | _C3_BLIE));
|
|
432
|
+
}
|
|
433
|
+
return status;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Enable one or more interrupt sources. `SOURCE_TIMER_A` sets WTAIE or
|
|
438
|
+
* CTAIE depending on Timer A's configured mode — call
|
|
439
|
+
* `configureTimerA()` first.
|
|
440
|
+
* @param {number} source - Bitwise OR of `PCF8523Full.SOURCE_*`.
|
|
441
|
+
* @returns {Promise<void>}
|
|
442
|
+
*/
|
|
443
|
+
async enableInterrupt(source) {
|
|
444
|
+
await this._setInterruptEnables(source, true);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Disable one or more interrupt sources.
|
|
449
|
+
* @param {number} source - Bitwise OR of `PCF8523Full.SOURCE_*`.
|
|
450
|
+
* @returns {Promise<void>}
|
|
451
|
+
*/
|
|
452
|
+
async disableInterrupt(source) {
|
|
453
|
+
await this._setInterruptEnables(source, false);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
async _setInterruptEnables(source, enable) {
|
|
457
|
+
const S = PCF8523Full;
|
|
458
|
+
if (source & (S.SOURCE_SECOND | S.SOURCE_ALARM)) {
|
|
459
|
+
const bits = (source & S.SOURCE_SECOND ? _C1_SIE : 0) | (source & S.SOURCE_ALARM ? _C1_AIE : 0);
|
|
460
|
+
const ctrl1 = await this._readControl1();
|
|
461
|
+
await this._writeReg(_REG_CONTROL_1, enable ? ctrl1 | bits : ctrl1 & ~bits);
|
|
462
|
+
}
|
|
463
|
+
if (source & (S.SOURCE_TIMER_A | S.SOURCE_TIMER_B)) {
|
|
464
|
+
let bits = 0;
|
|
465
|
+
if (source & S.SOURCE_TIMER_A) {
|
|
466
|
+
if (enable) {
|
|
467
|
+
const tac = (await this._readReg(_REG_TMR_CLKOUT_CTRL)) & _TMR_TAC_MASK;
|
|
468
|
+
bits |= tac === _TMR_TAC_WATCHDOG ? _C2_WTAIE : _C2_CTAIE;
|
|
469
|
+
} else {
|
|
470
|
+
bits |= _C2_WTAIE | _C2_CTAIE;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
if (source & S.SOURCE_TIMER_B) bits |= _C2_CTBIE;
|
|
474
|
+
const enables = (await this._readReg(_REG_CONTROL_2)) & _C2_ENABLES;
|
|
475
|
+
await this._writeControl2(enable ? enables | bits : enables & ~bits);
|
|
476
|
+
}
|
|
477
|
+
if (source & (S.SOURCE_BATTERY_SWITCH | S.SOURCE_BATTERY_LOW)) {
|
|
478
|
+
const bits = (source & S.SOURCE_BATTERY_SWITCH ? _C3_BSIE : 0) | (source & S.SOURCE_BATTERY_LOW ? _C3_BLIE : 0);
|
|
479
|
+
const ctrl3 = await this._readReg(_REG_CONTROL_3);
|
|
480
|
+
await this._writeControl3(enable ? ctrl3 | bits : ctrl3 & ~bits);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Subscribe to interrupts. Uses `connection.intPin.onEdge()` when an
|
|
486
|
+
* INT-line InputPin is wired; falls back to a 5 ms polling loop
|
|
487
|
+
* otherwise. Call `disableClockOutput()` first if INT1 still carries
|
|
488
|
+
* CLKOUT.
|
|
489
|
+
* @param {function(number): void} callback - Called with the status mask (test against SOURCE_*).
|
|
490
|
+
* @returns {Promise<void>}
|
|
491
|
+
*/
|
|
492
|
+
async onInterrupt(callback) {
|
|
493
|
+
this._callback = callback;
|
|
494
|
+
const dispatch = async () => {
|
|
495
|
+
const status = await this.pollInterrupt();
|
|
496
|
+
if (status && this._callback) this._callback(status);
|
|
497
|
+
};
|
|
498
|
+
if (this._conn.intPin) {
|
|
499
|
+
this._edgeHandler = dispatch;
|
|
500
|
+
await this._conn.intPin.onEdge(this._edgeHandler, 'falling');
|
|
501
|
+
} else {
|
|
502
|
+
this._pollTimer = setInterval(dispatch, 5);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Unsubscribe from interrupts.
|
|
508
|
+
* @returns {Promise<void>}
|
|
509
|
+
*/
|
|
510
|
+
async offInterrupt() {
|
|
511
|
+
if (this._conn.intPin && this._edgeHandler) {
|
|
512
|
+
await this._conn.intPin.offEdge(this._edgeHandler);
|
|
513
|
+
this._edgeHandler = null;
|
|
514
|
+
}
|
|
515
|
+
if (this._pollTimer) {
|
|
516
|
+
clearInterval(this._pollTimer);
|
|
517
|
+
this._pollTimer = null;
|
|
518
|
+
}
|
|
519
|
+
this._callback = null;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** Second tick (SF). */
|
|
524
|
+
PCF8523Full.SOURCE_SECOND = 0x01;
|
|
525
|
+
/** Timer A timed out (CTAF or WTAF). */
|
|
526
|
+
PCF8523Full.SOURCE_TIMER_A = 0x02;
|
|
527
|
+
/** Timer B timed out (CTBF). */
|
|
528
|
+
PCF8523Full.SOURCE_TIMER_B = 0x04;
|
|
529
|
+
/** All enabled alarm fields matched (AF). */
|
|
530
|
+
PCF8523Full.SOURCE_ALARM = 0x08;
|
|
531
|
+
/** Battery switch-over occurred (BSF). */
|
|
532
|
+
PCF8523Full.SOURCE_BATTERY_SWITCH = 0x10;
|
|
533
|
+
/** Battery low (BLF). */
|
|
534
|
+
PCF8523Full.SOURCE_BATTERY_LOW = 0x20;
|
|
535
|
+
|
|
536
|
+
/** Fixed 7-bit I²C address. */
|
|
537
|
+
PCF8523Minimal.I2C_ADDRESS = 0x68;
|
|
538
|
+
|
|
539
|
+
module.exports = { PCF8523Minimal, PCF8523Full };
|