stm32at-commandhal

How the callback functions work in stm32 Hal Library?


The Hal Lib provides some callback function to manage hardware interrupt, but I don't know how they work.

The fact is that I am using *HAL_UART_RxCpltCallback(UART_HandleTypeDef huart) this function so as to receive other devices' data and check those data. So I use the usart interrupt to receive them.

But I don't know when the callback function will be executed; does it depend on the receive buffer's length or the data buffer's?

I guess the hardware interrupt will be triggered while a character has been received, but the callback function will be executed after the receive buffer is full.

PS: I am using the stm32-nucleo-f410 development board to communicate with an AT commend device, and I am a novice about it.


Solution

  • The callback you are referring to is called when the amount of data specified in the receive functions (the third argument to HAL_UART_Receive_IT) is received on the UART. You are correct that the UART interrupt service routine (ISR) is called every time a character is received, but when using the HAL that happens internally to the library and doesn't need to be managed by you. Every time the ISR is called, the received character is moved into the array you provide via the second argument of HAL_UART_Receive_IT, and when the number of characters specified by the call is reached, the callback will be called in that ISR (so make sure not to do anything that will take too much time to complete - ISRs should be short, and the ISRs in the HAL library are already pretty lengthy to handle every possible use case).

    Further, if you find that the callback is not being triggered even if you are sending enough data to the peripheral, make sure the interrupt is actually enabled - the HAL_UART_Receive_IT function doesn't actually enable the interrupt, that has to be done during initialization of the peripheral.