From d613b079ef05e2f0411b5865173b87fc0b08f832 Mon Sep 17 00:00:00 2001 From: Evan Reichard Date: Sun, 26 Jul 2026 11:18:28 -0400 Subject: [PATCH] feat(xteink): count guest ADC reads per channel Host tooling needs to know when the guest has actually sampled a button, since the firmware only polls the resistor ladder between e-ink refreshes. --- hw/adc/esp32c3_adc.c | 11 ++++++++--- include/hw/adc/esp32c3_adc.h | 2 ++ scripts/{xteink-emu.py => xteink/emu.py} | 0 3 files changed, 10 insertions(+), 3 deletions(-) rename scripts/{xteink-emu.py => xteink/emu.py} (100%) diff --git a/hw/adc/esp32c3_adc.c b/hw/adc/esp32c3_adc.c index 4d38b3e13c..2f60dce175 100644 --- a/hw/adc/esp32c3_adc.c +++ b/hw/adc/esp32c3_adc.c @@ -26,9 +26,11 @@ static uint64_t esp32c3_adc_read(void *opaque, hwaddr addr, unsigned size) return ADC_DONE; } if (addr == ADC_DATA1 || addr == ADC_DATA2) { - return s->channel < ESP32C3_ADC_CHANNELS - ? MIN(qatomic_read(&s->input[s->channel]), ADC_MAX) - : ADC_MAX; + if (s->channel >= ESP32C3_ADC_CHANNELS) { + return ADC_MAX; + } + qatomic_inc(&s->samples[s->channel]); + return MIN(qatomic_read(&s->input[s->channel]), ADC_MAX); } return s->regs[addr / 4]; } @@ -60,6 +62,7 @@ static void esp32c3_adc_reset_hold(Object *obj, ResetType type) memset(s->regs, 0, sizeof(s->regs)); for (int i = 0; i < ESP32C3_ADC_CHANNELS; i++) { s->input[i] = ADC_MAX; + s->samples[i] = 0; } s->channel = 0; } @@ -75,6 +78,8 @@ static void esp32c3_adc_init(Object *obj) for (int i = 0; i < ESP32C3_ADC_CHANNELS; i++) { object_property_add_uint32_ptr(obj, "adci[*]", &s->input[i], OBJ_PROP_FLAG_READWRITE); + object_property_add_uint32_ptr(obj, "adcsamples[*]", &s->samples[i], + OBJ_PROP_FLAG_READ); } esp32c3_adc_reset_hold(obj, RESET_TYPE_COLD); } diff --git a/include/hw/adc/esp32c3_adc.h b/include/hw/adc/esp32c3_adc.h index 1b914e18da..7fb176f431 100644 --- a/include/hw/adc/esp32c3_adc.h +++ b/include/hw/adc/esp32c3_adc.h @@ -13,5 +13,7 @@ struct ESP32C3AdcState { MemoryRegion iomem; uint32_t regs[ESP32C3_ADC_IO_SIZE / sizeof(uint32_t)]; uint32_t input[ESP32C3_ADC_CHANNELS]; + /* Sample counters let a host driver hold a button until the guest has actually read it. */ + uint32_t samples[ESP32C3_ADC_CHANNELS]; uint8_t channel; }; diff --git a/scripts/xteink-emu.py b/scripts/xteink/emu.py similarity index 100% rename from scripts/xteink-emu.py rename to scripts/xteink/emu.py