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,900 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const R = {
|
|
4
|
+
GCONF: 0x00, GSTAT: 0x01, IFCNT: 0x02, SLAVECONF: 0x03, IOIN: 0x04, OUTPUT: 0x04,
|
|
5
|
+
X_COMPARE: 0x05, OTP_READ: 0x07, SHORT_CONF: 0x09, DRV_CONF: 0x0A, GLOBALSCALER: 0x0B,
|
|
6
|
+
OFFSET_READ: 0x0C, IHOLD_IRUN: 0x10, TPOWERDOWN: 0x11, TSTEP: 0x12, TPWMTHRS: 0x13,
|
|
7
|
+
TCOOLTHRS: 0x14, THIGH: 0x15, RAMPMODE: 0x20, XACTUAL: 0x21, VACTUAL: 0x22, VSTART: 0x23,
|
|
8
|
+
A1: 0x24, V1: 0x25, AMAX: 0x26, VMAX: 0x27, DMAX: 0x28, D1: 0x2A, VSTOP: 0x2B,
|
|
9
|
+
TZEROWAIT: 0x2C, XTARGET: 0x2D, VDCMIN: 0x33, SW_MODE: 0x34, RAMP_STAT: 0x35, XLATCH: 0x36,
|
|
10
|
+
ENCMODE: 0x38, X_ENC: 0x39, ENC_CONST: 0x3A, ENC_STATUS: 0x3B, ENC_LATCH: 0x3C,
|
|
11
|
+
ENC_DEVIATION: 0x3D, MSLUT0: 0x60, MSLUTSEL: 0x68, MSLUTSTART: 0x69, MSCNT: 0x6A,
|
|
12
|
+
MSCURACT: 0x6B, CHOPCONF: 0x6C, COOLCONF: 0x6D, DCCTRL: 0x6E, DRV_STATUS: 0x6F,
|
|
13
|
+
PWMCONF: 0x70, PWM_SCALE: 0x71, PWM_AUTO: 0x72, LOST_STEPS: 0x73,
|
|
14
|
+
};
|
|
15
|
+
const READABLE = new Set([0x00, 0x01, 0x02, 0x04, 0x07, 0x0C, 0x12, 0x20, 0x21, 0x22, 0x2D, 0x34,
|
|
16
|
+
0x35, 0x36, 0x38, 0x39, 0x3B, 0x3C, 0x6A, 0x6B, 0x6C, 0x6F, 0x71, 0x72, 0x73]);
|
|
17
|
+
const WRITABLE = new Set([0x00, 0x01, 0x03, 0x04, 0x05, 0x09, 0x0A, 0x0B, 0x10, 0x11, 0x13, 0x14,
|
|
18
|
+
0x15, 0x20, 0x21, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x2A, 0x2B, 0x2C, 0x2D, 0x33, 0x34,
|
|
19
|
+
0x35, 0x38, 0x39, 0x3A, 0x3B, 0x3D, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
|
|
20
|
+
0x69, 0x6C, 0x6D, 0x6E, 0x70]);
|
|
21
|
+
|
|
22
|
+
const VERSION = 0x30;
|
|
23
|
+
const V_FS = 0.325;
|
|
24
|
+
const CHOPCONF_DEFAULT = 0x000100C3;
|
|
25
|
+
const GCONF_EN_PWM_MODE = 1 << 2;
|
|
26
|
+
const GCONF_SHAFT = 1 << 4;
|
|
27
|
+
const GCONF_SMALL_HYSTERESIS = 1 << 14;
|
|
28
|
+
const GCONF_STOP_ENABLE = 1 << 15;
|
|
29
|
+
const GCONF_DIRECT_MODE = 1 << 16;
|
|
30
|
+
const SW_SG_STOP = 1 << 10;
|
|
31
|
+
const SW_SOFTSTOP = 1 << 11;
|
|
32
|
+
|
|
33
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
34
|
+
const signed = (v, bits) => {
|
|
35
|
+
const x = v & ((1 << bits) - 1);
|
|
36
|
+
return (x & (1 << (bits - 1))) ? x - (1 << bits) : x;
|
|
37
|
+
};
|
|
38
|
+
const s32 = (v) => v | 0;
|
|
39
|
+
|
|
40
|
+
/** CRC-8 (polynomial 0x07, init 0, LSB first) over a datagram body. */
|
|
41
|
+
function crc8(data) {
|
|
42
|
+
let crc = 0;
|
|
43
|
+
for (let byte of data) {
|
|
44
|
+
for (let i = 0; i < 8; i++) {
|
|
45
|
+
if (((crc >> 7) ^ (byte & 1)) !== 0) crc = ((crc << 1) ^ 0x07) & 0xFF;
|
|
46
|
+
else crc = (crc << 1) & 0xFF;
|
|
47
|
+
byte >>= 1;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return crc;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Trinamic TMC5160 stepper controller/driver over SPI or single-wire UART --
|
|
55
|
+
* minimal interface.
|
|
56
|
+
*
|
|
57
|
+
* Brings the chip up with the internal SixPoint ramp generator in positioning
|
|
58
|
+
* mode (256 microsteps, StealthChop below TPWMTHRS = 500, 0.5 A RMS run
|
|
59
|
+
* current, 200 full steps/s, 400 full steps/s^2) and moves the motor with
|
|
60
|
+
* register writes. Positions are in full steps, velocities in full steps per
|
|
61
|
+
* second, accelerations in full steps per second squared.
|
|
62
|
+
*
|
|
63
|
+
* The constructor performs no I/O; call `await init()` before use.
|
|
64
|
+
*/
|
|
65
|
+
class TMC5160Minimal {
|
|
66
|
+
/**
|
|
67
|
+
* @param {object} connection - SPI connection (Mode 3) or UART connection.
|
|
68
|
+
* @param {object} [options]
|
|
69
|
+
* @param {string} [options.busType='spi'] - 'spi' or 'uart'.
|
|
70
|
+
* @param {number} [options.rSenseOhm=0.075] - External sense resistor in ohm.
|
|
71
|
+
* @param {object|null} [options.enPin=null] - OutputPin wired to DRV_ENN (active low).
|
|
72
|
+
* @param {number} [options.address=0] - UART node address 0-253.
|
|
73
|
+
* @param {boolean} [options.echo=true] - UART: TX and RX share the wire.
|
|
74
|
+
* @param {boolean} [options.multiNode=false] - UART: write SENDDELAY = 2 first.
|
|
75
|
+
* @param {number} [options.fclkHz=12000000] - Chip clock in Hz.
|
|
76
|
+
* @throws {RangeError} If busType is unknown.
|
|
77
|
+
*/
|
|
78
|
+
constructor(connection, options = {}) {
|
|
79
|
+
const busType = options.busType ?? 'spi';
|
|
80
|
+
if (busType !== 'spi' && busType !== 'uart') throw new RangeError("busType must be 'spi' or 'uart'");
|
|
81
|
+
this._conn = connection;
|
|
82
|
+
this._uart = busType === 'uart';
|
|
83
|
+
this._rSense = options.rSenseOhm ?? 0.075;
|
|
84
|
+
this._enPin = options.enPin ?? null;
|
|
85
|
+
this._address = options.address ?? 0;
|
|
86
|
+
this._echo = options.echo ?? true;
|
|
87
|
+
this._multiNode = options.multiNode ?? false;
|
|
88
|
+
this._fclk = options.fclkHz ?? 12000000;
|
|
89
|
+
this._shadow = new Map();
|
|
90
|
+
this._usc = 256;
|
|
91
|
+
this._toff = 3;
|
|
92
|
+
this._rampmode = 0;
|
|
93
|
+
this._speed = 200;
|
|
94
|
+
this._speedReg = 0;
|
|
95
|
+
this._accel = 400;
|
|
96
|
+
this._senddelay = 0;
|
|
97
|
+
this._lastMove = [0, 200, 400];
|
|
98
|
+
this._writes = 0;
|
|
99
|
+
this._irun = 0;
|
|
100
|
+
this._ihold = 0;
|
|
101
|
+
this._iholdDelay = 6;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async _spiSecond() {
|
|
105
|
+
const r = Buffer.from(await this._conn.writeRead(Buffer.from([R.GCONF]), 4));
|
|
106
|
+
if (r.length !== 4) throw new Error('TMC5160: short read');
|
|
107
|
+
return r.readUInt32BE(0);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async _uartFlush() {
|
|
111
|
+
if (typeof this._conn.flush === 'function') await this._conn.flush();
|
|
112
|
+
await sleep(2);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async _uartWrite(reg, value) {
|
|
116
|
+
const body = Buffer.alloc(7);
|
|
117
|
+
body[0] = 0x05; body[1] = this._address; body[2] = (reg | 0x80) & 0xFF;
|
|
118
|
+
body.writeUInt32BE(value >>> 0, 3);
|
|
119
|
+
const dg = Buffer.concat([body, Buffer.from([crc8(body)])]);
|
|
120
|
+
await this._conn.write(dg);
|
|
121
|
+
if (this._echo && !Buffer.from(await this._conn.read(8)).equals(dg)) {
|
|
122
|
+
throw new Error('TMC5160: write echo mismatch');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async _uartRead(reg) {
|
|
127
|
+
const head = Buffer.from([0x05, this._address, reg & 0x7F]);
|
|
128
|
+
const req = Buffer.concat([head, Buffer.from([crc8(head)])]);
|
|
129
|
+
let reply;
|
|
130
|
+
if (this._echo) {
|
|
131
|
+
const raw = Buffer.from(await this._conn.writeRead(req, 12));
|
|
132
|
+
if (raw.length !== 12 || !raw.subarray(0, 4).equals(req)) throw new Error('TMC5160: read echo mismatch');
|
|
133
|
+
reply = raw.subarray(4);
|
|
134
|
+
} else {
|
|
135
|
+
reply = Buffer.from(await this._conn.writeRead(req, 8));
|
|
136
|
+
if (reply.length !== 8) throw new Error('TMC5160: short read');
|
|
137
|
+
}
|
|
138
|
+
if (reply[0] !== 0x05 || reply[1] !== 0xFF || reply[2] !== (reg & 0x7F)) throw new Error('TMC5160: malformed reply');
|
|
139
|
+
if (crc8(reply.subarray(0, 7)) !== reply[7]) throw new Error('TMC5160: reply CRC error');
|
|
140
|
+
return reply.readUInt32BE(3);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async _writeReg(reg, value, verify = false) {
|
|
144
|
+
value = value >>> 0;
|
|
145
|
+
if (this._uart) {
|
|
146
|
+
const before = verify ? (await this._readReg(R.IFCNT)) & 0xFF : 0;
|
|
147
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
148
|
+
try { await this._uartWrite(reg, value); break; } catch (e) {
|
|
149
|
+
if (attempt) throw e;
|
|
150
|
+
await this._uartFlush();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
this._writes++;
|
|
154
|
+
if (verify && (((await this._readReg(R.IFCNT)) & 0xFF) !== ((before + 1) & 0xFF))) {
|
|
155
|
+
throw new Error('TMC5160: IFCNT mismatch (write not acknowledged)');
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
const f = Buffer.alloc(5);
|
|
159
|
+
f[0] = (reg | 0x80) & 0xFF;
|
|
160
|
+
f.writeUInt32BE(value, 1);
|
|
161
|
+
await this._conn.write(f);
|
|
162
|
+
if (verify && (await this._spiSecond()) !== value) {
|
|
163
|
+
throw new Error('TMC5160: write mirror mismatch (reg 0x' + reg.toString(16) + ')');
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
this._shadow.set(reg, value);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async _readReg(reg) {
|
|
170
|
+
if (this._uart) {
|
|
171
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
172
|
+
try { return await this._uartRead(reg); } catch (e) {
|
|
173
|
+
if (attempt) throw e;
|
|
174
|
+
await this._uartFlush();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
await this._conn.writeRead(Buffer.from([reg & 0x7F]), 4);
|
|
179
|
+
return this._spiSecond();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async _modify(reg, mask, value) {
|
|
183
|
+
const cur = this._shadow.get(reg) ?? 0;
|
|
184
|
+
await this._writeReg(reg, ((cur & ~mask) | (value & mask)) >>> 0);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
_vReg(v) { return Math.round(v * this._usc * 16777216 / this._fclk); }
|
|
188
|
+
|
|
189
|
+
_aReg(a) { return Math.round(a * this._usc * 2199023255552 / (this._fclk * this._fclk)); }
|
|
190
|
+
|
|
191
|
+
_check(name, value, limit) {
|
|
192
|
+
if (!(value >= 0 && value <= limit)) throw new RangeError(name + ' out of range');
|
|
193
|
+
return value;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async _applyCurrent(rmsA, verify = false) {
|
|
197
|
+
const iMax = V_FS / (this._rSense * Math.SQRT2);
|
|
198
|
+
const r = rmsA / iMax;
|
|
199
|
+
if (r > 1 || r < 1 / 64) throw new RangeError('current outside ' + (iMax / 64).toFixed(3) + '..' + iMax.toFixed(3) + ' A RMS');
|
|
200
|
+
let irun, scaler;
|
|
201
|
+
if (r >= 0.5) { irun = 31; scaler = Math.round(256 * r); }
|
|
202
|
+
else { scaler = 128; irun = Math.max(0, Math.min(31, Math.round(64 * r) - 1)); }
|
|
203
|
+
await this._writeReg(R.GLOBALSCALER, scaler >= 256 ? 0 : scaler, verify);
|
|
204
|
+
this._irun = irun;
|
|
205
|
+
this._ihold = Math.floor(irun / 3);
|
|
206
|
+
this._iholdDelay = 6;
|
|
207
|
+
await this._writeIholdIrun(verify);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async _writeIholdIrun(verify = false) {
|
|
211
|
+
await this._writeReg(R.IHOLD_IRUN,
|
|
212
|
+
(this._ihold & 0x1F) | ((this._irun & 0x1F) << 8) | ((this._iholdDelay & 0xF) << 16), verify);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Probe the chip, apply the default configuration and verify the writes.
|
|
217
|
+
* @returns {Promise<void>}
|
|
218
|
+
* @throws {Error} If the chip does not answer, is strapped for STEP/DIR
|
|
219
|
+
* operation or a write is not acknowledged.
|
|
220
|
+
*/
|
|
221
|
+
async init() {
|
|
222
|
+
if (this._enPin) await this._enPin.set(true);
|
|
223
|
+
await sleep(10);
|
|
224
|
+
const v = !this._uart;
|
|
225
|
+
const base = this._uart ? (await this._readReg(R.IFCNT)) & 0xFF : 0;
|
|
226
|
+
const ioin = await this._readReg(R.IOIN);
|
|
227
|
+
if (((ioin >>> 24) & 0xFF) !== VERSION) throw new Error('TMC5160 not found (IOIN=0x' + ioin.toString(16) + ')');
|
|
228
|
+
if (ioin & (1 << 6)) throw new Error('TMC5160 is strapped for STEP/DIR operation (SD_MODE = 1)');
|
|
229
|
+
this._writes = 0;
|
|
230
|
+
if (this._uart && this._multiNode) {
|
|
231
|
+
this._senddelay = 2;
|
|
232
|
+
await this._writeReg(R.SLAVECONF, 2 << 8, true);
|
|
233
|
+
}
|
|
234
|
+
await this._readReg(R.GSTAT);
|
|
235
|
+
await this._writeReg(R.GSTAT, 0x07, v);
|
|
236
|
+
await this._writeReg(R.GLOBALSCALER, 0, v);
|
|
237
|
+
await this._applyCurrent(Math.min(0.5, V_FS / (this._rSense * Math.SQRT2)), v);
|
|
238
|
+
await this._writeReg(R.TPOWERDOWN, 10, v);
|
|
239
|
+
await this._writeReg(R.CHOPCONF, CHOPCONF_DEFAULT, v);
|
|
240
|
+
await this._writeReg(R.GCONF, GCONF_EN_PWM_MODE, v);
|
|
241
|
+
await this._writeReg(R.TPWMTHRS, 500, v);
|
|
242
|
+
await this._writeReg(R.RAMPMODE, 0, v);
|
|
243
|
+
await this._writeReg(R.VSTART, 0, v);
|
|
244
|
+
await this._writeReg(R.V1, 0, v);
|
|
245
|
+
await this._writeReg(R.VSTOP, 10, v);
|
|
246
|
+
await this._writeReg(R.TZEROWAIT, 0, v);
|
|
247
|
+
await this.setSpeed(200);
|
|
248
|
+
await this.setAcceleration(400);
|
|
249
|
+
if (this._uart && (((await this._readReg(R.IFCNT)) & 0xFF) !== ((base + this._writes) & 0xFF))) {
|
|
250
|
+
throw new Error('TMC5160: IFCNT mismatch (writes not acknowledged)');
|
|
251
|
+
}
|
|
252
|
+
if ((await this._readReg(R.GCONF)) !== this._shadow.get(R.GCONF) ||
|
|
253
|
+
(await this._readReg(R.CHOPCONF)) !== this._shadow.get(R.CHOPCONF)) {
|
|
254
|
+
throw new Error('TMC5160: GCONF/CHOPCONF readback mismatch');
|
|
255
|
+
}
|
|
256
|
+
this._rampmode = 0;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Switch the power stage on and wait 150 ms for StealthChop to settle. @returns {Promise<void>} */
|
|
260
|
+
async enable() {
|
|
261
|
+
if (this._enPin) await this._enPin.set(false);
|
|
262
|
+
else await this._writeReg(R.CHOPCONF, ((this._shadow.get(R.CHOPCONF) & ~0xF) | this._toff) >>> 0);
|
|
263
|
+
await sleep(150);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** Switch the power stage off; the motor outputs float. @returns {Promise<void>} */
|
|
267
|
+
async disable() {
|
|
268
|
+
if (this._enPin) { await this._enPin.set(true); return; }
|
|
269
|
+
const chop = this._shadow.get(R.CHOPCONF);
|
|
270
|
+
if (chop & 0xF) this._toff = chop & 0xF;
|
|
271
|
+
await this._writeReg(R.CHOPCONF, (chop & ~0xF) >>> 0);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Set the run current (hold current becomes one third): GLOBALSCALER and IHOLD_IRUN.
|
|
276
|
+
* @param {number} rmsA - Run current in A RMS.
|
|
277
|
+
* @returns {Promise<void>}
|
|
278
|
+
* @throws {RangeError} If above the sense-resistor maximum or below 1/64 of it.
|
|
279
|
+
*/
|
|
280
|
+
async setCurrent(rmsA) { await this._applyCurrent(rmsA); }
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Set VMAX, the target velocity of positioning moves and velocity mode.
|
|
284
|
+
* @param {number} fullStepsPerS - Speed in full steps per second (> 0).
|
|
285
|
+
* @returns {Promise<void>}
|
|
286
|
+
* @throws {RangeError} If not positive or out of range.
|
|
287
|
+
*/
|
|
288
|
+
async setSpeed(fullStepsPerS) {
|
|
289
|
+
if (!(fullStepsPerS > 0)) throw new RangeError('speed must be > 0');
|
|
290
|
+
const reg = this._check('VMAX', this._vReg(fullStepsPerS), (1 << 23) - 512);
|
|
291
|
+
this._speed = fullStepsPerS;
|
|
292
|
+
this._speedReg = reg;
|
|
293
|
+
if (this._rampmode === 0) await this._writeReg(R.VMAX, reg);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Set a single trapezoid: A1 = AMAX = DMAX = D1 (with V1 = 0).
|
|
298
|
+
* @param {number} fullStepsPerS2 - Acceleration in full steps per second squared (> 0).
|
|
299
|
+
* @returns {Promise<void>}
|
|
300
|
+
* @throws {RangeError} If not positive or out of range.
|
|
301
|
+
*/
|
|
302
|
+
async setAcceleration(fullStepsPerS2) {
|
|
303
|
+
if (!(fullStepsPerS2 > 0)) throw new RangeError('acceleration must be > 0');
|
|
304
|
+
const reg = this._check('acceleration', this._aReg(fullStepsPerS2), 0xFFFF);
|
|
305
|
+
if (reg < 1) throw new RangeError('acceleration too small');
|
|
306
|
+
this._accel = fullStepsPerS2;
|
|
307
|
+
for (const n of ['A1', 'AMAX', 'DMAX', 'D1']) await this._writeReg(R[n], reg);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Start a positioning move to an absolute position (non-blocking).
|
|
312
|
+
* @param {number} position - Target in full steps.
|
|
313
|
+
* @returns {Promise<void>}
|
|
314
|
+
*/
|
|
315
|
+
async moveTo(position) {
|
|
316
|
+
const target = Math.round(position * this._usc);
|
|
317
|
+
this._lastMove = [Math.abs(position - await this.position()), this._speed, this._accel];
|
|
318
|
+
await this._writeReg(R.VMAX, this._speedReg);
|
|
319
|
+
await this._writeReg(R.RAMPMODE, 0);
|
|
320
|
+
this._rampmode = 0;
|
|
321
|
+
await this._writeReg(R.XTARGET, target >>> 0);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Blocking relative move; returns when the target is reached.
|
|
326
|
+
* @param {number} fullSteps - Distance in full steps (sign = direction).
|
|
327
|
+
* @param {number} [fullStepsPerS] - Optional speed override (restored afterwards).
|
|
328
|
+
* @returns {Promise<void>}
|
|
329
|
+
* @throws {Error} On timeout.
|
|
330
|
+
*/
|
|
331
|
+
async move(fullSteps, fullStepsPerS) {
|
|
332
|
+
const saved = this._speed;
|
|
333
|
+
if (fullStepsPerS !== undefined) await this.setSpeed(fullStepsPerS);
|
|
334
|
+
try {
|
|
335
|
+
await this.moveTo((await this.position()) + fullSteps);
|
|
336
|
+
await this.waitUntilReached();
|
|
337
|
+
} finally {
|
|
338
|
+
if (fullStepsPerS !== undefined) await this.setSpeed(saved);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Velocity mode: ramp to a velocity (sign = direction); 0 ramps to standstill.
|
|
344
|
+
* @param {number} fullStepsPerS - Velocity in full steps per second.
|
|
345
|
+
* @returns {Promise<void>}
|
|
346
|
+
*/
|
|
347
|
+
async run(fullStepsPerS) {
|
|
348
|
+
const reg = this._check('VMAX', this._vReg(Math.abs(fullStepsPerS)), (1 << 23) - 512);
|
|
349
|
+
const mode = fullStepsPerS < 0 ? 2 : 1;
|
|
350
|
+
await this._writeReg(R.RAMPMODE, mode);
|
|
351
|
+
this._rampmode = mode;
|
|
352
|
+
await this._writeReg(R.VMAX, reg);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** Controlled stop: velocity mode with VMAX = 0. @returns {Promise<void>} */
|
|
356
|
+
async stop() {
|
|
357
|
+
await this._writeReg(R.RAMPMODE, 1);
|
|
358
|
+
this._rampmode = 1;
|
|
359
|
+
await this._writeReg(R.VMAX, 0);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** @returns {Promise<number>} XACTUAL in full steps. */
|
|
363
|
+
async position() { return s32(await this._readReg(R.XACTUAL)) / this._usc; }
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Define the current position (no motion): writes XACTUAL and XTARGET.
|
|
367
|
+
* @param {number} position - New position in full steps.
|
|
368
|
+
* @returns {Promise<void>}
|
|
369
|
+
*/
|
|
370
|
+
async setPosition(position) {
|
|
371
|
+
const v = Math.round(position * this._usc) >>> 0;
|
|
372
|
+
await this._writeReg(R.XACTUAL, v);
|
|
373
|
+
await this._writeReg(R.XTARGET, v);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** @returns {Promise<number>} VACTUAL in full steps per second, signed. */
|
|
377
|
+
async velocity() {
|
|
378
|
+
return signed(await this._readReg(R.VACTUAL), 24) * (this._fclk / 2 / 8388608) / this._usc;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** @returns {Promise<boolean>} RAMP_STAT.position_reached. */
|
|
382
|
+
async isPositionReached() { return ((await this._readReg(R.RAMP_STAT)) & (1 << 9)) !== 0; }
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Poll RAMP_STAT until position_reached (positioning) or vzero (velocity mode).
|
|
386
|
+
* @param {number} [timeoutS] - Timeout in seconds; estimated from the last move if omitted.
|
|
387
|
+
* @returns {Promise<boolean>} True once reached.
|
|
388
|
+
* @throws {Error} On timeout.
|
|
389
|
+
*/
|
|
390
|
+
async waitUntilReached(timeoutS) {
|
|
391
|
+
if (timeoutS === undefined) {
|
|
392
|
+
const [d, s, a] = this._lastMove;
|
|
393
|
+
timeoutS = 2 * d / s + 2 * s / a + 1;
|
|
394
|
+
}
|
|
395
|
+
const bit = this._rampmode === 0 ? (1 << 9) : (1 << 10);
|
|
396
|
+
await sleep(1);
|
|
397
|
+
let waited = 0;
|
|
398
|
+
for (;;) {
|
|
399
|
+
if ((await this._readReg(R.RAMP_STAT)) & bit) return true;
|
|
400
|
+
if (waited >= timeoutS) throw new Error('TMC5160: target not reached (timeout)');
|
|
401
|
+
await sleep(5);
|
|
402
|
+
waited += 0.005;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** @returns {Promise<boolean>} DRV_STATUS.stst. */
|
|
407
|
+
async isStandstill() { return ((await this._readReg(R.DRV_STATUS)) >>> 31) === 1; }
|
|
408
|
+
|
|
409
|
+
/** @returns {Promise<number>} Bitmask of FAULT_* constants, 0 when none. */
|
|
410
|
+
async readFault() {
|
|
411
|
+
const s = await this._readReg(R.DRV_STATUS);
|
|
412
|
+
let f = 0;
|
|
413
|
+
if (s & (1 << 26)) f |= TMC5160Minimal.FAULT_OVERTEMP_WARNING;
|
|
414
|
+
if (s & (1 << 25)) f |= TMC5160Minimal.FAULT_OVERTEMP;
|
|
415
|
+
if (s & (3 << 27)) f |= TMC5160Minimal.FAULT_SHORT_GND;
|
|
416
|
+
if (s & (3 << 12)) f |= TMC5160Minimal.FAULT_SHORT_SUPPLY;
|
|
417
|
+
if (s & (3 << 29)) f |= TMC5160Minimal.FAULT_OPEN_LOAD;
|
|
418
|
+
return f;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
TMC5160Minimal.FAULT_OVERTEMP_WARNING = 0x01;
|
|
422
|
+
TMC5160Minimal.FAULT_OVERTEMP = 0x02;
|
|
423
|
+
TMC5160Minimal.FAULT_SHORT_GND = 0x04;
|
|
424
|
+
TMC5160Minimal.FAULT_SHORT_SUPPLY = 0x08;
|
|
425
|
+
TMC5160Minimal.FAULT_OPEN_LOAD = 0x10;
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Complete TMC5160 driver: raw register access, the full SixPoint ramp,
|
|
429
|
+
* reference switches, chopper / StealthChop tuning, StallGuard2 / CoolStep,
|
|
430
|
+
* DcStep, the ABN encoder, gate-driver configuration and every status read-back.
|
|
431
|
+
*/
|
|
432
|
+
class TMC5160Full extends TMC5160Minimal {
|
|
433
|
+
/**
|
|
434
|
+
* @param {object} connection - SPI (Mode 3) or UART connection.
|
|
435
|
+
* @param {object} [options] - As TMC5160Minimal, plus:
|
|
436
|
+
* @param {object|null} [options.diag0Pin=null] - InputPin wired to DIAG0 (SPI only).
|
|
437
|
+
* @param {object|null} [options.diag1Pin=null] - InputPin wired to DIAG1 (SPI only).
|
|
438
|
+
* @throws {RangeError} If a DIAG pin is given on the UART bus.
|
|
439
|
+
*/
|
|
440
|
+
constructor(connection, options = {}) {
|
|
441
|
+
super(connection, options);
|
|
442
|
+
if (this._uart && (options.diag0Pin || options.diag1Pin)) throw new RangeError('DIAG pins are not available on the UART bus');
|
|
443
|
+
this._diagPins = [options.diag0Pin ?? null, options.diag1Pin ?? null];
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** @param {number} reg - Readable register. @returns {Promise<number>} Raw 32-bit value. @throws {RangeError} For write-only/reserved registers. */
|
|
447
|
+
async readRegister(reg) {
|
|
448
|
+
if (!READABLE.has(reg)) throw new RangeError('register 0x' + reg.toString(16) + ' is not readable');
|
|
449
|
+
return this._readReg(reg);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** @param {number} reg - Writable register. @param {number} value - 32-bit value. @param {boolean} [verify=false] - SPI mirror / UART IFCNT check. @returns {Promise<void>} @throws {RangeError} For read-only/reserved registers. */
|
|
453
|
+
async writeRegister(reg, value, verify = false) {
|
|
454
|
+
if (!WRITABLE.has(reg)) throw new RangeError('register 0x' + reg.toString(16) + ' is not writable');
|
|
455
|
+
await this._writeReg(reg, value, verify);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** @param {number} rmsA - Hold current in A RMS. @returns {Promise<void>} @throws {RangeError} If above the run current. */
|
|
459
|
+
async setHoldCurrent(rmsA) {
|
|
460
|
+
const gs = this._shadow.get(R.GLOBALSCALER) || 256;
|
|
461
|
+
const full = gs / 256 * V_FS / (this._rSense * Math.SQRT2);
|
|
462
|
+
const ihold = Math.round(rmsA / full * 32) - 1;
|
|
463
|
+
if (!(rmsA > 0) || ihold > this._irun) throw new RangeError('hold current above run current');
|
|
464
|
+
this._ihold = Math.max(0, ihold);
|
|
465
|
+
await this._writeIholdIrun();
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** @param {number} value - 0 or 32-256 (256 is written as 0). @returns {Promise<void>} @throws {RangeError} For 1-31 or above 256. */
|
|
469
|
+
async setGlobalScaler(value) {
|
|
470
|
+
if (!(value === 0 || (value >= 32 && value <= 256))) throw new RangeError('GLOBALSCALER must be 0 or 32..256');
|
|
471
|
+
await this._writeReg(R.GLOBALSCALER, value === 256 ? 0 : value);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** @param {number} irun 0-31 @param {number} ihold 0-31 @param {number} iholdDelay 0-15 @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
475
|
+
async setCurrentScale(irun, ihold, iholdDelay) {
|
|
476
|
+
if (!(irun >= 0 && irun <= 31 && ihold >= 0 && ihold <= 31 && iholdDelay >= 0 && iholdDelay <= 15)) throw new RangeError('IHOLD_IRUN field out of range');
|
|
477
|
+
this._irun = irun; this._ihold = ihold; this._iholdDelay = iholdDelay;
|
|
478
|
+
await this._writeIholdIrun();
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/** @param {number} tpowerdown - 0-255 (values below 2 break StealthChop auto-tuning). @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
482
|
+
async setPowerDownDelay(tpowerdown) {
|
|
483
|
+
if (!(tpowerdown >= 0 && tpowerdown <= 255)) throw new RangeError('tpowerdown 0-255');
|
|
484
|
+
await this._writeReg(R.TPOWERDOWN, tpowerdown);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Program the full SixPoint ramp in physical units (full steps/s, full steps/s^2).
|
|
489
|
+
* @param {number} vstart @param {number} a1 @param {number} v1 (0 disables A1/D1) @param {number} amax @param {number} vmax @param {number} dmax @param {number} d1 (> 0) @param {number} vstop (> 0)
|
|
490
|
+
* @returns {Promise<void>}
|
|
491
|
+
* @throws {RangeError} If out of range, d1 or vstop is 0, or vstop < vstart.
|
|
492
|
+
*/
|
|
493
|
+
async configureRamp(vstart, a1, v1, amax, vmax, dmax, d1, vstop) {
|
|
494
|
+
await this.setRampRaw(this._vReg(vstart), this._aReg(a1), this._vReg(v1), this._aReg(amax),
|
|
495
|
+
this._vReg(vmax), this._aReg(dmax), this._aReg(d1), this._vReg(vstop));
|
|
496
|
+
this._speed = vmax;
|
|
497
|
+
this._accel = amax;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Program the full SixPoint ramp in raw register units.
|
|
502
|
+
* @param {number} vstart @param {number} a1 @param {number} v1 @param {number} amax @param {number} vmax @param {number} dmax @param {number} d1 @param {number} vstop
|
|
503
|
+
* @returns {Promise<void>}
|
|
504
|
+
* @throws {RangeError} If out of range, d1 or vstop is 0, or vstop < vstart.
|
|
505
|
+
*/
|
|
506
|
+
async setRampRaw(vstart, a1, v1, amax, vmax, dmax, d1, vstop) {
|
|
507
|
+
this._check('VSTART', vstart, (1 << 18) - 1);
|
|
508
|
+
this._check('A1', a1, 0xFFFF);
|
|
509
|
+
this._check('V1', v1, (1 << 20) - 1);
|
|
510
|
+
this._check('AMAX', amax, 0xFFFF);
|
|
511
|
+
this._check('VMAX', vmax, (1 << 23) - 512);
|
|
512
|
+
this._check('DMAX', dmax, 0xFFFF);
|
|
513
|
+
this._check('D1', d1, 0xFFFF);
|
|
514
|
+
this._check('VSTOP', vstop, (1 << 18) - 1);
|
|
515
|
+
if (d1 < 1 || vstop < 1) throw new RangeError('D1 and VSTOP must be at least 1');
|
|
516
|
+
if (vstop < vstart) throw new RangeError('VSTOP must be >= VSTART');
|
|
517
|
+
const vals = { VSTART: vstart, A1: a1, V1: v1, AMAX: amax, VMAX: vmax, DMAX: dmax, D1: d1, VSTOP: vstop };
|
|
518
|
+
for (const [n, v] of Object.entries(vals)) await this._writeReg(R[n], v);
|
|
519
|
+
this._speedReg = vmax;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/** @param {number} seconds - TZEROWAIT (0 to about 2.8 s at 12 MHz). @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
523
|
+
async setZeroWait(seconds) {
|
|
524
|
+
await this._writeReg(R.TZEROWAIT, this._check('TZEROWAIT', Math.round(seconds * this._fclk / 512), 0xFFFF));
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** @param {number} mode - RAMPMODE 0 position, 1 velocity +, 2 velocity -, 3 hold. @returns {Promise<void>} @throws {RangeError} If not 0-3. */
|
|
528
|
+
async setRampMode(mode) {
|
|
529
|
+
if (!(mode >= 0 && mode <= 3)) throw new RangeError('mode 0-3');
|
|
530
|
+
await this._writeReg(R.RAMPMODE, mode);
|
|
531
|
+
this._rampmode = mode;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** Hold mode: keep the present velocity until a stop event. @returns {Promise<void>} */
|
|
535
|
+
async hold() { await this.setRampMode(3); }
|
|
536
|
+
|
|
537
|
+
/** @param {number} xtarget - Signed 32-bit XTARGET in microsteps. @returns {Promise<void>} */
|
|
538
|
+
async setTargetRaw(xtarget) { await this._writeReg(R.XTARGET, xtarget >>> 0); }
|
|
539
|
+
|
|
540
|
+
/** @returns {Promise<object>} Decoded RAMP_STAT flags. */
|
|
541
|
+
async readRampStatus() {
|
|
542
|
+
const s = await this._readReg(R.RAMP_STAT);
|
|
543
|
+
const names = ['status_stop_l', 'status_stop_r', 'status_latch_l', 'status_latch_r', 'event_stop_l',
|
|
544
|
+
'event_stop_r', 'event_stop_sg', 'event_pos_reached', 'velocity_reached', 'position_reached',
|
|
545
|
+
'vzero', 't_zerowait_active', 'second_move', 'status_sg'];
|
|
546
|
+
const out = {};
|
|
547
|
+
names.forEach((n, i) => { out[n] = !!(s & (1 << i)); });
|
|
548
|
+
return out;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/** Write 1 to the clear-on-write bits of RAMP_STAT. @returns {Promise<void>} */
|
|
552
|
+
async clearRampEvents() { await this._writeReg(R.RAMP_STAT, (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) | (1 << 12)); }
|
|
553
|
+
|
|
554
|
+
/** @param {number} position - X_COMPARE in full steps (DIAG1 high while XACTUAL equals it). @returns {Promise<void>} */
|
|
555
|
+
async setPositionCompare(position) { await this._writeReg(R.X_COMPARE, Math.round(position * this._usc) >>> 0); }
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Write SW_MODE bits 0-4 and 11 (latch and sg_stop bits are kept).
|
|
559
|
+
* @param {object} [o] - stopLeft, stopRight, leftActiveLow, rightActiveLow, swap, softStop (booleans).
|
|
560
|
+
* @returns {Promise<void>}
|
|
561
|
+
* @throws {RangeError} If soft stop is requested while sg_stop is enabled.
|
|
562
|
+
*/
|
|
563
|
+
async configureSwitches(o = {}) {
|
|
564
|
+
const sw = this._shadow.get(R.SW_MODE) ?? 0;
|
|
565
|
+
if (o.softStop && (sw & SW_SG_STOP)) throw new RangeError('soft stop cannot be combined with sg_stop');
|
|
566
|
+
const v = (o.stopLeft ? 1 : 0) | (o.stopRight ? 2 : 0) | (o.leftActiveLow ? 4 : 0) |
|
|
567
|
+
(o.rightActiveLow ? 8 : 0) | (o.swap ? 16 : 0) | (o.softStop ? SW_SOFTSTOP : 0);
|
|
568
|
+
await this._modify(R.SW_MODE, 0x1F | SW_SOFTSTOP, v);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/** @param {object} [o] - leftActive, leftInactive, rightActive, rightInactive, latchEncoder (booleans). @returns {Promise<void>} */
|
|
572
|
+
async configureLatch(o = {}) {
|
|
573
|
+
const v = (o.leftActive ? 1 << 5 : 0) | (o.leftInactive ? 1 << 6 : 0) | (o.rightActive ? 1 << 7 : 0) |
|
|
574
|
+
(o.rightInactive ? 1 << 8 : 0) | (o.latchEncoder ? 1 << 9 : 0);
|
|
575
|
+
await this._modify(R.SW_MODE, 0x1F << 5, v);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/** @returns {Promise<number>} XLATCH in full steps. */
|
|
579
|
+
async readLatchedPosition() { return s32(await this._readReg(R.XLATCH)) / this._usc; }
|
|
580
|
+
|
|
581
|
+
/** @returns {Promise<boolean[]>} [status_stop_l, status_stop_r]. */
|
|
582
|
+
async readSwitches() {
|
|
583
|
+
const s = await this._readReg(R.RAMP_STAT);
|
|
584
|
+
return [!!(s & 1), !!(s & 2)];
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/** @param {number} n - 1, 2, 4, ... 256 (the ramp registers are not rewritten). @returns {Promise<void>} @throws {RangeError} For any other value. */
|
|
588
|
+
async setMicrosteps(n) {
|
|
589
|
+
if (![1, 2, 4, 8, 16, 32, 64, 128, 256].includes(n)) throw new RangeError('microsteps must be a power of two, 1-256');
|
|
590
|
+
await this._modify(R.CHOPCONF, 0xF << 24, (8 - Math.log2(n)) << 24);
|
|
591
|
+
this._usc = n;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/** @param {boolean} enabled - CHOPCONF.intpol. @returns {Promise<void>} */
|
|
595
|
+
async setInterpolation(enabled) { await this._modify(R.CHOPCONF, 1 << 28, enabled ? 1 << 28 : 0); }
|
|
596
|
+
|
|
597
|
+
/** @param {boolean} spreadcycle - True for SpreadCycle only (switch at standstill). @returns {Promise<void>} */
|
|
598
|
+
async setChopperMode(spreadcycle) { await this._modify(R.GCONF, GCONF_EN_PWM_MODE, spreadcycle ? 0 : GCONF_EN_PWM_MODE); }
|
|
599
|
+
|
|
600
|
+
async _threshold(name, v) {
|
|
601
|
+
if (!(v >= 0 && v <= 0xFFFFF)) throw new RangeError(name + ' 0-1048575');
|
|
602
|
+
await this._writeReg(R[name], v);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** @param {number} tpwmthrs - TPWMTHRS (20 bit), 0 disables. @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
606
|
+
async setStealthchopThreshold(tpwmthrs) { await this._threshold('TPWMTHRS', tpwmthrs); }
|
|
607
|
+
|
|
608
|
+
/** @param {number} thigh - THIGH (20 bit). @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
609
|
+
async setHighVelocityThreshold(thigh) { await this._threshold('THIGH', thigh); }
|
|
610
|
+
|
|
611
|
+
/** SpreadCycle (chm = 0) fields. @param {number} toff 0-15 @param {number} tbl 0-3 @param {number} hstrt 0-7 @param {number} hend raw 0-15 @param {number} [tpfd=0] 0-15 @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
612
|
+
async configureChopper(toff, tbl, hstrt, hend, tpfd = 0) {
|
|
613
|
+
if (!(toff >= 0 && toff <= 15 && tbl >= 0 && tbl <= 3 && hstrt >= 0 && hstrt <= 7 && hend >= 0 && hend <= 15 && tpfd >= 0 && tpfd <= 15)) throw new RangeError('chopper field out of range');
|
|
614
|
+
if (toff) this._toff = toff;
|
|
615
|
+
const mask = 0xF | (3 << 15) | (7 << 4) | (0xF << 7) | (0xF << 20) | (1 << 14) | (1 << 11);
|
|
616
|
+
await this._modify(R.CHOPCONF, mask, toff | (tbl << 15) | (hstrt << 4) | (hend << 7) | (tpfd << 20));
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/** Constant-off-time (chm = 1) fields. @param {number} toff @param {number} tbl @param {number} tfd 0-15 @param {number} offset -3..12 @param {boolean} disfdcc @param {number} [tpfd=0] @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
620
|
+
async configureConstantOffTime(toff, tbl, tfd, offset, disfdcc, tpfd = 0) {
|
|
621
|
+
if (!(toff >= 0 && toff <= 15 && tbl >= 0 && tbl <= 3 && tfd >= 0 && tfd <= 15 && offset >= -3 && offset <= 12 && tpfd >= 0 && tpfd <= 15)) throw new RangeError('chopper field out of range');
|
|
622
|
+
if (toff) this._toff = toff;
|
|
623
|
+
const mask = 0xF | (3 << 15) | (7 << 4) | (0xF << 7) | (0xF << 20) | (1 << 14) | (1 << 12) | (1 << 11);
|
|
624
|
+
const v = toff | (tbl << 15) | ((tfd & 7) << 4) | ((offset + 3) << 7) | (tpfd << 20) | (1 << 14) |
|
|
625
|
+
(disfdcc ? 1 << 12 : 0) | (((tfd >> 3) & 1) << 11);
|
|
626
|
+
await this._modify(R.CHOPCONF, mask, v);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/** @param {boolean} vhighchm @param {boolean} vhighfs @returns {Promise<void>} */
|
|
630
|
+
async configureHighVelocity(vhighchm, vhighfs) {
|
|
631
|
+
await this._modify(R.CHOPCONF, (1 << 19) | (1 << 18), (vhighchm ? 1 << 19 : 0) | (vhighfs ? 1 << 18 : 0));
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/** PWMCONF fields (freewheel kept). @param {boolean} autoscale @param {boolean} autograd @param {number} pwmOfs 0-255 @param {number} pwmGrad 0-255 @param {number} pwmFreq 0-3 @param {number} pwmReg 1-15 @param {number} pwmLim 0-15 @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
635
|
+
async configureStealthchop(autoscale, autograd, pwmOfs, pwmGrad, pwmFreq, pwmReg, pwmLim) {
|
|
636
|
+
if (!(pwmOfs >= 0 && pwmOfs <= 255 && pwmGrad >= 0 && pwmGrad <= 255 && pwmFreq >= 0 && pwmFreq <= 3 && pwmReg >= 1 && pwmReg <= 15 && pwmLim >= 0 && pwmLim <= 15)) throw new RangeError('stealthchop field out of range');
|
|
637
|
+
if (!this._shadow.has(R.PWMCONF)) this._shadow.set(R.PWMCONF, 0xC40C001E);
|
|
638
|
+
const mask = (0xF << 28) | (0xF << 24) | (1 << 19) | (1 << 18) | (3 << 16) | (0xFF << 8) | 0xFF;
|
|
639
|
+
const v = (pwmLim << 28) | (pwmReg << 24) | (autograd ? 1 << 19 : 0) | (autoscale ? 1 << 18 : 0) |
|
|
640
|
+
(pwmFreq << 16) | (pwmGrad << 8) | pwmOfs;
|
|
641
|
+
await this._modify(R.PWMCONF, mask, v);
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** @param {number} mode - PWMCONF.freewheel 0-3. @returns {Promise<void>} @throws {RangeError} If not 0-3. */
|
|
645
|
+
async setFreewheel(mode) {
|
|
646
|
+
if (!(mode >= 0 && mode <= 3)) throw new RangeError('freewheel mode 0-3');
|
|
647
|
+
if (!this._shadow.has(R.PWMCONF)) this._shadow.set(R.PWMCONF, 0xC40C001E);
|
|
648
|
+
await this._modify(R.PWMCONF, 3 << 20, mode << 20);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/** @param {boolean} shortGnd - Keep short-to-GND protection. @param {boolean} shortSupply - Keep short-to-supply protection. @returns {Promise<void>} */
|
|
652
|
+
async setShortProtection(shortGnd, shortSupply) {
|
|
653
|
+
await this._modify(R.CHOPCONF, (1 << 30) | (1 << 31), (shortGnd ? 0 : 1 << 30) | (shortSupply ? 0 : 1 << 31));
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/** @param {boolean} enabled - CHOPCONF.dedge (external STEP/DIR only). @returns {Promise<void>} */
|
|
657
|
+
async setDoubleEdge(enabled) { await this._modify(R.CHOPCONF, 1 << 29, enabled ? 1 << 29 : 0); }
|
|
658
|
+
|
|
659
|
+
/** @param {boolean} inverted - GCONF.shaft. @returns {Promise<void>} */
|
|
660
|
+
async setDirectionInverted(inverted) { await this._modify(R.GCONF, GCONF_SHAFT, inverted ? GCONF_SHAFT : 0); }
|
|
661
|
+
|
|
662
|
+
/** @param {boolean} enabled - GCONF.faststandstill. @returns {Promise<void>} */
|
|
663
|
+
async setFastStandstill(enabled) { await this._modify(R.GCONF, 1 << 1, enabled ? 1 << 1 : 0); }
|
|
664
|
+
|
|
665
|
+
/** @param {boolean} enabled - GCONF.recalibrate. @returns {Promise<void>} */
|
|
666
|
+
async setRecalibrate(enabled) { await this._modify(R.GCONF, 1, enabled ? 1 : 0); }
|
|
667
|
+
|
|
668
|
+
/** @param {boolean} enabled - GCONF.small_hysteresis. @returns {Promise<void>} */
|
|
669
|
+
async setSmallHysteresis(enabled) { await this._modify(R.GCONF, GCONF_SMALL_HYSTERESIS, enabled ? GCONF_SMALL_HYSTERESIS : 0); }
|
|
670
|
+
|
|
671
|
+
/** @param {boolean} enabled - GCONF.stop_enable. @returns {Promise<void>} */
|
|
672
|
+
async setEmergencyStop(enabled) { await this._modify(R.GCONF, GCONF_STOP_ENABLE, enabled ? GCONF_STOP_ENABLE : 0); }
|
|
673
|
+
|
|
674
|
+
/** GCONF bits 7, 8, 12, 13. @param {object} [o] - diag0StepOutput, diag1DirOutput, diag0Pushpull, diag1Pushpull. @returns {Promise<void>} */
|
|
675
|
+
async configureDiag(o = {}) {
|
|
676
|
+
const v = (o.diag0StepOutput ? 1 << 7 : 0) | (o.diag1DirOutput ? 1 << 8 : 0) |
|
|
677
|
+
(o.diag0Pushpull ? 1 << 12 : 0) | (o.diag1Pushpull ? 1 << 13 : 0);
|
|
678
|
+
await this._modify(R.GCONF, (1 << 7) | (1 << 8) | (1 << 12) | (1 << 13), v);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** @param {number} sgt - COOLCONF.sgt -64..63 (lower is more sensitive). @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
682
|
+
async setStallThreshold(sgt) {
|
|
683
|
+
if (!(sgt >= -64 && sgt <= 63)) throw new RangeError('sgt -64..63');
|
|
684
|
+
await this._modify(R.COOLCONF, 0x7F << 16, (sgt & 0x7F) << 16);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/** @param {boolean} enabled - COOLCONF.sfilt. @returns {Promise<void>} */
|
|
688
|
+
async setStallguardFilter(enabled) { await this._modify(R.COOLCONF, 1 << 24, enabled ? 1 << 24 : 0); }
|
|
689
|
+
|
|
690
|
+
/** @param {number} tcoolthrs - TCOOLTHRS (20 bit), 0 disables. @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
691
|
+
async setCoolstepThreshold(tcoolthrs) { await this._threshold('TCOOLTHRS', tcoolthrs); }
|
|
692
|
+
|
|
693
|
+
/** CoolStep fields (sgt / sfilt kept). @param {number} semin 0-15 @param {number} semax 0-15 @param {number} seup 0-3 @param {number} sedn 0-3 @param {number} seimin 0|1 @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
694
|
+
async configureCoolstep(semin, semax, seup, sedn, seimin) {
|
|
695
|
+
if (!(semin >= 0 && semin <= 15 && semax >= 0 && semax <= 15 && seup >= 0 && seup <= 3 && sedn >= 0 && sedn <= 3 && (seimin === 0 || seimin === 1))) throw new RangeError('COOLCONF field out of range');
|
|
696
|
+
await this._modify(R.COOLCONF, 0xF | (3 << 5) | (0xF << 8) | (3 << 13) | (1 << 15),
|
|
697
|
+
semin | (seup << 5) | (semax << 8) | (sedn << 13) | (seimin << 15));
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/** @param {boolean} enabled - SW_MODE.sg_stop (needs TCOOLTHRS; not during spin-up). @returns {Promise<void>} @throws {RangeError} With soft stop enabled. */
|
|
701
|
+
async enableStallStop(enabled) {
|
|
702
|
+
if (enabled && ((this._shadow.get(R.SW_MODE) ?? 0) & SW_SOFTSTOP)) throw new RangeError('sg_stop cannot be combined with soft stop');
|
|
703
|
+
await this._modify(R.SW_MODE, SW_SG_STOP, enabled ? SW_SG_STOP : 0);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/** @returns {Promise<number>} SG_RESULT 0-1023. */
|
|
707
|
+
async readStallguard() { return (await this._readReg(R.DRV_STATUS)) & 0x3FF; }
|
|
708
|
+
|
|
709
|
+
/** @returns {Promise<boolean>} DRV_STATUS.StallGuard. */
|
|
710
|
+
async isStalled() { return (((await this._readReg(R.DRV_STATUS)) >>> 24) & 1) === 1; }
|
|
711
|
+
|
|
712
|
+
/** @param {number} n - 0 for DIAG0, 1 for DIAG1. @returns {boolean} Pin level. @throws {RangeError} If n is not 0/1. @throws {Error} If not wired (or UART). */
|
|
713
|
+
readDiag(n) {
|
|
714
|
+
if (n !== 0 && n !== 1) throw new RangeError('n must be 0 or 1');
|
|
715
|
+
const pin = this._diagPins[n];
|
|
716
|
+
if (!pin) throw new Error('readDiag() requires diag' + n + 'Pin on the SPI bus');
|
|
717
|
+
return !!pin.isHigh();
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/** @param {number} vdcmin 0..2^23-1 @param {number} dcTime 0-1023 @param {number} dcSg 0-255 @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
721
|
+
async setDcstep(vdcmin, dcTime, dcSg) {
|
|
722
|
+
if (!(vdcmin >= 0 && vdcmin <= 0x7FFFFF && dcTime >= 0 && dcTime <= 1023 && dcSg >= 0 && dcSg <= 255)) throw new RangeError('DcStep field out of range');
|
|
723
|
+
await this._writeReg(R.VDCMIN, vdcmin);
|
|
724
|
+
await this._writeReg(R.DCCTRL, (dcTime | (dcSg << 16)) >>> 0);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/** @param {boolean} enabled - GCONF.direct_mode (bypasses the ramp generator). @returns {Promise<void>} */
|
|
728
|
+
async setDirectMode(enabled) {
|
|
729
|
+
await this._modify(R.GCONF, GCONF_DIRECT_MODE, enabled ? GCONF_DIRECT_MODE : 0);
|
|
730
|
+
if (!enabled) await this._writeReg(R.XTARGET, this._shadow.get(R.XTARGET) ?? 0);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/** XDIRECT (shares the XTARGET address). @param {number} coilA -255..255 @param {number} coilB -255..255 @returns {Promise<void>} @throws {Error} If direct mode is off. @throws {RangeError} If out of range. */
|
|
734
|
+
async setCoilCurrents(coilA, coilB) {
|
|
735
|
+
if (!((this._shadow.get(R.GCONF) ?? 0) & GCONF_DIRECT_MODE)) throw new Error('setCoilCurrents() requires direct mode');
|
|
736
|
+
if (!(coilA >= -255 && coilA <= 255 && coilB >= -255 && coilB <= 255)) throw new RangeError('coil currents -255..255');
|
|
737
|
+
await this._writeReg(R.XTARGET, ((coilA & 0x1FF) | ((coilB & 0x1FF) << 16)) >>> 0);
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
/** Program the microstep table (call at MSCNT = 0). @param {number[]} mslut - 8 words @param {number} mslutsel @param {number} mslutstart @returns {Promise<void>} @throws {RangeError} If mslut does not hold 8 words. */
|
|
741
|
+
async writeMicrostepTable(mslut, mslutsel, mslutstart) {
|
|
742
|
+
if (mslut.length !== 8) throw new RangeError('mslut needs 8 words');
|
|
743
|
+
for (let i = 0; i < 8; i++) await this._writeReg(R.MSLUT0 + i, mslut[i]);
|
|
744
|
+
await this._writeReg(R.MSLUTSEL, mslutsel);
|
|
745
|
+
await this._writeReg(R.MSLUTSTART, mslutstart);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/** DRV_CONF -- board-specific, wrong values can destroy the MOSFETs. @param {number} bbmtime 0-24 @param {number} bbmclks 0-15 @param {number} otselect 0-3 @param {number} drvstrength 0-3 @param {number} filtIsense 0-3 @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
749
|
+
async configureGateDriver(bbmtime, bbmclks, otselect, drvstrength, filtIsense) {
|
|
750
|
+
if (!(bbmtime >= 0 && bbmtime <= 24 && bbmclks >= 0 && bbmclks <= 15 && otselect >= 0 && otselect <= 3 && drvstrength >= 0 && drvstrength <= 3 && filtIsense >= 0 && filtIsense <= 3)) throw new RangeError('DRV_CONF field out of range');
|
|
751
|
+
await this._writeReg(R.DRV_CONF, bbmtime | (bbmclks << 8) | (otselect << 16) | (drvstrength << 18) | (filtIsense << 20));
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/** SHORT_CONF -- board-specific. @param {number} s2vsLevel 4-15 @param {number} s2gLevel 2-15 @param {number} shortfilter 0-3 @param {number} shortdelay 0|1 @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
755
|
+
async configureShortDetection(s2vsLevel, s2gLevel, shortfilter, shortdelay) {
|
|
756
|
+
if (!(s2vsLevel >= 4 && s2vsLevel <= 15 && s2gLevel >= 2 && s2gLevel <= 15 && shortfilter >= 0 && shortfilter <= 3 && (shortdelay === 0 || shortdelay === 1))) throw new RangeError('SHORT_CONF field out of range');
|
|
757
|
+
await this._writeReg(R.SHORT_CONF, s2vsLevel | (s2gLevel << 8) | (shortfilter << 16) | (shortdelay << 18));
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/** ENCMODE (all booleans, in register bit order). @returns {Promise<void>} */
|
|
761
|
+
async configureEncoder(polA, polB, polN, ignoreAb, clrCont, clrOnce, posEdge, negEdge, clrEncX, latchXAct, decimal) {
|
|
762
|
+
const flags = [polA, polB, polN, ignoreAb, clrCont, clrOnce, posEdge, negEdge, clrEncX, latchXAct, decimal];
|
|
763
|
+
let v = 0;
|
|
764
|
+
flags.forEach((f, i) => { if (f) v |= 1 << i; });
|
|
765
|
+
await this._writeReg(R.ENCMODE, v);
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/** @param {number} value - ENC_CONST: signed 16.16 (or integer + 4-digit decimal fraction when ENCMODE.enc_sel_decimal is set). @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
769
|
+
async setEncoderConstant(value) {
|
|
770
|
+
if (Math.abs(value) > 32767.9999) throw new RangeError('encoder constant out of range');
|
|
771
|
+
let raw;
|
|
772
|
+
if ((this._shadow.get(R.ENCMODE) ?? 0) & (1 << 10)) {
|
|
773
|
+
const whole = Math.floor(Math.abs(value));
|
|
774
|
+
raw = ((whole << 16) | Math.round((Math.abs(value) - whole) * 10000)) >>> 0;
|
|
775
|
+
if (value < 0) raw = (-raw) >>> 0;
|
|
776
|
+
} else {
|
|
777
|
+
raw = Math.round(value * 65536) >>> 0;
|
|
778
|
+
}
|
|
779
|
+
await this._writeReg(R.ENC_CONST, raw);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
/** @param {number} counts - X_ENC. @returns {Promise<void>} */
|
|
783
|
+
async setEncoderPosition(counts) { await this._writeReg(R.X_ENC, counts >>> 0); }
|
|
784
|
+
|
|
785
|
+
/** @returns {Promise<number>} X_ENC signed. */
|
|
786
|
+
async readEncoderPosition() { return s32(await this._readReg(R.X_ENC)); }
|
|
787
|
+
|
|
788
|
+
/** @returns {Promise<number>} ENC_LATCH signed. */
|
|
789
|
+
async readEncoderLatch() { return s32(await this._readReg(R.ENC_LATCH)); }
|
|
790
|
+
|
|
791
|
+
/** @param {number} steps - ENC_DEVIATION 0-2^20-1 (0 = off). @returns {Promise<void>} @throws {RangeError} If out of range. */
|
|
792
|
+
async setEncoderDeviation(steps) {
|
|
793
|
+
if (!(steps >= 0 && steps <= 0xFFFFF)) throw new RangeError('steps 0-1048575');
|
|
794
|
+
await this._writeReg(R.ENC_DEVIATION, steps);
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/** @returns {Promise<object>} {n_event, deviation_warn}. */
|
|
798
|
+
async readEncoderStatus() {
|
|
799
|
+
const s = await this._readReg(R.ENC_STATUS);
|
|
800
|
+
return { n_event: !!(s & 1), deviation_warn: !!(s & 2) };
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
/** Write 1 to the ENC_STATUS event bits. @returns {Promise<void>} */
|
|
804
|
+
async clearEncoderStatus() { await this._writeReg(R.ENC_STATUS, 0x03); }
|
|
805
|
+
|
|
806
|
+
_requireUart() { if (!this._uart) throw new Error('only available on the UART bus'); }
|
|
807
|
+
|
|
808
|
+
/** @param {number} slaveaddr - SLAVEADDR 0-253 (UART only). @returns {Promise<void>} @throws {Error} On SPI. @throws {RangeError} If out of range. */
|
|
809
|
+
async setSlaveAddress(slaveaddr) {
|
|
810
|
+
this._requireUart();
|
|
811
|
+
if (!(slaveaddr >= 0 && slaveaddr <= 253)) throw new RangeError('slaveaddr 0-253');
|
|
812
|
+
await this._writeReg(R.SLAVECONF, slaveaddr | (this._senddelay << 8));
|
|
813
|
+
this._address = slaveaddr;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/** @param {number} sd - SENDDELAY 0-15 (UART only; at least 2 on multi-node buses). @returns {Promise<void>} @throws {Error} On SPI. @throws {RangeError} If out of range. */
|
|
817
|
+
async setSendDelay(sd) {
|
|
818
|
+
this._requireUart();
|
|
819
|
+
if (!(sd >= 0 && sd <= 15)) throw new RangeError('send delay 0-15');
|
|
820
|
+
this._senddelay = sd;
|
|
821
|
+
await this._writeReg(R.SLAVECONF, ((this._shadow.get(R.SLAVECONF) ?? 0) & 0xFF) | (sd << 8));
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/** @param {boolean} level - OUTPUT bit 0, the NAO polarity (UART only). @returns {Promise<void>} @throws {Error} On SPI. */
|
|
825
|
+
async setNaoPolarity(level) {
|
|
826
|
+
this._requireUart();
|
|
827
|
+
await this._writeReg(R.OUTPUT, level ? 1 : 0);
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/** @returns {Promise<number>} IFCNT (UART only). @throws {Error} On SPI. */
|
|
831
|
+
async readIfcnt() {
|
|
832
|
+
this._requireUart();
|
|
833
|
+
return (await this._readReg(R.IFCNT)) & 0xFF;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
/** @returns {Promise<number>} MSCNT 0-1023. */
|
|
837
|
+
async microstepPosition() { return (await this._readReg(R.MSCNT)) & 0x3FF; }
|
|
838
|
+
|
|
839
|
+
/** @returns {Promise<number>} DRV_STATUS.CS_ACTUAL 0-31. */
|
|
840
|
+
async readCurrentScale() { return ((await this._readReg(R.DRV_STATUS)) >>> 16) & 0x1F; }
|
|
841
|
+
|
|
842
|
+
/** @returns {Promise<number[]>} Signed [CUR_A, CUR_B] from MSCURACT. */
|
|
843
|
+
async readCoilCurrents() {
|
|
844
|
+
const raw = await this._readReg(R.MSCURACT);
|
|
845
|
+
return [signed(raw >>> 16, 9), signed(raw, 9)];
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
/** @returns {Promise<number>} TSTEP (20 bit). */
|
|
849
|
+
async readTstep() { return (await this._readReg(R.TSTEP)) & 0xFFFFF; }
|
|
850
|
+
|
|
851
|
+
/** @returns {Promise<number[]>} [PWM_SCALE_SUM, PWM_SCALE_AUTO signed]. */
|
|
852
|
+
async readPwmScale() {
|
|
853
|
+
const raw = await this._readReg(R.PWM_SCALE);
|
|
854
|
+
return [raw & 0xFF, signed(raw >>> 16, 9)];
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
/** @returns {Promise<number[]>} [PWM_OFS_AUTO, PWM_GRAD_AUTO]. */
|
|
858
|
+
async readPwmAuto() {
|
|
859
|
+
const raw = await this._readReg(R.PWM_AUTO);
|
|
860
|
+
return [raw & 0xFF, (raw >>> 16) & 0xFF];
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/** @returns {Promise<number[]>} Signed [phase A, phase B] from OFFSET_READ. */
|
|
864
|
+
async readOffset() {
|
|
865
|
+
const raw = await this._readReg(R.OFFSET_READ);
|
|
866
|
+
return [signed(raw >>> 8, 8), signed(raw, 8)];
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
/** @returns {Promise<object>} Decoded DRV_STATUS. */
|
|
870
|
+
async readStatus() {
|
|
871
|
+
const s = await this._readReg(R.DRV_STATUS);
|
|
872
|
+
return {
|
|
873
|
+
stst: !!(s & 0x80000000), stealth: !!(s & (1 << 14)), fsactive: !!(s & (1 << 15)),
|
|
874
|
+
cs_actual: (s >>> 16) & 0x1F, sg_result: s & 0x3FF, stallguard: !!(s & (1 << 24)),
|
|
875
|
+
otpw: !!(s & (1 << 26)), ot: !!(s & (1 << 25)), short_gnd: !!(s & (3 << 27)),
|
|
876
|
+
short_supply: !!(s & (3 << 12)), open_load: !!(s & (3 << 29)),
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
/** @returns {Promise<object>} {reset, drv_err, uv_cp} (GSTAT; not cleared by the read). */
|
|
881
|
+
async readGlobalStatus() {
|
|
882
|
+
const g = await this._readReg(R.GSTAT);
|
|
883
|
+
return { reset: !!(g & 1), drv_err: !!(g & 2), uv_cp: !!(g & 4) };
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/** Write 0x07 to GSTAT. @returns {Promise<void>} */
|
|
887
|
+
async clearGlobalStatus() { await this._writeReg(R.GSTAT, 0x07); }
|
|
888
|
+
|
|
889
|
+
/** @returns {Promise<object>} {raw, refl, refr, encb, enca, drv_enn, encn, sd_mode, swcomp_in, version}. */
|
|
890
|
+
async readIoin() {
|
|
891
|
+
const raw = await this._readReg(R.IOIN);
|
|
892
|
+
return {
|
|
893
|
+
raw, refl: !!(raw & 1), refr: !!(raw & 2), encb: !!(raw & 4), enca: !!(raw & 8),
|
|
894
|
+
drv_enn: !!(raw & 16), encn: !!(raw & 32), sd_mode: !!(raw & 64), swcomp_in: !!(raw & 128),
|
|
895
|
+
version: (raw >>> 24) & 0xFF,
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
module.exports = { TMC5160Minimal, TMC5160Full, crc8 };
|