1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
32 /* Check + save alarm bit first, before the power thread starts watching */
33 rtc_check_alarm_started(false);
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
;
49 alarm_state
&= ~release_alarm
;
51 /* This call resets AF, so we store the state for later recall */
52 rc
= alarm_state
= rtc_check_alarm_flag();
59 * Checks the A2F flag. This call resets A2F once read.
62 bool rtc_check_alarm_flag(void)
67 sw_i2c_read(RTC_ADDR
, 0x0f, buf
, 1);
68 if (buf
[0] & 0x02) flag
= true;
71 sw_i2c_write(RTC_ADDR
, 0x0f, buf
, 1);
76 /* set alarm time registers to the given time (repeat once per day) */
77 void rtc_set_alarm(int h
, int m
)
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
)
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 void rtc_enable_alarm(bool enable
)
104 unsigned char buf
[2];
106 buf
[0] = enable
? 0x26 : 0x24; /* BBSQI INTCN A2IE vs INTCH only */
107 buf
[1] = 0x00; /* reset alarm flags (and OSF for good measure) */
109 sw_i2c_write(RTC_ADDR
, 0x0e, buf
, 2);
112 #endif /* HAVE_RTC_ALARM */
114 int rtc_read_datetime(struct tm
*tm
)
117 unsigned char buf
[7];
119 rc
= sw_i2c_read(RTC_ADDR
, 0, buf
, sizeof(buf
));
121 /* convert from bcd, avoid getting extra bits */
122 tm
->tm_sec
= BCD2DEC(buf
[0] & 0x7f);
123 tm
->tm_min
= BCD2DEC(buf
[1] & 0x7f);
124 tm
->tm_hour
= BCD2DEC(buf
[2] & 0x3f);
125 tm
->tm_wday
= BCD2DEC(buf
[3] & 0x7) - 1; /* timefuncs wants 0..6 for wday */
126 tm
->tm_mday
= BCD2DEC(buf
[4] & 0x3f);
127 tm
->tm_mon
= BCD2DEC(buf
[5] & 0x1f) - 1;
128 tm
->tm_year
= BCD2DEC(buf
[6]) + 100;
133 int rtc_write_datetime(const struct tm
*tm
)
137 unsigned char buf
[7];
141 buf
[2] = tm
->tm_hour
;
142 buf
[3] = tm
->tm_wday
+ 1; /* chip wants 1..7 for wday */
143 buf
[4] = tm
->tm_mday
;
144 buf
[5] = tm
->tm_mon
+ 1;
145 buf
[6] = tm
->tm_year
- 100;
147 for (i
= 0; i
< sizeof(buf
); i
++)
148 buf
[i
] = DEC2BCD(buf
[i
]);
150 buf
[5] |= 0x80; /* chip wants century (always 20xx) */
152 rc
= sw_i2c_write(RTC_ADDR
, 0, buf
, sizeof(buf
));