Byte alignment, structures, and data from peripherals

Technical questions regarding the XTC tools and programming with XMOS.
zonedar
Member
Posts: 14
Joined: Fri Nov 14, 2014 6:58 pm

Byte alignment, structures, and data from peripherals

Post by zonedar »

I am interfacing to an I2C device that has descriptors that I need to read. This data is byte aligned on one byte boundaries. Normally I use typedefed structures with a pragma instruction to pack on one byte alignment [ #pragma pack(1) ]. This way I let the complier deal with the data whether I cast or use memcpy.

However the XMOS is using SYSV byte alignment and this causes my data to be off.

For example here is a first part of a struct that I’ve declared.

Code: Select all

             
typedef struct _module_descriptor_
{
    uint8_t  macAddress0;
    uint8_t  macAddress1;
    uint8_t  macAddress2;
    uint8_t  macAddress3;
    uint8_t  macAddress4;
    uint8_t  macAddress5;
    uint32_t  vendorID;                      
    uint32_t  moduleID;       

[…]

}   
If I do the following :

Code: Select all

                         

My_module_descriptor_getter (&Descriptor);

unsigned l; unsigned char XXX[200];
memcpy(&XXX, & Descriptor, sizeof(Descriptor));

for(i=0; i< sizeof(Descriptor); i++);
{
    printf("0x%02X\n", XXX[i]);
}
l=0;
printf("\n0x%08X\n", Descriptor.vendorID);
printf("0x%08X\n", Descriptor.moduleID);
                         c_lcd <:  Descriptor; 
I get the following printed out (comments added for readability):

0x02 //<- macAddress0
0xEA //<- macAddress1
0x36 //<- macAddress2
0x00 //<- macAddress3
0xB7 //<- macAddress4
0xA6 //<- macAddress5
0x03 //<- Descriptor.vendorID should be 0x0000E003
0xE0
0x00
0x00
0x06 //<- Descriptor. moduleID should be 0x00000006
0x00
0x00
0x00
0x00
0x74
0x25
0x00
0x00
[…]


0x00060000 // output from printf("\n0x%08X\n", Descriptor.vendorID);
0x74000000 // output from printf("0x%08X\n", Descriptor.moduleID);

I’ve also had a similar problem with a header to a binary file (TGA graphic) that I read from an SDcard. Is there a trick that I can use to get the byte alignment correct when referencing the struct or do I need to roll-my-own functions for each structure that I need to read?

Thanks,

-Steve


krishnabalan
Member++
Posts: 24
Joined: Thu Aug 14, 2014 10:55 am

Post by krishnabalan »

The topic of structure alignment in XC is discussed in multiple threads before.
As of now, XC doesn't not support packed structure (one byte aligned).

But you have a solution, you can move all your structure definitions and its related functions to a '.c' file (i.e. add new file into your project and keep it's extension as '.c' itself and not as '.xc') and add the "__attribute__((packed))" statement in the structure declaration like following

Code: Select all

typedef struct module_descriptor_tag
{
    uint8_t  macAddress0;
    uint8_t  macAddress1;
    uint8_t  macAddress2;
    uint8_t  macAddress3;
    uint8_t  macAddress4;
    uint8_t  macAddress5;
    uint32_t  vendorID;
    uint32_t  moduleID;
} __attribute__((packed)) module_descriptor_t;

module_descriptor_t md;
If you have to access the structure elements in xc files, then you got to do that using helper functions written in .c file.

You can even give it a read https://www.xmos.com/download/public/Ca ... 65F%29.pdf to get to know about how to call functions between XC and C.

Cheers,

Krishna
zonedar
Member
Posts: 14
Joined: Fri Nov 14, 2014 6:58 pm

Post by zonedar »

Krishna,

Thanks and I understand.

-Steve