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,565 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _SYNC = 0x05;
|
|
4
|
+
const _MASTER_ADDR = 0xFF;
|
|
5
|
+
|
|
6
|
+
const _REG_GCONF = 0x00;
|
|
7
|
+
const _REG_GSTAT = 0x01;
|
|
8
|
+
const _REG_IFCNT = 0x02;
|
|
9
|
+
const _REG_NODECONF = 0x03;
|
|
10
|
+
const _REG_OTP_READ = 0x05;
|
|
11
|
+
const _REG_IOIN = 0x06;
|
|
12
|
+
const _REG_IHOLD_IRUN = 0x10;
|
|
13
|
+
const _REG_TPOWERDOWN = 0x11;
|
|
14
|
+
const _REG_TSTEP = 0x12;
|
|
15
|
+
const _REG_TPWMTHRS = 0x13;
|
|
16
|
+
const _REG_TCOOLTHRS = 0x14;
|
|
17
|
+
const _REG_VACTUAL = 0x22;
|
|
18
|
+
const _REG_SGTHRS = 0x40;
|
|
19
|
+
const _REG_SG_RESULT = 0x41;
|
|
20
|
+
const _REG_COOLCONF = 0x42;
|
|
21
|
+
const _REG_MSCNT = 0x6A;
|
|
22
|
+
const _REG_MSCURACT = 0x6B;
|
|
23
|
+
const _REG_CHOPCONF = 0x6C;
|
|
24
|
+
const _REG_DRV_STATUS = 0x6F;
|
|
25
|
+
const _REG_PWMCONF = 0x70;
|
|
26
|
+
const _REG_PWM_SCALE = 0x71;
|
|
27
|
+
const _REG_PWM_AUTO = 0x72;
|
|
28
|
+
|
|
29
|
+
const _READABLE = new Set([0x00, 0x01, 0x02, 0x05, 0x06, 0x07, 0x12, 0x41, 0x6A, 0x6B, 0x6C, 0x6F, 0x70, 0x71, 0x72]);
|
|
30
|
+
const _WRITABLE = new Set([0x00, 0x01, 0x03, 0x10, 0x11, 0x13, 0x14, 0x22, 0x40, 0x42, 0x6C, 0x70]);
|
|
31
|
+
|
|
32
|
+
const _GCONF_I_SCALE_ANALOG = 1 << 0;
|
|
33
|
+
const _GCONF_INTERNAL_RSENSE = 1 << 1;
|
|
34
|
+
const _GCONF_EN_SPREADCYCLE = 1 << 2;
|
|
35
|
+
const _GCONF_SHAFT = 1 << 3;
|
|
36
|
+
const _GCONF_INDEX_OTPW = 1 << 4;
|
|
37
|
+
const _GCONF_INDEX_STEP = 1 << 5;
|
|
38
|
+
const _GCONF_PDN_DISABLE = 1 << 6;
|
|
39
|
+
const _GCONF_MSTEP_REG_SELECT = 1 << 7;
|
|
40
|
+
const _GCONF_MULTISTEP_FILT = 1 << 8;
|
|
41
|
+
|
|
42
|
+
const _VERSION = 0x21;
|
|
43
|
+
const _PWMCONF_DEFAULT = 0xC10D0024;
|
|
44
|
+
|
|
45
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
46
|
+
|
|
47
|
+
/** CRC-8 (polynomial 0x07, init 0, LSB first) over a datagram body. */
|
|
48
|
+
function crc8(data) {
|
|
49
|
+
let crc = 0;
|
|
50
|
+
for (let byte of data) {
|
|
51
|
+
for (let i = 0; i < 8; i++) {
|
|
52
|
+
if (((crc >> 7) ^ (byte & 1)) !== 0) crc = ((crc << 1) ^ 0x07) & 0xFF;
|
|
53
|
+
else crc = (crc << 1) & 0xFF;
|
|
54
|
+
byte >>= 1;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return crc;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function toSigned(value, bits) {
|
|
61
|
+
const v = value & ((1 << bits) - 1);
|
|
62
|
+
return (v & (1 << (bits - 1))) ? v - (1 << bits) : v;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Trinamic TMC2209 stepper driver over single-wire UART
|
|
67
|
+
* — minimal interface.
|
|
68
|
+
*
|
|
69
|
+
* Brings the driver up in UART mode (pdn_disable, microstep resolution from
|
|
70
|
+
* CHOPCONF.MRES), StealthChop, 16 microsteps with 256-step interpolation and
|
|
71
|
+
* 0.5 A RMS run current, then lets the internal step generator (VACTUAL)
|
|
72
|
+
* spin the motor -- no STEP pulses required. Datagram framing (sync byte,
|
|
73
|
+
* CRC8, host echo) is handled here; the connection only needs plain
|
|
74
|
+
* write / read / writeRead.
|
|
75
|
+
*
|
|
76
|
+
* The constructor performs no I/O; call `await init()` before use.
|
|
77
|
+
*/
|
|
78
|
+
class TMC2209Minimal {
|
|
79
|
+
/**
|
|
80
|
+
* @param {object} connection - UART connection to the chip's PDN_UART pin.
|
|
81
|
+
* @param {object} [options]
|
|
82
|
+
* @param {number} [options.rSenseOhm=0.11] - External sense resistor in ohm.
|
|
83
|
+
* @param {number} [options.address=0] - UART node address 0-3 (MS1_AD0 / MS2_AD1 pins).
|
|
84
|
+
* @param {object|null} [options.enPin=null] - OutputPin wired to ENN (active low).
|
|
85
|
+
* @param {boolean} [options.echo=true] - True when TX and RX share PDN_UART.
|
|
86
|
+
* @throws {RangeError} If address is not 0-3.
|
|
87
|
+
*/
|
|
88
|
+
constructor(connection, options = {}) {
|
|
89
|
+
this._address = options.address ?? 0;
|
|
90
|
+
if (!Number.isInteger(this._address) || this._address < 0 || this._address > 3) {
|
|
91
|
+
throw new RangeError('address must be 0-3');
|
|
92
|
+
}
|
|
93
|
+
this._multiNode = options.multiNode ?? false;
|
|
94
|
+
this._conn = connection;
|
|
95
|
+
this._rSense = options.rSenseOhm ?? 0.11;
|
|
96
|
+
this._enPin = options.enPin ?? null;
|
|
97
|
+
this._echo = options.echo ?? true;
|
|
98
|
+
this._fclk = options.fclkHz ?? 12000000;
|
|
99
|
+
this._shadow = new Map();
|
|
100
|
+
this._microsteps = 16;
|
|
101
|
+
this._vsense = false;
|
|
102
|
+
this._internalRsense = false;
|
|
103
|
+
this._toff = 3;
|
|
104
|
+
this._runCs = 0;
|
|
105
|
+
this._holdCs = 0;
|
|
106
|
+
this._iholdDelay = 6;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async _recover() {
|
|
110
|
+
if (typeof this._conn.flush === 'function') await this._conn.flush();
|
|
111
|
+
await sleep(2);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async _writeDatagram(reg, value) {
|
|
115
|
+
const body = Buffer.from([_SYNC, this._address, (reg | 0x80) & 0xFF,
|
|
116
|
+
(value >>> 24) & 0xFF, (value >>> 16) & 0xFF, (value >>> 8) & 0xFF, value & 0xFF]);
|
|
117
|
+
const datagram = Buffer.concat([body, Buffer.from([crc8(body)])]);
|
|
118
|
+
await this._conn.write(datagram);
|
|
119
|
+
if (this._echo) {
|
|
120
|
+
const echoed = Buffer.from(await this._conn.read(8));
|
|
121
|
+
if (!echoed.equals(datagram)) throw new Error('TMC2209: write echo mismatch');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async _readDatagram(reg) {
|
|
126
|
+
const head = Buffer.from([_SYNC, this._address, reg & 0x7F]);
|
|
127
|
+
const request = Buffer.concat([head, Buffer.from([crc8(head)])]);
|
|
128
|
+
let reply;
|
|
129
|
+
if (this._echo) {
|
|
130
|
+
const raw = Buffer.from(await this._conn.writeRead(request, 12));
|
|
131
|
+
if (raw.length !== 12) throw new Error('TMC2209: short read');
|
|
132
|
+
if (!raw.subarray(0, 4).equals(request)) throw new Error('TMC2209: read echo mismatch');
|
|
133
|
+
reply = raw.subarray(4);
|
|
134
|
+
} else {
|
|
135
|
+
reply = Buffer.from(await this._conn.writeRead(request, 8));
|
|
136
|
+
if (reply.length !== 8) throw new Error('TMC2209: short read');
|
|
137
|
+
}
|
|
138
|
+
if (reply[0] !== _SYNC || reply[1] !== _MASTER_ADDR || reply[2] !== (reg & 0x7F)) {
|
|
139
|
+
throw new Error('TMC2209: malformed reply');
|
|
140
|
+
}
|
|
141
|
+
if (crc8(reply.subarray(0, 7)) !== reply[7]) throw new Error('TMC2209: reply CRC error');
|
|
142
|
+
return reply.readUInt32BE(3);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async _writeReg(reg, value) {
|
|
146
|
+
value = value >>> 0;
|
|
147
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
148
|
+
try {
|
|
149
|
+
await this._writeDatagram(reg, value);
|
|
150
|
+
break;
|
|
151
|
+
} catch (e) {
|
|
152
|
+
if (attempt) throw e;
|
|
153
|
+
await this._recover();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
this._txCount = ((this._txCount ?? 0) + 1);
|
|
157
|
+
this._shadow.set(reg, value);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async _readReg(reg) {
|
|
161
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
162
|
+
try {
|
|
163
|
+
return await this._readDatagram(reg);
|
|
164
|
+
} catch (e) {
|
|
165
|
+
if (attempt) throw e;
|
|
166
|
+
await this._recover();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async _modify(reg, mask, value) {
|
|
172
|
+
const current = this._shadow.get(reg) ?? 0;
|
|
173
|
+
await this._writeReg(reg, ((current & ~mask) | (value & mask)) >>> 0);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
_currentToCs(rmsA) {
|
|
177
|
+
if (this._internalRsense) throw new Error('set_current is unsupported with internal Rsense');
|
|
178
|
+
const vfs = this._vsense ? 0.180 : 0.325;
|
|
179
|
+
const cs = Math.round(rmsA * Math.SQRT2 * (this._rSense + 0.020) * 32 / vfs) - 1;
|
|
180
|
+
return Math.max(0, Math.min(31, cs));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async _writeIholdIrun() {
|
|
184
|
+
await this._writeReg(_REG_IHOLD_IRUN,
|
|
185
|
+
(this._holdCs & 0x1F) | ((this._runCs & 0x1F) << 8) | ((this._iholdDelay & 0xF) << 16));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Probe the chip, apply the default configuration and verify IFCNT.
|
|
190
|
+
* @returns {Promise<void>}
|
|
191
|
+
* @throws {Error} If the chip does not answer, has an unexpected identity
|
|
192
|
+
* or does not acknowledge the writes.
|
|
193
|
+
*/
|
|
194
|
+
async init() {
|
|
195
|
+
if (this._enPin) await this._enPin.set(true);
|
|
196
|
+
await sleep(10);
|
|
197
|
+
const baseline = (await this._readReg(_REG_IFCNT)) & 0xFF;
|
|
198
|
+
const ioin = await this._readReg(_REG_IOIN);
|
|
199
|
+
const version = (ioin >>> 24) & 0xFF;
|
|
200
|
+
if (version !== _VERSION) {
|
|
201
|
+
throw new Error('TMC2209 not found (IOIN=0x' + ioin.toString(16) + ')');
|
|
202
|
+
}
|
|
203
|
+
let count = 6;
|
|
204
|
+
if (this._multiNode) {
|
|
205
|
+
await this._writeReg(_REG_NODECONF, 2 << 8);
|
|
206
|
+
count = 7;
|
|
207
|
+
}
|
|
208
|
+
await this._writeReg(_REG_GCONF, _GCONF_I_SCALE_ANALOG | _GCONF_PDN_DISABLE |
|
|
209
|
+
_GCONF_MSTEP_REG_SELECT | _GCONF_MULTISTEP_FILT);
|
|
210
|
+
await this._writeReg(_REG_GSTAT, 0x07);
|
|
211
|
+
this._runCs = this._currentToCs(0.5);
|
|
212
|
+
this._holdCs = this._currentToCs(0.5 / 3);
|
|
213
|
+
await this._writeIholdIrun();
|
|
214
|
+
await this._writeReg(_REG_TPOWERDOWN, 20);
|
|
215
|
+
let chop = await this._readReg(_REG_CHOPCONF);
|
|
216
|
+
chop &= ~(0xF | (3 << 15) | (7 << 4) | (0xF << 7) | (0xF << 24) | (1 << 17));
|
|
217
|
+
chop |= 3 | (2 << 15) | (5 << 4) | (4 << 24) | (1 << 28);
|
|
218
|
+
await this._writeReg(_REG_CHOPCONF, chop);
|
|
219
|
+
await this._writeReg(_REG_PWMCONF, _PWMCONF_DEFAULT);
|
|
220
|
+
const ifcnt = (await this._readReg(_REG_IFCNT)) & 0xFF;
|
|
221
|
+
if (ifcnt !== ((baseline + count) & 0xFF)) {
|
|
222
|
+
throw new Error('TMC2209: IFCNT mismatch (writes not acknowledged)');
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Switch the power stage on (ENN low, or TOFF restored without a pin). @returns {Promise<void>} */
|
|
227
|
+
async enable() {
|
|
228
|
+
if (this._enPin) await this._enPin.set(false);
|
|
229
|
+
else await this._writeReg(_REG_CHOPCONF, (((this._shadow.get(_REG_CHOPCONF) & ~0xF) | this._toff) >>> 0));
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Switch the power stage off; the motor freewheels. @returns {Promise<void>} */
|
|
233
|
+
async disable() {
|
|
234
|
+
if (this._enPin) { await this._enPin.set(true); return; }
|
|
235
|
+
const chop = this._shadow.get(_REG_CHOPCONF);
|
|
236
|
+
if (chop & 0xF) this._toff = chop & 0xF;
|
|
237
|
+
await this._writeReg(_REG_CHOPCONF, (chop & ~0xF) >>> 0);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Set the run current; hold current becomes one third of it.
|
|
242
|
+
* @param {number} rmsA - Run current in A RMS (at most 1.4).
|
|
243
|
+
* @returns {Promise<void>}
|
|
244
|
+
*/
|
|
245
|
+
async setCurrent(rmsA) {
|
|
246
|
+
this._runCs = this._currentToCs(rmsA);
|
|
247
|
+
this._holdCs = this._currentToCs(rmsA / 3);
|
|
248
|
+
await this._writeIholdIrun();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Move with the internal step generator (VACTUAL).
|
|
253
|
+
* @param {number} stepsPerS - Full steps per second at the configured microstepping; sign = direction, 0 stops.
|
|
254
|
+
* @returns {Promise<void>}
|
|
255
|
+
*/
|
|
256
|
+
async run(stepsPerS) {
|
|
257
|
+
let v = Math.round(stepsPerS * this._microsteps * 16777216 / this._fclk);
|
|
258
|
+
v = Math.max(-8388607, Math.min(8388607, v));
|
|
259
|
+
await this._writeReg(_REG_VACTUAL, (v & 0xFFFFFF) >>> 0);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Stop the internal step generator (VACTUAL = 0). @returns {Promise<void>} */
|
|
263
|
+
async stop() { await this._writeReg(_REG_VACTUAL, 0); }
|
|
264
|
+
|
|
265
|
+
/** @returns {Promise<boolean>} True when DRV_STATUS.stst is set. */
|
|
266
|
+
async isStandstill() { return ((await this._readReg(_REG_DRV_STATUS)) >>> 31) === 1; }
|
|
267
|
+
|
|
268
|
+
/** @returns {Promise<number>} Bitmask of FAULT_* constants, 0 when none. */
|
|
269
|
+
async readFault() {
|
|
270
|
+
const s = await this._readReg(_REG_DRV_STATUS);
|
|
271
|
+
let f = 0;
|
|
272
|
+
if (s & 0x01) f |= TMC2209Minimal.FAULT_OVERTEMP_WARNING;
|
|
273
|
+
if (s & 0x02) f |= TMC2209Minimal.FAULT_OVERTEMP;
|
|
274
|
+
if (s & 0x0C) f |= TMC2209Minimal.FAULT_SHORT_GND;
|
|
275
|
+
if (s & 0x30) f |= TMC2209Minimal.FAULT_SHORT_SUPPLY;
|
|
276
|
+
if (s & 0xC0) f |= TMC2209Minimal.FAULT_OPEN_LOAD;
|
|
277
|
+
return f;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
TMC2209Minimal.FAULT_OVERTEMP_WARNING = 0x01;
|
|
282
|
+
TMC2209Minimal.FAULT_OVERTEMP = 0x02;
|
|
283
|
+
TMC2209Minimal.FAULT_SHORT_GND = 0x04;
|
|
284
|
+
TMC2209Minimal.FAULT_SHORT_SUPPLY = 0x08;
|
|
285
|
+
TMC2209Minimal.FAULT_OPEN_LOAD = 0x10;
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Complete TMC2209 driver. Extends TMC2209Minimal with raw
|
|
289
|
+
* register access, independent hold current, microstep and chopper
|
|
290
|
+
* configuration, StealthChop tuning, GPIO STEP/DIR pulsing and every status
|
|
291
|
+
* read-back.
|
|
292
|
+
*/
|
|
293
|
+
class TMC2209Full extends TMC2209Minimal {
|
|
294
|
+
/**
|
|
295
|
+
* @param {object} connection - UART connection to PDN_UART.
|
|
296
|
+
* @param {object} [options]
|
|
297
|
+
* @param {number} [options.rSenseOhm=0.11] - External sense resistor in ohm.
|
|
298
|
+
* @param {object|null} [options.enPin=null] - OutputPin wired to ENN.
|
|
299
|
+
* @param {boolean} [options.echo=true] - True when TX and RX share PDN_UART.
|
|
300
|
+
* @param {object|null} [options.stepPin=null] - OutputPin wired to STEP.
|
|
301
|
+
* @param {object|null} [options.dirPin=null] - OutputPin wired to DIR.
|
|
302
|
+
* @param {object|null} [options.diagPin=null] - InputPin wired to DIAG (stall / driver error).
|
|
303
|
+
* @param {number} [options.address=0] - UART node address 0-3.
|
|
304
|
+
* @param {boolean} [options.multiNode=false] - Several chips share the UART; init writes NODECONF.SENDDELAY = 2 first.
|
|
305
|
+
* @param {number} [options.fclkHz=12000000] - Chip clock in Hz.
|
|
306
|
+
*/
|
|
307
|
+
constructor(connection, options = {}) {
|
|
308
|
+
super(connection, options);
|
|
309
|
+
this._diagPin = options.diagPin ?? null;
|
|
310
|
+
this._stepPin = options.stepPin ?? null;
|
|
311
|
+
this._dirPin = options.dirPin ?? null;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** @param {number} reg - Readable register address. @returns {Promise<number>} Raw 32-bit value. @throws {RangeError} For write-only/reserved registers. */
|
|
315
|
+
async readRegister(reg) {
|
|
316
|
+
if (!_READABLE.has(reg)) throw new RangeError('register 0x' + reg.toString(16) + ' is not readable');
|
|
317
|
+
return this._readReg(reg);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** @param {number} reg - Writable register address. @param {number} value - 32-bit value. @returns {Promise<void>} @throws {RangeError} For read-only/reserved registers, OTP_PROG and FACTORY_CONF. */
|
|
321
|
+
async writeRegister(reg, value) {
|
|
322
|
+
if (!_WRITABLE.has(reg)) throw new RangeError('register 0x' + reg.toString(16) + ' is not writable');
|
|
323
|
+
await this._writeReg(reg, value);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** @param {number} rmsA - Hold current in A RMS. @returns {Promise<void>} */
|
|
327
|
+
async setHoldCurrent(rmsA) {
|
|
328
|
+
this._holdCs = this._currentToCs(rmsA);
|
|
329
|
+
await this._writeIholdIrun();
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** @param {number} iholdDelay - IHOLDDELAY 0-15. @param {number} tpowerdown - TPOWERDOWN 0-255. @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
333
|
+
async setCurrentDelay(iholdDelay, tpowerdown) {
|
|
334
|
+
if (!(iholdDelay >= 0 && iholdDelay <= 15 && tpowerdown >= 0 && tpowerdown <= 255)) {
|
|
335
|
+
throw new RangeError('ihold_delay 0-15 / tpowerdown 0-255');
|
|
336
|
+
}
|
|
337
|
+
this._iholdDelay = iholdDelay;
|
|
338
|
+
await this._writeIholdIrun();
|
|
339
|
+
await this._writeReg(_REG_TPOWERDOWN, tpowerdown);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** @param {number} n - 1, 2, 4, 8, 16, 32, 64, 128 or 256. @returns {Promise<void>} @throws {RangeError} Otherwise. */
|
|
343
|
+
async setMicrosteps(n) {
|
|
344
|
+
const table = { 256: 0, 128: 1, 64: 2, 32: 3, 16: 4, 8: 5, 4: 6, 2: 7, 1: 8 };
|
|
345
|
+
if (!(n in table)) throw new RangeError('microsteps must be a power of two, 1-256');
|
|
346
|
+
await this._modify(_REG_CHOPCONF, 0xF << 24, table[n] << 24);
|
|
347
|
+
this._microsteps = n;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** @param {boolean} enabled - CHOPCONF.intpol. @returns {Promise<void>} */
|
|
351
|
+
async setInterpolation(enabled) { await this._modify(_REG_CHOPCONF, 1 << 28, enabled ? (1 << 28) : 0); }
|
|
352
|
+
|
|
353
|
+
/** @param {boolean} spreadcycle - true = SpreadCycle, false = StealthChop. @returns {Promise<void>} */
|
|
354
|
+
async setChopperMode(spreadcycle) { await this._modify(_REG_GCONF, _GCONF_EN_SPREADCYCLE, spreadcycle ? _GCONF_EN_SPREADCYCLE : 0); }
|
|
355
|
+
|
|
356
|
+
/** @param {number} tpwmthrs - TPWMTHRS (20 bit); 0 disables the threshold. @returns {Promise<void>} */
|
|
357
|
+
async setStealthchopThreshold(tpwmthrs) { await this._writeReg(_REG_TPWMTHRS, tpwmthrs & 0xFFFFF); }
|
|
358
|
+
|
|
359
|
+
/** @param {number} toff @param {number} tbl @param {number} hstrt @param {number} hend - Raw HEND field 0-15. @returns {Promise<void>} */
|
|
360
|
+
async configureChopper(toff, tbl, hstrt, hend) {
|
|
361
|
+
const mask = 0xF | (3 << 15) | (7 << 4) | (0xF << 7);
|
|
362
|
+
const value = (toff & 0xF) | ((tbl & 3) << 15) | ((hstrt & 7) << 4) | ((hend & 0xF) << 7);
|
|
363
|
+
if (toff) this._toff = toff & 0xF;
|
|
364
|
+
await this._modify(_REG_CHOPCONF, mask, value);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Write the StealthChop fields of PWMCONF. @returns {Promise<void>} */
|
|
368
|
+
async configureStealthchop(autoscale, autograd, pwmOfs, pwmGrad, pwmFreq, pwmReg, pwmLim) {
|
|
369
|
+
const mask = (0xF << 28) | (0xF << 24) | (1 << 19) | (1 << 18) | (3 << 16) | (0xFF << 8) | 0xFF;
|
|
370
|
+
const value = ((pwmLim & 0xF) << 28) | ((pwmReg & 0xF) << 24) | (autograd ? (1 << 19) : 0) |
|
|
371
|
+
(autoscale ? (1 << 18) : 0) | ((pwmFreq & 3) << 16) | ((pwmGrad & 0xFF) << 8) | (pwmOfs & 0xFF);
|
|
372
|
+
await this._modify(_REG_PWMCONF, mask, value);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** @param {number} mode - PWMCONF.freewheel 0-3. @returns {Promise<void>} @throws {RangeError} Otherwise. */
|
|
376
|
+
async setFreewheel(mode) {
|
|
377
|
+
if (!(mode >= 0 && mode <= 3)) throw new RangeError('freewheel mode 0-3');
|
|
378
|
+
await this._modify(_REG_PWMCONF, 3 << 20, mode << 20);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** @param {boolean} highSensitivity - true for the low 0.18 V range (changes current scaling). @returns {Promise<void>} */
|
|
382
|
+
async setVsense(highSensitivity) {
|
|
383
|
+
await this._modify(_REG_CHOPCONF, 1 << 17, highSensitivity ? (1 << 17) : 0);
|
|
384
|
+
this._vsense = !!highSensitivity;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** @param {boolean} enabled - GCONF.internal_Rsense (setCurrent() then throws). @returns {Promise<void>} */
|
|
388
|
+
async setInternalRsense(enabled) {
|
|
389
|
+
await this._modify(_REG_GCONF, _GCONF_INTERNAL_RSENSE, enabled ? _GCONF_INTERNAL_RSENSE : 0);
|
|
390
|
+
this._internalRsense = !!enabled;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** @param {boolean} enabled - GCONF.I_scale_analog. @returns {Promise<void>} */
|
|
394
|
+
async setAnalogScaling(enabled) { await this._modify(_REG_GCONF, _GCONF_I_SCALE_ANALOG, enabled ? _GCONF_I_SCALE_ANALOG : 0); }
|
|
395
|
+
|
|
396
|
+
/** @param {boolean} inverted - GCONF.shaft. @returns {Promise<void>} */
|
|
397
|
+
async setDirectionInverted(inverted) { await this._modify(_REG_GCONF, _GCONF_SHAFT, inverted ? _GCONF_SHAFT : 0); }
|
|
398
|
+
|
|
399
|
+
/** @param {number} mode - 0 first microstep, 1 otpw, 2 step pulses. @returns {Promise<void>} @throws {RangeError} Otherwise. */
|
|
400
|
+
async setIndexOutput(mode) {
|
|
401
|
+
if (![0, 1, 2].includes(mode)) throw new RangeError('index mode 0-2');
|
|
402
|
+
const bits = [0, _GCONF_INDEX_OTPW, _GCONF_INDEX_STEP][mode];
|
|
403
|
+
await this._modify(_REG_GCONF, _GCONF_INDEX_OTPW | _GCONF_INDEX_STEP, bits);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** @param {boolean} shortLs - keep low-side short protection. @param {boolean} shortGnd - keep short-to-GND protection. @returns {Promise<void>} */
|
|
407
|
+
async setDriverProtection(shortLs, shortGnd) {
|
|
408
|
+
const value = (shortLs ? 0 : 2 ** 31) + (shortGnd ? 0 : (1 << 30));
|
|
409
|
+
await this._modify(_REG_CHOPCONF, (2 ** 31) + (1 << 30), value);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** @param {boolean} enabled - CHOPCONF.dedge. @returns {Promise<void>} */
|
|
413
|
+
async setDoubleEdge(enabled) { await this._modify(_REG_CHOPCONF, 1 << 29, enabled ? (1 << 29) : 0); }
|
|
414
|
+
|
|
415
|
+
/** @param {number} n - NODECONF.SENDDELAY 0-15. @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
416
|
+
async setSendDelay(n) {
|
|
417
|
+
if (!(n >= 0 && n <= 15)) throw new RangeError('send delay 0-15');
|
|
418
|
+
await this._writeReg(_REG_NODECONF, n << 8);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Set the StallGuard4 threshold (SGTHRS); a stall is signalled when SG_RESULT <= 2 * sgthrs.
|
|
423
|
+
* @param {number} sgthrs - 0-255, higher is more sensitive.
|
|
424
|
+
* @returns {Promise<void>}
|
|
425
|
+
* @throws {RangeError} If out of range.
|
|
426
|
+
*/
|
|
427
|
+
async setStallThreshold(sgthrs) {
|
|
428
|
+
if (!Number.isInteger(sgthrs) || sgthrs < 0 || sgthrs > 255) throw new RangeError('sgthrs 0-255');
|
|
429
|
+
await this._writeReg(_REG_SGTHRS, sgthrs);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Set the lower velocity limit for CoolStep and the DIAG stall output.
|
|
434
|
+
* @param {number} tcoolthrs - TCOOLTHRS (20 bit); 0 disables.
|
|
435
|
+
* @returns {Promise<void>}
|
|
436
|
+
* @throws {RangeError} If out of range.
|
|
437
|
+
*/
|
|
438
|
+
async setCoolstepThreshold(tcoolthrs) {
|
|
439
|
+
if (!Number.isInteger(tcoolthrs) || tcoolthrs < 0 || tcoolthrs > 0xFFFFF) throw new RangeError('tcoolthrs 0-1048575');
|
|
440
|
+
await this._writeReg(_REG_TCOOLTHRS, tcoolthrs);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Write COOLCONF.
|
|
445
|
+
* @param {number} semin - Lower threshold 0-15; 0 disables CoolStep.
|
|
446
|
+
* @param {number} semax - Upper threshold offset 0-15.
|
|
447
|
+
* @param {number} seup - Current up step width 0-3 (1, 2, 4, 8).
|
|
448
|
+
* @param {number} sedn - Current down step speed 0-3.
|
|
449
|
+
* @param {number} seimin - 0 for half IRUN minimum, 1 for quarter.
|
|
450
|
+
* @returns {Promise<void>}
|
|
451
|
+
* @throws {RangeError} If a field is out of range.
|
|
452
|
+
*/
|
|
453
|
+
async configureCoolstep(semin, semax, seup, sedn, seimin) {
|
|
454
|
+
if (!(semin >= 0 && semin <= 15 && semax >= 0 && semax <= 15 && seup >= 0 && seup <= 3 &&
|
|
455
|
+
sedn >= 0 && sedn <= 3 && (seimin === 0 || seimin === 1))) {
|
|
456
|
+
throw new RangeError('COOLCONF field out of range');
|
|
457
|
+
}
|
|
458
|
+
await this._writeReg(_REG_COOLCONF,
|
|
459
|
+
(semin | (seup << 5) | (semax << 8) | (sedn << 13) | (seimin << 15)) >>> 0);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/** @returns {Promise<number>} SG_RESULT 0-510 (higher means less load). */
|
|
463
|
+
async readStallguard() { return (await this._readReg(_REG_SG_RESULT)) & 0x3FF; }
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* @returns {boolean} DIAG pin level (stall or driver error).
|
|
467
|
+
* @throws {Error} If no diagPin was given.
|
|
468
|
+
*/
|
|
469
|
+
isStalled() {
|
|
470
|
+
if (!this._diagPin) throw new Error('isStalled() requires diagPin');
|
|
471
|
+
return !!this._diagPin.isHigh();
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Emit STEP pulses on the GPIO pin.
|
|
476
|
+
* @param {number} count - Pulses; negative reverses (needs dirPin).
|
|
477
|
+
* @param {number} delayS - High and low time of each pulse in seconds.
|
|
478
|
+
* @returns {Promise<void>}
|
|
479
|
+
*/
|
|
480
|
+
async step(count, delayS) {
|
|
481
|
+
if (!this._stepPin) throw new Error('step() requires stepPin');
|
|
482
|
+
if (count < 0 && !this._dirPin) throw new RangeError('negative count requires dirPin');
|
|
483
|
+
if (this._dirPin) await this._dirPin.set(count >= 0);
|
|
484
|
+
for (let i = 0; i < Math.abs(count); i++) {
|
|
485
|
+
await this._stepPin.set(true);
|
|
486
|
+
await sleep(delayS * 1000);
|
|
487
|
+
await this._stepPin.set(false);
|
|
488
|
+
await sleep(delayS * 1000);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** @returns {Promise<number>} MSCNT 0-1023. */
|
|
493
|
+
async microstepPosition() { return (await this._readReg(_REG_MSCNT)) & 0x3FF; }
|
|
494
|
+
|
|
495
|
+
/** @returns {Promise<number>} DRV_STATUS.CS_ACTUAL 0-31. */
|
|
496
|
+
async readCurrentScale() { return ((await this._readReg(_REG_DRV_STATUS)) >>> 16) & 0x1F; }
|
|
497
|
+
|
|
498
|
+
/** @returns {Promise<[number, number]>} Signed MSCURACT values (bits 8:0, bits 24:16). */
|
|
499
|
+
async readCoilCurrents() {
|
|
500
|
+
const raw = await this._readReg(_REG_MSCURACT);
|
|
501
|
+
return [toSigned(raw, 9), toSigned(raw >>> 16, 9)];
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/** @returns {Promise<number>} TSTEP in clock periods (20 bit). */
|
|
505
|
+
async readTstep() { return (await this._readReg(_REG_TSTEP)) & 0xFFFFF; }
|
|
506
|
+
|
|
507
|
+
/** @returns {Promise<number>} t120..t157 as bits 0..3. */
|
|
508
|
+
async readTemperatureFlags() { return ((await this._readReg(_REG_DRV_STATUS)) >>> 8) & 0xF; }
|
|
509
|
+
|
|
510
|
+
/** @returns {Promise<object>} Decoded DRV_STATUS. */
|
|
511
|
+
async readStatus() {
|
|
512
|
+
const s = await this._readReg(_REG_DRV_STATUS);
|
|
513
|
+
return {
|
|
514
|
+
stst: ((s >>> 31) & 1) === 1,
|
|
515
|
+
stealth: ((s >>> 30) & 1) === 1,
|
|
516
|
+
cs_actual: (s >>> 16) & 0x1F,
|
|
517
|
+
otpw: (s & 0x01) !== 0,
|
|
518
|
+
ot: (s & 0x02) !== 0,
|
|
519
|
+
short_gnd: (s & 0x0C) !== 0,
|
|
520
|
+
short_supply: (s & 0x30) !== 0,
|
|
521
|
+
open_load: (s & 0xC0) !== 0,
|
|
522
|
+
t120: (s & (1 << 8)) !== 0,
|
|
523
|
+
t143: (s & (1 << 9)) !== 0,
|
|
524
|
+
t150: (s & (1 << 10)) !== 0,
|
|
525
|
+
t157: (s & (1 << 11)) !== 0,
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/** @param {boolean} [clear=false] - Write the flags back (write-1-to-clear). @returns {Promise<object>} {reset, drv_err, uv_cp}. */
|
|
530
|
+
async readGlobalStatus(clear = false) {
|
|
531
|
+
const g = await this._readReg(_REG_GSTAT);
|
|
532
|
+
if (clear) await this._writeReg(_REG_GSTAT, 0x07);
|
|
533
|
+
return { reset: (g & 1) !== 0, drv_err: (g & 2) !== 0, uv_cp: (g & 4) !== 0 };
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/** @returns {Promise<number>} IFCNT write counter 0-255. */
|
|
537
|
+
async readIfcnt() { return (await this._readReg(_REG_IFCNT)) & 0xFF; }
|
|
538
|
+
|
|
539
|
+
/** @returns {Promise<object>} {raw, enn, ms1, ms2, diag, pdn_uart, step, spread, dir, version}. */
|
|
540
|
+
async readIoin() {
|
|
541
|
+
const raw = await this._readReg(_REG_IOIN);
|
|
542
|
+
return {
|
|
543
|
+
raw, enn: (raw & 1) !== 0, ms1: (raw & 4) !== 0, ms2: (raw & 8) !== 0,
|
|
544
|
+
diag: (raw & 0x10) !== 0, pdn_uart: (raw & 0x40) !== 0, step: (raw & 0x80) !== 0,
|
|
545
|
+
spread: (raw & 0x100) !== 0, dir: (raw & 0x200) !== 0, version: (raw >>> 24) & 0xFF,
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/** @returns {Promise<number>} OTP_READ 24-bit word. */
|
|
550
|
+
async readOtp() { return (await this._readReg(_REG_OTP_READ)) & 0xFFFFFF; }
|
|
551
|
+
|
|
552
|
+
/** @returns {Promise<object>} {pwm_scale_sum, pwm_scale_auto, pwm_ofs_auto, pwm_grad_auto}. */
|
|
553
|
+
async readPwmAuto() {
|
|
554
|
+
const scale = await this._readReg(_REG_PWM_SCALE);
|
|
555
|
+
const auto = await this._readReg(_REG_PWM_AUTO);
|
|
556
|
+
return {
|
|
557
|
+
pwm_scale_sum: scale & 0xFF,
|
|
558
|
+
pwm_scale_auto: toSigned(scale >>> 16, 9),
|
|
559
|
+
pwm_ofs_auto: auto & 0xFF,
|
|
560
|
+
pwm_grad_auto: (auto >>> 16) & 0xFF,
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
module.exports = { TMC2209Minimal, TMC2209Full, crc8 };
|