Tuesday, December 16, 2008

Avoid auto padding by PIC32 compiler for data structure

Using __attribute__((packed)) for variable and structure member.




typedef struct sDaq2ComAggregationRaw001
{
GSNTYPES_BYTE FeederAndPhase; //0x01:Va 0x02:Vb....
GSNTYPES_BYTE TimeStampCycles;
GSNTYPES_ULONG TimeStampSeconds;
GSNTYPES_DOUBLE llDft_Goertzel_Aggregation_SquareSumImgReal[CABLE_TRACKER_MAX_NUM_HARMONICS]; GSNTYPES_DOUBLE llSquareSumForAggregationLog;
GSNTYPES_DOUBLE llSquareSumPerCycleMaxOverLoggingInterval; //Optional to be logged.--advanced feature ToDo
GSNTYPES_DOUBLE llSquareSumPerCycleMinOverLoggingInterval; //Optional to be logged.--advanced feature ToDo

}
__attribute__((packed)) S_DAQ2COM_AGGREGATION_RAW_001;



typedef struct sDaq2ComAggregationRaw002
{
GSNTYPES_BYTE FeederAndPhase; //0x11:Feeder1PhaseA, 0x12:Feeder1PhaseB...0x41:Feeder4PhaseA
GSNTYPES_BYTE TimeStampCycles;
GSNTYPES_ULONG TimeStampSeconds;
GSNTYPES_DOUBLE llDft_Goertzel_Aggregation_SquareSumImgReal[CABLE_TRACKER_MAX_NUM_HARMONICS]; GSNTYPES_DOUBLE llSquareSumForAggregationLog;
GSNTYPES_DOUBLE llSquareSumPerCycleMaxOverLoggingInterval;
GSNTYPES_DOUBLE llSquareSumPerCycleMinOverLoggingInterval;
GSNTYPES_DOUBLE llActivePwrSumForAggregationLog; //

}
__attribute__((packed)) S_DAQ2COM_AGGREGATION_RAW_002;



typedef struct sStructureID
{
GSNTYPES_USHORT variation;
GSNTYPES_BYTE version;
GSNTYPES_BYTE type; //config, log, events. see E_StructType definition.

}__attribute__((packed)) S_StructureID;


typedef struct sRdHeadDataStorage
{
GSNTYPES_USHORT len; //is based on the struct ID,number bytes of record body,exclude record header.
S_StructureID structID;
}S_RecordHeadDataStorage;



typedef union sDataRecordBody
{
//The ptrDataBody could point to following data Structures.

S_COM_CONFIG_BLOCK_001 com_configuration_001;
S_COM_CONFIG_BLOCK_002 com_configuration_002;
S_DAQ_CONFIG_BLOCK_001 daq_configuration_001;
S_DAQ_CONFIG_BLOCK_002 daq_configuration_002;
S_DAQ2COM_AGGREGATION_RAW_001 daq2com_aggregation_raw_001;
S_DAQ2COM_AGGREGATION_RAW_002 daq2com_aggregation_raw_002;
S_DAQ2COM_AGGREGATION_RAW_003 daq2com_aggregation_raw_003;
S_DAQ2COM_EVENTS_RAW_001 daq2com_events_raw_001;
S_DAQ2COM_EVENTS_RAW_002 daq2com_events_raw_002;
S_DAQ2COM_EVENTS_RAW_003 daq2com_events_raw_003;
S_COM2EEP_AGGREGATION_REAL_001 com2eep_aggregation_real_001;
S_COM2EEP_AGGREGATION_REAL_002 com2eep_aggregation_real_002;
S_COM2EEP_AGGREGATION_REAL_003 com2eep_aggregation_real_003;
S_COM2EEP_EVENTS_REAL_001 com2eep_events_real_001;
S_COM2EEP_EVENTS_REAL_002 com2eep_events_real_002;
S_COM2EEP_EVENTS_REAL_003 com2eep_events_real_003;

}U_DataRecordBody;


typedef struct sDataRecord
{
S_RecordHeadDataStorage rdHead;
U_DataRecordBody rdDataBody;

}__attribute__((packed)) S_DataRecord;

No comments: