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.

Need to char pack structs without padding

Other Parts Discussed in Thread: TMS570LS20216

I am using the Stellaris LM3S9B90 and CCS4.2.  I want to create structs that may be odd byte sizes and use "sizeof ( )" to define the size exactly.  Right now it seems that the compiler pads to the struct to make it an even boundary.  I DON'T WANT THIS.  How do I remove it??  Can't find any good information on #pragmas in your documentation.  Other compilers allow use of #pragma pack(1) to force byte size packing with no padding.  See below for example of struct:

 

struct ACT_ASSIGN_NADDR_RQ {

  unsigned short    vendor_id;
  unsigned short    ssn;
  char                        e0_naddr;
};

#define ACT_ASSIGN_NADDR_RQ_SZ        (sizeof(struct ACT_ASSIGN_NADDR_RQ))

 

ACT_ASSIGN_NADDR_RQ_SZ  should be defined as 5, but is being defined as 6.

  • Starting with ARM compiler release 4.6.1, we do support the GCC packed attribute for packing structures, so it is supported in CCS 4.2. To enable it you need to use the -gcc compiler option. Please take a look at the README.txt file found in your ARM compiler toolset (should be in \ccsv4\tools\compiler\tms470 within your ccs install folder) and also at the ARM Compiler Users Guide, Section 5.16.3.

  • I have dozens of structure templates.  Many of them with several members.  I have to add the following ((aligned1))) to each member??

     

    struct foo {
        int x[2] __attribute__ ((aligned (1)));
    };

  • The "packed" attribute may be applied to a struct or union type or to a member of a struct or union type. So you can do:

    struct ACT_ASSIGN_NADDR_RQ {
      unsigned short    vendor_id;
      unsigned short    ssn;
      char                        e0_naddr;
    }  __attribute__((__packed__));

    There is no way to apply it globally to all structures so it has to be applied to each struct.

  • Ok.  So I guess I'm out of luck for the following.  Compiler complains because of the bit fields.  Is there a way to handle this?  We use this all over the place.

     

    struct TRIGGER_DESCRIPTOR {
      UBYTE confirm: 1;
      UBYTE triggerType: 2;
      UBYTE triggerOptions: 5;
    };

    struct ACT_CREATE_EVENT_RQ {
      SvcID attrID;
      struct TRIGGER_DESCRIPTOR trigDescr;
      UBYTE pad;
      UBYTE trigData[ACT_CREATE_EVENT_MAXRQ_DATA];
    };

    #define ACT_CREATE_EVENT_RQFIXED_SZ   (sizeof(SvcID)+sizeof(struct TRIGGER_DESCRIPTOR)+sizeof(UBYTE))
    #define ACT_CREATE_EVENT_MAXRQ_DATA   (sizeof(ULONG)*2)
    #define ACT_CREATE_EVENT_MINRQ_SZ     (ACT_CREATE_EVENT_RQFIXED_SZ)
    #define ACT_CREATE_EVENT_MAXRQ_SZ     (sizeof(struct ACT_CREATE_EVENT_RQ))

  • ARM Code Generation tools 4.9 supports packed bit-fields. Someone else may be able to comment on the expected production release date.  I believe it is due out soon (i.e. this month).

     

  • Information about upcoming compiler releases can be found here on the Wiki.  ARM compiler v4.9.0 is planned to release on March 30.

    Thanks and regards,

    -George

  • Hi Georgem,

    I am facing similar issue in my project regarding structure padding.I tried using the Variable attribute and Type attribute using packed keyword as below. Both results in the warning  saying "invalid attribute for X".  I have enabled the gcc extetion acces also (--gcc). Please let me know how to handle this warning.

    Processor : TMS570LS20216, Code Composer Studio : 4.2.0.10018,  TMS470 C/C++ CODE GENERATION TOOLSRelease Notes4.6.3

    const struct foo1, {
     char a;
      int x[2] __attribute__ ((packed));
    } f2 = { 'A', {1,2}}

    const struct foo
    {
     char a;
      int x[2] __attribute__ ((packed));
    } f1 __attribute__ ((__packed__)) = { 'A', {1,2}}

     

     

  • For which silicon version are you compiling (using --silicon_version or -mv)?

    Compiler 4.6.3 only supports the packed attribute for Cortex-M3 (-mv7m3).  I believe 4.7.x extends that to any target which supports hardware unaligned access.  4.9.x implements packed for all targets with or without hardware support for unaligned access.

    Aside: Despite what the GCC documentation says, I have never found that packed can be applied to a variable in the form of your second declaration.  It can be used in a variable declaration if it "attached" to the type, that is, if it precedes the variable identifier.

  • The option i am using is 7R4. The processor i am using Cortex R4. It supports the unaligned access by default.

    So you mean to say that my current version of compiler 4.6.3 does not support padding ?

    Below is the second declariion i used. From the GCC documentation i found that  __attribute__ ((__packed__)) is for the entire structure. When i use the below definition, i see the same error. I am not clear on your argument about this. Could you please elaborate.

    const struct foo
    {
     char a;
      int x[2];
    } f1 __attribute__ ((__packed__))= { 'A', {1,2}} ;

  • Bindu Tanguturi said:
    The option i am using is 7R4. The processor i am using Cortex R4. It supports the unaligned access by default.

    So you mean to say that my current version of compiler 4.6.3 does not support padding ?

    4.6.3 supports the packed attribute only for Cortex-M3 (-mv7M3), not 7R4.  You need to use any version 4.9.x compiler; 4.9.2 is the current version.

    Bindu Tanguturi said:
    Below is the second declariion i used. From the GCC documentation i found that  __attribute__ ((__packed__)) is for the entire structure. When i use the below definition, i see the same error. I am not clear on your argument about this. Could you please elaborate.

    const struct foo
    {
     char a;
      int x[2];
    } f1 __attribute__ ((__packed__))= { 'A', {1,2}} ;

    Yes it applies to the entire structure, but the identifier 'f1' has to follow, not precede, the attribute.  Both the 4.9.1 TI compiler and gcc 4.6.2 (Linux/PC) ignore the attribute when the declaration is written as shown.  You see the same error with 4.6.3 because packed is not supported for your target in that version of the compiler.

     

  • Thanks Paul. 

    Just want you to update on the second question. If identifier f1 follows the attribute, it was showing as error, so i tried precede, then i see warning. Anyway this will also not work for my version of the compiler.