How do I load and access elements with different sizes in a structure?I'm trying to adapt a code for STM32F103.The original structure is defined like this:
typedef struct// FFB: PID Pool Feature Report { uint8_t reportId; // =7 uint16_t ramPoolSize; // ? uint8_t maxSimultaneousEffects; // ?? 40? uint8_t memoryManagement; // Bits: 0=DeviceManagedPool, 1=SharedParameterBlocks } USB_FFBReport_PIDPool_Feature_Data_t;
My code starts and accesses like this:
struct USB_FFBReport_PIDPool_Feature_Data_t ; ... USB_FFBReport_PIDPool_Feature_Data_t ans; uint8_t *Feature(uint16_t Length) { ... ans.reportId = 7; ans.ramPoolSize = 0xffff; ans.maxSimultaneousEffects = 14; ans.memoryManagement = 3; return (uint8_t *) &ans; }
The above routine returns me:0700FFFF0E
The correct is:07FFFF0E03
To have the correct values I have to leave the fields of the structure all with a byte (uint8_t).
typedef struct// FFB: PID Pool Feature Report { uint8_t reportId; uint8_t ramPoolSizeH; uint8_t ramPoolSizeL; uint8_t maxSimultaneousEffects; uint8_t memoryManagement; } USB_FFBReport_PIDPool_Feature_Data_t;
How should I proceed to access or load the structure correctly?
The compiler is padding your original struct to make for faster/easier access. To override this behaviour use
__attribute__ ((packed))
thank you so much.It worked.