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