periph 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -0
- package/examples/adc_dac/ad5251/complete.js +66 -0
- package/examples/adc_dac/ad5251/demo.js +69 -0
- package/examples/adc_dac/ad5251/minimal.js +23 -0
- package/examples/adc_dac/ad5252/complete.js +66 -0
- package/examples/adc_dac/ad5252/demo.js +69 -0
- package/examples/adc_dac/ad5252/minimal.js +23 -0
- package/examples/adc_dac/ads7828/complete.js +46 -0
- package/examples/adc_dac/ads7828/demo.js +45 -0
- package/examples/adc_dac/ads7828/minimal.js +17 -0
- package/examples/adc_dac/cat5171/complete.js +52 -0
- package/examples/adc_dac/cat5171/demo.js +59 -0
- package/examples/adc_dac/cat5171/minimal.js +23 -0
- package/examples/adc_dac/cs43l22/complete.js +88 -0
- package/examples/adc_dac/cs43l22/demo.js +56 -0
- package/examples/adc_dac/cs43l22/minimal.js +20 -0
- package/examples/adc_dac/ds1881/complete.js +62 -0
- package/examples/adc_dac/ds1881/demo.js +61 -0
- package/examples/adc_dac/ds1881/minimal.js +25 -0
- package/examples/adc_dac/ds3502/complete.js +46 -0
- package/examples/adc_dac/ds3502/demo.js +54 -0
- package/examples/adc_dac/ds3502/minimal.js +28 -0
- package/examples/adc_dac/mcp3422/complete.js +48 -0
- package/examples/adc_dac/mcp3422/demo.js +49 -0
- package/examples/adc_dac/mcp3422/minimal.js +25 -0
- package/examples/display/sh1106/complete.js +101 -0
- package/examples/display/sh1106/demo.js +68 -0
- package/examples/display/sh1106/minimal.js +25 -0
- package/examples/display/ssd1305/complete.js +129 -0
- package/examples/display/ssd1305/demo.js +71 -0
- package/examples/display/ssd1305/minimal.js +25 -0
- package/examples/display/ssd1306/complete.js +115 -0
- package/examples/display/ssd1306/demo.js +70 -0
- package/examples/display/ssd1306/minimal.js +25 -0
- package/examples/environmental/am2315/complete.js +34 -0
- package/examples/environmental/am2315/demo.js +46 -0
- package/examples/environmental/am2315/minimal.js +21 -0
- package/examples/humidity/htu21df/complete.js +49 -0
- package/examples/humidity/htu21df/demo.js +50 -0
- package/examples/humidity/htu21df/minimal.js +22 -0
- package/examples/humidity/sht31/complete.js +53 -0
- package/examples/humidity/sht31/demo.js +53 -0
- package/examples/humidity/sht31/minimal.js +21 -0
- package/examples/humidity/si7021/complete.js +55 -0
- package/examples/humidity/si7021/demo.js +58 -0
- package/examples/humidity/si7021/minimal.js +21 -0
- package/examples/imu/bno055/complete.js +85 -0
- package/examples/imu/bno055/demo.js +70 -0
- package/examples/imu/bno055/minimal.js +20 -0
- package/examples/io_expander/pca6408a/complete.js +63 -0
- package/examples/io_expander/pca6408a/demo.js +58 -0
- package/examples/io_expander/pca6408a/minimal.js +20 -0
- package/examples/io_expander/pca6416a/complete.js +68 -0
- package/examples/io_expander/pca6416a/demo.js +60 -0
- package/examples/io_expander/pca6416a/minimal.js +20 -0
- package/examples/io_expander/pca9536/complete.js +63 -0
- package/examples/io_expander/pca9536/demo.js +57 -0
- package/examples/io_expander/pca9536/minimal.js +20 -0
- package/examples/io_expander/pca9539/complete.js +68 -0
- package/examples/io_expander/pca9539/demo.js +60 -0
- package/examples/io_expander/pca9539/minimal.js +20 -0
- package/examples/light/max44009/complete.js +56 -0
- package/examples/light/max44009/demo.js +49 -0
- package/examples/light/max44009/minimal.js +22 -0
- package/examples/memory/at24c02n/complete.js +39 -0
- package/examples/memory/at24c02n/demo.js +39 -0
- package/examples/memory/at24c02n/minimal.js +15 -0
- package/examples/memory/at24c64/complete.js +39 -0
- package/examples/memory/at24c64/demo.js +39 -0
- package/examples/memory/at24c64/minimal.js +15 -0
- package/examples/memory/cat24c512/complete.js +39 -0
- package/examples/memory/cat24c512/demo.js +39 -0
- package/examples/memory/cat24c512/minimal.js +15 -0
- package/examples/other/pca9541/complete.js +52 -0
- package/examples/other/pca9541/demo.js +61 -0
- package/examples/other/pca9541/minimal.js +23 -0
- package/examples/temperature/tmp102/complete.js +74 -0
- package/examples/temperature/tmp102/demo.js +71 -0
- package/examples/temperature/tmp102/minimal.js +20 -0
- package/examples/tof/vl6180x/complete.js +62 -0
- package/examples/tof/vl6180x/demo.js +77 -0
- package/examples/tof/vl6180x/minimal.js +23 -0
- package/package.json +1 -1
- package/src/chips/adc_dac/ad5251.js +345 -0
- package/src/chips/adc_dac/ad5252.js +345 -0
- package/src/chips/adc_dac/ads7828.js +225 -0
- package/src/chips/adc_dac/cat5171.js +213 -0
- package/src/chips/adc_dac/cs43l22.js +645 -0
- package/src/chips/adc_dac/ds1881.js +357 -0
- package/src/chips/adc_dac/ds3502.js +241 -0
- package/src/chips/adc_dac/mcp3422.js +312 -0
- package/src/chips/display/mono_oled.js +182 -0
- package/src/chips/display/sh1106.js +496 -0
- package/src/chips/display/ssd1305.js +655 -0
- package/src/chips/display/ssd1306.js +458 -0
- package/src/chips/environmental/am2315.js +237 -0
- package/src/chips/humidity/htu21df.js +222 -0
- package/src/chips/humidity/sht31.js +284 -0
- package/src/chips/humidity/si7021.js +282 -0
- package/src/chips/imu/bno055.js +496 -0
- package/src/chips/io_expander/pca6408a.js +407 -0
- package/src/chips/io_expander/pca6416a.js +455 -0
- package/src/chips/io_expander/pca9536.js +398 -0
- package/src/chips/io_expander/pca9539.js +457 -0
- package/src/chips/light/max44009.js +336 -0
- package/src/chips/memory/at24c02n.js +220 -0
- package/src/chips/memory/at24c64.js +220 -0
- package/src/chips/memory/cat24c512.js +221 -0
- package/src/chips/other/pca9541.js +315 -0
- package/src/chips/temperature/tmp102.js +361 -0
- package/src/chips/tof/vl6180x.js +580 -0
- package/src/discovery/registry.js +65 -13
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* CS43L22 — low-power stereo DAC with headphone and Class-D speaker amplifiers (Cirrus Logic).
|
|
5
|
+
*
|
|
6
|
+
* Configured entirely through the I²C control port (7-bit address 0x4A with AD0 = DGND,
|
|
7
|
+
* 0x4B with AD0 = VL; 100 kHz maximum). Audio data travels over a separate I²S / PCM port
|
|
8
|
+
* that this driver does not touch: the application supplies MCLK and the stream, calls
|
|
9
|
+
* `start()` once the clocks run and `stop()` before they go away. Registers are accessed
|
|
10
|
+
* through `readReg` / `writeReg` with a one-byte address and written one at a time.
|
|
11
|
+
*
|
|
12
|
+
* Order matters: configuration is written while POWER_CTL_1 is 0x01 (powered down), then the
|
|
13
|
+
* required initialization block (registers 0x00, 0x47, 0x32) runs, and only `start()` writes 0x9E.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const I2C_ADDRESS = 0x4A;
|
|
17
|
+
|
|
18
|
+
const REG_ID = 0x01;
|
|
19
|
+
const REG_POWER_CTL_1 = 0x02;
|
|
20
|
+
const REG_POWER_CTL_2 = 0x04;
|
|
21
|
+
const REG_CLOCKING_CTL = 0x05;
|
|
22
|
+
const REG_INTERFACE_CTL_1 = 0x06;
|
|
23
|
+
const REG_INTERFACE_CTL_2 = 0x07;
|
|
24
|
+
const REG_PASSTHRU_A_SEL = 0x08;
|
|
25
|
+
const REG_PASSTHRU_B_SEL = 0x09;
|
|
26
|
+
const REG_ANALOG_ZC_SR = 0x0A;
|
|
27
|
+
const REG_PLAYBACK_CTL_1 = 0x0D;
|
|
28
|
+
const REG_MISC_CTL = 0x0E;
|
|
29
|
+
const REG_PLAYBACK_CTL_2 = 0x0F;
|
|
30
|
+
const REG_PASSTHRU_A_VOL = 0x14;
|
|
31
|
+
const REG_PASSTHRU_B_VOL = 0x15;
|
|
32
|
+
const REG_PCM_A_VOL = 0x1A;
|
|
33
|
+
const REG_PCM_B_VOL = 0x1B;
|
|
34
|
+
const REG_BEEP_FREQ_ON = 0x1C;
|
|
35
|
+
const REG_BEEP_VOL_OFF = 0x1D;
|
|
36
|
+
const REG_BEEP_TONE_CFG = 0x1E;
|
|
37
|
+
const REG_TONE_CTL = 0x1F;
|
|
38
|
+
const REG_MASTER_A_VOL = 0x20;
|
|
39
|
+
const REG_MASTER_B_VOL = 0x21;
|
|
40
|
+
const REG_HP_A_VOL = 0x22;
|
|
41
|
+
const REG_HP_B_VOL = 0x23;
|
|
42
|
+
const REG_SPK_A_VOL = 0x24;
|
|
43
|
+
const REG_SPK_B_VOL = 0x25;
|
|
44
|
+
const REG_PCM_MIXER_SWAP = 0x26;
|
|
45
|
+
const REG_LIMIT_CTL_1 = 0x27;
|
|
46
|
+
const REG_LIMIT_CTL_2 = 0x28;
|
|
47
|
+
const REG_LIMIT_ATTACK = 0x29;
|
|
48
|
+
const REG_STATUS = 0x2E;
|
|
49
|
+
const REG_BATTERY_COMP = 0x2F;
|
|
50
|
+
const REG_VP_LEVEL = 0x30;
|
|
51
|
+
const REG_SPEAKER_STATUS = 0x31;
|
|
52
|
+
const REG_CHARGE_PUMP_FREQ = 0x34;
|
|
53
|
+
const REG_INIT_00 = 0x00;
|
|
54
|
+
const REG_INIT_32 = 0x32;
|
|
55
|
+
const REG_INIT_47 = 0x47;
|
|
56
|
+
|
|
57
|
+
const READABLE = new Set([
|
|
58
|
+
0x00, 0x01, 0x02, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0C, 0x0D, 0x0E, 0x0F,
|
|
59
|
+
0x14, 0x15, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
|
|
60
|
+
0x26, 0x27, 0x28, 0x29, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x34, 0x47]);
|
|
61
|
+
const READ_ONLY = new Set([0x01, 0x2E, 0x30, 0x31]);
|
|
62
|
+
|
|
63
|
+
const HP_GAINS = [0.3959, 0.4571, 0.5111, 0.6047, 0.7099, 0.8399, 1.000, 1.1430];
|
|
64
|
+
const LIMIT_DB = [0, -3, -6, -9, -12, -18, -24, -30];
|
|
65
|
+
|
|
66
|
+
const FS_ROUND = [8000, 12000, 16000, 24000, 32000, 48000, 96000];
|
|
67
|
+
const FS_44K = [11025, 22050, 44100, 88200];
|
|
68
|
+
const FS_FRAC = [12e6 / 1088, 12e6 / 544, 12e6 / 272, 12e6 / 136];
|
|
69
|
+
const FS_27M = [8000, 12000, 16000, 24000, 32000, 48000];
|
|
70
|
+
|
|
71
|
+
/** Output power modes for `setOutputs`. */
|
|
72
|
+
const OUT = { FOLLOW_LOW: 0, FOLLOW_HIGH: 1, ON: 2, OFF: 3 };
|
|
73
|
+
/** Serial port formats for `setInterface`. */
|
|
74
|
+
const FMT = { LJ: 0, I2S: 1, RJ: 2 };
|
|
75
|
+
/** Beep modes for `setBeep`. */
|
|
76
|
+
const BEEP = { OFF: 0, SINGLE: 1, MULTIPLE: 2, CONTINUOUS: 3 };
|
|
77
|
+
/** PCM channel sources for `setPcmSwap`. */
|
|
78
|
+
const SRC = { NORMAL: 0, MIX: 1, SWAPPED: 3 };
|
|
79
|
+
/** Headphone gain indices for `setHeadphoneGain`. */
|
|
80
|
+
const GAIN = { G_0_3959: 0, G_0_4571: 1, G_0_5111: 2, G_0_6047: 3, G_0_7099: 4, G_0_8399: 5, G_1_000: 6, G_1_143: 7 };
|
|
81
|
+
|
|
82
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
83
|
+
const _roundHalfUp = (x) => Math.floor(x + 0.5);
|
|
84
|
+
const _clamp = (x, lo, hi) => (x < lo ? lo : x > hi ? hi : x);
|
|
85
|
+
const _halfDbCode = (db, lo, hi) => _roundHalfUp(_clamp(db, lo, hi) * 2) & 0xFF;
|
|
86
|
+
const _signed = (code) => (code > 127 ? code - 256 : code);
|
|
87
|
+
const _limitedVolumeCode = (db) => (db < -96 ? 0x01 : _roundHalfUp(_clamp(db, -96, 0) * 2) & 0xFF);
|
|
88
|
+
const _near = (v, c) => Math.abs(v - c) <= c * 1e-4;
|
|
89
|
+
const _nearAny = (v, list) => list.some((c) => _near(v, c));
|
|
90
|
+
|
|
91
|
+
function _nearestLimit(db) {
|
|
92
|
+
let best = 0;
|
|
93
|
+
let bestErr = Math.abs(db - LIMIT_DB[0]);
|
|
94
|
+
for (let i = 1; i < 8; i++) {
|
|
95
|
+
const err = Math.abs(db - LIMIT_DB[i]);
|
|
96
|
+
if (err < bestErr) { best = i; bestErr = err; }
|
|
97
|
+
}
|
|
98
|
+
return best;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Datasheet Table 1 clocking settings for an MCLK / sample-rate pair.
|
|
103
|
+
*
|
|
104
|
+
* @param {number} mclkHz - 12.288, 11.2896, 18.432, 16.9344, 12.0, 24.0 or 27.0 MHz, in Hz.
|
|
105
|
+
* @param {number} fsHz - Sample rate in Hz (a rate listed for that MCLK in Table 1).
|
|
106
|
+
* @returns {{auto: boolean, speed: number, group32k: boolean, videoClk: boolean, ratio: number, mclkDiv2: boolean}}
|
|
107
|
+
* The arguments of `setClocking` with `auto` false.
|
|
108
|
+
* @throws {RangeError} If the combination is not in the datasheet table.
|
|
109
|
+
*/
|
|
110
|
+
function clockingFor(mclkHz, fsHz) {
|
|
111
|
+
let video = false;
|
|
112
|
+
let div2 = false;
|
|
113
|
+
let ratio = -1;
|
|
114
|
+
if (_nearAny(mclkHz, [12288000, 18432000])) {
|
|
115
|
+
if (_nearAny(fsHz, FS_ROUND)) ratio = 0;
|
|
116
|
+
} else if (_near(mclkHz, 11289600)) {
|
|
117
|
+
if (_nearAny(fsHz, FS_44K)) ratio = 0;
|
|
118
|
+
} else if (_near(mclkHz, 16934400)) {
|
|
119
|
+
if (_nearAny(fsHz, FS_44K)) ratio = 0;
|
|
120
|
+
else if (_near(fsHz, 16934400 / 2112)) ratio = 2;
|
|
121
|
+
} else if (_nearAny(mclkHz, [12000000, 24000000])) {
|
|
122
|
+
div2 = mclkHz > 18000000;
|
|
123
|
+
if (_nearAny(fsHz, FS_ROUND)) ratio = 1;
|
|
124
|
+
else if (_nearAny(fsHz, FS_FRAC)) ratio = 3;
|
|
125
|
+
} else if (_near(mclkHz, 27000000)) {
|
|
126
|
+
video = true;
|
|
127
|
+
if (_nearAny(fsHz, FS_27M)) ratio = 1;
|
|
128
|
+
else if (_nearAny(fsHz, FS_FRAC.slice(0, 3))) ratio = 3;
|
|
129
|
+
}
|
|
130
|
+
if (ratio < 0) throw new RangeError('MCLK / sample-rate combination is not in the datasheet table');
|
|
131
|
+
const group32k = _nearAny(fsHz, [8000, 16000, 32000]);
|
|
132
|
+
const speed = fsHz <= 12100 ? 3 : fsHz <= 24100 ? 2 : fsHz <= 48100 ? 1 : 0;
|
|
133
|
+
return { auto: false, speed, group32k, videoClk: video, ratio, mclkDiv2: div2 };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* CS43L22 stereo DAC — minimal interface.
|
|
138
|
+
*
|
|
139
|
+
* Defaults: I²S slave, 24-bit, clock speed auto-detected from the MCLK / LRCK ratio, headphone
|
|
140
|
+
* amplifiers always on, speaker amplifiers off, headphone gain 0.6047, master volume 0 dB,
|
|
141
|
+
* digital soft ramp on. `init()` resets the chip (when a RESET pin is given), checks the ID,
|
|
142
|
+
* loads these defaults and runs the required initialization; the chip stays powered down until
|
|
143
|
+
* `start()` is called with MCLK running. The constructor makes no bus access.
|
|
144
|
+
*/
|
|
145
|
+
class CS43L22Minimal {
|
|
146
|
+
/**
|
|
147
|
+
* @param {object} connection - I2C connection with `readReg` / `writeReg` (address 0x4A / 0x4B).
|
|
148
|
+
* @param {object} [resPin] - Optional OutputPin (async `set(high)`) wired to the active-low RESET pin.
|
|
149
|
+
*/
|
|
150
|
+
constructor(connection, resPin = null) {
|
|
151
|
+
this._conn = connection;
|
|
152
|
+
this._resPin = resPin;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async _read(reg) { return (await this._conn.readReg(reg, 1))[0]; }
|
|
156
|
+
|
|
157
|
+
async _write(reg, value) { await this._conn.writeReg(reg, value & 0xFF); }
|
|
158
|
+
|
|
159
|
+
async _update(reg, mask, value) {
|
|
160
|
+
await this._write(reg, ((await this._read(reg)) & ~mask & 0xFF) | (value & mask));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async _setBit(reg, bit, on) { await this._update(reg, 1 << bit, on ? 1 << bit : 0); }
|
|
164
|
+
|
|
165
|
+
async _initChip() {
|
|
166
|
+
if (this._resPin) {
|
|
167
|
+
await this._resPin.set(false);
|
|
168
|
+
await _sleep(2);
|
|
169
|
+
await this._resPin.set(true);
|
|
170
|
+
await _sleep(2);
|
|
171
|
+
}
|
|
172
|
+
if (((await this._read(REG_ID)) & 0xF8) !== 0xE0) throw new Error('CS43L22 not found: chip ID mismatch');
|
|
173
|
+
await this._write(REG_POWER_CTL_1, 0x01);
|
|
174
|
+
await this._write(REG_POWER_CTL_2, 0xAF);
|
|
175
|
+
await this._write(REG_CLOCKING_CTL, 0xA0);
|
|
176
|
+
await this._write(REG_INTERFACE_CTL_1, 0x04);
|
|
177
|
+
await this._write(REG_PLAYBACK_CTL_1, 0x60);
|
|
178
|
+
await this._write(REG_MASTER_A_VOL, 0x00);
|
|
179
|
+
await this._write(REG_MASTER_B_VOL, 0x00);
|
|
180
|
+
// Required initialization settings (datasheet §4.11).
|
|
181
|
+
await this._write(REG_INIT_00, 0x99);
|
|
182
|
+
await this._write(REG_INIT_47, 0x80);
|
|
183
|
+
const v = await this._read(REG_INIT_32);
|
|
184
|
+
await this._write(REG_INIT_32, v | 0x80);
|
|
185
|
+
await this._write(REG_INIT_32, v & 0x7F);
|
|
186
|
+
await this._write(REG_INIT_00, 0x00);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Reset (when a RESET pin is given), verify the chip ID, load the defaults and run the
|
|
191
|
+
* required initialization. The chip stays powered down.
|
|
192
|
+
* @returns {Promise<void>}
|
|
193
|
+
* @throws {Error} If the chip ID register does not read 0xE0 in bits 7:3.
|
|
194
|
+
*/
|
|
195
|
+
async init() { await this._initChip(); }
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Power the chip up (POWER_CTL_1 = 0x9E). Call after MCLK, SCLK and LRCK are running.
|
|
199
|
+
* @returns {Promise<void>}
|
|
200
|
+
*/
|
|
201
|
+
async start() { await this._write(REG_POWER_CTL_1, 0x9E); }
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Power-down sequence: mute, disable soft ramp / zero-cross, POWER_CTL_1 = 0x9F, wait 100 µs.
|
|
205
|
+
* MCLK may be removed after the promise resolves.
|
|
206
|
+
* @returns {Promise<void>}
|
|
207
|
+
*/
|
|
208
|
+
async stop() {
|
|
209
|
+
await this._update(REG_PLAYBACK_CTL_2, 0xF0, 0xF0);
|
|
210
|
+
await this._update(REG_MISC_CTL, 0x03, 0x00);
|
|
211
|
+
await this._update(REG_ANALOG_ZC_SR, 0x0F, 0x00);
|
|
212
|
+
await this._write(REG_POWER_CTL_1, 0x9F);
|
|
213
|
+
await _sleep(1);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Set the master volume of both channels.
|
|
218
|
+
* @param {number} db - −102 … +12 dB, nearest 0.5 dB step (clamped).
|
|
219
|
+
* @returns {Promise<void>}
|
|
220
|
+
*/
|
|
221
|
+
async setVolume(db) {
|
|
222
|
+
const code = _halfDbCode(db, -102, 12);
|
|
223
|
+
await this._write(REG_MASTER_A_VOL, code);
|
|
224
|
+
await this._write(REG_MASTER_B_VOL, code);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Master volume of channel A.
|
|
229
|
+
* @returns {Promise<number>} dB.
|
|
230
|
+
*/
|
|
231
|
+
async getVolume() { return _signed(await this._read(REG_MASTER_A_VOL)) / 2; }
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Mute or unmute both headphone channels.
|
|
235
|
+
* @param {boolean} [on=true] - true to mute.
|
|
236
|
+
* @returns {Promise<void>}
|
|
237
|
+
*/
|
|
238
|
+
async mute(on = true) { await this._update(REG_PLAYBACK_CTL_2, 0xC0, on ? 0xC0 : 0); }
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* CS43L22 stereo DAC — full interface.
|
|
243
|
+
*
|
|
244
|
+
* Adds per-channel volumes, output power selection, clocking and serial interface configuration,
|
|
245
|
+
* tone control, beep generator, peak limiter, soft ramp / zero-cross, analog passthrough, battery
|
|
246
|
+
* compensation, status reads and raw register access.
|
|
247
|
+
*/
|
|
248
|
+
class CS43L22Full extends CS43L22Minimal {
|
|
249
|
+
/**
|
|
250
|
+
* Read the chip ID and revision.
|
|
251
|
+
* @returns {Promise<{chipId: number, revision: number}>} chipId is 0x1C for the CS43L22.
|
|
252
|
+
*/
|
|
253
|
+
async readId() {
|
|
254
|
+
const v = await this._read(REG_ID);
|
|
255
|
+
return { chipId: v >> 3, revision: v & 7 };
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Pulse RESET and re-run the Minimal initialization.
|
|
260
|
+
* @returns {Promise<void>}
|
|
261
|
+
* @throws {Error} If no RESET pin was given.
|
|
262
|
+
*/
|
|
263
|
+
async reset() {
|
|
264
|
+
if (!this._resPin) throw new Error('reset requires a resPin');
|
|
265
|
+
await this._initChip();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Select the power mode of the headphone and speaker amplifiers.
|
|
270
|
+
* @param {number} headphone - `OUT.FOLLOW_LOW` / `FOLLOW_HIGH` / `ON` / `OFF` (0–3).
|
|
271
|
+
* @param {number} speaker - Same coding.
|
|
272
|
+
* @returns {Promise<void>}
|
|
273
|
+
*/
|
|
274
|
+
async setOutputs(headphone, speaker) {
|
|
275
|
+
if (!(headphone >= 0 && headphone <= 3 && speaker >= 0 && speaker <= 3)) throw new RangeError('output mode must be 0-3');
|
|
276
|
+
await this._write(REG_POWER_CTL_2, (headphone << 6) | (headphone << 4) | (speaker << 2) | speaker);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Independent master volume of channel A (left) and B (right).
|
|
281
|
+
* @param {number} leftDb - −102 … +12 dB.
|
|
282
|
+
* @param {number} rightDb - −102 … +12 dB.
|
|
283
|
+
* @returns {Promise<void>}
|
|
284
|
+
*/
|
|
285
|
+
async setVolumeChannels(leftDb, rightDb) {
|
|
286
|
+
await this._write(REG_MASTER_A_VOL, _halfDbCode(leftDb, -102, 12));
|
|
287
|
+
await this._write(REG_MASTER_B_VOL, _halfDbCode(rightDb, -102, 12));
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Headphone volume.
|
|
292
|
+
* @param {number} leftDb - 0 … −96 dB; below −96 dB the channel is muted (code 0x01).
|
|
293
|
+
* @param {number} [rightDb] - Right channel; defaults to `leftDb`.
|
|
294
|
+
* @returns {Promise<void>}
|
|
295
|
+
*/
|
|
296
|
+
async setHeadphoneVolume(leftDb, rightDb = leftDb) {
|
|
297
|
+
await this._write(REG_HP_A_VOL, _limitedVolumeCode(leftDb));
|
|
298
|
+
await this._write(REG_HP_B_VOL, _limitedVolumeCode(rightDb));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Speaker volume.
|
|
303
|
+
* @param {number} leftDb - 0 … −96 dB; below −96 dB the channel is muted.
|
|
304
|
+
* @param {number} [rightDb] - Right channel; defaults to `leftDb`.
|
|
305
|
+
* @returns {Promise<void>}
|
|
306
|
+
*/
|
|
307
|
+
async setSpeakerVolume(leftDb, rightDb = leftDb) {
|
|
308
|
+
await this._write(REG_SPK_A_VOL, _limitedVolumeCode(leftDb));
|
|
309
|
+
await this._write(REG_SPK_B_VOL, _limitedVolumeCode(rightDb));
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* PCM mixer volume (the mute bits are kept).
|
|
314
|
+
* @param {number} leftDb - −51.5 … +12 dB.
|
|
315
|
+
* @param {number} [rightDb] - Right channel; defaults to `leftDb`.
|
|
316
|
+
* @returns {Promise<void>}
|
|
317
|
+
*/
|
|
318
|
+
async setPcmVolume(leftDb, rightDb = leftDb) {
|
|
319
|
+
await this._update(REG_PCM_A_VOL, 0x7F, _halfDbCode(leftDb, -51.5, 12));
|
|
320
|
+
await this._update(REG_PCM_B_VOL, 0x7F, _halfDbCode(rightDb, -51.5, 12));
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Mute or unmute the PCM channels.
|
|
325
|
+
* @param {boolean} left - true to mute channel A.
|
|
326
|
+
* @param {boolean} [right] - Channel B; defaults to `left`.
|
|
327
|
+
* @returns {Promise<void>}
|
|
328
|
+
*/
|
|
329
|
+
async mutePcm(left, right = left) {
|
|
330
|
+
await this._setBit(REG_PCM_A_VOL, 7, left);
|
|
331
|
+
await this._setBit(REG_PCM_B_VOL, 7, right);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Select the headphone / line analog gain.
|
|
336
|
+
* @param {number} index - 0–7 (`GAIN.G_0_3959` … `GAIN.G_1_143`).
|
|
337
|
+
* @returns {Promise<void>}
|
|
338
|
+
*/
|
|
339
|
+
async setHeadphoneGain(index) {
|
|
340
|
+
if (!(index >= 0 && index <= 7)) throw new RangeError('gain index must be 0-7');
|
|
341
|
+
await this._update(REG_PLAYBACK_CTL_1, 0xE0, index << 5);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Headphone analog gain multiplier.
|
|
346
|
+
* @returns {Promise<number>} 0.3959 … 1.143.
|
|
347
|
+
*/
|
|
348
|
+
async headphoneGain() { return HP_GAINS[(await this._read(REG_PLAYBACK_CTL_1)) >> 5]; }
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Mute or unmute the headphone and speaker outputs (both channels).
|
|
352
|
+
* @param {boolean|null} [headphone] - true mute, false unmute, null/undefined keep.
|
|
353
|
+
* @param {boolean|null} [speaker] - Same for the speaker.
|
|
354
|
+
* @returns {Promise<void>}
|
|
355
|
+
*/
|
|
356
|
+
async muteOutputs(headphone = null, speaker = null) {
|
|
357
|
+
if (headphone !== null && headphone !== undefined) await this._update(REG_PLAYBACK_CTL_2, 0xC0, headphone ? 0xC0 : 0);
|
|
358
|
+
if (speaker !== null && speaker !== undefined) await this._update(REG_PLAYBACK_CTL_2, 0x30, speaker ? 0x30 : 0);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Configure the speaker channels.
|
|
363
|
+
* @param {boolean} mono - Mix both channels to mono.
|
|
364
|
+
* @param {boolean} swap - Swap the speaker channels.
|
|
365
|
+
* @param {boolean} [mute5050=false] - Mute as an exact 50 % duty cycle.
|
|
366
|
+
* @returns {Promise<void>}
|
|
367
|
+
*/
|
|
368
|
+
async setSpeakerMode(mono, swap, mute5050 = false) {
|
|
369
|
+
await this._update(REG_PLAYBACK_CTL_2, 0x07, (mono ? 2 : 0) | (swap ? 4 : 0) | (mute5050 ? 1 : 0));
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Write CLOCKING_CTL.
|
|
374
|
+
* @param {object} [o]
|
|
375
|
+
* @param {boolean} [o.auto=true] - Auto-detect the speed mode (slave).
|
|
376
|
+
* @param {number} [o.speed=1] - Speed mode 0–3.
|
|
377
|
+
* @param {boolean} [o.group32k=false] - 8, 16 or 32 kHz sample rates.
|
|
378
|
+
* @param {boolean} [o.videoClk=false] - MCLK is 27 MHz.
|
|
379
|
+
* @param {number} [o.ratio=0] - Internal ratio code 0–3 (128, 125, 132, 136).
|
|
380
|
+
* @param {boolean} [o.mclkDiv2=false] - Divide MCLK by 2.
|
|
381
|
+
* @returns {Promise<void>}
|
|
382
|
+
*/
|
|
383
|
+
async setClocking({ auto = true, speed = 1, group32k = false, videoClk = false, ratio = 0, mclkDiv2 = false } = {}) {
|
|
384
|
+
if (!(speed >= 0 && speed <= 3 && ratio >= 0 && ratio <= 3)) throw new RangeError('speed and ratio must be 0-3');
|
|
385
|
+
await this._write(REG_CLOCKING_CTL,
|
|
386
|
+
(auto ? 0x80 : 0) | (speed << 5) | (group32k ? 0x10 : 0) | (videoClk ? 0x08 : 0) | (ratio << 1) | (mclkDiv2 ? 1 : 0));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Configure the serial audio port (INTERFACE_CTL_1 / INTERFACE_CTL_2).
|
|
391
|
+
* @param {object} [o]
|
|
392
|
+
* @param {boolean} [o.master=false] - Master mode.
|
|
393
|
+
* @param {number} [o.fmt=FMT.I2S] - `FMT.LJ`, `FMT.I2S` or `FMT.RJ`.
|
|
394
|
+
* @param {number} [o.wordLength=0] - AWL code 0–3.
|
|
395
|
+
* @param {boolean} [o.dsp=false] - DSP mode (requires `FMT.LJ`).
|
|
396
|
+
* @param {boolean} [o.invertSclk=false] - Invert SCLK polarity.
|
|
397
|
+
* @param {boolean} [o.sclkEqMclk=false] - SCLK = MCLK.
|
|
398
|
+
* @param {boolean} [o.invertSwitch=false] - Invert the SPKR/HP_SW sense.
|
|
399
|
+
* @returns {Promise<void>}
|
|
400
|
+
*/
|
|
401
|
+
async setInterface({ master = false, fmt = FMT.I2S, wordLength = 0, dsp = false, invertSclk = false,
|
|
402
|
+
sclkEqMclk = false, invertSwitch = false } = {}) {
|
|
403
|
+
if (!(fmt >= 0 && fmt <= 2 && wordLength >= 0 && wordLength <= 3)) throw new RangeError('fmt must be 0-2 and wordLength 0-3');
|
|
404
|
+
if (dsp && fmt !== FMT.LJ) throw new RangeError('DSP mode requires FMT.LJ');
|
|
405
|
+
await this._write(REG_INTERFACE_CTL_1,
|
|
406
|
+
(master ? 0x80 : 0) | (invertSclk ? 0x40 : 0) | (dsp ? 0x10 : 0) | (fmt << 2) | wordLength);
|
|
407
|
+
await this._write(REG_INTERFACE_CTL_2, (sclkEqMclk ? 0x40 : 0) | (invertSwitch ? 0x08 : 0));
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Treble / bass shelving filters.
|
|
412
|
+
* @param {number} trebleDb - −10.5 … +12 dB (nearest 1.5 dB step).
|
|
413
|
+
* @param {number} bassDb - −10.5 … +12 dB.
|
|
414
|
+
* @param {number} [trebleCorner=0] - 0–3 (5 / 7 / 10 / 15 kHz).
|
|
415
|
+
* @param {number} [bassCorner=0] - 0–3 (50 / 100 / 200 / 250 Hz).
|
|
416
|
+
* @param {boolean} [enabled=true] - Enable the tone control.
|
|
417
|
+
* @returns {Promise<void>}
|
|
418
|
+
*/
|
|
419
|
+
async setTone(trebleDb, bassDb, trebleCorner = 0, bassCorner = 0, enabled = true) {
|
|
420
|
+
if (!(trebleCorner >= 0 && trebleCorner <= 3 && bassCorner >= 0 && bassCorner <= 3)) throw new RangeError('corner must be 0-3');
|
|
421
|
+
const treb = _clamp(8 - _roundHalfUp(trebleDb / 1.5), 0, 15);
|
|
422
|
+
const bass = _clamp(8 - _roundHalfUp(bassDb / 1.5), 0, 15);
|
|
423
|
+
await this._write(REG_TONE_CTL, (treb << 4) | bass);
|
|
424
|
+
await this._update(REG_BEEP_TONE_CFG, 0x1F, (trebleCorner << 3) | (bassCorner << 1) | (enabled ? 1 : 0));
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Program and start the beep generator (timing first, mode last). `BEEP.OFF` only stops it.
|
|
429
|
+
* @param {number} frequency - Code 0–15 (260.87 … 2181.82 Hz).
|
|
430
|
+
* @param {number} onTime - Code 0–15.
|
|
431
|
+
* @param {number} offTime - Code 0–7.
|
|
432
|
+
* @param {number} volumeDb - +6 … −56 dB in 2 dB steps (nearest).
|
|
433
|
+
* @param {number} mode - `BEEP.OFF` / `SINGLE` / `MULTIPLE` / `CONTINUOUS`.
|
|
434
|
+
* @param {boolean} [mix=true] - Mix the beep with the PCM stream.
|
|
435
|
+
* @returns {Promise<void>}
|
|
436
|
+
* @throws {Error} If a beep is active and `mode` is not `BEEP.OFF`.
|
|
437
|
+
*/
|
|
438
|
+
async setBeep(frequency, onTime, offTime, volumeDb, mode, mix = true) {
|
|
439
|
+
if (!(frequency >= 0 && frequency <= 15 && onTime >= 0 && onTime <= 15 && offTime >= 0 && offTime <= 7 && mode >= 0 && mode <= 3)) {
|
|
440
|
+
throw new RangeError('beep argument out of range');
|
|
441
|
+
}
|
|
442
|
+
if (mode === BEEP.OFF) { await this.stopBeep(); return; }
|
|
443
|
+
if (((await this._read(REG_BEEP_TONE_CFG)) & 0xC0) !== 0) throw new Error('beep active: call stopBeep() before changing the timing');
|
|
444
|
+
const n = _clamp(_roundHalfUp((6 - volumeDb) / 2), 0, 31);
|
|
445
|
+
await this._write(REG_BEEP_FREQ_ON, (frequency << 4) | onTime);
|
|
446
|
+
await this._write(REG_BEEP_VOL_OFF, (offTime << 5) | ((6 - n) & 0x1F));
|
|
447
|
+
await this._update(REG_BEEP_TONE_CFG, 0xE0, (mode << 6) | (mix ? 0 : 0x20));
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Stop the beep generator (BEEP[1:0] = 0).
|
|
452
|
+
* @returns {Promise<void>}
|
|
453
|
+
*/
|
|
454
|
+
async stopBeep() { await this._update(REG_BEEP_TONE_CFG, 0xC0, 0); }
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Configure the peak limiter.
|
|
458
|
+
* @param {boolean} enabled - Enable the limiter.
|
|
459
|
+
* @param {object} [o]
|
|
460
|
+
* @param {number} [o.maxDb=0] - Maximum threshold, nearest of 0, −3, −6, −9, −12, −18, −24, −30 dB.
|
|
461
|
+
* @param {number} [o.cushionDb=0] - Cushion threshold, same steps.
|
|
462
|
+
* @param {number} [o.release=0x3F] - Release rate 0 (fastest) … 63.
|
|
463
|
+
* @param {number} [o.attack=0] - Attack rate 0 … 63.
|
|
464
|
+
* @param {boolean} [o.limitAll=true] - Limit both channels together.
|
|
465
|
+
* @param {boolean} [o.softRampDisable=false] - Disable soft ramp while limiting.
|
|
466
|
+
* @param {boolean} [o.zeroCrossDisable=false] - Disable zero-cross while limiting.
|
|
467
|
+
* @returns {Promise<void>}
|
|
468
|
+
*/
|
|
469
|
+
async setLimiter(enabled, { maxDb = 0, cushionDb = 0, release = 0x3F, attack = 0, limitAll = true,
|
|
470
|
+
softRampDisable = false, zeroCrossDisable = false } = {}) {
|
|
471
|
+
if (!(release >= 0 && release <= 63 && attack >= 0 && attack <= 63)) throw new RangeError('release and attack must be 0-63');
|
|
472
|
+
await this._write(REG_LIMIT_CTL_1,
|
|
473
|
+
(_nearestLimit(maxDb) << 5) | (_nearestLimit(cushionDb) << 2) | (softRampDisable ? 2 : 0) | (zeroCrossDisable ? 1 : 0));
|
|
474
|
+
await this._write(REG_LIMIT_CTL_2, (enabled ? 0x80 : 0) | (limitAll ? 0x40 : 0) | release);
|
|
475
|
+
await this._write(REG_LIMIT_ATTACK, attack);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Soft-ramp volume transitions.
|
|
480
|
+
* @param {boolean} digital - Digital volume (DIGSFT).
|
|
481
|
+
* @param {boolean} [analogLeft=false] - Analog channel A (ANLGSFTA).
|
|
482
|
+
* @param {boolean} [analogRight=false] - Analog channel B (ANLGSFTB).
|
|
483
|
+
* @returns {Promise<void>}
|
|
484
|
+
*/
|
|
485
|
+
async setSoftRamp(digital, analogLeft = false, analogRight = false) {
|
|
486
|
+
await this._setBit(REG_MISC_CTL, 1, digital);
|
|
487
|
+
await this._update(REG_ANALOG_ZC_SR, 0x0A, (analogLeft ? 0x02 : 0) | (analogRight ? 0x08 : 0));
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Zero-cross volume transitions.
|
|
492
|
+
* @param {boolean} digital - Digital volume (DIGZC).
|
|
493
|
+
* @param {boolean} [analogLeft=false] - Analog channel A (ANLGZCA).
|
|
494
|
+
* @param {boolean} [analogRight=false] - Analog channel B (ANLGZCB).
|
|
495
|
+
* @returns {Promise<void>}
|
|
496
|
+
*/
|
|
497
|
+
async setZeroCross(digital, analogLeft = false, analogRight = false) {
|
|
498
|
+
await this._setBit(REG_MISC_CTL, 0, digital);
|
|
499
|
+
await this._update(REG_ANALOG_ZC_SR, 0x05, (analogLeft ? 0x01 : 0) | (analogRight ? 0x04 : 0));
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Digital de-emphasis filter.
|
|
504
|
+
* @param {boolean} enabled
|
|
505
|
+
* @returns {Promise<void>}
|
|
506
|
+
*/
|
|
507
|
+
async setDeemphasis(enabled) { await this._setBit(REG_MISC_CTL, 2, enabled); }
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Source of each PCM channel (PCM_MIXER_SWAP).
|
|
511
|
+
* @param {number} leftSource - `SRC.NORMAL` (0), `SRC.MIX` (1) or `SRC.SWAPPED` (3).
|
|
512
|
+
* @param {number} rightSource - Same coding.
|
|
513
|
+
* @returns {Promise<void>}
|
|
514
|
+
*/
|
|
515
|
+
async setPcmSwap(leftSource, rightSource) {
|
|
516
|
+
if (!(leftSource >= 0 && leftSource <= 3 && rightSource >= 0 && rightSource <= 3)) throw new RangeError('source must be 0-3');
|
|
517
|
+
await this._write(REG_PCM_MIXER_SWAP, (leftSource << 6) | (rightSource << 4));
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Invert the PCM polarity of each channel.
|
|
522
|
+
* @param {boolean} left
|
|
523
|
+
* @param {boolean} right
|
|
524
|
+
* @returns {Promise<void>}
|
|
525
|
+
*/
|
|
526
|
+
async invertPcm(left, right) { await this._update(REG_PLAYBACK_CTL_1, 0x0C, (left ? 0x04 : 0) | (right ? 0x08 : 0)); }
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Route the analog line inputs to the headphone outputs.
|
|
530
|
+
* @param {boolean} enabled - Enable passthrough on both channels.
|
|
531
|
+
* @param {number} [leftInput=1] - AIN1A … AIN4A (1–4).
|
|
532
|
+
* @param {number} [rightInput=1] - AIN1B … AIN4B (1–4).
|
|
533
|
+
* @returns {Promise<void>}
|
|
534
|
+
*/
|
|
535
|
+
async setPassthrough(enabled, leftInput = 1, rightInput = 1) {
|
|
536
|
+
if (!(leftInput >= 1 && leftInput <= 4 && rightInput >= 1 && rightInput <= 4)) throw new RangeError('input must be 1-4');
|
|
537
|
+
await this._write(REG_PASSTHRU_A_SEL, 0x80 | (1 << (leftInput - 1)));
|
|
538
|
+
await this._write(REG_PASSTHRU_B_SEL, 0x80 | (1 << (rightInput - 1)));
|
|
539
|
+
await this._update(REG_MISC_CTL, 0xC0, enabled ? 0xC0 : 0);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Passthrough volume.
|
|
544
|
+
* @param {number} leftDb - −60 … +12 dB.
|
|
545
|
+
* @param {number} [rightDb] - Right channel; defaults to `leftDb`.
|
|
546
|
+
* @returns {Promise<void>}
|
|
547
|
+
*/
|
|
548
|
+
async setPassthroughVolume(leftDb, rightDb = leftDb) {
|
|
549
|
+
await this._write(REG_PASSTHRU_A_VOL, _halfDbCode(leftDb, -60, 12));
|
|
550
|
+
await this._write(REG_PASSTHRU_B_VOL, _halfDbCode(rightDb, -60, 12));
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Mute the passthrough channels.
|
|
555
|
+
* @param {boolean} left
|
|
556
|
+
* @param {boolean} [right] - Defaults to `left`.
|
|
557
|
+
* @returns {Promise<void>}
|
|
558
|
+
*/
|
|
559
|
+
async mutePassthrough(left, right = left) {
|
|
560
|
+
await this._setBit(REG_MISC_CTL, 4, left);
|
|
561
|
+
await this._setBit(REG_MISC_CTL, 5, right);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Hold (true) or apply (false) register changes (FREEZE).
|
|
566
|
+
* @param {boolean} on
|
|
567
|
+
* @returns {Promise<void>}
|
|
568
|
+
*/
|
|
569
|
+
async freeze(on) { await this._setBit(REG_MISC_CTL, 3, on); }
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Battery compensation for the speaker supply VP.
|
|
573
|
+
* @param {boolean} enabled
|
|
574
|
+
* @param {number} [vpRefIndex=0] - VPREF code 0–15.
|
|
575
|
+
* @param {boolean} [monitor=false] - Enable the VP monitor.
|
|
576
|
+
* @returns {Promise<void>}
|
|
577
|
+
*/
|
|
578
|
+
async setBatteryCompensation(enabled, vpRefIndex = 0, monitor = false) {
|
|
579
|
+
if (!(vpRefIndex >= 0 && vpRefIndex <= 15)) throw new RangeError('vpRefIndex must be 0-15');
|
|
580
|
+
await this._write(REG_BATTERY_COMP, (enabled ? 0x80 : 0) | (monitor ? 0x40 : 0) | vpRefIndex);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* VP supply voltage.
|
|
585
|
+
* @param {number} vaVolts - The analog supply VA (1.8 or 2.5).
|
|
586
|
+
* @returns {Promise<number>} Volts: VP_LEVEL × VA / 63.3.
|
|
587
|
+
*/
|
|
588
|
+
async readVpVoltage(vaVolts) { return ((await this._read(REG_VP_LEVEL)) * vaVolts) / 63.3; }
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Read the status register — reading clears the flags.
|
|
592
|
+
* @returns {Promise<{clockError: boolean, dspAOverflow: boolean, dspBOverflow: boolean, pcmAOverflow: boolean, pcmBOverflow: boolean}>}
|
|
593
|
+
*/
|
|
594
|
+
async readStatus() {
|
|
595
|
+
const v = await this._read(REG_STATUS);
|
|
596
|
+
return {
|
|
597
|
+
clockError: !!(v & 0x40), dspAOverflow: !!(v & 0x20), dspBOverflow: !!(v & 0x10),
|
|
598
|
+
pcmAOverflow: !!(v & 0x08), pcmBOverflow: !!(v & 0x04),
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Read the speaker status register.
|
|
604
|
+
* @returns {Promise<{speakerAShort: boolean, speakerBShort: boolean, spkrHpPin: boolean}>}
|
|
605
|
+
*/
|
|
606
|
+
async readSpeakerStatus() {
|
|
607
|
+
const v = await this._read(REG_SPEAKER_STATUS);
|
|
608
|
+
return { speakerAShort: !!(v & 0x20), speakerBShort: !!(v & 0x10), spkrHpPin: !!(v & 0x08) };
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Charge-pump frequency divider (frequency = 64·Fs / (N + 2)).
|
|
613
|
+
* @param {number} n - 0–15.
|
|
614
|
+
* @returns {Promise<void>}
|
|
615
|
+
*/
|
|
616
|
+
async setChargePumpFrequency(n) {
|
|
617
|
+
if (!(n >= 0 && n <= 15)) throw new RangeError('n must be 0-15');
|
|
618
|
+
await this._update(REG_CHARGE_PUMP_FREQ, 0xF0, n << 4);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Read any documented register (reading STATUS clears it).
|
|
623
|
+
* @param {number} reg
|
|
624
|
+
* @returns {Promise<number>}
|
|
625
|
+
*/
|
|
626
|
+
async readRegister(reg) {
|
|
627
|
+
if (!READABLE.has(reg)) throw new RangeError('register is reserved');
|
|
628
|
+
return this._read(reg);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Write any documented writable register.
|
|
633
|
+
* @param {number} reg
|
|
634
|
+
* @param {number} value
|
|
635
|
+
* @returns {Promise<void>}
|
|
636
|
+
*/
|
|
637
|
+
async writeRegister(reg, value) {
|
|
638
|
+
if (!READABLE.has(reg) || READ_ONLY.has(reg)) throw new RangeError('register is reserved or read-only');
|
|
639
|
+
await this._write(reg, value);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
CS43L22Full.clockingFor = clockingFor;
|
|
644
|
+
|
|
645
|
+
module.exports = { CS43L22Minimal, CS43L22Full, clockingFor, I2C_ADDRESS, OUT, FMT, BEEP, SRC, GAIN };
|