Rev 196 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
196 | killagreg | 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** |
1 | ingob | 2 | * File Name : 91x_rtc.c |
3 | * Author : MCD Application Team |
||
196 | killagreg | 4 | * Version : V2.1 |
5 | * Date : 12/22/2008 |
||
6 | * Description : This file provides the RTC library firmware functions |
||
1 | ingob | 7 | ******************************************************************************** |
8 | * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH |
||
9 | * CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS |
||
10 | * A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT |
||
11 | * OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT |
||
12 | * OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION |
||
13 | * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
||
14 | *******************************************************************************/ |
||
15 | |||
16 | /* Includes ------------------------------------------------------------------*/ |
||
17 | #include "91x_rtc.h" |
||
18 | #include "91x_scu.h" |
||
19 | |||
20 | /* Include of other module interface headers ---------------------------------*/ |
||
21 | /* Local includes ------------------------------------------------------------*/ |
||
22 | /* Private typedef -----------------------------------------------------------*/ |
||
23 | /* Private define ------------------------------------------------------------*/ |
||
24 | /* Private macro -------------------------------------------------------------*/ |
||
25 | /* Private variables ---------------------------------------------------------*/ |
||
26 | /* Private function prototypes -----------------------------------------------*/ |
||
27 | u8 BYTEToBCD2(u8 value); |
||
28 | u16 WORDToBCD3(u16 value); |
||
29 | u8 BCD2ToBYTE(u8 value); |
||
30 | u16 BCD3ToBYTE(u16 value); |
||
31 | /* Interface functions -------------------------------------------------------*/ |
||
32 | /* Private functions ---------------------------------------------------------*/ |
||
33 | |||
34 | /******************************************************************************* |
||
35 | * Function Name : BYTEToBCD2 |
||
36 | * Description : Converts a 2 digit decimal to BCD format |
||
37 | * Input : None |
||
38 | * Output : None |
||
39 | * Return : Converted byte |
||
40 | *******************************************************************************/ |
||
41 | u8 BYTEToBCD2(u8 value) |
||
42 | { |
||
43 | u8 bcdhigh = 0; |
||
44 | while (value >= 10) |
||
45 | { |
||
46 | bcdhigh++; |
||
47 | value -= 10; |
||
48 | } |
||
49 | return (bcdhigh << 4) | value; |
||
50 | } |
||
51 | /******************************************************************************* |
||
52 | * Function Name : WORDToBCD3 |
||
53 | * Description : Converts a 3 digit decimal to BCD format |
||
54 | * Input : None |
||
55 | * Output : None |
||
56 | * Return : Converted word |
||
57 | *******************************************************************************/ |
||
58 | u16 WORDToBCD3(u16 value) |
||
59 | { |
||
60 | u16 bcdhigh = 0; |
||
61 | while (value >= 100) |
||
62 | { |
||
63 | bcdhigh++; |
||
64 | value -= 100; |
||
65 | } |
||
66 | bcdhigh <<= 4; |
||
67 | while (value >= 10) |
||
68 | { |
||
69 | bcdhigh++; |
||
70 | value -= 10; |
||
71 | } |
||
72 | return (bcdhigh << 4) | value; |
||
73 | } |
||
74 | |||
75 | /******************************************************************************* |
||
76 | * Function Name : BCD3ToWORD |
||
77 | * Description : convert from 3 digit BCD to Binary |
||
78 | * Input : None |
||
79 | * Output : None |
||
80 | * Return : Converted word |
||
81 | *******************************************************************************/ |
||
82 | u16 BCD3ToWORD(u16 value) |
||
83 | { |
||
84 | return (u16)((((value&0xF00)>>8)*100) + (((value&0x0F0)>>4)*10) + (value&0x0F)); |
||
85 | } |
||
86 | |||
87 | /******************************************************************************* |
||
88 | * Function Name : BCD2ToBYTE |
||
89 | * Description : convert from 2 digit BCD to Binary |
||
90 | * Input : None |
||
91 | * Output : None |
||
92 | * Return : Converted word |
||
93 | *******************************************************************************/ |
||
94 | u8 BCD2ToBYTE(u8 value) |
||
95 | { |
||
96 | u32 tmp; |
||
97 | tmp= ((value&0xF0)>>4)*10; |
||
98 | return (u8)(tmp+ (value&0x0F)); |
||
99 | } |
||
100 | |||
101 | /******************************************************************************* |
||
102 | * Function Name : RTC_DeInit |
||
103 | * Description : Resets the RTC peripheral registers |
||
104 | * Input : None |
||
105 | * Output : None |
||
106 | * Return : None |
||
107 | *******************************************************************************/ |
||
108 | void RTC_DeInit(void) |
||
109 | { |
||
110 | SCU_APBPeriphReset(__RTC,ENABLE); |
||
111 | SCU_APBPeriphReset(__RTC,DISABLE); |
||
112 | } |
||
113 | |||
114 | /******************************************************************************* |
||
115 | * Function Name : RTC_SetDate |
||
116 | * Description : Sets the Date register |
||
117 | * Input : struct of type RTC_DATE |
||
118 | * Output : None |
||
119 | * Return : None |
||
120 | *******************************************************************************/ |
||
121 | void RTC_SetDate(RTC_DATE Date) |
||
122 | { |
||
123 | u32 tmp = 0; |
||
124 | |||
125 | RTC->CR |=0x80; /*Enable write operation in DTR register*/ |
||
126 | RTC->DTR = 0; |
||
127 | tmp = BYTEToBCD2(Date.century); |
||
128 | RTC->DTR|=tmp<<24; |
||
129 | tmp = BYTEToBCD2(Date.year); |
||
130 | RTC->DTR|=tmp<<16; |
||
131 | tmp = BYTEToBCD2(Date.month); |
||
132 | RTC->DTR|=tmp<<8; |
||
133 | tmp = BYTEToBCD2(Date.weekday); |
||
134 | RTC->DTR|=tmp; |
||
135 | RTC->TR &=0xFFFFFF; |
||
136 | tmp = BYTEToBCD2(Date.day); |
||
137 | RTC->TR|=tmp<<24; |
||
138 | RTC->CR &=~0x80; /*Disable write operation in DTR register*/ |
||
139 | } |
||
140 | /******************************************************************************* |
||
141 | * Function Name : RTC_SetTime |
||
142 | * Description : Sets the Time register |
||
143 | * Input : struct of type RTC_TIME |
||
144 | * Output : None |
||
145 | * Return : None |
||
146 | *******************************************************************************/ |
||
147 | void RTC_SetTime(RTC_TIME Time) |
||
148 | { |
||
149 | u32 tmp = 0; |
||
150 | |||
151 | RTC->CR |=0x80; /*Enable write operation in TR register*/ |
||
152 | RTC->TR &= 0xFF000000; |
||
153 | tmp = BYTEToBCD2(Time.hours); |
||
154 | RTC->TR|=tmp<<16; |
||
155 | tmp = BYTEToBCD2(Time.minutes); |
||
156 | RTC->TR|=tmp<<8; |
||
157 | tmp = BYTEToBCD2(Time.seconds); |
||
158 | RTC->TR|=tmp; |
||
159 | RTC->MILR = 0; |
||
160 | RTC->MILR |= WORDToBCD3(Time.milliseconds); |
||
161 | RTC->CR &=~0x80; /*Disable write operation in TR register*/ |
||
162 | } |
||
163 | /******************************************************************************* |
||
164 | * Function Name : RTC_SetAlarm |
||
165 | * Description : Sets the Alarm register |
||
166 | * Input : Struct of type RTC_ALARM |
||
167 | * Output : Date |
||
168 | * Return : None |
||
169 | *******************************************************************************/ |
||
170 | void RTC_SetAlarm(RTC_ALARM Alarm) |
||
171 | { |
||
172 | u32 tmp = 0; |
||
173 | |||
174 | RTC->CR |=0x80; /*Enable write operation in ATR register*/ |
||
175 | RTC->ATR = 0; |
||
176 | tmp = BYTEToBCD2(Alarm.day); |
||
177 | RTC->ATR|=tmp<<24; |
||
178 | tmp = BYTEToBCD2(Alarm.hours); |
||
179 | RTC->ATR|=tmp<<16; |
||
180 | tmp = BYTEToBCD2(Alarm.minutes); |
||
181 | RTC->ATR|=tmp<<8; |
||
182 | tmp = BYTEToBCD2(Alarm.seconds); |
||
183 | RTC->ATR|=tmp; |
||
184 | RTC->CR &=~0x80; /*Disable write operation in ATR register*/ |
||
185 | } |
||
186 | |||
187 | /******************************************************************************* |
||
188 | * Function Name : RTC_GetDate |
||
189 | * Description : Gets RTC date in BCD coded or BINARY code |
||
190 | * Input : -Format: BCD or BINARY |
||
191 | * -Date: pointer to structure of type RTC_DATE to be filled by function |
||
192 | * Output : None |
||
193 | * Return : None |
||
194 | *******************************************************************************/ |
||
195 | void RTC_GetDate(u8 Format, RTC_DATE * Date) |
||
196 | { |
||
197 | Date->century = (u8)((RTC->DTR&0xFF000000)>>24); |
||
198 | Date->year = (u8)((RTC->DTR&0x00FF0000)>>16); |
||
199 | Date->month = (u8)((RTC->DTR&0x00001F00)>>8); |
||
200 | Date->day = (u8)((RTC->TR&0x3F000000)>>24); |
||
201 | Date->weekday = (u8)(RTC->DTR&0xF); |
||
202 | if (Format == BINARY) |
||
203 | { |
||
204 | Date->century = BCD2ToBYTE(Date->century); |
||
205 | Date->year = BCD2ToBYTE(Date->year); |
||
206 | Date->month = BCD2ToBYTE(Date->month); |
||
207 | Date->day = BCD2ToBYTE(Date->day); |
||
208 | Date->weekday = BCD2ToBYTE(Date->weekday); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /******************************************************************************* |
||
213 | * Function Name : RTC_GetTime |
||
214 | * Description : Gets TIME in BCD coded or BINARY code |
||
215 | * Input : -Format: BCD or BINARY |
||
216 | * -Time : pointer to structure of type RTC_TIME to be filled by function |
||
217 | * Output : Time |
||
218 | * Return : None |
||
219 | *******************************************************************************/ |
||
220 | void RTC_GetTime(u8 Format, RTC_TIME * Time) |
||
221 | { |
||
222 | |||
223 | Time->hours = (u8)((RTC->TR&0x003F0000)>>16); |
||
224 | Time->minutes = (u8)((RTC->TR&0x00007F00)>>8); |
||
225 | Time->seconds = (u8)(RTC->TR&0x7F); |
||
226 | Time->milliseconds =(u16)(RTC->MILR&0xFFF); |
||
227 | if (Format == BINARY) |
||
228 | { |
||
229 | Time->hours = BCD2ToBYTE(Time->hours); |
||
230 | Time->minutes = BCD2ToBYTE(Time->minutes); |
||
231 | Time->seconds = BCD2ToBYTE(Time->seconds); |
||
232 | Time->milliseconds = BCD3ToWORD(Time->milliseconds); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | |||
237 | /******************************************************************************* |
||
238 | * Function Name : RTC_GetAlarm |
||
239 | * Description : Gets the RTC Alarm in BCD or BINARY code |
||
240 | * Input : -Format: BCD or BINARY |
||
241 | * -Alarm : pointer to structure of type RTC_ALARM to be filled by function |
||
242 | * Output : Alarm |
||
243 | * Return : None |
||
244 | *******************************************************************************/ |
||
245 | void RTC_GetAlarm(u8 Format,RTC_ALARM * Alarm) |
||
246 | { |
||
247 | Alarm->day = (u8)((RTC->ATR&0x3F000000)>>24); |
||
248 | Alarm->hours = (u8)((RTC->ATR&0x003F0000)>>16); |
||
249 | Alarm->minutes = (u8)((RTC->ATR&0x00007F00)>>8); |
||
250 | Alarm->seconds = (u8)((RTC->ATR)&0x7F); |
||
251 | if (Format == BINARY) |
||
252 | { |
||
253 | Alarm->day = BCD2ToBYTE(Alarm->day); |
||
254 | Alarm->hours = BCD2ToBYTE(Alarm->hours); |
||
255 | Alarm->minutes = BCD2ToBYTE(Alarm->minutes); |
||
256 | Alarm->seconds = BCD2ToBYTE(Alarm->seconds); |
||
257 | } |
||
258 | } |
||
259 | |||
260 | /******************************************************************************* |
||
261 | * Function Name : RTC_TamperConfig |
||
262 | * Description : configures the Tamper mode and tamper polarity |
||
263 | * Input : -TamperMode: RTC_TamperMode_Edge or RTC_TamperMode_Level |
||
264 | * -TamperPol : RTC_TamperPol_Low or RTC_TamperMode_High |
||
265 | * Output : None |
||
266 | * Return : None |
||
267 | *******************************************************************************/ |
||
268 | void RTC_TamperConfig(u32 TamperMode, u32 TamperPol) |
||
269 | { |
||
270 | RTC->CR&=RTC_TamperMode_Edge; |
||
271 | if (TamperMode!=RTC_TamperMode_Edge) |
||
272 | RTC->CR|=RTC_TamperMode_Level; |
||
273 | |||
274 | RTC->CR&=RTC_TamperPol_Low; |
||
275 | if (TamperPol!=RTC_TamperPol_Low) |
||
276 | RTC->CR|=RTC_TamperPol_High; |
||
277 | } |
||
278 | |||
279 | /******************************************************************************* |
||
280 | * Function Name : RTC_TamperCmd |
||
281 | * Description : Enable or Disable Tamper |
||
282 | * Input : NewState: ENABLE or DISABLE |
||
283 | * Output : None |
||
284 | * Return : None |
||
285 | *******************************************************************************/ |
||
286 | void RTC_TamperCmd(FunctionalState NewState) |
||
287 | { |
||
288 | RTC->CR&=0xFFFFFFFE; |
||
289 | if (NewState==ENABLE) |
||
290 | RTC->CR|=0x1; |
||
291 | } |
||
292 | |||
293 | /******************************************************************************* |
||
294 | * Function Name : RTC_AlarmCmd |
||
295 | * Description : Enable or Disable Alarm |
||
296 | * Input : NewState: ENABLE or DISABLE |
||
297 | * Output : None |
||
298 | * Return : None |
||
299 | *******************************************************************************/ |
||
300 | void RTC_AlarmCmd(FunctionalState NewState) |
||
301 | { |
||
302 | RTC->CR&=~0x100000; |
||
303 | if (NewState==ENABLE) |
||
304 | RTC->CR|=0x100000; |
||
305 | } |
||
306 | |||
307 | /******************************************************************************* |
||
308 | * Function Name : RTC_CalibClockCmd |
||
309 | * Description : Enable or Disable RTC Calibration Clock Output |
||
310 | * Input : NewState: ENABLE or DISABLE |
||
311 | * Output : None |
||
312 | * Return : None |
||
313 | *******************************************************************************/ |
||
314 | void RTC_CalibClockCmd(FunctionalState NewState) |
||
315 | { |
||
316 | RTC->CR&=~0x40; |
||
317 | if (NewState ==ENABLE) |
||
318 | RTC->CR|=0x40; |
||
319 | } |
||
320 | |||
321 | /******************************************************************************* |
||
322 | * Function Name : SRAMBattPowerCmd |
||
323 | * Description : Enable or Disable SRAM backup Power by VBATT |
||
324 | * Input : NewState : ENABLE or DISABLE |
||
325 | * Output : None |
||
326 | * Return : None |
||
327 | *******************************************************************************/ |
||
328 | void RTC_SRAMBattPowerCmd(FunctionalState NewState) |
||
329 | { |
||
330 | RTC->CR&=~0x8; |
||
331 | if (NewState ==ENABLE) |
||
332 | RTC->CR|=0x8; |
||
333 | } |
||
334 | |||
335 | /******************************************************************************* |
||
336 | * Function Name : RTC_PeridicIntConfig |
||
337 | * Description : Select a Periodic CLock |
||
338 | * Input : PeriodicClock |
||
339 | * Output : None |
||
340 | * Return : None |
||
341 | * Note : When PeriodicClock = RTC_Per_DISABLE the Periodic clock generation |
||
342 | * will be disabled. |
||
343 | *******************************************************************************/ |
||
344 | void RTC_PeriodicIntConfig(u32 PeriodicClock) |
||
345 | { |
||
346 | RTC->CR &=~0xF0000; |
||
347 | RTC->CR|=PeriodicClock; |
||
348 | } |
||
349 | |||
350 | /******************************************************************************* |
||
351 | * Function Name : RTC_ITConfig |
||
352 | * Description : Enable or Disable an interrupt |
||
353 | * Input : -RTC_IT : RTC interrupt |
||
354 | * -Newstate: Enable or Disable |
||
355 | * Output : None |
||
356 | * Return : None |
||
357 | *******************************************************************************/ |
||
358 | void RTC_ITConfig(u32 RTC_IT, FunctionalState NewState) |
||
359 | { |
||
360 | RTC->CR&=~RTC_IT; |
||
361 | if (NewState==ENABLE) |
||
362 | RTC->CR|=RTC_IT; |
||
363 | } |
||
364 | |||
365 | /******************************************************************************* |
||
366 | * Function Name : RTC_GetFlagStatus |
||
367 | * Description : Gets a RTC flag status |
||
368 | * Input : RTC_FLAG |
||
369 | * Output : None |
||
370 | * Return : FlagStatus :SET or RESET |
||
371 | *******************************************************************************/ |
||
372 | FlagStatus RTC_GetFlagStatus(u32 RTC_FLAG) |
||
373 | { |
||
374 | if (RTC->SR&RTC_FLAG) return SET; |
||
375 | else return RESET; |
||
376 | } |
||
377 | |||
378 | /******************************************************************************* |
||
379 | * Function Name : RTC_ClearFlag |
||
380 | * Description : Clears a RTC flag |
||
381 | * Input : RTC_FLAG |
||
382 | * Output : None |
||
383 | * Return : None |
||
384 | * Note : Before clearing the RTC Periodic Flag you need to disable the |
||
385 | * Periodic interrupt generation, to do this use function |
||
386 | * RTC_PeriodicIntConfig(RTC_Per_DISABLE) |
||
387 | *******************************************************************************/ |
||
388 | void RTC_ClearFlag(u32 RTC_FLAG) |
||
389 | { |
||
390 | vu32 tmp=0; |
||
391 | if (RTC_FLAG == RTC_FLAG_Per) tmp=RTC->SR; |
||
392 | else if (RTC_FLAG == RTC_FLAG_Alarm) RTC->CR&=~0x100000; |
||
393 | else if (RTC_FLAG == RTC_FLAG_Tamper) RTC->CR&=~0x1; |
||
394 | } |
||
395 | |||
396 | |||
196 | killagreg | 397 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ |