STM32F303 CubeMX - LL_GPIO_TogglePin() - race condition?
__STATIC_INLINE void LL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint32_t PinMask)
{
WRITE_REG(GPIOx->ODR, READ_REG(GPIOx->ODR) ^ PinMask);
}I was trying out a Nucleo F303RE board and installed CubeIDE and the HAL/LL library, then stumbled upon this.
That function, part of the LL API, first takes a temporary copy of the whole GPIO output register, *then* could be interrupted by an ISR or RTOS task which may change a certain pin bit on the same GPIO port, and then control returns back to above function, which then writes, directly into the ODR of the GPIO an outdated value of ODR xor'red with PinMask - oops, reverting the pin state change made by the ISR.
Should it not be more like this?
__STATIC_INLINE void LL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint32_t PinMask)
{
if (GPIOx->ODR & PinMask)
GPIOx->BRR = PinMask;
else GPIOx->BSRR = PinMask;
}