1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 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 ****************************************************************************/
29 /* Values which each disable one alarm time register */
30 static const char alarm_disable
[] = {
31 0x7f, 0x7f, 0x3f, 0x07, 0x3f, 0x1f, 0xff
36 rtc_check_alarm_started(false);
39 int rtc_read_datetime(struct tm
*tm
)
44 rc
= pcf50605_read_multiple(0x0a, buf
, sizeof(buf
));
46 for (i
= 0; i
< sizeof(buf
); i
++)
47 buf
[i
] = BCD2DEC(buf
[i
]);
54 tm
->tm_mon
= buf
[5] - 1;
55 tm
->tm_year
= buf
[6] + 100;
60 int rtc_write_datetime(const struct tm
*tm
)
70 buf
[5] = tm
->tm_mon
+ 1;
71 buf
[6] = tm
->tm_year
- 100;
73 for (i
= 0; i
< sizeof(buf
); i
++)
74 buf
[i
] = DEC2BCD(buf
[i
]);
76 pcf50605_write_multiple(0x0a, buf
, sizeof(buf
));
82 * Checks the PCF interrupt 1 register bit 7 to see if an alarm interrupt has
83 * triggered since last we checked.
85 bool rtc_check_alarm_flag(void)
87 return pcf50605_read(0x02) & 0x80;
91 * Enables or disables the alarm.
92 * The Ipod bootloader clears all PCF interrupt registers and always enables
93 * the "wake on RTC" bit on OOCC1, so we have to rely on other means to find
94 * out if we just woke from an alarm.
95 * Return value is always false for us.
97 bool rtc_enable_alarm(bool enable
)
100 /* Tell the PCF to ignore everything but second, minute and hour, so
101 * that an alarm will trigger the next time the alarm time occurs.
103 pcf50605_write_multiple(0x14, alarm_disable
+ 3, 4);
104 /* Unmask the alarm interrupt (might be unneeded) */
105 pcf50605_write(0x5, pcf50605_read(0x5) & ~0x80);
106 /* Make sure wake on RTC is set when shutting down */
107 pcf50605_wakeup_flags
|= 0x10;
109 /* We use this year to indicate a disabled alarm. If you happen to live
110 * around this time and are annoyed by this, feel free to seek out my
111 * grave and do something nasty to it.
113 pcf50605_write(0x17, 0x99);
114 /* Make sure we don't wake on RTC after shutting down */
115 pcf50605_wakeup_flags
&= ~0x10;
121 * Check if alarm caused unit to start.
123 bool rtc_check_alarm_started(bool release_alarm
)
125 static bool run_before
= false, alarm_state
;
130 alarm_state
&= ~release_alarm
;
133 /* The Ipod bootloader seems to read (and thus clear) the PCF interrupt
134 * registers, so we need to find some other way to detect if an alarm
137 pcf50605_read_multiple(0x0a, rt
, 3);
138 pcf50605_read_multiple(0x11, at
, 3);
140 /* If alarm time and real time match within 10 seconds of each other, we
141 * assume an alarm just triggered
143 rc
= alarm_state
= rt
[1] == at
[1] && rt
[2] == at
[2]
144 && (rt
[0] - at
[0]) <= 10;
150 void rtc_set_alarm(int h
, int m
)
152 /* Set us to wake at the first second of the specified time */
153 pcf50605_write(0x11, 0);
155 pcf50605_write(0x12, DEC2BCD(m
));
156 pcf50605_write(0x13, DEC2BCD(h
));
159 void rtc_get_alarm(int *h
, int *m
)
163 pcf50605_read_multiple(0x12, buf
, sizeof(buf
));
164 /* Convert from BCD */
165 *m
= BCD2DEC(buf
[0]);
166 *h
= BCD2DEC(buf
[1]);