periph 1.3.2 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -0
- package/examples/accelerometer/bma180/complete.js +83 -0
- package/examples/accelerometer/bma180/demo.js +47 -0
- package/examples/accelerometer/bma180/minimal.js +21 -0
- package/examples/accelerometer/fxos8700/complete.js +111 -0
- package/examples/accelerometer/fxos8700/demo.js +80 -0
- package/examples/accelerometer/fxos8700/minimal.js +20 -0
- package/examples/accelerometer/lis3dh/complete.js +123 -0
- package/examples/accelerometer/lis3dh/demo.js +99 -0
- package/examples/accelerometer/lis3dh/minimal.js +19 -0
- package/examples/accelerometer/lsm303/complete.js +82 -0
- package/examples/accelerometer/lsm303/demo.js +96 -0
- package/examples/accelerometer/lsm303/minimal.js +23 -0
- package/examples/accelerometer/mma8451q/complete.js +96 -0
- package/examples/accelerometer/mma8451q/demo.js +120 -0
- package/examples/accelerometer/mma8451q/minimal.js +20 -0
- package/examples/adc_dac/ad5243/complete.js +52 -0
- package/examples/adc_dac/ad5243/demo.js +50 -0
- package/examples/adc_dac/ad5243/minimal.js +23 -0
- package/examples/adc_dac/ad5248/complete.js +49 -0
- package/examples/adc_dac/ad5248/demo.js +42 -0
- package/examples/adc_dac/ad5248/minimal.js +21 -0
- package/examples/adc_dac/ads1013/complete.js +51 -0
- package/examples/adc_dac/ads1013/demo.js +51 -0
- package/examples/adc_dac/ads1013/minimal.js +21 -0
- package/examples/adc_dac/ads1014/complete.js +88 -0
- package/examples/adc_dac/ads1014/demo.js +52 -0
- package/examples/adc_dac/ads1014/minimal.js +21 -0
- package/examples/adc_dac/ads1113/complete.js +47 -0
- package/examples/adc_dac/ads1113/demo.js +39 -0
- package/examples/adc_dac/ads1113/minimal.js +20 -0
- package/examples/adc_dac/ads1114/complete.js +77 -0
- package/examples/adc_dac/ads1114/demo.js +47 -0
- package/examples/adc_dac/ads1114/minimal.js +20 -0
- package/examples/adc_dac/ads1115/complete.js +82 -0
- package/examples/adc_dac/ads1115/demo.js +55 -0
- package/examples/adc_dac/ads1115/minimal.js +20 -0
- package/examples/adc_dac/es8311/complete.js +80 -0
- package/examples/adc_dac/es8311/demo.js +59 -0
- package/examples/adc_dac/es8311/minimal.js +18 -0
- package/examples/adc_dac/mcp4xxx/complete.js +97 -0
- package/examples/adc_dac/mcp4xxx/demo.js +76 -0
- package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
- package/examples/display/pcf8578/complete.js +58 -0
- package/examples/display/pcf8578/demo.js +62 -0
- package/examples/display/pcf8578/minimal.js +19 -0
- package/examples/display/pcf8579/complete.js +58 -0
- package/examples/display/pcf8579/demo.js +61 -0
- package/examples/display/pcf8579/minimal.js +19 -0
- package/examples/imu/icm20948/complete.js +114 -0
- package/examples/imu/icm20948/demo.js +79 -0
- package/examples/imu/icm20948/minimal.js +22 -0
- package/examples/imu/qmi8658a/complete.js +134 -0
- package/examples/imu/qmi8658a/demo.js +81 -0
- package/examples/imu/qmi8658a/minimal.js +22 -0
- package/examples/io_expander/mcp23008/complete.js +70 -0
- package/examples/io_expander/mcp23008/demo.js +58 -0
- package/examples/io_expander/mcp23008/minimal.js +20 -0
- package/examples/memory/mb85rc/complete.js +40 -0
- package/examples/memory/mb85rc/demo.js +58 -0
- package/examples/memory/mb85rc/minimal.js +15 -0
- package/examples/motor/drv2605/complete.js +100 -0
- package/examples/motor/drv2605/demo.js +55 -0
- package/examples/motor/drv2605/minimal.js +24 -0
- package/examples/motor/tmc2130/complete.js +138 -0
- package/examples/motor/tmc2130/demo.js +96 -0
- package/examples/motor/tmc2130/minimal.js +30 -0
- package/examples/motor/tmc2208/complete.js +92 -0
- package/examples/motor/tmc2208/demo.js +85 -0
- package/examples/motor/tmc2208/minimal.js +25 -0
- package/examples/motor/tmc2209/complete.js +106 -0
- package/examples/motor/tmc2209/demo.js +97 -0
- package/examples/motor/tmc2209/minimal.js +25 -0
- package/examples/motor/tmc5160/complete.js +205 -0
- package/examples/motor/tmc5160/demo.js +104 -0
- package/examples/motor/tmc5160/minimal.js +35 -0
- package/examples/power/axp2101/complete.js +135 -0
- package/examples/power/axp2101/demo.js +68 -0
- package/examples/power/axp2101/minimal.js +24 -0
- package/examples/rtc/pcf85063a/complete.js +68 -0
- package/examples/rtc/pcf85063a/demo.js +76 -0
- package/examples/rtc/pcf85063a/minimal.js +20 -0
- package/package.json +1 -1
- package/src/chips/accelerometer/bma180.js +573 -0
- package/src/chips/accelerometer/fxos8700.js +1600 -0
- package/src/chips/accelerometer/lis3dh.js +1341 -0
- package/src/chips/accelerometer/lsm303.js +1471 -0
- package/src/chips/accelerometer/mma8451q.js +1406 -0
- package/src/chips/adc_dac/_ads111x_base.js +272 -0
- package/src/chips/adc_dac/ad5243.js +231 -0
- package/src/chips/adc_dac/ad5248.js +194 -0
- package/src/chips/adc_dac/ads1013.js +219 -0
- package/src/chips/adc_dac/ads1014.js +509 -0
- package/src/chips/adc_dac/ads1113.js +112 -0
- package/src/chips/adc_dac/ads1114.js +201 -0
- package/src/chips/adc_dac/ads1115.js +232 -0
- package/src/chips/adc_dac/es8311.js +621 -0
- package/src/chips/adc_dac/mcp4xxx.js +982 -0
- package/src/chips/display/pcf8578.js +452 -0
- package/src/chips/display/pcf8579.js +451 -0
- package/src/chips/imu/icm20948.js +849 -0
- package/src/chips/imu/qmi8658a.js +1011 -0
- package/src/chips/io_expander/mcp23008.js +558 -0
- package/src/chips/memory/mb85rc.js +166 -0
- package/src/chips/motor/drv2605.js +592 -0
- package/src/chips/motor/tmc2130.js +537 -0
- package/src/chips/motor/tmc2208.js +490 -0
- package/src/chips/motor/tmc2209.js +565 -0
- package/src/chips/motor/tmc5160.js +900 -0
- package/src/chips/power/axp2101.js +968 -0
- package/src/chips/rtc/pcf85063a.js +482 -0
- package/src/connection/uart.js +9 -0
- package/src/connection/uart_mock.js +65 -0
- package/src/discovery/registry.js +45 -19
|
@@ -0,0 +1,849 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_WHO_AM_I = 0x00;
|
|
4
|
+
const _REG_USER_CTRL = 0x03;
|
|
5
|
+
const _REG_LP_CONFIG = 0x05;
|
|
6
|
+
const _REG_PWR_MGMT_1 = 0x06;
|
|
7
|
+
const _REG_PWR_MGMT_2 = 0x07;
|
|
8
|
+
const _REG_INT_PIN_CFG = 0x0F;
|
|
9
|
+
const _REG_INT_ENABLE = 0x10;
|
|
10
|
+
const _REG_INT_ENABLE_1 = 0x11;
|
|
11
|
+
const _REG_INT_ENABLE_2 = 0x12;
|
|
12
|
+
const _REG_I2C_MST_STATUS = 0x17;
|
|
13
|
+
const _REG_INT_STATUS = 0x19;
|
|
14
|
+
const _REG_INT_STATUS_1 = 0x1A;
|
|
15
|
+
const _REG_ACCEL_XOUT_H = 0x2D;
|
|
16
|
+
const _REG_GYRO_XOUT_H = 0x33;
|
|
17
|
+
const _REG_TEMP_OUT_H = 0x39;
|
|
18
|
+
const _REG_EXT_SLV_SENS_DATA_00 = 0x3B;
|
|
19
|
+
const _REG_FIFO_EN_2 = 0x67;
|
|
20
|
+
const _REG_FIFO_RST = 0x68;
|
|
21
|
+
const _REG_FIFO_MODE = 0x69;
|
|
22
|
+
const _REG_FIFO_COUNTH = 0x70;
|
|
23
|
+
const _REG_FIFO_R_W = 0x72;
|
|
24
|
+
const _REG_BANK_SEL = 0x7F;
|
|
25
|
+
|
|
26
|
+
// Bank 1
|
|
27
|
+
const _REG_XA_OFFS_H = 0x14;
|
|
28
|
+
const _REG_TIMEBASE_CORRECTION_PLL = 0x28;
|
|
29
|
+
|
|
30
|
+
// Bank 2
|
|
31
|
+
const _REG_GYRO_SMPLRT_DIV = 0x00;
|
|
32
|
+
const _REG_GYRO_CONFIG_1 = 0x01;
|
|
33
|
+
const _REG_GYRO_CONFIG_2 = 0x02;
|
|
34
|
+
const _REG_XG_OFFS_USRH = 0x03;
|
|
35
|
+
const _REG_ACCEL_SMPLRT_DIV_1 = 0x10;
|
|
36
|
+
const _REG_ACCEL_SMPLRT_DIV_2 = 0x11;
|
|
37
|
+
const _REG_ACCEL_INTEL_CTRL = 0x12;
|
|
38
|
+
const _REG_ACCEL_WOM_THR = 0x13;
|
|
39
|
+
const _REG_ACCEL_CONFIG = 0x14;
|
|
40
|
+
const _REG_ACCEL_CONFIG_2 = 0x15;
|
|
41
|
+
|
|
42
|
+
// Bank 3
|
|
43
|
+
const _REG_I2C_MST_CTRL = 0x01;
|
|
44
|
+
const _REG_I2C_SLV0_ADDR = 0x03;
|
|
45
|
+
const _REG_I2C_SLV0_REG = 0x04;
|
|
46
|
+
const _REG_I2C_SLV0_CTRL = 0x05;
|
|
47
|
+
const _REG_I2C_SLV4_ADDR = 0x13;
|
|
48
|
+
const _REG_I2C_SLV4_REG = 0x14;
|
|
49
|
+
const _REG_I2C_SLV4_CTRL = 0x15;
|
|
50
|
+
const _REG_I2C_SLV4_DO = 0x16;
|
|
51
|
+
const _REG_I2C_SLV4_DI = 0x17;
|
|
52
|
+
|
|
53
|
+
const _WHO_AM_I_VALUE = 0xEA;
|
|
54
|
+
|
|
55
|
+
const _AK_ADDR = 0x0C;
|
|
56
|
+
const _AK_WIA2 = 0x01;
|
|
57
|
+
const _AK_ST1 = 0x10;
|
|
58
|
+
const _AK_CNTL2 = 0x31;
|
|
59
|
+
const _AK_CNTL3 = 0x32;
|
|
60
|
+
const _AK_WIA2_VALUE = 0x09;
|
|
61
|
+
const _MAG_SENSITIVITY = 0.15;
|
|
62
|
+
|
|
63
|
+
const _ACCEL_SENSITIVITY = [16384.0, 8192.0, 4096.0, 2048.0];
|
|
64
|
+
const _GYRO_SENSITIVITY = [131.0, 65.5, 32.8, 16.4];
|
|
65
|
+
const _G = 9.80665;
|
|
66
|
+
const _DEG_TO_RAD = Math.PI / 180.0;
|
|
67
|
+
|
|
68
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
69
|
+
|
|
70
|
+
/** Interrupt source bit flags returned by `pollInterrupt()` and accepted by `enableInterrupt()`. */
|
|
71
|
+
const ICM20948Source = {
|
|
72
|
+
SOURCE_DATA_READY: 0x01,
|
|
73
|
+
SOURCE_WAKE_ON_MOTION: 0x02,
|
|
74
|
+
SOURCE_I2C_MASTER: 0x04,
|
|
75
|
+
SOURCE_PLL_READY: 0x08,
|
|
76
|
+
SOURCE_FIFO_OVERFLOW: 0x10,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// source -> [enable register, mask]
|
|
80
|
+
const _INT_ENABLE_MAP = {
|
|
81
|
+
0x01: [_REG_INT_ENABLE_1, 0x01],
|
|
82
|
+
0x02: [_REG_INT_ENABLE, 0x08],
|
|
83
|
+
0x04: [_REG_INT_ENABLE, 0x01],
|
|
84
|
+
0x08: [_REG_INT_ENABLE, 0x04],
|
|
85
|
+
0x10: [_REG_INT_ENABLE_2, 0x1F],
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* ICM-20948 9-axis MotionTracking device (TDK InvenSense) — minimal interface.
|
|
90
|
+
*
|
|
91
|
+
* Provides 3-axis acceleration and 3-axis angular rate with no configuration
|
|
92
|
+
* beyond the connection. The registers are organised in four user banks
|
|
93
|
+
* selected through `REG_BANK_SEL` (0x7F); the driver caches the selected bank
|
|
94
|
+
* and returns to bank 0 after every bank 1–3 access. VDDIO is 1.71–1.95 V
|
|
95
|
+
* only. SPI and the DMP are out of scope.
|
|
96
|
+
*
|
|
97
|
+
* Default configuration (written at construction):
|
|
98
|
+
* - Gyroscope ±250 dps, low-pass filter 51.2 Hz (`GYRO_DLPFCFG` = 3)
|
|
99
|
+
* - Accelerometer ±2 g, low-pass filter 50.4 Hz (`ACCEL_DLPFCFG` = 3)
|
|
100
|
+
* - Both output data rates 102.3 Hz (divider 10)
|
|
101
|
+
* - Clock auto (`CLKSEL` = 1), all six axes and the temperature sensor on
|
|
102
|
+
* - FIFO, DMP, I²C master and all interrupts off
|
|
103
|
+
*
|
|
104
|
+
* Constructor caveat: JS constructors cannot be async, so the datasheet
|
|
105
|
+
* Initialization Sequence — including the `WHO_AM_I` identity check — starts
|
|
106
|
+
* in the constructor and every public method awaits it first. A wrong or
|
|
107
|
+
* absent device therefore rejects the first call made on the driver; call
|
|
108
|
+
* `await imu.init()` right after construction to surface it at a predictable
|
|
109
|
+
* point.
|
|
110
|
+
*/
|
|
111
|
+
class ICM20948Minimal {
|
|
112
|
+
/**
|
|
113
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C connection (0x68 with AD0 = GND, 0x69 with AD0 = VDDIO).
|
|
114
|
+
*/
|
|
115
|
+
constructor(connection) {
|
|
116
|
+
this._conn = connection;
|
|
117
|
+
this._bank = 0;
|
|
118
|
+
this._accelFs = 0;
|
|
119
|
+
this._gyroFs = 0;
|
|
120
|
+
this._accelDlpf = 3;
|
|
121
|
+
this._gyroDlpf = 3;
|
|
122
|
+
this._accelFilter = true;
|
|
123
|
+
this._gyroFilter = true;
|
|
124
|
+
this._lpEn = false;
|
|
125
|
+
this._lpSuspended = false;
|
|
126
|
+
this._ready = this._runInitSequence();
|
|
127
|
+
// The rejection is re-raised by every public method; mark it handled
|
|
128
|
+
// here so it never surfaces as an unhandled rejection on its own.
|
|
129
|
+
this._ready.catch(() => {});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Wait for the Initialization Sequence started by the constructor.
|
|
134
|
+
* @returns {Promise<void>}
|
|
135
|
+
* @throws {Error} If `WHO_AM_I` is not 0xEA (wrong chip, wrong address, or wiring problem).
|
|
136
|
+
*/
|
|
137
|
+
async init() {
|
|
138
|
+
await this._ready;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async _runInitSequence() {
|
|
142
|
+
await this._selectBank(0, true);
|
|
143
|
+
const who = (await this._conn.readReg(_REG_WHO_AM_I, 1))[0];
|
|
144
|
+
if (who !== _WHO_AM_I_VALUE) {
|
|
145
|
+
throw new Error('ICM20948 WHO_AM_I: expected 0x' + _WHO_AM_I_VALUE.toString(16) +
|
|
146
|
+
', got 0x' + who.toString(16));
|
|
147
|
+
}
|
|
148
|
+
await this._conn.writeReg(_REG_PWR_MGMT_1, 0x80); // DEVICE_RESET
|
|
149
|
+
await _sleep(100);
|
|
150
|
+
this._bank = 0; // reset returns to bank 0
|
|
151
|
+
await this._conn.writeReg(_REG_PWR_MGMT_1, 0x01); // SLEEP = 0, CLKSEL = 1
|
|
152
|
+
await this._conn.writeReg(_REG_PWR_MGMT_2, 0x00); // all axes on
|
|
153
|
+
await this._writeGyroConfig();
|
|
154
|
+
await this._writeAccelConfig();
|
|
155
|
+
await this._wr(2, _REG_GYRO_SMPLRT_DIV, 0x0A);
|
|
156
|
+
await this._wr(2, _REG_ACCEL_SMPLRT_DIV_1, 0x00);
|
|
157
|
+
await this._wr(2, _REG_ACCEL_SMPLRT_DIV_2, 0x0A);
|
|
158
|
+
await _sleep(35);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async _selectBank(bank, force = false) {
|
|
162
|
+
if (force || bank !== this._bank) {
|
|
163
|
+
await this._conn.writeReg(_REG_BANK_SEL, bank << 4);
|
|
164
|
+
this._bank = bank;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async _rd(bank, reg, length) {
|
|
169
|
+
await this._selectBank(bank);
|
|
170
|
+
const data = await this._conn.readReg(reg, length);
|
|
171
|
+
if (bank !== 0) await this._selectBank(0);
|
|
172
|
+
return data;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async _wr(bank, reg, data) {
|
|
176
|
+
await this._selectBank(bank);
|
|
177
|
+
await this._conn.writeReg(reg, data);
|
|
178
|
+
if (bank !== 0) await this._selectBank(0);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async _writeGyroConfig() {
|
|
182
|
+
await this._wr(2, _REG_GYRO_CONFIG_1,
|
|
183
|
+
(this._gyroDlpf << 3) | (this._gyroFs << 1) | (this._gyroFilter ? 1 : 0));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async _writeAccelConfig() {
|
|
187
|
+
await this._wr(2, _REG_ACCEL_CONFIG,
|
|
188
|
+
(this._accelDlpf << 3) | (this._accelFs << 1) | (this._accelFilter ? 1 : 0));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Read 3-axis linear acceleration.
|
|
193
|
+
* @returns {Promise<number[]>} [x, y, z] acceleration in m/s².
|
|
194
|
+
*/
|
|
195
|
+
async accel() {
|
|
196
|
+
await this._ready;
|
|
197
|
+
const buf = await this._rd(0, _REG_ACCEL_XOUT_H, 6);
|
|
198
|
+
const sens = _ACCEL_SENSITIVITY[this._accelFs];
|
|
199
|
+
return [buf.readInt16BE(0) / sens * _G, buf.readInt16BE(2) / sens * _G,
|
|
200
|
+
buf.readInt16BE(4) / sens * _G];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Read 3-axis angular rate.
|
|
205
|
+
* @returns {Promise<number[]>} [x, y, z] angular rate in rad/s.
|
|
206
|
+
*/
|
|
207
|
+
async gyro() {
|
|
208
|
+
await this._ready;
|
|
209
|
+
const buf = await this._rd(0, _REG_GYRO_XOUT_H, 6);
|
|
210
|
+
const sens = _GYRO_SENSITIVITY[this._gyroFs];
|
|
211
|
+
return [buf.readInt16BE(0) / sens * _DEG_TO_RAD, buf.readInt16BE(2) / sens * _DEG_TO_RAD,
|
|
212
|
+
buf.readInt16BE(4) / sens * _DEG_TO_RAD];
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* ICM-20948 full interface — extends `ICM20948Minimal`.
|
|
218
|
+
*
|
|
219
|
+
* Adds raw and burst reads, temperature, full-scale / filter / sample-rate
|
|
220
|
+
* configuration, sensor power control, low-power modes, gyro and
|
|
221
|
+
* accelerometer offsets, the AK09916 magnetometer (read through the chip's
|
|
222
|
+
* own I²C master — no second connection is needed), single-byte
|
|
223
|
+
* auxiliary-bus access, the 4 KiB FIFO, wake-on-motion, interrupts,
|
|
224
|
+
* self-test and the PLL time-base correction.
|
|
225
|
+
*
|
|
226
|
+
* Methods that touch banks 1–3 select the bank and return to bank 0, so the
|
|
227
|
+
* bank register is 0 between calls.
|
|
228
|
+
*/
|
|
229
|
+
class ICM20948Full extends ICM20948Minimal {
|
|
230
|
+
/**
|
|
231
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C connection to the ICM-20948.
|
|
232
|
+
*/
|
|
233
|
+
constructor(connection) {
|
|
234
|
+
super(connection);
|
|
235
|
+
this._magEnabled = false;
|
|
236
|
+
this._intCallback = null;
|
|
237
|
+
this._intHandler = null;
|
|
238
|
+
this._pollTimer = null;
|
|
239
|
+
this._intActiveLow = false;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async _lpSuspend() {
|
|
243
|
+
if (this._lpEn && !this._lpSuspended) {
|
|
244
|
+
const v = (await this._conn.readReg(_REG_PWR_MGMT_1, 1))[0];
|
|
245
|
+
await this._conn.writeReg(_REG_PWR_MGMT_1, v & ~0x20);
|
|
246
|
+
this._lpSuspended = true;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async _lpResume() {
|
|
251
|
+
if (this._lpSuspended) {
|
|
252
|
+
const v = (await this._conn.readReg(_REG_PWR_MGMT_1, 1))[0];
|
|
253
|
+
await this._conn.writeReg(_REG_PWR_MGMT_1, v | 0x20);
|
|
254
|
+
this._lpSuspended = false;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Read raw 3-axis accelerometer values.
|
|
260
|
+
* @returns {Promise<number[]>} [x, y, z] signed 16-bit counts.
|
|
261
|
+
*/
|
|
262
|
+
async accelRaw() {
|
|
263
|
+
await this._ready;
|
|
264
|
+
const b = await this._rd(0, _REG_ACCEL_XOUT_H, 6);
|
|
265
|
+
return [b.readInt16BE(0), b.readInt16BE(2), b.readInt16BE(4)];
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Read raw 3-axis gyroscope values.
|
|
270
|
+
* @returns {Promise<number[]>} [x, y, z] signed 16-bit counts.
|
|
271
|
+
*/
|
|
272
|
+
async gyroRaw() {
|
|
273
|
+
await this._ready;
|
|
274
|
+
const b = await this._rd(0, _REG_GYRO_XOUT_H, 6);
|
|
275
|
+
return [b.readInt16BE(0), b.readInt16BE(2), b.readInt16BE(4)];
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Burst-read acceleration, angular rate and temperature in one sample.
|
|
280
|
+
* @returns {Promise<Array>} `[[ax, ay, az], [gx, gy, gz], tempC]` — m/s², rad/s, °C.
|
|
281
|
+
*/
|
|
282
|
+
async readAll() {
|
|
283
|
+
await this._ready;
|
|
284
|
+
const b = await this._rd(0, _REG_ACCEL_XOUT_H, 14);
|
|
285
|
+
const aSens = _ACCEL_SENSITIVITY[this._accelFs];
|
|
286
|
+
const gSens = _GYRO_SENSITIVITY[this._gyroFs];
|
|
287
|
+
const accel = [0, 2, 4].map((o) => b.readInt16BE(o) / aSens * _G);
|
|
288
|
+
const gyro = [6, 8, 10].map((o) => b.readInt16BE(o) / gSens * _DEG_TO_RAD);
|
|
289
|
+
return [accel, gyro, b.readInt16BE(12) / 333.87 + 21.0];
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Read die temperature.
|
|
294
|
+
* @returns {Promise<number>} Temperature in °C.
|
|
295
|
+
*/
|
|
296
|
+
async temperature() {
|
|
297
|
+
await this._ready;
|
|
298
|
+
const b = await this._rd(0, _REG_TEMP_OUT_H, 2);
|
|
299
|
+
return b.readInt16BE(0) / 333.87 + 21.0;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Set gyroscope full-scale range.
|
|
304
|
+
* @param {number} [fullScale=0] - 0–3 (0=±250, 1=±500, 2=±1000, 3=±2000 dps).
|
|
305
|
+
* @returns {Promise<void>}
|
|
306
|
+
*/
|
|
307
|
+
async configureGyro(fullScale = 0) {
|
|
308
|
+
await this._ready;
|
|
309
|
+
await this._lpSuspend();
|
|
310
|
+
this._gyroFs = fullScale & 0x03;
|
|
311
|
+
await this._writeGyroConfig();
|
|
312
|
+
await this._lpResume();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Set accelerometer full-scale range.
|
|
317
|
+
* @param {number} [fullScale=0] - 0–3 (0=±2, 1=±4, 2=±8, 3=±16 g).
|
|
318
|
+
* @returns {Promise<void>}
|
|
319
|
+
*/
|
|
320
|
+
async configureAccel(fullScale = 0) {
|
|
321
|
+
await this._ready;
|
|
322
|
+
await this._lpSuspend();
|
|
323
|
+
this._accelFs = fullScale & 0x03;
|
|
324
|
+
await this._writeAccelConfig();
|
|
325
|
+
await this._lpResume();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Set the digital low-pass filters.
|
|
330
|
+
* @param {number} [gyroDlpf=3] - `GYRO_DLPFCFG` 0–7 (3 = 51.2 Hz).
|
|
331
|
+
* @param {number} [accelDlpf=3] - `ACCEL_DLPFCFG` 0–7 (3 = 50.4 Hz).
|
|
332
|
+
* @param {boolean} [gyroFilter=true] - false bypasses the gyro filter (9 kHz output rate).
|
|
333
|
+
* @param {boolean} [accelFilter=true] - false bypasses the accelerometer filter (4.5 kHz).
|
|
334
|
+
* @returns {Promise<void>}
|
|
335
|
+
*/
|
|
336
|
+
async configureFilters(gyroDlpf = 3, accelDlpf = 3, gyroFilter = true, accelFilter = true) {
|
|
337
|
+
await this._ready;
|
|
338
|
+
await this._lpSuspend();
|
|
339
|
+
this._gyroDlpf = gyroDlpf & 0x07;
|
|
340
|
+
this._accelDlpf = accelDlpf & 0x07;
|
|
341
|
+
this._gyroFilter = !!gyroFilter;
|
|
342
|
+
this._accelFilter = !!accelFilter;
|
|
343
|
+
await this._writeGyroConfig();
|
|
344
|
+
await this._writeAccelConfig();
|
|
345
|
+
await this._lpResume();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Set the sample-rate dividers; ODR = 1125 Hz / (1 + div).
|
|
350
|
+
* @param {number} [gyroDiv=10] - `GYRO_SMPLRT_DIV` 0–255.
|
|
351
|
+
* @param {number} [accelDiv=10] - `ACCEL_SMPLRT_DIV` 0–4095.
|
|
352
|
+
* @returns {Promise<void>}
|
|
353
|
+
*/
|
|
354
|
+
async configureSampleRate(gyroDiv = 10, accelDiv = 10) {
|
|
355
|
+
await this._ready;
|
|
356
|
+
await this._lpSuspend();
|
|
357
|
+
await this._wr(2, _REG_GYRO_SMPLRT_DIV, gyroDiv & 0xFF);
|
|
358
|
+
await this._wr(2, _REG_ACCEL_SMPLRT_DIV_1, (accelDiv >> 8) & 0x0F);
|
|
359
|
+
await this._wr(2, _REG_ACCEL_SMPLRT_DIV_2, accelDiv & 0xFF);
|
|
360
|
+
await this._lpResume();
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Switch the accelerometer and gyroscope on or off (`PWR_MGMT_2`).
|
|
365
|
+
* Turning a sensor on again needs its start-up time (accel 20 ms, gyro 35 ms).
|
|
366
|
+
* @param {boolean} [accel=true]
|
|
367
|
+
* @param {boolean} [gyro=true]
|
|
368
|
+
* @returns {Promise<void>}
|
|
369
|
+
*/
|
|
370
|
+
async setSensors(accel = true, gyro = true) {
|
|
371
|
+
await this._ready;
|
|
372
|
+
await this._conn.writeReg(_REG_PWR_MGMT_2, (accel ? 0 : 0x38) | (gyro ? 0 : 0x07));
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Set or clear the `SLEEP` bit in `PWR_MGMT_1`.
|
|
377
|
+
* @param {boolean} [sleep=true]
|
|
378
|
+
* @returns {Promise<void>}
|
|
379
|
+
*/
|
|
380
|
+
async setSleep(sleep = true) {
|
|
381
|
+
await this._ready;
|
|
382
|
+
let v = (await this._conn.readReg(_REG_PWR_MGMT_1, 1))[0];
|
|
383
|
+
v = sleep ? (v | 0x40) : (v & ~0x40);
|
|
384
|
+
await this._conn.writeReg(_REG_PWR_MGMT_1, v);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Configure duty-cycled low-power operation; `LP_EN` is set last because it blocks most register writes.
|
|
389
|
+
* @param {boolean} [accel=false] - Duty-cycle the accelerometer.
|
|
390
|
+
* @param {boolean} [gyro=false] - Duty-cycle the gyroscope.
|
|
391
|
+
* @param {number} [accelAvg=0] - `DEC3_CFG` averaging code 0–3.
|
|
392
|
+
* @param {number} [gyroAvg=0] - `GYRO_AVGCFG` averaging code 0–7.
|
|
393
|
+
* @returns {Promise<void>}
|
|
394
|
+
*/
|
|
395
|
+
async setLowPower(accel = false, gyro = false, accelAvg = 0, gyroAvg = 0) {
|
|
396
|
+
await this._ready;
|
|
397
|
+
this._lpSuspended = false;
|
|
398
|
+
const v = (await this._conn.readReg(_REG_PWR_MGMT_1, 1))[0] & ~0x20;
|
|
399
|
+
await this._conn.writeReg(_REG_PWR_MGMT_1, v);
|
|
400
|
+
await this._wr(2, _REG_ACCEL_CONFIG_2, accelAvg & 0x03);
|
|
401
|
+
await this._wr(2, _REG_GYRO_CONFIG_2, gyroAvg & 0x07);
|
|
402
|
+
await this._conn.writeReg(_REG_LP_CONFIG, 0x40 | (accel ? 0x20 : 0) | (gyro ? 0x10 : 0));
|
|
403
|
+
this._lpEn = !!(accel || gyro);
|
|
404
|
+
if (this._lpEn) await this._conn.writeReg(_REG_PWR_MGMT_1, v | 0x20);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Write the gyroscope offset-cancellation registers.
|
|
409
|
+
* @param {number} x - X offset in rad/s (0.0305 dps/LSB, signed 16-bit).
|
|
410
|
+
* @param {number} y - Y offset in rad/s.
|
|
411
|
+
* @param {number} z - Z offset in rad/s.
|
|
412
|
+
* @returns {Promise<void>}
|
|
413
|
+
*/
|
|
414
|
+
async setGyroOffset(x, y, z) {
|
|
415
|
+
await this._ready;
|
|
416
|
+
const buf = Buffer.alloc(6);
|
|
417
|
+
[x, y, z].forEach((v, i) => {
|
|
418
|
+
const code = Math.max(-32768, Math.min(32767, Math.round(v / _DEG_TO_RAD / 0.0305)));
|
|
419
|
+
buf.writeInt16BE(code, 2 * i);
|
|
420
|
+
});
|
|
421
|
+
await this._lpSuspend();
|
|
422
|
+
await this._wr(2, _REG_XG_OFFS_USRH, buf);
|
|
423
|
+
await this._lpResume();
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Read the 15-bit accelerometer offset codes (0.98 mg/LSB). The registers
|
|
428
|
+
* hold a per-part factory trim: read, adjust, write back.
|
|
429
|
+
* @returns {Promise<number[]>} [x, y, z] signed 15-bit offset codes.
|
|
430
|
+
*/
|
|
431
|
+
async getAccelOffset() {
|
|
432
|
+
await this._ready;
|
|
433
|
+
const raw = await this._rd(1, _REG_XA_OFFS_H, 8);
|
|
434
|
+
return [0, 3, 6].map((i) => {
|
|
435
|
+
let code = (raw[i] << 7) | (raw[i + 1] >> 1);
|
|
436
|
+
if (code & 0x4000) code -= 0x8000;
|
|
437
|
+
return code;
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Write the 15-bit accelerometer offset codes.
|
|
443
|
+
* @param {number} x - X offset code (signed 15-bit).
|
|
444
|
+
* @param {number} y - Y offset code.
|
|
445
|
+
* @param {number} z - Z offset code.
|
|
446
|
+
* @returns {Promise<void>}
|
|
447
|
+
*/
|
|
448
|
+
async setAccelOffset(x, y, z) {
|
|
449
|
+
await this._ready;
|
|
450
|
+
await this._lpSuspend();
|
|
451
|
+
const regs = [0x14, 0x17, 0x1A];
|
|
452
|
+
const vals = [x, y, z];
|
|
453
|
+
for (let i = 0; i < 3; i++) {
|
|
454
|
+
const code = vals[i] & 0x7FFF;
|
|
455
|
+
await this._wr(1, regs[i], Buffer.from([(code >> 7) & 0xFF, (code << 1) & 0xFE]));
|
|
456
|
+
}
|
|
457
|
+
await this._lpResume();
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
async _slv4Wait() {
|
|
461
|
+
for (let i = 0; i < 100; i++) {
|
|
462
|
+
const status = (await this._conn.readReg(_REG_I2C_MST_STATUS, 1))[0];
|
|
463
|
+
if (status & 0x10) throw new Error('ICM20948 auxiliary I2C NACK');
|
|
464
|
+
if (status & 0x40) return;
|
|
465
|
+
await _sleep(1);
|
|
466
|
+
}
|
|
467
|
+
throw new Error('ICM20948 auxiliary I2C transfer timed out');
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Read one byte from an auxiliary-bus device through I²C-master slave 4.
|
|
472
|
+
* @param {number} address - 7-bit device address.
|
|
473
|
+
* @param {number} register - Register inside the device.
|
|
474
|
+
* @returns {Promise<number>} The byte read.
|
|
475
|
+
* @throws {Error} On NACK or timeout.
|
|
476
|
+
*/
|
|
477
|
+
async auxReadByte(address, register) {
|
|
478
|
+
await this._ready;
|
|
479
|
+
await this._selectBank(3);
|
|
480
|
+
await this._conn.writeReg(_REG_I2C_SLV4_ADDR, 0x80 | (address & 0x7F));
|
|
481
|
+
await this._conn.writeReg(_REG_I2C_SLV4_REG, register & 0xFF);
|
|
482
|
+
await this._conn.writeReg(_REG_I2C_SLV4_CTRL, 0x80);
|
|
483
|
+
await this._selectBank(0);
|
|
484
|
+
await this._slv4Wait();
|
|
485
|
+
return (await this._rd(3, _REG_I2C_SLV4_DI, 1))[0];
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Write one byte to an auxiliary-bus device through I²C-master slave 4.
|
|
490
|
+
* @param {number} address - 7-bit device address.
|
|
491
|
+
* @param {number} register - Register inside the device.
|
|
492
|
+
* @param {number} value - Byte to write.
|
|
493
|
+
* @returns {Promise<void>}
|
|
494
|
+
* @throws {Error} On NACK or timeout.
|
|
495
|
+
*/
|
|
496
|
+
async auxWriteByte(address, register, value) {
|
|
497
|
+
await this._ready;
|
|
498
|
+
await this._selectBank(3);
|
|
499
|
+
await this._conn.writeReg(_REG_I2C_SLV4_ADDR, address & 0x7F);
|
|
500
|
+
await this._conn.writeReg(_REG_I2C_SLV4_REG, register & 0xFF);
|
|
501
|
+
await this._conn.writeReg(_REG_I2C_SLV4_DO, value & 0xFF);
|
|
502
|
+
await this._conn.writeReg(_REG_I2C_SLV4_CTRL, 0x80);
|
|
503
|
+
await this._selectBank(0);
|
|
504
|
+
await this._slv4Wait();
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Enable the AK09916 magnetometer via the I²C master. Resets the
|
|
509
|
+
* magnetometer, verifies `WIA2` == 0x09, selects continuous measurement 4
|
|
510
|
+
* (100 Hz) and configures slave 0 to copy the 9-byte `ST1..ST2` block to
|
|
511
|
+
* `EXT_SLV_SENS_DATA_00..08` continuously.
|
|
512
|
+
* @returns {Promise<void>}
|
|
513
|
+
* @throws {Error} If `WIA2` does not read 0x09 or an auxiliary transfer fails.
|
|
514
|
+
*/
|
|
515
|
+
async enableMag() {
|
|
516
|
+
await this._ready;
|
|
517
|
+
await this._lpSuspend();
|
|
518
|
+
const ctrl = (await this._conn.readReg(_REG_USER_CTRL, 1))[0];
|
|
519
|
+
await this._conn.writeReg(_REG_USER_CTRL, ctrl | 0x20); // I2C_MST_EN
|
|
520
|
+
await this._wr(3, _REG_I2C_MST_CTRL, 0x07); // 345.6 kHz
|
|
521
|
+
await this.auxWriteByte(_AK_ADDR, _AK_CNTL3, 0x01); // soft reset
|
|
522
|
+
await _sleep(2);
|
|
523
|
+
const wia = await this.auxReadByte(_AK_ADDR, _AK_WIA2);
|
|
524
|
+
if (wia !== _AK_WIA2_VALUE) {
|
|
525
|
+
throw new Error('AK09916 WIA2: expected 0x09, got 0x' + wia.toString(16));
|
|
526
|
+
}
|
|
527
|
+
await this.auxWriteByte(_AK_ADDR, _AK_CNTL2, 0x08); // continuous 4
|
|
528
|
+
await this._selectBank(3);
|
|
529
|
+
await this._conn.writeReg(_REG_I2C_SLV0_ADDR, 0x80 | _AK_ADDR);
|
|
530
|
+
await this._conn.writeReg(_REG_I2C_SLV0_REG, _AK_ST1);
|
|
531
|
+
await this._conn.writeReg(_REG_I2C_SLV0_CTRL, 0x89);
|
|
532
|
+
await this._selectBank(0);
|
|
533
|
+
this._magEnabled = true;
|
|
534
|
+
await this._lpResume();
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Write the magnetometer `CNTL2` mode through slave 4.
|
|
539
|
+
* @param {number} mode - 0 power-down, 1 single, 2/4/6 continuous 1–3, 8 continuous 4.
|
|
540
|
+
* @returns {Promise<void>}
|
|
541
|
+
*/
|
|
542
|
+
async magSetMode(mode) {
|
|
543
|
+
await this.auxWriteByte(_AK_ADDR, _AK_CNTL2, mode & 0x1F);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
async _magBlock() {
|
|
547
|
+
await this._ready;
|
|
548
|
+
if (!this._magEnabled) {
|
|
549
|
+
throw new Error('Magnetometer not enabled. Call enableMag() first.');
|
|
550
|
+
}
|
|
551
|
+
return this._rd(0, _REG_EXT_SLV_SENS_DATA_00, 9);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Read the magnetic field.
|
|
556
|
+
* @returns {Promise<number[]>} [x, y, z] in µT, as the chip reports them.
|
|
557
|
+
* @throws {Error} If `enableMag()` has not been called, or the sensor overflowed (`HOFL`).
|
|
558
|
+
*/
|
|
559
|
+
async mag() {
|
|
560
|
+
const b = await this._magBlock();
|
|
561
|
+
if (b[8] & 0x08) throw new Error('Magnetometer overflow (HOFL)');
|
|
562
|
+
return [b.readInt16LE(1) * _MAG_SENSITIVITY, b.readInt16LE(3) * _MAG_SENSITIVITY,
|
|
563
|
+
b.readInt16LE(5) * _MAG_SENSITIVITY];
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Read the raw magnetometer values.
|
|
568
|
+
* @returns {Promise<number[]>} [x, y, z] signed 16-bit counts.
|
|
569
|
+
* @throws {Error} If `enableMag()` has not been called.
|
|
570
|
+
*/
|
|
571
|
+
async magRaw() {
|
|
572
|
+
const b = await this._magBlock();
|
|
573
|
+
return [b.readInt16LE(1), b.readInt16LE(3), b.readInt16LE(5)];
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Check the magnetometer `DRDY` flag from the shadowed block.
|
|
578
|
+
* @returns {Promise<boolean>}
|
|
579
|
+
* @throws {Error} If `enableMag()` has not been called.
|
|
580
|
+
*/
|
|
581
|
+
async magDataReady() {
|
|
582
|
+
const b = await this._magBlock();
|
|
583
|
+
return (b[0] & 0x01) !== 0;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Check whether new raw data is available (`INT_STATUS_1.RAW_DATA_0_RDY_INT`).
|
|
588
|
+
* @returns {Promise<boolean>}
|
|
589
|
+
*/
|
|
590
|
+
async dataReady() {
|
|
591
|
+
await this._ready;
|
|
592
|
+
return ((await this._rd(0, _REG_INT_STATUS_1, 1))[0] & 0x01) !== 0;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Select FIFO sources, enable the FIFO and reset it.
|
|
597
|
+
* @param {boolean} [accel=true] - Write accelerometer data (6 bytes).
|
|
598
|
+
* @param {boolean} [gyro=true] - Write gyroscope data (6 bytes).
|
|
599
|
+
* @param {boolean} [temp=false] - Write temperature data (2 bytes).
|
|
600
|
+
* @returns {Promise<void>}
|
|
601
|
+
*/
|
|
602
|
+
async enableFifo(accel = true, gyro = true, temp = false) {
|
|
603
|
+
await this._ready;
|
|
604
|
+
await this._lpSuspend();
|
|
605
|
+
await this._conn.writeReg(_REG_FIFO_EN_2, (accel ? 0x10 : 0) | (gyro ? 0x0E : 0) | (temp ? 0x01 : 0));
|
|
606
|
+
const ctrl = (await this._conn.readReg(_REG_USER_CTRL, 1))[0];
|
|
607
|
+
await this._conn.writeReg(_REG_USER_CTRL, ctrl | 0x40);
|
|
608
|
+
await this.resetFifo();
|
|
609
|
+
await this._lpResume();
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Select FIFO mode.
|
|
614
|
+
* @param {boolean} [snapshot=false] - true stops when full, false overwrites the oldest data.
|
|
615
|
+
* @returns {Promise<void>}
|
|
616
|
+
*/
|
|
617
|
+
async setFifoMode(snapshot = false) {
|
|
618
|
+
await this._ready;
|
|
619
|
+
await this._lpSuspend();
|
|
620
|
+
await this._conn.writeReg(_REG_FIFO_MODE, snapshot ? 0x1F : 0x00);
|
|
621
|
+
await this._lpResume();
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Read the number of bytes in the FIFO.
|
|
626
|
+
* @returns {Promise<number>} Byte count (0–4096).
|
|
627
|
+
*/
|
|
628
|
+
async fifoCount() {
|
|
629
|
+
await this._ready;
|
|
630
|
+
const b = await this._rd(0, _REG_FIFO_COUNTH, 2);
|
|
631
|
+
return ((b[0] & 0x1F) << 8) | b[1];
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Read bytes from the FIFO (the frame layout is not documented in the datasheet).
|
|
636
|
+
* @param {number} [count] - Maximum bytes to read (default: all available).
|
|
637
|
+
* @returns {Promise<Buffer>} FIFO data.
|
|
638
|
+
*/
|
|
639
|
+
async readFifo(count) {
|
|
640
|
+
const available = await this.fifoCount();
|
|
641
|
+
let remaining = (count === undefined || count > available) ? available : count;
|
|
642
|
+
const chunks = [];
|
|
643
|
+
while (remaining > 0) {
|
|
644
|
+
const n = Math.min(remaining, 32);
|
|
645
|
+
chunks.push(await this._rd(0, _REG_FIFO_R_W, n));
|
|
646
|
+
remaining -= n;
|
|
647
|
+
}
|
|
648
|
+
return Buffer.concat(chunks);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* Reset the FIFO by asserting and de-asserting `FIFO_RST`.
|
|
653
|
+
* @returns {Promise<void>}
|
|
654
|
+
*/
|
|
655
|
+
async resetFifo() {
|
|
656
|
+
await this._ready;
|
|
657
|
+
await this._conn.writeReg(_REG_FIFO_RST, 0x1F);
|
|
658
|
+
await this._conn.writeReg(_REG_FIFO_RST, 0x00);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* Configure wake-on-motion (needs the accelerometer on with its filter enabled).
|
|
663
|
+
* @param {number} thresholdMg - Threshold in mg (4 mg/LSB, 0–1020).
|
|
664
|
+
* @param {boolean} [enabled=true] - true to arm, false to disarm.
|
|
665
|
+
* @returns {Promise<void>}
|
|
666
|
+
*/
|
|
667
|
+
async setWakeOnMotion(thresholdMg, enabled = true) {
|
|
668
|
+
await this._ready;
|
|
669
|
+
await this._lpSuspend();
|
|
670
|
+
const thr = Math.max(0, Math.min(255, Math.round(thresholdMg / 4)));
|
|
671
|
+
await this._wr(2, _REG_ACCEL_WOM_THR, thr);
|
|
672
|
+
await this._wr(2, _REG_ACCEL_INTEL_CTRL, enabled ? 0x03 : 0x00);
|
|
673
|
+
if (enabled) await this.enableInterrupt(ICM20948Source.SOURCE_WAKE_ON_MOTION);
|
|
674
|
+
else await this.disableInterrupt(ICM20948Source.SOURCE_WAKE_ON_MOTION);
|
|
675
|
+
await this._lpResume();
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* Configure the INT1 pin (`INT_PIN_CFG`).
|
|
680
|
+
* @param {boolean} [activeLow=false]
|
|
681
|
+
* @param {boolean} [openDrain=false]
|
|
682
|
+
* @param {boolean} [latch=false] - Hold the level until the status is cleared.
|
|
683
|
+
* @param {boolean} [anyReadClears=false] - Any read clears `INT_STATUS`.
|
|
684
|
+
* @returns {Promise<void>}
|
|
685
|
+
*/
|
|
686
|
+
async setInterruptPin(activeLow = false, openDrain = false, latch = false, anyReadClears = false) {
|
|
687
|
+
await this._ready;
|
|
688
|
+
this._intActiveLow = !!activeLow;
|
|
689
|
+
await this._conn.writeReg(_REG_INT_PIN_CFG,
|
|
690
|
+
(activeLow ? 0x80 : 0) | (openDrain ? 0x40 : 0) | (latch ? 0x20 : 0) | (anyReadClears ? 0x10 : 0));
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
async _intEnable(source, on) {
|
|
694
|
+
await this._ready;
|
|
695
|
+
const entry = _INT_ENABLE_MAP[source];
|
|
696
|
+
if (!entry) throw new Error('unknown interrupt source: ' + source);
|
|
697
|
+
const [reg, mask] = entry;
|
|
698
|
+
const v = (await this._conn.readReg(reg, 1))[0];
|
|
699
|
+
await this._conn.writeReg(reg, on ? (v | mask) : (v & ~mask));
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* Enable one interrupt source.
|
|
704
|
+
* @param {number} source - One of the `SOURCE_*` constants.
|
|
705
|
+
* @returns {Promise<void>}
|
|
706
|
+
*/
|
|
707
|
+
async enableInterrupt(source) {
|
|
708
|
+
await this._intEnable(source, true);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* Disable one interrupt source.
|
|
713
|
+
* @param {number} source - One of the `SOURCE_*` constants.
|
|
714
|
+
* @returns {Promise<void>}
|
|
715
|
+
*/
|
|
716
|
+
async disableInterrupt(source) {
|
|
717
|
+
await this._intEnable(source, false);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* Read (and clear) `INT_STATUS`, `INT_STATUS_1` and `INT_STATUS_2`.
|
|
722
|
+
* @returns {Promise<number>} Combined status using the `SOURCE_*` bit layout.
|
|
723
|
+
*/
|
|
724
|
+
async pollInterrupt() {
|
|
725
|
+
await this._ready;
|
|
726
|
+
const s = await this._rd(0, _REG_INT_STATUS, 3);
|
|
727
|
+
let status = 0;
|
|
728
|
+
if (s[1] & 0x01) status |= ICM20948Source.SOURCE_DATA_READY;
|
|
729
|
+
if (s[0] & 0x08) status |= ICM20948Source.SOURCE_WAKE_ON_MOTION;
|
|
730
|
+
if (s[0] & 0x01) status |= ICM20948Source.SOURCE_I2C_MASTER;
|
|
731
|
+
if (s[0] & 0x04) status |= ICM20948Source.SOURCE_PLL_READY;
|
|
732
|
+
if (s[2] & 0x1F) status |= ICM20948Source.SOURCE_FIFO_OVERFLOW;
|
|
733
|
+
return status;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* Subscribe to INT1 assertions. Uses the connection's `intPin` when
|
|
738
|
+
* present, otherwise falls back to a 5 ms polling timer.
|
|
739
|
+
* @param {function(number): void} callback - Called with the `pollInterrupt()` status.
|
|
740
|
+
* @returns {Promise<void>}
|
|
741
|
+
*/
|
|
742
|
+
async onInterrupt(callback) {
|
|
743
|
+
await this._ready;
|
|
744
|
+
this._intCallback = callback;
|
|
745
|
+
const intPin = this._conn ? this._conn.intPin : null;
|
|
746
|
+
if (intPin) {
|
|
747
|
+
this._intHandler = () => { this._dispatchInt(); };
|
|
748
|
+
await intPin.onEdge(this._intHandler, this._intActiveLow ? 'falling' : 'rising');
|
|
749
|
+
} else {
|
|
750
|
+
this._pollTimer = setInterval(() => { this._dispatchInt(); }, 5);
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Unsubscribe from INT1 assertions.
|
|
756
|
+
* @returns {Promise<void>}
|
|
757
|
+
*/
|
|
758
|
+
async offInterrupt() {
|
|
759
|
+
this._intCallback = null;
|
|
760
|
+
const intPin = this._conn ? this._conn.intPin : null;
|
|
761
|
+
if (intPin && this._intHandler) {
|
|
762
|
+
await intPin.offEdge(this._intHandler);
|
|
763
|
+
}
|
|
764
|
+
this._intHandler = null;
|
|
765
|
+
if (this._pollTimer !== null) {
|
|
766
|
+
clearInterval(this._pollTimer);
|
|
767
|
+
this._pollTimer = null;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
async _dispatchInt() {
|
|
772
|
+
const cb = this._intCallback;
|
|
773
|
+
if (!cb) return;
|
|
774
|
+
try {
|
|
775
|
+
const status = await this.pollInterrupt();
|
|
776
|
+
if (status) cb(status);
|
|
777
|
+
} catch (err) {
|
|
778
|
+
// ignore: the next edge or poll retries
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
async _averageRaw(samples = 5) {
|
|
783
|
+
const sa = [0, 0, 0];
|
|
784
|
+
const sg = [0, 0, 0];
|
|
785
|
+
for (let n = 0; n < samples; n++) {
|
|
786
|
+
const a = await this.accelRaw();
|
|
787
|
+
const g = await this.gyroRaw();
|
|
788
|
+
for (let i = 0; i < 3; i++) { sa[i] += a[i]; sg[i] += g[i]; }
|
|
789
|
+
await _sleep(11);
|
|
790
|
+
}
|
|
791
|
+
return [sa.map((v) => v / samples), sg.map((v) => v / samples)];
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/**
|
|
795
|
+
* Run the self-test enable procedure and return the raw response.
|
|
796
|
+
* Configures ±2 g / ±250 dps, averages samples with and without the
|
|
797
|
+
* self-test actuation and restores the configuration. The datasheet gives
|
|
798
|
+
* no pass/fail limits, so none is evaluated.
|
|
799
|
+
* @returns {Promise<Array>} `[[ax, ay, az], [gx, gy, gz]]` — raw response (enabled minus disabled) per axis.
|
|
800
|
+
*/
|
|
801
|
+
async selfTest() {
|
|
802
|
+
await this._ready;
|
|
803
|
+
const saved = [this._accelFs, this._gyroFs];
|
|
804
|
+
await this.configureAccel(0);
|
|
805
|
+
await this.configureGyro(0);
|
|
806
|
+
await _sleep(50);
|
|
807
|
+
const [a0, g0] = await this._averageRaw();
|
|
808
|
+
await this._lpSuspend();
|
|
809
|
+
await this._wr(2, _REG_GYRO_CONFIG_2, 0x38);
|
|
810
|
+
await this._wr(2, _REG_ACCEL_CONFIG_2, 0x1C);
|
|
811
|
+
await _sleep(50);
|
|
812
|
+
const [a1, g1] = await this._averageRaw();
|
|
813
|
+
await this._wr(2, _REG_GYRO_CONFIG_2, 0x00);
|
|
814
|
+
await this._wr(2, _REG_ACCEL_CONFIG_2, 0x00);
|
|
815
|
+
await this._lpResume();
|
|
816
|
+
await this.configureAccel(saved[0]);
|
|
817
|
+
await this.configureGyro(saved[1]);
|
|
818
|
+
return [a1.map((v, i) => v - a0[i]), g1.map((v, i) => v - g0[i])];
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
/**
|
|
822
|
+
* Read the per-part PLL time-base correction.
|
|
823
|
+
* @returns {Promise<number>} Deviation in percent (0.079 %/LSB, signed).
|
|
824
|
+
*/
|
|
825
|
+
async readTimebaseCorrection() {
|
|
826
|
+
await this._ready;
|
|
827
|
+
const raw = (await this._rd(1, _REG_TIMEBASE_CORRECTION_PLL, 1))[0];
|
|
828
|
+
return (raw > 127 ? raw - 256 : raw) * 0.079;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
/**
|
|
832
|
+
* Issue `DEVICE_RESET` and wait. The chip is left in sleep with default
|
|
833
|
+
* registers; create a new driver afterwards to run the initialization again.
|
|
834
|
+
* @returns {Promise<void>}
|
|
835
|
+
*/
|
|
836
|
+
async reset() {
|
|
837
|
+
await this._ready;
|
|
838
|
+
await this._conn.writeReg(_REG_PWR_MGMT_1, 0x80);
|
|
839
|
+
await _sleep(100);
|
|
840
|
+
this._bank = 0;
|
|
841
|
+
this._lpEn = false;
|
|
842
|
+
this._lpSuspended = false;
|
|
843
|
+
this._magEnabled = false;
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
Object.assign(ICM20948Full, ICM20948Source);
|
|
848
|
+
|
|
849
|
+
module.exports = { ICM20948Minimal, ICM20948Full, ICM20948Source };
|