Unable to change digital output pin value multiple times without a delay
If a digital output pin value is changed multiple times within the same main loop, within the same interrupt, or within the same task and not separated by a delay then the output pin does not work as expected.
This applies to both HAL_GPIO_TogglePin() and HAL_GPIO_WritePin()
Using the NUCLEO-G031K8 PCB with the STM32CubeIDE
Working Example:
Code in main() while loop with no interrupts, and no RTOS
HAL_GPIO_TogglePin() is called 2 times and is separated with a delay
Output pins work as expected
PB0 & PB1 toggle 1 time per ms
Channe1 = PB0, Channel2 = PB1
while (1)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);
HAL_Delay(0); //Delay 1 ms
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);
HAL_Delay(0); //Delay 1 ms
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
Example with digital output not working as expected:
Code in main() while loop with no interrupts, and no RTOS
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0) is called 2 times with no delay in between
GPIO_PIN_0 (PB0) does not toggle when it should
PB0 toggles a small random number of times (example: 4 times) then there is a large random length delay (can be hundreds of ms long)
GPIO_PIN_0 (PB1) works as expected because it is separated with delays
Channe1 = PB0, Channel2 = PB1
The main loop is called every 2 ms so the yellow trace (GPIO_PIN_0) and the blue trace (GPIO_PIN_1) should toggle each time through the main loop, but the yellow trace is not toggling when it should.
while (1)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);
HAL_Delay(0); //Delay 1 ms
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);
HAL_Delay(0); //Delay 1 ms
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
