Skip to main content
NK.13
Associate II
January 29, 2020
Question

is there any way to get interrupt on data ready in UART buffer in HAL library........?

  • January 29, 2020
  • 6 replies
  • 4537 views

I were trying to get data continues from a sensor and didnt find any flag that will acknowledge when a data is received . Only thing is there is HAL_UART_Receive_IT(),HAL_UART_Receive() functions.i need to seperate my data from Uart interupt handler.

This topic has been closed for replies.

6 replies

KnarfB
Super User
January 29, 2020

You can use HAL_UART_Receive_IT( &huart1, &ch, 1 ); to receive a single char ch. When using HAL, the interrupt is wrapped for you in a callback. You implement at global level:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance==USART1) {
		// process ch
		HAL_UART_Receive_IT( &huart1, &ch, 1 );	// receive next char
	}
}

There are more callbacks available, see HAL stm32f0xx_hal_uart.h

This works well if the throughput is low or moderate. For high throughput you might want to switch to DMA handling more than 1 char per interrupt. The most elaborated examples I know of are from Tilen Majerle https://stm32f4-discovery.net/2017/07/stm32-tutorial-efficiently-receive-uart-data-using-dma/

hth

KnarfB

NK.13
NK.13Author
Associate II
January 29, 2020

KnarfB,

according to my understating this HAL_UART_RxCpltCallback is interrupted when a reception complete.then how this will get its first interrupt......?

Ozone
Principal
January 29, 2020

No.

The callback is executed in the context of the Rx interrupt.

Only other interrupts with higher priority could interrupt.

Interrupts of the same priority, including other Uart RX interrupts, get "tail-chained", i.e. execute once the current one finished.

This does not save you from trouble when characters arrive faster then you process them.

KnarfB
Super User
January 29, 2020

Correct. For starting, you call HAL_UART_Receive_IT( &huart1, &ch, 1 ); once in main().

berendi
Principal
January 29, 2020

Unfortunately there is no such functionality in the HAL library.

The workaround presented above is a serious waste of the limited flash and ram space and processing power of the STM32F0, unnecessarily checking flags again and again, needlessly disabling and enabling interrupts after each received byte, etc.

Using the code examples in appendix A, setting up the UART is 2 lines of code, interrupt handling is another 2 lines (receive byte code example goes directly in UART1_IRQHandler()).

NK.13
NK.13Author
Associate II
January 29, 2020

Where i get this code examples appendix A....?

berendi
Principal
January 29, 2020

sorry, Appendix A of the Reference manual

NK.13
NK.13Author
Associate II
January 29, 2020

this is the handler you mentioned above,isnt.....?

i set this in my while(1) loop and do i need to define UART1_IRQHandler() seperatly....?

RMcCa
Senior II
January 29, 2020

It is easy to write your own ISRs that conpletely skip all the hal callback faff.

Here's what i do:​

1. Look in the interrupt vector ​table to get the name of the interrupt you want to use

2: declare a void [isr name] ( void ) function in your code. This is your isr.

3. Build the code and then delete/ comment out the old definition of the isr when the linker complains & rebuild.

This, of course, isn't the only approach, just what i did at first​