Rockbox supports not only 1bpp BMPs
[kugel-rb.git] / firmware / drivers / rtc / rtc_ds1339_ds3231.c
blobd7d3f2492c4a12dd818b0cdd62e745bb19bbbf08
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(unsigned char* buf)
120 int i;
122 i = sw_i2c_read(RTC_ADDR, 0, buf, 7);
124 buf[3]--; /* timefuncs wants 0..6 for wday */
126 return i;
129 int rtc_write_datetime(unsigned char* buf)
131 int i;
133 buf[3]++; /* chip wants 1..7 for wday */
134 buf[5]|=0x80; /* chip wants century (always 20xx) */
136 i = sw_i2c_write(RTC_ADDR, 0, buf, 7);
138 return i;