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.
Files changed (114) hide show
  1. package/README.md +27 -0
  2. package/examples/accelerometer/bma180/complete.js +83 -0
  3. package/examples/accelerometer/bma180/demo.js +47 -0
  4. package/examples/accelerometer/bma180/minimal.js +21 -0
  5. package/examples/accelerometer/fxos8700/complete.js +111 -0
  6. package/examples/accelerometer/fxos8700/demo.js +80 -0
  7. package/examples/accelerometer/fxos8700/minimal.js +20 -0
  8. package/examples/accelerometer/lis3dh/complete.js +123 -0
  9. package/examples/accelerometer/lis3dh/demo.js +99 -0
  10. package/examples/accelerometer/lis3dh/minimal.js +19 -0
  11. package/examples/accelerometer/lsm303/complete.js +82 -0
  12. package/examples/accelerometer/lsm303/demo.js +96 -0
  13. package/examples/accelerometer/lsm303/minimal.js +23 -0
  14. package/examples/accelerometer/mma8451q/complete.js +96 -0
  15. package/examples/accelerometer/mma8451q/demo.js +120 -0
  16. package/examples/accelerometer/mma8451q/minimal.js +20 -0
  17. package/examples/adc_dac/ad5243/complete.js +52 -0
  18. package/examples/adc_dac/ad5243/demo.js +50 -0
  19. package/examples/adc_dac/ad5243/minimal.js +23 -0
  20. package/examples/adc_dac/ad5248/complete.js +49 -0
  21. package/examples/adc_dac/ad5248/demo.js +42 -0
  22. package/examples/adc_dac/ad5248/minimal.js +21 -0
  23. package/examples/adc_dac/ads1013/complete.js +51 -0
  24. package/examples/adc_dac/ads1013/demo.js +51 -0
  25. package/examples/adc_dac/ads1013/minimal.js +21 -0
  26. package/examples/adc_dac/ads1014/complete.js +88 -0
  27. package/examples/adc_dac/ads1014/demo.js +52 -0
  28. package/examples/adc_dac/ads1014/minimal.js +21 -0
  29. package/examples/adc_dac/ads1113/complete.js +47 -0
  30. package/examples/adc_dac/ads1113/demo.js +39 -0
  31. package/examples/adc_dac/ads1113/minimal.js +20 -0
  32. package/examples/adc_dac/ads1114/complete.js +77 -0
  33. package/examples/adc_dac/ads1114/demo.js +47 -0
  34. package/examples/adc_dac/ads1114/minimal.js +20 -0
  35. package/examples/adc_dac/ads1115/complete.js +82 -0
  36. package/examples/adc_dac/ads1115/demo.js +55 -0
  37. package/examples/adc_dac/ads1115/minimal.js +20 -0
  38. package/examples/adc_dac/es8311/complete.js +80 -0
  39. package/examples/adc_dac/es8311/demo.js +59 -0
  40. package/examples/adc_dac/es8311/minimal.js +18 -0
  41. package/examples/adc_dac/mcp4xxx/complete.js +97 -0
  42. package/examples/adc_dac/mcp4xxx/demo.js +76 -0
  43. package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
  44. package/examples/display/pcf8578/complete.js +58 -0
  45. package/examples/display/pcf8578/demo.js +62 -0
  46. package/examples/display/pcf8578/minimal.js +19 -0
  47. package/examples/display/pcf8579/complete.js +58 -0
  48. package/examples/display/pcf8579/demo.js +61 -0
  49. package/examples/display/pcf8579/minimal.js +19 -0
  50. package/examples/imu/icm20948/complete.js +114 -0
  51. package/examples/imu/icm20948/demo.js +79 -0
  52. package/examples/imu/icm20948/minimal.js +22 -0
  53. package/examples/imu/qmi8658a/complete.js +134 -0
  54. package/examples/imu/qmi8658a/demo.js +81 -0
  55. package/examples/imu/qmi8658a/minimal.js +22 -0
  56. package/examples/io_expander/mcp23008/complete.js +70 -0
  57. package/examples/io_expander/mcp23008/demo.js +58 -0
  58. package/examples/io_expander/mcp23008/minimal.js +20 -0
  59. package/examples/memory/mb85rc/complete.js +40 -0
  60. package/examples/memory/mb85rc/demo.js +58 -0
  61. package/examples/memory/mb85rc/minimal.js +15 -0
  62. package/examples/motor/drv2605/complete.js +100 -0
  63. package/examples/motor/drv2605/demo.js +55 -0
  64. package/examples/motor/drv2605/minimal.js +24 -0
  65. package/examples/motor/tmc2130/complete.js +138 -0
  66. package/examples/motor/tmc2130/demo.js +96 -0
  67. package/examples/motor/tmc2130/minimal.js +30 -0
  68. package/examples/motor/tmc2208/complete.js +92 -0
  69. package/examples/motor/tmc2208/demo.js +85 -0
  70. package/examples/motor/tmc2208/minimal.js +25 -0
  71. package/examples/motor/tmc2209/complete.js +106 -0
  72. package/examples/motor/tmc2209/demo.js +97 -0
  73. package/examples/motor/tmc2209/minimal.js +25 -0
  74. package/examples/motor/tmc5160/complete.js +205 -0
  75. package/examples/motor/tmc5160/demo.js +104 -0
  76. package/examples/motor/tmc5160/minimal.js +35 -0
  77. package/examples/power/axp2101/complete.js +135 -0
  78. package/examples/power/axp2101/demo.js +68 -0
  79. package/examples/power/axp2101/minimal.js +24 -0
  80. package/examples/rtc/pcf85063a/complete.js +68 -0
  81. package/examples/rtc/pcf85063a/demo.js +76 -0
  82. package/examples/rtc/pcf85063a/minimal.js +20 -0
  83. package/package.json +1 -1
  84. package/src/chips/accelerometer/bma180.js +573 -0
  85. package/src/chips/accelerometer/fxos8700.js +1600 -0
  86. package/src/chips/accelerometer/lis3dh.js +1341 -0
  87. package/src/chips/accelerometer/lsm303.js +1471 -0
  88. package/src/chips/accelerometer/mma8451q.js +1406 -0
  89. package/src/chips/adc_dac/_ads111x_base.js +272 -0
  90. package/src/chips/adc_dac/ad5243.js +231 -0
  91. package/src/chips/adc_dac/ad5248.js +194 -0
  92. package/src/chips/adc_dac/ads1013.js +219 -0
  93. package/src/chips/adc_dac/ads1014.js +509 -0
  94. package/src/chips/adc_dac/ads1113.js +112 -0
  95. package/src/chips/adc_dac/ads1114.js +201 -0
  96. package/src/chips/adc_dac/ads1115.js +232 -0
  97. package/src/chips/adc_dac/es8311.js +621 -0
  98. package/src/chips/adc_dac/mcp4xxx.js +982 -0
  99. package/src/chips/display/pcf8578.js +452 -0
  100. package/src/chips/display/pcf8579.js +451 -0
  101. package/src/chips/imu/icm20948.js +849 -0
  102. package/src/chips/imu/qmi8658a.js +1011 -0
  103. package/src/chips/io_expander/mcp23008.js +558 -0
  104. package/src/chips/memory/mb85rc.js +166 -0
  105. package/src/chips/motor/drv2605.js +592 -0
  106. package/src/chips/motor/tmc2130.js +537 -0
  107. package/src/chips/motor/tmc2208.js +490 -0
  108. package/src/chips/motor/tmc2209.js +565 -0
  109. package/src/chips/motor/tmc5160.js +900 -0
  110. package/src/chips/power/axp2101.js +968 -0
  111. package/src/chips/rtc/pcf85063a.js +482 -0
  112. package/src/connection/uart.js +9 -0
  113. package/src/connection/uart_mock.js +65 -0
  114. package/src/discovery/registry.js +45 -19
@@ -0,0 +1,106 @@
1
+ 'use strict';
2
+
3
+ const { UARTConnection } = require('periph/src/connection/uart');
4
+ const { TMC2209Full } = require('periph/src/chips/motor/tmc2209');
5
+
6
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
7
+
8
+ (async () => {
9
+ const port = process.env.UART_PORT || '/dev/ttyS0';
10
+ const connection = new UARTConnection(port, { baudRate: 115200, timeoutMs: 100 });
11
+ await connection.open();
12
+ const motor = new TMC2209Full(connection, { rSenseOhm: 0.11 }); // Create TMC2209 Full driver, (connection, {rSenseOhm=0.11 Ω, address=0, enPin, echo=true, stepPin, dirPin, diagPin, multiNode=false, fclkHz=12000000 Hz})
13
+ await motor.init(); // Probe the chip and apply defaults, () → None
14
+ // reads IOIN to verify the chip, writes the default configuration and checks IFCNT
15
+
16
+ await motor.enable(); // Power stage on, () → None
17
+ // restores TOFF (or drives ENN low when enPin is wired)
18
+ await motor.setCurrent(0.8); // Set run current, (rmsA A RMS) → None
19
+ // converts amperes to the 0–31 scale; hold current becomes a third
20
+ await motor.setHoldCurrent(0.2); // Set hold current, (rmsA A RMS) → None
21
+ // overrides the automatic one-third hold current
22
+ await motor.setCurrentDelay(6, 20); // Set power-down timing, (iholdDelay 0–15, tpowerdown 0–255) → None
23
+ // IHOLDDELAY and TPOWERDOWN, units of 2^18 clocks
24
+ await motor.setMicrosteps(32); // Set microstep resolution, (n 1–256) → None
25
+ // writes CHOPCONF.MRES
26
+ await motor.setInterpolation(true); // Interpolate to 256 microsteps, (enabled) → None
27
+ // sets CHOPCONF.intpol
28
+ await motor.setChopperMode(false); // Choose chopper, (spreadcycle) → None
29
+ // false selects StealthChop
30
+ await motor.setStealthchopThreshold(300); // Set StealthChop velocity limit, (tpwmthrs 20 bit) → None
31
+ // faster than this the chip switches to SpreadCycle
32
+ await motor.configureChopper(3, 2, 5, 0); // Configure chopper timing, (toff, tbl, hstrt, hend) → None
33
+ // direct CHOPCONF fields
34
+ await motor.configureStealthchop(true, true, 36, 14, 1, 4, 12); // Configure StealthChop, (autoscale, autograd, pwmOfs, pwmGrad, pwmFreq, pwmReg, pwmLim) → None
35
+ // direct PWMCONF fields
36
+ await motor.setFreewheel(0); // Set standstill mode, (mode 0–3) → None
37
+ // PWMCONF.freewheel, only effective when IHOLD is 0
38
+ await motor.setVsense(false); // Select sense range, (highSensitivity) → None
39
+ // false keeps the 0.325 V full scale
40
+ await motor.setAnalogScaling(true); // Current reference source, (enabled) → None
41
+ // true takes the reference from VREF
42
+ await motor.setDirectionInverted(false); // Invert direction, (inverted) → None
43
+ // GCONF.shaft
44
+ await motor.setIndexOutput(0); // Select INDEX output, (mode 0–2) → None
45
+ // 0 first microstep, 1 pre-warning, 2 step pulses
46
+ await motor.setDriverProtection(true, true); // Short protections, (shortLs, shortGnd) → None
47
+ // true keeps the protection enabled
48
+ await motor.setDoubleEdge(false); // Step on both edges, (enabled) → None
49
+ // CHOPCONF.dedge
50
+ await motor.setStallThreshold(60); // Set StallGuard4 threshold, (sgthrs 0–255) → None
51
+ // stall when SG_RESULT <= 2 * SGTHRS
52
+ await motor.setCoolstepThreshold(1000); // Set CoolStep / stall velocity limit, (tcoolthrs 20 bit) → None
53
+ // active while TCOOLTHRS >= TSTEP > TPWMTHRS
54
+ await motor.configureCoolstep(5, 2, 1, 1, 0); // Configure CoolStep, (semin 0–15, semax 0–15, seup 0–3, sedn 0–3, seimin 0–1) → None
55
+ // semin = 0 would disable CoolStep
56
+ await motor.setSendDelay(0); // Set reply delay, (n 0–15) → None
57
+ // NODECONF.SENDDELAY
58
+
59
+ await motor.run(300); // Spin with the internal generator, (stepsPerS full steps/s) → None
60
+ // converts to VACTUAL using the 12 MHz clock
61
+ await sleep(2000);
62
+ console.log('position', await motor.microstepPosition()); // Read microstep counter, () → number 0–1023
63
+ // MSCNT
64
+ console.log('currents', await motor.readCoilCurrents()); // Read coil currents, () → [number, number] table values
65
+ // two signed 9-bit MSCURACT fields
66
+ console.log('sg_result', await motor.readStallguard()); // Read StallGuard4 result, () → number 0–510
67
+ // SG_RESULT, higher = less load
68
+ try {
69
+ console.log('stalled', motor.isStalled()); // Read DIAG pin, () → boolean
70
+ // needs a diagPin; true on a stall or a driver error
71
+ } catch (e) {
72
+ console.log('stalled: no diagPin wired');
73
+ }
74
+ console.log('tstep', await motor.readTstep()); // Read step interval, () → number clocks
75
+ // time between 1/256 microsteps
76
+ console.log('scale', await motor.readCurrentScale()); // Read actual current scale, () → number 0–31
77
+ // DRV_STATUS.CS_ACTUAL
78
+ console.log('temp', await motor.readTemperatureFlags()); // Read temperature flags, () → number 0–15
79
+ // bit 0 t120 … bit 3 t157
80
+ console.log('status', await motor.readStatus()); // Read driver status, () → object
81
+ // decoded DRV_STATUS
82
+ console.log('standstill', await motor.isStandstill()); // Read standstill flag, () → boolean
83
+ // DRV_STATUS.stst
84
+ console.log('fault', await motor.readFault()); // Read fault bitmask, () → number
85
+ // FAULT_* constants, 0 = none
86
+ await motor.stop(); // Stop the motor, () → None
87
+ // VACTUAL = 0, STEP input takes over again
88
+
89
+ console.log('gstat', await motor.readGlobalStatus(true)); // Read and clear GSTAT, (clear=false) → object
90
+ // reset, drv_err, uv_cp
91
+ console.log('ifcnt', await motor.readIfcnt()); // Read write counter, () → number
92
+ // IFCNT
93
+ console.log('ioin', await motor.readIoin()); // Read IOIN, () → object
94
+ // raw word, decoded pins and silicon version
95
+ console.log('otp', (await motor.readOtp()).toString(16)); // Read OTP image, () → number
96
+ // OTP_READ
97
+ console.log('pwm_auto', await motor.readPwmAuto()); // Read StealthChop tuning, () → object
98
+ // PWM_SCALE and PWM_AUTO results
99
+ console.log('gconf', (await motor.readRegister(0x00)).toString(16)); // Read a raw register, (reg) → number
100
+ // any readable register
101
+ await motor.writeRegister(0x13, 300); // Write a raw register, (reg, value) → None
102
+ // here TPWMTHRS again
103
+ await motor.disable(); // Power stage off, () → None
104
+ // the motor freewheels
105
+ await connection.close();
106
+ })();
@@ -0,0 +1,97 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Demo for the TMC2209 — sensorless homing of a linear axis with StallGuard4.
5
+ *
6
+ * Enables the driver at 0.8 A with 16 microsteps, proves the UART link by
7
+ * reading IOIN.VERSION (0x21) and IFCNT, then drives toward the end stop with
8
+ * the internal step generator at 200 full steps/s. When SG_RESULT falls to
9
+ * 2 * SGTHRS the axis has hit the stop: the demo stops, reverses for 2 s to
10
+ * back off and finishes with a CoolStep move. Any fault stops the motor,
11
+ * disables the driver and exits.
12
+ */
13
+
14
+ const { UARTConnection } = require('periph/src/connection/uart');
15
+ const { TMC2209Full } = require('periph/src/chips/motor/tmc2209');
16
+
17
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
18
+ const MICROSTEPS = 16;
19
+ const HOMING_SPEED = 200;
20
+ const SGTHRS = 60;
21
+
22
+ (async () => {
23
+ const port = process.env.UART_PORT || '/dev/ttyS0';
24
+ const connection = new UARTConnection(port, { baudRate: 115200, timeoutMs: 100 });
25
+ await connection.open();
26
+ const motor = new TMC2209Full(connection); // Create TMC2209 Full driver, (connection, {rSenseOhm=0.11 Ω, address=0, enPin})
27
+ await motor.init(); // Probe the chip and apply defaults, () → None
28
+
29
+ async function guard() {
30
+ // --- Stop on any driver fault ---
31
+ // Short circuits and over-temperature latch until the driver is
32
+ // disabled, so the demo shuts everything down and reports which fault fired.
33
+ const fault = await motor.readFault(); // Read fault bitmask, () → number
34
+ if (fault) {
35
+ await motor.stop(); // Stop the motor, () → None
36
+ await motor.disable(); // Power stage off, () → None
37
+ console.log('fault 0x' + fault.toString(16));
38
+ await connection.close();
39
+ process.exit(1);
40
+ }
41
+ }
42
+
43
+ // --- Bring up the link and the power stage ---
44
+ // IOIN.VERSION and IFCNT prove the UART wiring and echo handling work
45
+ // before any motion is attempted.
46
+ const ioin = await motor.readIoin(); // Read IOIN, () → object
47
+ console.log('version 0x' + ioin.version.toString(16), 'IFCNT', await motor.readIfcnt()); // Read write counter, () → number
48
+ await motor.setCurrent(0.8); // Set run current, (rmsA A RMS) → None
49
+ await motor.setMicrosteps(MICROSTEPS); // Set microstep resolution, (n 1–256) → None
50
+ await motor.enable(); // Power stage on, () → None
51
+
52
+ // --- Arm StallGuard4 for the homing speed ---
53
+ // StallGuard needs StealthChop and TCOOLTHRS >= TSTEP > TPWMTHRS; TCOOLTHRS
54
+ // is set just below the TSTEP of the homing speed so detection is active.
55
+ await motor.setStallThreshold(SGTHRS); // Set StallGuard4 threshold, (sgthrs 0–255) → None
56
+ await motor.setCoolstepThreshold(Math.floor(12e6 / (HOMING_SPEED * 256 / 2))); // Set CoolStep / stall velocity limit, (tcoolthrs 20 bit) → None
57
+
58
+ // --- Drive toward the end stop ---
59
+ // The first 0.5 s is spin-up, where SG_RESULT is not yet valid; afterwards
60
+ // the demo polls SG_RESULT every 100 ms until it drops to 2 * SGTHRS.
61
+ await motor.run(HOMING_SPEED); // Spin with the internal generator, (stepsPerS full steps/s) → None
62
+ await sleep(500);
63
+ let sg;
64
+ for (;;) {
65
+ sg = await motor.readStallguard(); // Read StallGuard4 result, () → number 0–510
66
+ console.log('SG_RESULT', sg);
67
+ if (sg <= 2 * SGTHRS) break;
68
+ await guard();
69
+ await sleep(100);
70
+ }
71
+
72
+ // --- End stop found: stop and back off ---
73
+ // Reversing for 2 s moves the axis clear of the stop.
74
+ await motor.stop(); // Stop the motor, () → None
75
+ console.log('stalled at SG_RESULT', sg);
76
+ await motor.run(-HOMING_SPEED); // Spin with the internal generator, (stepsPerS full steps/s) → None
77
+ await sleep(2000);
78
+ await motor.stop(); // Stop the motor, () → None
79
+
80
+ // --- Final move with CoolStep ---
81
+ // CoolStep lowers the current while the load is light; CS_ACTUAL shows it.
82
+ await motor.configureCoolstep(5, 2, 1, 1, 0); // Configure CoolStep, (semin 0–15, semax 0–15, seup 0–3, sedn 0–3, seimin 0–1) → None
83
+ await motor.run(HOMING_SPEED); // Spin with the internal generator, (stepsPerS full steps/s) → None
84
+ for (let i = 0; i < 10; i++) {
85
+ await sleep(500);
86
+ console.log('CS_ACTUAL', await motor.readCurrentScale()); // Read actual current scale, () → number 0–31
87
+ await guard();
88
+ }
89
+ await motor.stop(); // Stop the motor, () → None
90
+
91
+ // --- Shut down and report ---
92
+ // Temperature comparators and GSTAT show whether the run stressed the driver.
93
+ console.log('temperature flags', await motor.readTemperatureFlags()); // Read temperature flags, () → number 0–15
94
+ console.log('global status', await motor.readGlobalStatus()); // Read GSTAT, (clear=false) → object
95
+ await motor.disable(); // Power stage off, () → None
96
+ await connection.close();
97
+ })();
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ const { UARTConnection } = require('periph/src/connection/uart');
4
+ const { TMC2209Minimal } = require('periph/src/chips/motor/tmc2209');
5
+
6
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
7
+
8
+ (async () => {
9
+ const port = process.env.UART_PORT || '/dev/ttyS0';
10
+ const connection = new UARTConnection(port, { baudRate: 115200, timeoutMs: 100 });
11
+ await connection.open();
12
+ const motor = new TMC2209Minimal(connection, { rSenseOhm: 0.11 }); // Create TMC2209 driver, (connection, {rSenseOhm=0.11 Ω, address=0, enPin=null, echo=true})
13
+ await motor.init(); // Probe the chip and apply defaults, () → None
14
+
15
+ await motor.setCurrent(0.8); // Set run current, (rmsA A RMS) → None
16
+ for (const speed of [200, -200]) {
17
+ await motor.run(speed); // Spin with the internal generator, (stepsPerS full steps/s, sign = direction) → None
18
+ await sleep(3000);
19
+ console.log('standstill:', await motor.isStandstill()); // Read standstill flag, () → boolean
20
+ console.log('fault:', await motor.readFault()); // Read fault bitmask, () → number
21
+ }
22
+
23
+ await motor.stop(); // Stop the motor, () → None
24
+ await connection.close();
25
+ })();
@@ -0,0 +1,205 @@
1
+ 'use strict';
2
+
3
+ const { SPIConnection } = require('periph/src/connection/spi');
4
+ const { GpioOutputPin } = require('periph/src/connection/output_pin');
5
+ const { Default } = require('opengpio');
6
+ const { TMC5160Full } = require('periph/src/chips/motor/tmc5160');
7
+
8
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
9
+ const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
10
+ const SPI_DEV = parseInt(process.env.SPI_DEV || '0', 10);
11
+
12
+ (async () => {
13
+ const spi = new SPIConnection(SPI_BUS, SPI_DEV, { mode: 3, maxSpeedHz: 1_000_000 }); // Create SPI connection, (busNumber, deviceNumber, {mode=3, maxSpeedHz=1 MHz})
14
+ const enPin = new GpioOutputPin(Default.output({ chip: 0, line: 17 })); // Create DRV_ENN pin, (chip, line=17) → OutputPin
15
+ const diag0Pin = { isHigh: () => false }; // Stand-in DIAG0 input; use a real InputPin when DIAG0 is wired
16
+ const motor = new TMC5160Full(spi, { enPin, diag0Pin }); // Create TMC5160 Full driver, (connection, {busType='spi', rSenseOhm=0.075 Ω, enPin, address=0, echo=true, multiNode=false, fclkHz=12000000 Hz, diag0Pin, diag1Pin})
17
+ await motor.init(); // Probe the chip and apply defaults, () → None
18
+ // reads IOIN to verify the chip, writes the default configuration with verified writes
19
+
20
+ await motor.setCurrent(1.5); // Set run current, (rmsA A RMS) → None
21
+ // sets GLOBALSCALER and IRUN; hold current becomes a third
22
+ await motor.setHoldCurrent(0.3); // Set hold current, (rmsA A RMS) → None
23
+ // overrides the automatic one-third hold current
24
+ await motor.setGlobalScaler(200); // Set raw GLOBALSCALER, (value 0 or 32–256) → None
25
+ // 32/256 … 256/256 of the maximum current
26
+ await motor.setCurrentScale(31, 10, 6); // Set raw IHOLD_IRUN, (irun, ihold, iholdDelay) → None
27
+ // direct register fields
28
+ await motor.setPowerDownDelay(20); // Set TPOWERDOWN, (tpowerdown 0–255) → None
29
+ // units of 2^18 clocks
30
+ await motor.setSpeed(300); // Set target speed, (fullStepsPerS) → None
31
+ // VMAX
32
+ await motor.setAcceleration(800); // Set acceleration, (fullStepsPerS2) → None
33
+ // A1 = AMAX = DMAX = D1
34
+ await motor.configureRamp(0, 1200, 200, 600, 500, 900, 1500, 10); // Configure the SixPoint ramp, (vstart, a1, v1, amax, vmax, dmax, d1, vstop) → None
35
+ // physical units: full steps/s and full steps/s²
36
+ await motor.setRampRaw(0, 1564, 0, 1564, 71583, 1564, 1564, 10); // Configure the ramp in raw units, (vstart, a1, v1, amax, vmax, dmax, d1, vstop) → None
37
+ // register values
38
+ await motor.setZeroWait(0.1); // Set TZEROWAIT, (seconds) → None
39
+ // wait after ramp-down to zero
40
+ await motor.setMicrosteps(256); // Set microstep resolution, (n 1–256) → None
41
+ // 256 is the normal setting with the motion controller
42
+ await motor.setInterpolation(false); // Interpolation, (enabled) → None
43
+ // only useful for external STEP/DIR
44
+ await motor.setChopperMode(false); // Choose chopper, (spreadcycle) → None
45
+ // false enables StealthChop below TPWMTHRS
46
+ await motor.setStealthchopThreshold(500); // Set StealthChop velocity limit, (tpwmthrs 20 bit) → None
47
+ // faster than this the chip switches to SpreadCycle
48
+ await motor.setHighVelocityThreshold(0); // Set THIGH, (thigh 20 bit) → None
49
+ // 0 keeps the high-velocity features off
50
+ await motor.configureChopper(3, 2, 4, 1, 0); // Configure SpreadCycle timing, (toff, tbl, hstrt, hend, tpfd) → None
51
+ // direct CHOPCONF fields with chm = 0
52
+ await motor.configureConstantOffTime(3, 2, 4, 0, false, 0); // Configure constant off time, (toff, tbl, tfd, offset, disfdcc, tpfd) → None
53
+ // chm = 1 with fast decay
54
+ await motor.configureChopper(3, 2, 4, 1, 0); // Back to SpreadCycle, (toff, tbl, hstrt, hend, tpfd) → None
55
+ // chm = 0 again
56
+ await motor.configureHighVelocity(false, false); // Behaviour above THIGH, (vhighchm, vhighfs) → None
57
+ // both off
58
+ await motor.configureStealthchop(true, true, 30, 0, 0, 4, 12); // Configure StealthChop, (autoscale, autograd, pwmOfs, pwmGrad, pwmFreq, pwmReg, pwmLim) → None
59
+ // direct PWMCONF fields
60
+ await motor.setFreewheel(0); // Set standstill mode, (mode 0–3) → None
61
+ // PWMCONF.freewheel, only effective when IHOLD is 0
62
+ await motor.setShortProtection(true, true); // Short protections, (shortGnd, shortSupply) → None
63
+ // true keeps the protection enabled
64
+ await motor.setDoubleEdge(false); // Step on both edges, (enabled) → None
65
+ // external STEP/DIR only
66
+ await motor.setDirectionInverted(false); // Invert direction, (inverted) → None
67
+ // GCONF.shaft
68
+ await motor.setFastStandstill(false); // Fast standstill timeout, (enabled) → None
69
+ // GCONF.faststandstill
70
+ await motor.setRecalibrate(false); // Zero-crossing recalibration, (enabled) → None
71
+ // GCONF.recalibrate
72
+ await motor.setSmallHysteresis(false); // Comparison hysteresis, (enabled) → None
73
+ // false = 1/16
74
+ await motor.setEmergencyStop(false); // ENCA_DCIN emergency stop, (enabled) → None
75
+ // GCONF.stop_enable
76
+ await motor.configureDiag({}); // DIAG pin function, ({diag0StepOutput, diag1DirOutput, diag0Pushpull, diag1Pushpull}) → None
77
+ // DIAG0 = interrupt, DIAG1 = position compare
78
+ await motor.configureSwitches({ stopLeft: true, stopRight: true }); // Configure reference switches, ({stopLeft, stopRight, leftActiveLow, rightActiveLow, swap, softStop}) → None
79
+ // automatic stop on both switches
80
+ await motor.configureLatch({ leftActive: true, rightActive: true }); // Configure position latching, ({leftActive, leftInactive, rightActive, rightInactive, latchEncoder}) → None
81
+ // latch XACTUAL on active-going switch edges
82
+ await motor.setPositionCompare(100); // Set X_COMPARE, (position full steps) → None
83
+ // DIAG1 high while XACTUAL = X_COMPARE
84
+ await motor.setStallThreshold(0); // Set StallGuard2 threshold, (sgt -64..63) → None
85
+ // lower = more sensitive
86
+ await motor.setStallguardFilter(false); // StallGuard2 filter, (enabled) → None
87
+ // COOLCONF.sfilt
88
+ await motor.setCoolstepThreshold(1000); // Set CoolStep / stall velocity limit, (tcoolthrs 20 bit) → None
89
+ // active while TCOOLTHRS >= TSTEP >= THIGH
90
+ await motor.configureCoolstep(5, 2, 1, 1, 0); // Configure CoolStep, (semin, semax, seup, sedn, seimin) → None
91
+ // semin = 0 would disable CoolStep
92
+ await motor.enableStallStop(false); // Automatic stop on stall, (enabled) → None
93
+ // SW_MODE.sg_stop; needs TCOOLTHRS first
94
+ await motor.setDcstep(0, 0, 0); // Configure DcStep, (vdcmin, dcTime, dcSg) → None
95
+ // all zero keeps DcStep off
96
+ await motor.configureEncoder(false, false, false, false, false, false, false, false, false, false, false);// Configure the ABN encoder, (polA, polB, polN, ignoreAb, clrCont, clrOnce, posEdge, negEdge, clrEncX, latchXAct, decimal) → None
97
+ // ENCMODE bits
98
+ await motor.setEncoderConstant(1.0); // Set ENC_CONST, (value signed 16.16) → None
99
+ // encoder counts to microsteps
100
+ await motor.setEncoderPosition(0); // Set X_ENC, (counts) → None
101
+ // encoder position
102
+ await motor.setEncoderDeviation(0); // Set ENC_DEVIATION, (steps) → None
103
+ // 0 = off
104
+ await motor.configureGateDriver(0, 4, 0, 0, 0); // Configure DRV_CONF, (bbmtime, bbmclks, otselect, drvstrength, filtIsense) → None
105
+ // board-specific: wrong values can destroy the MOSFETs
106
+ await motor.configureShortDetection(6, 6, 1, 0); // Configure SHORT_CONF, (s2vsLevel, s2gLevel, shortfilter, shortdelay) → None
107
+ // board-specific
108
+ await motor.enable(); // Power stage on, () → None
109
+ // drives DRV_ENN low, waits 150 ms
110
+ await motor.setPosition(0); // Define the position, (position full steps) → None
111
+ // writes XACTUAL and XTARGET
112
+ await motor.moveTo(200); // Positioning move, (position full steps) → None
113
+ // non-blocking
114
+ console.log('reached', await motor.waitUntilReached()); // Wait for the target, (timeoutS) → boolean
115
+ // polls RAMP_STAT
116
+ console.log('position', await motor.position()); // Read actual position, () → number full steps
117
+ // XACTUAL
118
+ console.log('velocity', await motor.velocity()); // Read actual velocity, () → number full steps/s
119
+ // VACTUAL
120
+ console.log('is_reached', await motor.isPositionReached()); // Position reached flag, () → boolean
121
+ // RAMP_STAT.position_reached
122
+ await motor.move(-200, 400); // Blocking relative move, (fullSteps, fullStepsPerS) → None
123
+ // optional speed override
124
+ await motor.run(150); // Velocity mode, (fullStepsPerS) → None
125
+ // sign = direction
126
+ await sleep(1000);
127
+ await motor.setRampMode(1); // Set RAMPMODE, (mode 0–3) → None
128
+ // 1 = velocity mode to +VMAX
129
+ await motor.hold(); // Hold mode, () → None
130
+ // keep the present velocity
131
+ await motor.stop(); // Controlled stop, () → None
132
+ // VMAX = 0
133
+ await motor.waitUntilReached(); // Wait for standstill, (timeoutS) → boolean
134
+ // vzero
135
+ await motor.setTargetRaw(51200); // Write XTARGET raw, (xtarget microsteps) → None
136
+ // starts positioning in mode 0
137
+ await motor.setRampMode(0); // Back to positioning, (mode) → None
138
+ // RAMPMODE = 0
139
+ console.log('ramp', await motor.readRampStatus()); // Read RAMP_STAT, () → object
140
+ // decoded ramp and switch flags
141
+ await motor.clearRampEvents(); // Clear event flags, () → None
142
+ // write-1-to-clear bits
143
+ console.log('switches', await motor.readSwitches()); // Read switch states, () → [boolean, boolean]
144
+ // [left, right]
145
+ console.log('latch', await motor.readLatchedPosition()); // Read XLATCH, () → number full steps
146
+ // position at the last switch event
147
+ console.log('tstep', await motor.readTstep()); // Read step interval, () → number clocks
148
+ // time between 1/256 microsteps
149
+ console.log('mscnt', await motor.microstepPosition()); // Read microstep counter, () → number 0–1023
150
+ // MSCNT
151
+ console.log('currents', await motor.readCoilCurrents()); // Read coil currents, () → [number, number]
152
+ // signed [CUR_A, CUR_B] from MSCURACT
153
+ console.log('scale', await motor.readCurrentScale()); // Read actual current scale, () → number 0–31
154
+ // DRV_STATUS.CS_ACTUAL
155
+ console.log('sg_result', await motor.readStallguard()); // Read StallGuard2 result, () → number 0–1023
156
+ // SG_RESULT, 0 = highest load
157
+ console.log('stalled', await motor.isStalled()); // Read stall flag, () → boolean
158
+ // DRV_STATUS.StallGuard
159
+ console.log('pwm_scale', await motor.readPwmScale()); // Read PWM_SCALE, () → [number, number]
160
+ // [PWM_SCALE_SUM, PWM_SCALE_AUTO]
161
+ console.log('pwm_auto', await motor.readPwmAuto()); // Read PWM_AUTO, () → [number, number]
162
+ // [PWM_OFS_AUTO, PWM_GRAD_AUTO]
163
+ console.log('offset', await motor.readOffset()); // Read OFFSET_READ, () → [number, number]
164
+ // offset calibration of both phases
165
+ console.log('encoder', await motor.readEncoderPosition(), await motor.readEncoderLatch(), await motor.readEncoderStatus());// Read the encoder, () → number, number, object
166
+ // X_ENC, ENC_LATCH, ENC_STATUS
167
+ await motor.clearEncoderStatus(); // Clear encoder events, () → None
168
+ // write-1-to-clear
169
+ console.log('status', await motor.readStatus()); // Read driver status, () → object
170
+ // decoded DRV_STATUS
171
+ console.log('standstill', await motor.isStandstill()); // Read standstill flag, () → boolean
172
+ // DRV_STATUS.stst
173
+ console.log('fault', await motor.readFault()); // Read fault bitmask, () → number
174
+ // FAULT_* constants, 0 = none
175
+ console.log('gstat', await motor.readGlobalStatus()); // Read GSTAT, () → object
176
+ // reset, drv_err, uv_cp; not cleared by the read
177
+ await motor.clearGlobalStatus(); // Clear GSTAT, () → None
178
+ // writes 0x07
179
+ console.log('ioin', await motor.readIoin()); // Read IOIN, () → object
180
+ // raw word, pin states and silicon version
181
+ console.log('gconf', (await motor.readRegister(0x00)).toString(16)); // Read a raw register, (reg) → number
182
+ // any readable register
183
+ await motor.writeRegister(0x13, 500, true); // Write a raw register, (reg, value, verify) → None
184
+ // here TPWMTHRS again, with the mirror check
185
+ await motor.setDirectMode(true); // Direct coil-current mode, (enabled) → None
186
+ // bypasses the ramp generator
187
+ await motor.setCoilCurrents(100, 0); // Write XDIRECT, (coilA, coilB -255..255) → None
188
+ // scaled by IHOLD
189
+ await sleep(500);
190
+ await motor.setDirectMode(false); // Back to the ramp generator, (enabled) → None
191
+ // restores XTARGET
192
+ console.log('diag0', motor.readDiag(0)); // Read DIAG0 pin, (n 0 or 1) → boolean
193
+ // needs the diag pin to be wired (SPI only)
194
+ const customTable = null; // replace with { mslut: [8 words], mslutsel, mslutstart } to program a table
195
+ if (customTable && (await motor.microstepPosition()) === 0) {
196
+ await motor.writeMicrostepTable(customTable.mslut, customTable.mslutsel, customTable.mslutstart); // Program the microstep table, (mslut 8 words, mslutsel, mslutstart) → None
197
+ // MSLUT0..7, MSLUTSEL, MSLUTSTART; call at MSCNT = 0
198
+ }
199
+ for (const call of [() => motor.setSlaveAddress(0), () => motor.setSendDelay(2), () => motor.setNaoPolarity(true), () => motor.readIfcnt()]) {
200
+ try { await call(); } catch (e) { console.log('UART only'); } // setSlaveAddress, setSendDelay, setNaoPolarity and readIfcnt need busType 'uart'
201
+ }
202
+ await motor.disable(); // Power stage off, () → None
203
+ // drives DRV_ENN high; the outputs float
204
+ await spi.close();
205
+ })();
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Demo for the TMC5160 — sensorless homing and positioning of a linear axis.
5
+ *
6
+ * Enables the driver at 1.5 A, proves the link via IOIN.VERSION (0x30) and
7
+ * SD_MODE, switches to SpreadCycle (StallGuard2 does not work in StealthChop),
8
+ * arms the automatic stall stop and runs toward the end stop at 150 full
9
+ * steps/s. The motion controller stops the motor itself on the stall; the demo
10
+ * then defines zero, backs off, programs a two-phase ramp, makes three timed
11
+ * positioning moves and enables CoolStep for the last one. Any fault stops the
12
+ * motor, disables the driver and exits.
13
+ */
14
+
15
+ const { SPIConnection } = require('periph/src/connection/spi');
16
+ const { GpioOutputPin } = require('periph/src/connection/output_pin');
17
+ const { Default } = require('opengpio');
18
+ const { TMC5160Full } = require('periph/src/chips/motor/tmc5160');
19
+
20
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
21
+ const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
22
+ const SPI_DEV = parseInt(process.env.SPI_DEV || '0', 10);
23
+ const HOMING_SPEED = 150;
24
+
25
+ (async () => {
26
+ const spi = new SPIConnection(SPI_BUS, SPI_DEV, { mode: 3, maxSpeedHz: 1_000_000 }); // Create SPI connection, (busNumber, deviceNumber, {mode=3, maxSpeedHz=1 MHz})
27
+ const enPin = new GpioOutputPin(Default.output({ chip: 0, line: 17 })); // Create DRV_ENN pin, (chip, line=17) → OutputPin
28
+ const motor = new TMC5160Full(spi, { enPin }); // Create TMC5160 Full driver, (connection, {busType='spi', rSenseOhm=0.075 Ω, enPin})
29
+ await motor.init(); // Probe the chip and apply defaults, () → None
30
+
31
+ async function guard() {
32
+ // --- Stop on any driver fault ---
33
+ // Short circuits and over-temperature latch until the driver is
34
+ // disabled, so the demo shuts everything down and reports which fault fired.
35
+ const fault = await motor.readFault(); // Read fault bitmask, () → number
36
+ if (fault) {
37
+ await motor.stop(); // Controlled stop, () → None
38
+ await motor.disable(); // Power stage off, () → None
39
+ console.log('fault 0x' + fault.toString(16));
40
+ await spi.close();
41
+ process.exit(1);
42
+ }
43
+ }
44
+
45
+ // --- Bring up the link and the power stage ---
46
+ // IOIN.VERSION and SD_MODE prove the wiring and the strap setting before
47
+ // any motion is attempted.
48
+ const ioin = await motor.readIoin(); // Read IOIN, () → object
49
+ console.log('version 0x' + ioin.version.toString(16), 'SD_MODE', ioin.sd_mode);
50
+ await motor.setCurrent(1.5); // Set run current, (rmsA A RMS) → None
51
+ await motor.enable(); // Power stage on, () → None
52
+
53
+ // --- Arm the automatic stall stop ---
54
+ // StallGuard2 needs SpreadCycle and TSTEP <= TCOOLTHRS; TCOOLTHRS is set
55
+ // just above the TSTEP of the homing speed. sg_stop makes the chip stop by itself.
56
+ await motor.setChopperMode(true); // Choose chopper, (spreadcycle) → None
57
+ await motor.setStallThreshold(0); // Set StallGuard2 threshold, (sgt -64..63) → None
58
+ await motor.setCoolstepThreshold(Math.floor(12e6 / (HOMING_SPEED * 256 / 2))); // Set CoolStep / stall velocity limit, (tcoolthrs 20 bit) → None
59
+ await motor.setAcceleration(600); // Set acceleration, (fullStepsPerS2) → None
60
+
61
+ // --- Run toward the end stop ---
62
+ // The first 0.5 s is spin-up, where stall detection is not yet valid; then
63
+ // sg_stop is enabled and RAMP_STAT.event_stop_sg tells when the chip stopped.
64
+ await motor.run(-HOMING_SPEED); // Velocity mode, (fullStepsPerS) → None
65
+ await sleep(500);
66
+ await motor.enableStallStop(true); // Automatic stop on stall, (enabled) → None
67
+ while (!(await motor.readRampStatus()).event_stop_sg) { // Read RAMP_STAT, () → object
68
+ console.log('SG_RESULT', await motor.readStallguard()); // Read StallGuard2 result, () → number 0–1023
69
+ await guard();
70
+ await sleep(100);
71
+ }
72
+
73
+ // --- End stop found: define zero and back off ---
74
+ // The motion controller has already stopped the motor; clear the event,
75
+ // release sg_stop and move 20 full steps away while printing the ramp.
76
+ await motor.clearRampEvents(); // Clear event flags, () → None
77
+ await motor.enableStallStop(false); // Automatic stop on stall, (enabled) → None
78
+ await motor.setPosition(0); // Define the position, (position full steps) → None
79
+ await motor.setSpeed(300); // Set target speed, (fullStepsPerS) → None
80
+ await motor.moveTo(20); // Positioning move, (position full steps) → None
81
+ while (!(await motor.isPositionReached())) { // Position reached flag, () → boolean
82
+ console.log(await motor.position(), await motor.velocity(), await motor.readStallguard()); // position, velocity, SG_RESULT
83
+ await sleep(50);
84
+ }
85
+
86
+ // --- Two-phase ramp, three timed moves ---
87
+ // A1/D1 apply below V1 = 200 full steps/s, AMAX/DMAX above it.
88
+ await motor.configureRamp(0, 1200, 200, 600, 500, 900, 1500, 10); // Configure the SixPoint ramp, (vstart, a1, v1, amax, vmax, dmax, d1, vstop) → None
89
+ await motor.setChopperMode(false); // Choose chopper, (spreadcycle) → None
90
+ for (let i = 0; i < 3; i++) {
91
+ if (i === 2) await motor.configureCoolstep(5, 2, 1, 1, 0); // Configure CoolStep, (semin, semax, seup, sedn, seimin) → None
92
+ await motor.moveTo(i % 2 === 0 ? 420 : 20); // Positioning move, (position full steps) → None
93
+ await motor.waitUntilReached(); // Wait for the target, (timeoutS) → boolean
94
+ console.log('move', i, 'done, CS_ACTUAL', await motor.readCurrentScale()); // Read actual current scale, () → number 0–31
95
+ await guard();
96
+ }
97
+
98
+ // --- Shut down and report ---
99
+ // GSTAT shows whether the run stressed the driver.
100
+ console.log('status', await motor.readStatus()); // Read driver status, () → object
101
+ console.log('global status', await motor.readGlobalStatus()); // Read GSTAT, () → object
102
+ await motor.disable(); // Power stage off, () → None
103
+ await spi.close();
104
+ })();
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ const { SPIConnection } = require('periph/src/connection/spi');
4
+ const { GpioOutputPin } = require('periph/src/connection/output_pin');
5
+ const { Default } = require('opengpio');
6
+ const { TMC5160Minimal } = require('periph/src/chips/motor/tmc5160');
7
+
8
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
9
+ const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
10
+ const SPI_DEV = parseInt(process.env.SPI_DEV || '0', 10);
11
+
12
+ (async () => {
13
+ const spi = new SPIConnection(SPI_BUS, SPI_DEV, { mode: 3, maxSpeedHz: 1_000_000 }); // Create SPI connection, (busNumber, deviceNumber, {mode=3, maxSpeedHz=1 MHz})
14
+ const enPin = new GpioOutputPin(Default.output({ chip: 0, line: 17 })); // Create DRV_ENN pin, (chip, line=17) → OutputPin
15
+ const motor = new TMC5160Minimal(spi, { enPin }); // Create TMC5160 driver, (connection, {busType='spi', rSenseOhm=0.075 Ω, enPin, address=0, echo=true, multiNode=false, fclkHz=12000000 Hz})
16
+ await motor.init(); // Probe the chip and apply defaults, () → None
17
+
18
+ await motor.setCurrent(1.5); // Set run current, (rmsA A RMS) → None
19
+ await motor.setSpeed(300); // Set target speed, (fullStepsPerS full steps/s) → None
20
+ await motor.setAcceleration(800); // Set acceleration, (fullStepsPerS2 full steps/s²) → None
21
+ await motor.enable(); // Power stage on, () → None
22
+ await motor.moveTo(400); // Start a positioning move, (position full steps) → None
23
+ await motor.waitUntilReached(); // Wait for the target, (timeoutS) → boolean
24
+ console.log('position:', await motor.position()); // Read actual position, () → number full steps
25
+ await motor.move(-400); // Blocking relative move, (fullSteps, fullStepsPerS) → None
26
+ await motor.run(200); // Velocity mode, (fullStepsPerS full steps/s) → None
27
+ await sleep(2000);
28
+ console.log('velocity:', await motor.velocity()); // Read actual velocity, () → number full steps/s
29
+ await motor.stop(); // Controlled stop, () → None
30
+ await motor.waitUntilReached(); // Wait for standstill, (timeoutS) → boolean
31
+ console.log('standstill:', await motor.isStandstill()); // Read standstill flag, () → boolean
32
+ console.log('fault:', await motor.readFault()); // Read fault bitmask, () → number
33
+ await motor.disable(); // Power stage off, () → None
34
+ await spi.close();
35
+ })();