Delay in HAL Library (HAL_Delay())

81.5k views Asked by At

I'm trying to blink LEDs on my STM32F4 Discovery. Somehow it's stuck on the delay function. I have changed the SysTick interrupt priority to 0 and added the IncTick(), GetTick() functions. What am I missing?

#include "stm32f4xx.h" // Device header
#include "stm32f4xx_hal.h" // Keil::Device:STM32Cube HAL:Common

int main() {
  HAL_Init();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  GPIO_InitTypeDef GPIO_InitStruct;
  GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15, GPIO_PIN_SET);
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  HAL_IncTick();
  HAL_GetTick();
  HAL_Delay(100);
  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15, GPIO_PIN_RESET);
}
1

There are 1 answers

0
denis krasutski On BEST ANSWER

Function HAL_IncTick() is called from SysTick_Handler() interrupt, which is usually implemented in stm32f4xx_it.c. You don't call this function from your code!

void SysTick_Handler(void)
{
    HAL_IncTick();
}

The function HAL_Init() initializes the SysTick timer to a 1ms interval and enables the associated interrupt. So, after calling HAL_Init() the function HAL_Delay() should be work properly.

NOTE: The STM32 HAL allows you to override (see keyword __weak) functions:

  • HAL_InitTick()
  • HAL_IncTick()
  • HAL_GetTick()
  • HAL_Delay()

So if you want to use the default delay mechanism you should not implement these functions in your code!