cancel
Showing results for 
Search instead for 
Did you mean: 

HAL Delay () function does not work with HAL_UART_Receive()

xmart
Associate II

Hi everyone

I'm having this problem while using stm32L073RBT:

When I use HAL_UART_Receive() only it seems normal, but when I use HAL Delay () after or before HAL_UART_Receive() the program freezes,Knowing that I did not enable any interrupt!!

Thanks

11 REPLIES 11
prain
Senior III

Actually you have to enable SYSTICK interrupt. without this interrupt, HAL Delay () will not work .

Thanks for your answer

interrupt for SYSTICK is enabled by default because I am generating the code with stm32cubmx, and I did a simple test like this:

int i=0;

while(1)

{

i++;

HAL Delay (500) ;

}

The delay function works!!

but when using HAL_UART_Receive() function it does not work

maybe there is interference or something

never heard about such an interference. Things may become clearer if you show more code.

LMI2
Lead

Stupid question perhaps, but where and what exactly does get stuck. Do you for example have a very long timeout.

sure

interference=Translation error

When I am debugging: the variable i is actually incrementing, but no reception from uart

Also, when you click on the Step Over, the program skips HAL_UART_Receive (& huart1, buf, 200,500);

code:

#include "main.h"

#include "stm32l0xx_hal.h"

UART_HandleTypeDef huart1;

UART_HandleTypeDef huart2;

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART1_UART_Init(void);

static void MX_USART2_UART_Init(void);

   char buf[200];

   int i=0;

int main(void)

{

 HAL_Init();

 SystemClock_Config();

 MX_GPIO_Init();

 MX_USART1_UART_Init();

 MX_USART2_UART_Init();

 while (1)

 {

HAL_UART_Receive(&huart1,buf,200,500);

      HAL_Delay(500);

      i++;

 }

}

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct;

 RCC_ClkInitTypeDef RCC_ClkInitStruct;

 RCC_PeriphCLKInitTypeDef PeriphClkInit;

   /**Configure the main internal regulator output voltage

   */

 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

   /**Initializes the CPU, AHB and APB busses clocks

   */

 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.PLLMUL = RCC_PLLMUL_8;

 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

   _Error_Handler(__FILE__, __LINE__);

 }

   /**Initializes the CPU, AHB and APB busses 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_DIV1;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)

 {

   _Error_Handler(__FILE__, __LINE__);

 }

 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2;

 PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;

 PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;

 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

 {

   _Error_Handler(__FILE__, __LINE__);

 }

   /**Configure the Systick interrupt time

   */

 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

   /**Configure the Systick

   */

 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

 /* SysTick_IRQn interrupt configuration */

 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/* USART1 init function */

static void MX_USART1_UART_Init(void)

{

 huart1.Instance = USART1;

 huart1.Init.BaudRate = 9600;

 huart1.Init.WordLength = UART_WORDLENGTH_8B;

 huart1.Init.StopBits = UART_STOPBITS_1;

 huart1.Init.Parity = UART_PARITY_NONE;

 huart1.Init.Mode = UART_MODE_TX_RX;

 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;

 huart1.Init.OverSampling = UART_OVERSAMPLING_16;

 huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

 huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

 if (HAL_UART_Init(&huart1) != HAL_OK)

 {

   _Error_Handler(__FILE__, __LINE__);

 }

}

/* USART2 init function */

static void MX_USART2_UART_Init(void)

{

 huart2.Instance = USART2;

 huart2.Init.BaudRate = 9600;

 huart2.Init.WordLength = UART_WORDLENGTH_8B;

 huart2.Init.StopBits = UART_STOPBITS_1;

 huart2.Init.Parity = UART_PARITY_NONE;

 huart2.Init.Mode = UART_MODE_TX_RX;

 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

 huart2.Init.OverSampling = UART_OVERSAMPLING_16;

 huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

 huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

 if (HAL_UART_Init(&huart2) != HAL_OK)

 {

   _Error_Handler(__FILE__, __LINE__);

 }

}

/** Configure pins as

       * Analog

       * Input

       * Output

       * EVENT_OUT

       * EXTI

*/

static void MX_GPIO_Init(void)

{

 GPIO_InitTypeDef GPIO_InitStruct;

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOH_CLK_ENABLE();

 __HAL_RCC_GPIOA_CLK_ENABLE();

 /*Configure GPIO pin Output Level */

 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

 /*Configure GPIO pin : PA8 */

 GPIO_InitStruct.Pin = GPIO_PIN_8;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**

 * @brief This function is executed in case of error occurrence.

 * @param file: The file name as string.

 * @param line: The line in file as a number.

 * @retval None

 */

void _Error_Handler(char *file, int line)

{

 /* USER CODE BEGIN Error_Handler_Debug */

 /* User can add his own implementation to report the HAL error return state */

 while(1)

 {

 }

 /* USER CODE END Error_Handler_Debug */

}

#ifdef USE_FULL_ASSERT

/**

 * @brief Reports the name of the source file and the source line number

 *        where the assert_param error has occurred.

 * @param file: pointer to the source file name

 * @param line: assert_param error line source number

 * @retval None

 */

void assert_failed(uint8_t* file, uint32_t line)

{

 /* USER CODE BEGIN 6 */

 /* User can add his own implementation to report the file name and line number,

    tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

 /* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT */

/**

 * @}

 */

/**

 * @}

 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

xmart
Associate II

What do you mean stupid ?! I think it's not a good word, I am describing exactly what is happening to me

LMI2
Lead

I ment my own question. What exactly gets stuck?

xmart
Associate II

At first I thought that the program was stuck because no data was received from uart, but I later found out that it was not stuck because the counter is increasing, there is something that makes uart not working, then I noticed when I canceled the delay, uart received

The interference of UART vs SYSTICK is not something I can explain

I'd recommend clearing the local/auto variables

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

Check what interrupt handlers you have.

Have the loop transmit a character out of the UART so you can observe the looping independently of the debugger

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