fix(esp32): el mapa GPIO->canal ADC del shim pregunta al puente

setAdcVoltage/setAdcWaveform solo conocian el mapa del ESP32 clasico
(GPIO32..39): en un S3 devolvian false para TODOS los pines y el part no
podia alimentar el ADC. Ahora el shim consulta bridge.adcChannelForGpio si
existe (los puentes js exponen el mapa de su chip) y cae al mapa clasico
solo si no.
This commit is contained in:
David Montero Crespo 2026-07-28 04:15:04 +02:00
parent bca971bbb4
commit a419b13cb1
1 changed files with 14 additions and 7 deletions

View File

@ -215,16 +215,25 @@ class Esp32BridgeShim {
* Returns true if the pin is a valid ADC pin.
*/
setAdcVoltage(pin: number, voltage: number): boolean {
let channel = -1;
if (pin >= 36 && pin <= 39)
channel = pin - 36; // GPIO 36→CH0, 37→CH1, 38→CH2, 39→CH3
else if (pin >= 32 && pin <= 35) channel = pin - 28; // GPIO 32→CH4, 33→CH5, 34→CH6, 35→CH7
const channel = this.adcChannelForPin(pin);
if (channel < 0) return false;
const millivolts = Math.round(voltage * 1000);
this.bridge.setAdc(channel, millivolts);
return true;
}
/** GPIO -> ADC channel. The mapping is CHIP-specific, so ask the bridge
* when it knows its chip (S3: GPIO1..10 -> ADC1 ch0..9; C-family differs);
* fall back to the classic ESP32 map, which was the only one this shim
* handled before and silently returned false for every S3 pin. */
private adcChannelForPin(pin: number): number {
const b = this.bridge as unknown as { adcChannelForGpio?: (gpio: number) => number };
if (typeof b.adcChannelForGpio === 'function') return b.adcChannelForGpio(pin);
if (pin >= 36 && pin <= 39) return pin - 36; // GPIO 36→CH0 .. 39→CH3
if (pin >= 32 && pin <= 35) return pin - 28; // GPIO 32→CH4 .. 35→CH7
return -1;
}
/**
* Push a 12-bit waveform LUT to QEMU for per-read ADC interpolation.
* Call once per SPICE `.tran` solve; QEMU interpolates at every MMIO
@ -234,9 +243,7 @@ class Esp32BridgeShim {
* `samples` are 12-bit raw values (0-4095) aligned on a uniform grid.
*/
setAdcWaveform(pin: number, samples: Uint16Array, periodNs: number): boolean {
let channel = -1;
if (pin >= 36 && pin <= 39) channel = pin - 36;
else if (pin >= 32 && pin <= 35) channel = pin - 28;
const channel = this.adcChannelForPin(pin);
if (channel < 0) return false;
this.bridge.setAdcWaveform(channel, samples, periodNs);
return true;