Skip to content

Commit 8268acf

Browse files
authored
Fix Pin is not configured as analog channel error
Forum conversation on the issue here: https://community.simplefoc.com/t/pin-is-not-configured-as-analog-channel/6751/5 Current sense align is currently not working on ESP32 boards due to a `__analogChannelConfig(): Pin is not configured as analog channel` error during `current_sense.init()`. The error is coming from [this function](https://github.com/espressif/arduino-esp32/blob/bda7c4811728e538b4ea393a5be148cbb614daed/cores/esp32/esp32-hal-adc.c#L157C38-L172) in the `arduino-esp32` framework. It is checking to see if the pin type is `ESP32_BUS_TYPE_ADC_ONESHOT`. In `arduino-esp32`, it appears that the `ESP32_BUS_TYPE_ADC_ONESHOT` setting is only applied to a pin in the `__analogInit` function, which in turn is only ever invoked during an `analogRead` (or `analogReadMilliVolts`, which SimpleFOC doesn’t use). This is mainly a bug in `arduino-esp32` (since `analogSetPinAttenuation` should be able to work before an `analogRead` is called). But we can (and should) work around this in the SimpleFOC library since it may be a long time until a fix is released upstream. We can ensure the pin is initialised correctly by reversing the order of the `analogSetPinAttenuation` and the `analogRead` calls in the hardware specific driver for ESP32.
1 parent 7c80c9a commit 8268acf

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/current_sense/hardware_specific/esp32/esp32_adc_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ bool IRAM_ATTR adcInit(uint8_t pin){
162162
analogReadResolution(SIMPLEFOC_ADC_RES);
163163
}
164164
pinMode(pin, ANALOG);
165-
analogSetPinAttenuation(pin, SIMPLEFOC_ADC_ATTEN);
166165
analogRead(pin);
166+
analogSetPinAttenuation(pin, SIMPLEFOC_ADC_ATTEN);
167167

168168
#if CONFIG_IDF_TARGET_ESP32 // if esp32 variant
169169
__configFastADCs();

0 commit comments

Comments
 (0)