Question
Atomic GPIO register modifications in stm32u5xx_hal_gpio.c
Hello,
We have found that, in an OS context, if two tasks change the configuration of different GPIOs belonging to the same port, then the register can get corrupted due to a RMW race.
It seems the RMW operations are not protected:
// stm32u5xx_hal_gpio.c :232
tmp = GPIOx->MODER;
tmp &= ~(LPGPIO_MODER_MOD0 << position);
tmp |= ((pGPIO_Init->Mode & GPIO_MODE_OUTPUT_PP) << position);
GPIOx->MODER = tmp;Our solution is as follows:
ATOMIC_MODIFY_REG(GPIOx->MODER,
(LPGPIO_MODER_MOD0 << position),
((pGPIO_Init->Mode & GPIO_MODE_OUTPUT_PP) << position));- Is there any reason why these operations were not atomic in the first place ?
- Do you see any issue with our solution ?
- Is there a better way to handle this ?
