cancel
Showing results for 
Search instead for 
Did you mean: 

Custom context in callback handlers

Alexander4
Visitor

Hello,

I would like to request a feature to be added to the STM32 HAL.

Would it be possible to be able to add a custom user context when registering peripheral callbacks.

Example:

 

HAL_StatusTypeDef HAL_SPI_RegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID, pSPI_CallbackTypeDef pCallback, void* UserContext);

 

And then as callback function:

 

typedef void (*pSPI_CallbackTypeDef)(SPI_HandleTypeDef *hspi, void* UserContext);

 

I really miss this feature and have had to code around it for god knows how many times.

I have added it myself to the SPI driver, but if I regenerate its all gone ofc.

So it would be real nice if this could be added to drivers.

 

Thx,

Alexander

3 REPLIES 3
Pavel A.
Evangelist III

I have added it myself to the SPI driver, but if I regenerate its all gone 

To avoid losing your changes in the HAL libraries:

- When you use Cube to (re)generate code, select the option to create links to the libraries instead of copying all the files into your project. 

- Check your changes in the ST libraries into your version control (yes, make your private branch).

To minimize code differences, do not pass the UserContext as a new argument. Instead, add it to the SPI_HandleTypeDef struct. Then your callbacks will get this arg from the hspi pointer: hspi->UserContext.

 

 


@Pavel A. wrote:

To minimize code differences, do not pass the UserContext as a new argument. Instead, add it to the SPI_HandleTypeDef struct.


@Alexander4 there was some discussion of this in the 2021 thread:

https://community.st.com/t5/stm32-mcus-embedded-software/is-there-a-way-to-provide-context-pointer-to-hal-callback/m-p/182064