periph 1.6.0 → 1.6.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 +11 -0
- package/examples/gas/rrh62000/complete.js +74 -0
- package/examples/gas/rrh62000/demo.js +87 -0
- package/examples/gas/rrh62000/minimal.js +22 -0
- package/examples/gas/scd30/complete.js +73 -0
- package/examples/gas/scd30/demo.js +102 -0
- package/examples/gas/scd30/minimal.js +20 -0
- package/examples/gas/scd40/complete.js +76 -0
- package/examples/gas/scd40/demo.js +109 -0
- package/examples/gas/scd40/minimal.js +20 -0
- package/examples/light/tsl2561/complete.js +74 -0
- package/examples/light/tsl2561/demo.js +46 -0
- package/examples/light/tsl2561/minimal.js +22 -0
- package/examples/light/tsl2591/complete.js +90 -0
- package/examples/light/tsl2591/demo.js +119 -0
- package/examples/light/tsl2591/minimal.js +22 -0
- package/examples/light/vcnl4040/complete.js +102 -0
- package/examples/light/vcnl4040/demo.js +125 -0
- package/examples/light/vcnl4040/minimal.js +25 -0
- package/examples/light/vcnl4200/complete.js +101 -0
- package/examples/light/vcnl4200/demo.js +126 -0
- package/examples/light/vcnl4200/minimal.js +25 -0
- package/examples/rfid/st25dv/complete.js +94 -0
- package/examples/rfid/st25dv/demo.js +121 -0
- package/examples/rfid/st25dv/minimal.js +21 -0
- package/examples/rtc/bq32000/complete.js +53 -0
- package/examples/rtc/bq32000/demo.js +77 -0
- package/examples/rtc/bq32000/minimal.js +20 -0
- package/examples/rtc/ds1307/complete.js +54 -0
- package/examples/rtc/ds1307/demo.js +96 -0
- package/examples/rtc/ds1307/minimal.js +20 -0
- package/examples/rtc/ds1371/complete.js +53 -0
- package/examples/rtc/ds1371/demo.js +85 -0
- package/examples/rtc/ds1371/minimal.js +25 -0
- package/package.json +1 -1
- package/src/chips/gas/rrh62000.js +418 -0
- package/src/chips/gas/scd30.js +368 -0
- package/src/chips/gas/scd40.js +451 -0
- package/src/chips/light/tsl2561.js +445 -0
- package/src/chips/light/tsl2591.js +561 -0
- package/src/chips/light/vcnl4040.js +576 -0
- package/src/chips/light/vcnl4200.js +592 -0
- package/src/chips/rfid/st25dv.js +628 -0
- package/src/chips/rtc/bq32000.js +334 -0
- package/src/chips/rtc/ds1307.js +327 -0
- package/src/chips/rtc/ds1371.js +453 -0
- package/src/discovery/registry.js +11 -11
|
@@ -0,0 +1,628 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ST25DV04K / ST25DV16K / ST25DV64K dynamic NFC/RFID tag driver.
|
|
5
|
+
*
|
|
6
|
+
* The chip answers on two I²C addresses, both with 16-bit register addresses:
|
|
7
|
+
* 0x53 (user memory, dynamic registers, fast-transfer-mode mailbox) and 0x57
|
|
8
|
+
* (system configuration: static registers, UID, IC_REF, MEM_SIZE, I²C password).
|
|
9
|
+
* The driver therefore takes two register connections on the same bus, both
|
|
10
|
+
* created with a 2-byte register address width.
|
|
11
|
+
*
|
|
12
|
+
* EEPROM writes are self-timed (5 ms per 4-byte internal page); the chip does
|
|
13
|
+
* not acknowledge its address during the cycle, so every write is completed by
|
|
14
|
+
* ACK polling. While an RF reader accesses the EEPROM the chip NACKs I²C, so
|
|
15
|
+
* reads are retried a few times before rejecting.
|
|
16
|
+
* @module st25dv
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const _SYS_MEM_SIZE = 0x0014;
|
|
20
|
+
const _CHUNK = 16;
|
|
21
|
+
const _WRITE_TIMEOUT_MS = 50;
|
|
22
|
+
const _READ_RETRIES = 3;
|
|
23
|
+
|
|
24
|
+
// System area (E2 = 1)
|
|
25
|
+
const _SYS_GPO = 0x0000;
|
|
26
|
+
const _SYS_IT_TIME = 0x0001;
|
|
27
|
+
const _SYS_EH_MODE = 0x0002;
|
|
28
|
+
const _SYS_RFA1SS = 0x0004;
|
|
29
|
+
const _SYS_ENDA1 = 0x0005;
|
|
30
|
+
const _SYS_I2CSS = 0x000B;
|
|
31
|
+
const _SYS_LOCK_CCFILE = 0x000C;
|
|
32
|
+
const _SYS_MB_MODE = 0x000D;
|
|
33
|
+
const _SYS_MB_WDG = 0x000E;
|
|
34
|
+
const _SYS_UID = 0x0018;
|
|
35
|
+
const _SYS_IC_REV = 0x0020;
|
|
36
|
+
const _SYS_I2C_PWD = 0x0900;
|
|
37
|
+
|
|
38
|
+
// Dynamic registers (E2 = 0)
|
|
39
|
+
const _DYN_GPO_CTRL = 0x2000;
|
|
40
|
+
const _DYN_EH_CTRL = 0x2002;
|
|
41
|
+
const _DYN_RF_MNGT = 0x2003;
|
|
42
|
+
const _DYN_I2C_SSO = 0x2004;
|
|
43
|
+
const _DYN_IT_STS = 0x2005;
|
|
44
|
+
const _DYN_MB_CTRL = 0x2006;
|
|
45
|
+
const _DYN_MB_LEN = 0x2007;
|
|
46
|
+
const _MAILBOX = 0x2008;
|
|
47
|
+
const _MAILBOX_SIZE = 256;
|
|
48
|
+
|
|
49
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
50
|
+
|
|
51
|
+
/** Interrupt sources: bit layout of IT_STS_Dyn (0x2005). */
|
|
52
|
+
const ST25DVSource = Object.freeze({
|
|
53
|
+
RF_USER: 0x01,
|
|
54
|
+
RF_ACTIVITY: 0x02,
|
|
55
|
+
RF_INTERRUPT: 0x04,
|
|
56
|
+
FIELD_FALLING: 0x08,
|
|
57
|
+
FIELD_RISING: 0x10,
|
|
58
|
+
RF_PUT_MSG: 0x20,
|
|
59
|
+
RF_GET_MSG: 0x40,
|
|
60
|
+
RF_WRITE: 0x80,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* ST25DV dynamic NFC tag — minimal interface: user memory as a byte store.
|
|
65
|
+
*
|
|
66
|
+
* Call `init()` once: it reads MEM_SIZE, BLK_SIZE and IC_REF from the system
|
|
67
|
+
* area and derives the user memory size. No configuration is written; the chip
|
|
68
|
+
* keeps its delivery state (area 1 = the whole memory, no protection,
|
|
69
|
+
* GPO_EN = 1, field-change interrupt only). Reads and writes are split into
|
|
70
|
+
* transfers of at most 16 bytes aligned to 16-byte boundaries, so a transfer
|
|
71
|
+
* never crosses a 256-byte page or an area border; every write is ACK-polled.
|
|
72
|
+
*/
|
|
73
|
+
class ST25DVMinimal {
|
|
74
|
+
/**
|
|
75
|
+
* @param {object} user - RegisterConnection at 0x53 (2-byte register addresses).
|
|
76
|
+
* @param {object} system - RegisterConnection at 0x57 (2-byte register addresses).
|
|
77
|
+
* @param {boolean} [gpoActiveHigh=false] - true for -JF variants (CMOS GPO, rising edge),
|
|
78
|
+
* false for -IE variants (open drain, falling edge).
|
|
79
|
+
*/
|
|
80
|
+
constructor(user, system, gpoActiveHigh = false) {
|
|
81
|
+
this._user = user;
|
|
82
|
+
this._system = system;
|
|
83
|
+
this._gpoActiveHigh = gpoActiveHigh;
|
|
84
|
+
this._size = 0;
|
|
85
|
+
this._icRef = 0;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Read MEM_SIZE, BLK_SIZE and IC_REF and derive the memory size.
|
|
90
|
+
* @returns {Promise<void>}
|
|
91
|
+
*/
|
|
92
|
+
async init() {
|
|
93
|
+
const id = await this._read(this._system, _SYS_MEM_SIZE, 4);
|
|
94
|
+
const mem = id[0] | (id[1] << 8);
|
|
95
|
+
this._size = (mem + 1) * (id[2] + 1);
|
|
96
|
+
this._icRef = id[3];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async _read(conn, reg, length) {
|
|
100
|
+
for (let attempt = 1; ; attempt++) {
|
|
101
|
+
try {
|
|
102
|
+
return Buffer.from(await conn.readReg(reg, length));
|
|
103
|
+
} catch (e) {
|
|
104
|
+
if (attempt >= _READ_RETRIES) throw e;
|
|
105
|
+
await _sleep(5);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async _ackPoll(conn, timeoutMs) {
|
|
111
|
+
const deadline = Date.now() + timeoutMs;
|
|
112
|
+
for (;;) {
|
|
113
|
+
try {
|
|
114
|
+
await conn.writeReg(0, Buffer.alloc(0));
|
|
115
|
+
return true;
|
|
116
|
+
} catch (e) {
|
|
117
|
+
// NACK while the write cycle is running
|
|
118
|
+
}
|
|
119
|
+
if (Date.now() >= deadline) return false;
|
|
120
|
+
await _sleep(1);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async _commit(conn) {
|
|
125
|
+
if (!(await this._ackPoll(conn, _WRITE_TIMEOUT_MS))) {
|
|
126
|
+
throw new Error('ST25DV write cycle did not complete');
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
_checkRange(address, length) {
|
|
131
|
+
if (!Number.isInteger(address) || !Number.isInteger(length) ||
|
|
132
|
+
address < 0 || length < 0 || address + length > this._size) {
|
|
133
|
+
throw new RangeError(`range outside the user memory 0-${this._size - 1}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Read user memory.
|
|
139
|
+
* @param {number} address - Start byte address.
|
|
140
|
+
* @param {number} length - Number of bytes.
|
|
141
|
+
* @returns {Promise<Buffer>} `length` bytes.
|
|
142
|
+
* @throws {RangeError} If the range lies outside the user memory.
|
|
143
|
+
*/
|
|
144
|
+
async read(address, length) {
|
|
145
|
+
this._checkRange(address, length);
|
|
146
|
+
const parts = [];
|
|
147
|
+
while (length > 0) {
|
|
148
|
+
const n = Math.min(_CHUNK - (address % _CHUNK), length);
|
|
149
|
+
parts.push(await this._read(this._user, address, n));
|
|
150
|
+
address += n;
|
|
151
|
+
length -= n;
|
|
152
|
+
}
|
|
153
|
+
return Buffer.concat(parts);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Write user memory in 16-byte-aligned pieces, ACK-polling after each.
|
|
158
|
+
* @param {number} address - Start byte address.
|
|
159
|
+
* @param {Buffer|number[]} data - Bytes to write.
|
|
160
|
+
* @returns {Promise<void>}
|
|
161
|
+
* @throws {RangeError} If the range lies outside the user memory.
|
|
162
|
+
* @throws {Error} If a write is NACKed or a write cycle does not complete.
|
|
163
|
+
*/
|
|
164
|
+
async write(address, data) {
|
|
165
|
+
const buf = Buffer.from(data);
|
|
166
|
+
this._checkRange(address, buf.length);
|
|
167
|
+
let offset = 0;
|
|
168
|
+
while (offset < buf.length) {
|
|
169
|
+
const a = address + offset;
|
|
170
|
+
const n = Math.min(_CHUNK - (a % _CHUNK), buf.length - offset);
|
|
171
|
+
await this._user.writeReg(a, buf.subarray(offset, offset + n));
|
|
172
|
+
await this._commit(this._user);
|
|
173
|
+
offset += n;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* ST25DV full interface — identification, system area, password session,
|
|
180
|
+
* memory areas, GPO interrupts, energy harvesting, RF state, mailbox and a
|
|
181
|
+
* minimal NDEF writer.
|
|
182
|
+
*
|
|
183
|
+
* System-area writes need an open I²C security session (`presentPassword`).
|
|
184
|
+
* Bits 0-6 of GPO_CTRL_Dyn are read-only over I²C, so interrupt sources are
|
|
185
|
+
* configured in the static GPO register (session required); only GPO_EN can be
|
|
186
|
+
* switched at runtime without a session.
|
|
187
|
+
*/
|
|
188
|
+
class ST25DVFull extends ST25DVMinimal {
|
|
189
|
+
/**
|
|
190
|
+
* @param {object} user - RegisterConnection at 0x53.
|
|
191
|
+
* @param {object} system - RegisterConnection at 0x57.
|
|
192
|
+
* @param {boolean} [gpoActiveHigh=false] - true for -JF variants.
|
|
193
|
+
*/
|
|
194
|
+
constructor(user, system, gpoActiveHigh = false) {
|
|
195
|
+
super(user, system, gpoActiveHigh);
|
|
196
|
+
this._callback = null;
|
|
197
|
+
this._intPinUsed = null;
|
|
198
|
+
this._pollTimer = null;
|
|
199
|
+
this._polling = false;
|
|
200
|
+
this._edgeHandler = () => { this._service(); };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** @returns {number} User memory size in bytes ((MEM_SIZE + 1) × (BLK_SIZE + 1)). */
|
|
204
|
+
memorySize() { return this._size; }
|
|
205
|
+
|
|
206
|
+
/** @returns {number} IC_REF: 0x24 (ST25DV04K) or 0x26 (ST25DV16K/64K). */
|
|
207
|
+
icReference() { return this._icRef; }
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Read the 64-bit unique ID.
|
|
211
|
+
* @returns {Promise<Buffer>} 8 bytes, most significant first (E0 02 …).
|
|
212
|
+
*/
|
|
213
|
+
async uid() {
|
|
214
|
+
const raw = await this._read(this._system, _SYS_UID, 8);
|
|
215
|
+
return Buffer.from(raw).reverse();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** @returns {Promise<number>} IC_REV. */
|
|
219
|
+
async icRevision() {
|
|
220
|
+
return (await this._read(this._system, _SYS_IC_REV, 1))[0];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Raw read of the system configuration area (device 0x57).
|
|
225
|
+
* @param {number} address - System register address.
|
|
226
|
+
* @param {number} length - Number of bytes.
|
|
227
|
+
* @returns {Promise<Buffer>}
|
|
228
|
+
*/
|
|
229
|
+
async readSystem(address, length) {
|
|
230
|
+
return this._read(this._system, address, length);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Raw write to the system area, then ACK poll (session required).
|
|
235
|
+
* @param {number} address - System register address.
|
|
236
|
+
* @param {Buffer|number[]|number} data - Bytes, or one byte value.
|
|
237
|
+
* @returns {Promise<void>}
|
|
238
|
+
*/
|
|
239
|
+
async writeSystem(address, data) {
|
|
240
|
+
const buf = typeof data === 'number' ? Buffer.from([data & 0xFF]) : Buffer.from(data);
|
|
241
|
+
await this._system.writeReg(address, buf);
|
|
242
|
+
await this._commit(this._system);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async _sysByte(address) { return (await this._read(this._system, address, 1))[0]; }
|
|
246
|
+
|
|
247
|
+
async _dynByte(address) { return (await this._read(this._user, address, 1))[0]; }
|
|
248
|
+
|
|
249
|
+
async _writeDyn(address, value) { await this._user.writeReg(address, Buffer.from([value & 0xFF])); }
|
|
250
|
+
|
|
251
|
+
static _passwordFrame(password, code) {
|
|
252
|
+
const pw = Buffer.from(password);
|
|
253
|
+
if (pw.length !== 8) throw new RangeError('password must be 8 bytes');
|
|
254
|
+
return Buffer.concat([pw, Buffer.from([code]), pw]);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Present the I²C password (opens the security session).
|
|
259
|
+
* @param {Buffer|number[]} password - 8 bytes, most significant first (factory 00 × 8).
|
|
260
|
+
* @returns {Promise<boolean>} true if the session is open afterwards.
|
|
261
|
+
*/
|
|
262
|
+
async presentPassword(password) {
|
|
263
|
+
await this._system.writeReg(_SYS_I2C_PWD, ST25DVFull._passwordFrame(password, 0x09));
|
|
264
|
+
await this._ackPoll(this._system, _WRITE_TIMEOUT_MS);
|
|
265
|
+
return this.sessionOpen();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Change the I²C password (session must be open).
|
|
270
|
+
* @param {Buffer|number[]} password - New 8-byte password, most significant first.
|
|
271
|
+
* @returns {Promise<void>}
|
|
272
|
+
* @throws {Error} If no security session is open or the write fails.
|
|
273
|
+
*/
|
|
274
|
+
async writePassword(password) {
|
|
275
|
+
const frame = ST25DVFull._passwordFrame(password, 0x07);
|
|
276
|
+
if (!(await this.sessionOpen())) throw new Error('ST25DV security session is closed');
|
|
277
|
+
await this._system.writeReg(_SYS_I2C_PWD, frame);
|
|
278
|
+
await this._commit(this._system);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** @returns {Promise<boolean>} I2C_SSO_Dyn bit 0: the I²C security session is open. */
|
|
282
|
+
async sessionOpen() {
|
|
283
|
+
return ((await this._dynByte(_DYN_I2C_SSO)) & 0x01) !== 0;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Set ENDA1..3 (session required); area n ends at byte endBlock × 32 + 31.
|
|
288
|
+
* @param {number} area - 1-3.
|
|
289
|
+
* @param {number} endBlock - ENDAn value 0-255.
|
|
290
|
+
* @returns {Promise<void>}
|
|
291
|
+
*/
|
|
292
|
+
async setAreaEnd(area, endBlock) {
|
|
293
|
+
if (!(area >= 1 && area <= 3)) throw new RangeError('area must be 1-3');
|
|
294
|
+
if (!(endBlock >= 0 && endBlock <= 0xFF)) throw new RangeError('endBlock must be 0-255');
|
|
295
|
+
await this.writeSystem(_SYS_ENDA1 + 2 * (area - 1), endBlock);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Set the RF and I²C protection of an area (session required).
|
|
300
|
+
* @param {number} area - 1-4.
|
|
301
|
+
* @param {number} rf - RFAnSS 0-15 (bits 3:2 RW protection, bits 1:0 RF password).
|
|
302
|
+
* @param {number} i2c - I2CSS level 0-3 (bit 0 write, bit 1 read protection).
|
|
303
|
+
* @returns {Promise<void>}
|
|
304
|
+
*/
|
|
305
|
+
async setAreaProtection(area, rf, i2c) {
|
|
306
|
+
if (!(area >= 1 && area <= 4)) throw new RangeError('area must be 1-4');
|
|
307
|
+
if (!(rf >= 0 && rf <= 0x0F && i2c >= 0 && i2c <= 3)) throw new RangeError('rf must be 0-15 and i2c 0-3');
|
|
308
|
+
await this.writeSystem(_SYS_RFA1SS + 2 * (area - 1), rf);
|
|
309
|
+
const shift = 2 * (area - 1);
|
|
310
|
+
let i2css = await this._sysByte(_SYS_I2CSS);
|
|
311
|
+
i2css = (i2css & ~(0x03 << shift) & 0xFF) | (i2c << shift);
|
|
312
|
+
await this.writeSystem(_SYS_I2CSS, i2css);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Read the protection of an area.
|
|
317
|
+
* @param {number} area - 1-4.
|
|
318
|
+
* @returns {Promise<{rf: number, i2c: number}>}
|
|
319
|
+
*/
|
|
320
|
+
async getAreaProtection(area) {
|
|
321
|
+
if (!(area >= 1 && area <= 4)) throw new RangeError('area must be 1-4');
|
|
322
|
+
const rf = (await this._sysByte(_SYS_RFA1SS + 2 * (area - 1))) & 0x0F;
|
|
323
|
+
const i2c = ((await this._sysByte(_SYS_I2CSS)) >> (2 * (area - 1))) & 0x03;
|
|
324
|
+
return { rf, i2c };
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Write the static GPO register (session required); the chip copies it into GPO_CTRL_Dyn.
|
|
329
|
+
* @param {number} sources - Mask of GPO_* enable bits 0-6.
|
|
330
|
+
* @param {boolean} [enable=true] - Sets GPO_EN.
|
|
331
|
+
* @returns {Promise<void>}
|
|
332
|
+
*/
|
|
333
|
+
async setGpo(sources, enable = true) {
|
|
334
|
+
await this.writeSystem(_SYS_GPO, (sources & 0x7F) | (enable ? ST25DVFull.GPO_EN : 0));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** @returns {Promise<number>} Static GPO register (GPO_* mask). */
|
|
338
|
+
async getGpo() {
|
|
339
|
+
return this._sysByte(_SYS_GPO);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Switch the GPO output at runtime (GPO_CTRL_Dyn.GPO_EN, no session; bits 0-6 are read-only).
|
|
344
|
+
* @param {boolean} enable - true enables the output.
|
|
345
|
+
* @returns {Promise<void>}
|
|
346
|
+
*/
|
|
347
|
+
async setGpoRuntime(enable) {
|
|
348
|
+
await this._writeDyn(_DYN_GPO_CTRL, enable ? ST25DVFull.GPO_EN : 0);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Set IT_TIME (session required); pulse = 301 µs − value × 37.65 µs.
|
|
353
|
+
* @param {number} value - 0-7.
|
|
354
|
+
* @returns {Promise<void>}
|
|
355
|
+
*/
|
|
356
|
+
async setInterruptPulse(value) {
|
|
357
|
+
if (!(value >= 0 && value <= 7)) throw new RangeError('value must be 0-7');
|
|
358
|
+
await this.writeSystem(_SYS_IT_TIME, value);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** @returns {Promise<boolean>} EH_CTRL_Dyn.FIELD_ON: an RF field is present. */
|
|
362
|
+
async fieldPresent() {
|
|
363
|
+
return ((await this._dynByte(_DYN_EH_CTRL)) & 0x04) !== 0;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Set EH_CTRL_Dyn.EH_EN; write EH_MODE (session required) only when `forced` changes it.
|
|
368
|
+
* @param {boolean} enabled - Energy harvesting on now.
|
|
369
|
+
* @param {boolean} [forced=false] - Forced after power-up instead of on demand.
|
|
370
|
+
* @returns {Promise<void>}
|
|
371
|
+
*/
|
|
372
|
+
async setEnergyHarvesting(enabled, forced = false) {
|
|
373
|
+
const mode = forced ? 0 : 1;
|
|
374
|
+
if (((await this._sysByte(_SYS_EH_MODE)) & 0x01) !== mode) {
|
|
375
|
+
await this.writeSystem(_SYS_EH_MODE, mode);
|
|
376
|
+
}
|
|
377
|
+
await this._writeDyn(_DYN_EH_CTRL, enabled ? 0x01 : 0x00);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Set the runtime RF state RF_MNGT_Dyn (the static RF_MNGT is unchanged).
|
|
382
|
+
* @param {boolean} [disable=false] - RF_DISABLE.
|
|
383
|
+
* @param {boolean} [sleep=false] - RF_SLEEP.
|
|
384
|
+
* @returns {Promise<void>}
|
|
385
|
+
*/
|
|
386
|
+
async setRfState(disable = false, sleep = false) {
|
|
387
|
+
await this._writeDyn(_DYN_RF_MNGT, (disable ? 0x01 : 0) | (sleep ? 0x02 : 0));
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Set MB_MODE if needed (session required), then MB_CTRL_Dyn.MB_EN.
|
|
392
|
+
* @returns {Promise<void>}
|
|
393
|
+
*/
|
|
394
|
+
async enableMailbox() {
|
|
395
|
+
if (!((await this._sysByte(_SYS_MB_MODE)) & 0x01)) {
|
|
396
|
+
await this.writeSystem(_SYS_MB_MODE, 0x01);
|
|
397
|
+
}
|
|
398
|
+
await this._writeDyn(_DYN_MB_CTRL, 0x01);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** Clear MB_CTRL_Dyn.MB_EN (MB_MODE stays). @returns {Promise<void>} */
|
|
402
|
+
async disableMailbox() {
|
|
403
|
+
await this._writeDyn(_DYN_MB_CTRL, 0x00);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Decode MB_CTRL_Dyn.
|
|
408
|
+
* @returns {Promise<{enabled: boolean, hostPut: boolean, rfPut: boolean, hostMiss: boolean,
|
|
409
|
+
* rfMiss: boolean, hostCurrent: boolean, rfCurrent: boolean}>}
|
|
410
|
+
*/
|
|
411
|
+
async mailboxStatus() {
|
|
412
|
+
const v = await this._dynByte(_DYN_MB_CTRL);
|
|
413
|
+
return {
|
|
414
|
+
enabled: (v & 0x01) !== 0,
|
|
415
|
+
hostPut: (v & 0x02) !== 0,
|
|
416
|
+
rfPut: (v & 0x04) !== 0,
|
|
417
|
+
hostMiss: (v & 0x10) !== 0,
|
|
418
|
+
rfMiss: (v & 0x20) !== 0,
|
|
419
|
+
hostCurrent: (v & 0x40) !== 0,
|
|
420
|
+
rfCurrent: (v & 0x80) !== 0,
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Put a message into the mailbox (one write starting at 0x2008).
|
|
426
|
+
* @param {Buffer|number[]} data - 1-256 bytes.
|
|
427
|
+
* @returns {Promise<void>}
|
|
428
|
+
* @throws {RangeError} If the length is not 1-256.
|
|
429
|
+
* @throws {Error} If the mailbox is disabled or still holds a message.
|
|
430
|
+
*/
|
|
431
|
+
async mailboxWrite(data) {
|
|
432
|
+
const buf = Buffer.from(data);
|
|
433
|
+
if (buf.length < 1 || buf.length > _MAILBOX_SIZE) throw new RangeError('mailbox message must be 1-256 bytes');
|
|
434
|
+
const ctrl = await this._dynByte(_DYN_MB_CTRL);
|
|
435
|
+
if (!(ctrl & 0x01)) throw new Error('ST25DV mailbox is disabled');
|
|
436
|
+
if (ctrl & 0x06) throw new Error('ST25DV mailbox is busy');
|
|
437
|
+
await this._user.writeReg(_MAILBOX, buf);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Read the mailbox message.
|
|
442
|
+
* @returns {Promise<Buffer>} MB_LEN_Dyn + 1 bytes, or empty when no message is present.
|
|
443
|
+
*/
|
|
444
|
+
async mailboxRead() {
|
|
445
|
+
const ctrl = await this._dynByte(_DYN_MB_CTRL);
|
|
446
|
+
if (!(ctrl & 0x06)) return Buffer.alloc(0);
|
|
447
|
+
const length = (await this._dynByte(_DYN_MB_LEN)) + 1;
|
|
448
|
+
const parts = [];
|
|
449
|
+
for (let offset = 0; offset < length; offset += _CHUNK) {
|
|
450
|
+
parts.push(await this._read(this._user, _MAILBOX + offset, Math.min(_CHUNK, length - offset)));
|
|
451
|
+
}
|
|
452
|
+
return Buffer.concat(parts);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Set MB_WDG (session required); watchdog = 2^(value − 1) × 30 ms, 0 disables it.
|
|
457
|
+
* @param {number} value - 0-7.
|
|
458
|
+
* @returns {Promise<void>}
|
|
459
|
+
*/
|
|
460
|
+
async setMailboxWatchdog(value) {
|
|
461
|
+
if (!(value >= 0 && value <= 7)) throw new RangeError('value must be 0-7');
|
|
462
|
+
await this.writeSystem(_SYS_MB_WDG, value);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Write LOCK_CCFILE (session required): RF write protection of blocks 0 and 1.
|
|
467
|
+
* @param {boolean} [lock=true] - true locks both blocks.
|
|
468
|
+
* @returns {Promise<void>}
|
|
469
|
+
*/
|
|
470
|
+
async lockCcfile(lock = true) {
|
|
471
|
+
await this.writeSystem(_SYS_LOCK_CCFILE, lock ? 0x03 : 0x00);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
_capabilityContainer() {
|
|
475
|
+
if (this._size <= 2048) {
|
|
476
|
+
return Buffer.from([0xE1, 0x40, Math.min(this._size >> 3, 0xFF), 0x01]);
|
|
477
|
+
}
|
|
478
|
+
const mlen = (this._size >> 3) - 1;
|
|
479
|
+
return Buffer.from([0xE2, 0x40, 0x00, 0x01, 0x00, 0x00, (mlen >> 8) & 0xFF, mlen & 0xFF]);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Write a Type 5 capability container and one NDEF TLV (03 len … FE; 03 FF hi lo … for ≥ 255 bytes).
|
|
484
|
+
* @param {Buffer|number[]} message - Encoded NDEF message.
|
|
485
|
+
* @returns {Promise<void>}
|
|
486
|
+
* @throws {RangeError} If the message does not fit.
|
|
487
|
+
*/
|
|
488
|
+
async writeNdef(message) {
|
|
489
|
+
const msg = Buffer.from(message);
|
|
490
|
+
const tlv = msg.length < 0xFF
|
|
491
|
+
? Buffer.from([0x03, msg.length])
|
|
492
|
+
: Buffer.from([0x03, 0xFF, (msg.length >> 8) & 0xFF, msg.length & 0xFF]);
|
|
493
|
+
const image = Buffer.concat([this._capabilityContainer(), tlv, msg, Buffer.from([0xFE])]);
|
|
494
|
+
if (image.length > this._size) throw new RangeError('NDEF message too large');
|
|
495
|
+
await this.write(0, image);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Read the message of the first NDEF TLV.
|
|
500
|
+
* @returns {Promise<Buffer>} The message, or empty when no CC or NDEF TLV is found.
|
|
501
|
+
*/
|
|
502
|
+
async readNdef() {
|
|
503
|
+
const cc = await this.read(0, 4);
|
|
504
|
+
let pos;
|
|
505
|
+
if (cc[0] === 0xE1) pos = 4;
|
|
506
|
+
else if (cc[0] === 0xE2) pos = 8;
|
|
507
|
+
else return Buffer.alloc(0);
|
|
508
|
+
while (pos < this._size) {
|
|
509
|
+
const t = (await this.read(pos, 1))[0];
|
|
510
|
+
if (t === 0x00) { pos++; continue; }
|
|
511
|
+
if (t === 0xFE || pos + 2 > this._size) return Buffer.alloc(0);
|
|
512
|
+
let length = (await this.read(pos + 1, 1))[0];
|
|
513
|
+
pos += 2;
|
|
514
|
+
if (length === 0xFF) {
|
|
515
|
+
if (pos + 2 > this._size) return Buffer.alloc(0);
|
|
516
|
+
const lb = await this.read(pos, 2);
|
|
517
|
+
length = (lb[0] << 8) | lb[1];
|
|
518
|
+
pos += 2;
|
|
519
|
+
}
|
|
520
|
+
if (t === 0x03) {
|
|
521
|
+
if (pos + length > this._size) return Buffer.alloc(0);
|
|
522
|
+
return this.read(pos, length);
|
|
523
|
+
}
|
|
524
|
+
pos += length;
|
|
525
|
+
}
|
|
526
|
+
return Buffer.alloc(0);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* ACK poll on 0x53 until the write cycle has finished.
|
|
531
|
+
* @returns {Promise<void>}
|
|
532
|
+
* @throws {Error} On timeout.
|
|
533
|
+
*/
|
|
534
|
+
async waitWriteComplete() {
|
|
535
|
+
await this._commit(this._user);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
static _sourceToGpo(source) {
|
|
539
|
+
let gpo = source & 0x07;
|
|
540
|
+
if (source & (ST25DVSource.FIELD_RISING | ST25DVSource.FIELD_FALLING)) gpo |= 0x08;
|
|
541
|
+
gpo |= (source >> 1) & 0x70;
|
|
542
|
+
return gpo;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Enable GPO sources in the static GPO register plus GPO_EN (session required).
|
|
547
|
+
* FIELD_RISING and FIELD_FALLING share FIELD_CHANGE_EN.
|
|
548
|
+
* @param {number} source - ST25DVSource mask.
|
|
549
|
+
* @returns {Promise<void>}
|
|
550
|
+
*/
|
|
551
|
+
async enableInterrupt(source) {
|
|
552
|
+
const gpo = (await this.getGpo()) | ST25DVFull._sourceToGpo(source) | ST25DVFull.GPO_EN;
|
|
553
|
+
await this.writeSystem(_SYS_GPO, gpo);
|
|
554
|
+
await this.setGpoRuntime(true);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Clear GPO sources in the static GPO register (session required).
|
|
559
|
+
* @param {number} source - ST25DVSource mask.
|
|
560
|
+
* @returns {Promise<void>}
|
|
561
|
+
*/
|
|
562
|
+
async disableInterrupt(source) {
|
|
563
|
+
const gpo = (await this.getGpo()) & ~ST25DVFull._sourceToGpo(source) & 0xFF;
|
|
564
|
+
await this.writeSystem(_SYS_GPO, gpo);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* Read IT_STS_Dyn (cleared on read).
|
|
569
|
+
* @returns {Promise<number>} ST25DVSource mask, 0 = none.
|
|
570
|
+
*/
|
|
571
|
+
async pollInterrupt() {
|
|
572
|
+
return (await this._dynByte(_DYN_IT_STS)) & 0xFF;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Subscribe to GPO events: the falling (-IE) or rising (-JF) edge via `intPin` (or the user
|
|
577
|
+
* connection's pin), otherwise a 10 ms polling timer on IT_STS_Dyn.
|
|
578
|
+
* @param {function(number): void} callback - Called with the ST25DVSource mask.
|
|
579
|
+
* @param {object|null} [intPin=null] - Optional InputPin wired to GPO.
|
|
580
|
+
* @returns {Promise<void>}
|
|
581
|
+
*/
|
|
582
|
+
async onInterrupt(callback, intPin = null) {
|
|
583
|
+
await this.offInterrupt();
|
|
584
|
+
this._callback = callback;
|
|
585
|
+
const ipin = intPin ?? this._user.intPin ?? null;
|
|
586
|
+
this._intPinUsed = ipin;
|
|
587
|
+
if (ipin) {
|
|
588
|
+
await ipin.onEdge(this._edgeHandler, this._gpoActiveHigh ? 'rising' : 'falling');
|
|
589
|
+
} else {
|
|
590
|
+
this._pollTimer = setInterval(() => { this._service(); }, 10);
|
|
591
|
+
if (this._pollTimer.unref) this._pollTimer.unref();
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/** Unsubscribe and stop delivery. @returns {Promise<void>} */
|
|
596
|
+
async offInterrupt() {
|
|
597
|
+
if (this._intPinUsed) {
|
|
598
|
+
await this._intPinUsed.offEdge(this._edgeHandler);
|
|
599
|
+
this._intPinUsed = null;
|
|
600
|
+
}
|
|
601
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
602
|
+
this._callback = null;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
async _service() {
|
|
606
|
+
if (this._polling) return;
|
|
607
|
+
this._polling = true;
|
|
608
|
+
try {
|
|
609
|
+
const status = await this.pollInterrupt();
|
|
610
|
+
if (status && this._callback) this._callback(status);
|
|
611
|
+
} catch (e) {
|
|
612
|
+
/* a failed poll is retried on the next edge / tick */
|
|
613
|
+
} finally {
|
|
614
|
+
this._polling = false;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
ST25DVFull.GPO_RF_USER = 0x01;
|
|
620
|
+
ST25DVFull.GPO_RF_ACTIVITY = 0x02;
|
|
621
|
+
ST25DVFull.GPO_RF_INTERRUPT = 0x04;
|
|
622
|
+
ST25DVFull.GPO_FIELD_CHANGE = 0x08;
|
|
623
|
+
ST25DVFull.GPO_RF_PUT_MSG = 0x10;
|
|
624
|
+
ST25DVFull.GPO_RF_GET_MSG = 0x20;
|
|
625
|
+
ST25DVFull.GPO_RF_WRITE = 0x40;
|
|
626
|
+
ST25DVFull.GPO_EN = 0x80;
|
|
627
|
+
|
|
628
|
+
module.exports = { ST25DVMinimal, ST25DVFull, ST25DVSource };
|