summaryrefslogtreecommitdiff
path: root/firmware/drivers/rtc/rtc_ds1339_ds3231.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/drivers/rtc/rtc_ds1339_ds3231.c')
-rw-r--r--firmware/drivers/rtc/rtc_ds1339_ds3231.c44
1 files changed, 32 insertions, 12 deletions
diff --git a/firmware/drivers/rtc/rtc_ds1339_ds3231.c b/firmware/drivers/rtc/rtc_ds1339_ds3231.c
index d7d3f2492c..3788dc75c0 100644
--- a/firmware/drivers/rtc/rtc_ds1339_ds3231.c
+++ b/firmware/drivers/rtc/rtc_ds1339_ds3231.c
@@ -115,26 +115,46 @@ bool rtc_enable_alarm(bool enable)
115 115
116#endif /* HAVE_RTC_ALARM */ 116#endif /* HAVE_RTC_ALARM */
117 117
118int rtc_read_datetime(unsigned char* buf) 118int rtc_read_datetime(struct tm *tm)
119{ 119{
120 int i; 120 int rc;
121 unsigned char buf[7];
121 122
122 i = sw_i2c_read(RTC_ADDR, 0, buf, 7); 123 rc = sw_i2c_read(RTC_ADDR, 0, buf, sizeof(buf));
123 124
124 buf[3]--; /* timefuncs wants 0..6 for wday */ 125 /* convert from bcd, avoid getting extra bits */
126 tm->tm_sec = BCD2DEC(buf[0] & 0x7f);
127 tm->tm_min = BCD2DEC(buf[1] & 0x7f);
128 tm->tm_hour = BCD2DEC(buf[2] & 0x3f);
129 tm->tm_wday = BCD2DEC(buf[3] & 0x3) - 1; /* timefuncs wants 0..6 for wday */
130 tm->tm_mday = BCD2DEC(buf[4] & 0x3f);
131 tm->tm_mon = BCD2DEC(buf[5] & 0x1f) - 1;
132 tm->tm_year = BCD2DEC(buf[6]) + 100;
125 133
126 return i; 134 return rc;
127} 135}
128 136
129int rtc_write_datetime(unsigned char* buf) 137int rtc_write_datetime(const struct tm *tm)
130{ 138{
131 int i; 139 unsigned int i;
140 int rc;
141 unsigned char buf[7];
132 142
133 buf[3]++; /* chip wants 1..7 for wday */ 143 buf[0] = tm->tm_sec;
134 buf[5]|=0x80; /* chip wants century (always 20xx) */ 144 buf[1] = tm->tm_min;
145 buf[2] = tm->tm_hour;
146 buf[3] = tm->tm_wday + 1; /* chip wants 1..7 for wday */
147 buf[4] = tm->tm_mday;
148 buf[5] = tm->tm_mon + 1;
149 buf[6] = tm->tm_year - 100;
135 150
136 i = sw_i2c_write(RTC_ADDR, 0, buf, 7); 151 for (i = 0; i < sizeof(buf); i++)
152 buf[i] = DEC2BCD(buf[i]);
137 153
138 return i; 154 buf[5] |= 0x80; /* chip wants century (always 20xx) */
155
156 rc = sw_i2c_write(RTC_ADDR, 0, buf, sizeof(buf));
157
158 return rc;
139} 159}
140 160