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