periph 1.4.0 → 1.5.0
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 +27 -0
- package/examples/adc_dac/ad5251/complete.js +66 -0
- package/examples/adc_dac/ad5251/demo.js +69 -0
- package/examples/adc_dac/ad5251/minimal.js +23 -0
- package/examples/adc_dac/ad5252/complete.js +66 -0
- package/examples/adc_dac/ad5252/demo.js +69 -0
- package/examples/adc_dac/ad5252/minimal.js +23 -0
- package/examples/adc_dac/ads7828/complete.js +46 -0
- package/examples/adc_dac/ads7828/demo.js +45 -0
- package/examples/adc_dac/ads7828/minimal.js +17 -0
- package/examples/adc_dac/cat5171/complete.js +52 -0
- package/examples/adc_dac/cat5171/demo.js +59 -0
- package/examples/adc_dac/cat5171/minimal.js +23 -0
- package/examples/adc_dac/cs43l22/complete.js +88 -0
- package/examples/adc_dac/cs43l22/demo.js +56 -0
- package/examples/adc_dac/cs43l22/minimal.js +20 -0
- package/examples/adc_dac/ds1881/complete.js +62 -0
- package/examples/adc_dac/ds1881/demo.js +61 -0
- package/examples/adc_dac/ds1881/minimal.js +25 -0
- package/examples/adc_dac/ds3502/complete.js +46 -0
- package/examples/adc_dac/ds3502/demo.js +54 -0
- package/examples/adc_dac/ds3502/minimal.js +28 -0
- package/examples/adc_dac/mcp3422/complete.js +48 -0
- package/examples/adc_dac/mcp3422/demo.js +49 -0
- package/examples/adc_dac/mcp3422/minimal.js +25 -0
- package/examples/display/sh1106/complete.js +101 -0
- package/examples/display/sh1106/demo.js +68 -0
- package/examples/display/sh1106/minimal.js +25 -0
- package/examples/display/ssd1305/complete.js +129 -0
- package/examples/display/ssd1305/demo.js +71 -0
- package/examples/display/ssd1305/minimal.js +25 -0
- package/examples/display/ssd1306/complete.js +115 -0
- package/examples/display/ssd1306/demo.js +70 -0
- package/examples/display/ssd1306/minimal.js +25 -0
- package/examples/environmental/am2315/complete.js +34 -0
- package/examples/environmental/am2315/demo.js +46 -0
- package/examples/environmental/am2315/minimal.js +21 -0
- package/examples/humidity/htu21df/complete.js +49 -0
- package/examples/humidity/htu21df/demo.js +50 -0
- package/examples/humidity/htu21df/minimal.js +22 -0
- package/examples/humidity/sht31/complete.js +53 -0
- package/examples/humidity/sht31/demo.js +53 -0
- package/examples/humidity/sht31/minimal.js +21 -0
- package/examples/humidity/si7021/complete.js +55 -0
- package/examples/humidity/si7021/demo.js +58 -0
- package/examples/humidity/si7021/minimal.js +21 -0
- package/examples/imu/bno055/complete.js +85 -0
- package/examples/imu/bno055/demo.js +70 -0
- package/examples/imu/bno055/minimal.js +20 -0
- package/examples/io_expander/pca6408a/complete.js +63 -0
- package/examples/io_expander/pca6408a/demo.js +58 -0
- package/examples/io_expander/pca6408a/minimal.js +20 -0
- package/examples/io_expander/pca6416a/complete.js +68 -0
- package/examples/io_expander/pca6416a/demo.js +60 -0
- package/examples/io_expander/pca6416a/minimal.js +20 -0
- package/examples/io_expander/pca9536/complete.js +63 -0
- package/examples/io_expander/pca9536/demo.js +57 -0
- package/examples/io_expander/pca9536/minimal.js +20 -0
- package/examples/io_expander/pca9539/complete.js +68 -0
- package/examples/io_expander/pca9539/demo.js +60 -0
- package/examples/io_expander/pca9539/minimal.js +20 -0
- package/examples/light/max44009/complete.js +56 -0
- package/examples/light/max44009/demo.js +49 -0
- package/examples/light/max44009/minimal.js +22 -0
- package/examples/memory/at24c02n/complete.js +39 -0
- package/examples/memory/at24c02n/demo.js +39 -0
- package/examples/memory/at24c02n/minimal.js +15 -0
- package/examples/memory/at24c64/complete.js +39 -0
- package/examples/memory/at24c64/demo.js +39 -0
- package/examples/memory/at24c64/minimal.js +15 -0
- package/examples/memory/cat24c512/complete.js +39 -0
- package/examples/memory/cat24c512/demo.js +39 -0
- package/examples/memory/cat24c512/minimal.js +15 -0
- package/examples/other/pca9541/complete.js +52 -0
- package/examples/other/pca9541/demo.js +61 -0
- package/examples/other/pca9541/minimal.js +23 -0
- package/examples/temperature/tmp102/complete.js +74 -0
- package/examples/temperature/tmp102/demo.js +71 -0
- package/examples/temperature/tmp102/minimal.js +20 -0
- package/examples/tof/vl6180x/complete.js +62 -0
- package/examples/tof/vl6180x/demo.js +77 -0
- package/examples/tof/vl6180x/minimal.js +23 -0
- package/package.json +1 -1
- package/src/chips/adc_dac/ad5251.js +345 -0
- package/src/chips/adc_dac/ad5252.js +345 -0
- package/src/chips/adc_dac/ads7828.js +225 -0
- package/src/chips/adc_dac/cat5171.js +213 -0
- package/src/chips/adc_dac/cs43l22.js +645 -0
- package/src/chips/adc_dac/ds1881.js +357 -0
- package/src/chips/adc_dac/ds3502.js +241 -0
- package/src/chips/adc_dac/mcp3422.js +312 -0
- package/src/chips/display/mono_oled.js +182 -0
- package/src/chips/display/sh1106.js +496 -0
- package/src/chips/display/ssd1305.js +655 -0
- package/src/chips/display/ssd1306.js +458 -0
- package/src/chips/environmental/am2315.js +237 -0
- package/src/chips/humidity/htu21df.js +222 -0
- package/src/chips/humidity/sht31.js +284 -0
- package/src/chips/humidity/si7021.js +282 -0
- package/src/chips/imu/bno055.js +496 -0
- package/src/chips/io_expander/pca6408a.js +407 -0
- package/src/chips/io_expander/pca6416a.js +455 -0
- package/src/chips/io_expander/pca9536.js +398 -0
- package/src/chips/io_expander/pca9539.js +457 -0
- package/src/chips/light/max44009.js +336 -0
- package/src/chips/memory/at24c02n.js +220 -0
- package/src/chips/memory/at24c64.js +220 -0
- package/src/chips/memory/cat24c512.js +221 -0
- package/src/chips/other/pca9541.js +315 -0
- package/src/chips/temperature/tmp102.js +361 -0
- package/src/chips/tof/vl6180x.js +580 -0
- package/src/discovery/registry.js +65 -13
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const SIZE = 0x2000;
|
|
4
|
+
const PAGE = 32;
|
|
5
|
+
const WRITE_TIMEOUT_MS = 25; // tWR = 10 ms max (20 ms at 1.8 V), plus margin
|
|
6
|
+
|
|
7
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
8
|
+
|
|
9
|
+
function _checkAddress(address) {
|
|
10
|
+
if (!Number.isInteger(address) || address < 0 || address >= SIZE) {
|
|
11
|
+
throw new RangeError('address out of range 0-8191');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function _checkRange(address, length) {
|
|
16
|
+
if (!Number.isInteger(address) || !Number.isInteger(length) ||
|
|
17
|
+
length < 0 || address < 0 || address + length > SIZE) {
|
|
18
|
+
throw new RangeError('range outside 0-8191');
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* AT24C64 64 Kbit (8 KiB) I2C EEPROM — minimal interface.
|
|
24
|
+
*
|
|
25
|
+
* Self-timed writes (10 ms max (20 ms at 1.8 V)) are completed by ACK polling: after every
|
|
26
|
+
* write the driver polls the device address until the chip answers again, so
|
|
27
|
+
* a resolved write is always committed. The chip has no configuration
|
|
28
|
+
* registers; it is ready after power-up.
|
|
29
|
+
*
|
|
30
|
+
* Memory layout: 0x0000-0x1FFF, 13-bit address, big-endian on the wire,
|
|
31
|
+
* 256 pages of 32 bytes.
|
|
32
|
+
*
|
|
33
|
+
* Writes are ignored by the chip while the WP pin is high (hardware only;
|
|
34
|
+
* the driver cannot detect it — verify with a read-back if WP is wired).
|
|
35
|
+
*/
|
|
36
|
+
class AT24C64Minimal {
|
|
37
|
+
/**
|
|
38
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection with regBytes=2, pointing at the device (address 0x50-0x57).
|
|
39
|
+
*/
|
|
40
|
+
constructor(connection) {
|
|
41
|
+
this._conn = connection;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** ACK-poll: an address-only write is acknowledged once the write cycle has ended. */
|
|
45
|
+
async _waitReady(timeoutMs) {
|
|
46
|
+
const deadline = Date.now() + timeoutMs;
|
|
47
|
+
for (;;) {
|
|
48
|
+
try {
|
|
49
|
+
await this._conn.writeReg(0, Buffer.alloc(0));
|
|
50
|
+
return true;
|
|
51
|
+
} catch (e) {
|
|
52
|
+
// NACK while the write cycle is running
|
|
53
|
+
}
|
|
54
|
+
if (Date.now() >= deadline) return false;
|
|
55
|
+
await _sleep(1);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async _commit() {
|
|
60
|
+
if (!(await this._waitReady(WRITE_TIMEOUT_MS))) {
|
|
61
|
+
throw new Error('AT24C64 write cycle did not complete');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Read a single byte (random read).
|
|
67
|
+
* @param {number} address - Memory address 0-8191.
|
|
68
|
+
* @returns {Promise<number>} Byte value 0-255.
|
|
69
|
+
* @throws {RangeError} If the address is outside 0-8191.
|
|
70
|
+
*/
|
|
71
|
+
async readByte(address) {
|
|
72
|
+
_checkAddress(address);
|
|
73
|
+
return (await this._conn.readReg(address, 1))[0];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Write a single byte, then ACK-poll until the write cycle completes.
|
|
78
|
+
* @param {number} address - Memory address 0-8191.
|
|
79
|
+
* @param {number} value - Byte value 0-255.
|
|
80
|
+
* @returns {Promise<void>}
|
|
81
|
+
* @throws {RangeError} If the address is outside 0-8191.
|
|
82
|
+
* @throws {Error} If the chip does not answer again within the write-cycle timeout.
|
|
83
|
+
*/
|
|
84
|
+
async writeByte(address, value) {
|
|
85
|
+
_checkAddress(address);
|
|
86
|
+
await this._conn.writeReg(address, value & 0xFF);
|
|
87
|
+
await this._commit();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* AT24C64 full interface — extends minimal with block access and page writes.
|
|
93
|
+
*
|
|
94
|
+
* Writes are split at 32-byte page boundaries (a write crossing a boundary
|
|
95
|
+
* would wrap inside the page and corrupt data); ranges that would run past
|
|
96
|
+
* 0x1FFF are rejected instead of wrapping to 0x0000 as the hardware would.
|
|
97
|
+
*
|
|
98
|
+
*/
|
|
99
|
+
class AT24C64Full extends AT24C64Minimal {
|
|
100
|
+
/**
|
|
101
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection with regBytes=2 (address 0x50-0x57).
|
|
102
|
+
*/
|
|
103
|
+
constructor(connection) {
|
|
104
|
+
super(connection);
|
|
105
|
+
this._maxChunk = 30;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Maximum data bytes per bus transaction (default 30; Arduino Wire limits transfers to 32 bytes including the 2 address bytes).
|
|
110
|
+
* @type {number}
|
|
111
|
+
*/
|
|
112
|
+
get maxChunk() { return this._maxChunk; }
|
|
113
|
+
|
|
114
|
+
set maxChunk(value) {
|
|
115
|
+
if (!Number.isInteger(value) || value < 1) throw new RangeError('maxChunk must be >= 1');
|
|
116
|
+
this._maxChunk = value;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Array size in bytes.
|
|
121
|
+
* @returns {number} 8192.
|
|
122
|
+
*/
|
|
123
|
+
size() { return SIZE; }
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Write page size in bytes.
|
|
127
|
+
* @returns {number} 32.
|
|
128
|
+
*/
|
|
129
|
+
pageSize() { return PAGE; }
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* ACK-poll until the chip answers its device address again.
|
|
133
|
+
* @param {number} timeoutMs - Maximum time to poll in milliseconds.
|
|
134
|
+
* @returns {Promise<boolean>} True if the chip responded within the timeout.
|
|
135
|
+
*/
|
|
136
|
+
async waitReady(timeoutMs) {
|
|
137
|
+
return this._waitReady(timeoutMs);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Sequential read, split into transactions of at most maxChunk bytes.
|
|
142
|
+
* @param {number} address - Starting address 0-8191.
|
|
143
|
+
* @param {number} length - Number of bytes to read.
|
|
144
|
+
* @returns {Promise<Buffer>} `length` bytes.
|
|
145
|
+
* @throws {RangeError} If address..address+length is not inside 0x0000-0x1FFF.
|
|
146
|
+
*/
|
|
147
|
+
async read(address, length) {
|
|
148
|
+
_checkRange(address, length);
|
|
149
|
+
const parts = [];
|
|
150
|
+
while (length > 0) {
|
|
151
|
+
const n = Math.min(length, this._maxChunk);
|
|
152
|
+
parts.push(await this._conn.readReg(address, n));
|
|
153
|
+
address += n;
|
|
154
|
+
length -= n;
|
|
155
|
+
}
|
|
156
|
+
return Buffer.concat(parts);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Write one page (or part of it) in a single transaction, then ACK-poll.
|
|
161
|
+
* @param {number} address - Starting address 0-8191.
|
|
162
|
+
* @param {Buffer|number[]} data - 1-32 bytes, all inside the same 32-byte page.
|
|
163
|
+
* @returns {Promise<void>}
|
|
164
|
+
* @throws {RangeError} If the data crosses a page boundary, is longer than a page, or runs outside 0x0000-0x1FFF.
|
|
165
|
+
*/
|
|
166
|
+
async writePage(address, data) {
|
|
167
|
+
const buf = Buffer.from(data);
|
|
168
|
+
_checkRange(address, buf.length);
|
|
169
|
+
if (buf.length > PAGE || (address % PAGE) + buf.length > PAGE) {
|
|
170
|
+
throw new RangeError('write crosses a page boundary');
|
|
171
|
+
}
|
|
172
|
+
if (buf.length === 0) return;
|
|
173
|
+
await this._conn.writeReg(address, buf);
|
|
174
|
+
await this._commit();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Write `data` starting at `address`, split at page boundaries. Each chunk is
|
|
179
|
+
* at most maxChunk bytes and never crosses a 32-byte page boundary; the write
|
|
180
|
+
* cycle is ACK-polled after every chunk.
|
|
181
|
+
* @param {number} address - Starting address 0-8191.
|
|
182
|
+
* @param {Buffer|number[]} data - Bytes to write.
|
|
183
|
+
* @returns {Promise<void>}
|
|
184
|
+
* @throws {RangeError} If address..address+data.length is not inside 0x0000-0x1FFF.
|
|
185
|
+
*/
|
|
186
|
+
async write(address, data) {
|
|
187
|
+
const buf = Buffer.from(data);
|
|
188
|
+
_checkRange(address, buf.length);
|
|
189
|
+
let off = 0;
|
|
190
|
+
while (off < buf.length) {
|
|
191
|
+
const room = PAGE - ((address + off) % PAGE);
|
|
192
|
+
const n = Math.min(room, this._maxChunk, buf.length - off);
|
|
193
|
+
await this._conn.writeReg(address + off, buf.subarray(off, off + n));
|
|
194
|
+
await this._commit();
|
|
195
|
+
off += n;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Write `value` over a range, page by page.
|
|
201
|
+
* @param {number} address - Starting address 0-8191.
|
|
202
|
+
* @param {number} length - Number of bytes to fill.
|
|
203
|
+
* @param {number} [value=0] - Byte value 0-255.
|
|
204
|
+
* @returns {Promise<void>}
|
|
205
|
+
* @throws {RangeError} If address..address+length is not inside 0x0000-0x1FFF.
|
|
206
|
+
*/
|
|
207
|
+
async fill(address, length, value = 0) {
|
|
208
|
+
_checkRange(address, length);
|
|
209
|
+
let done = 0;
|
|
210
|
+
while (done < length) {
|
|
211
|
+
const room = PAGE - ((address + done) % PAGE);
|
|
212
|
+
const n = Math.min(room, this._maxChunk, length - done);
|
|
213
|
+
await this._conn.writeReg(address + done, Buffer.alloc(n, value & 0xFF));
|
|
214
|
+
await this._commit();
|
|
215
|
+
done += n;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
module.exports = { AT24C64Minimal, AT24C64Full };
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const SIZE = 0x10000;
|
|
4
|
+
const PAGE = 128;
|
|
5
|
+
const WRITE_TIMEOUT_MS = 10; // tWR = 5 ms max, plus margin
|
|
6
|
+
|
|
7
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
8
|
+
|
|
9
|
+
function _checkAddress(address) {
|
|
10
|
+
if (!Number.isInteger(address) || address < 0 || address >= SIZE) {
|
|
11
|
+
throw new RangeError('address out of range 0-65535');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function _checkRange(address, length) {
|
|
16
|
+
if (!Number.isInteger(address) || !Number.isInteger(length) ||
|
|
17
|
+
length < 0 || address < 0 || address + length > SIZE) {
|
|
18
|
+
throw new RangeError('range outside 0-65535');
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* CAT24C512 512 Kbit (64 KiB) I2C EEPROM — minimal interface.
|
|
24
|
+
*
|
|
25
|
+
* Self-timed writes (5 ms max) are completed by ACK polling: after every
|
|
26
|
+
* write the driver polls the device address until the chip answers again, so
|
|
27
|
+
* a resolved write is always committed. The chip has no configuration
|
|
28
|
+
* registers; it is ready within 1 ms of power-up.
|
|
29
|
+
*
|
|
30
|
+
* Memory layout: 0x0000-0xFFFF, 16-bit address, big-endian on the wire,
|
|
31
|
+
* 512 pages of 128 bytes.
|
|
32
|
+
*
|
|
33
|
+
* Writes are ignored by the chip while the WP pin is high (hardware only;
|
|
34
|
+
* the driver cannot detect it — verify with a read-back if WP is wired).
|
|
35
|
+
*/
|
|
36
|
+
class CAT24C512Minimal {
|
|
37
|
+
/**
|
|
38
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection with regBytes=2, pointing at the device (address 0x50-0x57).
|
|
39
|
+
*/
|
|
40
|
+
constructor(connection) {
|
|
41
|
+
this._conn = connection;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** ACK-poll: an address-only write is acknowledged once the write cycle has ended. */
|
|
45
|
+
async _waitReady(timeoutMs) {
|
|
46
|
+
const deadline = Date.now() + timeoutMs;
|
|
47
|
+
for (;;) {
|
|
48
|
+
try {
|
|
49
|
+
await this._conn.writeReg(0, Buffer.alloc(0));
|
|
50
|
+
return true;
|
|
51
|
+
} catch (e) {
|
|
52
|
+
// NACK while the write cycle is running
|
|
53
|
+
}
|
|
54
|
+
if (Date.now() >= deadline) return false;
|
|
55
|
+
await _sleep(1);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async _commit() {
|
|
60
|
+
if (!(await this._waitReady(WRITE_TIMEOUT_MS))) {
|
|
61
|
+
throw new Error('CAT24C512 write cycle did not complete');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Read a single byte (random read).
|
|
67
|
+
* @param {number} address - Memory address 0-65535.
|
|
68
|
+
* @returns {Promise<number>} Byte value 0-255.
|
|
69
|
+
* @throws {RangeError} If the address is outside 0-65535.
|
|
70
|
+
*/
|
|
71
|
+
async readByte(address) {
|
|
72
|
+
_checkAddress(address);
|
|
73
|
+
return (await this._conn.readReg(address, 1))[0];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Write a single byte, then ACK-poll until the write cycle completes.
|
|
78
|
+
* @param {number} address - Memory address 0-65535.
|
|
79
|
+
* @param {number} value - Byte value 0-255.
|
|
80
|
+
* @returns {Promise<void>}
|
|
81
|
+
* @throws {RangeError} If the address is outside 0-65535.
|
|
82
|
+
* @throws {Error} If the chip does not answer again within the write-cycle timeout.
|
|
83
|
+
*/
|
|
84
|
+
async writeByte(address, value) {
|
|
85
|
+
_checkAddress(address);
|
|
86
|
+
await this._conn.writeReg(address, value & 0xFF);
|
|
87
|
+
await this._commit();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* CAT24C512 full interface — extends minimal with block access and page writes.
|
|
93
|
+
*
|
|
94
|
+
* Writes are split at 128-byte page boundaries (a write crossing a boundary
|
|
95
|
+
* would wrap inside the page and corrupt data); ranges that would run past
|
|
96
|
+
* 0xFFFF are rejected instead of wrapping to 0x0000 as the hardware would.
|
|
97
|
+
* For maximum endurance (on-chip ECC re-programs 4-byte groups) write
|
|
98
|
+
* 4-byte-aligned multiples of 4 bytes.
|
|
99
|
+
*/
|
|
100
|
+
class CAT24C512Full extends CAT24C512Minimal {
|
|
101
|
+
/**
|
|
102
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection with regBytes=2 (address 0x50-0x57).
|
|
103
|
+
*/
|
|
104
|
+
constructor(connection) {
|
|
105
|
+
super(connection);
|
|
106
|
+
this._maxChunk = 30;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Maximum data bytes per bus transaction (default 30; Arduino Wire limits transfers to 32 bytes including the 2 address bytes).
|
|
111
|
+
* @type {number}
|
|
112
|
+
*/
|
|
113
|
+
get maxChunk() { return this._maxChunk; }
|
|
114
|
+
|
|
115
|
+
set maxChunk(value) {
|
|
116
|
+
if (!Number.isInteger(value) || value < 1) throw new RangeError('maxChunk must be >= 1');
|
|
117
|
+
this._maxChunk = value;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Array size in bytes.
|
|
122
|
+
* @returns {number} 65536.
|
|
123
|
+
*/
|
|
124
|
+
size() { return SIZE; }
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Write page size in bytes.
|
|
128
|
+
* @returns {number} 128.
|
|
129
|
+
*/
|
|
130
|
+
pageSize() { return PAGE; }
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* ACK-poll until the chip answers its device address again.
|
|
134
|
+
* @param {number} timeoutMs - Maximum time to poll in milliseconds.
|
|
135
|
+
* @returns {Promise<boolean>} True if the chip responded within the timeout.
|
|
136
|
+
*/
|
|
137
|
+
async waitReady(timeoutMs) {
|
|
138
|
+
return this._waitReady(timeoutMs);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Sequential read, split into transactions of at most maxChunk bytes.
|
|
143
|
+
* @param {number} address - Starting address 0-65535.
|
|
144
|
+
* @param {number} length - Number of bytes to read.
|
|
145
|
+
* @returns {Promise<Buffer>} `length` bytes.
|
|
146
|
+
* @throws {RangeError} If address..address+length is not inside 0x0000-0xFFFF.
|
|
147
|
+
*/
|
|
148
|
+
async read(address, length) {
|
|
149
|
+
_checkRange(address, length);
|
|
150
|
+
const parts = [];
|
|
151
|
+
while (length > 0) {
|
|
152
|
+
const n = Math.min(length, this._maxChunk);
|
|
153
|
+
parts.push(await this._conn.readReg(address, n));
|
|
154
|
+
address += n;
|
|
155
|
+
length -= n;
|
|
156
|
+
}
|
|
157
|
+
return Buffer.concat(parts);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Write one page (or part of it) in a single transaction, then ACK-poll.
|
|
162
|
+
* @param {number} address - Starting address 0-65535.
|
|
163
|
+
* @param {Buffer|number[]} data - 1-128 bytes, all inside the same 128-byte page.
|
|
164
|
+
* @returns {Promise<void>}
|
|
165
|
+
* @throws {RangeError} If the data crosses a page boundary, is longer than a page, or runs outside 0x0000-0xFFFF.
|
|
166
|
+
*/
|
|
167
|
+
async writePage(address, data) {
|
|
168
|
+
const buf = Buffer.from(data);
|
|
169
|
+
_checkRange(address, buf.length);
|
|
170
|
+
if (buf.length > PAGE || (address % PAGE) + buf.length > PAGE) {
|
|
171
|
+
throw new RangeError('write crosses a page boundary');
|
|
172
|
+
}
|
|
173
|
+
if (buf.length === 0) return;
|
|
174
|
+
await this._conn.writeReg(address, buf);
|
|
175
|
+
await this._commit();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Write `data` starting at `address`, split at page boundaries. Each chunk is
|
|
180
|
+
* at most maxChunk bytes and never crosses a 128-byte page boundary; the write
|
|
181
|
+
* cycle is ACK-polled after every chunk.
|
|
182
|
+
* @param {number} address - Starting address 0-65535.
|
|
183
|
+
* @param {Buffer|number[]} data - Bytes to write.
|
|
184
|
+
* @returns {Promise<void>}
|
|
185
|
+
* @throws {RangeError} If address..address+data.length is not inside 0x0000-0xFFFF.
|
|
186
|
+
*/
|
|
187
|
+
async write(address, data) {
|
|
188
|
+
const buf = Buffer.from(data);
|
|
189
|
+
_checkRange(address, buf.length);
|
|
190
|
+
let off = 0;
|
|
191
|
+
while (off < buf.length) {
|
|
192
|
+
const room = PAGE - ((address + off) % PAGE);
|
|
193
|
+
const n = Math.min(room, this._maxChunk, buf.length - off);
|
|
194
|
+
await this._conn.writeReg(address + off, buf.subarray(off, off + n));
|
|
195
|
+
await this._commit();
|
|
196
|
+
off += n;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Write `value` over a range, page by page.
|
|
202
|
+
* @param {number} address - Starting address 0-65535.
|
|
203
|
+
* @param {number} length - Number of bytes to fill.
|
|
204
|
+
* @param {number} [value=0] - Byte value 0-255.
|
|
205
|
+
* @returns {Promise<void>}
|
|
206
|
+
* @throws {RangeError} If address..address+length is not inside 0x0000-0xFFFF.
|
|
207
|
+
*/
|
|
208
|
+
async fill(address, length, value = 0) {
|
|
209
|
+
_checkRange(address, length);
|
|
210
|
+
let done = 0;
|
|
211
|
+
while (done < length) {
|
|
212
|
+
const room = PAGE - ((address + done) % PAGE);
|
|
213
|
+
const n = Math.min(room, this._maxChunk, length - done);
|
|
214
|
+
await this._conn.writeReg(address + done, Buffer.alloc(n, value & 0xFF));
|
|
215
|
+
await this._commit();
|
|
216
|
+
done += n;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
module.exports = { CAT24C512Minimal, CAT24C512Full };
|