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.

Unaligned access in C6748 DSP

Other Parts Discussed in Thread: OMAP-L137

Hi TI experts

I am using CCS version 5.2, TI compiler 7.3.1 for C6748 DSP. Currently I want to make unaligned access but it is not working properly:

typedef struct _tSampleStruct
{
unsigned int numberA;
unsigned short numberB;
unsigned char numberC;
unsigned short numberD;
}
__attribute__ ((packed)) tSampleStruct;

But when I cast from a byte array to this struct the fields numberA -> numberD is not correct: (tSampleStruct*) byte_array 

It is already compiled with option -gcc, I find some instructions to enable option --unaligned_access in Runtime Model Options but in my case the option does not exist.

Please help me on this annoying issue.

Thank you very much,

Long

  • Please submit a test case which allows us to reproduce this issue.  Details on submitting a test case are at the end of the forum guidelines.

    Long Ngoc said:
    But when I cast from a byte array to this struct the fields numberA -> numberD is not correct: (tSampleStruct*) byte_array 

    I'm not sure what you mean.  What do you consider correct, and what are you seeing?  Please be very specific.

    Thanks and regards,

    -George

  • Hi George

    Usually in C, when we define a struct as packed we can cast it directly from a byte array, then we can access each byte segment in the byte array through accessing members of the struct. If we don't define a struct as packed, there might be some problems with alignment when doing so, and the value in struct's members will not reflect corresponding values in the byte array. So what I want to ask here is just I need confirmation about the ability of C6748 DSP to handle unalignment access and do I need to do any further configuration in CCS  to get it to work as I am dealing with a error but before we go any further to specific test case could you instruct me about this first?

    Thank you,

    Long

  • I had to deal with alignment problems on the C6747 and StarterWare USB. Here's some test code to check your structure.

    #include <stdio.h>

    #define offsetof(st, m) ((size_t) ( (char *)&((st *)0)->m - (char *)0 ))

    typedef struct _tSampleStruct
    {
      unsigned int numberA;
      unsigned short numberB;
      unsigned char numberC;
      unsigned char pad;
      unsigned short numberD;
    }
    tSampleStruct;

    void main( void )
    {
      printf("field    packed actual\n");
      printf("%s %2d %2d\n", "numberA", 0, offsetof(tSampleStruct,numberA));
      printf("%s %2d %2d\n", "numberB", 4, offsetof(tSampleStruct,numberB));
      printf("%s %2d %2d\n", "numberC", 6, offsetof(tSampleStruct,numberC));
      printf("%s %2d %2d\n", "numberD", 7, offsetof(tSampleStruct,numberD));
      printf("%s %2d %2d\n", "sizeof ", 9, sizeof(tSampleStruct));
    }
    /**************OMAP-L137, CCS3.3
    field    packed actual
    numberA  0  0
    numberB  4  4
    numberC  6  6
    numberD  7  8
    sizeof   9 12
    *****************/

    My ancient CCS3.3 does not support the packed attribute. I'd be interested to see if the packed attribute really does pack. The C6747 compiler, and probably the processor itself, appears to support even alignment. You just have to pad out char variables to the next even offset. By luck or design, the USB structures are even aligned. I have doubts the processor supports odd alignment access. If the compiler supports it, it is probably generating a lot of code to double read and combine bytes. Not fast or efificient.

  • Even if the "packed" attribute does what you think it should, there can still be an "endian" issue.  If that works okay, then as mentioned by someone else accessing the non-native data format can cost time and space.  And using an attribute or pragma is non-portable.  The portable and efficient C approach to matching externally imposed data formats to internal C variables is to create and use some "marshalling" and "unmarshalling" functions which do the conversion between byte arrays and the C data types, and perform all operations using the native C types..