periph 1.2.1 → 1.3.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/accelerometer/bma150/complete.js +62 -0
- package/examples/accelerometer/bma150/demo.js +65 -0
- package/examples/accelerometer/bma150/minimal.js +18 -0
- package/examples/adc_dac/hy3131/complete.js +66 -0
- package/examples/adc_dac/hy3131/demo.js +37 -0
- package/examples/adc_dac/hy3131/minimal.js +18 -0
- package/examples/discovery/complete.js +25 -0
- package/examples/discovery/demo.js +30 -0
- package/examples/discovery/minimal.js +12 -0
- package/examples/display/il0373/complete.js +71 -0
- package/examples/display/il0373/demo.js +49 -0
- package/examples/display/il0373/minimal.js +26 -0
- package/examples/display/ssd1331/complete.js +83 -0
- package/examples/display/ssd1331/demo.js +49 -0
- package/examples/display/ssd1331/minimal.js +27 -0
- package/examples/display/st7036/complete.js +71 -0
- package/examples/display/st7036/demo.js +56 -0
- package/examples/display/st7036/minimal.js +21 -0
- package/examples/display/uc1611s/complete.js +135 -0
- package/examples/display/uc1611s/complete_spi.js +128 -0
- package/examples/display/uc1611s/demo.js +90 -0
- package/examples/display/uc1611s/demo_spi.js +91 -0
- package/examples/display/uc1611s/minimal.js +28 -0
- package/examples/display/uc1611s/minimal_spi.js +30 -0
- package/examples/display/uc8159/complete.js +64 -0
- package/examples/display/uc8159/demo.js +41 -0
- package/examples/display/uc8159/minimal.js +28 -0
- package/examples/display/uc8276c/complete.js +73 -0
- package/examples/display/uc8276c/demo.js +49 -0
- package/examples/display/uc8276c/minimal.js +26 -0
- package/examples/environmental/bme280/complete.js +1 -1
- package/examples/environmental/bme280/demo.js +1 -1
- package/examples/environmental/bme280/minimal.js +1 -1
- package/examples/gyroscope/l3g4200d/complete.js +1 -1
- package/examples/gyroscope/l3g4200d/demo.js +1 -1
- package/examples/gyroscope/l3g4200d/minimal.js +1 -1
- package/examples/memory/23x1024/complete.js +54 -0
- package/examples/memory/23x1024/demo.js +51 -0
- package/examples/memory/23x1024/minimal.js +22 -0
- package/examples/memory/mx25l12845e/complete.js +66 -0
- package/examples/memory/mx25l12845e/demo.js +66 -0
- package/examples/memory/mx25l12845e/minimal.js +27 -0
- package/examples/memory/sd_spi/complete.js +55 -0
- package/examples/memory/sd_spi/demo.js +67 -0
- package/examples/memory/sd_spi/minimal.js +32 -0
- package/examples/pressure/bmp280/complete.js +1 -1
- package/examples/pressure/bmp280/demo.js +1 -1
- package/examples/pressure/bmp280/minimal.js +1 -1
- package/examples/pressure/bmp581/complete.js +1 -1
- package/examples/pressure/bmp581/demo.js +1 -1
- package/examples/pressure/bmp581/minimal.js +1 -1
- package/examples/pressure/lps22df/complete.js +1 -1
- package/examples/pressure/lps22df/demo.js +1 -1
- package/examples/pressure/lps22df/minimal.js +1 -1
- package/package.json +1 -1
- package/src/chips/accelerometer/adxl345.js +10 -31
- package/src/chips/accelerometer/adxl362.js +7 -7
- package/src/chips/accelerometer/bma150.js +419 -0
- package/src/chips/adc_dac/ad7705.js +36 -9
- package/src/chips/adc_dac/ad7706.js +39 -9
- package/src/chips/adc_dac/hy3131.js +311 -0
- package/src/chips/display/il0373.js +607 -0
- package/src/chips/display/ssd1331.js +412 -0
- package/src/chips/display/st7036.js +401 -0
- package/src/chips/display/uc1611s.js +648 -0
- package/src/chips/display/uc8159.js +394 -0
- package/src/chips/display/uc8276c.js +658 -0
- package/src/chips/environmental/bme280.js +31 -46
- package/src/chips/environmental/bme680.js +38 -43
- package/src/chips/gas/ens160.js +18 -26
- package/src/chips/gyroscope/l3g4200d.js +34 -50
- package/src/chips/gyroscope/l3gd20h.js +24 -40
- package/src/chips/imu/mpu6050.js +29 -41
- package/src/chips/imu/mpu9250.js +42 -66
- package/src/chips/imu/mpu9255.js +51 -75
- package/src/chips/io_expander/mcp23017.js +24 -33
- package/src/chips/light/apds-9930.js +52 -60
- package/src/chips/light/apds9960.js +69 -77
- package/src/chips/magnetometer/as5600.js +13 -27
- package/src/chips/magnetometer/hmc5883l.js +32 -29
- package/src/chips/memory/_23x1024.js +228 -0
- package/src/chips/memory/_24aa02uid.js +8 -11
- package/src/chips/memory/mx25l12845e.js +467 -0
- package/src/chips/memory/sd_spi.js +486 -0
- package/src/chips/motor/drv8830.js +4 -4
- package/src/chips/other/mpr121.js +7 -7
- package/src/chips/power/ina219.js +5 -8
- package/src/chips/power/ina226.js +5 -8
- package/src/chips/power/ina3221.js +5 -8
- package/src/chips/pressure/bmp085.js +17 -11
- package/src/chips/pressure/bmp180.js +7 -7
- package/src/chips/pressure/bmp280.js +21 -33
- package/src/chips/pressure/bmp384.js +28 -39
- package/src/chips/pressure/bmp581.js +59 -70
- package/src/chips/pressure/lps22df.js +50 -52
- package/src/chips/pressure/lps28dfw.js +15 -13
- package/src/chips/pressure/lps33hw.js +7 -5
- package/src/chips/rtc/ds3231.js +8 -8
- package/src/chips/rtc/pcf8523.js +8 -14
- package/src/chips/temperature/mcp9808.js +9 -7
- package/src/chips/temperature/tmp117.js +4 -4
- package/src/connection/connection.js +6 -4
- package/src/connection/dhtxx.js +5 -3
- package/src/connection/display.js +278 -0
- package/src/connection/display_mock.js +77 -0
- package/src/connection/hx711.js +5 -3
- package/src/connection/i2c.js +6 -4
- package/src/connection/i2c_mock.js +47 -0
- package/src/connection/input_pin.js +36 -5
- package/src/connection/register.js +14 -0
- package/src/connection/register_connection.js +65 -0
- package/src/connection/sipo.js +4 -2
- package/src/connection/sipo_mock.js +49 -0
- package/src/connection/smbus.js +6 -4
- package/src/connection/softspi.js +73 -0
- package/src/connection/spi.js +110 -23
- package/src/connection/spi_mock.js +50 -0
- package/src/connection/uart.js +2 -1
- package/src/discovery/discovery.js +218 -0
- package/src/discovery/registry.js +85 -0
- package/src/storage/block_device.js +95 -0
- package/src/storage/ram_block_device.js +72 -0
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Macronix MX25L12845E 128-Mbit (16 MiB) serial NOR flash (specs/memory/mx25l12845e.md).
|
|
5
|
+
*
|
|
6
|
+
* `MX25L12845EMinimal` is a byte-addressable store with transparent erase: `readBytes`, an
|
|
7
|
+
* erase-aware `writeBytes` (read-modify-erase-write per 4 KiB sector, programming only what
|
|
8
|
+
* changed) and `eraseSector`. `MX25L12845EFull` adds the raw program / erase commands, status
|
|
9
|
+
* and block protection, deep power-down, SFDP and the secured OTP, and implements the common
|
|
10
|
+
* `BlockDevice` contract (specs/feature_block_device.md): 512-byte blocks, 4 KiB erase units,
|
|
11
|
+
* `needsErase() === true`.
|
|
12
|
+
*
|
|
13
|
+
* Plain single-I/O SPI Mode 0 over a bare SPI connection (`write` and `writeRead`; CS is held
|
|
14
|
+
* low for each frame by the connection). Call `await flash.init()` before use (the constructor
|
|
15
|
+
* cannot await bus I/O).
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const {
|
|
19
|
+
BlockDevice, NotReady, OutOfRange, NotErased, WriteProtected, Timeout, Io,
|
|
20
|
+
} = require('../../storage/block_device');
|
|
21
|
+
|
|
22
|
+
const WREN = 0x06;
|
|
23
|
+
const RDID = 0x9F;
|
|
24
|
+
const RES = 0xAB;
|
|
25
|
+
const REMS = 0x90;
|
|
26
|
+
const RDSR = 0x05;
|
|
27
|
+
const WRSR = 0x01;
|
|
28
|
+
const READ = 0x03;
|
|
29
|
+
const FAST_READ = 0x0B;
|
|
30
|
+
const PP = 0x02;
|
|
31
|
+
const SE = 0x20;
|
|
32
|
+
const BE32K = 0x52;
|
|
33
|
+
const BE64K = 0xD8;
|
|
34
|
+
const CE = 0xC7;
|
|
35
|
+
const DP = 0xB9;
|
|
36
|
+
const RDP = 0xAB;
|
|
37
|
+
const RDSFDP = 0x5A;
|
|
38
|
+
const ENSO = 0xB1;
|
|
39
|
+
const EXSO = 0xC1;
|
|
40
|
+
const RDSCUR = 0x2B;
|
|
41
|
+
const WRSCUR = 0x2F;
|
|
42
|
+
const CLSR = 0x30;
|
|
43
|
+
|
|
44
|
+
const SR_WIP = 0x01;
|
|
45
|
+
const SR_WEL = 0x02;
|
|
46
|
+
const SR_BP_MASK = 0x3C;
|
|
47
|
+
const SR_BP_SHIFT = 2;
|
|
48
|
+
const SCUR_P_FAIL = 0x20;
|
|
49
|
+
const SCUR_E_FAIL = 0x40;
|
|
50
|
+
|
|
51
|
+
const T_WRSR_MS = 150;
|
|
52
|
+
const T_PP_MS = 10;
|
|
53
|
+
const T_SE_MS = 400;
|
|
54
|
+
const T_BE_MS = 3000;
|
|
55
|
+
const T_CE_MS = 250000;
|
|
56
|
+
const READ_CHUNK = 4096;
|
|
57
|
+
|
|
58
|
+
const PAGE_SIZE = 256;
|
|
59
|
+
const SECTOR_SIZE = 4096;
|
|
60
|
+
const SIZE = 16 * 1024 * 1024;
|
|
61
|
+
const BLOCK_SIZE = 512;
|
|
62
|
+
|
|
63
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
64
|
+
|
|
65
|
+
function header(cmd, address) {
|
|
66
|
+
return Buffer.from([cmd, (address >> 16) & 0xFF, (address >> 8) & 0xFF, address & 0xFF]);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** MX25L12845E — minimal driver: byte-addressed read, erase-aware write. */
|
|
70
|
+
class MX25L12845EMinimal {
|
|
71
|
+
/**
|
|
72
|
+
* @param {object} connection - SPI connection (`write`, `writeRead`).
|
|
73
|
+
* @param {object} [options]
|
|
74
|
+
* @param {number} [options.initTimeoutMs=250000] - How long `init()` waits for a chip that is still busy
|
|
75
|
+
* (a reset during a chip erase can leave it busy for up to 200 s).
|
|
76
|
+
*/
|
|
77
|
+
constructor(connection, options = {}) {
|
|
78
|
+
this._c = connection;
|
|
79
|
+
this._initTimeout = options.initTimeoutMs ?? T_CE_MS;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Leave deep power-down, check the JEDEC ID (C2 20 18) and wait until the chip is idle.
|
|
84
|
+
* @returns {Promise<void>}
|
|
85
|
+
* @throws {NotReady} The JEDEC ID did not match.
|
|
86
|
+
* @throws {Timeout} The chip stayed busy.
|
|
87
|
+
*/
|
|
88
|
+
async init() {
|
|
89
|
+
await this._c.write(Buffer.from([RDP])); // leave deep power-down if a previous session left it there
|
|
90
|
+
await sleep(1); // tRES2 = 100 us
|
|
91
|
+
const id = await this.jedecId();
|
|
92
|
+
if (id.toString('hex') !== 'c22018') throw new NotReady(`MX25L12845E not found (JEDEC ID ${id.toString('hex')})`);
|
|
93
|
+
await this._waitReady(this._initTimeout);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ---- bus primitives --------------------------------------------------
|
|
97
|
+
|
|
98
|
+
async _status() {
|
|
99
|
+
return (await this._c.writeRead(Buffer.from([RDSR]), 1))[0];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async _wren() {
|
|
103
|
+
await this._c.write(Buffer.from([WREN]));
|
|
104
|
+
if (!((await this._status()) & SR_WEL)) throw new Io('WEL not set after WREN'); // the next command would be ignored
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async _waitReady(timeoutMs) {
|
|
108
|
+
const start = Date.now();
|
|
109
|
+
while ((await this._status()) & SR_WIP) {
|
|
110
|
+
if (Date.now() - start >= timeoutMs) throw new Timeout('chip busy');
|
|
111
|
+
if (timeoutMs > 20) await sleep(1); // long operations: do not hammer the bus
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async _checkFail() {
|
|
116
|
+
const scur = (await this._c.writeRead(Buffer.from([RDSCUR]), 1))[0];
|
|
117
|
+
if (scur & (SCUR_P_FAIL | SCUR_E_FAIL)) {
|
|
118
|
+
await this._c.write(Buffer.from([CLSR]));
|
|
119
|
+
if ((await this._status()) & SR_BP_MASK) throw new WriteProtected('program/erase hit the block-protected area');
|
|
120
|
+
throw new Io(`program/erase failed (security register 0x${scur.toString(16).padStart(2, '0')})`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async _eraseCmd(cmd, address, timeoutMs) {
|
|
125
|
+
await this._wren();
|
|
126
|
+
await this._c.write(header(cmd, address));
|
|
127
|
+
await this._waitReady(timeoutMs);
|
|
128
|
+
await this._checkFail();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async _program(address, data) {
|
|
132
|
+
await this._wren();
|
|
133
|
+
await this._c.write(Buffer.concat([header(PP, address), Buffer.from(data)]));
|
|
134
|
+
await this._waitReady(T_PP_MS);
|
|
135
|
+
await this._checkFail();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async _readRaw(cmd, address, dummy, length) {
|
|
139
|
+
const parts = [];
|
|
140
|
+
while (length > 0) {
|
|
141
|
+
const n = Math.min(READ_CHUNK, length);
|
|
142
|
+
const frame = dummy ? Buffer.concat([header(cmd, address), Buffer.from([0])]) : header(cmd, address);
|
|
143
|
+
parts.push(Buffer.from(await this._c.writeRead(frame, n)));
|
|
144
|
+
address += n;
|
|
145
|
+
length -= n;
|
|
146
|
+
}
|
|
147
|
+
return Buffer.concat(parts);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ---- Minimal API -----------------------------------------------------
|
|
151
|
+
|
|
152
|
+
/** @returns {Promise<Buffer>} The three RDID bytes (manufacturer, memory type, density). */
|
|
153
|
+
async jedecId() {
|
|
154
|
+
return Buffer.from(await this._c.writeRead(Buffer.from([RDID]), 3));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** @returns {number} Capacity in bytes (16 777 216). */
|
|
158
|
+
size() { return SIZE; }
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Read `length` bytes starting at `address` (READ, 03h), any length and alignment.
|
|
162
|
+
* @param {number} address
|
|
163
|
+
* @param {number} length
|
|
164
|
+
* @returns {Promise<Buffer>}
|
|
165
|
+
* @throws {OutOfRange} The range leaves the array.
|
|
166
|
+
*/
|
|
167
|
+
async readBytes(address, length) {
|
|
168
|
+
if (address < 0 || address + length > SIZE) throw new OutOfRange('read beyond end');
|
|
169
|
+
return this._readRaw(READ, address, false, length);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Erase the 4 KiB sector containing `address` (SE, 20h).
|
|
174
|
+
* @param {number} address
|
|
175
|
+
* @returns {Promise<void>}
|
|
176
|
+
*/
|
|
177
|
+
async eraseSector(address) {
|
|
178
|
+
if (address < 0 || address >= SIZE) throw new OutOfRange('address beyond end');
|
|
179
|
+
await this._eraseCmd(SE, address, T_SE_MS);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Erase-aware write; resolves when the chip is ready. Per 4 KiB sector: if only 1->0 transitions are
|
|
184
|
+
* needed just the changed pages are programmed, otherwise the sector is erased and rewritten with the
|
|
185
|
+
* merged contents.
|
|
186
|
+
* @param {number} address
|
|
187
|
+
* @param {Buffer|Uint8Array} data
|
|
188
|
+
* @returns {Promise<void>}
|
|
189
|
+
* @throws {OutOfRange} The range leaves the array.
|
|
190
|
+
* @throws {WriteProtected} The chip reported a failure while block-protect bits are set.
|
|
191
|
+
*/
|
|
192
|
+
async writeBytes(address, data) {
|
|
193
|
+
data = Buffer.from(data);
|
|
194
|
+
const length = data.length;
|
|
195
|
+
if (address < 0 || address + length > SIZE) throw new OutOfRange('write beyond end');
|
|
196
|
+
let pos = 0;
|
|
197
|
+
while (pos < length) {
|
|
198
|
+
const base = address - (address % SECTOR_SIZE);
|
|
199
|
+
const off = address - base;
|
|
200
|
+
const n = Math.min(SECTOR_SIZE - off, length - pos);
|
|
201
|
+
const chunk = data.subarray(pos, pos + n);
|
|
202
|
+
const old = await this._readRaw(READ, base, false, SECTOR_SIZE);
|
|
203
|
+
let needsErase = false;
|
|
204
|
+
for (let i = 0; i < n; i++) {
|
|
205
|
+
if ((old[off + i] & chunk[i]) !== chunk[i]) { needsErase = true; break; } // would need a 0 -> 1 transition
|
|
206
|
+
}
|
|
207
|
+
if (!needsErase) {
|
|
208
|
+
let done = 0;
|
|
209
|
+
while (done < n) {
|
|
210
|
+
const a = address + done;
|
|
211
|
+
const part = Math.min(PAGE_SIZE - (a % PAGE_SIZE), n - done);
|
|
212
|
+
if (!old.subarray(off + done, off + done + part).equals(chunk.subarray(done, done + part))) {
|
|
213
|
+
await this._program(a, chunk.subarray(done, done + part));
|
|
214
|
+
}
|
|
215
|
+
done += part;
|
|
216
|
+
}
|
|
217
|
+
} else {
|
|
218
|
+
const merged = Buffer.from(old);
|
|
219
|
+
chunk.copy(merged, off);
|
|
220
|
+
await this._eraseCmd(SE, base, T_SE_MS);
|
|
221
|
+
for (let page = 0; page < SECTOR_SIZE; page += PAGE_SIZE) {
|
|
222
|
+
const block = merged.subarray(page, page + PAGE_SIZE);
|
|
223
|
+
if (!block.every((b) => b === 0xFF)) await this._program(base + page, block); // blank pages need no program
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
address += n;
|
|
227
|
+
pos += n;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* MX25L12845E — full driver: raw commands, protection, power, OTP, SFDP; implements `BlockDevice`.
|
|
234
|
+
*/
|
|
235
|
+
class MX25L12845EFull extends MX25L12845EMinimal {
|
|
236
|
+
/**
|
|
237
|
+
* @param {object} connection - SPI connection.
|
|
238
|
+
* @param {object} [options]
|
|
239
|
+
* @param {boolean} [options.autoErase=false] - `write()` erases sectors itself when a 0->1 transition is
|
|
240
|
+
* needed; otherwise it rejects with `NotErased`.
|
|
241
|
+
* @param {number} [options.initTimeoutMs] - See `MX25L12845EMinimal`.
|
|
242
|
+
*/
|
|
243
|
+
constructor(connection, options = {}) {
|
|
244
|
+
super(connection, options);
|
|
245
|
+
this._autoErase = options.autoErase ?? false;
|
|
246
|
+
this._bp = 0;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** As `MX25L12845EMinimal#init`, then caches the block-protect bits for `info()`. */
|
|
250
|
+
async init() {
|
|
251
|
+
await super.init();
|
|
252
|
+
await this.protection();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// ---- raw operations --------------------------------------------------
|
|
256
|
+
|
|
257
|
+
/** FAST_READ (0Bh) of `length` bytes. @returns {Promise<Buffer>} */
|
|
258
|
+
async fastRead(address, length) {
|
|
259
|
+
if (address < 0 || address + length > SIZE) throw new OutOfRange('read beyond end');
|
|
260
|
+
return this._readRaw(FAST_READ, address, true, length);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** One PP of 1..256 bytes that must not cross a 256-byte page boundary; the bytes must be erased. */
|
|
264
|
+
async pageProgram(address, data) {
|
|
265
|
+
const n = data.length;
|
|
266
|
+
if (n === 0 || n > PAGE_SIZE || address < 0 || address + n > SIZE) throw new OutOfRange('bad page program range');
|
|
267
|
+
if ((address % PAGE_SIZE) + n > PAGE_SIZE) throw new OutOfRange('page program would wrap inside the page');
|
|
268
|
+
await this._program(address, data);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** Erase the 32 KiB block containing `address` (BE32K, 52h). */
|
|
272
|
+
async eraseBlock32k(address) {
|
|
273
|
+
if (address < 0 || address >= SIZE) throw new OutOfRange('address beyond end');
|
|
274
|
+
await this._eraseCmd(BE32K, address, T_BE_MS);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Erase the 64 KiB block containing `address` (BE, D8h). */
|
|
278
|
+
async eraseBlock64k(address) {
|
|
279
|
+
if (address < 0 || address >= SIZE) throw new OutOfRange('address beyond end');
|
|
280
|
+
await this._eraseCmd(BE64K, address, T_BE_MS);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Erase the whole chip (CE, C7h); 80 s typical, 200 s max; needs BP3:0 == 0. */
|
|
284
|
+
async chipErase() {
|
|
285
|
+
await this._wren();
|
|
286
|
+
await this._c.write(Buffer.from([CE]));
|
|
287
|
+
await this._waitReady(T_CE_MS);
|
|
288
|
+
await this._checkFail();
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// ---- status / protection ---------------------------------------------
|
|
292
|
+
|
|
293
|
+
/** @returns {Promise<number>} The status register. */
|
|
294
|
+
async readStatus() { return this._status(); }
|
|
295
|
+
|
|
296
|
+
/** WREN + WRSR; waits for tW. */
|
|
297
|
+
async writeStatus(value) {
|
|
298
|
+
await this._wren();
|
|
299
|
+
await this._c.write(Buffer.from([WRSR, value & 0xFF]));
|
|
300
|
+
await this._waitReady(T_WRSR_MS);
|
|
301
|
+
this._bp = (value & SR_BP_MASK) >> SR_BP_SHIFT;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** @returns {Promise<number>} BP3:0 (0..15) from the status register. */
|
|
305
|
+
async protection() {
|
|
306
|
+
this._bp = ((await this._status()) & SR_BP_MASK) >> SR_BP_SHIFT;
|
|
307
|
+
return this._bp;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Set BP3:0 (non-volatile); QE and SRWD are preserved. */
|
|
311
|
+
async setProtection(bp) {
|
|
312
|
+
if (!Number.isInteger(bp) || bp < 0 || bp > 15) throw new OutOfRange('BP must be 0..15');
|
|
313
|
+
await this.writeStatus(((await this._status()) & ~SR_BP_MASK) | (bp << SR_BP_SHIFT));
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** @returns {Promise<boolean>} True while a program, erase or WRSR is running (WIP). */
|
|
317
|
+
async isBusy() { return !!((await this._status()) & SR_WIP); }
|
|
318
|
+
|
|
319
|
+
/** Wait until WIP = 0; rejects with `Timeout`. */
|
|
320
|
+
async waitReady(timeoutMs) { await this._waitReady(timeoutMs); }
|
|
321
|
+
|
|
322
|
+
/** @returns {Promise<number>} The security register (RDSCUR). */
|
|
323
|
+
async readSecurity() { return (await this._c.writeRead(Buffer.from([RDSCUR]), 1))[0]; }
|
|
324
|
+
|
|
325
|
+
/** CLSR: clear P_FAIL / E_FAIL. */
|
|
326
|
+
async clearFailFlags() { await this._c.write(Buffer.from([CLSR])); }
|
|
327
|
+
|
|
328
|
+
// ---- power / identification ------------------------------------------
|
|
329
|
+
|
|
330
|
+
/** DP: enter deep power-down (only RDP / RES are accepted afterwards). */
|
|
331
|
+
async deepPowerDown() { await this._c.write(Buffer.from([DP])); }
|
|
332
|
+
|
|
333
|
+
/** RDP: leave deep power-down and wait tRES2. */
|
|
334
|
+
async releaseDeepPowerDown() {
|
|
335
|
+
await this._c.write(Buffer.from([RDP]));
|
|
336
|
+
await sleep(1);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** @returns {Promise<number>} RES electronic ID (17h). */
|
|
340
|
+
async electronicId() { return (await this._c.writeRead(Buffer.from([RES, 0, 0, 0]), 1))[0]; }
|
|
341
|
+
|
|
342
|
+
/** @returns {Promise<number[]>} REMS: `[manufacturer, device]` = [0xC2, 0x17]. */
|
|
343
|
+
async deviceIds() {
|
|
344
|
+
const r = await this._c.writeRead(Buffer.from([REMS, 0, 0, 0]), 2);
|
|
345
|
+
return [r[0], r[1]];
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** RDSFDP (5Ah): `length` bytes of the SFDP table from `address`. @returns {Promise<Buffer>} */
|
|
349
|
+
async readSfdp(address, length) { return this._readRaw(RDSFDP, address, true, length); }
|
|
350
|
+
|
|
351
|
+
// ---- secured OTP (4 Kbit) --------------------------------------------
|
|
352
|
+
|
|
353
|
+
/** Read `length` bytes at OTP offset `offset` (0..511). @returns {Promise<Buffer>} */
|
|
354
|
+
async readOtp(offset, length) {
|
|
355
|
+
if (offset < 0 || offset + length > 512) throw new OutOfRange('OTP range');
|
|
356
|
+
await this._c.write(Buffer.from([ENSO]));
|
|
357
|
+
try {
|
|
358
|
+
return await this._readRaw(READ, offset, false, length);
|
|
359
|
+
} finally {
|
|
360
|
+
await this._c.write(Buffer.from([EXSO]));
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Program OTP bytes at `offset` (16..511 only: 0..15 are the ESN area); one-time programmable. */
|
|
365
|
+
async programOtp(offset, data) {
|
|
366
|
+
data = Buffer.from(data);
|
|
367
|
+
if (offset < 16 || data.length === 0 || offset + data.length > 512) throw new OutOfRange('OTP range (0..15 is the ESN area)');
|
|
368
|
+
await this._c.write(Buffer.from([ENSO]));
|
|
369
|
+
try {
|
|
370
|
+
let pos = 0;
|
|
371
|
+
while (pos < data.length) {
|
|
372
|
+
const n = Math.min(PAGE_SIZE - (offset % PAGE_SIZE), data.length - pos);
|
|
373
|
+
await this._program(offset, data.subarray(pos, pos + n));
|
|
374
|
+
offset += n;
|
|
375
|
+
pos += n;
|
|
376
|
+
}
|
|
377
|
+
} finally {
|
|
378
|
+
await this._c.write(Buffer.from([EXSO]));
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** Lock the customer OTP area for good (WRSCUR, sets LDSO) — IRREVERSIBLE. */
|
|
383
|
+
async lockOtp() {
|
|
384
|
+
await this._wren();
|
|
385
|
+
await this._c.write(Buffer.from([WRSCUR]));
|
|
386
|
+
await this._waitReady(T_WRSR_MS);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** @returns {Promise<Buffer>} OTP 000h-00Fh (the factory ESN when `readSecurity() & 1`; blank 0xFF otherwise). */
|
|
390
|
+
async serialNumber() { return this.readOtp(0, 16); }
|
|
391
|
+
|
|
392
|
+
// ---- BlockDevice -----------------------------------------------------
|
|
393
|
+
|
|
394
|
+
blockSize() { return BLOCK_SIZE; }
|
|
395
|
+
blockCount() { return SIZE / BLOCK_SIZE; }
|
|
396
|
+
eraseSize() { return SECTOR_SIZE; }
|
|
397
|
+
needsErase() { return true; }
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Read `count` blocks of 512 bytes.
|
|
401
|
+
* @returns {Promise<Buffer>}
|
|
402
|
+
*/
|
|
403
|
+
async read(block, count = 1) {
|
|
404
|
+
if (block < 0 || block + count > this.blockCount()) throw new OutOfRange('read beyond end');
|
|
405
|
+
return this._readRaw(READ, block * BLOCK_SIZE, false, count * BLOCK_SIZE);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Program `data.length / 512` blocks; with `autoErase` off a block that needs a 0->1 transition rejects
|
|
410
|
+
* with `NotErased` (nothing is erased).
|
|
411
|
+
*/
|
|
412
|
+
async write(block, data) {
|
|
413
|
+
data = Buffer.from(data);
|
|
414
|
+
const count = data.length / BLOCK_SIZE;
|
|
415
|
+
if (!Number.isInteger(count) || count < 1 || block < 0 || block + count > this.blockCount()) throw new OutOfRange('write range');
|
|
416
|
+
let address = block * BLOCK_SIZE;
|
|
417
|
+
if (this._autoErase) return this.writeBytes(address, data);
|
|
418
|
+
let pos = 0;
|
|
419
|
+
while (pos < data.length) { // program page by page, only where no 0 -> 1 transition is needed
|
|
420
|
+
const n = Math.min(PAGE_SIZE - (address % PAGE_SIZE), data.length - pos);
|
|
421
|
+
const old = await this._readRaw(READ, address, false, n);
|
|
422
|
+
const next = data.subarray(pos, pos + n);
|
|
423
|
+
for (let i = 0; i < n; i++) {
|
|
424
|
+
if ((old[i] & next[i]) !== next[i]) throw new NotErased('block needs an erase before it can be programmed');
|
|
425
|
+
}
|
|
426
|
+
if (!old.equals(next)) await this._program(address, next);
|
|
427
|
+
address += n;
|
|
428
|
+
pos += n;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** Erase `count` blocks; the range must be 4 KiB-aligned (multiples of 8 blocks). */
|
|
433
|
+
async erase(block, count = 1) {
|
|
434
|
+
if (block % 8 || count % 8 || block < 0 || block + count > this.blockCount()) throw new OutOfRange('erase range must be 4 KiB aligned');
|
|
435
|
+
let address = block * BLOCK_SIZE;
|
|
436
|
+
let left = count * BLOCK_SIZE;
|
|
437
|
+
while (left > 0) {
|
|
438
|
+
let step;
|
|
439
|
+
if (address % 65536 === 0 && left >= 65536) { await this.eraseBlock64k(address); step = 65536; }
|
|
440
|
+
else if (address % 32768 === 0 && left >= 32768) { await this.eraseBlock32k(address); step = 32768; }
|
|
441
|
+
else { await this.eraseSector(address); step = SECTOR_SIZE; }
|
|
442
|
+
address += step;
|
|
443
|
+
left -= step;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** Wait until the chip is idle. */
|
|
448
|
+
async sync() { await this._waitReady(T_CE_MS); }
|
|
449
|
+
|
|
450
|
+
/** @returns {object} Geometry and capabilities (see BlockDevice#info); `readOnly` when BP >= 8 as last read by
|
|
451
|
+
* `init()`, `protection()`, `setProtection()` or `writeStatus()`. */
|
|
452
|
+
info() {
|
|
453
|
+
return {
|
|
454
|
+
blockSize: BLOCK_SIZE, blockCount: this.blockCount(), eraseSize: SECTOR_SIZE, needsErase: true,
|
|
455
|
+
readOnly: this._bp >= 8, badBlocks: false, name: 'MX25L12845E',
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
async isBad(_unit) { return false; }
|
|
460
|
+
async markBad(_unit) {}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// Single inheritance: MX25L12845EFull conforms to BlockDevice by duck typing; the marker makes
|
|
464
|
+
// `flash instanceof BlockDevice` true.
|
|
465
|
+
BlockDevice.mark(MX25L12845EFull);
|
|
466
|
+
|
|
467
|
+
module.exports = { MX25L12845EMinimal, MX25L12845EFull, SIZE, PAGE_SIZE, SECTOR_SIZE };
|