2024-08-16 06:50 AM
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,...
Solved! Go to Solution.
2024-08-16 09:44 AM
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.
2024-08-16 09:44 AM
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.
2024-08-16 09:52 AM
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).