Index: firmware/export/config-h300.h =================================================================== --- firmware/export/config-h300.h (revision 14811) +++ firmware/export/config-h300.h (working copy) @@ -47,6 +47,9 @@ /* define this if you have a real-time clock */ #define CONFIG_RTC RTC_PCF50606 +/* define this if you have a real-time clock alarm */ +#define HAVE_RTC_ALARM + /* Define this if you have an remote lcd */ #define HAVE_REMOTE_LCD Index: firmware/export/config-iaudiox5.h =================================================================== --- firmware/export/config-iaudiox5.h (revision 14811) +++ firmware/export/config-iaudiox5.h (working copy) @@ -71,6 +71,9 @@ /* define this if you have a real-time clock */ #define CONFIG_RTC RTC_PCF50606 +/* define this if you have a real-time clock alarm */ +#define HAVE_RTC_ALARM + /* Define this if you have an remote lcd */ #define HAVE_REMOTE_LCD Index: firmware/drivers/rtc/rtc_pcf50606.c =================================================================== --- firmware/drivers/rtc/rtc_pcf50606.c (revision 14811) +++ firmware/drivers/rtc/rtc_pcf50606.c (working copy) @@ -24,28 +24,87 @@ #include "pcf50606.h" #include +static char alarm_disable[] = { 0x7f, 0x7f, 0x3f, 0x07, 0x3f, 0x1f, 0xff }; + void rtc_init(void) { + rtc_check_alarm_started(false); } -int rtc_read_datetime(unsigned char* buf) { - int rc; - int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL); - - rc = pcf50606_read_multiple(0x0a, buf, 7); +int rtc_read_datetime(unsigned char* buf) +{ + return pcf50606_read_multiple(0x0a, buf, 7); +} - set_irq_level(oldlevel); - return rc; +int rtc_write_datetime(unsigned char* buf) +{ + pcf50606_write_multiple(0x0a, buf, 7); + return 1; } -int rtc_write_datetime(unsigned char* buf) { - int rc; - int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL); - - rc = pcf50606_write_multiple(0x0a, buf, 7); +/* set alarm time registers to the given time (repeat once per day) */ +void rtc_set_alarm(int h, int m) +{ + /* Set us to wake at the first second of the specified time */ + pcf50606_write(0x11, 0); + /* Convert to BCD */ + pcf50606_write(0x12, ((m/10) << 4) | m%10); + pcf50606_write(0x13, ((h/10) << 4) | h%10); +} - set_irq_level(oldlevel); +/* read out the current alarm time */ +void rtc_get_alarm(int *h, int *m) +{ + char buf[2]; + pcf50606_read_multiple(0x12, buf, 2); + /* Convert from BCD */ + *m = ((buf[0] >> 4) & 0x7)*10 + (buf[0] & 0x0f); + *h = ((buf[1] >> 4) & 0x3)*10 + (buf[1] & 0x0f); +} + +bool rtc_enable_alarm(bool enable) +{ + if (enable) { + pcf50606_write_multiple(0x14, alarm_disable + 3, 4); + /* Unmask the alarm interrupt (might be unneeded) */ + pcf50606_write(0x5, pcf50606_read(0x5) & ~0x80); + /* Make sure wake on RTC is set */ + pcf50606_write(0x8, pcf50606_read(0x8) | 0x10); + } else + pcf50606_write(0x17, 0x99); + + return false; +} + +bool rtc_check_alarm_started(bool release_alarm) +{ + static bool run_before = false, alarm_state; + bool rc; + + if (run_before) { + rc = alarm_state; + alarm_state &= ~release_alarm; + } else { + char rt[3], at[3]; + /* The Ipod bootloader seems to read (and thus clear) the PCF interrupt + * registers, so we need to find some other way to detect if an alarm + * just happened + */ + pcf50606_read_multiple(0x0a, rt, 3); + pcf50606_read_multiple(0x11, at, 3); + + /* If alarm time and real time match within 10 seconds of each other, we + * assume an alarm just triggered + */ + rc = alarm_state = rt[1] == at[1] && rt[2] == at[2] + && (rt[0] - at[0]) <= 10; + run_before = true; + } return rc; } +bool rtc_check_alarm_flag(void) +{ + return pcf50606_read(0x02) & 0x80; +}