summaryrefslogtreecommitdiff
path: root/firmware/drivers/rtc/rtc_m41st84w.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/drivers/rtc/rtc_m41st84w.c')
-rw-r--r--firmware/drivers/rtc/rtc_m41st84w.c54
1 files changed, 38 insertions, 16 deletions
diff --git a/firmware/drivers/rtc/rtc_m41st84w.c b/firmware/drivers/rtc/rtc_m41st84w.c
index 738fb201bf..2627191252 100644
--- a/firmware/drivers/rtc/rtc_m41st84w.c
+++ b/firmware/drivers/rtc/rtc_m41st84w.c
@@ -110,10 +110,10 @@ void rtc_set_alarm(int h, int m)
110 110
111 /* for daily alarm, RPT5=RPT4=on, RPT1=RPT2=RPT3=off */ 111 /* for daily alarm, RPT5=RPT4=on, RPT1=RPT2=RPT3=off */
112 112
113 rtc_write(0x0e, 0x00); /* seconds 0 and RTP1 */ 113 rtc_write(0x0e, 0x00); /* seconds 0 and RTP1 */
114 rtc_write(0x0d, ((m / 10) << 4) | (m % 10)); /* minutes and RPT2 */ 114 rtc_write(0x0d, DEC2BCD(m); /* minutes and RPT2 */
115 rtc_write(0x0c, ((h / 10) << 4) | (h % 10)); /* hour and RPT3 */ 115 rtc_write(0x0c, DEC2BCD(h); /* hour and RPT3 */
116 rtc_write(0x0b, 0xc1); /* set date 01 and RPT4 and RTP5 */ 116 rtc_write(0x0b, 0xc1); /* set date 01 and RPT4 and RTP5 */
117 117
118 /* set month to 1, if it's invalid, the rtc does an alarm every second instead */ 118 /* set month to 1, if it's invalid, the rtc does an alarm every second instead */
119 data = rtc_read(0x0a); 119 data = rtc_read(0x0a);
@@ -128,10 +128,10 @@ void rtc_get_alarm(int *h, int *m)
128 unsigned char data; 128 unsigned char data;
129 129
130 data = rtc_read(0x0c); 130 data = rtc_read(0x0c);
131 *h = ((data & 0x30) >> 4) * 10 + (data & 0x0f); 131 *h = BCD2DEC(data & 0x3f);
132 132
133 data = rtc_read(0x0d); 133 data = rtc_read(0x0d);
134 *m = ((data & 0x70) >> 4) * 10 + (data & 0x0f); 134 *m = BCD2DEC(data & 0x7f);
135} 135}
136 136
137/* turn alarm on or off by setting the alarm flag enable */ 137/* turn alarm on or off by setting the alarm flag enable */
@@ -250,31 +250,53 @@ int rtc_read_multiple(unsigned char address, unsigned char *buf, int numbytes)
250 return ret; 250 return ret;
251} 251}
252 252
253int rtc_read_datetime(unsigned char* buf) { 253int rtc_read_datetime(struct tm *tm)
254{
254 int rc; 255 int rc;
256 unsigned char buf[7];
257
258 rc = rtc_read_multiple(1, buf, sizeof(buf));
255 259
256 rc = rtc_read_multiple(1, buf, 7); 260 /* convert from bcd, avoid getting extra bits */
261 tm->tm_sec = BCD2DEC(buf[0] & 0x7f);
262 tm->tm_min = BCD2DEC(buf[1] & 0x7f);
263 tm->tm_hour = BCD2DEC(buf[2] & 0x3f);
264 tm->tm_wday = BCD2DEC(buf[3] & 0x3);
265 tm->tm_mday = BCD2DEC(buf[4] & 0x3f);
266 tm->tm_mon = BCD2DEC(buf[5] & 0x1f) - 1;
267 tm->tm_year = BCD2DEC(buf[6]) + 100;
257 268
258 /* Adjust weekday */ 269 /* Adjust weekday */
259 if(buf[3] == 7) 270 if (tm->tm_wday == 7)
260 buf[3]=0; 271 tm->tm_wday = 0;
261 272
262 return rc; 273 return rc;
263} 274}
264 275
265int rtc_write_datetime(unsigned char* buf) { 276int rtc_write_datetime(const struct tm *tm)
266 int i; 277{
278 unsigned int i;
267 int rc = 0; 279 int rc = 0;
268 280 unsigned char buf[7];
281
282 buf[0] = tm->tm_sec;
283 buf[1] = tm->tm_min;
284 buf[2] = tm->tm_hour;
285 buf[3] = tm->tm_wday;
286 buf[4] = tm->tm_mday;
287 buf[5] = tm->tm_mon + 1;
288 buf[6] = tm->tm_year - 100;
289
269 /* Adjust weekday */ 290 /* Adjust weekday */
270 if(buf[3] == 0) 291 if (buf[3] == 0)
271 buf[3] = 7; 292 buf[3] = 7;
272 293
273 for (i = 0; i < 7 ; i++) 294 for (i = 0; i < sizeof(buf) ;i++)
274 { 295 {
275 rc |= rtc_write(i+1, buf[i]); 296 rc |= rtc_write(i + 1, DEC2BCD(buf[i]));
276 } 297 }
277 rc |= rtc_write(8, 0x80); /* Out=1, calibration = 0 */ 298 rc |= rtc_write(8, 0x80); /* Out=1, calibration = 0 */
278 299
279 return rc; 300 return rc;
280} 301}
302