- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Or make it a word and cast it back to int8 when you use it.
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
To pack a struct:
typedef struct
{
uint8 first;
int16 second;
} __attribute__ ((__packed__)) data;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The method posted by Odissey1 worked.
Using the "packed" attribute results in a warning: attribute "packed" ignored..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This one might work:
typedef myStruct
{
uint8 first;
int16 second;
} __attribute__ ((packed));
myStruct data;