periph 1.5.4 → 1.6.1

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 (73) hide show
  1. package/README.md +17 -0
  2. package/examples/gas/ccs811/complete.js +87 -0
  3. package/examples/gas/ccs811/demo.js +108 -0
  4. package/examples/gas/ccs811/minimal.js +20 -0
  5. package/examples/gas/rrh46410/complete.js +78 -0
  6. package/examples/gas/rrh46410/demo.js +86 -0
  7. package/examples/gas/rrh46410/minimal.js +21 -0
  8. package/examples/gas/rrh62000/complete.js +74 -0
  9. package/examples/gas/rrh62000/demo.js +87 -0
  10. package/examples/gas/rrh62000/minimal.js +22 -0
  11. package/examples/gas/scd30/complete.js +73 -0
  12. package/examples/gas/scd30/demo.js +102 -0
  13. package/examples/gas/scd30/minimal.js +20 -0
  14. package/examples/gas/scd40/complete.js +76 -0
  15. package/examples/gas/scd40/demo.js +109 -0
  16. package/examples/gas/scd40/minimal.js +20 -0
  17. package/examples/light/si1133/complete.js +89 -0
  18. package/examples/light/si1133/demo.js +93 -0
  19. package/examples/light/si1133/minimal.js +22 -0
  20. package/examples/light/si1145/complete.js +93 -0
  21. package/examples/light/si1145/demo.js +98 -0
  22. package/examples/light/si1145/minimal.js +22 -0
  23. package/examples/light/tsl2561/complete.js +74 -0
  24. package/examples/light/tsl2561/demo.js +46 -0
  25. package/examples/light/tsl2561/minimal.js +22 -0
  26. package/examples/light/tsl2591/complete.js +90 -0
  27. package/examples/light/tsl2591/demo.js +119 -0
  28. package/examples/light/tsl2591/minimal.js +22 -0
  29. package/examples/light/vcnl4040/complete.js +102 -0
  30. package/examples/light/vcnl4040/demo.js +125 -0
  31. package/examples/light/vcnl4040/minimal.js +25 -0
  32. package/examples/light/vcnl4200/complete.js +101 -0
  33. package/examples/light/vcnl4200/demo.js +126 -0
  34. package/examples/light/vcnl4200/minimal.js +25 -0
  35. package/examples/pressure/spl06/complete.js +81 -0
  36. package/examples/pressure/spl06/demo.js +106 -0
  37. package/examples/pressure/spl06/minimal.js +22 -0
  38. package/examples/rfid/pn532/complete.js +99 -0
  39. package/examples/rfid/pn532/demo.js +121 -0
  40. package/examples/rfid/pn532/minimal.js +22 -0
  41. package/examples/rfid/st25dv/complete.js +94 -0
  42. package/examples/rfid/st25dv/demo.js +121 -0
  43. package/examples/rfid/st25dv/minimal.js +21 -0
  44. package/examples/rtc/bq32000/complete.js +53 -0
  45. package/examples/rtc/bq32000/demo.js +77 -0
  46. package/examples/rtc/bq32000/minimal.js +20 -0
  47. package/examples/rtc/ds1307/complete.js +54 -0
  48. package/examples/rtc/ds1307/demo.js +96 -0
  49. package/examples/rtc/ds1307/minimal.js +20 -0
  50. package/examples/rtc/ds1371/complete.js +53 -0
  51. package/examples/rtc/ds1371/demo.js +85 -0
  52. package/examples/rtc/ds1371/minimal.js +25 -0
  53. package/package.json +1 -1
  54. package/src/chips/gas/ccs811.js +400 -0
  55. package/src/chips/gas/rrh46410.js +534 -0
  56. package/src/chips/gas/rrh62000.js +418 -0
  57. package/src/chips/gas/scd30.js +368 -0
  58. package/src/chips/gas/scd40.js +451 -0
  59. package/src/chips/light/si1133.js +583 -0
  60. package/src/chips/light/si1145.js +630 -0
  61. package/src/chips/light/tsl2561.js +445 -0
  62. package/src/chips/light/tsl2591.js +561 -0
  63. package/src/chips/light/vcnl4040.js +576 -0
  64. package/src/chips/light/vcnl4200.js +592 -0
  65. package/src/chips/pressure/spl06.js +461 -0
  66. package/src/chips/rfid/pn532.js +513 -0
  67. package/src/chips/rfid/st25dv.js +628 -0
  68. package/src/chips/rtc/bq32000.js +334 -0
  69. package/src/chips/rtc/ds1307.js +327 -0
  70. package/src/chips/rtc/ds1371.js +453 -0
  71. package/src/connection/i2c.js +46 -13
  72. package/src/connection/smbus.js +21 -8
  73. package/src/discovery/registry.js +17 -17
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { BQ32000Full } = require('periph/src/chips/rtc/bq32000');
5
+
6
+ (async () => {
7
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
8
+ const connection = new I2CConnection(bus, 0x68);
9
+ const rtc = new BQ32000Full(connection); // Create BQ32000 Full driver, (connection)
10
+ await rtc.init(); // Confirm presence, () → None
11
+ // reads the clock registers once, no writes
12
+
13
+ await rtc.setDatetime(2026, 11, 28, 6, 14, 30, 0); // Set the calendar clock, (year 2000–2099, month, day, weekday 0=Sun, hour, minute, second) → None
14
+ // one 7-byte write; STOP = 0, OF = 0, century off
15
+ const dt = await rtc.getDatetime(); // Read the calendar clock, () → {year, month, day, weekday, hour, minute, second}
16
+ // decodes the seven BCD registers 0x00–0x06
17
+ const failed = await rtc.oscillatorFailed(); // Query oscillator-fail flag, () → bool
18
+ // true after the first power-up or a crystal dropout
19
+ await rtc.clearOscillatorFail(); // Clear oscillator-fail flag, () → None
20
+ // read-modify-write of MINUTES bit 7
21
+ await rtc.stopOscillator(); // Stop the oscillator, () → None
22
+ // STOP = 1, the clock no longer advances
23
+ await rtc.startOscillator(); // Start the oscillator, () → None
24
+ // STOP = 0; 1 then 0 force-starts a fresh chip
25
+
26
+ await rtc.setCentury(true, 0); // Write century bits, (enable, century=0) → None
27
+ // CENT toggles when the year rolls 99 → 00
28
+ const century = await rtc.getCentury(); // Read century bits, () → {enabled, century}
29
+
30
+ await rtc.setCalibration(12.2); // Set calibration, (ppm −126.5…+63.5) → None
31
+ // nearest step: CAL = 6, S = 0 → +12.21 ppm (slows the RTC)
32
+ const ppm = await rtc.getCalibration(); // Read calibration, () → ppm
33
+
34
+ await rtc.setCalibrationOutput(1); // Output test signal on IRQ, (hz 512|1|null) → None
35
+ // 1 Hz includes the ppm adjustment (SF key sequence for FTF)
36
+ await rtc.setCalibrationOutput(null); // Turn the test signal off, (null) → None
37
+ await rtc.setIrq(true); // Drive IRQ as a level, (level) → None
38
+ // FT = 0, OUT = 1: open drain released (pull-up to VCC)
39
+
40
+ await rtc.enableTrickleCharge(false); // Enable trickle charger, (fetBypass=false) → None
41
+ // supercap only — never with a primary coin cell
42
+ await rtc.disableTrickleCharge(); // Disable trickle charger, () → None
43
+ // TCHE = 0xA, TCH2 = 0, TCFE = 0
44
+
45
+ const cfg1 = await rtc.readRegister(0x07); // Read raw register, (reg 0x00–0x09) → int
46
+ await rtc.writeRegister(0x07, cfg1); // Write raw register, (reg 0x00–0x09, value) → None
47
+ // reserved bits of 0x08/0x09 are preserved
48
+
49
+ console.log(dt, `oscillatorFailed=${failed}`);
50
+ console.log('century', century, `calibration ${ppm.toFixed(2)} ppm`, `CAL_CFG1=0x${cfg1.toString(16).padStart(2, '0')}`);
51
+
52
+ await connection.close();
53
+ })();
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ // Demo for the BQ32000 — a backup clock that keeps time without main power.
4
+ //
5
+ // Checks the oscillator-fail flag and seeds the clock from the host time on first use, prints
6
+ // the time for 10 s, trims the oscillator by +12.2 ppm, outputs the 1 Hz calibration signal on
7
+ // IRQ for 5 s, and invites you to pull VCC to watch the clock keep running from the backup
8
+ // supply. Pass --supercap to enable the trickle charger (only with a supercap).
9
+
10
+ const { I2CConnection } = require('periph/src/connection/i2c');
11
+ const { BQ32000Full } = require('periph/src/chips/rtc/bq32000');
12
+
13
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
14
+ const pad = (n) => String(n).padStart(2, '0');
15
+ const SUPERCAP = process.argv.includes('--supercap');
16
+
17
+ (async () => {
18
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
19
+ const connection = new I2CConnection(bus, 0x68);
20
+ const rtc = new BQ32000Full(connection); // Create BQ32000 Full driver, (connection)
21
+ await rtc.init(); // Confirm presence, () → None
22
+
23
+ const show = async () => {
24
+ const dt = await rtc.getDatetime(); // Read the calendar clock, () → {year, month, day, weekday, hour, minute, second}
25
+ console.log(`${dt.year}-${pad(dt.month)}-${pad(dt.day)} ${pad(dt.hour)}:${pad(dt.minute)}:${pad(dt.second)}`);
26
+ };
27
+
28
+ // --- Is the stored time valid? ---
29
+ // OF is always set after the very first power-up and after any oscillator dropout. Then the
30
+ // oscillator is force-started (STOP 1 → 0) and the clock seeded from the host time;
31
+ // setDatetime also clears OF.
32
+ const failed = await rtc.oscillatorFailed(); // Query oscillator-fail flag, () → bool
33
+ console.log('oscillatorFailed:', failed);
34
+ if (failed) {
35
+ await rtc.stopOscillator(); // Stop the oscillator, () → None
36
+ await rtc.startOscillator(); // Start the oscillator, () → None
37
+ const t = new Date();
38
+ await rtc.setDatetime(t.getFullYear(), t.getMonth() + 1, t.getDate(), t.getDay(), // Set the calendar clock, (year, month, day, weekday 0=Sun, hour, minute, second) → None
39
+ t.getHours(), t.getMinutes(), t.getSeconds());
40
+ console.log('clock seeded from host time');
41
+ }
42
+
43
+ // --- Watch it tick ---
44
+ for (let i = 0; i < 10; i++) {
45
+ await show();
46
+ await sleep(1000);
47
+ }
48
+
49
+ // --- Trim the crystal ---
50
+ // +12.2 ppm slows the clock by the nearest step (6 × 2.03 ppm = +12.21 ppm). Verify the
51
+ // direction with a frequency counter on the 1 Hz output.
52
+ await rtc.setCalibration(12.2); // Set calibration, (ppm −126.5…+63.5) → None
53
+ console.log(`calibration: ${(await rtc.getCalibration()).toFixed(2)} ppm`); // Read calibration, () → ppm
54
+
55
+ // --- 1 Hz calibration signal on IRQ for 5 s ---
56
+ // Connect a frequency counter or scope to IRQ (pull-up to VCC). The 1 Hz output includes the
57
+ // ppm adjustment; 512 Hz would not.
58
+ await rtc.setCalibrationOutput(1); // Output test signal on IRQ, (hz 512|1|null) → None
59
+ await sleep(5000);
60
+ await rtc.setCalibrationOutput(null); // Turn the test signal off, (null) → None
61
+
62
+ // --- Optional: charge a supercap ---
63
+ if (SUPERCAP) {
64
+ await rtc.enableTrickleCharge(); // Enable trickle charger, (fetBypass=false) → None
65
+ console.log('trickle charger enabled (supercap)');
66
+ }
67
+
68
+ // --- Backup switchover ---
69
+ // Manual step: disconnect VCC for a few seconds while VBACK (coin cell or supercap) stays
70
+ // connected, then reconnect it. The clock keeps running; wait > 1 s after VCC returns before
71
+ // trusting the registers.
72
+ console.log('disconnect VCC briefly now, then reconnect it ...');
73
+ await sleep(16100);
74
+ await show();
75
+
76
+ await connection.close();
77
+ })();
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { BQ32000Minimal } = require('periph/src/chips/rtc/bq32000');
5
+
6
+ (async () => {
7
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
8
+ const connection = new I2CConnection(bus, 0x68);
9
+ const rtc = new BQ32000Minimal(connection); // Create BQ32000 driver, (connection)
10
+ await rtc.init(); // Confirm presence (reads the clock, no writes), () → None
11
+
12
+ const pad = (n) => String(n).padStart(2, '0');
13
+ for (let i = 0; i < 10; i++) {
14
+ const dt = await rtc.getDatetime(); // Read the calendar clock, () → {year, month, day, weekday, hour, minute, second}
15
+ console.log(`${dt.year}-${pad(dt.month)}-${pad(dt.day)} ${pad(dt.hour)}:${pad(dt.minute)}:${pad(dt.second)}`);
16
+ await new Promise((r) => setTimeout(r, 1000));
17
+ }
18
+
19
+ await connection.close();
20
+ })();
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { DS1307Full } = require('periph/src/chips/rtc/ds1307');
5
+
6
+ (async () => {
7
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
8
+ const connection = new I2CConnection(bus, 0x68);
9
+ const rtc = new DS1307Full(connection); // Create DS1307 Full driver, (connection)
10
+ await rtc.init(); // Confirm presence, () → None
11
+ // reads SECONDS once, no writes
12
+
13
+ await rtc.setDatetime(2026, 11, 29, 7, 14, 30, 0); // Set the calendar clock, (year 2000–2099, month, day, weekday 1=Mon…7=Sun, hour, minute, second) → None
14
+ // one 7-byte write; CH = 0 starts the oscillator, 24-hour mode
15
+ const dt = await rtc.getDatetime(); // Read the calendar clock, () → {year, month, day, weekday, hour, minute, second}
16
+ // one burst read of 0x00–0x06; hour is always 0–23
17
+ const running = await rtc.isRunning(); // Query oscillator, () → bool
18
+ // true when SECONDS.CH = 0
19
+ const ok = await rtc.valid(); // Query time validity, () → bool
20
+ // false after a first power-up or a lost backup cell
21
+ await rtc.halt(); // Stop the oscillator, () → None
22
+ // CH = 1: minimises the backup current
23
+ await rtc.start(); // Start the oscillator, () → None
24
+ // CH = 0, the seconds value is kept
25
+
26
+ await rtc.setTime(9, 5, 0); // Set the time, (hour 0–23, minute, second) → None
27
+ // keeps CH and the current 12/24-hour mode
28
+ const time = await rtc.getTime(); // Read the time, () → {hour, minute, second}
29
+ await rtc.setDate(2026, 11, 30, 1); // Set the date, (year, month, day, weekday 1=Mon…7=Sun) → None
30
+ const date = await rtc.getDate(); // Read the date, () → {year, month, day, weekday}
31
+
32
+ await rtc.setHourMode(true); // Select 12/24-hour mode, (hour12) → None
33
+ // re-enters the current hour in the new format
34
+ await rtc.setHourMode(false); // Back to 24-hour mode, (hour12) → None
35
+
36
+ await rtc.setSquareWave(1); // Enable square wave, (frequencyHz 1|4096|8192|32768|null) → None
37
+ // SQWE = 1; open-drain SQW/OUT needs a pull-up
38
+ await rtc.setSquareWave(null); // Disable square wave, (null) → None
39
+ // SQWE = 0, the pin follows OUT
40
+ await rtc.setOutput(true); // Drive SQW/OUT as a level, (level) → None
41
+ // SQWE = 0, OUT = 1: open drain released
42
+ const control = await rtc.getControl(); // Read CONTROL, () → int
43
+ // OUT bit 7, SQWE bit 4, RS bits 1:0
44
+
45
+ await rtc.writeRam(0, Buffer.from('Periph')); // Write battery-backed RAM, (address 0–55, data) → None
46
+ // starts at register 0x08 + address, no wrap past 55
47
+ const data = await rtc.readRam(0, 6); // Read battery-backed RAM, (address 0–55, length) → Buffer
48
+
49
+ console.log(dt, `running=${running} valid=${ok}`);
50
+ console.log(time, date);
51
+ console.log(`CONTROL=0x${control.toString(16).padStart(2, '0')}`, data.toString());
52
+
53
+ await connection.close();
54
+ })();
@@ -0,0 +1,96 @@
1
+ 'use strict';
2
+
3
+ // Demo for the DS1307 — a battery-backed clock with a scratch-pad RAM.
4
+ //
5
+ // Checks whether the clock holds a valid time and sets a fixed date and time if it was halted,
6
+ // prints the time every second for 10 s, switches to 12-hour mode and back, halts and restarts
7
+ // the oscillator, outputs a 1 Hz square wave and then a static high/low level on SQW/OUT,
8
+ // stores a short message in the battery-backed RAM and reads it back, and finally invites you
9
+ // to cycle the main supply to see that time and RAM survive on the backup cell (skipped unless
10
+ // --battery is passed).
11
+
12
+ const { I2CConnection } = require('periph/src/connection/i2c');
13
+ const { DS1307Full } = require('periph/src/chips/rtc/ds1307');
14
+
15
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
16
+ const pad = (n) => String(n).padStart(2, '0');
17
+ const hex = (n) => `0x${n.toString(16).toUpperCase().padStart(2, '0')}`;
18
+ const BATTERY = process.argv.includes('--battery');
19
+
20
+ (async () => {
21
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
22
+ const connection = new I2CConnection(bus, 0x68);
23
+ const rtc = new DS1307Full(connection); // Create DS1307 Full driver, (connection)
24
+ await rtc.init(); // Confirm presence, () → None
25
+
26
+ const show = async () => {
27
+ const dt = await rtc.getDatetime(); // Read the calendar clock, () → {year, month, day, weekday, hour, minute, second}
28
+ console.log(`${dt.year}-${pad(dt.month)}-${pad(dt.day)} ${pad(dt.hour)}:${pad(dt.minute)}:${pad(dt.second)}`);
29
+ };
30
+
31
+ // --- Is the stored time valid? ---
32
+ // A fresh chip (or one whose backup cell was missing) powers up with CH = 1: the oscillator
33
+ // is stopped and the time is meaningless. setDatetime writes all seven registers in one
34
+ // transaction and clears CH, starting the clock.
35
+ if (!(await rtc.valid())) { // Query time validity, () → bool
36
+ await rtc.setDatetime(2026, 11, 29, 7, 12, 0, 0); // Set the calendar clock, (year, month, day, weekday 1=Mon…7=Sun, hour, minute, second) → None
37
+ console.log('clock was halted: set to 2026-11-29 12:00:00');
38
+ }
39
+ console.log('running:', await rtc.isRunning()); // Query oscillator, () → bool
40
+
41
+ // --- Watch it tick ---
42
+ for (let i = 0; i < 10; i++) {
43
+ await show();
44
+ await sleep(1000);
45
+ }
46
+
47
+ // --- 12-hour mode ---
48
+ // The chip can count in 12-hour AM/PM format; the driver still returns 0–23, so only the
49
+ // raw HOURS register changes (bit 6 = 12-hour, bit 5 = PM).
50
+ await rtc.setHourMode(true); // Select 12/24-hour mode, (hour12) → None
51
+ console.log('12-hour mode, time', await rtc.getTime()); // Read the time, () → {hour, minute, second}
52
+ await rtc.setHourMode(false); // Back to 24-hour mode, (hour12) → None
53
+
54
+ // --- Halt and restart ---
55
+ // While halted (CH = 1) the time stands still and the backup current drops.
56
+ await rtc.halt(); // Stop the oscillator, () → None
57
+ const before = await rtc.getTime();
58
+ await sleep(2000);
59
+ console.log('halted for 2 s:', before, '->', await rtc.getTime());
60
+ await rtc.start(); // Start the oscillator, () → None
61
+
62
+ // --- Correct the time and date separately ---
63
+ const now = await rtc.getTime();
64
+ await rtc.setTime(now.hour, now.minute, 0); // Set the time, (hour, minute, second) → None
65
+ await rtc.setDate(2026, 11, 29, 7); // Set the date, (year, month, day, weekday) → None
66
+ console.log('date:', await rtc.getDate()); // Read the date, () → {year, month, day, weekday}
67
+
68
+ // --- SQW/OUT ---
69
+ // Connect an LED (with resistor) or a scope to SQW/OUT with a pull-up. At 1 Hz the LED
70
+ // blinks; the clock registers update on its falling edge.
71
+ await rtc.setSquareWave(1); // Enable square wave, (frequencyHz 1|4096|8192|32768|null) → None
72
+ console.log('1 Hz square wave for 5 s, CONTROL =', hex(await rtc.getControl())); // Read CONTROL, () → int
73
+ await sleep(5000);
74
+ await rtc.setOutput(false); // Drive SQW/OUT as a level, (level) → None
75
+ console.log('static low for 2 s');
76
+ await sleep(2000);
77
+ await rtc.setOutput(true);
78
+ console.log('static high (released), CONTROL =', hex(await rtc.getControl()));
79
+
80
+ // --- Battery-backed RAM ---
81
+ // 56 bytes survive a power loss as long as the backup cell is fitted.
82
+ await rtc.writeRam(0, Buffer.from('hello DS1307')); // Write battery-backed RAM, (address 0–55, data) → None
83
+ console.log('RAM:', (await rtc.readRam(0, 12)).toString()); // Read battery-backed RAM, (address 0–55, length) → Buffer
84
+
85
+ // --- Power cycle (only with a backup cell) ---
86
+ // Manual step: remove the 5 V supply for a few seconds while the 3 V cell on VBAT stays
87
+ // connected, then restore it. Time keeps running and the RAM keeps the message.
88
+ if (BATTERY) {
89
+ console.log('remove VCC briefly now, then restore it ...');
90
+ await sleep(15000);
91
+ await show();
92
+ console.log('RAM after power cycle:', (await rtc.readRam(0, 12)).toString());
93
+ }
94
+
95
+ await connection.close();
96
+ })();
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { DS1307Minimal } = require('periph/src/chips/rtc/ds1307');
5
+
6
+ (async () => {
7
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
8
+ const connection = new I2CConnection(bus, 0x68);
9
+ const rtc = new DS1307Minimal(connection); // Create DS1307 driver, (connection)
10
+ await rtc.init(); // Confirm presence (reads SECONDS, no writes), () → None
11
+
12
+ const pad = (n) => String(n).padStart(2, '0');
13
+ for (let i = 0; i < 10; i++) {
14
+ const dt = await rtc.getDatetime(); // Read the calendar clock, () → {year, month, day, weekday, hour, minute, second}
15
+ console.log(`${dt.year}-${pad(dt.month)}-${pad(dt.day)} ${pad(dt.hour)}:${pad(dt.minute)}:${pad(dt.second)}`);
16
+ await new Promise((r) => setTimeout(r, 1000));
17
+ }
18
+
19
+ await connection.close();
20
+ })();
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { DS1371Full } = require('periph/src/chips/rtc/ds1371');
5
+
6
+ (async () => {
7
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
8
+ const connection = new I2CConnection(bus, DS1371Full.I2C_ADDRESS);
9
+ const rtc = new DS1371Full(connection); // Create DS1371 Full driver, (connection)
10
+ await rtc.init(); // Confirm device presence, () → None
11
+
12
+ await rtc.setSeconds(1798761600); // Write 32-bit counter, (seconds) → None
13
+ // one 4-byte write; resets the divider chain and clears OSF
14
+ const seconds = await rtc.getSeconds(); // Read 32-bit counter, () → number s
15
+ await rtc.setDatetime(2027, 1, 1, 5, 12, 0, 0); // Write UTC date/time, (year, month, day, weekday, hour, minute, second) → None
16
+ // converted to Unix seconds; weekday is only range-checked
17
+ const dt = await rtc.getDatetime(); // Read UTC date/time, () → {year, month, day, weekday, hour, minute, second}
18
+
19
+ const stopped = await rtc.oscillatorStopped(); // Read oscillator stop flag, () → boolean
20
+ await rtc.clearOscillatorFlag(); // Clear OSF, () → None
21
+ await rtc.stopOscillator(); // Stop oscillator (EOSC = 1), () → None
22
+ // low-power standby; the counter halts and OSF is set
23
+ await rtc.startOscillator(); // Start oscillator (EOSC = 0), () → None
24
+ const control = await rtc.getControl(); // Read CONTROL, () → number
25
+
26
+ await rtc.setSquareWave(1); // Square wave on SQW/INT, (frequencyHz: 1/4096/8192/32768/null) → None
27
+ // INTCN = 0; null selects interrupt mode with AIE = 0 (pin idle)
28
+
29
+ await rtc.disableAlarm(); // Disable counter (WACE = AIE = 0), () → None
30
+ await rtc.writeRam([0x12, 0x34, 0x56]); // Write the 3 counter bytes as RAM, (data: 3 bytes) → None
31
+ const ram = await rtc.readRam(); // Read the 3 counter bytes as RAM, () → Buffer
32
+
33
+ await rtc.setAlarm(5, true); // Periodic alarm, (seconds: 1..16777215, enableInterrupt = true) → None
34
+ // AF is set every 5 s; SQW/INT low until AF is cleared
35
+ const flag = await rtc.alarmFlag(); // Read AF, () → boolean
36
+ await rtc.clearAlarmFlag(); // Clear AF, () → None
37
+
38
+ await rtc.setWatchdog(2048); // Arm watchdog, (ticks: 1..16777215) → None
39
+ // timeout = ticks / 4096 s = 0.5 s; WDS must be low
40
+ await rtc.feedWatchdog(); // Reload watchdog, () → None
41
+ const ticks = await rtc.getWatchdog(); // Read counter seed, () → number
42
+ // reading the counter also reloads the watchdog
43
+
44
+ await rtc.onInterrupt((status) => console.log(`alarm status=0x${status.toString(16)}`)); // Subscribe to interrupts, (callback) → None
45
+ // callback gets the pre-clear STATUS byte when AF was set
46
+ await rtc.enableInterrupt(DS1371Full.SOURCE_ALARM); // Alarm/watchdog mode + INTCN/AIE/WACE, (source) → None
47
+ const status = await rtc.pollInterrupt(); // Read STATUS and clear AF, () → number
48
+ await rtc.disableInterrupt(DS1371Full.SOURCE_ALARM); // Clear AIE, WACE and INTCN, (source) → None
49
+ await rtc.offInterrupt(); // Unsubscribe, () → None
50
+
51
+ console.log({ seconds, dt, stopped, control, ram, flag, ticks, status });
52
+ await connection.close();
53
+ })();
@@ -0,0 +1,85 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { DS1371Full } = require('periph/src/chips/rtc/ds1371');
5
+
6
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
7
+
8
+ function iso(dt) {
9
+ const p = (v) => String(v).padStart(2, '0');
10
+ return `${dt.year}-${p(dt.month)}-${p(dt.day)}T${p(dt.hour)}:${p(dt.minute)}:${p(dt.second)}Z`;
11
+ }
12
+
13
+ (async () => {
14
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
15
+ const connection = new I2CConnection(bus, DS1371Full.I2C_ADDRESS);
16
+ const rtc = new DS1371Full(connection); // Create DS1371 Full driver, (connection)
17
+ await rtc.init(); // Confirm device presence, () → None
18
+
19
+ // --- A stopped oscillator means the counter is meaningless ---
20
+ // OSF is set at first power-up and after any oscillator stop. The counter
21
+ // has no epoch of its own, so seed it with the host's Unix time.
22
+ if (await rtc.oscillatorStopped()) { // Read OSF, () → boolean
23
+ await rtc.setSeconds(Math.floor(Date.now() / 1000)); // Write counter, (seconds) → None
24
+ console.log('oscillator was stopped - counter set from host time');
25
+ }
26
+
27
+ // --- Elapsed-time clock: the counter is plain Unix time ---
28
+ for (let i = 0; i < 10; i++) {
29
+ console.log(await rtc.getSeconds(), iso(await rtc.getDatetime())); // Read counter, () → number; read UTC date/time, () → object
30
+ await sleep(1000);
31
+ }
32
+
33
+ // --- 1 Hz square wave on SQW/INT (restarted by every counter write) ---
34
+ await rtc.setSquareWave(1); // Square wave, (frequencyHz) → None
35
+ console.log(`1 Hz square wave on SQW/INT, CONTROL=0x${(await rtc.getControl()).toString(16)}`); // Read CONTROL, () → number
36
+ await sleep(3000);
37
+
38
+ // --- Periodic alarm: AF every 5 s, SQW/INT low until cleared ---
39
+ let fired = false;
40
+ await rtc.onInterrupt(() => { fired = true; }); // Subscribe, (callback) → None
41
+ await rtc.setAlarm(5, true); // Periodic alarm, (seconds, enableInterrupt) → None
42
+ await rtc.enableInterrupt(DS1371Full.SOURCE_ALARM); // Alarm mode + INTCN/AIE/WACE, (source) → None
43
+ const deadline = Date.now() + 7000;
44
+ while (!fired && Date.now() < deadline) await sleep(50);
45
+ console.log(fired ? 'alarm fired' : 'alarm did not fire');
46
+ await rtc.offInterrupt(); // Unsubscribe, () → None
47
+ if ((await rtc.pollInterrupt()) & DS1371Full.STATUS_ALARM) console.log('AF was still set - cleared'); // Read STATUS and clear AF, () → number
48
+ if (await rtc.alarmFlag()) await rtc.clearAlarmFlag(); // Read AF, () → boolean; clear AF, () → None
49
+ await rtc.disableInterrupt(DS1371Full.SOURCE_ALARM); // Clear AIE/WACE/INTCN, (source) → None
50
+
51
+ // --- Watchdog: 2048 ticks = 0.5 s, fed three times, then left to expire ---
52
+ await rtc.setWatchdog(2048); // Arm watchdog, (ticks) → None
53
+ for (let i = 0; i < 3; i++) {
54
+ await sleep(200);
55
+ await rtc.feedWatchdog(); // Reload watchdog, () → None
56
+ console.log('fed watchdog, AF =', await rtc.alarmFlag());
57
+ }
58
+ console.log('watchdog seed', await rtc.getWatchdog(), 'ticks'); // Read counter seed (also feeds), () → number
59
+ await sleep(600);
60
+ console.log('watchdog expired, AF =', await rtc.alarmFlag()); // AF stays set during the 250 ms pulse
61
+ await sleep(300);
62
+ console.log('after the 250 ms pulse, AF =', await rtc.alarmFlag()); // AF clears itself in watchdog mode
63
+ await rtc.disableInterrupt(DS1371Full.SOURCE_WATCHDOG); // Clear AIE/WACE/INTCN, (source) → None
64
+
65
+ // --- With the counter disabled its three bytes are RAM ---
66
+ await rtc.disableAlarm(); // WACE = AIE = 0, () → None
67
+ await rtc.writeRam([0xCA, 0xFE, 0x42]); // Write RAM, (data: 3 bytes) → None
68
+ console.log('RAM', await rtc.readRam()); // Read RAM, () → Buffer
69
+
70
+ // --- Stopping the oscillator halts the counter and sets OSF ---
71
+ await rtc.stopOscillator(); // EOSC = 1, () → None
72
+ await sleep(1000);
73
+ console.log('OSF after stop:', await rtc.oscillatorStopped());
74
+ await rtc.startOscillator(); // EOSC = 0, () → None
75
+ await rtc.clearOscillatorFlag(); // Clear OSF, () → None
76
+ console.log('OSF after restart and clear:', await rtc.oscillatorStopped());
77
+
78
+ // --- setDatetime takes calendar fields instead of seconds ---
79
+ const now = new Date();
80
+ await rtc.setDatetime(now.getUTCFullYear(), now.getUTCMonth() + 1, now.getUTCDate(), // Write UTC date/time, (year, month, day, weekday, hour, minute, second) → None
81
+ ((now.getUTCDay() + 6) % 7) + 1, now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
82
+ console.log('counter reset to host time', iso(await rtc.getDatetime()));
83
+
84
+ await connection.close();
85
+ })();
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { DS1371Minimal } = require('periph/src/chips/rtc/ds1371');
5
+
6
+ function iso(dt) {
7
+ const p = (v) => String(v).padStart(2, '0');
8
+ return `${dt.year}-${p(dt.month)}-${p(dt.day)}T${p(dt.hour)}:${p(dt.minute)}:${p(dt.second)}Z`;
9
+ }
10
+
11
+ (async () => {
12
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
13
+ const connection = new I2CConnection(bus, DS1371Minimal.I2C_ADDRESS);
14
+ const rtc = new DS1371Minimal(connection); // Create DS1371 driver, (connection)
15
+ await rtc.init(); // Confirm device presence, () → None
16
+
17
+ for (let i = 0; i < 10; i++) {
18
+ const seconds = await rtc.getSeconds(); // Read 32-bit counter, () → number s since 1970-01-01 UTC
19
+ const dt = await rtc.getDatetime(); // Read UTC date/time, () → {year, month, day, weekday, hour, minute, second}
20
+ console.log(`${seconds} ${iso(dt)}`);
21
+ await new Promise((r) => setTimeout(r, 1000));
22
+ }
23
+
24
+ await connection.close();
25
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "periph",
3
- "version": "1.5.4",
3
+ "version": "1.6.1",
4
4
  "description": "JavaScript drivers for peripheral chips (I2C, SPI)",
5
5
  "main": "src/index.js",
6
6
  "keywords": [