Minimal code to replicate on my Sparkfun Thing Plus ESP32s3:
#include "Arduino.h"
void setup() {
Serial.begin(115200);
delay(1000); // Interestingly, the error isn't printed if we remove this delay (but the other printouts are)
int pin = 11;
analogSetPinAttenuation(pin, ADC_11db); // Error occurs here after reset button is pressed on Sparkfun Thing Plus ESP32s3
}
void loop() {
}
So it appears the problem is not in the SimpleFOC library itself.
Interestingly, the error no longer occurs if I do an analogRead
before setting the attenuation:
#include "Arduino.h"
void setup() {
Serial.begin(115200);
delay(1000); // Interestingly, the error isn't printed if we remove this delay
int pin = 11;
analogRead(pin);
analogSetPinAttenuation(pin, ADC_11db); // Error no longer occurs
}
void loop() {
}
Delving into arduino-esp32
a bit more deeply… 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 looks like a bug in arduino-esp32
, since analogSetPinAttenuation
should be able to work before an analogRead
is called. But we can work around it in the SimpleFOC library by reversing the order of the analogSetPinAttenuation
and the analogRead
calls in the hardware specific driver for ESP32 here.
Pull request here: Fix `Pin is not configured as analog channel` error by jeremiahrose · Pull Request #458 · simplefoc/Arduino-FOC · GitHub
We can’t work around it in our sketches by manually calling analogRead
, unfortunately, because the pins are reset by a pinMode(pin, ANALOG)
within the current_sense.init()
.