diff options
Diffstat (limited to 'firmware/drivers/rtc/rtc_rx5x348ab.c')
-rw-r--r-- | firmware/drivers/rtc/rtc_rx5x348ab.c | 49 |
1 files changed, 39 insertions, 10 deletions
diff --git a/firmware/drivers/rtc/rtc_rx5x348ab.c b/firmware/drivers/rtc/rtc_rx5x348ab.c index 331b2d6cb8..f31ab5623e 100644 --- a/firmware/drivers/rtc/rtc_rx5x348ab.c +++ b/firmware/drivers/rtc/rtc_rx5x348ab.c | |||
@@ -22,31 +22,60 @@ | |||
22 | #include "config.h" | 22 | #include "config.h" |
23 | #include "spi.h" | 23 | #include "spi.h" |
24 | #include "rtc.h" | 24 | #include "rtc.h" |
25 | #include <stdbool.h> | 25 | |
26 | /* Choose one of: */ | 26 | /* Choose one of: */ |
27 | #define ADDR_READ 0x04 | 27 | #define ADDR_READ 0x04 |
28 | #define ADDR_WRITE 0x00 | 28 | #define ADDR_WRITE 0x00 |
29 | /* and one of: */ | 29 | /* and one of: */ |
30 | #define ADDR_ONE 0x08 | 30 | #define ADDR_ONE 0x08 |
31 | #define ADDR_BURST 0x00 | 31 | #define ADDR_BURST 0x00 |
32 | |||
32 | void rtc_init(void) | 33 | void rtc_init(void) |
33 | { | 34 | { |
34 | } | 35 | } |
35 | 36 | ||
36 | int rtc_read_datetime(unsigned char* buf) | 37 | int rtc_read_datetime(struct tm *tm) |
37 | { | 38 | { |
39 | unsigned int i; | ||
40 | unsigned char buf[7]; | ||
38 | char command = ADDR_READ|ADDR_BURST; /* burst read from the start of the time/date reg */ | 41 | char command = ADDR_READ|ADDR_BURST; /* burst read from the start of the time/date reg */ |
39 | spi_block_transfer(SPI_target_RX5X348AB, &command, 1, buf, 7); | 42 | |
43 | spi_block_transfer(SPI_target_RX5X348AB, &command, 1, buf, sizeof(buf)); | ||
44 | |||
45 | for (i = 0; i < sizeof(buf); i++) | ||
46 | buf[i] = BCD2DEC(buf[i]); | ||
47 | |||
48 | tm->tm_sec = buf[0]; | ||
49 | tm->tm_min = buf[1]; | ||
50 | tm->tm_hour = buf[2]; | ||
51 | tm->tm_wday = buf[3]; | ||
52 | tm->tm_mday = buf[4]; | ||
53 | tm->tm_mon = buf[5] - 1; | ||
54 | tm->tm_year = buf[6] + 100; | ||
55 | |||
40 | return 1; | 56 | return 1; |
41 | } | 57 | } |
42 | int rtc_write_datetime(unsigned char* buf) | 58 | |
59 | int rtc_write_datetime(const struct tm *tm) | ||
43 | { | 60 | { |
61 | unsigned int i; | ||
44 | char command = ADDR_WRITE|ADDR_BURST; /* burst read from the start of the time/date reg */ | 62 | char command = ADDR_WRITE|ADDR_BURST; /* burst read from the start of the time/date reg */ |
45 | char data[8]; | 63 | unsigned char buf[8]; |
46 | int i; | 64 | |
47 | data[0] = command; | 65 | buf[0] = command; |
48 | for (i=1;i<8;i++) | 66 | buf[1] = tm->tm_sec; |
49 | data[i] = buf[i-1]; | 67 | buf[2] = tm->tm_min; |
50 | spi_block_transfer(SPI_target_RX5X348AB, data, 8, NULL, 0); | 68 | buf[3] = tm->tm_hour; |
69 | buf[4] = tm->tm_wday; | ||
70 | buf[5] = tm->tm_mday; | ||
71 | buf[6] = tm->tm_mon + 1; | ||
72 | buf[7] = tm->tm_year - 100; | ||
73 | |||
74 | /* don't encode the comand byte */ | ||
75 | for (i = 1; i < sizeof(buf); i++) | ||
76 | buf[i] = DEC2BCD(buf[i]); | ||
77 | |||
78 | spi_block_transfer(SPI_target_RX5X348AB, buf, sizeof(buf), NULL, 0); | ||
51 | return 1; | 79 | return 1; |
52 | } | 80 | } |
81 | |||