UC GEE
UC GEE
DIIDevHeads IoT Integration Server
Created by Sterling on 7/14/2024 in #firmware-and-baremetal
Do I need a mutex to protect 8-bit variables on STM32L476 with ARMv7E-M architecture using FreeRTOS?
Good question @Sterling . You are correct 💯 that ARMv7-M, which includes the Cortex-M4, guarantees atomicity for single-byte (8-bit) operations. This means that read and write operations to 8-bit variables are atomic and do not require additional synchronization mechanisms such as mutexes. Therefore, in a single-core environment, you generally do not need to use a mutex to protect 8-bit variables. However, you should be cautious if the variable is accessed from both an interrupt service routine (ISR) and the main application code, as this might require disabling interrupts to ensure atomic access. For example:
uint8_t sharedVar;

void main(void) {
// ...
__disable_irq();
sharedVar = newValue; // Critical section
__enable_irq();
// ...
}

void ISR_Handler(void) {
sharedVar = isrValue;
}
uint8_t sharedVar;

void main(void) {
// ...
__disable_irq();
sharedVar = newValue; // Critical section
__enable_irq();
// ...
}

void ISR_Handler(void) {
sharedVar = isrValue;
}
In this case, you disable interrupts before accessing the variable and re-enable them afterward to prevent race conditions.
6 replies