cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_Delay() hanging in main() function.

hhuyn.1
Associate II

I write a simple blinking project on STM32F4 Discovery board.

It calls HAL_Delay() function for blinking effect on one LED.

But the code hanging when reach HAL_Delay().

When I debug, the HAL_Delay() stuck at while() loop in there.

Here my code :

#include <stm32f4xx.h>
 
 
int main()
{
	RCC_OscInitTypeDef x0;
	RCC_ClkInitTypeDef x1;
	RCC_PLLInitTypeDef x2;
	GPIO_InitTypeDef x3;
	
	x2.PLLSource = RCC_OSCILLATORTYPE_HSE;
	x2.PLLM = 10;
	x2.PLLN = 250;
	x2.PLLP = RCC_PLLP_DIV2;
	x2.PLLQ = 5;
	
	x1.ClockType = RCC_CLOCKTYPE_SYSCLK;
	x1.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
	x1.AHBCLKDivider = RCC_SYSCLK_DIV1;
	x1.APB1CLKDivider = RCC_HCLK_DIV4;
	x1.APB2CLKDivider = RCC_HCLK_DIV4;
	
	x0.OscillatorType = RCC_OSCILLATORTYPE_HSE;
	x0.HSEState = RCC_HSE_ON;
	x0.PLL = x2;
	
	x3.Pin = GPIO_PIN_13;
	x3.Mode = GPIO_MODE_OUTPUT_OD;
	x3.Pull = GPIO_PULLUP;
	x3.Speed = GPIO_SPEED_FREQ_LOW;
	
	HAL_Init();
	HAL_RCC_OscConfig(&x0);
	HAL_RCC_ClockConfig(&x1, 5);
	
	__HAL_RCC_GPIOD_CLK_ENABLE();
	HAL_GPIO_Init(GPIOD, &x3);
	
	while(1)
	{
		HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13);
		HAL_Delay(1000);
	}
}

3 REPLIES 3
hhuyn.1
Associate II

In Keil C, I trace reversely the HAL_Init() functions. I guess it does not initial Systick completely.

At the end, one function in "core_cm4.h" is displayed blurry.

it's here :

/* ##################################    SysTick function  ############################################ */
/**
  \ingroup  CMSIS_Core_FunctionInterface
  \defgroup CMSIS_Core_SysTickFunctions SysTick Functions
  \brief    Functions that configure the System.
  @{
 */
 
#if defined (__Vendor_SysTickConfig) && (__Vendor_SysTickConfig == 0U)
 
/**
  \brief   System Tick Configuration
  \details Initializes the System Timer and its interrupt, and starts the System Tick Timer.
           Counter is in free running mode to generate periodic interrupts.
  \param [in]  ticks  Number of ticks between two interrupts.
  \return          0  Function succeeded.
  \return          1  Function failed.
  \note    When the variable <b>__Vendor_SysTickConfig</b> is set to 1, then the
           function <b>SysTick_Config</b> is not included. In this case, the file <b><i>device</i>.h</b>
           must contain a vendor-specific implementation of this function.
 */
__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
{
  if ((ticks - 1UL) > SysTick_LOAD_RELOAD_Msk)
  {
    return (1UL);                                                   /* Reload value impossible */
  }
 
  SysTick->LOAD  = (uint32_t)(ticks - 1UL);                         /* set reload register */
  NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */
  SysTick->VAL   = 0UL;                                             /* Load the SysTick Counter Value */
  SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
                   SysTick_CTRL_TICKINT_Msk   |
                   SysTick_CTRL_ENABLE_Msk;                         /* Enable SysTick IRQ and SysTick Timer */
  return (0UL);                                                     /* Function successful */
}
 
#endif
 
/*@} end of CMSIS_Core_SysTickFunctions */

Javier1
Principal

It looks like youre writting the setup functions yourself.

Please use Human Readable names for your structs, everyone including you will be happier (maybe OsciInit instead of x0).

My guess is: you got the NVIC stuff wrong.

im just going to take a look at what cubeMX generates (for my stm32f205) and compare it to your code (you should do the same for your stm32f4)

int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();

Inside HAL_Init i would take a look to what is going on in HAL_InitTick() this may give you some clues.

HAL_StatusTypeDef HAL_Init(void)
{
  /* Configure Flash prefetch, Instruction cache, Data cache */ 
#if (INSTRUCTION_CACHE_ENABLE != 0U)
   __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
#endif /* INSTRUCTION_CACHE_ENABLE */
 
#if (DATA_CACHE_ENABLE != 0U)
   __HAL_FLASH_DATA_CACHE_ENABLE();
#endif /* DATA_CACHE_ENABLE */
 
#if (PREFETCH_ENABLE != 0U)
  __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
#endif /* PREFETCH_ENABLE */
 
  /* Set Interrupt Group Priority */
  HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
 
  /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
  HAL_InitTick(TICK_INT_PRIORITY);
  
  /* Init the low level hardware */
  HAL_MspInit();
  
  /* Return function status */
  return HAL_OK;
}

Then compare your clock initializtion to this(from a stm32f205)

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 240;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 5;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }
}

SysTick shouldn't be hard to start properly using standard functions..​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..