cancel
Showing results for 
Search instead for 
Did you mean: 

Hi, I am trying to communicate Uart in interrupt mode.We are able to successfully transmit & receive data to PC (through USB-UART).But when we connect to UART based modem then we are getting HAL_UART_ErrorCallback & transmission is failing. Pls help us.

PBira.1
Associate II

This error callback function is not getting called when we connect to PC. But we are getting during modem (UART based)communication only. Whats purpose of this callback ? Is there any configuration need to add ?

25 REPLIES 25

Don't you need to clear the error once you've handled it?

0693W000008xsqBQAQ.png 

/**
  ******************************************************************************
  * @file    UART/UART_TwoBoards_ComIT/Src/main.c 
  * @author  MCD Application Team
  * @brief   This sample code shows how to use STM32F4xx UART HAL API to transmit 
  *          and receive a data buffer with a communication process based on
  *          IT transfer. 
  *          The communication is done using 2 Boards.
  ******************************************************************************
  */
 
/** @addtogroup STM32F4xx_HAL_Examples
  * @{
  */
 
/** @addtogroup UART_TwoBoards_ComIT
  * @{
  */ 
 
/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
static void Error_Handler(void);
 
/* Private functions ---------------------------------------------------------*/
 
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
 
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure LED3, LED4, LED5 and LED6 */
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
  BSP_LED_Init(LED5);
  BSP_LED_Init(LED6);
 
  /* Configure the system clock to 100 MHz */
  SystemClock_Config();  
 
  /*##-1- Configure the UART peripheral ######################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* UART1 configured as follow:
      - Word Length = 8 Bits
      - Stop Bit = One Stop bit
      - Parity = None
      - BaudRate = 9600 baud
      - Hardware flow control disabled (RTS and CTS signals) */
  UartHandle.Instance          = USARTx;
  
  UartHandle.Init.BaudRate     = 9600;
  UartHandle.Init.WordLength   = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits     = UART_STOPBITS_1;
  UartHandle.Init.Parity       = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode         = UART_MODE_TX_RX;
  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
    
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }
 
  while (1)
  {
	  switch (NumStage)
	  {
	  case 1:
		  memset(txbuff,'\0',sizeof(txbuff));
	  	  memcpy(txbuff,GPRS_ECHO_OFF, 5);
	  	  if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)txbuff, 5)!= HAL_OK)
	  	  {
	  		  Error_Handler();
	  	  }
	  	  
	  	  /*##-3- Wait for the end of the transfer ###################################*/
	  	  while (UartReady != SET)
	  	  {
 
	  	  }
		 
            	  /* Reset transmission flag */
		  UartReady = RESET;
 
		  /*##-4- Put UART peripheral in reception process ###########################*/
		  if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, 6) != HAL_OK)
		  {
			  Error_Handler();
		  }
 
		  /*##-5- Wait for the end of the transfer ###################################*/
		  while (UartReady != SET)
		 {
 
       		 }
		 if (UartReady != SET)
			NumStage = 1;
		 else
			NumStage = 2;
		 /* Reset transmission flag */
		 UartReady = RESET;
 
		 break;
 
	  default:
         	  break;
	  }
	  
	  HAL_Delay(2000);
	  memset(aRxBuffer,'\0',sizeof(aRxBuffer));
  }
 
/**
  * @brief  System Clock Configuration
  *         The system Clock is configured as follow : 
  *            System Clock source            = PLL (HSI)
  *            SYSCLK(Hz)                     = 100000000
  *            HCLK(Hz)                       = 100000000
  *            AHB Prescaler                  = 1
  *            APB1 Prescaler                 = 2
  *            APB2 Prescaler                 = 1
  *            HSI Frequency(Hz)              = 16000000
  *            PLL_M                          = 16
  *            PLL_N                          = 400
  *            PLL_P                          = 4
  *            PLL_Q                          = 7
  *            VDD(V)                         = 3.3
  *            Main regulator output voltage  = Scale2 mode
  *            Flash Latency(WS)              = 3
  * @param  None
  * @retval None
  */
static void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;
 
  /* Enable Power Control clock */
  __HAL_RCC_PWR_CLK_ENABLE();
  
  /* The voltage scaling allows optimizing the power consumption when the device is 
     clocked below the maximum system frequency, to update the voltage scaling value 
     regarding system frequency refer to product datasheet.  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  
  /* Enable HSI Oscillator and activate PLL with HSI as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 0x10;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 16;
  RCC_OscInitStruct.PLL.PLLN = 400;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  
  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
     clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;  
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  
  if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/**
  * @brief  Tx Transfer completed callback
  * @param  UartHandle: UART handle. 
  * @note   This example shows a simple way to report end of IT Tx transfer, and 
  *         you can add your own implementation. 
  * @retval None
  */
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
  /* Set transmission flag: transfer complete*/
  UartReady = SET;
 
  /* Turn LED6 on: Transfer in transmission process is correct */
  BSP_LED_On(LED6);
}
 
/**
  * @brief  Rx Transfer completed callback
  * @param  UartHandle: UART handle
  * @note   This example shows a simple way to report end of IT Rx transfer, and 
  *         you can add your own implementation.
  * @retval None
  */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
  /* Set transmission flag: transfer complete*/
  UartReady = SET;
  //memset(aRxBuffer,'\0',sizeof(aRxBuffer));
  /* Turn LED4 on: Transfer in reception process is correct */
  BSP_LED_On(LED4);
}
 
/**
  * @brief  UART error callbacks
  * @param  UartHandle: UART handle
  * @note   This example shows a simple way to report transfer error, and you can
  *         add your own implementation.
  * @retval None
  */
 void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
{
  /* Turn LED3 on: Transfer error in reception/transmission process */
 // UartReady = SET;
  BSP_LED_On(LED3); 
}
 
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
static void Error_Handler(void)
{
	int loop = 0;
  /* Turn LED5 on */
  BSP_LED_On(LED5);
  while(loop++ < 80000)
  {
 
  }
}
 
#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 can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
 
/**
  * @}
  */
 
/**
  * @}
  */
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

In above code, we have wrote one of the sample uart transmit and receive flow. Please suggest how we can handle error callback.

Again: Don't you need to clear the error once you've handled it?

Once we are getting error then we start getting into each time. And we don’t get data into receive buffer.

And again: Don't you need to clear the error once you've handled it?

We are writing again that we couldn’t able to receive any data in the receiver buffer once we are getting error callback .

PBira.1
Associate II

After running initial command successfully then we start getting transmit error(callback) each time and failed to get receive data. Please help us to resolve this issue.

Clearing the flags is apparently done inside HAL_UART_IRQHandler(), but, as all the HAL is not interrupt safe, the behavior is not predictable. Drivers stuck in "busy" state seems to be very common issue for the HAL bloatware...

"getting transmit error"

Which error(s), exactly, are you getting ?

We are getting overrun error.