periph 1.3.2 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -0
- package/examples/accelerometer/bma180/complete.js +83 -0
- package/examples/accelerometer/bma180/demo.js +47 -0
- package/examples/accelerometer/bma180/minimal.js +21 -0
- package/examples/accelerometer/fxos8700/complete.js +111 -0
- package/examples/accelerometer/fxos8700/demo.js +80 -0
- package/examples/accelerometer/fxos8700/minimal.js +20 -0
- package/examples/accelerometer/lis3dh/complete.js +123 -0
- package/examples/accelerometer/lis3dh/demo.js +99 -0
- package/examples/accelerometer/lis3dh/minimal.js +19 -0
- package/examples/accelerometer/lsm303/complete.js +82 -0
- package/examples/accelerometer/lsm303/demo.js +96 -0
- package/examples/accelerometer/lsm303/minimal.js +23 -0
- package/examples/accelerometer/mma8451q/complete.js +96 -0
- package/examples/accelerometer/mma8451q/demo.js +120 -0
- package/examples/accelerometer/mma8451q/minimal.js +20 -0
- package/examples/adc_dac/ad5243/complete.js +52 -0
- package/examples/adc_dac/ad5243/demo.js +50 -0
- package/examples/adc_dac/ad5243/minimal.js +23 -0
- package/examples/adc_dac/ad5248/complete.js +49 -0
- package/examples/adc_dac/ad5248/demo.js +42 -0
- package/examples/adc_dac/ad5248/minimal.js +21 -0
- package/examples/adc_dac/ads1013/complete.js +51 -0
- package/examples/adc_dac/ads1013/demo.js +51 -0
- package/examples/adc_dac/ads1013/minimal.js +21 -0
- package/examples/adc_dac/ads1014/complete.js +88 -0
- package/examples/adc_dac/ads1014/demo.js +52 -0
- package/examples/adc_dac/ads1014/minimal.js +21 -0
- package/examples/adc_dac/ads1113/complete.js +47 -0
- package/examples/adc_dac/ads1113/demo.js +39 -0
- package/examples/adc_dac/ads1113/minimal.js +20 -0
- package/examples/adc_dac/ads1114/complete.js +77 -0
- package/examples/adc_dac/ads1114/demo.js +47 -0
- package/examples/adc_dac/ads1114/minimal.js +20 -0
- package/examples/adc_dac/ads1115/complete.js +82 -0
- package/examples/adc_dac/ads1115/demo.js +55 -0
- package/examples/adc_dac/ads1115/minimal.js +20 -0
- package/examples/adc_dac/es8311/complete.js +80 -0
- package/examples/adc_dac/es8311/demo.js +59 -0
- package/examples/adc_dac/es8311/minimal.js +18 -0
- package/examples/adc_dac/mcp4xxx/complete.js +97 -0
- package/examples/adc_dac/mcp4xxx/demo.js +76 -0
- package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
- package/examples/display/pcf8578/complete.js +58 -0
- package/examples/display/pcf8578/demo.js +62 -0
- package/examples/display/pcf8578/minimal.js +19 -0
- package/examples/display/pcf8579/complete.js +58 -0
- package/examples/display/pcf8579/demo.js +61 -0
- package/examples/display/pcf8579/minimal.js +19 -0
- package/examples/imu/icm20948/complete.js +114 -0
- package/examples/imu/icm20948/demo.js +79 -0
- package/examples/imu/icm20948/minimal.js +22 -0
- package/examples/imu/qmi8658a/complete.js +134 -0
- package/examples/imu/qmi8658a/demo.js +81 -0
- package/examples/imu/qmi8658a/minimal.js +22 -0
- package/examples/io_expander/mcp23008/complete.js +70 -0
- package/examples/io_expander/mcp23008/demo.js +58 -0
- package/examples/io_expander/mcp23008/minimal.js +20 -0
- package/examples/memory/mb85rc/complete.js +40 -0
- package/examples/memory/mb85rc/demo.js +58 -0
- package/examples/memory/mb85rc/minimal.js +15 -0
- package/examples/motor/drv2605/complete.js +100 -0
- package/examples/motor/drv2605/demo.js +55 -0
- package/examples/motor/drv2605/minimal.js +24 -0
- package/examples/motor/tmc2130/complete.js +138 -0
- package/examples/motor/tmc2130/demo.js +96 -0
- package/examples/motor/tmc2130/minimal.js +30 -0
- package/examples/motor/tmc2208/complete.js +92 -0
- package/examples/motor/tmc2208/demo.js +85 -0
- package/examples/motor/tmc2208/minimal.js +25 -0
- package/examples/motor/tmc2209/complete.js +106 -0
- package/examples/motor/tmc2209/demo.js +97 -0
- package/examples/motor/tmc2209/minimal.js +25 -0
- package/examples/motor/tmc5160/complete.js +205 -0
- package/examples/motor/tmc5160/demo.js +104 -0
- package/examples/motor/tmc5160/minimal.js +35 -0
- package/examples/power/axp2101/complete.js +135 -0
- package/examples/power/axp2101/demo.js +68 -0
- package/examples/power/axp2101/minimal.js +24 -0
- package/examples/rtc/pcf85063a/complete.js +68 -0
- package/examples/rtc/pcf85063a/demo.js +76 -0
- package/examples/rtc/pcf85063a/minimal.js +20 -0
- package/package.json +1 -1
- package/src/chips/accelerometer/bma180.js +573 -0
- package/src/chips/accelerometer/fxos8700.js +1600 -0
- package/src/chips/accelerometer/lis3dh.js +1341 -0
- package/src/chips/accelerometer/lsm303.js +1471 -0
- package/src/chips/accelerometer/mma8451q.js +1406 -0
- package/src/chips/adc_dac/_ads111x_base.js +272 -0
- package/src/chips/adc_dac/ad5243.js +231 -0
- package/src/chips/adc_dac/ad5248.js +194 -0
- package/src/chips/adc_dac/ads1013.js +219 -0
- package/src/chips/adc_dac/ads1014.js +509 -0
- package/src/chips/adc_dac/ads1113.js +112 -0
- package/src/chips/adc_dac/ads1114.js +201 -0
- package/src/chips/adc_dac/ads1115.js +232 -0
- package/src/chips/adc_dac/es8311.js +621 -0
- package/src/chips/adc_dac/mcp4xxx.js +982 -0
- package/src/chips/display/pcf8578.js +452 -0
- package/src/chips/display/pcf8579.js +451 -0
- package/src/chips/imu/icm20948.js +849 -0
- package/src/chips/imu/qmi8658a.js +1011 -0
- package/src/chips/io_expander/mcp23008.js +558 -0
- package/src/chips/memory/mb85rc.js +166 -0
- package/src/chips/motor/drv2605.js +592 -0
- package/src/chips/motor/tmc2130.js +537 -0
- package/src/chips/motor/tmc2208.js +490 -0
- package/src/chips/motor/tmc2209.js +565 -0
- package/src/chips/motor/tmc5160.js +900 -0
- package/src/chips/power/axp2101.js +968 -0
- package/src/chips/rtc/pcf85063a.js +482 -0
- package/src/connection/uart.js +9 -0
- package/src/connection/uart_mock.js +65 -0
- package/src/discovery/registry.js +45 -19
|
@@ -0,0 +1,968 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AXP2101 single-cell Li-ion NVDC power-management IC (X-Powers).
|
|
5
|
+
*
|
|
6
|
+
* Switch charger (up to 1.5 A), four buck converters and eleven LDOs, a 14-bit
|
|
7
|
+
* ADC, a fuel gauge, a power-key controller, watchdog, JEITA protection and a
|
|
8
|
+
* 24-source interrupt controller. I²C address 0x34. Charger, rail and
|
|
9
|
+
* power-key settings are partly eFuse-programmed per product, so nothing but
|
|
10
|
+
* the ADC channels and the fuel gauge is touched at construction and every
|
|
11
|
+
* setter is a read-modify-write that preserves the bits it does not own.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const _REG_STATUS1 = 0x00;
|
|
15
|
+
const _REG_STATUS2 = 0x01;
|
|
16
|
+
const _REG_DATA_BUFFER0 = 0x04;
|
|
17
|
+
const _REG_COMMON_CONFIG = 0x10;
|
|
18
|
+
const _REG_BATFET_CTRL = 0x12;
|
|
19
|
+
const _REG_DIE_TEMP_CTRL = 0x13;
|
|
20
|
+
const _REG_MIN_VSYS = 0x14;
|
|
21
|
+
const _REG_VINDPM = 0x15;
|
|
22
|
+
const _REG_IINLIM = 0x16;
|
|
23
|
+
const _REG_GAUGE_RESET = 0x17;
|
|
24
|
+
const _REG_MODULE_ENABLE = 0x18;
|
|
25
|
+
const _REG_WATCHDOG_CTRL = 0x19;
|
|
26
|
+
const _REG_LOWBAT_WARN = 0x1A;
|
|
27
|
+
const _REG_GPIO1_CTRL = 0x1B;
|
|
28
|
+
const _REG_PWRON_STATUS = 0x20;
|
|
29
|
+
const _REG_PWROFF_STATUS = 0x21;
|
|
30
|
+
const _REG_PWROFF_EN = 0x22;
|
|
31
|
+
const _REG_DCDC_PROT = 0x23;
|
|
32
|
+
const _REG_VOFF = 0x24;
|
|
33
|
+
const _REG_PWROK_CTRL = 0x25;
|
|
34
|
+
const _REG_SLEEP_CTRL = 0x26;
|
|
35
|
+
const _REG_PWRKEY_LEVEL = 0x27;
|
|
36
|
+
const _REG_FAST_PWRON0 = 0x28;
|
|
37
|
+
const _REG_ADC_ENABLE = 0x30;
|
|
38
|
+
const _REG_VBAT_H = 0x34;
|
|
39
|
+
const _REG_TS_H = 0x36;
|
|
40
|
+
const _REG_VBUS_H = 0x38;
|
|
41
|
+
const _REG_VSYS_H = 0x3A;
|
|
42
|
+
const _REG_TDIE_H = 0x3C;
|
|
43
|
+
const _REG_IRQ_EN0 = 0x40;
|
|
44
|
+
const _REG_IRQ_STATUS0 = 0x48;
|
|
45
|
+
const _REG_TS_CTRL = 0x50;
|
|
46
|
+
const _REG_TS_HYS_L2H = 0x52;
|
|
47
|
+
const _REG_TS_HYS_H2L = 0x53;
|
|
48
|
+
const _REG_VLTF_CHG = 0x54;
|
|
49
|
+
const _REG_VHTF_CHG = 0x55;
|
|
50
|
+
const _REG_VLTF_WORK = 0x56;
|
|
51
|
+
const _REG_VHTF_WORK = 0x57;
|
|
52
|
+
const _REG_JEITA_EN = 0x58;
|
|
53
|
+
const _REG_JEITA_CV = 0x59;
|
|
54
|
+
const _REG_JEITA_COOL = 0x5A;
|
|
55
|
+
const _REG_JEITA_WARM = 0x5B;
|
|
56
|
+
const _REG_IPRECHG = 0x61;
|
|
57
|
+
const _REG_ICC = 0x62;
|
|
58
|
+
const _REG_ITERM = 0x63;
|
|
59
|
+
const _REG_CV = 0x64;
|
|
60
|
+
const _REG_THERMAL_REG = 0x65;
|
|
61
|
+
const _REG_CHG_TIMER = 0x67;
|
|
62
|
+
const _REG_BAT_DETECT = 0x68;
|
|
63
|
+
const _REG_CHGLED_CTRL = 0x69;
|
|
64
|
+
const _REG_BTN_BAT_VTERM = 0x6A;
|
|
65
|
+
const _REG_DCDC_EN = 0x80;
|
|
66
|
+
const _REG_DCDC_PWM = 0x81;
|
|
67
|
+
const _REG_BAT_PERCENT = 0xA4;
|
|
68
|
+
|
|
69
|
+
// name -> [enable register, enable bit, voltage register, kind]
|
|
70
|
+
const _RAILS = {
|
|
71
|
+
dcdc1: [0x80, 0, 0x82, 'dcdc1'],
|
|
72
|
+
dcdc2: [0x80, 1, 0x83, 'dcdc23'],
|
|
73
|
+
dcdc3: [0x80, 2, 0x84, 'dcdc23'],
|
|
74
|
+
dcdc4: [0x80, 3, 0x85, 'dcdc4'],
|
|
75
|
+
aldo1: [0x90, 0, 0x92, 'ldo100'],
|
|
76
|
+
aldo2: [0x90, 1, 0x93, 'ldo100'],
|
|
77
|
+
aldo3: [0x90, 2, 0x94, 'ldo100'],
|
|
78
|
+
aldo4: [0x90, 3, 0x95, 'ldo100'],
|
|
79
|
+
bldo1: [0x90, 4, 0x96, 'ldo100'],
|
|
80
|
+
bldo2: [0x90, 5, 0x97, 'ldo100'],
|
|
81
|
+
cpusldo: [0x90, 6, 0x98, 'ldo50'],
|
|
82
|
+
dldo1: [0x90, 7, 0x99, 'dldo1'],
|
|
83
|
+
dldo2: [0x91, 0, 0x9A, 'ldo50'],
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// kind -> [start V, step V, max code]
|
|
87
|
+
const _RAIL_KINDS = {
|
|
88
|
+
dcdc1: [1.5, 0.1, 19],
|
|
89
|
+
ldo100: [0.5, 0.1, 30],
|
|
90
|
+
ldo50: [0.5, 0.05, 19],
|
|
91
|
+
dldo1: [0.5, 0.1, 28],
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const _ICC_LIMIT = { 1: 1.5, 2: 1.5, 3: 1.4, 4: 1.1, 5: 1.0 };
|
|
95
|
+
const _CV_CODES = [[4.0, 1], [4.1, 2], [4.2, 3], [4.35, 4], [4.4, 5]];
|
|
96
|
+
const _IINLIM_CODES = [[0.1, 0], [0.5, 1], [0.9, 2], [1.0, 3], [1.5, 4], [2.0, 5]];
|
|
97
|
+
|
|
98
|
+
// rail -> [register offset from 0x28, shift]
|
|
99
|
+
const _FAST_RAILS = {
|
|
100
|
+
dcdc1: [0, 0], dcdc2: [0, 2], dcdc3: [0, 4], dcdc4: [0, 6],
|
|
101
|
+
aldo1: [1, 2], aldo2: [1, 4], aldo3: [1, 6],
|
|
102
|
+
aldo4: [2, 0], bldo1: [2, 2], bldo2: [2, 4], cpusldo: [2, 6],
|
|
103
|
+
dldo1: [3, 0], dldo2: [3, 2],
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
function _delayMs(ms) {
|
|
107
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function _round(x) {
|
|
111
|
+
return Math.floor(x + 0.5);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function _stepCode(value, start, step, maxCode, what) {
|
|
115
|
+
const code = _round((value - start) / step);
|
|
116
|
+
if (code < 0 || code > maxCode || Math.abs(value - (start + code * step)) > step / 2 + 1e-9) {
|
|
117
|
+
throw new RangeError(what + ' out of range: ' + value);
|
|
118
|
+
}
|
|
119
|
+
return code;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function _lookup(value, table, what) {
|
|
123
|
+
for (const [key, code] of table) {
|
|
124
|
+
if (Math.abs(key - value) < 1e-6) return code;
|
|
125
|
+
}
|
|
126
|
+
throw new RangeError(what + ' unsupported value: ' + value);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function _railCode(kind, volts, name) {
|
|
130
|
+
if (_RAIL_KINDS[kind]) {
|
|
131
|
+
const [start, step, maxCode] = _RAIL_KINDS[kind];
|
|
132
|
+
return _stepCode(volts, start, step, maxCode, name + ' voltage');
|
|
133
|
+
}
|
|
134
|
+
const top = kind === 'dcdc23' ? 87 : 102;
|
|
135
|
+
if (volts <= 1.21) return _stepCode(volts, 0.5, 0.01, 70, name + ' voltage');
|
|
136
|
+
return 71 + _stepCode(volts, 1.22, 0.02, top - 71, name + ' voltage');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function _railVolts(kind, code) {
|
|
140
|
+
if (_RAIL_KINDS[kind]) {
|
|
141
|
+
const [start, step] = _RAIL_KINDS[kind];
|
|
142
|
+
return start + code * step;
|
|
143
|
+
}
|
|
144
|
+
if (code <= 70) return 0.5 + code * 0.01;
|
|
145
|
+
return 1.22 + (code - 71) * 0.02;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function _railMask(kind) {
|
|
149
|
+
return (kind === 'dcdc23' || kind === 'dcdc4') ? 0x7F : 0x1F;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* AXP2101 PMIC — minimal interface: battery and power-source monitoring.
|
|
154
|
+
*
|
|
155
|
+
* Constructor caveat: JS constructors cannot be async, so the init sequence
|
|
156
|
+
* (bus check, ADC channels on, fuel gauge on, 20 ms wait) is started
|
|
157
|
+
* unawaited. Every method waits for it and rethrows its failure, so an absent
|
|
158
|
+
* device is reported by the first call (`await chip.ready` works too).
|
|
159
|
+
*/
|
|
160
|
+
class AXP2101Minimal {
|
|
161
|
+
/**
|
|
162
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C connection bound to address 0x34.
|
|
163
|
+
*/
|
|
164
|
+
constructor(connection) {
|
|
165
|
+
this._conn = connection;
|
|
166
|
+
this._ready = this._init();
|
|
167
|
+
this._ready.catch(() => {});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** @returns {Promise<void>} Resolves when the init sequence has finished (rejects if the bus check failed). */
|
|
171
|
+
get ready() {
|
|
172
|
+
return this._ready;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async _init() {
|
|
176
|
+
try {
|
|
177
|
+
await this._rawRead(_REG_STATUS1, 1);
|
|
178
|
+
} catch (e) {
|
|
179
|
+
throw new Error('AXP2101 not responding at I2C 0x34: ' + e.message);
|
|
180
|
+
}
|
|
181
|
+
const adc = (await this._rawRead(_REG_ADC_ENABLE, 1))[0];
|
|
182
|
+
await this._rawWrite(_REG_ADC_ENABLE, adc | 0x1F);
|
|
183
|
+
const mod = (await this._rawRead(_REG_MODULE_ENABLE, 1))[0];
|
|
184
|
+
await this._rawWrite(_REG_MODULE_ENABLE, mod | 0x08);
|
|
185
|
+
await _delayMs(20);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async _rawRead(reg, n) {
|
|
189
|
+
try {
|
|
190
|
+
return await this._conn.readReg(reg, n);
|
|
191
|
+
} catch (e) {
|
|
192
|
+
return this._conn.readReg(reg, n);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async _rawWrite(reg, value) {
|
|
197
|
+
try {
|
|
198
|
+
await this._conn.writeReg(reg, value);
|
|
199
|
+
} catch (e) {
|
|
200
|
+
await this._conn.writeReg(reg, value);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async _readReg(reg) {
|
|
205
|
+
await this._ready;
|
|
206
|
+
return (await this._rawRead(reg, 1))[0];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async _writeReg(reg, value) {
|
|
210
|
+
await this._ready;
|
|
211
|
+
await this._rawWrite(reg, value & 0xFF);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async _rmw(reg, mask, value) {
|
|
215
|
+
const cur = await this._readReg(reg);
|
|
216
|
+
await this._writeReg(reg, (cur & ~mask) | (value & mask));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async _command(reg, bit) {
|
|
220
|
+
const cur = await this._readReg(reg);
|
|
221
|
+
await this._writeReg(reg, cur | bit);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async _adc14(reg) {
|
|
225
|
+
const hi = await this._readReg(reg);
|
|
226
|
+
const lo = await this._readReg(reg + 1);
|
|
227
|
+
return ((hi & 0x3F) << 8) | lo;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** @returns {Promise<number>} Battery voltage in V (1 mV / LSB). */
|
|
231
|
+
async batteryVoltage() {
|
|
232
|
+
return (await this._adc14(_REG_VBAT_H)) * 0.001;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** @returns {Promise<number>} Fuel-gauge state of charge, 0-100 %. */
|
|
236
|
+
async batteryPercent() {
|
|
237
|
+
return (await this._readReg(_REG_BAT_PERCENT)) & 0x7F;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** @returns {Promise<number>} One of the CHARGE_* constants (STATUS2 bits 2:0). */
|
|
241
|
+
async chargeState() {
|
|
242
|
+
return (await this._readReg(_REG_STATUS2)) & 0x07;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** @returns {Promise<boolean>} True when a good input source is connected (STATUS1.VBUS_GOOD). */
|
|
246
|
+
async vbusPresent() {
|
|
247
|
+
return ((await this._readReg(_REG_STATUS1)) & 0x20) !== 0;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Charge states returned by chargeState(). */
|
|
252
|
+
AXP2101Minimal.CHARGE_TRICKLE = 0;
|
|
253
|
+
AXP2101Minimal.CHARGE_PRE = 1;
|
|
254
|
+
AXP2101Minimal.CHARGE_CC = 2;
|
|
255
|
+
AXP2101Minimal.CHARGE_CV = 3;
|
|
256
|
+
AXP2101Minimal.CHARGE_DONE = 4;
|
|
257
|
+
AXP2101Minimal.CHARGE_NONE = 5;
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* AXP2101 PMIC — full interface.
|
|
261
|
+
*
|
|
262
|
+
* Adds the remaining ADC channels, charger, battery-temperature, power rail,
|
|
263
|
+
* power-key, sleep, watchdog, fuel-gauge and interrupt control.
|
|
264
|
+
*
|
|
265
|
+
* Dangerous operations: disabling or re-voltaging the rail that powers the
|
|
266
|
+
* host (commonly DCDC1 or DCDC3), powerOff(), the power-off thresholds, charge
|
|
267
|
+
* voltage / current beyond the cell's limits and setBatfetOffEnable() can
|
|
268
|
+
* power the board down or damage the battery.
|
|
269
|
+
*/
|
|
270
|
+
class AXP2101Full extends AXP2101Minimal {
|
|
271
|
+
/**
|
|
272
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C connection bound to address 0x34.
|
|
273
|
+
*/
|
|
274
|
+
constructor(connection) {
|
|
275
|
+
super(connection);
|
|
276
|
+
this._callback = null;
|
|
277
|
+
this._edgeHandler = null;
|
|
278
|
+
this._pollTimer = null;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** @returns {Promise<number>} VBUS voltage in V. */
|
|
282
|
+
async vbusVoltage() {
|
|
283
|
+
return (await this._adc14(_REG_VBUS_H)) * 0.001;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** @returns {Promise<number>} System (VSYS) voltage in V. */
|
|
287
|
+
async systemVoltage() {
|
|
288
|
+
return (await this._adc14(_REG_VSYS_H)) * 0.001;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** @returns {Promise<number>} TS pin voltage in V (0.5 mV / LSB). */
|
|
292
|
+
async tsVoltage() {
|
|
293
|
+
return (await this._adc14(_REG_TS_H)) * 0.0005;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Die temperature using the vendor XPowersLib formula (not from the datasheet; verify on hardware).
|
|
298
|
+
* @returns {Promise<number>} Die temperature in °C.
|
|
299
|
+
*/
|
|
300
|
+
async dieTemperature() {
|
|
301
|
+
return 22 + (7274 - (await this._adc14(_REG_TDIE_H))) / 20;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Read every measurement.
|
|
306
|
+
* @returns {Promise<{batteryVoltage:number, vbusVoltage:number, systemVoltage:number, tsVoltage:number,
|
|
307
|
+
* dieTemperature:number, batteryPercent:number, chargeState:number, vbusPresent:boolean}>}
|
|
308
|
+
*/
|
|
309
|
+
async readAll() {
|
|
310
|
+
return {
|
|
311
|
+
batteryVoltage: await this.batteryVoltage(),
|
|
312
|
+
vbusVoltage: await this.vbusVoltage(),
|
|
313
|
+
systemVoltage: await this.systemVoltage(),
|
|
314
|
+
tsVoltage: await this.tsVoltage(),
|
|
315
|
+
dieTemperature: await this.dieTemperature(),
|
|
316
|
+
batteryPercent: await this.batteryPercent(),
|
|
317
|
+
chargeState: await this.chargeState(),
|
|
318
|
+
vbusPresent: await this.vbusPresent(),
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Decode STATUS1 / STATUS2.
|
|
324
|
+
* @returns {Promise<{vbusGood:boolean, batfetOpen:boolean, batteryPresent:boolean, batteryActive:boolean,
|
|
325
|
+
* thermalRegulation:boolean, currentLimit:boolean, batteryDirection:string, systemOn:boolean,
|
|
326
|
+
* vindpm:boolean, chargeState:number}>} batteryDirection is 'standby', 'charge', 'discharge' or 'reserved'.
|
|
327
|
+
*/
|
|
328
|
+
async status() {
|
|
329
|
+
const s1 = await this._readReg(_REG_STATUS1);
|
|
330
|
+
const s2 = await this._readReg(_REG_STATUS2);
|
|
331
|
+
return {
|
|
332
|
+
vbusGood: (s1 & 0x20) !== 0,
|
|
333
|
+
batfetOpen: (s1 & 0x10) !== 0,
|
|
334
|
+
batteryPresent: (s1 & 0x08) !== 0,
|
|
335
|
+
batteryActive: (s1 & 0x04) !== 0,
|
|
336
|
+
thermalRegulation: (s1 & 0x02) !== 0,
|
|
337
|
+
currentLimit: (s1 & 0x01) !== 0,
|
|
338
|
+
batteryDirection: ['standby', 'charge', 'discharge', 'reserved'][(s2 >> 5) & 3],
|
|
339
|
+
systemOn: (s2 & 0x10) !== 0,
|
|
340
|
+
vindpm: (s2 & 0x08) !== 0,
|
|
341
|
+
chargeState: s2 & 0x07,
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Select the enabled ADC channels (ADC_ENABLE).
|
|
347
|
+
* @param {boolean} [vbat=true] @param {boolean} [ts=true] @param {boolean} [vbus=true]
|
|
348
|
+
* @param {boolean} [vsys=true] @param {boolean} [tdie=true] @param {boolean} [gpadc=false]
|
|
349
|
+
* @returns {Promise<void>}
|
|
350
|
+
*/
|
|
351
|
+
async setAdcChannels(vbat = true, ts = true, vbus = true, vsys = true, tdie = true, gpadc = false) {
|
|
352
|
+
const value = (vbat ? 1 : 0) | (ts ? 2 : 0) | (vbus ? 4 : 0) | (vsys ? 8 : 0) | (tdie ? 16 : 0) | (gpadc ? 32 : 0);
|
|
353
|
+
await this._rmw(_REG_ADC_ENABLE, 0x3F, value);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Slow (64 s) ADC sampling when powered off on battery only (TS_H bit 7).
|
|
358
|
+
* @param {boolean} enabled
|
|
359
|
+
* @returns {Promise<void>}
|
|
360
|
+
*/
|
|
361
|
+
async setAdcLowFrequency(enabled) {
|
|
362
|
+
await this._rmw(_REG_TS_H, 0x80, enabled ? 0x80 : 0);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Enable or disable main-cell charging (MODULE_ENABLE bit 1).
|
|
367
|
+
* @param {boolean} enabled
|
|
368
|
+
* @returns {Promise<void>}
|
|
369
|
+
*/
|
|
370
|
+
async setCharging(enabled) {
|
|
371
|
+
await this._rmw(_REG_MODULE_ENABLE, 0x02, enabled ? 0x02 : 0);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Set the charge target voltage.
|
|
376
|
+
* @param {number} volts - 4.0, 4.1, 4.2, 4.35 or 4.4 V (the last two only for high-voltage cells).
|
|
377
|
+
* @returns {Promise<void>}
|
|
378
|
+
* @throws {RangeError} For any other value.
|
|
379
|
+
*/
|
|
380
|
+
async setChargeVoltage(volts) {
|
|
381
|
+
await this._rmw(_REG_CV, 0x07, _lookup(volts, _CV_CODES, 'charge voltage'));
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Set the constant-current charge current.
|
|
386
|
+
* @param {number} amps - 0-0.2 A in 25 mA steps, 0.2-1.5 A in 100 mA steps; capped by the programmed charge voltage.
|
|
387
|
+
* @returns {Promise<void>}
|
|
388
|
+
* @throws {RangeError} Out of range or above the charge-voltage limit.
|
|
389
|
+
*/
|
|
390
|
+
async setChargeCurrent(amps) {
|
|
391
|
+
if (amps < 0 || amps > 1.5 + 1e-9) throw new RangeError('charge current out of range: ' + amps);
|
|
392
|
+
const cv = (await this._readReg(_REG_CV)) & 0x07;
|
|
393
|
+
const limit = _ICC_LIMIT[cv];
|
|
394
|
+
if (limit !== undefined && amps > limit + 1e-9) {
|
|
395
|
+
throw new RangeError('charge current ' + amps + ' A exceeds ' + limit + ' A limit for this charge voltage');
|
|
396
|
+
}
|
|
397
|
+
const code = amps <= 0.25 ? Math.min(8, _round(amps / 0.025)) : 8 + _round((amps - 0.2) / 0.1);
|
|
398
|
+
await this._rmw(_REG_ICC, 0x1F, code);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Set the pre-charge current.
|
|
403
|
+
* @param {number} amps - 0-0.2 A in 25 mA steps.
|
|
404
|
+
* @returns {Promise<void>}
|
|
405
|
+
*/
|
|
406
|
+
async setPrechargeCurrent(amps) {
|
|
407
|
+
await this._rmw(_REG_IPRECHG, 0x0F, _stepCode(amps, 0, 0.025, 8, 'precharge current'));
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Set the charge termination current.
|
|
412
|
+
* @param {number} amps - 0-0.2 A in 25 mA steps.
|
|
413
|
+
* @param {boolean} [enabled=true] - Termination enable.
|
|
414
|
+
* @returns {Promise<void>}
|
|
415
|
+
*/
|
|
416
|
+
async setTermination(amps, enabled = true) {
|
|
417
|
+
const code = _stepCode(amps, 0, 0.025, 8, 'termination current');
|
|
418
|
+
await this._rmw(_REG_ITERM, 0x1F, code | (enabled ? 0x10 : 0));
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Set the input current limit.
|
|
423
|
+
* @param {number} amps - 0.1, 0.5, 0.9, 1.0, 1.5 or 2.0 A.
|
|
424
|
+
* @returns {Promise<void>}
|
|
425
|
+
*/
|
|
426
|
+
async setInputCurrentLimit(amps) {
|
|
427
|
+
await this._rmw(_REG_IINLIM, 0x07, _lookup(amps, _IINLIM_CODES, 'input current limit'));
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Set the input voltage limit (VINDPM).
|
|
432
|
+
* @param {number} volts - 3.88-5.08 V in 80 mV steps.
|
|
433
|
+
* @returns {Promise<void>}
|
|
434
|
+
*/
|
|
435
|
+
async setInputVoltageLimit(volts) {
|
|
436
|
+
await this._rmw(_REG_VINDPM, 0x0F, _stepCode(volts, 3.88, 0.08, 15, 'input voltage limit'));
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Set the minimum system voltage.
|
|
441
|
+
* @param {number} volts - 3.2-3.9 V in 100 mV steps.
|
|
442
|
+
* @returns {Promise<void>}
|
|
443
|
+
*/
|
|
444
|
+
async setMinSystemVoltage(volts) {
|
|
445
|
+
await this._rmw(_REG_MIN_VSYS, 0x07, _stepCode(volts, 3.2, 0.1, 7, 'minimum system voltage'));
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Configure the charger safety timers (CHG_TIMER).
|
|
450
|
+
* @param {number} [prechargeMin=60] - 40 / 50 / 60 / 70 min.
|
|
451
|
+
* @param {number} [doneHours=12] - 5 / 8 / 12 / 20 h.
|
|
452
|
+
* @param {boolean} [prechargeEnabled=true]
|
|
453
|
+
* @param {boolean} [doneEnabled=true]
|
|
454
|
+
* @param {boolean} [slowInDpm=true] - Half-speed timers during input DPM / thermal regulation.
|
|
455
|
+
* @returns {Promise<void>}
|
|
456
|
+
*/
|
|
457
|
+
async setChargeTimers(prechargeMin = 60, doneHours = 12, prechargeEnabled = true, doneEnabled = true, slowInDpm = true) {
|
|
458
|
+
const pre = _lookup(prechargeMin, [[40, 0], [50, 1], [60, 2], [70, 3]], 'precharge timer');
|
|
459
|
+
const done = _lookup(doneHours, [[5, 0], [8, 1], [12, 2], [20, 3]], 'done timer');
|
|
460
|
+
const value = pre | (prechargeEnabled ? 0x04 : 0) | (done << 4) | (doneEnabled ? 0x40 : 0) | (slowInDpm ? 0x80 : 0);
|
|
461
|
+
await this._rmw(_REG_CHG_TIMER, 0xF7, value);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Set the charger thermal-regulation threshold.
|
|
466
|
+
* @param {number} celsius - 60, 80, 100 or 120 °C.
|
|
467
|
+
* @returns {Promise<void>}
|
|
468
|
+
*/
|
|
469
|
+
async setThermalRegulation(celsius) {
|
|
470
|
+
await this._rmw(_REG_THERMAL_REG, 0x03, _lookup(celsius, [[60, 0], [80, 1], [100, 2], [120, 3]], 'thermal regulation'));
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Configure die over-temperature level 1 (DIE_TEMP_CTRL).
|
|
475
|
+
* @param {number} [level1Celsius=125] - 115, 125 or 135 °C.
|
|
476
|
+
* @param {boolean} [detect=true] - Die temperature detection enable.
|
|
477
|
+
* @returns {Promise<void>}
|
|
478
|
+
*/
|
|
479
|
+
async setDieProtection(level1Celsius = 125, detect = true) {
|
|
480
|
+
const code = _lookup(level1Celsius, [[115, 0], [125, 1], [135, 2]], 'die protection');
|
|
481
|
+
await this._rmw(_REG_DIE_TEMP_CTRL, 0x07, (code << 1) | (detect ? 1 : 0));
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Enable or disable battery detection (BAT_DETECT).
|
|
486
|
+
* @param {boolean} enabled
|
|
487
|
+
* @returns {Promise<void>}
|
|
488
|
+
*/
|
|
489
|
+
async setBatteryDetection(enabled) {
|
|
490
|
+
await this._rmw(_REG_BAT_DETECT, 0x01, enabled ? 1 : 0);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Configure charging of the backup (coin) cell.
|
|
495
|
+
* @param {boolean} enabled
|
|
496
|
+
* @param {number} [volts=2.9] - Termination voltage, 2.6-3.3 V in 100 mV steps.
|
|
497
|
+
* @returns {Promise<void>}
|
|
498
|
+
*/
|
|
499
|
+
async setBackupCharging(enabled, volts = 2.9) {
|
|
500
|
+
const code = _stepCode(volts, 2.6, 0.1, 7, 'backup charge voltage');
|
|
501
|
+
await this._rmw(_REG_BTN_BAT_VTERM, 0x07, code);
|
|
502
|
+
await this._rmw(_REG_MODULE_ENABLE, 0x04, enabled ? 0x04 : 0);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Configure the CHGLED pin.
|
|
507
|
+
* @param {string} [mode='type_a'] - 'off', 'type_a', 'type_b' or 'manual'.
|
|
508
|
+
* @param {string} [output='hiz'] - For 'manual': 'hiz', 'blink_1hz', 'blink_4hz' or 'low'.
|
|
509
|
+
* @returns {Promise<void>}
|
|
510
|
+
*/
|
|
511
|
+
async setChgled(mode = 'type_a', output = 'hiz') {
|
|
512
|
+
const modes = { off: 0x00, type_a: 0x01, type_b: 0x03, manual: 0x05 };
|
|
513
|
+
const outs = { hiz: 0, blink_1hz: 1, blink_4hz: 2, low: 3 };
|
|
514
|
+
if (!(mode in modes)) throw new RangeError('unknown CHGLED mode: ' + mode);
|
|
515
|
+
if (!(output in outs)) throw new RangeError('unknown CHGLED output: ' + output);
|
|
516
|
+
let value = modes[mode];
|
|
517
|
+
if (mode === 'manual') value |= outs[output] << 4;
|
|
518
|
+
await this._rmw(_REG_CHGLED_CTRL, 0x37, value);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Configure the TS pin (TS_CTRL).
|
|
523
|
+
* @param {boolean} [batterySensor=true] - Battery thermistor (true) or external input (false).
|
|
524
|
+
* @param {number} [currentUa=50] - 20 / 40 / 50 / 60 µA.
|
|
525
|
+
* @param {string} [source='when_adc'] - 'off', 'when_adc', 'when_ts' or 'always'.
|
|
526
|
+
* @returns {Promise<void>}
|
|
527
|
+
*/
|
|
528
|
+
async configureTs(batterySensor = true, currentUa = 50, source = 'when_adc') {
|
|
529
|
+
const sources = { off: 0, when_adc: 1, when_ts: 2, always: 3 };
|
|
530
|
+
if (!(source in sources)) throw new RangeError('unknown TS source: ' + source);
|
|
531
|
+
const cur = _lookup(currentUa, [[20, 0], [40, 1], [50, 2], [60, 3]], 'TS current');
|
|
532
|
+
await this._rmw(_REG_TS_CTRL, 0x1F, cur | (sources[source] << 2) | (batterySensor ? 0 : 0x10));
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Set the battery-temperature limits as TS pin voltages.
|
|
537
|
+
* @param {number} chargeCold - V, 32 mV steps. @param {number} chargeHot - V, 2 mV steps.
|
|
538
|
+
* @param {number} workCold - V, 32 mV steps. @param {number} workHot - V, 2 mV steps.
|
|
539
|
+
* @returns {Promise<void>}
|
|
540
|
+
*/
|
|
541
|
+
async setTsLimits(chargeCold, chargeHot, workCold, workHot) {
|
|
542
|
+
const codes = [
|
|
543
|
+
[_REG_VLTF_CHG, _stepCode(chargeCold, 0, 0.032, 255, 'charge cold limit')],
|
|
544
|
+
[_REG_VHTF_CHG, _stepCode(chargeHot, 0, 0.002, 255, 'charge hot limit')],
|
|
545
|
+
[_REG_VLTF_WORK, _stepCode(workCold, 0, 0.032, 255, 'work cold limit')],
|
|
546
|
+
[_REG_VHTF_WORK, _stepCode(workHot, 0, 0.002, 255, 'work hot limit')],
|
|
547
|
+
];
|
|
548
|
+
for (const [reg, code] of codes) await this._writeReg(reg, code);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Set the TS threshold hysteresis.
|
|
553
|
+
* @param {number} [lowToNormal=0.032] - V, 16 mV steps.
|
|
554
|
+
* @param {number} [highToNormal=0.004] - V, 4 mV steps.
|
|
555
|
+
* @returns {Promise<void>}
|
|
556
|
+
*/
|
|
557
|
+
async setTsHysteresis(lowToNormal = 0.032, highToNormal = 0.004) {
|
|
558
|
+
const l2h = _stepCode(lowToNormal, 0, 0.016, 255, 'TS low hysteresis');
|
|
559
|
+
const h2l = _stepCode(highToNormal, 0, 0.004, 255, 'TS high hysteresis');
|
|
560
|
+
await this._writeReg(_REG_TS_HYS_L2H, l2h);
|
|
561
|
+
await this._writeReg(_REG_TS_HYS_H2L, h2l);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Configure the JEITA charging profile.
|
|
566
|
+
* @param {boolean} enabled
|
|
567
|
+
* @param {?number} [coolVoltage=null] - Cool threshold T2 as TS voltage in V (16 mV steps); null keeps it.
|
|
568
|
+
* @param {?number} [warmVoltage=null] - Warm threshold T3 as TS voltage in V (8 mV steps); null keeps it.
|
|
569
|
+
* @param {boolean} [coolHalfCurrent=true] @param {boolean} [warmHalfCurrent=false]
|
|
570
|
+
* @param {number} [coolVoltageDrop=0] - 0-2 gears. @param {number} [warmVoltageDrop=1] - 0-2 gears.
|
|
571
|
+
* @returns {Promise<void>}
|
|
572
|
+
*/
|
|
573
|
+
async configureJeita(enabled, coolVoltage = null, warmVoltage = null, coolHalfCurrent = true,
|
|
574
|
+
warmHalfCurrent = false, coolVoltageDrop = 0, warmVoltageDrop = 1) {
|
|
575
|
+
if (![0, 1, 2].includes(coolVoltageDrop) || ![0, 1, 2].includes(warmVoltageDrop)) {
|
|
576
|
+
throw new RangeError('JEITA voltage drop must be 0-2');
|
|
577
|
+
}
|
|
578
|
+
const value = coolVoltageDrop | (warmVoltageDrop << 2) | (coolHalfCurrent ? 0x10 : 0) | (warmHalfCurrent ? 0x40 : 0);
|
|
579
|
+
await this._rmw(_REG_JEITA_CV, 0x5F, value);
|
|
580
|
+
if (coolVoltage !== null && coolVoltage !== undefined) {
|
|
581
|
+
await this._writeReg(_REG_JEITA_COOL, _stepCode(coolVoltage, 0, 0.016, 255, 'JEITA cool'));
|
|
582
|
+
}
|
|
583
|
+
if (warmVoltage !== null && warmVoltage !== undefined) {
|
|
584
|
+
await this._writeReg(_REG_JEITA_WARM, _stepCode(warmVoltage, 0, 0.008, 255, 'JEITA warm'));
|
|
585
|
+
}
|
|
586
|
+
await this._rmw(_REG_JEITA_EN, 0x01, enabled ? 1 : 0);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
_rail(name) {
|
|
590
|
+
if (!Object.prototype.hasOwnProperty.call(_RAILS, name)) throw new RangeError('unknown rail: ' + name);
|
|
591
|
+
return _RAILS[name];
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* Read the state of a power rail.
|
|
596
|
+
* @param {string} name - 'dcdc1'..'dcdc4', 'aldo1'..'aldo4', 'bldo1', 'bldo2', 'cpusldo', 'dldo1' or 'dldo2'.
|
|
597
|
+
* @returns {Promise<[boolean, number]>} `[enabled, volts]`.
|
|
598
|
+
*/
|
|
599
|
+
async getRail(name) {
|
|
600
|
+
const [enReg, enBit, vReg, kind] = this._rail(name);
|
|
601
|
+
const enabled = ((await this._readReg(enReg)) & (1 << enBit)) !== 0;
|
|
602
|
+
return [enabled, _railVolts(kind, (await this._readReg(vReg)) & _railMask(kind))];
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* Set a rail voltage, rounded to the nearest step.
|
|
607
|
+
* @param {string} name - Rail name as for getRail().
|
|
608
|
+
* @param {number} volts - Target in V; outside the rail's range raises.
|
|
609
|
+
* @returns {Promise<void>}
|
|
610
|
+
*/
|
|
611
|
+
async setRailVoltage(name, volts) {
|
|
612
|
+
const [, , vReg, kind] = this._rail(name);
|
|
613
|
+
await this._rmw(vReg, _railMask(kind), _railCode(kind, volts, name));
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* Enable or disable a rail. Disabling the rail that powers the host cuts the host's power.
|
|
618
|
+
* @param {string} name - Rail name as for getRail().
|
|
619
|
+
* @param {boolean} enabled
|
|
620
|
+
* @returns {Promise<void>}
|
|
621
|
+
*/
|
|
622
|
+
async setRailEnabled(name, enabled) {
|
|
623
|
+
const [enReg, enBit] = this._rail(name);
|
|
624
|
+
await this._rmw(enReg, 1 << enBit, enabled ? (1 << enBit) : 0);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Configure one DCDC converter.
|
|
629
|
+
* @param {string} name - 'dcdc1'..'dcdc4'.
|
|
630
|
+
* @param {boolean} [alwaysPwm=false] - Force PWM instead of automatic PWM/PFM.
|
|
631
|
+
* @param {?boolean} [dvm=null] - Dynamic voltage scaling for dcdc2 / dcdc3; null keeps it.
|
|
632
|
+
* @returns {Promise<void>}
|
|
633
|
+
*/
|
|
634
|
+
async configureDcdc(name, alwaysPwm = false, dvm = null) {
|
|
635
|
+
if (!['dcdc1', 'dcdc2', 'dcdc3', 'dcdc4'].includes(name)) throw new RangeError('not a DCDC: ' + name);
|
|
636
|
+
const bit = 1 << (parseInt(name.slice(4), 10) + 1);
|
|
637
|
+
await this._rmw(_REG_DCDC_PWM, bit, alwaysPwm ? bit : 0);
|
|
638
|
+
if (dvm !== null && dvm !== undefined) {
|
|
639
|
+
if (name !== 'dcdc2' && name !== 'dcdc3') throw new RangeError('DVM is only available on dcdc2 / dcdc3');
|
|
640
|
+
await this._rmw(_RAILS[name][2], 0x80, dvm ? 0x80 : 0);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Configure settings shared by all DCDC converters.
|
|
646
|
+
* @param {boolean} [forceCcm=false] @param {boolean} [slowRamp=false] - 31.25 µs per DVM step.
|
|
647
|
+
* @param {boolean} [frequencySpread=false] @param {boolean} [spread100khz=false]
|
|
648
|
+
* @param {number} [uvpDebounceUs=60] - 60 / 120 / 180 / 240 µs.
|
|
649
|
+
* @returns {Promise<void>}
|
|
650
|
+
*/
|
|
651
|
+
async configureDcdcGlobal(forceCcm = false, slowRamp = false, frequencySpread = false, spread100khz = false, uvpDebounceUs = 60) {
|
|
652
|
+
const deb = _lookup(uvpDebounceUs, [[60, 0], [120, 1], [180, 2], [240, 3]], 'UVP debounce');
|
|
653
|
+
await this._rmw(_REG_DCDC_EN, 0x60, (forceCcm ? 0x40 : 0) | (slowRamp ? 0x20 : 0));
|
|
654
|
+
await this._rmw(_REG_DCDC_PWM, 0xC3, (frequencySpread ? 0x80 : 0) | (spread100khz ? 0x40 : 0) | deb);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Enable output discharge when a rail is off (COMMON_CONFIG bit 5).
|
|
659
|
+
* @param {boolean} enabled
|
|
660
|
+
* @returns {Promise<void>}
|
|
661
|
+
*/
|
|
662
|
+
async setOffDischarge(enabled) {
|
|
663
|
+
await this._rmw(_REG_COMMON_CONFIG, 0x20, enabled ? 0x20 : 0);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/** Soft power-off: every output except RTCLDO turns off. @returns {Promise<void>} */
|
|
667
|
+
async powerOff() {
|
|
668
|
+
await this._command(_REG_COMMON_CONFIG, 0x01);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/** Restart the system (power-off / power-on). @returns {Promise<void>} */
|
|
672
|
+
async restart() {
|
|
673
|
+
await this._command(_REG_COMMON_CONFIG, 0x02);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/** Enter sleep; the host disables / re-voltages rails first. @returns {Promise<void>} */
|
|
677
|
+
async sleep() {
|
|
678
|
+
await this._command(_REG_SLEEP_CTRL, 0x01);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** Wake up from sleep. @returns {Promise<void>} */
|
|
682
|
+
async wakeup() {
|
|
683
|
+
await this._command(_REG_SLEEP_CTRL, 0x02);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* Configure sleep / wake-up behaviour (SLEEP_CTRL).
|
|
688
|
+
* @param {boolean} [wakeOnIrq=false] @param {boolean} [pwrokLow=true] @param {boolean} [restoreVoltages=false]
|
|
689
|
+
* @returns {Promise<void>}
|
|
690
|
+
*/
|
|
691
|
+
async configureSleep(wakeOnIrq = false, pwrokLow = true, restoreVoltages = false) {
|
|
692
|
+
await this._rmw(_REG_SLEEP_CTRL, 0x1C, (wakeOnIrq ? 0x10 : 0) | (pwrokLow ? 0x08 : 0) | (restoreVoltages ? 0x04 : 0));
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/** @returns {Promise<number>} PWRON_STATUS flags. */
|
|
696
|
+
async powerOnSource() {
|
|
697
|
+
return this._readReg(_REG_PWRON_STATUS);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/** @returns {Promise<number>} PWROFF_STATUS flags. */
|
|
701
|
+
async powerOffSource() {
|
|
702
|
+
return this._readReg(_REG_PWROFF_STATUS);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* Configure the power-key timing and actions.
|
|
707
|
+
* @param {number} [onMs=512] - 128 / 512 / 1000 / 2000 ms.
|
|
708
|
+
* @param {number} [offS=6] - 4 / 6 / 8 / 10 s.
|
|
709
|
+
* @param {number} [irqS=1.5] - 1 / 1.5 / 2 / 2.5 s.
|
|
710
|
+
* @param {boolean} [longPressOff=true] @param {boolean} [restartInstead=false] @param {boolean} [forceOff16s=false]
|
|
711
|
+
* @returns {Promise<void>}
|
|
712
|
+
*/
|
|
713
|
+
async configurePowerKey(onMs = 512, offS = 6, irqS = 1.5, longPressOff = true, restartInstead = false, forceOff16s = false) {
|
|
714
|
+
const on = _lookup(onMs, [[128, 0], [512, 1], [1000, 2], [2000, 3]], 'power key on time');
|
|
715
|
+
const off = _lookup(offS, [[4, 0], [6, 1], [8, 2], [10, 3]], 'power key off time');
|
|
716
|
+
const irq = _lookup(irqS, [[1, 0], [1.5, 1], [2, 2], [2.5, 3]], 'power key irq time');
|
|
717
|
+
await this._rmw(_REG_PWRKEY_LEVEL, 0x3F, on | (off << 2) | (irq << 4));
|
|
718
|
+
await this._rmw(_REG_PWROFF_EN, 0x03, (longPressOff ? 0x02 : 0) | (restartInstead ? 0x01 : 0));
|
|
719
|
+
await this._rmw(_REG_COMMON_CONFIG, 0x04, forceOff16s ? 0x04 : 0);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* Configure the power-off sources and thresholds.
|
|
724
|
+
* @param {boolean} [dieOverTemp=true] @param {number} [offVoltage=2.6] - 2.6-3.3 V in 100 mV steps.
|
|
725
|
+
* @param {boolean} [dcdcOvp=true] @param {number} [dcdcUvp=0x0F] - Mask of DCDC1-4 (bits 0-3).
|
|
726
|
+
* @returns {Promise<void>}
|
|
727
|
+
*/
|
|
728
|
+
async configurePowerOff(dieOverTemp = true, offVoltage = 2.6, dcdcOvp = true, dcdcUvp = 0x0F) {
|
|
729
|
+
const code = _stepCode(offVoltage, 2.6, 0.1, 7, 'power-off voltage');
|
|
730
|
+
await this._rmw(_REG_PWROFF_EN, 0x04, dieOverTemp ? 0x04 : 0);
|
|
731
|
+
await this._rmw(_REG_VOFF, 0x07, code);
|
|
732
|
+
await this._rmw(_REG_DCDC_PROT, 0x2F, (dcdcOvp ? 0x20 : 0) | (dcdcUvp & 0x0F));
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
* Configure the PWROK output.
|
|
737
|
+
* @param {boolean} [check=true] @param {number} [delayMs=8] - 8 / 16 / 32 / 64 ms.
|
|
738
|
+
* @param {boolean} [offDelay=true] @param {boolean} [reverseOffSequence=false] @param {boolean} [restartOnLow=false]
|
|
739
|
+
* @returns {Promise<void>}
|
|
740
|
+
*/
|
|
741
|
+
async configurePwrok(check = true, delayMs = 8, offDelay = true, reverseOffSequence = false, restartOnLow = false) {
|
|
742
|
+
const delay = _lookup(delayMs, [[8, 0], [16, 1], [32, 2], [64, 3]], 'PWROK delay');
|
|
743
|
+
const value = delay | (reverseOffSequence ? 0x04 : 0) | (offDelay ? 0x08 : 0) | (check ? 0x10 : 0);
|
|
744
|
+
await this._rmw(_REG_PWROK_CTRL, 0x1F, value);
|
|
745
|
+
await this._rmw(_REG_COMMON_CONFIG, 0x08, restartOnLow ? 0x08 : 0);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/**
|
|
749
|
+
* BATFET on when powered off on battery only (BATFET_CTRL bit 3).
|
|
750
|
+
* @param {boolean} enabled
|
|
751
|
+
* @returns {Promise<void>}
|
|
752
|
+
*/
|
|
753
|
+
async setBatfetOffEnable(enabled) {
|
|
754
|
+
await this._rmw(_REG_BATFET_CTRL, 0x08, enabled ? 0x08 : 0);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* Configure fast power-on / wake-up and the start sequence.
|
|
759
|
+
* @param {boolean} poweron @param {boolean} wakeup
|
|
760
|
+
* @param {?Object<string, ?number>} [sequence=null] - Rail -> start-sequence code 0-2, or null to disable the rail.
|
|
761
|
+
* @returns {Promise<void>}
|
|
762
|
+
*/
|
|
763
|
+
async configureFastPoweron(poweron, wakeup, sequence = null) {
|
|
764
|
+
if (sequence) {
|
|
765
|
+
for (const name of Object.keys(sequence)) {
|
|
766
|
+
if (!Object.prototype.hasOwnProperty.call(_FAST_RAILS, name)) throw new RangeError('unknown rail: ' + name);
|
|
767
|
+
let code = sequence[name];
|
|
768
|
+
if (code === null || code === undefined) code = 3;
|
|
769
|
+
if (![0, 1, 2, 3].includes(code)) throw new RangeError('start-sequence code must be 0-2 or null');
|
|
770
|
+
const [off, shift] = _FAST_RAILS[name];
|
|
771
|
+
await this._rmw(_REG_FAST_PWRON0 + off, 3 << shift, code << shift);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
await this._rmw(_REG_FAST_PWRON0 + 3, 0xC0, (poweron ? 0x80 : 0) | (wakeup ? 0x40 : 0));
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* Configure the watchdog.
|
|
779
|
+
* @param {boolean} enabled @param {number} [timeoutS=64] - 1, 2, 4, 8, 16, 32, 64 or 128 s.
|
|
780
|
+
* @param {string} [action='irq'] - 'irq', 'reset', 'reset_pwrok' or 'power_cycle'.
|
|
781
|
+
* @returns {Promise<void>}
|
|
782
|
+
*/
|
|
783
|
+
async configureWatchdog(enabled, timeoutS = 64, action = 'irq') {
|
|
784
|
+
const actions = { irq: 0, reset: 1, reset_pwrok: 2, power_cycle: 3 };
|
|
785
|
+
if (!(action in actions)) throw new RangeError('unknown watchdog action: ' + action);
|
|
786
|
+
const code = _lookup(timeoutS, [[1, 0], [2, 1], [4, 2], [8, 3], [16, 4], [32, 5], [64, 6], [128, 7]], 'watchdog timeout');
|
|
787
|
+
await this._rmw(_REG_WATCHDOG_CTRL, 0x37, code | (actions[action] << 4));
|
|
788
|
+
await this._rmw(_REG_MODULE_ENABLE, 0x01, enabled ? 1 : 0);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
/** Clear (feed) the watchdog. @returns {Promise<void>} */
|
|
792
|
+
async feedWatchdog() {
|
|
793
|
+
await this._command(_REG_WATCHDOG_CTRL, 0x08);
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
/**
|
|
797
|
+
* Set the low-battery warning levels (LOWBAT_WARN).
|
|
798
|
+
* @param {number} [level2=10] - 5-20 %. @param {number} [level1=1] - 0-15 %.
|
|
799
|
+
* @returns {Promise<void>}
|
|
800
|
+
*/
|
|
801
|
+
async configureBatteryWarning(level2 = 10, level1 = 1) {
|
|
802
|
+
if (level2 < 5 || level2 > 20 || level1 < 0 || level1 > 15) throw new RangeError('battery warning levels out of range');
|
|
803
|
+
await this._writeReg(_REG_LOWBAT_WARN, ((level2 - 5) << 4) | level1);
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* Enable or disable the fuel gauge (MODULE_ENABLE bit 3).
|
|
808
|
+
* @param {boolean} enabled
|
|
809
|
+
* @returns {Promise<void>}
|
|
810
|
+
*/
|
|
811
|
+
async setGaugeEnabled(enabled) {
|
|
812
|
+
await this._rmw(_REG_MODULE_ENABLE, 0x08, enabled ? 0x08 : 0);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
/**
|
|
816
|
+
* Reset the fuel gauge.
|
|
817
|
+
* @param {boolean} [full=false] - Deeper reset that also clears gauge data beyond the registers.
|
|
818
|
+
* @returns {Promise<void>}
|
|
819
|
+
*/
|
|
820
|
+
async resetGauge(full = false) {
|
|
821
|
+
await this._command(_REG_GAUGE_RESET, full ? 0x04 : 0x08);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* Drive the open-drain GPIO1 output.
|
|
826
|
+
* @param {boolean} low - True drives low, false is Hi-Z.
|
|
827
|
+
* @returns {Promise<void>}
|
|
828
|
+
*/
|
|
829
|
+
async setGpio1(low) {
|
|
830
|
+
await this._rmw(_REG_GPIO1_CTRL, 0x0C, low ? 0x04 : 0);
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* Read one of the four general-purpose bytes.
|
|
835
|
+
* @param {number} index - 0-3.
|
|
836
|
+
* @returns {Promise<number>}
|
|
837
|
+
*/
|
|
838
|
+
async readDataBuffer(index) {
|
|
839
|
+
if (!Number.isInteger(index) || index < 0 || index > 3) throw new RangeError('data buffer index must be 0-3');
|
|
840
|
+
return this._readReg(_REG_DATA_BUFFER0 + index);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/**
|
|
844
|
+
* Write one of the four general-purpose bytes (survives soft restarts, not a power-on reset).
|
|
845
|
+
* @param {number} index - 0-3. @param {number} value - Byte.
|
|
846
|
+
* @returns {Promise<void>}
|
|
847
|
+
*/
|
|
848
|
+
async writeDataBuffer(index, value) {
|
|
849
|
+
if (!Number.isInteger(index) || index < 0 || index > 3) throw new RangeError('data buffer index must be 0-3');
|
|
850
|
+
await this._writeReg(_REG_DATA_BUFFER0 + index, value);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/**
|
|
854
|
+
* Enable interrupt sources (IRQ_EN0..2 read-modify-write).
|
|
855
|
+
* @param {number} source - Bitwise OR of SOURCE_* constants.
|
|
856
|
+
* @returns {Promise<void>}
|
|
857
|
+
*/
|
|
858
|
+
async enableInterrupt(source) {
|
|
859
|
+
for (let k = 0; k < 3; k++) {
|
|
860
|
+
const bits = (source >> (8 * k)) & 0xFF;
|
|
861
|
+
if (bits) await this._rmw(_REG_IRQ_EN0 + k, bits, bits);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/**
|
|
866
|
+
* Disable interrupt sources.
|
|
867
|
+
* @param {number} source - Bitwise OR of SOURCE_* constants.
|
|
868
|
+
* @returns {Promise<void>}
|
|
869
|
+
*/
|
|
870
|
+
async disableInterrupt(source) {
|
|
871
|
+
for (let k = 0; k < 3; k++) {
|
|
872
|
+
const bits = (source >> (8 * k)) & 0xFF;
|
|
873
|
+
if (bits) await this._rmw(_REG_IRQ_EN0 + k, bits, 0);
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* Read and clear the interrupt status. Writes back exactly the bits it saw set so an event arriving
|
|
879
|
+
* between the read and the write is not lost.
|
|
880
|
+
* @returns {Promise<number>} 24-bit status: IRQ_STATUS0 in bits 7:0, STATUS1 in 15:8, STATUS2 in 23:16.
|
|
881
|
+
*/
|
|
882
|
+
async pollInterrupt() {
|
|
883
|
+
let status = 0;
|
|
884
|
+
for (let k = 0; k < 3; k++) {
|
|
885
|
+
const value = await this._readReg(_REG_IRQ_STATUS0 + k);
|
|
886
|
+
if (value) await this._writeReg(_REG_IRQ_STATUS0 + k, value);
|
|
887
|
+
status |= value << (8 * k);
|
|
888
|
+
}
|
|
889
|
+
return status;
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* Subscribe to IRQ pin assertions. Uses `connection.intPin` (falling edge) when present, otherwise a 5 ms
|
|
894
|
+
* polling timer.
|
|
895
|
+
* @param {function(number): void} callback - Receives the 24-bit status.
|
|
896
|
+
* @returns {Promise<void>}
|
|
897
|
+
*/
|
|
898
|
+
async onInterrupt(callback) {
|
|
899
|
+
await this.offInterrupt();
|
|
900
|
+
this._callback = callback;
|
|
901
|
+
const dispatch = async () => {
|
|
902
|
+
const status = await this.pollInterrupt();
|
|
903
|
+
if (status && this._callback) this._callback(status);
|
|
904
|
+
};
|
|
905
|
+
if (this._conn.intPin) {
|
|
906
|
+
this._edgeHandler = dispatch;
|
|
907
|
+
await this._conn.intPin.onEdge(this._edgeHandler, 'falling');
|
|
908
|
+
} else {
|
|
909
|
+
let busy = false;
|
|
910
|
+
this._pollTimer = setInterval(async () => {
|
|
911
|
+
if (busy) return;
|
|
912
|
+
busy = true;
|
|
913
|
+
try {
|
|
914
|
+
await dispatch();
|
|
915
|
+
} catch (e) {
|
|
916
|
+
// ignore transient bus errors while polling
|
|
917
|
+
} finally {
|
|
918
|
+
busy = false;
|
|
919
|
+
}
|
|
920
|
+
}, 5);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
/**
|
|
925
|
+
* Unsubscribe from IRQ pin assertions.
|
|
926
|
+
* @returns {Promise<void>}
|
|
927
|
+
*/
|
|
928
|
+
async offInterrupt() {
|
|
929
|
+
if (this._conn.intPin && this._edgeHandler) {
|
|
930
|
+
await this._conn.intPin.offEdge(this._edgeHandler);
|
|
931
|
+
}
|
|
932
|
+
this._edgeHandler = null;
|
|
933
|
+
if (this._pollTimer) {
|
|
934
|
+
clearInterval(this._pollTimer);
|
|
935
|
+
this._pollTimer = null;
|
|
936
|
+
}
|
|
937
|
+
this._callback = null;
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
AXP2101Full.SOURCE_BAT_UNDER_TEMP_WORK = 0x000001;
|
|
942
|
+
AXP2101Full.SOURCE_BAT_OVER_TEMP_WORK = 0x000002;
|
|
943
|
+
AXP2101Full.SOURCE_BAT_UNDER_TEMP_CHG = 0x000004;
|
|
944
|
+
AXP2101Full.SOURCE_BAT_OVER_TEMP_CHG = 0x000008;
|
|
945
|
+
AXP2101Full.SOURCE_GAUGE_NEW_SOC = 0x000010;
|
|
946
|
+
AXP2101Full.SOURCE_GAUGE_WDT = 0x000020;
|
|
947
|
+
AXP2101Full.SOURCE_SOC_WARN_L1 = 0x000040;
|
|
948
|
+
AXP2101Full.SOURCE_SOC_WARN_L2 = 0x000080;
|
|
949
|
+
AXP2101Full.SOURCE_PWRON_POS_EDGE = 0x000100;
|
|
950
|
+
AXP2101Full.SOURCE_PWRON_NEG_EDGE = 0x000200;
|
|
951
|
+
AXP2101Full.SOURCE_PWRON_LONG = 0x000400;
|
|
952
|
+
AXP2101Full.SOURCE_PWRON_SHORT = 0x000800;
|
|
953
|
+
AXP2101Full.SOURCE_BAT_REMOVE = 0x001000;
|
|
954
|
+
AXP2101Full.SOURCE_BAT_INSERT = 0x002000;
|
|
955
|
+
AXP2101Full.SOURCE_VBUS_REMOVE = 0x004000;
|
|
956
|
+
AXP2101Full.SOURCE_VBUS_INSERT = 0x008000;
|
|
957
|
+
AXP2101Full.SOURCE_BAT_OVER_VOLTAGE = 0x010000;
|
|
958
|
+
AXP2101Full.SOURCE_CHARGER_TIMER = 0x020000;
|
|
959
|
+
AXP2101Full.SOURCE_DIE_OVER_TEMP_L1 = 0x040000;
|
|
960
|
+
AXP2101Full.SOURCE_CHARGE_START = 0x080000;
|
|
961
|
+
AXP2101Full.SOURCE_CHARGE_DONE = 0x100000;
|
|
962
|
+
AXP2101Full.SOURCE_LDO_OVER_CURRENT = 0x400000;
|
|
963
|
+
AXP2101Full.SOURCE_WATCHDOG_EXPIRE = 0x800000;
|
|
964
|
+
|
|
965
|
+
/** Default 7-bit I²C address. */
|
|
966
|
+
AXP2101Minimal.I2C_ADDRESS = 0x34;
|
|
967
|
+
|
|
968
|
+
module.exports = { AXP2101Minimal, AXP2101Full };
|