diff options
Diffstat (limited to 'firmware/drivers/rtc/rtc_pcf50605.c')
-rw-r--r-- | firmware/drivers/rtc/rtc_pcf50605.c | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/firmware/drivers/rtc/rtc_pcf50605.c b/firmware/drivers/rtc/rtc_pcf50605.c index fe12766c4a..b030fba37a 100644 --- a/firmware/drivers/rtc/rtc_pcf50605.c +++ b/firmware/drivers/rtc/rtc_pcf50605.c | |||
@@ -36,14 +36,45 @@ void rtc_init(void) | |||
36 | rtc_check_alarm_started(false); | 36 | rtc_check_alarm_started(false); |
37 | } | 37 | } |
38 | 38 | ||
39 | int rtc_read_datetime(unsigned char* buf) | 39 | int rtc_read_datetime(struct tm *tm) |
40 | { | 40 | { |
41 | return pcf50605_read_multiple(0x0a, buf, 7); | 41 | unsigned int i; |
42 | int rc; | ||
43 | unsigned char buf[7]; | ||
44 | rc = pcf50605_read_multiple(0x0a, buf, sizeof(buf)); | ||
45 | |||
46 | for (i = 0; i < sizeof(buf); i++) | ||
47 | buf[i] = BCD2DEC(buf[i]); | ||
48 | |||
49 | tm->tm_sec = buf[0]; | ||
50 | tm->tm_min = buf[1]; | ||
51 | tm->tm_hour = buf[2]; | ||
52 | tm->tm_wday = buf[3]; | ||
53 | tm->tm_mday = buf[4]; | ||
54 | tm->tm_mon = buf[5] - 1; | ||
55 | tm->tm_year = buf[6] + 100; | ||
56 | |||
57 | return rc; | ||
42 | } | 58 | } |
43 | 59 | ||
44 | int rtc_write_datetime(unsigned char* buf) | 60 | int rtc_write_datetime(const struct tm *tm) |
45 | { | 61 | { |
46 | pcf50605_write_multiple(0x0a, buf, 7); | 62 | unsigned int i; |
63 | unsigned char buf[7]; | ||
64 | |||
65 | buf[0] = tm->tm_sec; | ||
66 | buf[1] = tm->tm_min; | ||
67 | buf[2] = tm->tm_hour; | ||
68 | buf[3] = tm->tm_wday; | ||
69 | buf[4] = tm->tm_mday; | ||
70 | buf[5] = tm->tm_mon + 1; | ||
71 | buf[6] = tm->tm_year - 100; | ||
72 | |||
73 | for (i = 0; i < sizeof(buf); i++) | ||
74 | buf[i] = DEC2BCD(buf[i]); | ||
75 | |||
76 | pcf50605_write_multiple(0x0a, buf, sizeof(buf)); | ||
77 | |||
47 | return 1; | 78 | return 1; |
48 | } | 79 | } |
49 | 80 | ||
@@ -121,17 +152,17 @@ void rtc_set_alarm(int h, int m) | |||
121 | /* Set us to wake at the first second of the specified time */ | 152 | /* Set us to wake at the first second of the specified time */ |
122 | pcf50605_write(0x11, 0); | 153 | pcf50605_write(0x11, 0); |
123 | /* Convert to BCD */ | 154 | /* Convert to BCD */ |
124 | pcf50605_write(0x12, ((m/10) << 4) | m%10); | 155 | pcf50605_write(0x12, DEC2BCD(m)); |
125 | pcf50605_write(0x13, ((h/10) << 4) | h%10); | 156 | pcf50605_write(0x13, DEC2BCD(h)); |
126 | } | 157 | } |
127 | 158 | ||
128 | void rtc_get_alarm(int *h, int *m) | 159 | void rtc_get_alarm(int *h, int *m) |
129 | { | 160 | { |
130 | char buf[2]; | 161 | char buf[2]; |
131 | 162 | ||
132 | pcf50605_read_multiple(0x12, buf, 2); | 163 | pcf50605_read_multiple(0x12, buf, sizeof(buf)); |
133 | /* Convert from BCD */ | 164 | /* Convert from BCD */ |
134 | *m = ((buf[0] >> 4) & 0x7)*10 + (buf[0] & 0x0f); | 165 | *m = BCD2DEC(buf[0]); |
135 | *h = ((buf[1] >> 4) & 0x3)*10 + (buf[1] & 0x0f); | 166 | *h = BCD2DEC(buf[1]); |
136 | } | 167 | } |
137 | 168 | ||