2024-10-12 12:36 AM
Hi there,
I am using an STM32 G4 MCU for my project, and the FPU is enabled for this project. I need to implement complex floating point calculations in different ISRs with different priorities. Are there any potential risks associated with doing this? If so, are there any solutions to mitigate the problem?
A short code snippet is shown below:
#incldue <math.h>
float a;
float b;
float k = const_float_value;
float c;
float d = const_float_valued;
float e;
void isr_adc_high_handler(void)
{
a = some_float_value; //ADC value converted to a
b = some_other_float_value // ADC value converted to b
c = k * (a + b)/(a-b);
}
void isr_sys_tick_low_handler(void)
{
e = c * d; //Convert ADC sample result to e every 1ms
}
Thanks!
2024-10-12 01:07 AM
> Are there any potential risks associated with doing this?
The ISR execution will last longer than if you wouldn't use floating point, because the processor needs to stack (and unstack at ISR exit) the FPU registers. Whether this is a risk or not depends on your particular needs (e.g. required minimal latencies for interrupts of the same or lower priority).
JW
2024-10-12 04:01 AM
Dear @YWang.15 ,
I do not see a particular issue on doing that in your ISR firing each 1ms. Just ensure that hardware FPU is activated and using SP single point Float as mentioned in your code . I’m interested to know your Compiler and assembly code generated . Your can see this Application Note starting from Chapter 3 and then compare the performance versus what you observe.
Hope it helps you ,
STOne-32
2024-10-12 06:36 AM
Generally safe, need to VPUSH/VPOP context at the IRQHandler, might not be done auto-magically if the top-level function lacks any float code the compiler can identify. Check generated code and if you need some explicit attribute or #pragma
float's don't have a lot of precision and the FPU doesn't hold intermediate precision.