Skip to main content
Visitor
September 16, 2026
Question

Use of C language bitfields for device register I/O in STM32CubeC5

  • September 16, 2026
  • 4 replies
  • 19 views

I noticed that stm32c5xx_hal_fdcan.c uses C language bitfields to access device registers. Since bitfields are compiler specific, every knowledgeable embedded software engineer knows that using bitfields to access device registers is non-standard and not to be used. I suggest that using bitfields to access device registers be changed. You can refer to STM32CubeH7 for correctly accessing device registers.

 

BTW. I tried to open a support issue in the on-line support, but received the following error:

4 replies

TDK
September 16, 2026

I noticed that stm32c5xx_hal_fdcan.c uses C language bitfields to access device registers.

Where? I don’t see it. Could be missing it. The file is 4800 lines, so some specifics would help here.

"If you feel a post has answered your question, please click ""Accept as Solution""."
mtcAuthor
Visitor
September 16, 2026

The following structures use bitfields: hal_fdcan_tx_header_thal_fdcan_rx_header_thal_fdcan_tx_evt_fifo_header_t

In routine FDCAN_CopyMessageToRAM(), lines 4419, 4420.

In routine HAL_FDCAN_GetReceivedMessage(), line 3194

In routine HAL_FDCAN_GetTxEvent(), line 3044

 

TDK
September 16, 2026

I tend to agree with you here, though it’s not an issue in practice. HAL is built for specific set of toolchains that all behave consistently.

It’s interesting that they moved from the structure in HAL to the HAL2/C5 structure. One of the common complaints of HAL was the size of the library. This was one way to cut down the memory footprint. Can’t have everything.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Pavel A.
September 17, 2026

> Since bitfields are compiler specific, every knowledgeable embedded software engineer knows that using bitfields to access device registers is non-standard and not to be used

True, but as the things change, this statement became less solid. All compilers that matter these days have options to ensure the desired allocation of bit fields.

A static assert or runtime check can be used to verify that a struct with bit fields is compiled correctly.

Of course if you have a legacy toolchain or a specific requirement, there’s a problem.