samm_flynn
Well-known member
I'm using a Teensy 4.1, and I have a float variable (tgtfr) that's updated in serialEvent() based on serial input. This same variable is read inside a timer interrupt that fires every 1 ms.
The main loop is running very fast — over 7 MHz, based on measurements (just incrementing a counter per loop and checking it in the ISR).
My concern is about a simple assignment like:
a = b; // where both are float
Since a float is 4 bytes, is it possible that the interrupt triggers in the middle of this assignment — meaning the variable might only be partially updated when the ISR runs and reads it?
Does the Teensy 4.1 guarantee that a float assignment is atomic? Or do I need to protect access (e.g., disable interrupts briefly, which I dont want to do because it will screw up my high performance control algorithm, which expects bang-on timing to work) when reading or writing shared float variables between the main loop and an ISR?
What's the correct and safe way to handle this on this platform? I want to avoid any possibility of corrupted values — it could cause serious issues in my robot.
The main loop is running very fast — over 7 MHz, based on measurements (just incrementing a counter per loop and checking it in the ISR).
My concern is about a simple assignment like:
a = b; // where both are float
Since a float is 4 bytes, is it possible that the interrupt triggers in the middle of this assignment — meaning the variable might only be partially updated when the ISR runs and reads it?
Does the Teensy 4.1 guarantee that a float assignment is atomic? Or do I need to protect access (e.g., disable interrupts briefly, which I dont want to do because it will screw up my high performance control algorithm, which expects bang-on timing to work) when reading or writing shared float variables between the main loop and an ISR?
What's the correct and safe way to handle this on this platform? I want to avoid any possibility of corrupted values — it could cause serious issues in my robot.
C++:
void serialEvent() // measured call rate 7.25 MHz
{
int bytesAvailable = Serial.available();
if (Serial.available() < 1) return;
char buffer[bytesAvailable + 1] = { 0 };
int bytesRead = Serial.readBytes(buffer, bytesAvailable);
buffer[bytesRead] = '\0'; // Null terminate
String input(buffer);
input.trim();
float val = input.toFloat();
if (val >= 0 && val <= 100)
{
tgtfr = val;
}
}