AnsweredAssumed Answered

HAL_GPIO_TogglePin() not thread-safe

Question asked by David Littell on May 30, 2018
Latest reply on May 30, 2018 by waclawek.jan

STM32Cube_FW_F7_V1.11.0's HAL_GPIO_TogglePin() appears broken in a multi-threaded environment:

 

void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

{

  /* Check the parameters */

  assert_param(IS_GPIO_PIN(GPIO_Pin));

 

   GPIOx->ODR ^= GPIO_Pin;

}

 

One fix might be:

 

void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

{

  /* Check the parameters */

  assert_param(IS_GPIO_PIN(GPIO_Pin));

 

  if( (GPIOx->ODR & GPIO_Pin) != 0x0 )

    {

      GPIOx->BSRR = ((uint32_t) GPIO_Pin) << 16;   /* Set a BSRR.BRx bit so the GPIO goes low. */

    }

  else   /* the GPIO should be set high... */

    {

      GPIOx->BSRR = (uint32_t) GPIO_Pin;   /* Set a BSRR.BSx bit so the GPIO goes high. */

    }

}

 

I suspect all the HAL's may suffer from this...

Outcomes