This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Union + Struct - doubts

Good moorning,

I am trying to create a struct of date of this style:

//Struct 1

union{
    struct{
        union{
            struct{
                bool interrupt_receive;
                bool fifo_mode;
                bool enable;
                bool bits_14_mode;
                bool int_source_readed;
                bool void6;
								bool void7;
								bool void8;
            };
            unsigned char registro;
        }REG_CTRL_FLG;

        union{
            struct{
                unsigned int REG_CTRL_TIMEH;
                unsigned int REG_CTRL_TIMEL;
            };
            unsigned short REG_CTRL_TIME;
        };

        unsigned int threshold; //Para pruebas pendiente de eliminar
        
        //AXIS_STRUCT data_axis;   
        //AXIS_EXTEND_STRUCT data_full_axis;
    }registros_acelerometro;
    unsigned int registro[3];
}ACELEROMETRO;


//Struct 2

typedef union{
	
	struct{

		unsigned char dlc_0;

		union{
			struct{
				unsigned char p15:1;
				unsigned char p14:1;
				unsigned char p13:1;
				unsigned char p12:1;
				unsigned char p11:1;
				unsigned char p10:1;
				unsigned char p9:1;				
				unsigned char p8:1;

				unsigned char p7:1;
				unsigned char p6:1;
				unsigned char p5:1;
				unsigned char p4:1;
				unsigned char p3:1;
				unsigned char p2:1;
				unsigned char p1:1;
				unsigned char p0:1;
			};
			unsigned short p;
		}pulsadores;

		union{
			struct{
				unsigned char lb0:1;
				unsigned char lb1:1;
				unsigned char ls0:1;
				unsigned char ls1:1;
				unsigned char iluminacion:1;
				unsigned char completo:1;
				unsigned char sobrecarga:1;
				unsigned char void7:1;
			}registro_cabina;
		unsigned char estado_cabina;
		};

		unsigned char dlc_4;

	}registros_can_data;

	unsigned char can_data[5];
}TRESA_PULS;


The struct 1, i have been used with a STM32F7, ARM Compiler 6.11, and a Keil MDK 5.21, without any problem, but with the configuration that i use in the struct 2, it causes me some problems

The unions that contained int the structure, seem need a aditional memory space to work, which distorts the structure.

I've been thinking about it, but i dont find the error

Do you know if anything has changed?
Is there a pre-processing order that should be used?
Has it happened to any of you?
Do you see any trouble?

Thanks for your help and time.
Kinds Regards.

Parents
  • The alignment of the union pulsadores is set by the alignment requirement of its member 'p'. The compiler inserts a padding byte between dlc_0 and pulsadores. You can tell it not to using type attributes __attribute__((packed)) or __packed, depending on your compiler. There are many examples of use in the compiler books included with the Keil IDE. 

Reply
  • The alignment of the union pulsadores is set by the alignment requirement of its member 'p'. The compiler inserts a padding byte between dlc_0 and pulsadores. You can tell it not to using type attributes __attribute__((packed)) or __packed, depending on your compiler. There are many examples of use in the compiler books included with the Keil IDE. 

Children
No data