C: Address trap error when accessing a struct with odd no. of members

149 views Asked by At

I am working with MPLABX with compiler xc16-gcc developing for the 16-bit dsPIC33CH128MP508 microcontroller. I am making code to read from RTC through I2C. I have defined the following structs:

typedef struct tagRTC_VL_SECONDSBITS{
    uint8_t SECONDS: 7 ;
    uint8_t VL:1 ;      /* 0: clock integrity is guarantee */ 
}sRTC_VL_SECONDSBITS_t;

typedef struct tagRTC_MINUTESBITS{
    uint8_t MINUTES: 7 ;
    uint8_t :1 ;
}sRTC_MINUTESBITS_t;

typedef struct tagRTC_HOURSBITS{
    uint8_t HOURS: 6 ;
    uint8_t :2 ;
}sRTC_HOURSBITS_t;

typedef struct tagRTC_DAYSBITS{
    uint8_t DAYS: 6 ;
    uint8_t :2 ;
}sRTC_DAYSBITS_t;

typedef struct tagRTC_WEEKDAYSBITS{
    uint8_t WEEKDAYS: 3 ;
    uint8_t :5 ;
}sRTC_WEEKDAYSBITS_t;

typedef struct tagRTC_CENTURY_MONTHSBITS{
    uint8_t MONTHS: 5 ;
    uint8_t :2 ;
    uint8_t C:1 ;
}sRTC_CENTURY_MONTHSBITS_t;

typedef struct tagRTC_YEARSBITS{
    uint8_t YEARS ;
}sRTC_YEARSBITS_t;

typedef struct tagRTC_TIME{
    sRTC_VL_SECONDSBITS_t sec;
    sRTC_MINUTESBITS_t min ;
    sRTC_HOURSBITS_t hr ;
    sRTC_DAYSBITS_t day ;
    sRTC_WEEKDAYSBITS_t wDay ;
    sRTC_CENTURY_MONTHSBITS_t month ;
    sRTC_YEARSBITS_t year ;

}sRTC_TIME_t ;

The issue with struct sRTC_TIME_t has odd numbers of members, the memory alignment is packed automatically by the compiler. The reading from RTC requires the struct packed also.

When trying to read the first member sec the code trap with TRAPS_ADDRESS_ERR = 2, /** Address error Trap vector */.

sRTC_TIME_t time;

second_reading = time.sec.SECONDS;

When we add an eighth dummy member in the struct sRTC_TIME_t there is no error!

What could be the source of this issue?

0

There are 0 answers