Skip to main content
Associate
Jan 27, 2025 8:38 PM UTC
Solved

Question about pUserData field on HAL

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 ?

This topic has been closed for replies.
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
Jan 27, 2025 9:05 PM UTC

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..

10 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
Jan 27, 2025 9:05 PM UTC

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
dimercurAuthor
Associate
Jan 28, 2025 8:21 AM UTC

(...) 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.

Andrew Neil
Super User
Jan 28, 2025 10:19 AM UTC

@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.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
TDK
Jan 27, 2025 9:40 PM UTC

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""."
Andrew Neil
Super User
Jan 28, 2025 8:43 AM UTC

@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.

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

 

PS:

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

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
dimercurAuthor
Associate
Jan 28, 2025 10:17 AM UTC

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

ST Technical Moderator
Jan 28, 2025 10:46 AM UTC

Hello @dimercur 

Why you don't use the pTxBuffPtr and pRxBuffPtr?

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om
ST Technical Moderator
Jan 28, 2025 9:51 AM UTC

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.

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om