Skip to main content
NSemrHomeInit
Associate III
March 9, 2023
Solved

Bit endianness is an issue in bitfields?

  • March 9, 2023
  • 5 replies
  • 4840 views

Dear ST hello,

I have a question about the bit field,

I am using the stm32F429 discovery board, and I am working on the gyroscope Mems.

if I declare a union like this:

typedef volatile union
{
	uint8_t All;
	struct
	{
		uint8_t Yen:1; /*X axis enable*/
		uint8_t Xen:1; /*Y axis enable*/
		uint8_t Zen:1; /*Z axis enable*/
		uint8_t PD:1; /*Power-down mode enable*/
		uint8_t BW0:1;
		uint8_t BW1:1; /*Bandwidth selection*/
		uint8_t DR0:1;
		uint8_t DR1:1; /*Output data rate selection*/
	} RegBits;
 
} Tst_CTRL_REG1;

How does the compile organize the bits in memory?

Does the MSB is Yen or DR1?

Thank you in advance,

This topic has been closed for replies.
Best answer by NSemrHomeInit

this project could be run on many boards and in the context we send the handler and the read and write functions to write and read to the specific bus depending on the board chosen.

5 replies

waclawek.jan
Super User
March 9, 2023

Yen is LSB, DR1 is MSB.

JW

NSemrHomeInit
Associate III
March 9, 2023

Thank you for your answer,

I think these kind of information could be found in the compiler user Manuel.

do i have to look for is in stm or arm cortex m4 website?

Tesla DeLorean
Guru
March 9, 2023

GCC​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
NSemrHomeInit
Associate III
March 9, 2023

Realy, are we uisng Gcc in cubeIDE?

I think you want to say arm ...GCC. Here is the compiled called when I compile the project arm-none-eabi-gcc.

waclawek.jan
Super User
March 9, 2023

>>I think these kind of information could be found in the compiler user Manuel.

>GCC

It's more complicated than one would think.

https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html :

The order of allocation of bit-fields within a unit (C90 6.5.2.1, C99 and C11 6.7.2.1):

Determined by ABI.

Okay, so where's ABI for ARM (i.e. ARM's rules determining how to write compilers so that their objects are mutully linkable)?

https://github.com/ARM-software/abi-aa - the bitfield ordering is in "Procedure Call Standard" https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst, bitfields are namely in https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst#8171bit-fields-no-larger-than-their-container

ARM Cortex-M are fixed little endian, so

For little-endian data types

K(F)

is the offset from the least significant bit of the container to the least significant bit of the bit-field.

JW

S.Ma
Principal
March 9, 2023

Did you browse the STMems sensor drivers on github ?

Here is an extract:

typedef struct
{
#if DRV_BYTE_ORDER == DRV_LITTLE_ENDIAN
 uint8_t bit0 : 1;
 uint8_t bit1 : 1;
 uint8_t bit2 : 1;
 uint8_t bit3 : 1;
 uint8_t bit4 : 1;
 uint8_t bit5 : 1;
 uint8_t bit6 : 1;
 uint8_t bit7 : 1;
#elif DRV_BYTE_ORDER == DRV_BIG_ENDIAN
 uint8_t bit7 : 1;
 uint8_t bit6 : 1;
 uint8_t bit5 : 1;
 uint8_t bit4 : 1;
 uint8_t bit3 : 1;
 uint8_t bit2 : 1;
 uint8_t bit1 : 1;
 uint8_t bit0 : 1;
#endif /* DRV_BYTE_ORDER */
} bitwise_t;

NSemrHomeInit
Associate III
March 9, 2023

No, I am creating my one driver.

Pavel A.
Super User
March 9, 2023

IIRC the gcc compiler has an option to select endianness of bit fields.

Generally bit fields in shared code (libraries) are deprecated, exactly because of portability among various compilers. Otherwise, be ready to write the bitield structs twice and use custom defines, as in the example above.

From the ABI docum cited by Jan:

"The AAPCS does not allow exported interfaces to contain packed structures or bit-fields."