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,315 @@
1
+ 'use strict';
2
+
3
+ const REG_IE = 0x00;
4
+ const REG_CONTROL = 0x01;
5
+ const REG_ISTAT = 0x02;
6
+ const AI = 0x10;
7
+
8
+ const C_MYBUS = 0x01;
9
+ const C_NMYBUS = 0x02;
10
+ const C_BUSON = 0x04;
11
+ const C_NBUSON = 0x08;
12
+ const C_BUSINIT = 0x10;
13
+ const C_TESTON = 0x40;
14
+ const C_NTESTON = 0x80;
15
+
16
+ const MASKABLE = 0x0F;
17
+ const POLL_INTERVAL_MS = 100;
18
+
19
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
20
+
21
+ const hasControlBits = (c) => ((c & C_MYBUS) !== 0) === ((c & C_NMYBUS) !== 0);
22
+ const connectedBits = (c) => ((c & C_BUSON) !== 0) !== ((c & C_NBUSON) !== 0);
23
+ const nbuson = (c) => (c & C_NBUSON ? 1 : 0);
24
+ const nmybus = (c) => (c & C_NMYBUS ? 1 : 0);
25
+
26
+ /**
27
+ * PCA9541 2-to-1 I²C-bus master selector (NXP) — minimal interface.
28
+ *
29
+ * Two upstream masters share one downstream bus of slave devices through the
30
+ * PCA9541's pass gates; this driver is used by **one** of the two masters (the
31
+ * same software runs on both). There is no arbitration: whichever master writes
32
+ * its CONTROL register last before a STOP wins. Each master sees its own copy of
33
+ * the IE, CONTROL and ISTAT registers behind the same addresses. The 7-bit I²C
34
+ * address is 0x70 + A (A3..A0); prefer 0x70–0x77.
35
+ *
36
+ * `init()` reads CONTROL as a bus check (it rejects if the chip does not
37
+ * acknowledge) and ISTAT once to clear stale flags; it writes nothing, so
38
+ * initialising a master never changes the connection state. The CONTROL byte to
39
+ * write depends on the read-only complement bits, so every change reads CONTROL
40
+ * first and applies the datasheet's Table 12 rule.
41
+ */
42
+ class PCA9541Minimal {
43
+ /**
44
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (1-byte command code).
45
+ */
46
+ constructor(connection) {
47
+ this._conn = connection;
48
+ }
49
+
50
+ /** 7-bit I²C address with A3..A0 = 0. */
51
+ static get I2C_ADDRESS() { return 0x70; }
52
+
53
+ async _readReg(reg) {
54
+ return (await this._conn.readReg(reg, 1))[0];
55
+ }
56
+
57
+ async _writeReg(reg, value) {
58
+ await this._conn.writeReg(reg, value & 0xFF);
59
+ }
60
+
61
+ /**
62
+ * Run the initialization sequence: read CONTROL as a bus check and ISTAT once
63
+ * to clear stale BUSLOST / BUSOK / BUSINIT flags. Writes nothing.
64
+ * @returns {Promise<void>}
65
+ */
66
+ async init() {
67
+ await this._readReg(REG_CONTROL);
68
+ await this._readReg(REG_ISTAT);
69
+ }
70
+
71
+ /**
72
+ * Report whether this master's channel is connected to the downstream bus.
73
+ * @returns {Promise<boolean>} true if MYBUS equals NMYBUS and the bus is connected (BUSON differs from NBUSON).
74
+ */
75
+ async hasControl() {
76
+ const c = await this._readReg(REG_CONTROL);
77
+ return hasControlBits(c) && connectedBits(c);
78
+ }
79
+
80
+ /**
81
+ * Take the downstream bus. Reads CONTROL and writes MYBUS / BUSON per the
82
+ * datasheet rule; the write ends with a STOP, which makes the switch. With
83
+ * `busInit` the chip first sends nine clocks, a NACK and a STOP on the
84
+ * downstream bus and flags BUSINIT in ISTAT.
85
+ * @param {boolean} [busInit=false] - Request bus initialization / recovery.
86
+ * @param {number} [timeoutMs=20] - Maximum time to wait for the switch, in milliseconds.
87
+ * @returns {Promise<boolean>} true if this master has control afterwards, false if not within `timeoutMs`.
88
+ */
89
+ async acquire(busInit = false, timeoutMs = 20) {
90
+ const c = await this._readReg(REG_CONTROL);
91
+ let w = ((1 - nbuson(c)) << 2) | nmybus(c);
92
+ if (busInit) w |= C_BUSINIT;
93
+ await this._writeReg(REG_CONTROL, w);
94
+ const attempts = Math.floor(timeoutMs) + 1;
95
+ for (let i = 0; i < attempts; i++) {
96
+ if (busInit && !((await this._readReg(REG_ISTAT)) & 0x02)) {
97
+ await sleep(1);
98
+ continue;
99
+ }
100
+ if (await this.hasControl()) return true;
101
+ await sleep(1);
102
+ }
103
+ return false;
104
+ }
105
+
106
+ /**
107
+ * Give the downstream bus to the other master (MYBUS = NOT NMYBUS; the bus stays connected).
108
+ * @returns {Promise<void>}
109
+ */
110
+ async release() {
111
+ const c = await this._readReg(REG_CONTROL);
112
+ await this._writeReg(REG_CONTROL, ((1 - nbuson(c)) << 2) | (1 - nmybus(c)));
113
+ }
114
+ }
115
+
116
+ /**
117
+ * PCA9541 full interface — extends Minimal with raw control access, connect /
118
+ * disconnect, bus recovery, interrupt masks, the INT functional test and
119
+ * interrupt delivery.
120
+ */
121
+ class PCA9541Full extends PCA9541Minimal {
122
+ constructor(connection) {
123
+ super(connection);
124
+ this._callback = null;
125
+ this._intPinUsed = null;
126
+ this._pollTimer = null;
127
+ this._polling = false;
128
+ this._edgeHandler = () => { this._service(); };
129
+ }
130
+
131
+ static get SOURCE_INT_IN() { return 0x01; }
132
+ static get SOURCE_BUS_INIT() { return 0x02; }
133
+ static get SOURCE_BUS_NOT_IDLE() { return 0x04; }
134
+ static get SOURCE_BUS_LOST() { return 0x08; }
135
+ static get SOURCE_TEST() { return 0x40; }
136
+ static get SOURCE_OTHER_TEST() { return 0x80; }
137
+
138
+ /**
139
+ * Read the raw CONTROL byte.
140
+ * @returns {Promise<number>}
141
+ */
142
+ async getControl() {
143
+ return this._readReg(REG_CONTROL);
144
+ }
145
+
146
+ /**
147
+ * Whether the downstream bus is connected to some master (BUSON differs from NBUSON).
148
+ * @returns {Promise<boolean>}
149
+ */
150
+ async isConnected() {
151
+ return connectedBits(await this._readReg(REG_CONTROL));
152
+ }
153
+
154
+ /**
155
+ * Decode the CONTROL register.
156
+ * @returns {Promise<{hasControl: boolean, connected: boolean, busInitRequested: boolean, ownTest: boolean, otherTest: boolean}>}
157
+ */
158
+ async status() {
159
+ const c = await this._readReg(REG_CONTROL);
160
+ return {
161
+ hasControl: hasControlBits(c),
162
+ connected: connectedBits(c),
163
+ busInitRequested: Boolean(c & C_BUSINIT),
164
+ ownTest: Boolean(c & C_TESTON),
165
+ otherTest: Boolean(c & C_NTESTON),
166
+ };
167
+ }
168
+
169
+ /**
170
+ * Turn the connection on without changing ownership (BUSON = NOT NBUSON; MYBUS as read).
171
+ * @returns {Promise<void>}
172
+ */
173
+ async connect() {
174
+ const c = await this._readReg(REG_CONTROL);
175
+ await this._writeReg(REG_CONTROL, ((1 - nbuson(c)) << 2) | (c & C_MYBUS));
176
+ }
177
+
178
+ /**
179
+ * Disconnect both masters from the downstream bus (BUSON = NBUSON; ownership bits left).
180
+ * @returns {Promise<void>}
181
+ */
182
+ async disconnect() {
183
+ const c = await this._readReg(REG_CONTROL);
184
+ await this._writeReg(REG_CONTROL, (nbuson(c) << 2) | (c & C_MYBUS));
185
+ }
186
+
187
+ /**
188
+ * Recover a hung downstream bus and take it (acquire with bus initialization).
189
+ * @param {number} [timeoutMs=20] - Maximum time to wait for BUSINIT, in milliseconds.
190
+ * @returns {Promise<boolean>} true if this master has control afterwards.
191
+ */
192
+ async recoverBus(timeoutMs = 20) {
193
+ return this.acquire(true, timeoutMs);
194
+ }
195
+
196
+ /**
197
+ * Write the IE register (true = masked, no interrupt); all-false re-enables every interrupt.
198
+ * @param {object} [mask]
199
+ * @param {boolean} [mask.busLost=false] - Mask the "other master took the bus" interrupt.
200
+ * @param {boolean} [mask.busNotIdle=false] - Mask the "bus non-idle at the switch" interrupt.
201
+ * @param {boolean} [mask.busInit=false] - Mask the "bus initialization done" interrupt.
202
+ * @param {boolean} [mask.intIn=false] - Mask the downstream INT_IN interrupt.
203
+ * @returns {Promise<void>}
204
+ */
205
+ async setInterruptMask({ busLost = false, busNotIdle = false, busInit = false, intIn = false } = {}) {
206
+ await this._writeReg(REG_IE, (busLost ? 8 : 0) | (busNotIdle ? 4 : 0) | (busInit ? 2 : 0) | (intIn ? 1 : 0));
207
+ }
208
+
209
+ /**
210
+ * Read the IE register (bit set = interrupt masked).
211
+ * @returns {Promise<number>}
212
+ */
213
+ async getInterruptMask() {
214
+ return this._readReg(REG_IE);
215
+ }
216
+
217
+ /**
218
+ * Drive the INT lines for the functional test; the bus bits are written back unchanged.
219
+ * @param {boolean} [own=false] - Drive this master's INT low (TESTON).
220
+ * @param {boolean} [other=false] - Drive the other master's INT low (NTESTON).
221
+ * @returns {Promise<void>}
222
+ */
223
+ async testInterrupt(own = false, other = false) {
224
+ const c = await this._readReg(REG_CONTROL);
225
+ await this._writeReg(REG_CONTROL, (c & (C_BUSON | C_MYBUS)) | (own ? C_TESTON : 0) | (other ? C_NTESTON : 0));
226
+ }
227
+
228
+ /**
229
+ * Read IE, CONTROL and ISTAT in one auto-increment transaction (clears the self-clearing ISTAT flags).
230
+ * @returns {Promise<{ie: number, control: number, istat: number}>}
231
+ */
232
+ async readAll() {
233
+ const b = await this._conn.readReg(AI | REG_IE, 3);
234
+ return { ie: b[0], control: b[1], istat: b[2] };
235
+ }
236
+
237
+ /**
238
+ * Enable an interrupt source (clear its mask bit in IE); the test sources need no enable.
239
+ * @param {number} source - One of SOURCE_INT_IN, SOURCE_BUS_INIT, SOURCE_BUS_NOT_IDLE, SOURCE_BUS_LOST.
240
+ * @returns {Promise<void>}
241
+ */
242
+ async enableInterrupt(source) {
243
+ const bit = source & MASKABLE;
244
+ if (bit) await this._writeReg(REG_IE, (await this._readReg(REG_IE)) & ~bit);
245
+ }
246
+
247
+ /**
248
+ * Disable an interrupt source (set its mask bit in IE).
249
+ * @param {number} source - One of SOURCE_INT_IN, SOURCE_BUS_INIT, SOURCE_BUS_NOT_IDLE, SOURCE_BUS_LOST.
250
+ * @returns {Promise<void>}
251
+ */
252
+ async disableInterrupt(source) {
253
+ const bit = source & MASKABLE;
254
+ if (bit) await this._writeReg(REG_IE, (await this._readReg(REG_IE)) | bit);
255
+ }
256
+
257
+ /**
258
+ * Read ISTAT (clears BUSLOST / BUSOK / BUSINIT).
259
+ * @returns {Promise<number>} ISTAT byte; bits as the SOURCE_* constants.
260
+ */
261
+ async pollInterrupt() {
262
+ return this._readReg(REG_ISTAT);
263
+ }
264
+
265
+ async _arm(intPin) {
266
+ this._intPinUsed = intPin;
267
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
268
+ if (intPin) {
269
+ await intPin.onEdge(this._edgeHandler, 'falling');
270
+ } else {
271
+ this._pollTimer = setInterval(() => { this._service(); }, POLL_INTERVAL_MS);
272
+ }
273
+ }
274
+
275
+ /**
276
+ * Subscribe to this master's INT pin (falling edge). `callback(status)`
277
+ * receives the ISTAT byte read (which clears the self-clearing flags).
278
+ * Delivery is via the INT line (options.intPin or connection.intPin) or a
279
+ * 100 ms polling timer.
280
+ * @param {function} callback - Called with (status).
281
+ * @param {object} [options]
282
+ * @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
283
+ * @returns {Promise<void>}
284
+ */
285
+ async onInterrupt(callback, options = {}) {
286
+ this._callback = callback;
287
+ await this._arm(options.intPin ?? this._conn.intPin ?? null);
288
+ }
289
+
290
+ /**
291
+ * Unsubscribe and stop delivery.
292
+ * @returns {Promise<void>}
293
+ */
294
+ async offInterrupt() {
295
+ if (this._intPinUsed) {
296
+ await this._intPinUsed.offEdge(this._edgeHandler);
297
+ this._intPinUsed = null;
298
+ }
299
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
300
+ this._callback = null;
301
+ }
302
+
303
+ async _service() {
304
+ if (this._polling) return;
305
+ this._polling = true;
306
+ try {
307
+ const status = await this.pollInterrupt();
308
+ if (status && this._callback) this._callback(status);
309
+ } finally {
310
+ this._polling = false;
311
+ }
312
+ }
313
+ }
314
+
315
+ module.exports = { PCA9541Minimal, PCA9541Full };