cancel
Showing results for 
Search instead for 
Did you mean: 

How to use HAL_*_RegisterCallback to register an object method (C++)

SBros.1
Associate II

Hi everyone,

I have written class that handles U(S)ART communication on top of HAL. In my application I have several objects of that class, e.g. one for USART2, another for LPUART1,...

Currently, I have my own way of registering callbacks: I have static method that stores, which UART handle (UART_HandleTypeDef) belongs to which of my object. The callback function then looks up what object is responsible for the handle passed to it. E.g.:

 

 

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
  UartComm* uc = UartComm::get_object_by_UART_Handle(huart);
  if (uc == nullptr) {
    return;
  }
  uc->RxEventCallback_(Size);
}

 

 

 

If understand correctly, the HAL provides HAL_*_RegisterCallback, which would allow a similar registration directly. Having "ordinary" functions this should be straight forward but in my case I would need binding to an object method.

Is there a clean / preferred way of doing so? Or better stick with the current solution :\
I am using Keil MDK-ARM Plus Version: 5.39, C++11 / C11


Thanks for any help, suggestions,...

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

You can't do this. The function prototype must match what it expects, and object functions carry around the "self" parameter which none of the HAL callbacks do.

Instead, set up a static function as the callback which redirects to your member function, which is basically what you're doing here.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

2 REPLIES 2
TDK
Guru

You can't do this. The function prototype must match what it expects, and object functions carry around the "self" parameter which none of the HAL callbacks do.

Instead, set up a static function as the callback which redirects to your member function, which is basically what you're doing here.

If you feel a post has answered your question, please click "Accept as Solution".
SBros.1
Associate II

Hi TDK
Thanks for the fast response. I was hoping that there's some trick or a nicer way e.g. using lambda or std::bind() - but I'm not too familiar with those concepts and I don't known if this is fully supported by my compiler (that I could probably check).