Fix day of week in mc146818
PATCH 1/1 Fix day of week in mc146818
Hello,
According to mc146818 specification, Day of Week register (#6) is between 1 and 7, 1 representing Sunday. According C specification, tm_wday field in struct tm is between 0 and 6, 0 representing Sunday.
Bit 2 of register B (#11) is named DM (data mode) and specifies if RTC stores values in BCD or in binary form.
Attached patch fixes the off by one error and replaces magic value 0x04 by REG_B_DM.
Hervé
mc146818rtc.c +5 -4
@@ -54,6 +54,7 @@#define REG_B_PIE 0x40#define REG_B_AIE 0x20#define REG_B_UIE 0x10 +#define REG_B_DM 0x04struct RTCState {uint8_t cmos_data[128 ];
@@ -186,7 +187,7 @@static inline int to_bcd (RTCState *s,int a) { -if (s->cmos_data[RTC_REG_B ] &0x04 ) { +if (s->cmos_data[RTC_REG_B ] ®_B_DM ) {return a; }else {return ((a /10 ) <<4 ) | (a %10 );
@@ -195,7 +196,7 @@static inline int from_bcd (RTCState *s,int a) { -if (s->cmos_data[RTC_REG_B ] &0x04 ) { +if (s->cmos_data[RTC_REG_B ] ®_B_DM ) {return a; }else {return ((a >>4 ) *10 ) + (a &0x0f );
@@ -213,7 +214,7 @@ (s->cmos_data[RTC_HOURS] & 0x80)) { tm->tm_hour +=12 ; } - tm->tm_wday =from_bcd (s, s->cmos_data[RTC_DAY_OF_WEEK ]); + tm->tm_wday =from_bcd (s, s->cmos_data[RTC_DAY_OF_WEEK ]) -1 ; tm->tm_mday =from_bcd (s, s->cmos_data[RTC_DAY_OF_MONTH ]); tm->tm_mon =from_bcd (s, s->cmos_data[RTC_MONTH ]) -1 ; tm->tm_year =from_bcd (s, s->cmos_data[RTC_YEAR ]) +100 ;
@@ -234,7 +235,7 @@if (tm->tm_hour >=12 ) s->cmos_data[RTC_HOURS ] |=0x80 ; } - s->cmos_data[RTC_DAY_OF_WEEK ] =to_bcd (s, tm->tm_wday); + s->cmos_data[RTC_DAY_OF_WEEK ] =to_bcd (s, tm->tm_wday +1 ); s->cmos_data[RTC_DAY_OF_MONTH ] =to_bcd (s, tm->tm_mday); s->cmos_data[RTC_MONTH ] =to_bcd (s, tm->tm_mon +1 ); s->cmos_data[RTC_YEAR ] =to_bcd (s, tm->tm_year %100 );
Aurelien Jarno
AppliedQuoted 7 lines · Hervé Poussineau
Hello,
According to mc146818 specification, Day of Week register (#6) is between 1 and 7, 1 representing Sunday.
According C specification,tm_wdayfield in struct tm is between 0 and 6, 0 representing Sunday.
Quoted 8 lines
Bit 2 of register B (#11) is named DM (data mode) and specifies if RTC stores values in BCD or in binary form.
Attached patch fixes the off by one error and replaces magic value
0x04byREG_B_DM.Signed-off-by Hervé Poussineau <[email protected]>
Thanks, applied.
Signature · 4 lines
.''`. Aurelien Jarno | GPG: 1024D/F1BCDB73 : :' : Debian developer | Electrical Engineer `. `' [email protected] | [email protected] `- people.debian.org/~aurel32 | www.aurel32.net