periph 1.0.1 → 1.2.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 +54 -12
- package/examples/accelerometer/adxl345/complete.js +52 -0
- package/examples/accelerometer/adxl345/demo.js +34 -0
- package/examples/accelerometer/adxl345/minimal.js +18 -0
- package/examples/accelerometer/adxl362/complete.js +57 -0
- package/examples/accelerometer/adxl362/demo.js +51 -0
- package/examples/accelerometer/adxl362/minimal.js +21 -0
- package/examples/adc_dac/ad7705/complete.js +32 -0
- package/examples/adc_dac/ad7705/demo.js +42 -0
- package/examples/adc_dac/ad7705/minimal.js +16 -0
- package/examples/adc_dac/ad7706/complete.js +35 -0
- package/examples/adc_dac/ad7706/demo.js +44 -0
- package/examples/adc_dac/ad7706/minimal.js +16 -0
- package/examples/adc_dac/hx710a/complete.js +45 -0
- package/examples/adc_dac/hx710a/demo.js +52 -0
- package/examples/adc_dac/hx710a/minimal.js +16 -0
- package/examples/adc_dac/hx710b/complete.js +45 -0
- package/examples/adc_dac/hx710b/demo.js +64 -0
- package/examples/adc_dac/hx710b/minimal.js +16 -0
- package/examples/adc_dac/hx711/complete.js +42 -0
- package/examples/adc_dac/hx711/demo.js +35 -0
- package/examples/adc_dac/hx711/minimal.js +16 -0
- package/examples/adc_dac/mcp4725/complete.js +19 -17
- package/examples/adc_dac/mcp4725/demo.js +5 -5
- package/examples/adc_dac/mcp4725/minimal.js +8 -6
- package/examples/adc_dac/mcp4728/complete.js +43 -0
- package/examples/adc_dac/mcp4728/demo.js +41 -0
- package/examples/adc_dac/mcp4728/minimal.js +13 -0
- package/examples/adc_dac/pcf8591/complete.js +41 -0
- package/examples/adc_dac/pcf8591/demo.js +39 -0
- package/examples/adc_dac/pcf8591/minimal.js +19 -0
- package/examples/comms/mcp2515/complete.js +75 -0
- package/examples/comms/mcp2515/demo.js +66 -0
- package/examples/comms/mcp2515/minimal.js +27 -0
- package/examples/comms/rda5807m/complete.js +55 -0
- package/examples/comms/rda5807m/demo.js +68 -0
- package/examples/comms/rda5807m/minimal.js +15 -0
- package/examples/comms/rfm9x/complete.js +50 -0
- package/examples/comms/rfm9x/demo.js +55 -0
- package/examples/comms/rfm9x/minimal.js +20 -0
- package/examples/display/pcf8576/complete.js +35 -0
- package/examples/display/pcf8576/demo.js +48 -0
- package/examples/display/pcf8576/minimal.js +20 -0
- package/examples/environmental/aht21/complete.js +32 -0
- package/examples/environmental/aht21/demo.js +46 -0
- package/examples/environmental/aht21/minimal.js +12 -0
- package/examples/environmental/bme280/complete.js +37 -0
- package/examples/environmental/bme280/demo.js +67 -0
- package/examples/environmental/bme280/minimal.js +20 -0
- package/examples/environmental/bme680/complete.js +40 -0
- package/examples/environmental/bme680/demo.js +54 -0
- package/examples/environmental/bme680/minimal.js +21 -0
- package/examples/gas/ens160/complete.js +54 -0
- package/examples/gas/ens160/demo.js +54 -0
- package/examples/gas/ens160/minimal.js +29 -0
- package/examples/gnss/neo6/complete.js +49 -0
- package/examples/gnss/neo6/demo.js +54 -0
- package/examples/gnss/neo6/minimal.js +30 -0
- package/examples/gyroscope/l3g4200d/complete.js +56 -0
- package/examples/gyroscope/l3g4200d/demo.js +49 -0
- package/examples/gyroscope/l3g4200d/minimal.js +19 -0
- package/examples/gyroscope/l3gd20h/complete.js +42 -0
- package/examples/gyroscope/l3gd20h/demo.js +29 -0
- package/examples/gyroscope/l3gd20h/minimal.js +13 -0
- package/examples/humidity/dht11/complete.js +22 -0
- package/examples/humidity/dht11/demo.js +38 -0
- package/examples/humidity/dht11/minimal.js +18 -0
- package/examples/imu/mpu6050/complete.js +58 -0
- package/examples/imu/mpu6050/demo.js +37 -0
- package/examples/imu/mpu6050/minimal.js +14 -0
- package/examples/imu/mpu9250/complete.js +65 -0
- package/examples/imu/mpu9250/demo.js +53 -0
- package/examples/imu/mpu9250/minimal.js +21 -0
- package/examples/imu/mpu9255/complete.js +73 -0
- package/examples/imu/mpu9255/demo.js +73 -0
- package/examples/imu/mpu9255/minimal.js +21 -0
- package/examples/io_expander/mcp23017/complete.js +70 -77
- package/examples/io_expander/mcp23017/demo.js +37 -33
- package/examples/io_expander/mcp23017/minimal.js +15 -11
- package/examples/io_expander/pcf8574/complete.js +60 -65
- package/examples/io_expander/pcf8574/demo.js +30 -25
- package/examples/io_expander/pcf8574/minimal.js +15 -11
- package/examples/io_expander/pcf8575/complete.js +24 -20
- package/examples/io_expander/pcf8575/demo.js +27 -23
- package/examples/io_expander/pcf8575/minimal.js +16 -12
- package/examples/io_expander/tpic6b595/complete.js +57 -0
- package/examples/io_expander/tpic6b595/demo.js +75 -0
- package/examples/io_expander/tpic6b595/minimal.js +26 -0
- package/examples/led/apa102/complete.js +72 -0
- package/examples/led/apa102/demo.js +92 -0
- package/examples/led/apa102/minimal.js +21 -0
- package/examples/led/sk6812rgbw/complete.js +42 -40
- package/examples/led/sk6812rgbw/demo.js +16 -16
- package/examples/led/sk6812rgbw/minimal.js +15 -17
- package/examples/led/ws2812b/complete.js +40 -38
- package/examples/led/ws2812b/demo.js +16 -16
- package/examples/led/ws2812b/minimal.js +13 -14
- package/examples/led/ws2814/complete.js +71 -0
- package/examples/led/ws2814/demo.js +82 -0
- package/examples/led/ws2814/minimal.js +23 -0
- package/examples/light/apds-9930/complete.js +46 -0
- package/examples/light/apds-9930/demo.js +31 -0
- package/examples/light/apds-9930/minimal.js +20 -0
- package/examples/light/apds9960/complete.js +64 -0
- package/examples/light/apds9960/demo.js +34 -0
- package/examples/light/apds9960/minimal.js +12 -0
- package/examples/magnetometer/as5600/complete.js +31 -29
- package/examples/magnetometer/as5600/demo.js +35 -33
- package/examples/magnetometer/as5600/minimal.js +7 -7
- package/examples/magnetometer/hmc5883l/complete.js +47 -0
- package/examples/magnetometer/hmc5883l/demo.js +50 -0
- package/examples/magnetometer/hmc5883l/minimal.js +15 -0
- package/examples/memory/24aa02uid/complete.js +42 -0
- package/examples/memory/24aa02uid/demo.js +42 -0
- package/examples/memory/24aa02uid/minimal.js +12 -0
- package/examples/other/mpr121/complete.js +50 -0
- package/examples/other/mpr121/demo.js +30 -0
- package/examples/other/mpr121/minimal.js +17 -0
- package/examples/power/ade7953/complete.js +44 -0
- package/examples/power/ade7953/demo.js +28 -0
- package/examples/power/ade7953/minimal.js +15 -0
- package/examples/power/ina219/complete.js +20 -18
- package/examples/power/ina219/demo.js +15 -13
- package/examples/power/ina219/minimal.js +8 -8
- package/examples/power/ina226/complete.js +24 -22
- package/examples/power/ina226/demo.js +22 -20
- package/examples/power/ina226/minimal.js +5 -5
- package/examples/power/ina3221/complete.js +51 -49
- package/examples/power/ina3221/demo.js +13 -13
- package/examples/power/ina3221/minimal.js +8 -8
- package/examples/pressure/bmp085/complete.js +29 -0
- package/examples/pressure/bmp085/demo.js +34 -0
- package/examples/pressure/bmp085/minimal.js +19 -0
- package/examples/pressure/bmp180/complete.js +20 -17
- package/examples/pressure/bmp180/demo.js +24 -22
- package/examples/pressure/bmp180/minimal.js +13 -11
- package/examples/pressure/bmp280/complete.js +29 -22
- package/examples/pressure/bmp280/demo.js +42 -23
- package/examples/pressure/bmp280/minimal.js +13 -11
- package/examples/pressure/bmp384/complete.js +29 -0
- package/examples/pressure/bmp384/demo.js +40 -0
- package/examples/pressure/bmp384/minimal.js +18 -0
- package/examples/pressure/bmp581/complete.js +34 -0
- package/examples/pressure/bmp581/demo.js +56 -0
- package/examples/pressure/bmp581/minimal.js +19 -0
- package/examples/pressure/lps22df/complete.js +50 -0
- package/examples/pressure/lps22df/demo.js +41 -0
- package/examples/pressure/lps22df/minimal.js +19 -0
- package/examples/pressure/lps28dfw/complete.js +37 -0
- package/examples/pressure/lps28dfw/demo.js +33 -0
- package/examples/pressure/lps28dfw/minimal.js +19 -0
- package/examples/pressure/lps33hw/complete.js +43 -0
- package/examples/pressure/lps33hw/demo.js +43 -0
- package/examples/pressure/lps33hw/minimal.js +19 -0
- package/examples/rfid/mfrc522/complete.js +44 -0
- package/examples/rfid/mfrc522/demo.js +70 -0
- package/examples/rfid/mfrc522/minimal.js +17 -0
- package/package.json +5 -1
- package/src/chips/accelerometer/adxl345.js +482 -0
- package/src/chips/accelerometer/adxl362.js +594 -0
- package/src/chips/adc_dac/ad7705.js +345 -0
- package/src/chips/adc_dac/ad7706.js +331 -0
- package/src/chips/adc_dac/hx710a.js +184 -0
- package/src/chips/adc_dac/hx710b.js +186 -0
- package/src/chips/adc_dac/hx711.js +165 -0
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +297 -0
- package/src/chips/adc_dac/pcf8591.js +196 -0
- package/src/chips/comms/mcp2515.js +659 -0
- package/src/chips/comms/rda5807m.js +393 -0
- package/src/chips/comms/rfm9x.js +590 -0
- package/src/chips/display/pcf8576.js +232 -0
- package/src/chips/environmental/aht21.js +185 -0
- package/src/chips/environmental/bme280.js +385 -0
- package/src/chips/environmental/bme680.js +512 -0
- package/src/chips/gas/ens160.js +298 -0
- package/src/chips/gnss/neo6.js +438 -0
- package/src/chips/gyroscope/l3g4200d.js +366 -0
- package/src/chips/gyroscope/l3gd20h.js +314 -0
- package/src/chips/humidity/dht11.js +135 -0
- package/src/chips/imu/mpu6050.js +289 -0
- package/src/chips/imu/mpu9250.js +394 -0
- package/src/chips/imu/mpu9255.js +447 -0
- package/src/chips/io_expander/mcp23017.js +240 -157
- package/src/chips/io_expander/pcf8574.js +149 -124
- package/src/chips/io_expander/pcf8575.js +164 -135
- package/src/chips/io_expander/tpic6b595.js +242 -0
- package/src/chips/led/_NeoPixelRGBBase.js +204 -0
- package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
- package/src/chips/led/apa102.js +236 -0
- package/src/chips/led/sk6812rgbw.js +15 -149
- package/src/chips/led/ws2812b.js +14 -139
- package/src/chips/led/ws2814.js +49 -0
- package/src/chips/light/apds-9930.js +342 -0
- package/src/chips/light/apds9960.js +499 -0
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +182 -0
- package/src/chips/other/mpr121.js +375 -0
- package/src/chips/power/ade7953.js +427 -0
- package/src/chips/power/ina219.js +45 -40
- package/src/chips/power/ina226.js +50 -45
- package/src/chips/power/ina3221.js +95 -76
- package/src/chips/pressure/bmp085.js +251 -0
- package/src/chips/pressure/bmp180.js +54 -37
- package/src/chips/pressure/bmp280.js +188 -206
- package/src/chips/pressure/bmp384.js +410 -0
- package/src/chips/pressure/bmp581.js +489 -0
- package/src/chips/pressure/lps22df.js +341 -0
- package/src/chips/pressure/lps28dfw.js +314 -0
- package/src/chips/pressure/lps33hw.js +255 -0
- package/src/chips/rfid/mfrc522.js +674 -0
- package/src/connection/connection.js +111 -0
- package/src/connection/dhtxx.js +138 -0
- package/src/connection/dhtxx_mock.js +39 -0
- package/src/connection/hx711.js +132 -0
- package/src/connection/hx711_mock.js +52 -0
- package/src/{transport → connection}/i2c.js +13 -9
- package/src/connection/i2c_mock.js +91 -0
- package/src/connection/input_pin.js +156 -0
- package/src/connection/neo6_mock.js +45 -0
- package/src/{transport → connection}/neopixel.js +23 -4
- package/src/connection/output_pin.js +41 -0
- package/src/connection/sipo.js +159 -0
- package/src/{transport → connection}/smbus.js +14 -10
- package/src/{transport → connection}/spi.js +12 -8
- package/src/connection/spi_mock.js +151 -0
- package/src/{transport → connection}/uart.js +33 -26
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/pressure/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/pressure/.gitkeep +0 -0
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* MCP2515 stand-alone CAN 2.0B controller driver (SPI).
|
|
5
|
+
*
|
|
6
|
+
* Supports standard (11-bit) and extended (29-bit) identifiers, data frames
|
|
7
|
+
* of 0–8 bytes, and CAN bus speeds up to 1 Mbit/s. Three TX buffers, two RX
|
|
8
|
+
* buffers, six acceptance filters, two acceptance masks.
|
|
9
|
+
*
|
|
10
|
+
* The driver is built around an internal `_MCP2515Base` that owns the
|
|
11
|
+
* register-level logic and the SPI instruction helpers. `MCP2515Minimal`
|
|
12
|
+
* adds the primary send/recv API; `MCP2515Full` extends Minimal with mode
|
|
13
|
+
* control, error counters, acceptance filters/masks, abort and one-shot
|
|
14
|
+
* support, and explicit per-buffer TX selection.
|
|
15
|
+
*
|
|
16
|
+
* Default configuration baked into Minimal:
|
|
17
|
+
* - Standard 11-bit or extended 29-bit ID accepted (set per-call)
|
|
18
|
+
* - All three TX buffers enabled; TXB0 used by default with priority 3
|
|
19
|
+
* - All acceptance filters and masks pass every ID (RXM[1:0]=11 in
|
|
20
|
+
* RXB0CTRL and RXB1CTRL; RXM0SIDH..RXM1EID0 = 0x00)
|
|
21
|
+
* - BUKT=1 in RXB0CTRL (RXB0 → RXB1 overflow rollover)
|
|
22
|
+
* - CANINTE=0x00 (polled operation; INT pin not used)
|
|
23
|
+
* - OSM=0 (retransmit on error or loss of arbitration)
|
|
24
|
+
* - Loopback mode not enabled — Normal mode at init
|
|
25
|
+
* - Bit timing: 125 kbit/s with 8 MHz oscillator (CNF1=0x01, CNF2=0xBA,
|
|
26
|
+
* CNF3=0x03); overridable via `init()` arguments.
|
|
27
|
+
*
|
|
28
|
+
* All SPI transactions follow the chip's instruction set:
|
|
29
|
+
* RESET 0xC0
|
|
30
|
+
* READ 0x03 + addr + n bytes out
|
|
31
|
+
* READ RX BUFFER 0x90|n + bytes out
|
|
32
|
+
* WRITE 0x02 + addr + n bytes in
|
|
33
|
+
* LOAD TX BUFFER 0x40|n + bytes in
|
|
34
|
+
* RTS 0x80|mask
|
|
35
|
+
* READ STATUS 0xA0 + 1 byte out
|
|
36
|
+
* RX STATUS 0xB0 + 1 byte out
|
|
37
|
+
* BIT MODIFY 0x05 + addr + mask + data
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
// SPI instruction set
|
|
41
|
+
const INSTR_RESET = 0xC0;
|
|
42
|
+
const INSTR_READ = 0x03;
|
|
43
|
+
const INSTR_READ_RX_BUF = 0x90;
|
|
44
|
+
const INSTR_WRITE = 0x02;
|
|
45
|
+
const INSTR_LOAD_TX_BUF = 0x40;
|
|
46
|
+
const INSTR_RTS = 0x80;
|
|
47
|
+
const INSTR_READ_STATUS = 0xA0;
|
|
48
|
+
const INSTR_RX_STATUS = 0xB0;
|
|
49
|
+
const INSTR_BIT_MODIFY = 0x05;
|
|
50
|
+
|
|
51
|
+
// Register addresses
|
|
52
|
+
const REG_CANSTAT = 0x0E;
|
|
53
|
+
const REG_CANCTRL = 0x0F;
|
|
54
|
+
const REG_CNF3 = 0x28;
|
|
55
|
+
const REG_CNF2 = 0x29;
|
|
56
|
+
const REG_CNF1 = 0x2A;
|
|
57
|
+
const REG_CANINTE = 0x2B;
|
|
58
|
+
const REG_CANINTF = 0x2C;
|
|
59
|
+
const REG_EFLG = 0x2D;
|
|
60
|
+
|
|
61
|
+
const REG_RXB0CTRL = 0x60;
|
|
62
|
+
const REG_RXB1CTRL = 0x70;
|
|
63
|
+
|
|
64
|
+
const REG_TXB0CTRL = 0x30;
|
|
65
|
+
const REG_TXB1CTRL = 0x40;
|
|
66
|
+
const REG_TXB2CTRL = 0x50;
|
|
67
|
+
|
|
68
|
+
const REG_RXM0SIDH = 0x20;
|
|
69
|
+
const REG_RXM1SIDH = 0x24;
|
|
70
|
+
|
|
71
|
+
const REG_TEC = 0x1C;
|
|
72
|
+
const REG_REC = 0x1D;
|
|
73
|
+
|
|
74
|
+
const CANSTAT_OPMOD_MASK = 0xE0;
|
|
75
|
+
const CANSTAT_OPMOD_NORMAL = 0x00;
|
|
76
|
+
const CANSTAT_OPMOD_SLEEP = 0x20;
|
|
77
|
+
const CANSTAT_OPMOD_LOOPBACK = 0x40;
|
|
78
|
+
const CANSTAT_OPMOD_LISTEN_ONLY = 0x60;
|
|
79
|
+
const CANSTAT_OPMOD_CONFIG = 0x80;
|
|
80
|
+
|
|
81
|
+
const CANCTRL_REQOP_NORMAL = 0x00;
|
|
82
|
+
const CANCTRL_REQOP_SLEEP = 0x20;
|
|
83
|
+
const CANCTRL_REQOP_LOOPBACK = 0x40;
|
|
84
|
+
const CANCTRL_REQOP_LISTEN_ONLY = 0x60;
|
|
85
|
+
const CANCTRL_REQOP_CONFIG = 0x80;
|
|
86
|
+
|
|
87
|
+
const TXBnCTRL_TXREQ = 0x08;
|
|
88
|
+
const TXBnCTRL_TXP_MASK = 0x03;
|
|
89
|
+
|
|
90
|
+
const RTS_MASK_TXB0 = 0x01;
|
|
91
|
+
const RTS_MASK_TXB1 = 0x02;
|
|
92
|
+
const RTS_MASK_TXB2 = 0x04;
|
|
93
|
+
|
|
94
|
+
const RXBnCTRL_RXM_MASK = 0x60;
|
|
95
|
+
const RXBnCTRL_RXM_ANY = 0x60;
|
|
96
|
+
const RXBnCTRL_BUKT = 0x04;
|
|
97
|
+
|
|
98
|
+
const CANINTF_RX0IF = 0x01;
|
|
99
|
+
const CANINTF_RX1IF = 0x02;
|
|
100
|
+
|
|
101
|
+
const EFLG_RX0OVR = 0x40;
|
|
102
|
+
const EFLG_RX1OVR = 0x80;
|
|
103
|
+
|
|
104
|
+
const CNF_PRESCALER_8MHZ = {
|
|
105
|
+
125: [0x01, 0xBA, 0x03],
|
|
106
|
+
250: [0x00, 0xBA, 0x03],
|
|
107
|
+
500: [0x00, 0x91, 0x01],
|
|
108
|
+
1000: [0x00, 0x80, 0x00],
|
|
109
|
+
};
|
|
110
|
+
const CNF_PRESCALER_16MHZ = {
|
|
111
|
+
125: [0x03, 0xBA, 0x03],
|
|
112
|
+
250: [0x01, 0xBA, 0x03],
|
|
113
|
+
500: [0x00, 0xBA, 0x03],
|
|
114
|
+
1000: [0x00, 0x91, 0x01],
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A CAN 2.0B data or remote frame.
|
|
119
|
+
*/
|
|
120
|
+
class CanFrame {
|
|
121
|
+
/**
|
|
122
|
+
* @param {number} id - 11-bit (standard) or 29-bit (extended) CAN identifier.
|
|
123
|
+
* @param {Buffer|Uint8Array} [data=Buffer.alloc(0)] - Payload bytes, 0–8 bytes.
|
|
124
|
+
* @param {boolean} [extended=false] - True if this is a 29-bit extended-ID frame.
|
|
125
|
+
* @param {boolean} [rtr=false] - True if this is a remote transmission request frame.
|
|
126
|
+
*/
|
|
127
|
+
constructor(id, data = Buffer.alloc(0), extended = false, rtr = false) {
|
|
128
|
+
if (extended) {
|
|
129
|
+
if (!Number.isInteger(id) || id < 0 || id > 0x1FFFFFFF) {
|
|
130
|
+
throw new RangeError(`extended id 0x${id.toString(16)} out of range [0, 0x1FFFFFFF]`);
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
if (!Number.isInteger(id) || id < 0 || id > 0x7FF) {
|
|
134
|
+
throw new RangeError(`standard id 0x${id.toString(16)} out of range [0, 0x7FF]`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
138
|
+
if (buf.length > 8) {
|
|
139
|
+
throw new RangeError(`CAN data length ${buf.length} exceeds 8 bytes`);
|
|
140
|
+
}
|
|
141
|
+
this.id = id;
|
|
142
|
+
this.data = buf;
|
|
143
|
+
this.extended = !!extended;
|
|
144
|
+
this.rtr = !!rtr;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Base class for the MCP2515 CAN controller — register logic and SPI helpers.
|
|
150
|
+
*
|
|
151
|
+
* Owns the connection and exposes register read/write helpers plus the SPI
|
|
152
|
+
* instruction set. The Minimal-stage public API (`init`, `send`, `recv`)
|
|
153
|
+
* lives in `MCP2515Minimal`; Full-only functionality is implemented as
|
|
154
|
+
* underscore-prefixed private helpers here and re-exposed via
|
|
155
|
+
* `MCP2515FullMixin`.
|
|
156
|
+
*
|
|
157
|
+
* Not exported — construct one of the `MCP2515Minimal` /
|
|
158
|
+
* `MCP2515Full` classes.
|
|
159
|
+
*/
|
|
160
|
+
class _MCP2515Base {
|
|
161
|
+
/**
|
|
162
|
+
* @param {object} connection - Configured SPI connection (write, read, writeRead).
|
|
163
|
+
* @param {number} [bitrateKbps=125] - Bus bitrate in kbit/s (125, 250, 500, or 1000).
|
|
164
|
+
* @param {number} [oscMhz=8] - Oscillator frequency in MHz (8 or 16).
|
|
165
|
+
*/
|
|
166
|
+
constructor(connection, bitrateKbps = 125, oscMhz = 8) {
|
|
167
|
+
this._connection = connection;
|
|
168
|
+
if (bitrateKbps !== 125 && bitrateKbps !== 250 && bitrateKbps !== 500 && bitrateKbps !== 1000) {
|
|
169
|
+
throw new RangeError('bitrateKbps must be one of 125, 250, 500, 1000');
|
|
170
|
+
}
|
|
171
|
+
if (oscMhz !== 8 && oscMhz !== 16) {
|
|
172
|
+
throw new RangeError('oscMhz must be 8 or 16');
|
|
173
|
+
}
|
|
174
|
+
this._bitrateKbps = bitrateKbps;
|
|
175
|
+
this._oscMhz = oscMhz;
|
|
176
|
+
|
|
177
|
+
this._runInit(bitrateKbps, oscMhz);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async _runInit(bitrateKbps, oscMhz) {
|
|
181
|
+
await this._reset();
|
|
182
|
+
await this._sleep(5);
|
|
183
|
+
await this._waitOpMode(CANSTAT_OPMOD_CONFIG);
|
|
184
|
+
|
|
185
|
+
const cnf = this._cnfFor(bitrateKbps, oscMhz);
|
|
186
|
+
await this._writeReg(REG_CNF1, cnf[0]);
|
|
187
|
+
await this._writeReg(REG_CNF2, cnf[1]);
|
|
188
|
+
await this._writeReg(REG_CNF3, cnf[2]);
|
|
189
|
+
|
|
190
|
+
await this._writeReg(REG_RXB0CTRL, RXBnCTRL_RXM_ANY | RXBnCTRL_BUKT);
|
|
191
|
+
await this._writeReg(REG_RXB1CTRL, RXBnCTRL_RXM_ANY);
|
|
192
|
+
|
|
193
|
+
await this._writeReg(REG_CANINTE, 0x00);
|
|
194
|
+
|
|
195
|
+
await this._writeReg(REG_TXB0CTRL, TXBnCTRL_TXP_MASK);
|
|
196
|
+
await this._writeReg(REG_TXB1CTRL, TXBnCTRL_TXP_MASK);
|
|
197
|
+
await this._writeReg(REG_TXB2CTRL, TXBnCTRL_TXP_MASK);
|
|
198
|
+
|
|
199
|
+
await this._writeReg(REG_CANCTRL, CANCTRL_REQOP_NORMAL);
|
|
200
|
+
await this._waitOpMode(CANSTAT_OPMOD_NORMAL);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
_cnfFor(bitrateKbps, oscMhz) {
|
|
204
|
+
const table = (oscMhz === 8) ? CNF_PRESCALER_8MHZ : CNF_PRESCALER_16MHZ;
|
|
205
|
+
return table[bitrateKbps];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
_sleep(ms) {
|
|
209
|
+
return new Promise(r => setTimeout(r, ms));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async _reset() {
|
|
213
|
+
await this._connection.write(Buffer.from([INSTR_RESET]));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async _writeReg(reg, value) {
|
|
217
|
+
await this._connection.write(Buffer.from([INSTR_WRITE, reg & 0xFF, value & 0xFF]));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async _readReg(reg) {
|
|
221
|
+
const out = await this._connection.writeRead(Buffer.from([INSTR_READ, reg & 0xFF]), 1);
|
|
222
|
+
return out[0];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async _modifyReg(reg, mask, value) {
|
|
226
|
+
await this._connection.write(Buffer.from([
|
|
227
|
+
INSTR_BIT_MODIFY, reg & 0xFF, mask & 0xFF, value & 0xFF,
|
|
228
|
+
]));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async _readStatus() {
|
|
232
|
+
const out = await this._connection.writeRead(Buffer.from([INSTR_READ_STATUS]), 1);
|
|
233
|
+
return out[0];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async _rts(mask) {
|
|
237
|
+
await this._connection.write(Buffer.from([INSTR_RTS | (mask & 0x07)]));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async _waitOpMode(target, timeoutMs = 100) {
|
|
241
|
+
const targetMasked = target & CANSTAT_OPMOD_MASK;
|
|
242
|
+
const start = Date.now();
|
|
243
|
+
while (Date.now() - start < timeoutMs) {
|
|
244
|
+
const cur = await this._readReg(REG_CANSTAT);
|
|
245
|
+
if ((cur & CANSTAT_OPMOD_MASK) === targetMasked) return;
|
|
246
|
+
await this._sleep(5);
|
|
247
|
+
}
|
|
248
|
+
throw new Error(`timed out waiting for CANSTAT.OPMOD = 0x${targetMasked.toString(16)}`);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
_packId(canId, extended) {
|
|
252
|
+
if (extended) {
|
|
253
|
+
return [
|
|
254
|
+
(canId >> 21) & 0xFF,
|
|
255
|
+
(((canId >> 18) & 0x07) << 5) | 0x08 | ((canId >> 16) & 0x03),
|
|
256
|
+
(canId >> 8) & 0xFF,
|
|
257
|
+
canId & 0xFF,
|
|
258
|
+
];
|
|
259
|
+
}
|
|
260
|
+
return [
|
|
261
|
+
(canId >> 3) & 0xFF,
|
|
262
|
+
(canId & 0x07) << 5,
|
|
263
|
+
0,
|
|
264
|
+
0,
|
|
265
|
+
];
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
_unpackId(sidh, sidl, eid8, eid0, ide) {
|
|
269
|
+
if (ide) {
|
|
270
|
+
return (sidh << 21) | ((sidl >> 5) << 18) | ((sidl & 0x03) << 16) | (eid8 << 8) | eid0;
|
|
271
|
+
}
|
|
272
|
+
return (sidh << 3) | (sidl >> 5);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
_txBufOffset(bufIndex) {
|
|
276
|
+
if (bufIndex === 0) return 0;
|
|
277
|
+
if (bufIndex === 1) return 2;
|
|
278
|
+
if (bufIndex === 2) return 4;
|
|
279
|
+
throw new RangeError('bufIndex must be 0, 1, or 2');
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
_txRegBase(bufIndex) {
|
|
283
|
+
return [REG_TXB0CTRL, REG_TXB1CTRL, REG_TXB2CTRL][bufIndex];
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async _txFreeBuf(timeoutMs = 10) {
|
|
287
|
+
const start = Date.now();
|
|
288
|
+
while (Date.now() - start < timeoutMs) {
|
|
289
|
+
const status = await this._readStatus();
|
|
290
|
+
if (!(status & 0x04)) return 0;
|
|
291
|
+
if (!(status & 0x10)) return 1;
|
|
292
|
+
if (!(status & 0x40)) return 2;
|
|
293
|
+
await this._sleep(1);
|
|
294
|
+
}
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async _loadTxBuffer(bufIndex, canId, data, extended, rtr) {
|
|
299
|
+
const [sidh, sidl, eid8, eid0] = this._packId(canId, extended);
|
|
300
|
+
const dlc = (data.length & 0x0F) | (rtr ? 0x40 : 0x00);
|
|
301
|
+
const base = this._txBufOffset(bufIndex);
|
|
302
|
+
const payload = Buffer.alloc(13);
|
|
303
|
+
payload[0] = sidh;
|
|
304
|
+
payload[1] = sidl;
|
|
305
|
+
payload[2] = eid8;
|
|
306
|
+
payload[3] = eid0;
|
|
307
|
+
payload[4] = dlc;
|
|
308
|
+
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
309
|
+
for (let i = 0; i < buf.length; i++) payload[5 + i] = buf[i];
|
|
310
|
+
const cmd = Buffer.from([INSTR_LOAD_TX_BUF | base]);
|
|
311
|
+
await this._connection.write(Buffer.concat([cmd, payload]));
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
async _send(canId, data, extended, rtr, bufIndex) {
|
|
315
|
+
let freeBuf = bufIndex;
|
|
316
|
+
if (bufIndex === null || bufIndex === undefined) {
|
|
317
|
+
freeBuf = await this._txFreeBuf();
|
|
318
|
+
if (freeBuf === null) throw new Error('all TX buffers busy');
|
|
319
|
+
}
|
|
320
|
+
await this._loadTxBuffer(freeBuf, canId, data, extended, rtr);
|
|
321
|
+
await this._rts(1 << freeBuf);
|
|
322
|
+
const base = this._txRegBase(freeBuf);
|
|
323
|
+
const start = Date.now();
|
|
324
|
+
while (Date.now() - start < 1000) {
|
|
325
|
+
const ctrl = await this._readReg(base);
|
|
326
|
+
if (!(ctrl & TXBnCTRL_TXREQ)) return;
|
|
327
|
+
await this._sleep(1);
|
|
328
|
+
}
|
|
329
|
+
throw new Error(`TX buffer ${freeBuf} did not clear TXREQ within 1 s`);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
async _readRxBuffer(rxIndex) {
|
|
333
|
+
const offset = (rxIndex === 0) ? 0 : 4;
|
|
334
|
+
const data = await this._connection.writeRead(
|
|
335
|
+
Buffer.from([INSTR_READ_RX_BUF | offset]),
|
|
336
|
+
13,
|
|
337
|
+
);
|
|
338
|
+
const sidh = data[0], sidl = data[1], eid8 = data[2], eid0 = data[3], dlc = data[4];
|
|
339
|
+
const ide = (sidl & 0x08) !== 0;
|
|
340
|
+
const rtr = ide ? !!(dlc & 0x40) : !!(sidl & 0x10);
|
|
341
|
+
const canId = this._unpackId(sidh, sidl, eid8, eid0, ide);
|
|
342
|
+
const length = dlc & 0x0F;
|
|
343
|
+
return new CanFrame(canId, data.subarray(5, 5 + length), ide, rtr);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
async _pollRx(timeoutMs) {
|
|
347
|
+
if (timeoutMs <= 0) {
|
|
348
|
+
const status = await this._readStatus();
|
|
349
|
+
if (status & CANINTF_RX0IF) return await this._readRxBuffer(0);
|
|
350
|
+
if (status & CANINTF_RX1IF) return await this._readRxBuffer(1);
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
const start = Date.now();
|
|
354
|
+
while (Date.now() - start < timeoutMs) {
|
|
355
|
+
const status = await this._readStatus();
|
|
356
|
+
if (status & CANINTF_RX0IF) return await this._readRxBuffer(0);
|
|
357
|
+
if (status & CANINTF_RX1IF) return await this._readRxBuffer(1);
|
|
358
|
+
await this._sleep(1);
|
|
359
|
+
}
|
|
360
|
+
return null;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async _setMode(mode) {
|
|
364
|
+
let reqop;
|
|
365
|
+
switch (mode) {
|
|
366
|
+
case 'normal': reqop = CANCTRL_REQOP_NORMAL; break;
|
|
367
|
+
case 'loopback': reqop = CANCTRL_REQOP_LOOPBACK; break;
|
|
368
|
+
case 'listen_only': reqop = CANCTRL_REQOP_LISTEN_ONLY; break;
|
|
369
|
+
case 'sleep': reqop = CANCTRL_REQOP_SLEEP; break;
|
|
370
|
+
case 'config': reqop = CANCTRL_REQOP_CONFIG; break;
|
|
371
|
+
default: throw new RangeError(`mode must be 'normal', 'loopback', 'listen_only', 'sleep', or 'config'`);
|
|
372
|
+
}
|
|
373
|
+
await this._modifyReg(REG_CANCTRL, 0xE0, reqop);
|
|
374
|
+
await this._waitOpMode(reqop);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
async _getMode() {
|
|
378
|
+
const opmod = (await this._readReg(REG_CANSTAT)) & CANSTAT_OPMOD_MASK;
|
|
379
|
+
if (opmod === CANSTAT_OPMOD_NORMAL) return 'normal';
|
|
380
|
+
if (opmod === CANSTAT_OPMOD_SLEEP) return 'sleep';
|
|
381
|
+
if (opmod === CANSTAT_OPMOD_LOOPBACK) return 'loopback';
|
|
382
|
+
if (opmod === CANSTAT_OPMOD_LISTEN_ONLY) return 'listen_only';
|
|
383
|
+
if (opmod === CANSTAT_OPMOD_CONFIG) return 'config';
|
|
384
|
+
return 'unknown';
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
async _setFilter(n, canId, extended) {
|
|
388
|
+
if (n < 0 || n > 5) throw new RangeError('filterNum must be 0–5');
|
|
389
|
+
const base = n * 4;
|
|
390
|
+
const [sidh, sidl, eid8, eid0] = this._packId(canId, extended);
|
|
391
|
+
await this._writeReg(base, sidh);
|
|
392
|
+
await this._writeReg(base + 1, sidl);
|
|
393
|
+
await this._writeReg(base + 2, eid8);
|
|
394
|
+
await this._writeReg(base + 3, eid0);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
async _setMask(n, mask, extended) {
|
|
398
|
+
if (n < 0 || n > 1) throw new RangeError('maskNum must be 0 or 1');
|
|
399
|
+
const base = (n === 0) ? REG_RXM0SIDH : REG_RXM1SIDH;
|
|
400
|
+
const [sidh, sidl, eid8, eid0] = this._packId(mask, extended);
|
|
401
|
+
await this._writeReg(base, sidh);
|
|
402
|
+
await this._writeReg(base + 1, sidl);
|
|
403
|
+
await this._writeReg(base + 2, eid8);
|
|
404
|
+
await this._writeReg(base + 3, eid0);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
async _setRxMode(buf, mode) {
|
|
408
|
+
if (buf !== 0 && buf !== 1) throw new RangeError('buf must be 0 or 1');
|
|
409
|
+
if (mode !== 0 && mode !== 1 && mode !== 3) {
|
|
410
|
+
throw new RangeError('mode must be 0 (std filter), 1 (ext filter), or 3 (accept all)');
|
|
411
|
+
}
|
|
412
|
+
const reg = (buf === 0) ? REG_RXB0CTRL : REG_RXB1CTRL;
|
|
413
|
+
await this._modifyReg(reg, RXBnCTRL_RXM_MASK, (mode & 0x03) << 5);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
async _readErrors() {
|
|
417
|
+
const tec = await this._readReg(REG_TEC);
|
|
418
|
+
const rec = await this._readReg(REG_REC);
|
|
419
|
+
const eflg = await this._readReg(REG_EFLG);
|
|
420
|
+
return { tec, rec, eflg };
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
async _clearOverflow(buf) {
|
|
424
|
+
if (buf === 0) await this._modifyReg(REG_EFLG, EFLG_RX0OVR, 0x00);
|
|
425
|
+
else if (buf === 1) await this._modifyReg(REG_EFLG, EFLG_RX1OVR, 0x00);
|
|
426
|
+
else throw new RangeError('buf must be 0 or 1');
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
async _abortTx() {
|
|
430
|
+
await this._modifyReg(REG_CANCTRL, 0x10, 0x10);
|
|
431
|
+
const start = Date.now();
|
|
432
|
+
while (Date.now() - start < 500) {
|
|
433
|
+
const ctrl = await this._readReg(REG_CANCTRL);
|
|
434
|
+
if (!(ctrl & 0x10)) return;
|
|
435
|
+
await this._sleep(5);
|
|
436
|
+
}
|
|
437
|
+
throw new Error('ABAT did not clear within 500 ms');
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
async _setOneShot(enable) {
|
|
441
|
+
await this._modifyReg(REG_CANCTRL, 0x08, enable ? 0x08 : 0x00);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* MCP2515 minimal driver — send/recv with default configuration.
|
|
447
|
+
*
|
|
448
|
+
* Runs the chip's full init sequence at construction: software reset,
|
|
449
|
+
* Configuration mode, accept-all filters, RXB0→RXB1 rollover, polled
|
|
450
|
+
* operation (no interrupts), TXB0/1/2 priority 3, Normal mode.
|
|
451
|
+
*/
|
|
452
|
+
class MCP2515Minimal extends _MCP2515Base {
|
|
453
|
+
/**
|
|
454
|
+
* @param {object} connection - Configured SPI connection bound to the device.
|
|
455
|
+
* @param {number} [bitrateKbps=125] - Bus bitrate in kbit/s (125, 250, 500, or 1000).
|
|
456
|
+
* @param {number} [oscMhz=8] - Oscillator frequency in MHz (8 or 16).
|
|
457
|
+
*/
|
|
458
|
+
constructor(connection, bitrateKbps = 125, oscMhz = 8) {
|
|
459
|
+
super(connection, bitrateKbps, oscMhz);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Re-run the full init sequence with new bitrate/oscillator values.
|
|
464
|
+
* @param {number} [bitrateKbps=125] - Bus bitrate in kbit/s.
|
|
465
|
+
* @param {number} [oscMhz=8] - Oscillator frequency in MHz.
|
|
466
|
+
* @returns {Promise<void>}
|
|
467
|
+
*/
|
|
468
|
+
async init(bitrateKbps = 125, oscMhz = 8) {
|
|
469
|
+
if (bitrateKbps !== 125 && bitrateKbps !== 250 && bitrateKbps !== 500 && bitrateKbps !== 1000) {
|
|
470
|
+
throw new RangeError('bitrateKbps must be one of 125, 250, 500, 1000');
|
|
471
|
+
}
|
|
472
|
+
if (oscMhz !== 8 && oscMhz !== 16) {
|
|
473
|
+
throw new RangeError('oscMhz must be 8 or 16');
|
|
474
|
+
}
|
|
475
|
+
this._bitrateKbps = bitrateKbps;
|
|
476
|
+
this._oscMhz = oscMhz;
|
|
477
|
+
await this._runInit(bitrateKbps, oscMhz);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Send a CAN frame.
|
|
482
|
+
*
|
|
483
|
+
* Loads the next free TX buffer (TXB0 first, then TXB1, TXB2), issues
|
|
484
|
+
* RTS, and waits up to 1 s for TXREQ to clear.
|
|
485
|
+
*
|
|
486
|
+
* @param {number} id - 11-bit (standard) or 29-bit (extended) identifier.
|
|
487
|
+
* @param {Buffer|Uint8Array} [data=Buffer.alloc(0)] - Payload bytes, 0–8 bytes.
|
|
488
|
+
* @param {boolean} [extended=false] - True for 29-bit extended-ID frame.
|
|
489
|
+
* @returns {Promise<void>}
|
|
490
|
+
*/
|
|
491
|
+
async send(id, data = Buffer.alloc(0), extended = false) {
|
|
492
|
+
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
493
|
+
await this._send(id, buf, extended, false, null);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Receive a single CAN frame.
|
|
498
|
+
*
|
|
499
|
+
* Polls READ STATUS until a frame is available in RXB0 or RXB1, or the
|
|
500
|
+
* timeout elapses.
|
|
501
|
+
*
|
|
502
|
+
* @param {number} [timeoutMs=0] - Receive timeout in ms; 0 means non-blocking poll.
|
|
503
|
+
* @returns {Promise<CanFrame|null>} Received frame, or null on timeout.
|
|
504
|
+
*/
|
|
505
|
+
async recv(timeoutMs = 0) {
|
|
506
|
+
return await this._pollRx(timeoutMs);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Mixin that adds Full-stage functionality on top of a `MCP2515Minimal` subclass.
|
|
512
|
+
*
|
|
513
|
+
* `_MCP2515Base` structurally lacks these methods (they are not defined
|
|
514
|
+
* anywhere on it), so applying this mixin is the only way to obtain them.
|
|
515
|
+
*
|
|
516
|
+
* Usage: `class MCP2515Full extends _MCP2515FullMixin(MCP2515Minimal) {}`.
|
|
517
|
+
*
|
|
518
|
+
* @param {typeof _MCP2515Base} Base - A `MCP2515Minimal` class (itself extending `_MCP2515Base`).
|
|
519
|
+
* @returns {typeof _MCP2515Base} A subclass of `Base` with the Full API added.
|
|
520
|
+
*/
|
|
521
|
+
const _MCP2515FullMixin = (Base) => class extends Base {
|
|
522
|
+
/**
|
|
523
|
+
* Send a CAN frame using a specific TX buffer.
|
|
524
|
+
* @param {number} id - 11-bit (standard) or 29-bit (extended) identifier.
|
|
525
|
+
* @param {Buffer|Uint8Array} [data=Buffer.alloc(0)] - Payload bytes, 0–8 bytes.
|
|
526
|
+
* @param {boolean} [extended=false] - True for 29-bit extended-ID frame.
|
|
527
|
+
* @param {number} [buf=0] - TX buffer index 0, 1, or 2.
|
|
528
|
+
* @returns {Promise<void>}
|
|
529
|
+
*/
|
|
530
|
+
async sendBuffered(id, data = Buffer.alloc(0), extended = false, buf = 0) {
|
|
531
|
+
const bytes = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
532
|
+
await this._send(id, bytes, extended, false, buf);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Configure an acceptance filter.
|
|
537
|
+
* @param {number} filterNum - Filter index 0–5.
|
|
538
|
+
* @param {number} id - Identifier value to match.
|
|
539
|
+
* @param {boolean} [extended=false] - True for 29-bit extended-ID filter.
|
|
540
|
+
* @returns {Promise<void>}
|
|
541
|
+
*/
|
|
542
|
+
async setFilter(filterNum, id, extended = false) {
|
|
543
|
+
const prevMode = await this._getMode();
|
|
544
|
+
if (prevMode !== 'config') await this._setMode('config');
|
|
545
|
+
try {
|
|
546
|
+
await this._setFilter(filterNum, id, extended);
|
|
547
|
+
} finally {
|
|
548
|
+
if (prevMode !== 'config') await this._setMode(prevMode);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* Configure an acceptance mask.
|
|
554
|
+
*
|
|
555
|
+
* Mask bit = 1: filter bit must match. Mask bit = 0: don't care.
|
|
556
|
+
*
|
|
557
|
+
* @param {number} maskNum - 0 (RXB0; filters 0–1) or 1 (RXB1; filters 2–5).
|
|
558
|
+
* @param {number} mask - Mask value.
|
|
559
|
+
* @param {boolean} [extended=false] - True for 29-bit extended-ID mask.
|
|
560
|
+
* @returns {Promise<void>}
|
|
561
|
+
*/
|
|
562
|
+
async setMask(maskNum, mask, extended = false) {
|
|
563
|
+
const prevMode = await this._getMode();
|
|
564
|
+
if (prevMode !== 'config') await this._setMode('config');
|
|
565
|
+
try {
|
|
566
|
+
await this._setMask(maskNum, mask, extended);
|
|
567
|
+
} finally {
|
|
568
|
+
if (prevMode !== 'config') await this._setMode(prevMode);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Set RXM[1:0] for the given RX buffer.
|
|
574
|
+
* @param {number} buf - RX buffer index 0 or 1.
|
|
575
|
+
* @param {number} mode - 0=standard filter, 1=extended filter, 3=accept all.
|
|
576
|
+
* @returns {Promise<void>}
|
|
577
|
+
*/
|
|
578
|
+
async setRxMode(buf, mode) {
|
|
579
|
+
await this._setRxMode(buf, mode);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Switch operating mode and wait until CANSTAT confirms.
|
|
584
|
+
* @param {string} mode - 'normal', 'loopback', 'listen_only', 'sleep', or 'config'.
|
|
585
|
+
* @returns {Promise<void>}
|
|
586
|
+
*/
|
|
587
|
+
async setMode(mode) {
|
|
588
|
+
await this._setMode(mode);
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* Return the current operating mode from CANSTAT.OPMOD.
|
|
593
|
+
* @returns {Promise<string>} 'normal', 'sleep', 'loopback', 'listen_only', 'config', or 'unknown'.
|
|
594
|
+
*/
|
|
595
|
+
async getMode() {
|
|
596
|
+
return await this._getMode();
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Issue a SPI RESET command; chip returns to Configuration mode.
|
|
601
|
+
*
|
|
602
|
+
* After a reset, all configuration is back to POR defaults. Re-run
|
|
603
|
+
* `init()` to set the configuration again.
|
|
604
|
+
*
|
|
605
|
+
* @returns {Promise<void>}
|
|
606
|
+
*/
|
|
607
|
+
async reset() {
|
|
608
|
+
await this._reset();
|
|
609
|
+
await this._sleep(5);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Return TEC, REC, and EFLG register values.
|
|
614
|
+
* @returns {Promise<{tec:number, rec:number, eflg:number}>}
|
|
615
|
+
*/
|
|
616
|
+
async readErrors() {
|
|
617
|
+
return await this._readErrors();
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* Clear the RX0OVR or RX1OVR flag in EFLG.
|
|
622
|
+
* @param {number} buf - RX buffer 0 or 1.
|
|
623
|
+
* @returns {Promise<void>}
|
|
624
|
+
*/
|
|
625
|
+
async clearOverflow(buf) {
|
|
626
|
+
await this._clearOverflow(buf);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Set ABAT in CANCTRL; poll until cleared by hardware.
|
|
631
|
+
* @returns {Promise<void>}
|
|
632
|
+
*/
|
|
633
|
+
async abortTx() {
|
|
634
|
+
await this._abortTx();
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Enable or disable one-shot mode (OSM in CANCTRL).
|
|
639
|
+
*
|
|
640
|
+
* In one-shot mode the chip does not retransmit on error or loss of
|
|
641
|
+
* arbitration.
|
|
642
|
+
*
|
|
643
|
+
* @param {boolean} enable - True to enable, false to disable.
|
|
644
|
+
* @returns {Promise<void>}
|
|
645
|
+
*/
|
|
646
|
+
async setOneShot(enable) {
|
|
647
|
+
await this._setOneShot(enable);
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* MCP2515 full driver — adds mode/filter/error-counter methods.
|
|
653
|
+
*
|
|
654
|
+
* Inherits send/recv/init from `MCP2515Minimal`; re-exposes the
|
|
655
|
+
* Full-only methods from `_MCP2515Base` via `_MCP2515FullMixin`.
|
|
656
|
+
*/
|
|
657
|
+
class MCP2515Full extends _MCP2515FullMixin(MCP2515Minimal) {}
|
|
658
|
+
|
|
659
|
+
module.exports = { CanFrame, MCP2515Minimal, MCP2515Full };
|