cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to receive all charcaters through uart interrupt

umesh_patil
Associate II

I am not able to receive whole characters from the uart interrupt . if I enter "hello" thorigh command line , I gets h , e , o . I miss other characters. below is my code snippet.

uint8_t str;

//Main code

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();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_USART2_UART_Init();

 /* USER CODE BEGIN 2 */

__HAL_UART_ENABLE_IT(&huart2 , UART_IT_RXNE);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

//    HAL_UART_Transmit(&huart2 , (uint8_t *) buff , 8 ,1000);

//    HAL_UART_Receive_IT(&huart2 , (uint8_t *) str , 10);

//    HAL_Delay(100);

   /* USER CODE END WHILE */

   /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

//Interrupt function

void USART2_IRQHandler(void)

{

 /* USER CODE BEGIN USART2_IRQn 0 */

 /* USER CODE END USART2_IRQn 0 */

 HAL_UART_IRQHandler(&huart2);

 /* USER CODE BEGIN USART2_IRQn 1 */

   HAL_UART_Receive_IT(&huart2 ,(uint8_t*) &str , 1);

 /* USER CODE END USART2_IRQn 1 */

}

//Callback function

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){

   print("Recvd char : %c \r\n" , str);

}

1 ACCEPTED SOLUTION

Accepted Solutions
Ozone
Lead

> void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

> {

>   print("Recvd char : %c \r\n" , str);

I sugest to avoid calling blocking functions in interrupt context.

View solution in original post

4 REPLIES 4
Ozone
Lead

> void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

> {

>   print("Recvd char : %c \r\n" , str);

I sugest to avoid calling blocking functions in interrupt context.

berendi
Principal

The HAL UART interface doesn't anticipate this use case, when received characters are to be processed immediately in some way. Write your interrupt handler, and don't use the HAL_UART_Receive_IT() function.

To start receiving, enable the UART interrupt in NVIC, and set the UE, RE, and RXNEIE bits in CR1.

In the interrupt handler, check the SR register, and if the RXNE bit is set, read the data byte from RDR (called DR on older models).

Using printf() in an interrupt handler can go very wrong, ensure that whatever you do with the received data, it'll be finished before the next one arrives (in less than 86 microseconds at 115200 baud UART speed), and all functions are safe to be called from an interrupt handler (you can assume it's the same thing as a signal handler).

Thank u...it worked

its working ...thank u for the guidance