cancel
Showing results for 
Search instead for 
Did you mean: 

uart rx interrupt advice

Peter Curtis
Associate II

Hi All,

I'm after a little bit of advice on how to use the uart interrupts. I would like to implement an interrupt driven uart receiver which takes a single byte at a time and stuffs it into a buffer. When a terminating char is received, a flag is raised and the data can be read.

I have the uart (lpuart) working in polling mode (both receive and transmit) using the HAL commands.

I have implemented this kind of system successfully on other controllers, but am struggling a bit with this one (stm32l476).

In the past, I would create an interrupt handler, clear the rx receive flag, enable the rx interrupt then enable the global interrupt. As a char is received, I would stuff it into a buffer, clear the receive flag and wait for the next char and so on.

I have let STMCubeMX create the interrupt handler template for me and set up the NVIC. However, I do not seem to be getting an interrupt. The NVIC_init has a HAL_NVIC_EnableIRQ(LPUART1_IRQn) in it - do I need to do anything else to enable the interrupts?

I have put together a very simple bit of code in main to flag up if a uart interrupt has been received:-

  while (1)
  {  
 
	if(LPUART1_RX_DATAREADY == 1)
       {
          HAL_UART_Transmit(&hlpuart1, (uint8_t*)"\r\nGotSomething", 14, 1000); 
          LPUART1_RX_DATAREADY = 0;
       }
		
  /* USER CODE END WHILE */ 
 
  /* USER CODE BEGIN 3 */
 
  }
  /* USER CODE END 3 */
 
}

LPUART interrupt handler:-

void LPUART1_IRQHandler(void)
{
  /* USER CODE BEGIN LPUART1_IRQn 0 */
 
  if(__HAL_UART_GET_IT(&hlpuart1, UART_IT_RXNE) != RESET)
   {
	LPUART1_RX_DATAREADY = 1;
	LPUART1_RX_BUFFER[LPUART1_RX_P] = (uint8_t)(hlpuart1.Instance->RDR & 0x00FF);  // read receive reg to clear interrupt.
   }	
		
  /* USER CODE END LPUART1_IRQn 0 */
  HAL_UART_IRQHandler(&hlpuart1);
  /* USER CODE BEGIN LPUART1_IRQn 1 */
 
  /* USER CODE END LPUART1_IRQn 1 */
}

What other steps should I be taking? I have found a few examples on google with respect to the HAL_receiveIT library command, but this would be useless in this case as I do not know the length of the receive data in advance + the time out will be dependant on how quick someone can type.

Any constructive advice/pointers would be gratefully received.

Thanks

Peter

1 REPLY 1
Pavel A.
Evangelist III

Use the LL API for this, not HAL. And it will be as simple as you expect.

Hope this advice is constructive.

-- pa