periph 1.1.0 → 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 +7 -7
- package/examples/adc_dac/hx711/demo.js +6 -6
- package/examples/adc_dac/hx711/minimal.js +7 -7
- 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 +24 -22
- package/examples/adc_dac/mcp4728/demo.js +22 -22
- package/examples/adc_dac/mcp4728/minimal.js +8 -6
- 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 +18 -15
- package/examples/display/pcf8576/demo.js +6 -6
- package/examples/display/pcf8576/minimal.js +12 -10
- package/examples/environmental/aht21/complete.js +15 -13
- package/examples/environmental/aht21/demo.js +8 -6
- package/examples/environmental/aht21/minimal.js +5 -5
- package/examples/environmental/bme280/complete.js +23 -20
- package/examples/environmental/bme280/demo.js +14 -14
- package/examples/environmental/bme280/minimal.js +13 -11
- package/examples/environmental/bme680/complete.js +25 -22
- package/examples/environmental/bme680/demo.js +38 -36
- package/examples/environmental/bme680/minimal.js +14 -12
- package/examples/gas/ens160/complete.js +29 -27
- package/examples/gas/ens160/demo.js +39 -37
- package/examples/gas/ens160/minimal.js +17 -15
- 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 +27 -25
- package/examples/imu/mpu6050/demo.js +29 -26
- package/examples/imu/mpu6050/minimal.js +6 -6
- 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 +45 -43
- package/examples/light/apds9960/demo.js +11 -9
- package/examples/light/apds9960/minimal.js +5 -5
- 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 +23 -21
- package/examples/memory/24aa02uid/demo.js +32 -30
- package/examples/memory/24aa02uid/minimal.js +5 -5
- 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 +23 -20
- package/examples/pressure/bmp280/demo.js +44 -41
- 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 +15 -15
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +57 -44
- 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 +42 -33
- package/src/chips/environmental/aht21.js +52 -37
- package/src/chips/environmental/bme280.js +85 -66
- package/src/chips/environmental/bme680.js +110 -87
- package/src/chips/gas/ens160.js +82 -64
- 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 +89 -66
- 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 +158 -123
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +32 -29
- 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 +72 -53
- 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/{transport → connection}/hx711.js +49 -22
- 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/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _CMD_IDLE = 0x00;
|
|
4
|
+
const _CMD_MEM = 0x01;
|
|
5
|
+
const _CMD_RANDOM_ID = 0x02;
|
|
6
|
+
const _CMD_CALC_CRC = 0x03;
|
|
7
|
+
const _CMD_TRANSMIT = 0x04;
|
|
8
|
+
const _CMD_NO_CMD_CHANGE = 0x07;
|
|
9
|
+
const _CMD_RECEIVE = 0x08;
|
|
10
|
+
const _CMD_TRANSCEIVE = 0x0C;
|
|
11
|
+
const _CMD_MFAUTHENT = 0x0E;
|
|
12
|
+
const _CMD_SOFT_RESET = 0x0F;
|
|
13
|
+
|
|
14
|
+
const _REG_COMMAND = 0x01;
|
|
15
|
+
const _REG_COM_IRQ = 0x04;
|
|
16
|
+
const _REG_DIV_IRQ = 0x05;
|
|
17
|
+
const _REG_ERROR = 0x06;
|
|
18
|
+
const _REG_STATUS_2 = 0x08;
|
|
19
|
+
const _REG_FIFO_DATA = 0x09;
|
|
20
|
+
const _REG_FIFO_LEVEL = 0x0A;
|
|
21
|
+
const _REG_BIT_FRAMING = 0x0D;
|
|
22
|
+
const _REG_TX_MODE = 0x12;
|
|
23
|
+
const _REG_RX_MODE = 0x13;
|
|
24
|
+
const _REG_TX_CONTROL = 0x14;
|
|
25
|
+
const _REG_TX_ASK = 0x15;
|
|
26
|
+
const _REG_MODE = 0x11;
|
|
27
|
+
const _REG_CRC_RESULT_H = 0x21;
|
|
28
|
+
const _REG_CRC_RESULT_L = 0x22;
|
|
29
|
+
const _REG_RF_CFG = 0x26;
|
|
30
|
+
const _REG_T_MODE = 0x2A;
|
|
31
|
+
const _REG_T_PRESCALER = 0x2B;
|
|
32
|
+
const _REG_T_RELOAD_H = 0x2C;
|
|
33
|
+
const _REG_T_RELOAD_L = 0x2D;
|
|
34
|
+
const _REG_AUTO_TEST = 0x36;
|
|
35
|
+
const _REG_VERSION = 0x37;
|
|
36
|
+
|
|
37
|
+
const _IRQ_RX = 0x30;
|
|
38
|
+
const _IRQ_IDLE = 0x10;
|
|
39
|
+
const _IRQ_TIMER = 0x01;
|
|
40
|
+
const _IRQ_ALL = 0x7F;
|
|
41
|
+
|
|
42
|
+
const _STATUS_2_CRYPTO1ON = 0x08;
|
|
43
|
+
const _FIFO_FLUSH = 0x80;
|
|
44
|
+
|
|
45
|
+
const _PICC_REQA = 0x26;
|
|
46
|
+
const _PICC_WUPA = 0x52;
|
|
47
|
+
const _PICC_HLTA = 0x50;
|
|
48
|
+
const _PICC_CT = 0x88;
|
|
49
|
+
const _PICC_SEL_BIT = 0x70;
|
|
50
|
+
const _PICC_SAK_NOT_COMPLETE = 0x04;
|
|
51
|
+
|
|
52
|
+
function _delay(ms) {
|
|
53
|
+
const start = Date.now();
|
|
54
|
+
while (Date.now() - start < ms) { /* spin */ }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* MFRC522 13.56 MHz contactless reader/writer — minimal interface.
|
|
59
|
+
*
|
|
60
|
+
* Provides a 13.56 MHz RFID/NFC reader/writer frontend that detects an
|
|
61
|
+
* ISO/IEC 14443 Type A card in the field and reads its UID. No
|
|
62
|
+
* configuration beyond the connection is required.
|
|
63
|
+
*
|
|
64
|
+
* Supports three host connections — I²C, SPI, and UART — all of which
|
|
65
|
+
* expose the same 64-register internal bank; the address-byte framing
|
|
66
|
+
* differs per connection. The driver selects the correct framing from a
|
|
67
|
+
* busType parameter.
|
|
68
|
+
*
|
|
69
|
+
* Default configuration (baked in at construction):
|
|
70
|
+
* - 25 ms receive timeout (TReloadReg = 1000 @ TPrescaler = 169)
|
|
71
|
+
* - Force100ASK modulation
|
|
72
|
+
* - ISO/IEC 14443-3 CRC_A preset (0x6363)
|
|
73
|
+
* - Antenna enabled
|
|
74
|
+
* - 106 kBd, 33 dB RX gain (reset default)
|
|
75
|
+
*
|
|
76
|
+
* Constructor caveat: JS constructors cannot be async. The SoftReset +
|
|
77
|
+
* initialization sequence is fired off unawaited, matching the
|
|
78
|
+
* fire-and-forget convention used throughout this port — in practice it
|
|
79
|
+
* settles before any subsequent `await` in caller code is scheduled, since
|
|
80
|
+
* the underlying I2C/SPI/UART connection is synchronous or near-instant
|
|
81
|
+
* under the hood, but this is not a guaranteed blocking wait the way the
|
|
82
|
+
* pre-async version was. Call `await reader.isCardPresent()` (or any other
|
|
83
|
+
* async method) once before relying on the chip being initialized if this
|
|
84
|
+
* matters for your use case.
|
|
85
|
+
*
|
|
86
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C, SPI, or UART connection.
|
|
87
|
+
* @param {string} [busType='spi'] - Bus type: 'spi', 'i2c', or 'uart'.
|
|
88
|
+
*/
|
|
89
|
+
class MFRC522Minimal {
|
|
90
|
+
constructor(connection, busType = 'spi') {
|
|
91
|
+
this._conn = connection;
|
|
92
|
+
this._busType = busType;
|
|
93
|
+
this._initChip();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
_addrFor(reg, read) {
|
|
97
|
+
if (this._busType === 'spi') {
|
|
98
|
+
return ((reg << 1) & 0x7E) | (read ? 0x80 : 0x00);
|
|
99
|
+
}
|
|
100
|
+
if (this._busType === 'uart') {
|
|
101
|
+
return (reg & 0x3F) | (read ? 0x80 : 0x00);
|
|
102
|
+
}
|
|
103
|
+
return reg & 0x3F;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async _writeReg(reg, value) {
|
|
107
|
+
const addr = this._addrFor(reg, false);
|
|
108
|
+
await this._conn.write(Buffer.from([addr, value & 0xFF]));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async _readReg(reg) {
|
|
112
|
+
const addr = this._addrFor(reg, true);
|
|
113
|
+
return (await this._conn.writeRead(Buffer.from([addr]), 1))[0];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async _setBits(reg, mask) {
|
|
117
|
+
await this._writeReg(reg, (await this._readReg(reg)) | mask);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async _clearBits(reg, mask) {
|
|
121
|
+
await this._writeReg(reg, (await this._readReg(reg)) & ~mask);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async _initChip() {
|
|
125
|
+
await this._writeReg(_REG_COMMAND, _CMD_SOFT_RESET);
|
|
126
|
+
for (let i = 0; i < 50; i++) {
|
|
127
|
+
if (((await this._readReg(_REG_COMMAND)) & 0x10) === 0) break;
|
|
128
|
+
_delay(1);
|
|
129
|
+
}
|
|
130
|
+
_delay(50);
|
|
131
|
+
await this._writeReg(_REG_T_MODE, 0x80);
|
|
132
|
+
await this._writeReg(_REG_T_PRESCALER, 0xA9);
|
|
133
|
+
await this._writeReg(_REG_T_RELOAD_H, 0x03);
|
|
134
|
+
await this._writeReg(_REG_T_RELOAD_L, 0xE8);
|
|
135
|
+
await this._writeReg(_REG_TX_ASK, 0x40);
|
|
136
|
+
await this._writeReg(_REG_MODE, 0x3D);
|
|
137
|
+
await this._setBits(_REG_TX_CONTROL, 0x03);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async _readFifo(n) {
|
|
141
|
+
const out = Buffer.alloc(n);
|
|
142
|
+
for (let i = 0; i < n; i++) {
|
|
143
|
+
out[i] = await this._readReg(_REG_FIFO_DATA);
|
|
144
|
+
}
|
|
145
|
+
return out;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async _writeFifo(data) {
|
|
149
|
+
for (const b of data) {
|
|
150
|
+
await this._writeReg(_REG_FIFO_DATA, b & 0xFF);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async _flushFifo() {
|
|
155
|
+
await this._writeReg(_REG_FIFO_LEVEL, _FIFO_FLUSH);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async _cardCommand(command, waitIrq, sendData) {
|
|
159
|
+
await this._writeReg(_REG_COMMAND, _CMD_IDLE);
|
|
160
|
+
await this._writeReg(_REG_COM_IRQ, 0x7F);
|
|
161
|
+
await this._flushFifo();
|
|
162
|
+
if (sendData && sendData.length) {
|
|
163
|
+
await this._writeFifo(sendData);
|
|
164
|
+
}
|
|
165
|
+
await this._writeReg(_REG_COMMAND, command);
|
|
166
|
+
if (command === _CMD_TRANSCEIVE) {
|
|
167
|
+
await this._setBits(_REG_BIT_FRAMING, 0x80);
|
|
168
|
+
}
|
|
169
|
+
for (let i = 0; i < 200; i++) {
|
|
170
|
+
const n = await this._readReg(_REG_COM_IRQ);
|
|
171
|
+
if (n & waitIrq) return true;
|
|
172
|
+
if (n & 0x01) return false;
|
|
173
|
+
}
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async _transceive(send) {
|
|
178
|
+
const ok = await this._cardCommand(_CMD_TRANSCEIVE, _IRQ_RX | _IRQ_IDLE, send);
|
|
179
|
+
if (!ok) return null;
|
|
180
|
+
const err = await this._readReg(_REG_ERROR);
|
|
181
|
+
if (err & 0x13) return null;
|
|
182
|
+
const fifoLevel = await this._readReg(_REG_FIFO_LEVEL);
|
|
183
|
+
if (fifoLevel === 0) return null;
|
|
184
|
+
return this._readFifo(fifoLevel);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async _calcCrc(data) {
|
|
188
|
+
await this._writeReg(_REG_COMMAND, _CMD_IDLE);
|
|
189
|
+
await this._writeReg(_REG_DIV_IRQ, 0x04);
|
|
190
|
+
await this._flushFifo();
|
|
191
|
+
await this._writeFifo(data);
|
|
192
|
+
await this._writeReg(_REG_COMMAND, _CMD_CALC_CRC);
|
|
193
|
+
for (let i = 0; i < 100; i++) {
|
|
194
|
+
if ((await this._readReg(_REG_DIV_IRQ)) & 0x04) break;
|
|
195
|
+
_delay(1);
|
|
196
|
+
}
|
|
197
|
+
await this._writeReg(_REG_COMMAND, _CMD_IDLE);
|
|
198
|
+
return Buffer.from([
|
|
199
|
+
await this._readReg(_REG_CRC_RESULT_H),
|
|
200
|
+
await this._readReg(_REG_CRC_RESULT_L),
|
|
201
|
+
]);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Detect a card in the RF field.
|
|
206
|
+
*
|
|
207
|
+
* Sends a REQA short frame. Returns true if a card answered with
|
|
208
|
+
* a valid 2-byte ATQA.
|
|
209
|
+
*
|
|
210
|
+
* @returns {Promise<boolean>} true if a card is in the field.
|
|
211
|
+
*/
|
|
212
|
+
async isCardPresent() {
|
|
213
|
+
await this._writeReg(_REG_BIT_FRAMING, 0x07);
|
|
214
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
215
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
216
|
+
const back = await this._transceive(Buffer.from([_PICC_REQA]));
|
|
217
|
+
return back !== null && back.length === 2;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async _anticollision(cmd) {
|
|
221
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
222
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
223
|
+
await this._writeReg(_REG_BIT_FRAMING, 0x00);
|
|
224
|
+
const send = Buffer.alloc(7);
|
|
225
|
+
send[0] = cmd;
|
|
226
|
+
send[1] = 0x20;
|
|
227
|
+
_delay(1);
|
|
228
|
+
const back = await this._transceive(send);
|
|
229
|
+
if (!back || back.length !== 5) return null;
|
|
230
|
+
let bcc = 0;
|
|
231
|
+
for (let i = 0; i < 4; i++) bcc ^= back[i];
|
|
232
|
+
if (bcc !== back[4]) return null;
|
|
233
|
+
return back.subarray(0, 4);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async _select(cmd, uidPart) {
|
|
237
|
+
const buf = Buffer.alloc(9);
|
|
238
|
+
buf[0] = cmd;
|
|
239
|
+
buf[1] = _PICC_SEL_BIT;
|
|
240
|
+
uidPart.copy(buf, 2, 0, 4);
|
|
241
|
+
let bcc = 0;
|
|
242
|
+
for (let i = 0; i < 4; i++) bcc ^= uidPart[i];
|
|
243
|
+
buf[6] = bcc;
|
|
244
|
+
const crc = await this._calcCrc(buf.subarray(0, 7));
|
|
245
|
+
buf[7] = crc[0];
|
|
246
|
+
buf[8] = crc[1];
|
|
247
|
+
await this._writeReg(_REG_TX_MODE, 0x80);
|
|
248
|
+
await this._writeReg(_REG_RX_MODE, 0x80);
|
|
249
|
+
_delay(1);
|
|
250
|
+
const back = await this._transceive(buf);
|
|
251
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
252
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
253
|
+
if (!back || back.length < 1) return null;
|
|
254
|
+
return back[0];
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async _selectCard() {
|
|
258
|
+
const cascade = [
|
|
259
|
+
[0x93, 0x93],
|
|
260
|
+
[0x95, 0x95],
|
|
261
|
+
[0x97, 0x97],
|
|
262
|
+
];
|
|
263
|
+
const uid = [];
|
|
264
|
+
for (const [anti, sel] of cascade) {
|
|
265
|
+
const part = await this._anticollision(anti);
|
|
266
|
+
if (!part) return null;
|
|
267
|
+
const sak = await this._select(sel, part);
|
|
268
|
+
if (sak === null) return null;
|
|
269
|
+
if (!(sak & _PICC_SAK_NOT_COMPLETE)) {
|
|
270
|
+
if (part[0] === _PICC_CT) {
|
|
271
|
+
uid.push(part[1], part[2], part[3]);
|
|
272
|
+
} else {
|
|
273
|
+
uid.push(part[0], part[1], part[2], part[3]);
|
|
274
|
+
}
|
|
275
|
+
return Buffer.from(uid);
|
|
276
|
+
} else {
|
|
277
|
+
uid.push(part[1], part[2], part[3]);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async _haltCard() {
|
|
284
|
+
const buf = Buffer.alloc(4);
|
|
285
|
+
buf[0] = _PICC_HLTA;
|
|
286
|
+
buf[1] = 0x00;
|
|
287
|
+
await this._writeReg(_REG_TX_MODE, 0x80);
|
|
288
|
+
await this._writeReg(_REG_RX_MODE, 0x80);
|
|
289
|
+
const crc = await this._calcCrc(buf.subarray(0, 2));
|
|
290
|
+
buf[2] = crc[0];
|
|
291
|
+
buf[3] = crc[1];
|
|
292
|
+
_delay(1);
|
|
293
|
+
await this._cardCommand(_CMD_TRANSCEIVE, _IRQ_RX | _IRQ_IDLE, buf);
|
|
294
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
295
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Detect a card, run anticollision/Select (all cascade levels), and HLTA.
|
|
300
|
+
*
|
|
301
|
+
* Returns the reassembled UID (4, 7, or 10 bytes). A card read this way
|
|
302
|
+
* is immediately halted, so the next call re-detects it from scratch.
|
|
303
|
+
*
|
|
304
|
+
* @returns {Promise<Buffer|null>} UID bytes, or null if no card answered.
|
|
305
|
+
*/
|
|
306
|
+
async readUid() {
|
|
307
|
+
if (!(await this.isCardPresent())) return null;
|
|
308
|
+
const uid = await this._selectCard();
|
|
309
|
+
await this._haltCard();
|
|
310
|
+
return uid;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* MFRC522 full interface — extends minimal with configuration, antenna
|
|
316
|
+
* control, self test, MIFARE Classic authenticated operations, and MIFARE
|
|
317
|
+
* Ultralight / NTAG page read/write.
|
|
318
|
+
*/
|
|
319
|
+
class MFRC522Full extends MFRC522Minimal {
|
|
320
|
+
static KEY_A = 0x60;
|
|
321
|
+
static KEY_B = 0x61;
|
|
322
|
+
|
|
323
|
+
static RX_GAIN_18_DB = 0x00;
|
|
324
|
+
static RX_GAIN_23_DB = 0x10;
|
|
325
|
+
static RX_GAIN_33_DB = 0x40;
|
|
326
|
+
static RX_GAIN_38_DB = 0x50;
|
|
327
|
+
static RX_GAIN_43_DB = 0x60;
|
|
328
|
+
static RX_GAIN_48_DB = 0x70;
|
|
329
|
+
|
|
330
|
+
constructor(connection, busType = 'spi') {
|
|
331
|
+
super(connection, busType);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Re-run SoftReset and the full initialization sequence.
|
|
336
|
+
* @returns {Promise<void>}
|
|
337
|
+
*/
|
|
338
|
+
async reset() { await this._initChip(); }
|
|
339
|
+
|
|
340
|
+
/** Enable the antenna driver (TX1 + TX2).
|
|
341
|
+
* @returns {Promise<void>} */
|
|
342
|
+
async antennaOn() { await this._setBits(_REG_TX_CONTROL, 0x03); }
|
|
343
|
+
|
|
344
|
+
/** Disable the antenna driver (TX1 + TX2).
|
|
345
|
+
* @returns {Promise<void>} */
|
|
346
|
+
async antennaOff() { await this._clearBits(_REG_TX_CONTROL, 0x03); }
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Set the receiver gain.
|
|
350
|
+
* @param {number} dB - One of 18, 23, 33, 38, 43, or 48 dB.
|
|
351
|
+
* @returns {Promise<void>}
|
|
352
|
+
* @throws {RangeError} If dB is not one of the supported gains.
|
|
353
|
+
*/
|
|
354
|
+
async setAntennaGain(dB) {
|
|
355
|
+
const map = { 18: 0x00, 23: 0x10, 33: 0x40, 38: 0x50, 43: 0x60, 48: 0x70 };
|
|
356
|
+
const gain = map[dB];
|
|
357
|
+
if (gain === undefined) {
|
|
358
|
+
throw new RangeError(`Unsupported gain: ${dB} dB (choose 18, 23, 33, 38, 43, 48)`);
|
|
359
|
+
}
|
|
360
|
+
const cur = (await this._readReg(_REG_RF_CFG)) & 0x8F;
|
|
361
|
+
await this._writeReg(_REG_RF_CFG, cur | gain);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Read the currently configured receiver gain.
|
|
366
|
+
* @returns {Promise<number>} Gain in dB (one of 18, 23, 33, 38, 43, 48).
|
|
367
|
+
*/
|
|
368
|
+
async antennaGain() {
|
|
369
|
+
const cur = (await this._readReg(_REG_RF_CFG)) & 0x70;
|
|
370
|
+
const map = { 0x00: 18, 0x10: 23, 0x40: 33, 0x50: 38, 0x60: 43, 0x70: 48 };
|
|
371
|
+
return map[cur] || 0;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Read the version register and decode it.
|
|
376
|
+
* @returns {Promise<{chipType: number, version: number}>} chipType=0x09 for MFRC522.
|
|
377
|
+
*/
|
|
378
|
+
async version() {
|
|
379
|
+
const raw = await this._readReg(_REG_VERSION);
|
|
380
|
+
return { chipType: (raw >> 4) & 0x0F, version: raw & 0x0F };
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Run the datasheet-defined digital self test.
|
|
385
|
+
* @returns {Promise<boolean>} true if all 64 FIFO bytes match the version-specific reference.
|
|
386
|
+
*/
|
|
387
|
+
async selfTest() {
|
|
388
|
+
const refV10 = [
|
|
389
|
+
0x00, 0x87, 0x98, 0x0F, 0x49, 0xFF, 0x07, 0x19,
|
|
390
|
+
0xBF, 0x22, 0x30, 0x49, 0x59, 0x63, 0xAD, 0xCA,
|
|
391
|
+
0x7F, 0xE3, 0x4E, 0x03, 0x5C, 0x4E, 0x49, 0x50,
|
|
392
|
+
0x47, 0x9A, 0x37, 0x61, 0xE7, 0xE2, 0xC6, 0x2E,
|
|
393
|
+
0x75, 0x5A, 0xED, 0x04, 0x3D, 0x02, 0x4B, 0x78,
|
|
394
|
+
0x32, 0xFF, 0x58, 0x3B, 0x7C, 0xE9, 0x00, 0x94,
|
|
395
|
+
0xB4, 0x4A, 0x59, 0x5B, 0xFD, 0xC9, 0x29, 0xDF,
|
|
396
|
+
0x35, 0x96, 0x98, 0x9E, 0x4F, 0x30, 0x32, 0x8D,
|
|
397
|
+
];
|
|
398
|
+
const refV20 = [
|
|
399
|
+
0x00, 0xEB, 0x66, 0xBA, 0x57, 0xBF, 0x23, 0x95,
|
|
400
|
+
0xD0, 0xE3, 0x0D, 0x3D, 0x27, 0x89, 0x5C, 0xDE,
|
|
401
|
+
0x9D, 0x3B, 0xA7, 0x00, 0x21, 0x5B, 0x89, 0x82,
|
|
402
|
+
0x51, 0x3A, 0xEB, 0x02, 0x0C, 0xA5, 0x00, 0x49,
|
|
403
|
+
0x7C, 0x84, 0x4D, 0xB3, 0xCC, 0xD2, 0x1B, 0x81,
|
|
404
|
+
0x5D, 0x48, 0x76, 0xD5, 0x71, 0x61, 0x21, 0xA9,
|
|
405
|
+
0x86, 0x96, 0x83, 0x38, 0xCF, 0x9D, 0x5B, 0x6D,
|
|
406
|
+
0xDC, 0x15, 0xBA, 0x3E, 0x7D, 0x95, 0x3B, 0x2F,
|
|
407
|
+
];
|
|
408
|
+
const v = (await this.version()).version;
|
|
409
|
+
const ref = v === 1 ? refV10 : refV20;
|
|
410
|
+
await this._writeReg(_REG_AUTO_TEST, 0x09);
|
|
411
|
+
await this._writeReg(_REG_FIFO_LEVEL, _FIFO_FLUSH);
|
|
412
|
+
await this._writeReg(_REG_COMMAND, _CMD_IDLE);
|
|
413
|
+
for (let i = 0; i < 255; i++) {
|
|
414
|
+
if ((await this._readReg(_REG_FIFO_LEVEL)) >= 64) break;
|
|
415
|
+
await this._writeReg(_REG_COMMAND, _CMD_CALC_CRC);
|
|
416
|
+
_delay(1);
|
|
417
|
+
}
|
|
418
|
+
await this._writeReg(_REG_AUTO_TEST, 0x00);
|
|
419
|
+
await this._writeReg(_REG_COMMAND, _CMD_SOFT_RESET);
|
|
420
|
+
_delay(50);
|
|
421
|
+
await this._initChip();
|
|
422
|
+
const got = await this._readFifo(64);
|
|
423
|
+
for (let i = 0; i < 64; i++) {
|
|
424
|
+
if (got[i] !== ref[i]) return false;
|
|
425
|
+
}
|
|
426
|
+
return true;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** WUPA — wake a HALTed card. Same as isCardPresent but with WUPA.
|
|
430
|
+
* @returns {Promise<boolean>} */
|
|
431
|
+
async wakeupCard() {
|
|
432
|
+
await this._writeReg(_REG_BIT_FRAMING, 0x07);
|
|
433
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
434
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
435
|
+
const back = await this._transceive(Buffer.from([_PICC_WUPA]));
|
|
436
|
+
return back !== null && back.length === 2;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Run anticollision / Select only — leaves the card active for further ops.
|
|
441
|
+
* @returns {Promise<Buffer|null>} UID bytes, or null if no card answered.
|
|
442
|
+
*/
|
|
443
|
+
async selectCard() {
|
|
444
|
+
if (!(await this.wakeupCard())) return null;
|
|
445
|
+
return this._selectCard();
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** Send HLTA — put the currently selected card into HALT state.
|
|
449
|
+
* @returns {Promise<void>} */
|
|
450
|
+
async haltCard() { await this._haltCard(); }
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Run MIFARE Classic Crypto1 authentication.
|
|
454
|
+
* @param {number} blockAddress - Block number to authenticate against.
|
|
455
|
+
* @param {number} keyType - MFRC522Full.KEY_A (0x60) or MFRC522Full.KEY_B (0x61).
|
|
456
|
+
* @param {Buffer} key - 6-byte key.
|
|
457
|
+
* @param {Buffer} uid - 4-byte UID of the card.
|
|
458
|
+
* @returns {Promise<boolean>} true on success.
|
|
459
|
+
*/
|
|
460
|
+
async authenticate(blockAddress, keyType, key, uid) {
|
|
461
|
+
if (key.length !== 6 || uid.length !== 4) return false;
|
|
462
|
+
const buf = Buffer.alloc(12);
|
|
463
|
+
buf[0] = keyType;
|
|
464
|
+
buf[1] = blockAddress & 0xFF;
|
|
465
|
+
key.copy(buf, 2, 0, 6);
|
|
466
|
+
uid.copy(buf, 8, 0, 4);
|
|
467
|
+
await this._writeReg(_REG_COM_IRQ, _IRQ_ALL);
|
|
468
|
+
await this._writeReg(_REG_STATUS_2, 0x00);
|
|
469
|
+
await this._flushFifo();
|
|
470
|
+
await this._writeFifo(buf);
|
|
471
|
+
await this._writeReg(_REG_COMMAND, _CMD_MFAUTHENT);
|
|
472
|
+
for (let i = 0; i < 200; i++) {
|
|
473
|
+
const n = await this._readReg(_REG_STATUS_2);
|
|
474
|
+
if (n & _STATUS_2_CRYPTO1ON) return true;
|
|
475
|
+
_delay(1);
|
|
476
|
+
}
|
|
477
|
+
return false;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** Clear Status2Reg.MFCrypto1On.
|
|
481
|
+
* @returns {Promise<void>} */
|
|
482
|
+
async stopCrypto() { await this._clearBits(_REG_STATUS_2, _STATUS_2_CRYPTO1ON); }
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Read a 16-byte MIFARE Classic block.
|
|
486
|
+
* @param {number} blockAddress - Block number.
|
|
487
|
+
* @returns {Promise<Buffer|null>} 16 data bytes, or null on failure.
|
|
488
|
+
*/
|
|
489
|
+
async readBlock(blockAddress) {
|
|
490
|
+
const cmd = Buffer.from([0x30, blockAddress & 0xFF]);
|
|
491
|
+
await this._writeReg(_REG_TX_MODE, 0x80);
|
|
492
|
+
await this._writeReg(_REG_RX_MODE, 0x80);
|
|
493
|
+
const crc = await this._calcCrc(cmd);
|
|
494
|
+
const full = Buffer.concat([cmd, crc]);
|
|
495
|
+
const back = await this._transceive(full);
|
|
496
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
497
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
498
|
+
if (!back || back.length !== 16) return null;
|
|
499
|
+
return back;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Write a 16-byte MIFARE Classic block.
|
|
504
|
+
* @param {number} blockAddress - Block number.
|
|
505
|
+
* @param {Buffer} data - 16 bytes to write.
|
|
506
|
+
* @returns {Promise<boolean>} true on success.
|
|
507
|
+
*/
|
|
508
|
+
async writeBlock(blockAddress, data) {
|
|
509
|
+
if (data.length !== 16) return false;
|
|
510
|
+
// Phase 1: 0xA0 + block_address, expect 4-bit ACK
|
|
511
|
+
const c = Buffer.from([0xA0, blockAddress & 0xFF]);
|
|
512
|
+
await this._writeReg(_REG_TX_MODE, 0x80);
|
|
513
|
+
await this._writeReg(_REG_RX_MODE, 0x80);
|
|
514
|
+
const crc = await this._calcCrc(c);
|
|
515
|
+
const full = Buffer.concat([c, crc]);
|
|
516
|
+
const back = await this._transceive(full);
|
|
517
|
+
if (!back || back.length < 1 || (back[0] & 0x0F) !== 0x0A) {
|
|
518
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
519
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
520
|
+
return false;
|
|
521
|
+
}
|
|
522
|
+
// Phase 2: 16 data bytes, expect 4-bit ACK
|
|
523
|
+
const crc2 = await this._calcCrc(data);
|
|
524
|
+
const buf = Buffer.concat([data, crc2]);
|
|
525
|
+
const back2 = await this._transceive(buf);
|
|
526
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
527
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
528
|
+
return back2 !== null && back2.length >= 1 && (back2[0] & 0x0F) === 0x0A;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Increment the value block at blockAddress by delta and transfer it back.
|
|
533
|
+
* @param {number} blockAddress - Source value block.
|
|
534
|
+
* @param {number} delta - Unsigned 32-bit increment.
|
|
535
|
+
* @returns {Promise<boolean>} true on success.
|
|
536
|
+
*/
|
|
537
|
+
async incrementValue(blockAddress, delta) {
|
|
538
|
+
if (!(await this._valueOp(0xC1, blockAddress, delta, false))) return false;
|
|
539
|
+
return this._transfer(blockAddress);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Decrement the value block at blockAddress by delta and transfer it back.
|
|
544
|
+
* @param {number} blockAddress - Source value block.
|
|
545
|
+
* @param {number} delta - Unsigned 32-bit decrement.
|
|
546
|
+
* @returns {Promise<boolean>} true on success.
|
|
547
|
+
*/
|
|
548
|
+
async decrementValue(blockAddress, delta) {
|
|
549
|
+
if (!(await this._valueOp(0xC0, blockAddress, delta, false))) return false;
|
|
550
|
+
return this._transfer(blockAddress);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Restore (re-read) the value block at blockAddress into the internal data register.
|
|
555
|
+
* @param {number} blockAddress - Value block to restore.
|
|
556
|
+
* @returns {Promise<boolean>} true on success.
|
|
557
|
+
*/
|
|
558
|
+
async restoreValue(blockAddress) {
|
|
559
|
+
if (!(await this._valueOp(0xC2, blockAddress, 0, true))) return false;
|
|
560
|
+
return this._transfer(blockAddress);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Commit the internal data register to destinationBlock.
|
|
565
|
+
* @param {number} destinationBlock - Block to write the data register to.
|
|
566
|
+
* @returns {Promise<boolean>} true on success.
|
|
567
|
+
*/
|
|
568
|
+
async transferValue(destinationBlock) { return this._transfer(destinationBlock); }
|
|
569
|
+
|
|
570
|
+
async _valueOp(cmd, blockAddress, delta, dummy) {
|
|
571
|
+
const c = Buffer.from([cmd, blockAddress & 0xFF]);
|
|
572
|
+
await this._writeReg(_REG_TX_MODE, 0x80);
|
|
573
|
+
await this._writeReg(_REG_RX_MODE, 0x80);
|
|
574
|
+
const crc = await this._calcCrc(c);
|
|
575
|
+
const full = Buffer.concat([c, crc]);
|
|
576
|
+
const back = await this._transceive(full);
|
|
577
|
+
if (!back || back.length < 1 || (back[0] & 0x0F) !== 0x0A) {
|
|
578
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
579
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
580
|
+
return false;
|
|
581
|
+
}
|
|
582
|
+
const data = Buffer.alloc(6);
|
|
583
|
+
if (dummy) {
|
|
584
|
+
data[0] = data[1] = data[2] = data[3] = 0;
|
|
585
|
+
} else {
|
|
586
|
+
data[0] = delta & 0xFF;
|
|
587
|
+
data[1] = (delta >> 8) & 0xFF;
|
|
588
|
+
data[2] = (delta >> 16) & 0xFF;
|
|
589
|
+
data[3] = (delta >> 24) & 0xFF;
|
|
590
|
+
}
|
|
591
|
+
const crc2 = await this._calcCrc(data.subarray(0, 4));
|
|
592
|
+
data[4] = crc2[0];
|
|
593
|
+
data[5] = crc2[1];
|
|
594
|
+
const back2 = await this._transceive(data);
|
|
595
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
596
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
597
|
+
if (!back2 || back2.length < 1 || (back2[0] & 0x0F) !== 0x0A) return false;
|
|
598
|
+
return true;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
async _transfer(blockAddress) {
|
|
602
|
+
const c = Buffer.from([0xB0, blockAddress & 0xFF]);
|
|
603
|
+
await this._writeReg(_REG_TX_MODE, 0x80);
|
|
604
|
+
await this._writeReg(_REG_RX_MODE, 0x80);
|
|
605
|
+
const crc = await this._calcCrc(c);
|
|
606
|
+
const full = Buffer.concat([c, crc]);
|
|
607
|
+
const back = await this._transceive(full);
|
|
608
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
609
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
610
|
+
if (!back || back.length < 1 || (back[0] & 0x0F) !== 0x0A) return false;
|
|
611
|
+
return true;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/**
|
|
615
|
+
* Read 4 consecutive pages (16 bytes) starting at pageAddress.
|
|
616
|
+
* @param {number} pageAddress - Page number (0-based).
|
|
617
|
+
* @returns {Promise<Buffer|null>} 16 data bytes, or null on failure.
|
|
618
|
+
*/
|
|
619
|
+
async readUltralightPage(pageAddress) {
|
|
620
|
+
const cmd = Buffer.from([0x30, pageAddress & 0xFF]);
|
|
621
|
+
await this._writeReg(_REG_TX_MODE, 0x80);
|
|
622
|
+
await this._writeReg(_REG_RX_MODE, 0x80);
|
|
623
|
+
const crc = await this._calcCrc(cmd);
|
|
624
|
+
const full = Buffer.concat([cmd, crc]);
|
|
625
|
+
const back = await this._transceive(full);
|
|
626
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
627
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
628
|
+
if (!back || back.length !== 16) return null;
|
|
629
|
+
return back;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Write a 4-byte page (MIFARE Ultralight / NTAG).
|
|
634
|
+
* @param {number} pageAddress - Page number.
|
|
635
|
+
* @param {Buffer} data - 4 bytes to write.
|
|
636
|
+
* @returns {Promise<boolean>} true on success.
|
|
637
|
+
*/
|
|
638
|
+
async writeUltralightPage(pageAddress, data) {
|
|
639
|
+
if (data.length !== 4) return false;
|
|
640
|
+
const buf = Buffer.alloc(8);
|
|
641
|
+
buf[0] = 0xA2;
|
|
642
|
+
buf[1] = pageAddress & 0xFF;
|
|
643
|
+
data.copy(buf, 2, 0, 4);
|
|
644
|
+
await this._writeReg(_REG_TX_MODE, 0x80);
|
|
645
|
+
await this._writeReg(_REG_RX_MODE, 0x80);
|
|
646
|
+
const crc = await this._calcCrc(buf.subarray(0, 6));
|
|
647
|
+
buf[6] = crc[0];
|
|
648
|
+
buf[7] = crc[1];
|
|
649
|
+
const back = await this._transceive(buf);
|
|
650
|
+
await this._writeReg(_REG_TX_MODE, 0x00);
|
|
651
|
+
await this._writeReg(_REG_RX_MODE, 0x00);
|
|
652
|
+
if (!back || back.length < 1 || (back[0] & 0x0F) !== 0x0A) return false;
|
|
653
|
+
return true;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* Run the Generate RandomID command and return the 10-byte ID.
|
|
658
|
+
* @returns {Promise<Buffer>} 10-byte random ID.
|
|
659
|
+
*/
|
|
660
|
+
async generateRandomId() {
|
|
661
|
+
await this._writeReg(_REG_COMMAND, _CMD_IDLE);
|
|
662
|
+
await this._writeReg(_REG_COM_IRQ, _IRQ_ALL);
|
|
663
|
+
await this._writeReg(_REG_DIV_IRQ, 0x14);
|
|
664
|
+
await this._writeReg(_REG_COMMAND, _CMD_RANDOM_ID);
|
|
665
|
+
for (let i = 0; i < 50; i++) {
|
|
666
|
+
if ((await this._readReg(_REG_COM_IRQ)) & 0x10) break;
|
|
667
|
+
_delay(1);
|
|
668
|
+
}
|
|
669
|
+
await this._writeReg(_REG_COMMAND, _CMD_IDLE);
|
|
670
|
+
return this._readFifo(10);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
module.exports = { MFRC522Minimal, MFRC522Full };
|