diff options
Diffstat (limited to 'firmware/drivers/rtc')
-rw-r--r-- | firmware/drivers/rtc/rtc_e8564.c | 138 |
1 files changed, 133 insertions, 5 deletions
diff --git a/firmware/drivers/rtc/rtc_e8564.c b/firmware/drivers/rtc/rtc_e8564.c index c3ae9349f4..0e81ee276d 100644 --- a/firmware/drivers/rtc/rtc_e8564.c +++ b/firmware/drivers/rtc/rtc_e8564.c | |||
@@ -7,7 +7,8 @@ | |||
7 | * \/ \/ \/ \/ \/ | 7 | * \/ \/ \/ \/ \/ |
8 | * $Id$ | 8 | * $Id$ |
9 | * | 9 | * |
10 | * Copyright (C) 2002 by Linus Nielsen Feltzing, Uwe Freese, Laurent Baum | 10 | * Copyright (C) 2002 by Linus Nielsen Feltzing, Uwe Freese, Laurent Baum, |
11 | * Przemyslaw Holubowski | ||
11 | * | 12 | * |
12 | * All files in this archive are subject to the GNU General Public License. | 13 | * All files in this archive are subject to the GNU General Public License. |
13 | * See the file COPYING in the source tree root for full license agreement. | 14 | * See the file COPYING in the source tree root for full license agreement. |
@@ -25,12 +26,27 @@ | |||
25 | #include <stdbool.h> | 26 | #include <stdbool.h> |
26 | 27 | ||
27 | /* RTC registers */ | 28 | /* RTC registers */ |
28 | #define RTC_CTRL1 0x00 | 29 | #define RTC_CTRL1 0x00 |
29 | #define RTC_CTRL2 0x01 | 30 | #define RTC_CTRL2 0x01 |
31 | #define RTC_ALARM_MINUTES 0x09 | ||
32 | #define RTC_ALARM_HOURS 0x0A | ||
33 | #define RTC_ALARM_DAY 0x0B | ||
34 | #define RTC_ALARM_WDAY 0x0C | ||
35 | #define RTC_TIMER_CTRL 0x0E | ||
36 | #define RTC_TIMER 0x0F | ||
30 | 37 | ||
31 | /* Control 2 register flags */ | 38 | /* Control 2 register flags */ |
32 | #define RTC_TF 0x04 | 39 | #define RTC_TIE 0x01 |
33 | #define RTC_AF 0x08 | 40 | #define RTC_AIE 0x02 |
41 | #define RTC_TF 0x04 | ||
42 | #define RTC_AF 0x08 | ||
43 | #define RTC_TI_TP 0x10 | ||
44 | |||
45 | /* Alarm registers flags */ | ||
46 | #define RTC_AE 0x80 | ||
47 | |||
48 | /* Timer register flags */ | ||
49 | #define RTC_TE 0x80 | ||
34 | 50 | ||
35 | void rtc_init(void) | 51 | void rtc_init(void) |
36 | { | 52 | { |
@@ -82,3 +98,115 @@ int rtc_write_datetime(unsigned char* buf) | |||
82 | return 1; | 98 | return 1; |
83 | } | 99 | } |
84 | 100 | ||
101 | void rtc_set_alarm(int h, int m) | ||
102 | { | ||
103 | unsigned char buf[4]={0}; | ||
104 | int rv=0; | ||
105 | int i=0; | ||
106 | |||
107 | /* clear alarm interrupt */ | ||
108 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,buf); | ||
109 | buf[0] &= RTC_AF; | ||
110 | pp_i2c_send(0x51, RTC_CTRL2,buf[0]); | ||
111 | |||
112 | /* prepare new alarm */ | ||
113 | if( m >= 0 ) | ||
114 | buf[0] = (((m/10) << 4) | m%10); | ||
115 | else | ||
116 | /* ignore minutes comparison query */ | ||
117 | buf[0] = RTC_AE; | ||
118 | |||
119 | if( h >= 0 ) | ||
120 | buf[1] = (((h/10) << 4) | h%10); | ||
121 | else | ||
122 | /* ignore hours comparison query */ | ||
123 | buf[1] = RTC_AE; | ||
124 | |||
125 | /* ignore day and wday */ | ||
126 | buf[2] = RTC_AE; | ||
127 | buf[3] = RTC_AE; | ||
128 | |||
129 | /* write new alarm */ | ||
130 | for(;i<4;i++) | ||
131 | pp_i2c_send(0x51, RTC_ALARM_MINUTES+i,buf[i]); | ||
132 | |||
133 | /* note: alarm is not enabled at the point */ | ||
134 | } | ||
135 | |||
136 | void rtc_get_alarm(int *h, int *m) | ||
137 | { | ||
138 | unsigned char buf[4]={0}; | ||
139 | |||
140 | /* get alarm preset */ | ||
141 | i2c_readbytes(0x51, RTC_ALARM_MINUTES,4,buf); | ||
142 | |||
143 | *m = ((buf[0] >> 4) & 0x7)*10 + (buf[0] & 0x0f); | ||
144 | *h = ((buf[1] >> 4) & 0x3)*10 + (buf[1] & 0x0f); | ||
145 | |||
146 | } | ||
147 | |||
148 | bool rtc_enable_alarm(bool enable) | ||
149 | { | ||
150 | unsigned char tmp=0; | ||
151 | int rv=0; | ||
152 | |||
153 | if(enable) | ||
154 | { | ||
155 | /* enable alarm interrupt */ | ||
156 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); | ||
157 | tmp |= RTC_AIE; | ||
158 | pp_i2c_send(0x51, RTC_CTRL2,tmp); | ||
159 | } | ||
160 | else | ||
161 | { | ||
162 | /* disable alarm interrupt */ | ||
163 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); | ||
164 | tmp &= ~RTC_AIE; | ||
165 | pp_i2c_send(0x51, RTC_CTRL2,tmp); | ||
166 | } | ||
167 | |||
168 | return false; | ||
169 | } | ||
170 | |||
171 | bool rtc_check_alarm_started(bool release_alarm) | ||
172 | { | ||
173 | static bool alarm_state, run_before = false; | ||
174 | unsigned char tmp=0; | ||
175 | bool started; | ||
176 | int rv=0; | ||
177 | |||
178 | if (run_before) | ||
179 | { | ||
180 | started = alarm_state; | ||
181 | alarm_state &= ~release_alarm; | ||
182 | } | ||
183 | else | ||
184 | { | ||
185 | /* read Control 2 register which contains alarm flag */ | ||
186 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); | ||
187 | |||
188 | alarm_state = started = tmp & (RTC_AF | RTC_AIE); | ||
189 | |||
190 | if(release_alarm) | ||
191 | { | ||
192 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); | ||
193 | /* clear alarm interrupt enable and alarm flag */ | ||
194 | tmp &= ~(RTC_AF | RTC_AIE); | ||
195 | pp_i2c_send(0x51, RTC_CTRL2,tmp); | ||
196 | } | ||
197 | run_before = true; | ||
198 | } | ||
199 | |||
200 | return started; | ||
201 | } | ||
202 | |||
203 | bool rtc_check_alarm_flag(void) | ||
204 | { | ||
205 | unsigned char tmp=0; | ||
206 | int rv=0; | ||
207 | |||
208 | /* read Control 2 register which contains alarm flag */ | ||
209 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); | ||
210 | |||
211 | return (tmp & RTC_AF); | ||
212 | } | ||