]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/pcf8583.c
Merge branch 'master' into timelib
[z180-stamp.git] / avr / pcf8583.c
1 /*
2 * Date & Time support for Philips PCF8583 RTC
3 */
4
5 #include "common.h"
6 #include <stdlib.h>
7 #include "time.h"
8 #include "rtc.h"
9 #include "i2c.h"
10 #include "command.h"
11 #include "debug.h"
12
13 #define DEBUG_RTC 0
14
15 #define debug_rtc(fmt, args...) \
16 debug_cond(DEBUG_RTC, fmt, ##args)
17
18 #define REG_CS 0x00 /* control/status */
19 #define REG_CSEC 0x01 /* hundredth of a second */
20 #define REG_SEC 0x02 /* seconds */
21 #define REG_MIN 0x03 /* minutes */
22 #define REG_HOUR 0x04 /* hours */
23 #define REG_YRDATE 0x05 /* year/date */
24 #define REG_WDMON 0x06 /* weekdays/months */
25 #define NR_OF_REGS 7
26
27
28 /* ------------------------------------------------------------------------- */
29
30 static uint_fast8_t bcd2bin(uint8_t val)
31 {
32 return (val >> 4) * 10 + (val & 0x0f);
33 }
34
35 static uint8_t bin2bcd (uint_fast8_t val)
36 {
37 div_t d = div(val, 10);
38
39 return (d.quot << 4) | d.rem;
40 }
41
42
43 int rtc_get (struct tm *tmp)
44 {
45 int rel = 0;
46 uint8_t rtcbuf[NR_OF_REGS];
47 int16_t year;
48
49 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, rtcbuf, NR_OF_REGS);
50 i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0x10, 1, (uint8_t *) &year, 2);
51
52 debug_rtc("Get RTC year: %u, year/date: %02x, wdays/month: %02x, "
53 "hour: %02x, min: %02x, sec: %02x, (stat: %02x)\n", year,
54 rtcbuf[6], rtcbuf[5], rtcbuf[4], rtcbuf[3], rtcbuf[2], rtcbuf[0]);
55
56 tmp->tm_sec = bcd2bin (rtcbuf[REG_SEC] & 0x7F);
57 tmp->tm_min = bcd2bin (rtcbuf[REG_MIN] & 0x7F);
58 tmp->tm_hour = bcd2bin (rtcbuf[REG_HOUR] & 0x3F);
59 tmp->tm_mday = bcd2bin (rtcbuf[REG_YRDATE] & 0x3F);
60 tmp->tm_mon = bcd2bin (rtcbuf[REG_WDMON] & 0x1F) - 1;
61 while (year%4 < (rtcbuf[REG_YRDATE]>>6)) {
62 year++;
63 /* TODO: update RTC ram */
64 }
65 tmp->tm_year = year;
66 tmp->tm_wday = rtcbuf[REG_WDMON] >> 5;
67 tmp->tm_yday = 0;
68 tmp->tm_isdst= 0;
69
70
71 debug_rtc( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
72 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
73 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
74
75 return rel;
76 }
77
78 int rtc_set (struct tm *tmp)
79 {
80 uint8_t rtcbuf[NR_OF_REGS];
81
82 debug_rtc("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
83 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
84 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
85
86 rtcbuf[REG_CS] = 0x84;
87 rtcbuf[REG_CSEC] = 0x00;
88 rtcbuf[REG_WDMON ] = (bin2bcd(tmp->tm_mon) + 1) | ((tmp->tm_wday) << 5);
89 rtcbuf[REG_YRDATE] = ((tmp->tm_year % 4) << 6) | bin2bcd(tmp->tm_mday);
90 rtcbuf[REG_HOUR ] = bin2bcd(tmp->tm_hour);
91 rtcbuf[REG_MIN ] = bin2bcd(tmp->tm_min);
92 rtcbuf[REG_SEC ] = bin2bcd(tmp->tm_sec);
93
94 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, rtcbuf, NR_OF_REGS);
95 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0x10, 1, (uint8_t *) &tmp->tm_year, 2);
96 rtcbuf[REG_CS] = 0x04;
97 i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, rtcbuf, 1);
98
99
100 return 0;
101 }