Skip to main content
Associate
September 3, 2026
Solved

HAL2 User Data pointers tp *non* const

  • September 3, 2026
  • 1 reply
  • 58 views

This is a feature request.

In HAL2, we can configure an STM32CubeMX2 project to use User Data pointers (pointers managed only by the application, not the HAL2 library). E.g. setting USE_HAL_UART_USER_DATA to 1 will add the field `const void *p_user_data;` to hal_uart_handle_t.

This pointer can be added to other handles: timers, SPI, I2C, FLASH, …

I would the pointer to be a pointer-to-mutable, or in other words to remove the “const” from p_user_data.

 

If this is not possible/wanted by ST, please let me know why it was designed like this. I am personally using this pointer to keep track of the amount of successful read/write and errors for UART communication, so it points to a custom-defined structure that is non-const.

Best answer by Khaled_DHIF

 Hello @TheBigBadBoy

Thanks for the feature request.

p_user_data is currently declared as const void * in HAL2. That means the HAL can store and forward an application-owned pointer, while signaling that it does not modify the pointed-to data through the HAL API.

If your application uses this field for mutable state such as counters, error tracking, or per-instance context, you can still store a pointer to a mutable object in it and cast it back to the corresponding type in your application code, provided the underlying object is actually mutable and remains valid for the required lifetime.

For example: 

​
typedef struct {
  uint32_t errors;
} UartCtx_t;

static UartCtx_t uart_ctx = {0};

huart1.p_user_data = &uart_ctx;

UartCtx_t *ctx = (UartCtx_t *)huart1.p_user_data;

ctx->errors++; // Value of "errors" element is mutable

​

 

Kin regards,

DHIF Khaled

1 reply

Khaled_DHIFBest answer
ST Employee
September 16, 2026

 Hello @TheBigBadBoy

Thanks for the feature request.

p_user_data is currently declared as const void * in HAL2. That means the HAL can store and forward an application-owned pointer, while signaling that it does not modify the pointed-to data through the HAL API.

If your application uses this field for mutable state such as counters, error tracking, or per-instance context, you can still store a pointer to a mutable object in it and cast it back to the corresponding type in your application code, provided the underlying object is actually mutable and remains valid for the required lifetime.

For example: 

​
typedef struct {
  uint32_t errors;
} UartCtx_t;

static UartCtx_t uart_ctx = {0};

huart1.p_user_data = &uart_ctx;

UartCtx_t *ctx = (UartCtx_t *)huart1.p_user_data;

ctx->errors++; // Value of "errors" element is mutable

​

 

Kin regards,

DHIF Khaled

"Please mark my answer as best by clicking on the “Accept as solution"" button if it fully answered your question. This will help other users find this solution faster.​"