SAMD21, is floating point allowed inside an ISR?

Question about SAMD21 with Arduino IDE:
Is it safe to use floating point artithmetic inside an interrupt service routine, if the main program does also use floating point?
I know from the old Keil C51 compiler that this was only allowed if some internal variables were pushed to the stack at the beginning of the ISR. But this might be totally different with SAMD21 and GCC, that’s why I ask.
Is there anything else that’s not allowed inside an ISR?

1 Like

Hi there,

AFAIK, it’s not recommended…However ;
It is generally safe to use floating-point arithmetic inside an interrupt service routine (ISR) on the SAMD21 using the GCC compiler with the Arduino IDE, provided certain conditions are met regarding the Floating Point Unit (FPU) context saving.
As always keep the ISR short for best performance , leave the FPU work for the main code, , Set a flag or change a register in the ISR only. It may work the other way but in a HEAVY loaded system as soon as you add more latency to the system it will break. Let the speed of the math determine the performance. :fight_cloud:

HTH
GL :slight_smile: PJ :v:

1 Like

wow why does floating point numbers matter…

it appears the FPA is a multi-step process that can be corrupted with interupts?

also i would say look into analog computing… is floating point precision necessary for your system…

Do you know which are these certain conditions? Does the compiler automatically make it safe? Or must I push something at the beginning of the ISR?

Michael

That’s the question. Does the compiler automatically make the floating point code interrupt-safe, or not?

That’s what I’m doing since 3 years…
https://www.astro-electronic.de/THAT_Analog_Computer_Book.pdf

Michael

1 Like

wow.. awesome work!

wow.. awesome work!

Hi there,

SO, Floating Point in ISRs : tells us this "

The GCC compiler (specifically the ARM GCC compiler used for the SAMD21’s Cortex-M0+ core) manages the context switching for the Floating Point Unit (FPU) if any floating-point instructions are used within the ISR [2, 4]. This means you do not need to add any manual code to save/restore registers as you would with older, less sophisticated compilers.

The key condition is that both the main program and the ISR must use floating-point math at least once [4]. If only one section uses it, the compiler might not automatically enable the FPU context save/restore mechanism, potentially leading to errors.

HTH
GL :slight_smile: PJ :v:

1 Like