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.

"PACKED" compiler directive - c2000

Is there a non-manual workaround for "PACKED" in c2000 compiler? To rephrase the question - I do not want to manually PACK/UNPACK every struct. Is there an alternative compiler directive to do this in c2000? 

  • Hello,

    I'll move your post to the Compilers Forum for better support.

    Elizabeth
  • Raghu Rajappa said:
    Is there a non-manual workaround for "PACKED" in c2000 compiler?

    Unfortunately, no.

    Thanks and regards,

    -George

  • Thank you George.

    Ok, Manually I plan to use bitfields. Example code shown below.

    typedef struct CC_PACKED
    {
       INT16U PSA;
       INT16U Length;
    
    #if defined(EC_LITTLE_ENDIAN)
       INT8U Mode:2;
       INT8U Direction:2;
       INT8U IntECAT:1;
       INT8U IntPDI:1;
       INT8U WTE:1;
       INT8U R1:1;
    
       INT8U IntW:1;
       INT8U IntR:1;
       INT8U R2:1;
       INT8U MBXstat:1;
       INT8U BUFstat:2;
       INT8U R3:2;
    
       INT8U ECsm:1;
       INT8U ECrep:1;
       INT8U ECr4:4;
       INT8U EClatchEC:1;
       INT8U EClatchPDI:1;
    
       INT8U PDIsm:1;
       INT8U PDIrep:1;
       INT8U PDIr5:6;
    #endif
    
    } _ESCsm;

    The problem occurs when I have to use arrays in the structure (bitfield on array is not possible is my understanding - please correct me if I am wrong) 

    typedef struct CC_PACKED
    {
       _MBXh header;
       INT8U b[MBXDSIZE];
    } _MBX;

    Since my controller is 16 bit, and the array (b) is 8 bit array of size MBXDSIZE, it will look like this inside the memory

    ADDRESS X ->        b[0], padded byte
    ADDRESS X + 1 -> b[1], padded byte
    ADDRESS X + 2 -> b[2], padded byte
    ....

    I am not so sure how to make a workaround for this. Any help would be appreciated. The intention is to keep the code as simple as possible while getting rid of padding when PACKED is not supported... Also please correct my understanding if needed.

  • It's not possible on C28x to have an array of 8-bit objects because each array element must be at least one addressable unit (16 bits) in size, even when using packed.

    Perhaps you could make use of the __byte instrinsic:
    __byte(b, 0) /* 1st 8 bits of array b */
    __byte(b, 1) /* 2nd 8 bits of array b */
    __byte(b, 2) /* 3rd 8 bits of array b */
  • Please try the techniques from the wiki article Byte Accesses with the C28x CPU .  I'm sorry I didn't think to refer you to this article sooner.

    Thanks and regards,

    -George