fuzev2: prevent button light flickering when accessing µSD
[kugel-rb.git] / firmware / drivers / rtc / rtc_ds1339_ds3231.c
blob01624e048bdd901f803ff37c7bb10674fcc07083
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Robert Kukla
11 * based on Archos code by Linus Nielsen Feltzing, Uwe Freese, Laurent Baum
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "rtc.h"
22 #include "logf.h"
23 #include "sw_i2c.h"
25 #define RTC_ADDR 0xD0
27 void rtc_init(void)
29 sw_i2c_init();
31 #ifdef HAVE_RTC_ALARM
32 /* Check + save alarm bit first, before the power thread starts watching */
33 rtc_check_alarm_started(false);
34 #endif
38 #ifdef HAVE_RTC_ALARM
40 /* check whether the unit has been started by the RTC alarm function */
41 /* (check for A2F, which => started using wakeup alarm) */
42 bool rtc_check_alarm_started(bool release_alarm)
44 static bool alarm_state, run_before;
45 bool rc;
47 if (run_before) {
48 rc = alarm_state;
49 alarm_state &= ~release_alarm;
50 } else {
51 /* This call resets AF, so we store the state for later recall */
52 rc = alarm_state = rtc_check_alarm_flag();
53 run_before = true;
56 return rc;
59 * Checks the A2F flag. This call resets A2F once read.
62 bool rtc_check_alarm_flag(void)
64 unsigned char buf[1];
65 bool flag = false;
67 sw_i2c_read(RTC_ADDR, 0x0f, buf, 1);
68 if (buf[0] & 0x02) flag = true;
70 buf[0] = 0x00;
71 sw_i2c_write(RTC_ADDR, 0x0f, buf, 1);
73 return flag;
76 /* set alarm time registers to the given time (repeat once per day) */
77 void rtc_set_alarm(int h, int m)
79 unsigned char buf[3];
81 buf[0] = (((m / 10) << 4) | (m % 10)) & 0x7f; /* minutes */
82 buf[1] = (((h / 10) << 4) | (h % 10)) & 0x3f; /* hour */
83 buf[2] = 0x80; /* repeat every day */
85 sw_i2c_write(RTC_ADDR, 0x0b, buf, 3);
88 /* read out the current alarm time */
89 void rtc_get_alarm(int *h, int *m)
91 unsigned char buf[2];
93 sw_i2c_read(RTC_ADDR, 0x0b, buf, 2);
95 *m = ((buf[0] & 0x70) >> 4) * 10 + (buf[0] & 0x0f);
96 *h = ((buf[1] & 0x30) >> 4) * 10 + (buf[1] & 0x0f);
99 /* turn alarm on or off by setting the alarm flag enable */
100 /* the alarm is automatically disabled when the RTC gets Vcc power at startup */
101 /* avoid that an alarm occurs when the device is on because this locks the ON key forever */
102 /* returns false if alarm was set and alarm flag (output) is off */
103 /* returns true if alarm flag went on, which would lock the device, so the alarm was disabled again */
104 bool rtc_enable_alarm(bool enable)
106 unsigned char buf[2];
108 buf[0] = enable ? 0x26 : 0x24; /* BBSQI INTCN A2IE vs INTCH only */
109 buf[1] = 0x00; /* reset alarm flags (and OSF for good measure) */
111 sw_i2c_write(RTC_ADDR, 0x0e, buf, 2);
113 return false; /* all ok */
116 #endif /* HAVE_RTC_ALARM */
118 int rtc_read_datetime(struct tm *tm)
120 int rc;
121 unsigned char buf[7];
123 rc = sw_i2c_read(RTC_ADDR, 0, buf, sizeof(buf));
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] & 0x7) - 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;
134 return rc;
137 int rtc_write_datetime(const struct tm *tm)
139 unsigned int i;
140 int rc;
141 unsigned char buf[7];
143 buf[0] = tm->tm_sec;
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;
151 for (i = 0; i < sizeof(buf); i++)
152 buf[i] = DEC2BCD(buf[i]);
154 buf[5] |= 0x80; /* chip wants century (always 20xx) */
156 rc = sw_i2c_write(RTC_ADDR, 0, buf, sizeof(buf));
158 return rc;