Skip to main content
SJant.1
Associate III
March 5, 2021
Solved

Registering a User callback for USART on STM32L4S5ZI

  • March 5, 2021
  • 1 reply
  • 3482 views

Hi,

I have got confused in knowing how to actually register a User call back function for Transfer complete event in UART. I am using DMA.

To give an idea on what I am doing, below re the lines that I have added in my code.

void TxComplete_Callback(struct __UART_HandleTypeDef *huart); //declaration of user callback fun
 
huart1.TxCpltCallback = TxComplete_Callback; // assigning it to transfer complete event callback of the uart instance
 
void TxComplete_Callback(struct __UART_HandleTypeDef *huart) {
 
 } // function definition which is currently not full

My question is whether the argument for the callback function has to be compulsorily (struct __UART_HandleTypeDef *huart)? because in UART_DMATransmitCplt() function it knows that the event received is DMA Transfer Complete and so it checks if huart->TxCpltCallback is populated or else it executes the weak function as can be see below

#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
 /*Call registered Tx complete callback*/
 huart->TxCpltCallback(huart);
#else
 /*Call legacy weak Tx complete callback*/
 HAL_UART_TxCpltCallback(huart);
#endif /* USE_HAL_UART_REGISTER_CALLBACKS */

So the doubt I have is if this is how a callback function can be assigned, then what does the HAL_UART_RegisterCallback() function help me to do?

And will the changes that I have made work and whenever the DMA transfer complete event occurs, my custom function TxComplete_Callback will be called and executed? Can I similarly add a RxComplete_Callback function?

Please help me in these confusions. I have set USE_HAL_UART_REGISTER_CALLBACKS to 1 to enable the callbacks.

Thanks!!

This topic has been closed for replies.
Best answer by Amel NASRI

Hi @SJant.1​ ,

A complete explanation with examples is provided by @Tilen MAJERLE​  in this Github article.

-Amel

1 reply

Amel NASRI
Amel NASRIBest answer
ST Technical Moderator
March 9, 2021

Hi @SJant.1​ ,

A complete explanation with examples is provided by @Tilen MAJERLE​  in this Github article.

-Amel

To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.
SJant.1
SJant.1Author
Associate III
March 11, 2021

Thanks Amel!