This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430 CGT 4.1.x GCC "packed" variable attributes for enum

Team,

i am trying to make an example code for packed enumeration data type using MSP430 CGT v4.1.x. However it seems the compiler doesn't like it. Either i try to do this:

enum __attribute__((packed)) my_enum_1
{
  ENUM_1_ZERO, 
  ENUM_1_ONE,
  ENUM_1_TWO,
  ENUM_1_THREE,
};

or this:

typedef enum __attribute__((packed))
{
  ENUM_1_ZERO,
  ENUM_1_ONE,
  ENUM_1_TWO,
  ENUM_1_THREE,
} my_enum_1;

the compiler will give error "#111 expected either a definition or a tag name".

However it will compile without error if i do this:

enum my_enum_1
{
  ENUM_1_ZERO, 
  ENUM_1_ONE,
  ENUM_1_TWO,
  ENUM_1_THREE,
} __attribute__((packed));

or

typedef enum 
{
  ENUM_1_ZERO, 
  ENUM_1_ONE,
  ENUM_1_TWO,
  ENUM_1_THREE,
} __attribute__((packed)) my_enum_1;

but the map file still shows that my_enum_1 occupies two bytes of data instead of one byte which i would expect.

Any idea what i am doing wrong here? Thanks for the help.

  • Leo,

    I have not been able to reproduce this. Could you attach a complete test case that can be compiled and linked so the size of the enums can be verified? Also please be sure to include your build options.

    Have you also tried out the --small_enum compiler option? It is documented in the MSP430 Compiler Users Guide.

  • Hi Aarti,

    thanks for the reply. I checked the --small_enum, and it works. However using the __attributes__((packed)) type variable still give me error altough the GCC language extension is already activated.

    Please find the complete CCS project which i use below:

    2021.TEST_UNION_PACK.zip

  • Leo,

    If the enums are defined as below, there are no compile errors and you can then verify from the .map file that my_data_1a and my_data_1b are packed while my_data_2a and my_data_2b are not since they do not have the packed attribute. I was able to modify your source file as below and verify this.

    enum my_enum_1a
    {
      ENUM_1A_ZERO,
      ENUM_1A_ONE,
      ENUM_1A_TWO,
      ENUM_1A_THREE,
    } __attribute__((packed)) ;

    typedef enum
    {
      ENUM_1B_ZERO,
      ENUM_1B_ONE,
      ENUM_1B_TWO,
      ENUM_1B_THREE,
    } __attribute__((packed)) my_enum_1b;

    I have attached the modified main.c file. Please give it a try and let us know if it works.

    #include <msp430.h>
    
    // packed enums - using __attribute__((packed)) will cause
    // compile error - #111 expected either a definition or a tag name
    #if 0
    enum __attribute__((packed)) my_enum_1a
    {
      ENUM_1A_ZERO,
      ENUM_1A_ONE,
      ENUM_1A_TWO,
      ENUM_1A_THREE,
    };
    
    typedef enum  __attribute__((packed))
    {
      ENUM_1B_ZERO,
      ENUM_1B_ONE,
      ENUM_1B_TWO,
      ENUM_1B_THREE,
    } my_enum_1b;
    #endif
    
    enum my_enum_1a
    {
      ENUM_1A_ZERO,
      ENUM_1A_ONE,
      ENUM_1A_TWO,
      ENUM_1A_THREE,
    } __attribute__((packed)) ;
    
    typedef enum
    {
      ENUM_1B_ZERO,
      ENUM_1B_ONE,
      ENUM_1B_TWO,
      ENUM_1B_THREE,
    } __attribute__((packed)) my_enum_1b;
    
    // normal enums
    enum my_enum_2a
    {
      ENUM_2A_ZERO,
      ENUM_2A_ONE,
      ENUM_2A_TWO,
      ENUM_2A_THREE,
    };
    
    typedef enum
    {
      ENUM_2B_ZERO,
      ENUM_2B_ONE,
      ENUM_2B_TWO,
      ENUM_2B_THREE,
    } my_enum_2b;
    
    volatile enum my_enum_1a my_data_1a = ENUM_1A_ZERO;
    volatile my_enum_1b my_data_1b = ENUM_1B_ONE;
    volatile enum my_enum_2a my_data_2a = ENUM_2A_ZERO;
    volatile my_enum_2b my_data_2b = ENUM_2B_ONE;
    volatile unsigned int int_delay;
    volatile unsigned char char_delay;
    
    #define BITFIELDS  (((unsigned char) my_data_1a) + \
                        ((unsigned char) my_data_1b) + \
                        ((unsigned char) my_data_2a) + \
                        ((unsigned char) my_data_2b))
    
    void main(void)
    {
      // stop WDT
      WDTCTL = WDTPW + WDTHOLD;
    
      // initialize hardware
      P1OUT = BITFIELDS;
    
      while(1)
      {
    	P1OUT ^= BITFIELDS;
    	for(int_delay=50000; int_delay; int_delay--);
    	for(char_delay=200; char_delay; char_delay--);
      }
    }
    

  • Hi Aarti,

    it works! thanks.