periph 1.2.0 → 1.3.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 +18 -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/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -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/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/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 +45 -69
- package/src/chips/imu/mpu9255.js +54 -78
- 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 +239 -0
- 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 +504 -0
- package/src/chips/rtc/pcf8523.js +533 -0
- package/src/chips/temperature/mcp9808.js +490 -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/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
- 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,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { SPIConnection } = require('../../../src/connection/spi'); // SPIConnection class, (bus, dev, options) → SPIConnection
|
|
4
|
+
const { SRAM23X1024Full } = require('../../../src/chips/memory/_23x1024'); // SRAM23X1024Full class, (connection) → SRAM23X1024Full
|
|
5
|
+
|
|
6
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
7
|
+
const SPI_DEV = parseInt(process.env.SPI_DEV || '0', 10);
|
|
8
|
+
|
|
9
|
+
const hex = (v) => v.toString(16).toUpperCase().padStart(2, '0');
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
const connection = new SPIConnection(SPI_BUS, SPI_DEV, { mode: 0, maxSpeedHz: 8_000_000 }); // Open SPI bus, (busNumber, deviceNumber, mode=0, maxSpeedHz=8e6) → SPIConnection
|
|
13
|
+
const sram = new SRAM23X1024Full(connection); // Create 23X1024 Full driver, (connection) → SRAM23X1024Full
|
|
14
|
+
await sram.init(); // Select Sequential mode and verify it, () → Promise<void>
|
|
15
|
+
// writes MODE=0x40 and reads it back with RDMR
|
|
16
|
+
|
|
17
|
+
console.log(`size: ${sram.size()} bytes`); // Array size, () → number bytes
|
|
18
|
+
// always 131072
|
|
19
|
+
|
|
20
|
+
await sram.writeByte(0x00010, 0xA5); // Write one byte, (address 0–131071, value 0–255) → Promise<void>
|
|
21
|
+
// WRITE 0x02 + 24-bit address + data
|
|
22
|
+
console.log(`byte @0x10: 0x${hex(await sram.readByte(0x00010))}`); // Read one byte, (address 0–131071) → Promise<number>
|
|
23
|
+
// READ 0x03 + 24-bit address
|
|
24
|
+
|
|
25
|
+
const pattern = Buffer.from(Array.from({ length: 300 }, (_, i) => i & 0xFF));
|
|
26
|
+
await sram.write(0x00100, pattern); // Block write, (address, data) → Promise<void>
|
|
27
|
+
// split into transactions of at most maxChunk bytes
|
|
28
|
+
const readback = await sram.read(0x00100, pattern.length); // Block read, (address, length) → Promise<Buffer>
|
|
29
|
+
// sequential read across the chunk boundary
|
|
30
|
+
console.log('block of 300:', readback.equals(pattern) ? 'match' : 'MISMATCH');
|
|
31
|
+
|
|
32
|
+
await sram.fill(0x00200, 64, 0xFF); // Fill a range, (address, length, value=0) → Promise<void>
|
|
33
|
+
// writes value over the range, chunked
|
|
34
|
+
console.log(`fill @0x200: 0x${hex(await sram.readByte(0x00200))}`);
|
|
35
|
+
|
|
36
|
+
sram.maxChunk = 64; // Limit bytes per transaction, (n 1–256)
|
|
37
|
+
// smaller frames for bus layers with small buffers
|
|
38
|
+
console.log('maxChunk:', sram.maxChunk);
|
|
39
|
+
sram.maxChunk = 256; // Limit bytes per transaction, (n 1–256)
|
|
40
|
+
|
|
41
|
+
await sram.setMode(SRAM23X1024Full.MODE_PAGE); // Select operating mode, (mode=MODE_PAGE=0x80) → Promise<void>
|
|
42
|
+
// WRMR then RDMR check; accesses now wrap within 32-byte pages
|
|
43
|
+
console.log(`mode: 0x${hex(await sram.getMode())}`); // Read operating mode, () → Promise<number>
|
|
44
|
+
// RDMR bits 7:6
|
|
45
|
+
await sram.write(0x00110, pattern.subarray(0, 64)); // Block write, (address, data) → Promise<void>
|
|
46
|
+
// the driver splits at page boundaries, so the data stays linear
|
|
47
|
+
const paged = await sram.read(0x00110, 64); // Block read, (address, length) → Promise<Buffer>
|
|
48
|
+
console.log('page mode 64 bytes:', paged.equals(pattern.subarray(0, 64)) ? 'match' : 'MISMATCH');
|
|
49
|
+
|
|
50
|
+
await sram.setMode(SRAM23X1024Full.MODE_SEQUENTIAL); // Select operating mode, (mode=MODE_SEQUENTIAL=0x40) → Promise<void>
|
|
51
|
+
await sram.resetIo(); // Return to SPI mode, () → Promise<void>
|
|
52
|
+
// sends RSTIO (0xFF); harmless while already in SPI mode
|
|
53
|
+
await connection.close();
|
|
54
|
+
})();
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { SPIConnection } = require('../../../src/connection/spi'); // SPIConnection class, (bus, dev, options) → SPIConnection
|
|
4
|
+
const { SRAM23X1024Full } = require('../../../src/chips/memory/_23x1024'); // SRAM23X1024Full class, (connection) → SRAM23X1024Full
|
|
5
|
+
|
|
6
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
7
|
+
const SPI_DEV = parseInt(process.env.SPI_DEV || '0', 10);
|
|
8
|
+
|
|
9
|
+
const hex = (v) => v.toString(16).toUpperCase().padStart(2, '0');
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
const connection = new SPIConnection(SPI_BUS, SPI_DEV, { mode: 0, maxSpeedHz: 8_000_000 }); // Open SPI bus, (busNumber, deviceNumber, mode=0, maxSpeedHz=8e6) → SPIConnection
|
|
13
|
+
const sram = new SRAM23X1024Full(connection); // Create 23X1024 Full driver, (connection) → SRAM23X1024Full
|
|
14
|
+
await sram.init(); // Select Sequential mode and verify it, () → Promise<void>
|
|
15
|
+
|
|
16
|
+
// --- Store a 256-entry lookup table in external SRAM ---
|
|
17
|
+
// A sine table is the kind of data a microcontroller with little RAM
|
|
18
|
+
// keeps outside: written once, then read back entry by entry.
|
|
19
|
+
const table = Buffer.from(Array.from({ length: 256 }, (_, i) => Math.floor(127.5 + 127.5 * Math.sin(2 * Math.PI * i / 256))));
|
|
20
|
+
await sram.write(0x00000, table); // Block write, (address, data) → Promise<void>
|
|
21
|
+
const check = await sram.read(0x00000, table.length); // Block read, (address, length) → Promise<Buffer>
|
|
22
|
+
console.log(`sine table: ${check.equals(table) ? 'ok' : 'MISMATCH'} (sin(64/256 turn) = ${await sram.readByte(64)})`); // Read one byte, (address) → Promise<number>
|
|
23
|
+
|
|
24
|
+
// --- Wipe the whole array and spot-check it ---
|
|
25
|
+
// Zero write time means the 128 KiB fill runs at SPI speed with no
|
|
26
|
+
// waiting; the first, middle and last byte prove the full range.
|
|
27
|
+
await sram.fill(0x00000, sram.size(), 0xA5); // Fill a range, (address, length, value=0) → Promise<void>
|
|
28
|
+
console.log(`fill spot check: 0x${hex(await sram.readByte(0x00000))} 0x${hex(await sram.readByte(0x10000))} 0x${hex(await sram.readByte(0x1FFFF))}`); // Read one byte, (address) → Promise<number>
|
|
29
|
+
|
|
30
|
+
// --- Page mode: a write that crosses a 32-byte page boundary ---
|
|
31
|
+
// The hardware would wrap inside the page; the driver splits the
|
|
32
|
+
// transfer so the 64 bytes starting at address 16 still read back in order.
|
|
33
|
+
await sram.setMode(SRAM23X1024Full.MODE_PAGE); // Select operating mode, (mode=MODE_PAGE=0x80) → Promise<void>
|
|
34
|
+
const ramp = Buffer.from(Array.from({ length: 64 }, (_, i) => 0x40 + i));
|
|
35
|
+
await sram.write(16, ramp); // Block write, (address, data) → Promise<void>
|
|
36
|
+
const rampBack = await sram.read(16, ramp.length); // Block read, (address, length) → Promise<Buffer>
|
|
37
|
+
console.log('page-crossing write:', rampBack.equals(ramp) ? 'ok' : 'MISMATCH');
|
|
38
|
+
await sram.setMode(SRAM23X1024Full.MODE_SEQUENTIAL); // Select operating mode, (mode=MODE_SEQUENTIAL=0x40) → Promise<void>
|
|
39
|
+
|
|
40
|
+
// --- Measure throughput for a 4 KiB transfer ---
|
|
41
|
+
// Sequential mode streams the block in 256-byte transactions;
|
|
42
|
+
// process.hrtime.bigint() times them.
|
|
43
|
+
const block = Buffer.from(Array.from({ length: 4096 }, (_, i) => i & 0xFF));
|
|
44
|
+
const t0 = process.hrtime.bigint();
|
|
45
|
+
await sram.write(0x01000, block); // Block write, (address, data) → Promise<void>
|
|
46
|
+
const t1 = process.hrtime.bigint();
|
|
47
|
+
await sram.read(0x01000, block.length); // Block read, (address, length) → Promise<Buffer>
|
|
48
|
+
const t2 = process.hrtime.bigint();
|
|
49
|
+
console.log(`4 KiB write: ${Number(t1 - t0) / 1e6} ms, read: ${Number(t2 - t1) / 1e6} ms`);
|
|
50
|
+
await connection.close();
|
|
51
|
+
})();
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { SPIConnection } = require('../../../src/connection/spi'); // SPIConnection class, (bus, dev, options) → SPIConnection
|
|
4
|
+
const { SRAM23X1024Minimal } = require('../../../src/chips/memory/_23x1024'); // SRAM23X1024Minimal class, (connection) → SRAM23X1024Minimal
|
|
5
|
+
|
|
6
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
7
|
+
const SPI_DEV = parseInt(process.env.SPI_DEV || '0', 10);
|
|
8
|
+
|
|
9
|
+
(async () => {
|
|
10
|
+
const connection = new SPIConnection(SPI_BUS, SPI_DEV, { mode: 0, maxSpeedHz: 8_000_000 }); // Open SPI bus, (busNumber, deviceNumber, mode=0, maxSpeedHz=8e6) → SPIConnection
|
|
11
|
+
const sram = new SRAM23X1024Minimal(connection); // Create 23X1024 driver, (connection) → SRAM23X1024Minimal
|
|
12
|
+
await sram.init(); // Select Sequential mode and verify it, () → Promise<void>
|
|
13
|
+
|
|
14
|
+
let counter = 0;
|
|
15
|
+
while (true) {
|
|
16
|
+
await sram.writeByte(0x00000, counter); // Write one byte, (address 0–131071, value 0–255) → Promise<void>
|
|
17
|
+
const value = await sram.readByte(0x00000); // Read one byte, (address 0–131071) → Promise<number>
|
|
18
|
+
console.log(`wrote 0x${counter.toString(16).padStart(2, '0')}, read 0x${value.toString(16).padStart(2, '0')}`);
|
|
19
|
+
counter = (counter + 1) & 0xFF;
|
|
20
|
+
await new Promise(r => setTimeout(r, 500));
|
|
21
|
+
}
|
|
22
|
+
})();
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Wiring: SPI0 (MOSI, MISO, SCK, CE0) to the MX25L12845E, 3.3 V. Override with SPI_BUS, SPI_DEV.
|
|
3
|
+
// All erases and writes stay inside the last 64 KiB of the chip (0xFF0000..0xFFFFFF).
|
|
4
|
+
const { SPIConnection } = require('../../../src/connection/spi'); // SPIConnection class, (bus, dev, options) → SPIConnection
|
|
5
|
+
const { MX25L12845EFull } = require('../../../src/chips/memory/mx25l12845e'); // MX25L12845EFull class, (connection, options) → MX25L12845EFull
|
|
6
|
+
|
|
7
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
8
|
+
const SPI_DEV = parseInt(process.env.SPI_DEV || '0', 10);
|
|
9
|
+
const SCRATCH = 0xFF0000;
|
|
10
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
11
|
+
const hex = (b) => Buffer.from(b).toString('hex').match(/../g).join(' ');
|
|
12
|
+
|
|
13
|
+
(async () => {
|
|
14
|
+
const connection = new SPIConnection(SPI_BUS, SPI_DEV, { mode: 0, maxSpeedHz: 8_000_000 }); // Open SPI bus, (busNumber, deviceNumber, mode=0, maxSpeedHz=8e6) → SPIConnection
|
|
15
|
+
const flash = new MX25L12845EFull(connection); // Create MX25L12845E driver, (connection, options{autoErase=false}) → MX25L12845EFull
|
|
16
|
+
await flash.init(); // Release deep power-down, check the JEDEC ID, () → Promise<void>
|
|
17
|
+
|
|
18
|
+
const id = await flash.jedecId(); // Read JEDEC ID, () → Promise<Buffer>
|
|
19
|
+
// C2 20 18 for the MX25L12845E
|
|
20
|
+
const res = await flash.electronicId(); // Read the old-style electronic ID (RES), () → Promise<number>
|
|
21
|
+
const [man, dev] = await flash.deviceIds(); // Read manufacturer + device ID (REMS), () → Promise<[number, number]>
|
|
22
|
+
console.log(`JEDEC ${hex(id)}, RES ${res.toString(16)}, REMS ${man.toString(16)} ${dev.toString(16)}`);
|
|
23
|
+
|
|
24
|
+
const sr = await flash.readStatus(); // Read the status register, () → Promise<number>
|
|
25
|
+
const bp = await flash.protection(); // Read BP3:0, () → Promise<number>
|
|
26
|
+
const scur = await flash.readSecurity(); // Read the security register, () → Promise<number>
|
|
27
|
+
console.log(`status 0x${sr.toString(16)}, BP=${bp}, security 0x${scur.toString(16)}`);
|
|
28
|
+
console.log('ESN:', hex(await flash.serialNumber())); // Read the serial-number OTP area, () → Promise<Buffer>
|
|
29
|
+
// the factory ESN when security bit 0 is set, blank (FF) otherwise
|
|
30
|
+
console.log('SFDP:', hex(await flash.readSfdp(0, 16))); // Read the SFDP table, (address, length) → Promise<Buffer>
|
|
31
|
+
// starts with the signature "SFDP"
|
|
32
|
+
|
|
33
|
+
await flash.eraseSector(SCRATCH); // Erase one 4 KiB sector, (address) → Promise<void>
|
|
34
|
+
// takes 60 ms typ, 300 ms max
|
|
35
|
+
const page = Buffer.from(Array.from({ length: 256 }, (_, i) => i));
|
|
36
|
+
await flash.pageProgram(SCRATCH, page); // Program one page (must not cross a page boundary), (address, data) → Promise<void>
|
|
37
|
+
// 1.4 ms typ for 256 bytes; bits only go 1 -> 0
|
|
38
|
+
const back = await flash.fastRead(SCRATCH, 256); // FAST_READ, (address, length) → Promise<Buffer>
|
|
39
|
+
// same data as readBytes, for clocks above 50 MHz
|
|
40
|
+
console.log('page program + fast_read:', page.equals(back));
|
|
41
|
+
|
|
42
|
+
await flash.eraseBlock32k(SCRATCH); // Erase a 32 KiB block, (address) → Promise<void>
|
|
43
|
+
// 0.5 s typ
|
|
44
|
+
await flash.eraseBlock64k(SCRATCH); // Erase a 64 KiB block, (address) → Promise<void>
|
|
45
|
+
// 0.7 s typ
|
|
46
|
+
console.log('after block erase:', hex(await flash.readBytes(SCRATCH, 16))); // Read bytes, (address, length) → Promise<Buffer>
|
|
47
|
+
// chipErase() (80 s typ, wipes all 16 MiB) is deliberately not called here.
|
|
48
|
+
|
|
49
|
+
const text = Buffer.from('erase-aware write');
|
|
50
|
+
await flash.writeBytes(SCRATCH + 100, text); // Write bytes, erasing the sector only when needed, (address, data) → Promise<void>
|
|
51
|
+
await flash.writeBytes(SCRATCH + 100, text); // the same bytes again: nothing is programmed or erased
|
|
52
|
+
console.log('text:', (await flash.readBytes(SCRATCH + 100, text.length)).toString());
|
|
53
|
+
|
|
54
|
+
await flash.deepPowerDown(); // Enter deep power-down, () → Promise<void>
|
|
55
|
+
// only RDP / RES are accepted afterwards
|
|
56
|
+
await flash.releaseDeepPowerDown(); // Leave deep power-down (RDP, waits tRES2), () → Promise<void>
|
|
57
|
+
console.log('busy:', await flash.isBusy()); // WIP bit, () → Promise<boolean>
|
|
58
|
+
await flash.waitReady(1000); // Wait for WIP = 0, (timeoutMs) → Promise<void>
|
|
59
|
+
|
|
60
|
+
console.log(flash.info()); // Geometry and capabilities, () → object
|
|
61
|
+
// 512-byte blocks, 4 KiB erase unit, needsErase
|
|
62
|
+
// setProtection(), writeStatus(), programOtp() and lockOtp() change non-volatile state; see the spec before using them.
|
|
63
|
+
|
|
64
|
+
await flash.eraseSector(SCRATCH);
|
|
65
|
+
console.log('done');
|
|
66
|
+
})();
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Wiring: SPI0 (MOSI, MISO, SCK, CE0) to the MX25L12845E, 3.3 V. Override with SPI_BUS, SPI_DEV.
|
|
3
|
+
// All erases and writes stay inside the last 64 KiB of the chip (0xFF0000..0xFFFFFF).
|
|
4
|
+
const { SPIConnection } = require('../../../src/connection/spi'); // SPIConnection class, (bus, dev, options) → SPIConnection
|
|
5
|
+
const { MX25L12845EFull } = require('../../../src/chips/memory/mx25l12845e'); // MX25L12845EFull class, (connection, options) → MX25L12845EFull
|
|
6
|
+
|
|
7
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
8
|
+
const SPI_DEV = parseInt(process.env.SPI_DEV || '0', 10);
|
|
9
|
+
const SCRATCH = 0xFF0000;
|
|
10
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
11
|
+
const hex = (b) => Buffer.from(b).toString('hex').match(/../g).join(' ');
|
|
12
|
+
|
|
13
|
+
const RING = 0xFFC000;
|
|
14
|
+
const SECTORS = 4;
|
|
15
|
+
const SLOT_LEN = 32;
|
|
16
|
+
const SLOTS = SECTORS * 4096 / SLOT_LEN;
|
|
17
|
+
|
|
18
|
+
(async () => {
|
|
19
|
+
// --- Bring up the chip ---
|
|
20
|
+
// Plain single-I/O SPI: init() releases deep power-down, checks the JEDEC ID and
|
|
21
|
+
// waits until the chip is idle. The serial-number area is the factory ESN on parts that have one.
|
|
22
|
+
const connection = new SPIConnection(SPI_BUS, SPI_DEV, { mode: 0, maxSpeedHz: 8_000_000 }); // Open SPI bus, (busNumber, deviceNumber, mode=0, maxSpeedHz=8e6) → SPIConnection
|
|
23
|
+
const flash = new MX25L12845EFull(connection); // Create MX25L12845E driver, (connection, options) → MX25L12845EFull
|
|
24
|
+
await flash.init(); // Release deep power-down, check the JEDEC ID, () → Promise<void>
|
|
25
|
+
console.log(`MX25L12845E, ${flash.size() / (1024 * 1024)} MiB, serial ${hex(await flash.serialNumber())}`); // Read the serial-number OTP area, () → Promise<Buffer>
|
|
26
|
+
|
|
27
|
+
// --- Append records to a ring of four sectors ---
|
|
28
|
+
// 32-byte records (sequence, tick, payload) fill a sector, 128 per sector. A blank slot
|
|
29
|
+
// (all 0xFF) needs no erase, so appending is just a page program; only when the writer
|
|
30
|
+
// enters the next sector is that sector erased first - one erase per 128 records.
|
|
31
|
+
for (let s = 0; s < SECTORS; s++) await flash.eraseSector(RING + s * 4096); // Erase one 4 KiB sector, (address) → Promise<void>
|
|
32
|
+
const t0 = Date.now();
|
|
33
|
+
let erases = 0;
|
|
34
|
+
let slot = 0;
|
|
35
|
+
for (let seq = 0; seq < 300; seq++) {
|
|
36
|
+
if (slot % 128 === 0 && seq >= SLOTS) { // wrapped into the next sector: make it blank
|
|
37
|
+
await flash.eraseSector(RING + Math.floor(slot / 128) * 4096);
|
|
38
|
+
erases++;
|
|
39
|
+
}
|
|
40
|
+
const rec = Buffer.alloc(SLOT_LEN, 0xA5);
|
|
41
|
+
rec.writeUInt32LE(seq, 0);
|
|
42
|
+
rec.writeUInt32LE(Date.now() - t0, 4);
|
|
43
|
+
await flash.pageProgram(RING + slot * SLOT_LEN, rec); // Program one record (inside a page), (address, data) → Promise<void>
|
|
44
|
+
slot = (slot + 1) % SLOTS;
|
|
45
|
+
}
|
|
46
|
+
console.log(`appended 300 records in ${Date.now() - t0} ms, ${erases} sector erases`);
|
|
47
|
+
|
|
48
|
+
// --- Verify by scanning the ring ---
|
|
49
|
+
// Every slot must carry a valid sequence number and the 0xA5 payload.
|
|
50
|
+
let good = 0;
|
|
51
|
+
let newest = 0;
|
|
52
|
+
const all = await flash.readBytes(RING, SECTORS * 4096); // Read bytes, (address, length) → Promise<Buffer>
|
|
53
|
+
for (let s = 0; s < SLOTS; s++) {
|
|
54
|
+
const rec = all.subarray(s * SLOT_LEN, (s + 1) * SLOT_LEN);
|
|
55
|
+
const seq = rec.readUInt32LE(0);
|
|
56
|
+
if (seq !== 0xFFFFFFFF && rec[20] === 0xA5) {
|
|
57
|
+
good++;
|
|
58
|
+
newest = Math.max(newest, seq);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
console.log(`ring holds ${good} valid records, newest sequence number ${newest}`);
|
|
62
|
+
|
|
63
|
+
// --- Leave the chip clean ---
|
|
64
|
+
for (let s = 0; s < SECTORS; s++) await flash.eraseSector(RING + s * 4096);
|
|
65
|
+
console.log('ring erased');
|
|
66
|
+
})();
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Wiring: SPI0 (MOSI, MISO, SCK, CE0) to the MX25L12845E, 3.3 V. Override with SPI_BUS, SPI_DEV.
|
|
3
|
+
// All erases and writes stay inside the last 64 KiB of the chip (0xFF0000..0xFFFFFF).
|
|
4
|
+
const { SPIConnection } = require('../../../src/connection/spi'); // SPIConnection class, (bus, dev, options) → SPIConnection
|
|
5
|
+
const { MX25L12845EMinimal } = require('../../../src/chips/memory/mx25l12845e'); // MX25L12845EMinimal class, (connection, options) → MX25L12845EMinimal
|
|
6
|
+
|
|
7
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
8
|
+
const SPI_DEV = parseInt(process.env.SPI_DEV || '0', 10);
|
|
9
|
+
const SCRATCH = 0xFF0000;
|
|
10
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
11
|
+
const hex = (b) => Buffer.from(b).toString('hex').match(/../g).join(' ');
|
|
12
|
+
|
|
13
|
+
(async () => {
|
|
14
|
+
const connection = new SPIConnection(SPI_BUS, SPI_DEV, { mode: 0, maxSpeedHz: 8_000_000 }); // Open SPI bus, (busNumber, deviceNumber, mode=0, maxSpeedHz=8e6) → SPIConnection
|
|
15
|
+
const flash = new MX25L12845EMinimal(connection); // Create MX25L12845E driver, (connection, options) → MX25L12845EMinimal
|
|
16
|
+
await flash.init(); // Release deep power-down, check the JEDEC ID, () → Promise<void>
|
|
17
|
+
|
|
18
|
+
console.log(`JEDEC ID ${hex(await flash.jedecId())}, ${flash.size() / (1024 * 1024)} MiB`); // Read JEDEC ID, () → Promise<Buffer>; capacity in bytes, () → number
|
|
19
|
+
for (let counter = 0; counter < 10; counter++) {
|
|
20
|
+
await flash.writeBytes(SCRATCH, Buffer.from([counter])); // Write bytes (erases the sector when needed), (address, data) → Promise<void>
|
|
21
|
+
const value = (await flash.readBytes(SCRATCH, 1))[0]; // Read bytes, (address, length) → Promise<Buffer>
|
|
22
|
+
console.log(`wrote 0x${counter.toString(16).padStart(2, '0')}, read 0x${value.toString(16).padStart(2, '0')}`);
|
|
23
|
+
await sleep(500);
|
|
24
|
+
}
|
|
25
|
+
await flash.eraseSector(SCRATCH); // Erase one 4 KiB sector, (address) → Promise<void>
|
|
26
|
+
console.log('done, scratch sector erased');
|
|
27
|
+
})();
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Wiring: SPI0 (MOSI, MISO, SCK) plus a free GPIO for the card's chip select (default line 25 on /dev/gpiochip0):
|
|
3
|
+
// the kernel cannot hold chip select across calls, so the connection drives CS itself (spi-device noChipSelect).
|
|
4
|
+
// Override with SPI_BUS, SPI_DEV, GPIOCHIP, CS_LINE. 3.3 V cards only.
|
|
5
|
+
const { Default } = require('opengpio'); // opengpio Default chip factory
|
|
6
|
+
const { SPIConnection } = require('../../../src/connection/spi'); // SPIConnection class, (bus, dev, options) → SPIConnection
|
|
7
|
+
const { GpioOutputPin } = require('../../../src/connection/output_pin'); // GpioOutputPin class, (output) → GpioOutputPin
|
|
8
|
+
const { SDSPIFull } = require('../../../src/chips/memory/sd_spi'); // SDSPIFull class, (connection, options) → SDSPIFull
|
|
9
|
+
|
|
10
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
11
|
+
const SPI_DEV = parseInt(process.env.SPI_DEV || '0', 10);
|
|
12
|
+
const GPIOCHIP = process.env.GPIOCHIP || '/dev/gpiochip0';
|
|
13
|
+
const CS_LINE = parseInt(process.env.CS_LINE || '25', 10);
|
|
14
|
+
|
|
15
|
+
const open = () => new SPIConnection(SPI_BUS, SPI_DEV, { // Open SPI bus with GPIO chip select, (busNumber, deviceNumber, mode, maxSpeedHz, csPin) → SPIConnection
|
|
16
|
+
mode: 0,
|
|
17
|
+
maxSpeedHz: 400_000, // SD init needs <= 400 kHz; the driver raises it afterwards
|
|
18
|
+
csPin: new GpioOutputPin(Default.output({ chip: GPIOCHIP, line: CS_LINE })),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
(async () => {
|
|
22
|
+
const sd = new SDSPIFull(open()); // Create SD driver, (connection, options) → SDSPIFull
|
|
23
|
+
await sd.init(); // Run the SPI-mode init sequence, () → Promise<void>
|
|
24
|
+
|
|
25
|
+
console.log('type:', sd.cardType()); // Card generation, () → number
|
|
26
|
+
// 1 = SDv1, 2 = SDv2, 3 = SDHC
|
|
27
|
+
console.log(sd.info()); // Geometry and capabilities, () → object
|
|
28
|
+
// block size, count, erase unit, name from the CID
|
|
29
|
+
console.log('CID:', (await sd.cid()).toString('hex')); // Read CID register, () → Promise<Buffer>
|
|
30
|
+
// manufacturer, name, revision, serial, date
|
|
31
|
+
console.log('CSD:', sd.csd().toString('hex')); // CSD register read at init, () → Buffer
|
|
32
|
+
console.log('OCR:', (await sd.ocr()).toString(16).padStart(8, '0')); // Read OCR, () → Promise<number>
|
|
33
|
+
// voltage window and CCS (block addressing) bit
|
|
34
|
+
console.log('present:', await sd.isPresent()); // Card responds, () → Promise<boolean>
|
|
35
|
+
|
|
36
|
+
// Use the last 8 blocks of the card as scratch, restoring their contents.
|
|
37
|
+
const scratch = sd.blockCount() - 8;
|
|
38
|
+
const saved = await sd.read(scratch, 8); // Read blocks, (block, count) → Promise<Buffer>
|
|
39
|
+
// CMD18 multi-block read for count > 1
|
|
40
|
+
const pattern = Buffer.from(Array.from({ length: 8 * 512 }, (_, i) => (i * 7 + 3) & 0xFF));
|
|
41
|
+
await sd.write(scratch, pattern); // Write blocks, (block, data) → Promise<void>
|
|
42
|
+
// CMD25 multi-block write with stop token
|
|
43
|
+
await sd.sync(); // Wait until programming is done, () → Promise<void>
|
|
44
|
+
console.log('roundtrip:', (await sd.read(scratch, 8)).equals(pattern) ? 'ok' : 'MISMATCH');
|
|
45
|
+
|
|
46
|
+
await sd.setCrc(false); // Disable CRC checking, (enabled) → Promise<void>
|
|
47
|
+
// CMD59 off; data CRC16 is no longer verified
|
|
48
|
+
await sd.setCrc(true);
|
|
49
|
+
console.log('set_speed:', await sd.setSpeed(6_000_000)); // Change SCK frequency, (hz) → Promise<boolean>
|
|
50
|
+
// sets the spidev clock
|
|
51
|
+
await sd.erase(scratch, 8); // Erase blocks, (block, count) → Promise<void>
|
|
52
|
+
// CMD32/33/38; contents afterwards are card dependent
|
|
53
|
+
await sd.write(scratch, saved); // restore the original contents
|
|
54
|
+
console.log('done');
|
|
55
|
+
})();
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Wiring: SPI0 (MOSI, MISO, SCK) plus a free GPIO for the card's chip select (default line 25 on /dev/gpiochip0):
|
|
3
|
+
// the kernel cannot hold chip select across calls, so the connection drives CS itself (spi-device noChipSelect).
|
|
4
|
+
// Override with SPI_BUS, SPI_DEV, GPIOCHIP, CS_LINE. 3.3 V cards only.
|
|
5
|
+
const { Default } = require('opengpio'); // opengpio Default chip factory
|
|
6
|
+
const { SPIConnection } = require('../../../src/connection/spi'); // SPIConnection class, (bus, dev, options) → SPIConnection
|
|
7
|
+
const { GpioOutputPin } = require('../../../src/connection/output_pin'); // GpioOutputPin class, (output) → GpioOutputPin
|
|
8
|
+
const { SDSPIFull } = require('../../../src/chips/memory/sd_spi'); // SDSPIFull class, (connection, options) → SDSPIFull
|
|
9
|
+
|
|
10
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
11
|
+
const SPI_DEV = parseInt(process.env.SPI_DEV || '0', 10);
|
|
12
|
+
const GPIOCHIP = process.env.GPIOCHIP || '/dev/gpiochip0';
|
|
13
|
+
const CS_LINE = parseInt(process.env.CS_LINE || '25', 10);
|
|
14
|
+
|
|
15
|
+
const open = () => new SPIConnection(SPI_BUS, SPI_DEV, { // Open SPI bus with GPIO chip select, (busNumber, deviceNumber, mode, maxSpeedHz, csPin) → SPIConnection
|
|
16
|
+
mode: 0,
|
|
17
|
+
maxSpeedHz: 400_000, // SD init needs <= 400 kHz; the driver raises it afterwards
|
|
18
|
+
csPin: new GpioOutputPin(Default.output({ chip: GPIOCHIP, line: CS_LINE })),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const BLOCKS = 64;
|
|
22
|
+
|
|
23
|
+
(async () => {
|
|
24
|
+
// --- Bring up the card ---
|
|
25
|
+
// The card starts in SD mode; the driver sends 80 clocks with CS high, CMD0 with CS
|
|
26
|
+
// low to enter SPI mode, then negotiates the card type. SCK runs at 400 kHz during
|
|
27
|
+
// init and is raised to 12 MHz afterwards.
|
|
28
|
+
const sd = new SDSPIFull(open()); // Create SD driver, (connection, options) → SDSPIFull
|
|
29
|
+
await sd.init(); // Run the SPI-mode init sequence, () → Promise<void>
|
|
30
|
+
const info = sd.info(); // Geometry and capabilities, () → object
|
|
31
|
+
console.log(`card ${info.name}, ${info.blockCount / 2048} MiB`);
|
|
32
|
+
|
|
33
|
+
// --- Log samples to a raw block range (filesystem-free data logger) ---
|
|
34
|
+
// Each 512-byte block holds a header (sequence number, tick) and 126 samples.
|
|
35
|
+
// The last blocks of the card are used and restored afterwards, so a card that
|
|
36
|
+
// carries a filesystem keeps its data (only the tail of the volume is touched).
|
|
37
|
+
const first = sd.blockCount() - BLOCKS;
|
|
38
|
+
const saved = await sd.read(first, BLOCKS); // Read blocks, (block, count) → Promise<Buffer>
|
|
39
|
+
const t0 = Date.now();
|
|
40
|
+
for (let seq = 0; seq < BLOCKS; seq++) {
|
|
41
|
+
const block = Buffer.alloc(512);
|
|
42
|
+
block.writeUInt32LE(seq, 0);
|
|
43
|
+
block.writeUInt32LE((Date.now() - t0) >>> 0, 4);
|
|
44
|
+
for (let i = 2; i < 128; i++) block.writeUInt32LE(seq * 1000 + i, i * 4);
|
|
45
|
+
await sd.write(first + seq, block); // Write blocks, (block, data) → Promise<void>
|
|
46
|
+
}
|
|
47
|
+
await sd.sync(); // Wait until programming is done, () → Promise<void>
|
|
48
|
+
console.log(`wrote ${BLOCKS} blocks in ${Date.now() - t0} ms`);
|
|
49
|
+
|
|
50
|
+
// --- Verify by reading back ---
|
|
51
|
+
// Read every block back and recompute the expected sample values; any single
|
|
52
|
+
// mismatch (or a CRC16 error thrown by the driver) is counted.
|
|
53
|
+
let bad = 0;
|
|
54
|
+
for (let seq = 0; seq < BLOCKS; seq++) {
|
|
55
|
+
let block;
|
|
56
|
+
try { block = await sd.read(first + seq, 1); } catch (_e) { bad++; continue; } // Read blocks, (block, count) → Promise<Buffer>
|
|
57
|
+
let ok = block.readUInt32LE(0) === seq;
|
|
58
|
+
for (let i = 2; i < 128 && ok; i++) ok = block.readUInt32LE(i * 4) === seq * 1000 + i;
|
|
59
|
+
if (!ok) bad++;
|
|
60
|
+
}
|
|
61
|
+
console.log(`verify: ${bad} bad blocks`);
|
|
62
|
+
|
|
63
|
+
// --- Restore the original contents ---
|
|
64
|
+
await sd.write(first, saved);
|
|
65
|
+
await sd.sync();
|
|
66
|
+
console.log('restored');
|
|
67
|
+
})();
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Wiring: SPI0 (MOSI, MISO, SCK) plus a free GPIO for the card's chip select (default line 25 on /dev/gpiochip0):
|
|
3
|
+
// the kernel cannot hold chip select across calls, so the connection drives CS itself (spi-device noChipSelect).
|
|
4
|
+
// Override with SPI_BUS, SPI_DEV, GPIOCHIP, CS_LINE. 3.3 V cards only.
|
|
5
|
+
const { Default } = require('opengpio'); // opengpio Default chip factory
|
|
6
|
+
const { SPIConnection } = require('../../../src/connection/spi'); // SPIConnection class, (bus, dev, options) → SPIConnection
|
|
7
|
+
const { GpioOutputPin } = require('../../../src/connection/output_pin'); // GpioOutputPin class, (output) → GpioOutputPin
|
|
8
|
+
const { SDSPIMinimal } = require('../../../src/chips/memory/sd_spi'); // SDSPIMinimal class, (connection, options) → SDSPIMinimal
|
|
9
|
+
|
|
10
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
11
|
+
const SPI_DEV = parseInt(process.env.SPI_DEV || '0', 10);
|
|
12
|
+
const GPIOCHIP = process.env.GPIOCHIP || '/dev/gpiochip0';
|
|
13
|
+
const CS_LINE = parseInt(process.env.CS_LINE || '25', 10);
|
|
14
|
+
|
|
15
|
+
const open = () => new SPIConnection(SPI_BUS, SPI_DEV, { // Open SPI bus with GPIO chip select, (busNumber, deviceNumber, mode, maxSpeedHz, csPin) → SPIConnection
|
|
16
|
+
mode: 0,
|
|
17
|
+
maxSpeedHz: 400_000, // SD init needs <= 400 kHz; the driver raises it afterwards
|
|
18
|
+
csPin: new GpioOutputPin(Default.output({ chip: GPIOCHIP, line: CS_LINE })),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
(async () => {
|
|
22
|
+
const sd = new SDSPIMinimal(open()); // Create SD driver, (connection, options) → SDSPIMinimal
|
|
23
|
+
await sd.init(); // Run the SPI-mode init sequence, () → Promise<void>
|
|
24
|
+
|
|
25
|
+
console.log(`capacity: ${sd.blockCount()} blocks = ${sd.blockCount() / 2048} MiB`); // Capacity in 512-byte blocks, () → number
|
|
26
|
+
while (true) {
|
|
27
|
+
const block = await sd.readBlock(0); // Read one 512-byte block, (block) → Promise<Buffer>
|
|
28
|
+
console.log('block 0:', block.subarray(0, 16).toString('hex'));
|
|
29
|
+
console.log(`boot signature: ${block.subarray(510, 512).toString('hex')}`);
|
|
30
|
+
await new Promise(r => setTimeout(r, 2000));
|
|
31
|
+
}
|
|
32
|
+
})();
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { DRV8830Full } = require('periph/src/chips/motor/drv8830');
|
|
5
|
+
|
|
6
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
7
|
+
|
|
8
|
+
(async () => {
|
|
9
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
10
|
+
const connection = new I2CConnection(bus, DRV8830Full.I2C_ADDRESS);
|
|
11
|
+
const motor = new DRV8830Full(connection); // Create DRV8830 Full driver, (connection)
|
|
12
|
+
await motor.init(); // Confirm device presence, () → None
|
|
13
|
+
// plain CONTROL read; the chip has no identity register
|
|
14
|
+
|
|
15
|
+
await motor.drive(2.5); // Drive at regulated voltage, (voltage V, + = forward) → None
|
|
16
|
+
// maps 2.5 V to the nearest VSET code and sets IN1=1, IN2=0
|
|
17
|
+
await sleep(1000);
|
|
18
|
+
const out = await motor.readOutput(); // Read back CONTROL, () → {voltage V, direction}
|
|
19
|
+
// decodes VSET to volts and IN1/IN2 to a direction name
|
|
20
|
+
console.log(`commanded ${out.voltage.toFixed(2)} V ${out.direction}`);
|
|
21
|
+
|
|
22
|
+
await motor.drive(-1.5); // Drive at regulated voltage, (voltage V, - = reverse) → None
|
|
23
|
+
// a negative voltage sets IN1=0, IN2=1
|
|
24
|
+
await sleep(1000);
|
|
25
|
+
|
|
26
|
+
await motor.setOutput(37, true, false); // Write raw CONTROL fields, (vset 6–63, in1, in2) → None
|
|
27
|
+
// VSET 37 is ~2.97 V forward; codes 0–5 throw RangeError
|
|
28
|
+
await sleep(1000);
|
|
29
|
+
|
|
30
|
+
await motor.brake(); // Short-brake, () → None
|
|
31
|
+
// IN1=IN2=1 drives both outputs high
|
|
32
|
+
await sleep(500);
|
|
33
|
+
await motor.stop(); // Coast to standby, () → None
|
|
34
|
+
// IN1=IN2=0 leaves both outputs high-impedance
|
|
35
|
+
|
|
36
|
+
const fault = await motor.readFault(); // Read fault status, () → {fault, ocp, uvlo, ots, ilimit}
|
|
37
|
+
// does not clear — latched OCP/ILIMIT keep the bridge off
|
|
38
|
+
console.log('fault', fault);
|
|
39
|
+
await motor.clearFault(); // Clear fault bits, () → None
|
|
40
|
+
// writes CLEAR=1; re-enables a latched-off bridge
|
|
41
|
+
|
|
42
|
+
await motor.onInterrupt((status) => console.log('fault interrupt', status)); // Subscribe to FAULTn, (callback) → None
|
|
43
|
+
// falls back to 5 ms polling when no intPin is wired
|
|
44
|
+
const status = await motor.pollInterrupt(); // Poll fault status, () → {fault, ocp, uvlo, ots, ilimit}
|
|
45
|
+
// same as readFault(); never clears implicitly
|
|
46
|
+
await motor.offInterrupt(); // Unsubscribe, () → None
|
|
47
|
+
console.log('poll', status);
|
|
48
|
+
|
|
49
|
+
await connection.close();
|
|
50
|
+
})();
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Battery-powered toy motor controller: holds a regulated 3.0 V forward, then
|
|
4
|
+
// 2.0 V reverse, printing the commanded output every second — the DRV8830
|
|
5
|
+
// keeps that average voltage constant as the battery sags. Brakes, then
|
|
6
|
+
// coasts. After every drive() call the fault register is checked; a fault
|
|
7
|
+
// (e.g. a stalled motor tripping ILIMIT) stops the motor and clears it.
|
|
8
|
+
|
|
9
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
10
|
+
const { DRV8830Full } = require('periph/src/chips/motor/drv8830');
|
|
11
|
+
|
|
12
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
13
|
+
|
|
14
|
+
(async () => {
|
|
15
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
16
|
+
const connection = new I2CConnection(bus, DRV8830Full.I2C_ADDRESS);
|
|
17
|
+
const motor = new DRV8830Full(connection); // Create DRV8830 Full driver, (connection)
|
|
18
|
+
await motor.init(); // Confirm device presence, () → None
|
|
19
|
+
|
|
20
|
+
async function checkFault() {
|
|
21
|
+
// --- Recover from a fault instead of leaving the bridge latched off ---
|
|
22
|
+
// OCP and ILIMIT disable the H-bridge until CLEAR is written; stop first
|
|
23
|
+
// so the motor does not lurch back to the old command on clear.
|
|
24
|
+
const f = await motor.readFault(); // Read fault status, () → {fault, ocp, uvlo, ots, ilimit}
|
|
25
|
+
if (f.fault) {
|
|
26
|
+
const names = ['ocp', 'uvlo', 'ots', 'ilimit'].filter((k) => f[k]).map((k) => k.toUpperCase());
|
|
27
|
+
console.log('fault:', names.join(', '));
|
|
28
|
+
await motor.stop(); // Coast to standby, () → None
|
|
29
|
+
await motor.clearFault(); // Clear fault bits, () → None
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function run(voltage, seconds) {
|
|
34
|
+
// --- Hold a regulated voltage and watch it stay put ---
|
|
35
|
+
// The chip PWM-regulates the bridge against VCC internally, so the
|
|
36
|
+
// commanded voltage (and motor speed) holds while the battery discharges.
|
|
37
|
+
await motor.drive(voltage); // Drive at regulated voltage, (voltage V, signed) → None
|
|
38
|
+
await checkFault();
|
|
39
|
+
for (let i = 0; i < seconds; i++) {
|
|
40
|
+
await sleep(1000);
|
|
41
|
+
const out = await motor.readOutput(); // Read back CONTROL, () → {voltage V, direction}
|
|
42
|
+
console.log(`${out.direction.padEnd(7)} ${out.voltage.toFixed(2)} V`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
await run(3.0, 5);
|
|
47
|
+
await run(-2.0, 5);
|
|
48
|
+
|
|
49
|
+
// --- Stop quickly, then release ---
|
|
50
|
+
// Braking shorts the winding for a fast stop; coasting afterwards removes
|
|
51
|
+
// the load so the motor does not sit shorted indefinitely.
|
|
52
|
+
await motor.brake(); // Short-brake, () → None
|
|
53
|
+
await sleep(500);
|
|
54
|
+
await motor.stop(); // Coast to standby, () → None
|
|
55
|
+
|
|
56
|
+
await connection.close();
|
|
57
|
+
})();
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { DRV8830Minimal } = require('periph/src/chips/motor/drv8830');
|
|
5
|
+
|
|
6
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
7
|
+
|
|
8
|
+
(async () => {
|
|
9
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
10
|
+
const connection = new I2CConnection(bus, DRV8830Minimal.I2C_ADDRESS);
|
|
11
|
+
const motor = new DRV8830Minimal(connection); // Create DRV8830 driver, (connection)
|
|
12
|
+
await motor.init(); // Confirm device presence, () → None
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < 5; i++) {
|
|
15
|
+
await motor.drive(3.0); // Drive at regulated voltage, (voltage V, + = forward) → None
|
|
16
|
+
await sleep(2000);
|
|
17
|
+
await motor.drive(-3.0); // Drive at regulated voltage, (voltage V, - = reverse) → None
|
|
18
|
+
await sleep(2000);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
await motor.stop(); // Coast to standby, () → None
|
|
22
|
+
await connection.close();
|
|
23
|
+
})();
|
|
@@ -6,7 +6,7 @@ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
|
6
6
|
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
|
|
7
7
|
|
|
8
8
|
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
9
|
-
const bmp = new BMP280Full(connection); // Create BMP280 driver, (connection
|
|
9
|
+
const bmp = new BMP280Full(connection); // Create BMP280 driver, (connection)
|
|
10
10
|
|
|
11
11
|
(async () => {
|
|
12
12
|
const cid = await bmp.chipId(); // Read chip ID, () → number
|
|
@@ -10,7 +10,7 @@ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
|
10
10
|
// --- Weather monitoring preset: lowest power, forced mode ---
|
|
11
11
|
// BMP280 datasheet Table 7: ×1/×1, filter off, forced mode.
|
|
12
12
|
// One sample per second for 30 seconds.
|
|
13
|
-
const bmp = new BMP280Full(connection); // Create BMP280 driver, (connection
|
|
13
|
+
const bmp = new BMP280Full(connection); // Create BMP280 driver, (connection)
|
|
14
14
|
|
|
15
15
|
(async () => {
|
|
16
16
|
await bmp.configure(BMP280Full.OSRS_X1, BMP280Full.OSRS_X1, BMP280Full.MODE_FORCED, BMP280Full.FILTER_OFF, BMP280Full.T_SB_0_5_MS); // Configure chip, (osrsT=×1, osrsP=×1, mode=forced, filter=off, tSb=0) → undefined
|