HAL_Delay() Not Working Before OS Kernel Start.
Hi,
I need to initialize a display by calling my function in main.c. This function uses the I2C4 channel and requires implementing some delays.
Since it uses I2C4, it must be placed after the CubeMX Init functions, specifically in /* USER CODE 2 */. Doing so ensures that the I2C4-related part of the function works correctly. However, the delay mechanism does not function as expected.
TimeBase is already set to TIM6 (instead of SysTick which is used by FreeRTOS).
I initially tried using HAL_Delay(), which should work because the OS kernel has not yet started.
While it works well when I place it in the /* USER CODE BEGIN SysInit */ section but not in the /* USER CODE BEGIN 2 */ section.
It gets stuck inside HAL_Delay(). The HAL_GetTick() function within HAL_Delay() continuously returns the same value, meaning uwTick is never updated.
I also attempted using osDelay(), but since the kernel is not yet running at that point, it does not work either.
How can I resolve this issue while using HAL_Delay(), osDelay(), or another portable approach (i.e., avoiding simple for loops)? Would rearranging the code help?
Below are the main.c and the code snippets where HALDelay() gets stuck.
main()
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the System Power */
SystemPower_Config();
/* Configure the system clock */
SystemClock_Config();
/* Configure the peripherals common clocks */
PeriphCommonClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LTDC_Init();
MX_RTC_Init();
MX_OCTOSPI1_Init();
MX_ADC1_Init();
MX_TIM1_Init();
MX_TIM8_Init();
MX_UART5_Init();
MX_ICACHE_Init();
MX_DSIHOST_DSI_Init();
MX_CRC_Init();
MX_I2C4_Init();
MX_TouchGFX_Init();
/* Call PreOsInit function */
MX_TouchGFX_PreOSInit();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Init scheduler */
osKernelInitialize();
/* Call init function for freertos objects (in app_freertos.c) */
MX_FREERTOS_Init();
/* Start scheduler */
osKernelStart();
}
The issue occurs because HAL_Delay() gets stuck in the while loop inside the following implementation:
__weak void HAL_Delay(uint32_t Delay)
{
uint32_t tickstart = HAL_GetTick();
uint32_t wait = Delay;
/* Add a freq to guarantee minimum wait */
if (wait < HAL_MAX_DELAY)
{
wait += (uint32_t)(uwTickFreq);
}
while ((HAL_GetTick() - tickstart) < wait)
{
}
}
More specifically, HAL_GetTick() does not update uwTick:
__weak uint32_t HAL_GetTick(void)
{
return uwTick;
}
Thank you
Rick
