Do I need a mutex to protect 8-bit variables on STM32L476 with ARMv7E-M architecture using FreeRTOS?

Good day @Middleware & OS , I am using an STM32L476 with CMSIS OS2, which implements FreeRTOS v10.3.1. The STM32L476 is based on a Cortex-M4 MCU that follows the ARMv7E-M architecture: https://en.wikipedia.org/wiki/ARM_Cortex-M.

The key difference between ARMv7E-M and ARMv7-M is the inclusion of DSP extensions in ARMv7E-M, which I do not use in my application. Hence, functionally, my setup is akin to ARMv7-M: https://developer.arm.com/documentation/ddi0403/latest (Page A1-22).

According to the ARM reference manual, ARMv7-M guarantees atomicity for single-byte (8-bit) operations: https://developer.arm.com/documentation/ddi0403/latest (Page A3-80).

Given this, do I still need to use a mutex to protect 8-bit variables (e.g., uint8_t and int8_t) in my application?

I have found discussions on the need for mutexes with 32-bit variables and for non-ARM Cortex M4 environments, but I seek clarity specifically for this scenario.

My expectation is that a mutex is not required for 8-bit variable types, but I would like to confirm this understanding.
Solution
Yes @Sterling , multi-byte variables like 16-bit and 32-bit types are not guaranteed to be atomic on ARM Cortex-M4. For these variables, you should use a mutex or other synchronization mechanism to protect access if they are shared between tasks. For example, you can use a FreeRTOS mutex:

uint32_t sharedVar;
SemaphoreHandle_t xMutex;

void main(void) {
    xMutex = xSemaphoreCreateMutex();
    if(xMutex != NULL) {
        if(xSemaphoreTake(xMutex, portMAX_DELAY) == pdTRUE) {
            sharedVar = newValue;
            xSemaphoreGive(xMutex);
        }
    }
}

void ISR_Handler(void) {
    // Ensure no preemption and data corruption
    taskENTER_CRITICAL();
    sharedVar = isrValue;
    taskEXIT_CRITICAL();
}


In this case, the mutex ensures that only one task can access the variable at a time. For access from ISRs, you use taskENTER_CRITICAL() and taskEXIT_CRITICAL() to protect the critical section.
Was this page helpful?