cancel
Showing results for 
Search instead for 
Did you mean: 

Question about pUserData field on HAL

dimercur
Associate II

I am developing in C++ on a STM32U5, with HAL and was trying to figure how to embed interrupt handling in a class.

So I've questioned ChatGPT and DeepSeek for a solution and both gave an answer using pUserData field for storing object reference.
pUserData is a field provided by any handler (like UART_HandleTypeDef) and ... I do not have such field with my version of HAL that CubeIDE used for building my project.

I've pinpointed that problem to DeepSeek that gave me this answer:

Okay, the user mentioned that the HAL handlers don't have a pUserData field. Hmm, I need to recall which STM32 HAL versions include that. Oh right, older versions might not have it, especially before HAL introduced the MSP layer with user data pointers.

So, is there a version of HAL with that field ? My version of HAL seems not so old (and U5 family is rather new), so how much recent should be HAL version to have this field ? Or both AI are totally dumb ?

1 ACCEPTED SOLUTION

Accepted Solutions

Good question, I've seen in used in the STM32_USB_Device_Library (L4 v1.14.0) as a means of passing a related user/extension to the primary handle.

In other situations I've seen nested-doll type structures passed as UART_HandleTypeDef, where user data you want to carry around with the pointer thru call-in and call-back gets appended to the end of ST's structure.

You could also modify the UART_HandleTypeDef structure to carry your own information/extension

 

@STTwo-32 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

10 REPLIES 10

Good question, I've seen in used in the STM32_USB_Device_Library (L4 v1.14.0) as a means of passing a related user/extension to the primary handle.

In other situations I've seen nested-doll type structures passed as UART_HandleTypeDef, where user data you want to carry around with the pointer thru call-in and call-back gets appended to the end of ST's structure.

You could also modify the UART_HandleTypeDef structure to carry your own information/extension

 

@STTwo-32 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TDK
Guru

Nothing like that exists in HAL. The pUserData field exists in the USB library, but despite the name it isn't a field the user can modify arbitrarily.

Code search results

 

ChatGPT (and analogs) is set up to deliver convincing answers. It is not designed to deliver correct answers. With niche subjects like STM32, it is only good for very generic questions and even then still needs verified manually.

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

(...) You could also modify the UART_HandleTypeDef structure to carry your own information/extension (...)

Yes, this solution was also provided by both AI, but I would prefer not to modify anything in HAL files.


@dimercur wrote:

pUserData is a field provided by any handler (like UART_HandleTypeDef) ?


You (or your AI) seem to be confusing a handler and a handle.

  • handler  is a function which handles (processes) an event;
  • handle is a data structure.

 

PS:

UART_HandleTypeDef defines a handle (a data structure) - not a handler.

Saket_Om
ST Employee

Hello @dimercur 

Are you referring to a handle or a handler?

If you mean a handle, could you please specify which handle you are referring to? For example, I2C_HandleTypeDef.

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

Ok, sorry, my bad.

I am dealing with handle, like one created out of UART_HandleTypeDef. So, is there any version of HAL where such data structure defines a pUserData field ? 

Thank you


@dimercur wrote:

(...) You could also modify the UART_HandleTypeDef structure to carry your own information/extension (...)

Yes, this solution was also provided by both AI, but I would prefer not to modify anything in HAL files.


The handles are just global data - so you could provide your own, separate, global* structure for it ...

 

* Or whatever scope works for you.

Hello @dimercur 

Why you don't use the pTxBuffPtr and pRxBuffPtr?

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

@Saket_Om wrote:

Why you don't use the pTxBuffPtr and pRxBuffPtr?


OP wants somewhere to store an Object Reference.

@dimercur you could always create your own "handle" structure - and have it include the HAL handle...