I was working on a project with a nucelo f303k8 and trying to get audio in and out via analogread and analogout at 32khz via timer interrupt. I was using an array as a short delay line for an audio effect.
I suspected the 303 might not be able to keep up, but even removing a couple float calculations for filtering and lowering the interrupt to 100uS it would work for a minute and then eventually become unresponsive.
I did get it kind of working on a nucleo F401re, but had to use pwm out. The standard pwm library doesn’t really have the resolution for it though.
I got it competently working with a nucleo f446re.
This is just bare bones though and I’m concerned about moving forward so I did some testing using a 1,000,000 for loop running the same instruction over.
partial code:
a = timer.read_us();
for( int i = 0; i < 1000000; i++) {
tempFloat = input;
}
c= timer.read_us();
c= c - emptyLoopTime;
tempFloat = (c-a) / 1000000.0f;
printf("filter aRead uS %f", tempFloat);
tempFloat = tempFloat *64;
printf("1 aRead clock ticks %f", tempFloat);
printf("1M aRead %d \n", c-a);
emptyLoopTime is from an empty for loop to subtract the loop overhead.
I found a single analog read on the 303 was taking about 680 clock cycles or 10.3 uS.
on the 401 it was about 453 cycles or 5.4 uS.
time difference betwee read_u16() and float was minimal.
AnalogOut took about 2 uS, but that’s totally manageable.
This float calculation took about the same amount of time. (float not double)
lpfBuffer = lpfBuffer + ((input - lpfBuffer) * .45f);
(I also tried this on mbed studio and everything was significantly significantly slower)
I believe analogread is slow because mbed waits for the ADC to stabilize. I assume this is because there are a lot more ADC pins than internal ADCs and switching between them too quickly would cause poor read quality. Is this right?
I tried looking into DMA, but I think it’s over my head. I know there are libraries for other boards for faster analogread, but couldn’t find much info regarding STM32s.
I knew the ADC wouldn’t be super fast though. the float calculation was more surprising as I was hoping to add more math into the loops. I thought the FPU was enabled on both of these boards. Should I be doing my math differently?
Any suggestions or explanations would be appreciated.
Thanks