This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

how to force the bit order in a struct

Hi,

I saw several structs that you have in your library and I am curious as how you are forcing the alignment, since I want to do the same.

This is one of them:

    typedef union
{
  struct
  {
    uint32_t _reserved0:16;              /*!< bit:  0..15  Reserved */
    uint32_t GE:4;                       /*!< bit: 16..19  Greater than or Equal flags */
    uint32_t _reserved1:7;               /*!< bit: 20..26  Reserved */
    uint32_t Q:1;                        /*!< bit:     27  Saturation condition flag */
    uint32_t V:1;                        /*!< bit:     28  Overflow condition code flag */
    uint32_t C:1;                        /*!< bit:     29  Carry condition code flag */
    uint32_t Z:1;                        /*!< bit:     30  Zero condition code flag */
    uint32_t N:1;                        /*!< bit:     31  Negative condition code flag */
  } b;                                   /*!< Structure used for bit  access */
  uint32_t w;                            /*!< Type      used for word access */
} APSR_Type;

I am trying to implement this but I cannot force the aligment as I want. Do I need some kind of pragma? In your header in case core_cm4.h does not seem to be needed.

This is my struct:

typedef union {
	struct {
		uint16_t			pk_type	:2;	// 00
		uint16_t			prod_num:3;	// 100
		uint16_t			brd_num	:3;	// 011
		uint16_t			sw_maj	:3;	// 000
		uint16_t			sw_min  :5; // 00001
	}				b;
	
	uint16_t 		sh;
	} ps_btn_dev_info_t;
Parents
  • not sure what the compiler is doing with half words. It should have worked. Can you try uint32_t and see if you get desired output?

    typedef union {
        struct {
            uint32_t            pk_type :2; // 00
            uint32_t            prod_num:3; // 100
            uint32_t            brd_num :3; // 011
            uint32_t            sw_maj  :3; // 000
            uint32_t            sw_min  :5; // 00001
            uint32_t            sh  :16 ;
        }               b;
        } ps_btn_dev_info_t;
    

    notice that i moved sh into the union.

  • sh (short) is for accessing the whole half world, hence the union with the bits. I tried uint32_t and did not worked either. The compiler shuffled the fields all around. This is the order that it came out after looking at the memory, as if it placed the number of bits in descending order:

    typedef union {
    	struct {
    		uint32_t			sw_min  :5; //1 00001
    		uint32_t			sw_maj	:3;	//0 000
    		uint32_t			brd_num	:3;	//3 011
    		uint32_t			prod_num:3;	//4 100
    		uint32_t			pk_type	:2;	//0 00
    	}				b;
    	
    	uint32_t 		w;			// 08 70 = 0000 1000 0111 0000
    	} ps_btn_dev_info_t;
    

    I am using Keil 5.2, SD 132 and SDK 11.0

Reply
  • sh (short) is for accessing the whole half world, hence the union with the bits. I tried uint32_t and did not worked either. The compiler shuffled the fields all around. This is the order that it came out after looking at the memory, as if it placed the number of bits in descending order:

    typedef union {
    	struct {
    		uint32_t			sw_min  :5; //1 00001
    		uint32_t			sw_maj	:3;	//0 000
    		uint32_t			brd_num	:3;	//3 011
    		uint32_t			prod_num:3;	//4 100
    		uint32_t			pk_type	:2;	//0 00
    	}				b;
    	
    	uint32_t 		w;			// 08 70 = 0000 1000 0111 0000
    	} ps_btn_dev_info_t;
    

    I am using Keil 5.2, SD 132 and SDK 11.0

Children
No Data
Related