1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Linus Nielsen Feltzing, Uwe Freese, Laurent Baum,
11 * Przemyslaw Holubowski
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
30 /*RTC_E8564's slave address is 0x51*/
34 #define RTC_CTRL1 0x00
35 #define RTC_CTRL2 0x01
36 #define RTC_ALARM_MINUTES 0x09
37 #define RTC_ALARM_HOURS 0x0A
38 #define RTC_ALARM_DAY 0x0B
39 #define RTC_ALARM_WDAY 0x0C
40 #define RTC_TIMER_CTRL 0x0E
41 #define RTC_TIMER 0x0F
43 /* Control 2 register flags */
48 #define RTC_TI_TP 0x10
50 /* Alarm registers flags */
53 /* Timer register flags */
56 bool rtc_lock_alarm_clear
=true;
63 /* initialize Control 1 register */
65 pp_i2c_send(RTC_ADDR
, RTC_CTRL1
, tmp
);
67 /* read value of the Control 2 register - we'll need it to preserve alarm and timer interrupt assertion flags */
68 rv
= i2c_readbytes(RTC_ADDR
, RTC_CTRL2
, 1, &tmp
);
69 /* preserve alarm and timer interrupt flags */
70 tmp
&= (RTC_TF
| RTC_AF
| RTC_TIE
| RTC_AIE
);
71 pp_i2c_send(RTC_ADDR
, RTC_CTRL2
, tmp
);
74 int rtc_read_datetime(struct tm
*tm
)
79 read
= i2c_readbytes(RTC_ADDR
, 0x02, sizeof(buf
), buf
);
81 /* convert from bcd, avoid getting extra bits */
82 tm
->tm_sec
= BCD2DEC(buf
[0] & 0x7f);
83 tm
->tm_min
= BCD2DEC(buf
[1] & 0x7f);
84 tm
->tm_hour
= BCD2DEC(buf
[2] & 0x3f);
85 tm
->tm_mday
= BCD2DEC(buf
[3] & 0x3f);
86 tm
->tm_wday
= BCD2DEC(buf
[4] & 0x7);
87 tm
->tm_mon
= BCD2DEC(buf
[5] & 0x1f) - 1;
88 tm
->tm_year
= BCD2DEC(buf
[6]) + 100;
93 int rtc_write_datetime(const struct tm
*tm
)
100 buf
[2] = tm
->tm_hour
;
101 buf
[3] = tm
->tm_mday
;
102 buf
[4] = tm
->tm_wday
;
103 buf
[5] = tm
->tm_mon
+ 1;
104 buf
[6] = tm
->tm_year
- 100;
106 for (i
= 0; i
< sizeof(buf
); i
++)
107 pp_i2c_send(RTC_ADDR
, 0x02 + i
, DEC2BCD(buf
[i
]));
112 void rtc_set_alarm(int h
, int m
)
114 unsigned char buf
[4] = {0};
117 /* clear alarm interrupt */
118 rv
= i2c_readbytes(RTC_ADDR
, RTC_CTRL2
, 1, buf
);
120 pp_i2c_send(RTC_ADDR
, RTC_CTRL2
, buf
[0]);
122 /* prepare new alarm */
126 /* ignore minutes comparison query */
132 /* ignore hours comparison query */
135 /* ignore day and wday */
139 /* write new alarm */
140 for(i
= 0; i
< 4; i
++)
141 pp_i2c_send(RTC_ADDR
, RTC_ALARM_MINUTES
+ i
, buf
[i
]);
143 /* note: alarm is not enabled at the point */
146 void rtc_get_alarm(int *h
, int *m
)
148 unsigned char buf
[4]={0};
150 /* get alarm preset */
151 i2c_readbytes(RTC_ADDR
, RTC_ALARM_MINUTES
, 4 ,buf
);
153 *m
= BCD2DEC(buf
[0] & 0x7f);
154 *h
= BCD2DEC(buf
[0] & 0x3f);
157 bool rtc_enable_alarm(bool enable
)
164 /* enable alarm interrupt */
165 rv
= i2c_readbytes(RTC_ADDR
, RTC_CTRL2
, 1, &tmp
);
168 pp_i2c_send(RTC_ADDR
, RTC_CTRL2
, tmp
);
172 /* disable alarm interrupt */
173 if(rtc_lock_alarm_clear
)
174 /* lock disabling alarm before it was checked whether or not the unit was started by RTC alarm */
176 rv
= i2c_readbytes(RTC_ADDR
, RTC_CTRL2
, 1, &tmp
);
177 tmp
&= ~(RTC_AIE
| RTC_AF
);
178 pp_i2c_send(RTC_ADDR
, RTC_CTRL2
, tmp
);
184 bool rtc_check_alarm_started(bool release_alarm
)
186 static bool alarm_state
, run_before
= false;
193 started
= alarm_state
;
194 alarm_state
&= ~release_alarm
;
198 /* read Control 2 register which contains alarm flag */
199 rv
= i2c_readbytes(RTC_ADDR
, RTC_CTRL2
, 1, &tmp
);
201 alarm_state
= started
= ( (tmp
& RTC_AF
) && (tmp
& RTC_AIE
) );
203 if(release_alarm
&& started
)
205 rv
= i2c_readbytes(RTC_ADDR
, RTC_CTRL2
, 1, &tmp
);
206 /* clear alarm interrupt enable and alarm flag */
207 tmp
&= ~(RTC_AF
| RTC_AIE
);
208 pp_i2c_send(RTC_ADDR
, RTC_CTRL2
, tmp
);
211 rtc_lock_alarm_clear
= false;
217 bool rtc_check_alarm_flag(void)
222 /* read Control 2 register which contains alarm flag */
223 rv
= i2c_readbytes(RTC_ADDR
, RTC_CTRL2
, 1, &tmp
);
225 return (tmp
& RTC_AF
);