cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC 4 MCU

AnWa_1259496
Contributor

I have a USB report structure:

   

Uint8 Buttons

   

int16 Xaxis

   

int16 Y axis

   

 

   

But the compiler assigns 2 bytes for the Unit8 instead of one so the USB packet is incorrect. Is this expected?

0 Likes
8 Replies
JoMe_264151
Expert II

Since the int16s have to be aligned on a word boundary this is expected. You could try to put your uint8 as the last element in the structure, then the size will be 5 bytes.

   

 

   

Bob

0 Likes
ETRO_SSN583
Esteemed Contributor

Or make it a word and cast it back to int8 when you use it.

   

 

   

Regards, Dana.

0 Likes
AnWa_1259496
Contributor

 But the data format has to be as stated because it needs to match the USB descriptor.

   

I guess I will need to make it all int8 and programatically split the 16 bits into 8s.

0 Likes
odissey1
Honored Contributor II

 You need to make a 'packed' type structure, so it will be exactly 5 bytes long

   

See example below, now sizeof(MyStruc)=5.

   

odissey1

   

 

   

pragma pack(push, 1) // exact fit - no padding

   

struct MyStruct { uint8 a; uint16 b; uint16 c; };

   

#pragma pack(pop) //back to whatever the previous packing mode was 

0 Likes
JoMe_264151
Expert II

Have a look at the packed attribute for structs.

   

 

   

Bob

0 Likes
HeLi_263931
Honored Contributor II

To pack a struct:

   

typedef struct
{
    uint8 first;
    int16 second;
} __attribute__ ((__packed__)) data;

0 Likes
AnWa_1259496
Contributor

 The method posted by Odissey1 worked.

   

Using the "packed" attribute results in a warning: attribute "packed" ignored..

0 Likes
odissey1
Honored Contributor II

This one might work:

   

typedef myStruct
{
    uint8 first;
    int16 second;
} __attribute__ ((packed));

   

myStruct data;

0 Likes