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.

TMS320F280039C: How to unalign struct bytes

Part Number: TMS320F280039C

Tool/software:

typedef struct
{
    float  a;
    float  b;
    int    c;
    float  d;
    float  e;
    int    f;
    int    g;
    Uint16 h;
    float  i;
    float  j;
    int    k;
    int    l;
    float  m;
    float  n;
    float  o;
    int    p;
    int    q;
    }Test_dat_strut1;
This is the struct I used to test, I observed in the simulation that the variable c takes up 32 bits, I tried to add the __attribute__((packed)) and #pragma pack(1) commands to the struct definition, but none worked, since this struct is going to be used in communication, how do I unalign the struct

  • Unfortunately, the C2000 compiler does not support any method for packing members of a structure.  

    since this struct is going to be used in communication, how do I unalign the struct

    Manually force a member of a struct to the desired offset from the beginning by adding in extra unused members of the size needed.  To be sure you did it right, you can use a technique similar to this one to have the compiler tell you the offset, in bits, of a given member.  Write code similar to ...

    #include <stddef.h>   /* for offsetof */
    #include <limits.h>   /* for CHAR_BIT */
    
    /* define Test_dat_strut1 here */
    
    int test_c()
    {
        return offsetof(Test_dat_strut1, c) * CHAR_BIT;
    }

    Build that code with the option --src_interlist.  This tells the compiler to keep the auto-generated assembly file, and add comments to it that make it easier to understand.  The assembly file has the same name as the source file, with the extension changed to .asm.  Inspect the file to see lines similar to ...

    ;----------------------------------------------------------------------
    ;  29 | return offsetof(Test_dat_strut1, c) * CHAR_BIT;                        
    ;----------------------------------------------------------------------
            MOVB      AL,#64                ; [CPU_ALU] |29|
      

    This tells you the offset of the member c is 64 bits from the beginning of the struct.  The macros offsetof and CHAR_BIT are from the C standards.  So you can use the technique with other compilers, to insure you get the same offset.

    Thanks and regards,

    -George