diff options
-rw-r--r-- | firmware/drivers/rtc/rtc_e8564.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/firmware/drivers/rtc/rtc_e8564.c b/firmware/drivers/rtc/rtc_e8564.c index 0e81ee276d..322b04b20b 100644 --- a/firmware/drivers/rtc/rtc_e8564.c +++ b/firmware/drivers/rtc/rtc_e8564.c | |||
@@ -48,6 +48,8 @@ | |||
48 | /* Timer register flags */ | 48 | /* Timer register flags */ |
49 | #define RTC_TE 0x80 | 49 | #define RTC_TE 0x80 |
50 | 50 | ||
51 | bool rtc_lock_alarm_clear=true; | ||
52 | |||
51 | void rtc_init(void) | 53 | void rtc_init(void) |
52 | { | 54 | { |
53 | unsigned char tmp; | 55 | unsigned char tmp; |
@@ -59,8 +61,8 @@ void rtc_init(void) | |||
59 | 61 | ||
60 | /* read value of the Control 2 register - we'll need it to preserve alarm and timer interrupt assertion flags */ | 62 | /* read value of the Control 2 register - we'll need it to preserve alarm and timer interrupt assertion flags */ |
61 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); | 63 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); |
62 | /* clear alarm and timer interrupts */ | 64 | /* preserve alarm and timer interrupt flags */ |
63 | tmp &= (RTC_TF | RTC_AF); | 65 | tmp &= (RTC_TF | RTC_AF | RTC_TIE | RTC_AIE); |
64 | pp_i2c_send(0x51, RTC_CTRL2,tmp); | 66 | pp_i2c_send(0x51, RTC_CTRL2,tmp); |
65 | } | 67 | } |
66 | 68 | ||
@@ -149,19 +151,23 @@ bool rtc_enable_alarm(bool enable) | |||
149 | { | 151 | { |
150 | unsigned char tmp=0; | 152 | unsigned char tmp=0; |
151 | int rv=0; | 153 | int rv=0; |
152 | 154 | ||
153 | if(enable) | 155 | if(enable) |
154 | { | 156 | { |
155 | /* enable alarm interrupt */ | 157 | /* enable alarm interrupt */ |
156 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); | 158 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); |
157 | tmp |= RTC_AIE; | 159 | tmp |= RTC_AIE; |
160 | tmp &= ~RTC_AF; | ||
158 | pp_i2c_send(0x51, RTC_CTRL2,tmp); | 161 | pp_i2c_send(0x51, RTC_CTRL2,tmp); |
159 | } | 162 | } |
160 | else | 163 | else |
161 | { | 164 | { |
162 | /* disable alarm interrupt */ | 165 | /* disable alarm interrupt */ |
166 | if(rtc_lock_alarm_clear) | ||
167 | /* lock disabling alarm before it was checked whether or not the unit was started by RTC alarm */ | ||
168 | return false; | ||
163 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); | 169 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); |
164 | tmp &= ~RTC_AIE; | 170 | tmp &= ~(RTC_AIE | RTC_AF); |
165 | pp_i2c_send(0x51, RTC_CTRL2,tmp); | 171 | pp_i2c_send(0x51, RTC_CTRL2,tmp); |
166 | } | 172 | } |
167 | 173 | ||
@@ -185,9 +191,9 @@ bool rtc_check_alarm_started(bool release_alarm) | |||
185 | /* read Control 2 register which contains alarm flag */ | 191 | /* read Control 2 register which contains alarm flag */ |
186 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); | 192 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); |
187 | 193 | ||
188 | alarm_state = started = tmp & (RTC_AF | RTC_AIE); | 194 | alarm_state = started = ( (tmp & RTC_AF) && (tmp & RTC_AIE) ); |
189 | 195 | ||
190 | if(release_alarm) | 196 | if(release_alarm && started) |
191 | { | 197 | { |
192 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); | 198 | rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp); |
193 | /* clear alarm interrupt enable and alarm flag */ | 199 | /* clear alarm interrupt enable and alarm flag */ |
@@ -195,6 +201,7 @@ bool rtc_check_alarm_started(bool release_alarm) | |||
195 | pp_i2c_send(0x51, RTC_CTRL2,tmp); | 201 | pp_i2c_send(0x51, RTC_CTRL2,tmp); |
196 | } | 202 | } |
197 | run_before = true; | 203 | run_before = true; |
204 | rtc_lock_alarm_clear = false; | ||
198 | } | 205 | } |
199 | 206 | ||
200 | return started; | 207 | return started; |