analogRead slow with ST libraries
Posted: Sat Mar 13, 2021 7:25 pm
Hello!
This is a question I originally asked on the Arduino forums but folks thought I might have better luck here - so here I am!
My question is simple though I suspect it might have a complex answer. I'm doing a project with an STM32F103 "blue pill" board and need fast sampling to read some characteristics of a 60 Hz waveform (this is for a generator monitor).
I have both "blue pill" board definitions installed... the STMicroelectronics ones and the "stm32duino" ones. But I'm stuck using the former because the parallel LCD I'm using seems to require the "MCUFRIEND_kbv" library and that library doesn't place nice with the stm32duino board def (it gives compiler errors).
Anyhow here's the problem... analogRead() is WAY slower with the ST board definition than the other one. I wrote this very basic sketch to test the speed with each:
Using the ST board definitions, t takes about 5645 microseconds to do the 100 conversions, so 56.45 uSec per conversion (including the loop instructions but those only take about 1 uSec, I checked). Using the stm32duino board definitions, it only takes 698 microseconds... almost 10 times faster!
Does anyone know anything that can be done about that? I know there are other more complex ways to access the A/D like interleaving and DMA but I'd prefer to keep things simple. And it just doesn't make sense that the exact same function would be almost 10 times faster with one board definition than another.
This is a question I originally asked on the Arduino forums but folks thought I might have better luck here - so here I am!
My question is simple though I suspect it might have a complex answer. I'm doing a project with an STM32F103 "blue pill" board and need fast sampling to read some characteristics of a 60 Hz waveform (this is for a generator monitor).
I have both "blue pill" board definitions installed... the STMicroelectronics ones and the "stm32duino" ones. But I'm stuck using the former because the parallel LCD I'm using seems to require the "MCUFRIEND_kbv" library and that library doesn't place nice with the stm32duino board def (it gives compiler errors).
Anyhow here's the problem... analogRead() is WAY slower with the ST board definition than the other one. I wrote this very basic sketch to test the speed with each:
Code: Select all
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(2500);
}
void loop() {
// put your main code here, to run repeatedly:
int temp;
unsigned long time_start = micros();
for (int i = 0; i < 100; i++){
temp = analogRead(PA0);
}
unsigned long time_end = micros();
Serial.println(time_end - time_start);
delay(1000);
}
Does anyone know anything that can be done about that? I know there are other more complex ways to access the A/D like interleaving and DMA but I'd prefer to keep things simple. And it just doesn't make sense that the exact same function would be almost 10 times faster with one board definition than another.