cancel
Showing results for 
Search instead for 
Did you mean: 

Registering a User callback for USART on STM32L4S5ZI

SJant.1
Associate III

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!!

1 ACCEPTED SOLUTION

Accepted Solutions
Amel NASRI
ST Employee

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 Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

4 REPLIES 4
Amel NASRI
ST Employee

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 Accept as Solution on the reply which solved your issue or answered your question.

Thanks Amel!

Hi Amel,

I found the Github article quite informative. I found an answer for the same.

I have a new question now, and if you can throw some light on it, it would be really helpful.

In my application, I am using DMA to transfer and receive data to a serial terminal which we are using as a debug-shell. The challenge I am facing is, in the UART_READ function, I am using HAL_UART_Receive_DMA function. the user provides the length of data to be read, but I have to internally read the Rx data byte by byte strictly. Hence even though I know the length of data to be read, I cannot use that length in the HAL_UART_Receive_DMA() function and compulsorily use sizeof(char). I am using a receive complete callback and everytime a char is read, the callback function is executed and in that I am not sure what should I actually put in. should I call the receive function again? Even if I do that and if the user enters a string for example "hello", then won't some of the characters be overwritten in RDR register before they are actually read?

I you have any idea on how this situation can be handled, please let me know.

Thanks!!

Tilen's article is great, but it's not an explanation of HAL library. Actually it's the exact opposite - it shows how to drop the unusable broken HAL bloatware and make a working USART implementation.