From f5331e0a0530bda33c598e209734ebf0d14dde27 Mon Sep 17 00:00:00 2001 From: Felipe Neves Date: Wed, 15 Oct 2025 16:44:04 -0300 Subject: [PATCH 1/2] ESP32: replaced the mod base circular increment of the ADC buffer with a branch based option to avoid the compiler to use a MOD instruction that consumes several cycles. Signed-off-by: Felipe Neves --- .../hardware_specific/esp32/esp32_mcpwm_mcu.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp b/src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp index 029daa64..65a534fa 100644 --- a/src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp +++ b/src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp @@ -106,7 +106,11 @@ static bool IRAM_ATTR _mcpwmTriggerADCCallback(mcpwm_timer_handle_t tim, const m // sample the phase currents one at a time // ESP's adc read takes around 10us which is very long // increment buffer index - p->buffer_index = (p->buffer_index + 1) % p->no_adc_channels; + p->buffer_index = (p->buffer_index + 1); + if(p->buffer_index == p->no_adc_channels){ + p->buffer_index = 0; + } + // so we are sampling one phase per call p->adc_buffer[p->buffer_index] = adcRead(p->pins[p->buffer_index]); From 9c616baedabc0f50be46531b2fb1cf6c696860b1 Mon Sep 17 00:00:00 2001 From: Felipe Neves Date: Thu, 16 Oct 2025 08:51:44 -0300 Subject: [PATCH 2/2] ESP32: reorder the ADC buffer increment To happen after the ADC reading to avoid shifting the order of the samples. Signed-off-by: Felipe Neves --- .../hardware_specific/esp32/esp32_mcpwm_mcu.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp b/src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp index 65a534fa..51b200ff 100644 --- a/src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp +++ b/src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp @@ -105,15 +105,15 @@ static bool IRAM_ATTR _mcpwmTriggerADCCallback(mcpwm_timer_handle_t tim, const m // sample the phase currents one at a time // ESP's adc read takes around 10us which is very long + // so we are sampling one phase per call + p->adc_buffer[p->buffer_index] = adcRead(p->pins[p->buffer_index]); + // increment buffer index - p->buffer_index = (p->buffer_index + 1); - if(p->buffer_index == p->no_adc_channels){ + p->buffer_index++; + if(p->buffer_index >= p->no_adc_channels){ p->buffer_index = 0; } - // so we are sampling one phase per call - p->adc_buffer[p->buffer_index] = adcRead(p->pins[p->buffer_index]); - #ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG // debugging toggle pin to measure the time of the interrupt with oscilloscope gpio_set_level(GPIO_NUM,0); //cca 250ns for on+off #endif