FreeCalypso > hg > freecalypso-citrine
comparison bsp/rtc/rtc_functions.c @ 0:75a11d740a02
initial import of gsm-fw from freecalypso-sw rev 1033:5ab737ac3ad7
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 09 Jun 2016 00:02:41 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:75a11d740a02 |
---|---|
1 /******************************************************************************/ | |
2 /* rtc_functions.c : contains low level function for the rtc */ | |
3 /* */ | |
4 /* */ | |
5 /* Author: Laurent Sollier */ | |
6 /* */ | |
7 /* version: 1.0 */ | |
8 /* */ | |
9 /* Date: 03/20/01 */ | |
10 /******************************************************************************/ | |
11 | |
12 #include "../../include/config.h" | |
13 | |
14 #include <string.h> /* needed for memset */ | |
15 | |
16 #include "rtc_messages_i.h" | |
17 #include "rtc_api.h" | |
18 #include "rtc_i.h" | |
19 #include "rtc_config.h" | |
20 | |
21 #include "../../riviera/rvm/rvm_use_id_list.h" | |
22 | |
23 #include "../mem.h" | |
24 #include "../iq.h" | |
25 #include "../ulpd.h" | |
26 #include "../inth.h" | |
27 | |
28 #include "../../nucleus/nucleus.h" | |
29 | |
30 /* ----- Macro ----- */ | |
31 /*-------------------------------------------------------------*/ | |
32 /* RTC_STOP */ | |
33 /*-------------------------------------------------------------*/ | |
34 /* Parameters : none */ | |
35 /* Return : none */ | |
36 /* Functionality : Stop the RTC (STOP_RTC is cleared) */ | |
37 /*-------------------------------------------------------------*/ | |
38 #define RTC_STOP (* (volatile UINT8 *) RTC_CTRL_REG &= ~RTC_START_RTC) | |
39 | |
40 /*-------------------------------------------------------------*/ | |
41 /* RTC_START */ | |
42 /*-------------------------------------------------------------*/ | |
43 /* Parameters : none */ | |
44 /* Return : none */ | |
45 /* Functionality : Start the RTC */ | |
46 /*-------------------------------------------------------------*/ | |
47 #define RTC_START (* (volatile UINT8 *) RTC_CTRL_REG |= RTC_START_RTC) | |
48 | |
49 /*-------------------------------------------------------------*/ | |
50 /* RTC_SET_32_CNT */ | |
51 /*-------------------------------------------------------------*/ | |
52 /* Parameters : none */ | |
53 /* Return : none */ | |
54 /* Functionality : Set the SET_32_COUNTER bit */ | |
55 /*-------------------------------------------------------------*/ | |
56 #define RTC_SET_32_CNT (* (volatile UINT8 *) RTC_CTRL_REG |= RTC_SET_32_COUNTER) | |
57 | |
58 /*-------------------------------------------------------------*/ | |
59 /* RTC_RESET_32_CNT */ | |
60 /*-------------------------------------------------------------*/ | |
61 /* Parameters : none */ | |
62 /* Return : none */ | |
63 /* Functionality : Reset the SET_32_COUNTER bit */ | |
64 /*-------------------------------------------------------------*/ | |
65 #define RTC_RESET_32_CNT (* (volatile UINT8 *) RTC_CTRL_REG &= ~RTC_SET_32_COUNTER) | |
66 | |
67 /*-------------------------------------------------------------*/ | |
68 /* RTC_SET_TEST_MODE */ | |
69 /*-------------------------------------------------------------*/ | |
70 /* Parameters : none */ | |
71 /* Return : none */ | |
72 /* Functionality : Set the TEST_MODE bit */ | |
73 /*-------------------------------------------------------------*/ | |
74 #define RTC_SET_TEST_MODE (* (volatile UINT8 *) RTC_CTRL_REG |= RTC_TEST_MODE) | |
75 | |
76 /*-------------------------------------------------------------*/ | |
77 /* RTC_ENABLE_IT_ALARM */ | |
78 /*-------------------------------------------------------------*/ | |
79 /* Parameters : none */ | |
80 /* Return : none */ | |
81 /* Functionality : Enable the IT_ALARM */ | |
82 /*-------------------------------------------------------------*/ | |
83 #define RTC_ENABLE_IT_ALARM (* (volatile UINT8 *) RTC_INTERRUPTS_REG |= RTC_IT_ALARM) | |
84 | |
85 /*-------------------------------------------------------------*/ | |
86 /* RTC_DISABLE_IT_ALARM */ | |
87 /*-------------------------------------------------------------*/ | |
88 /* Parameters : none */ | |
89 /* Return : none */ | |
90 /* Functionality : Disable the IT_ALARM */ | |
91 /*-------------------------------------------------------------*/ | |
92 #define RTC_DISABLE_IT_ALARM (* (volatile UINT8 *) RTC_INTERRUPTS_REG &= ~RTC_IT_ALARM) | |
93 | |
94 /*-------------------------------------------------------------*/ | |
95 /* RTC_ENABLE_IT_TIMER */ | |
96 /*-------------------------------------------------------------*/ | |
97 /* Parameters : none */ | |
98 /* Return : none */ | |
99 /* Functionality : Enable the IT_TIMER */ | |
100 /*-------------------------------------------------------------*/ | |
101 #define RTC_ENABLE_IT_TIMER (* (volatile UINT8 *) RTC_INTERRUPTS_REG |= RTC_IT_TIMER) | |
102 | |
103 /*-------------------------------------------------------------*/ | |
104 /* RTC_DISABLE_IT_TIMER */ | |
105 /*-------------------------------------------------------------*/ | |
106 /* Parameters : none */ | |
107 /* Return : none */ | |
108 /* Functionality : Disable the IT_TIMER */ | |
109 /*-------------------------------------------------------------*/ | |
110 #define RTC_DISABLE_IT_TIMER (* (volatile UINT8 *) RTC_INTERRUPTS_REG &= ~RTC_IT_TIMER) | |
111 | |
112 /*-------------------------------------------------------------*/ | |
113 /* RTC_SET_EVENT_TIMER */ | |
114 /*-------------------------------------------------------------*/ | |
115 /* Parameters : event to enable or disable */ | |
116 /* Return : none */ | |
117 /* Functionality : Set the event for the IT_TIMER */ | |
118 /* Enable or disable IT */ | |
119 /*-------------------------------------------------------------*/ | |
120 #define RTC_SET_EVENT_TIMER(x) (* (volatile UINT8 *) RTC_INTERRUPTS_REG = (* (volatile UINT8 *) RTC_INTERRUPTS_REG & 0xfffc) | (x)) | |
121 | |
122 /*-------------------------------------------------------------*/ | |
123 /* RTC_TEST_BUSY */ | |
124 /*-------------------------------------------------------------*/ | |
125 /* Parameters : none */ | |
126 /* Return : none */ | |
127 /* Functionality : Return the RTC state busy signal */ | |
128 /*-------------------------------------------------------------*/ | |
129 #define RTC_TEST_BUSY ((* (volatile UINT8 *) RTC_STATUS_REG) & RTC_BUSY) | |
130 | |
131 /*-------------------------------------------------------------*/ | |
132 /* RTC_RUN_STATE */ | |
133 /*-------------------------------------------------------------*/ | |
134 /* Parameters : none */ | |
135 /* Return : none */ | |
136 /* Functionality : Return the RTC run state */ | |
137 /*-------------------------------------------------------------*/ | |
138 #define RTC_RUN_STATE ((* (volatile UINT8 *) RTC_STATUS_REG) & RTC_RUN) | |
139 | |
140 /*-------------------------------------------------------------*/ | |
141 /* RTC_FREE_AL_ITLINE */ | |
142 /*-------------------------------------------------------------*/ | |
143 /* Parameters : none */ | |
144 /* Return : none */ | |
145 /* Functionality : free it AL line */ | |
146 /*-------------------------------------------------------------*/ | |
147 | |
148 #define RTC_FREE_AL_ITLINE ((* (volatile UINT8 *) RTC_STATUS_REG) |= RTC_ALARM) | |
149 | |
150 /*-------------------------------------------------------------*/ | |
151 /* RTC_FREE_POWER_UP */ | |
152 /*-------------------------------------------------------------*/ | |
153 /* Parameters : none */ | |
154 /* Return : none */ | |
155 /* Functionality : clear reset event */ | |
156 /*-------------------------------------------------------------*/ | |
157 | |
158 #define RTC_FREE_POWER_UP ((* (volatile UINT8 *) RTC_STATUS_REG) |= RTC_POWER_UP) | |
159 | |
160 | |
161 /*-------------------------------------------------------------*/ | |
162 /* RTC_GET_EVENT_TIMER */ | |
163 /*-------------------------------------------------------------*/ | |
164 /* Parameters : none */ | |
165 /* Return : none */ | |
166 /* Functionality : Get the event status for the IT_TIMER */ | |
167 /*-------------------------------------------------------------*/ | |
168 #define RTC_GET_EVENT_TIMER ((* (volatile UINT8 *) RTC_STATUS_REG & 0x3c)) | |
169 | |
170 | |
171 | |
172 | |
173 /* ----- Global variables ----- */ | |
174 static T_RTC_DATE_TIME rtc_date_time_alarm = {0}; | |
175 | |
176 static T_RV_RETURN rtc_return_path = {RVF_INVALID_ADDR_ID, NULL}; | |
177 static T_RTC_ALARM* rtc_msg_alarm_event = NULL; | |
178 | |
179 extern T_RTC_ENV_CTRL_BLK* rtc_env_ctrl_blk; | |
180 | |
181 #if CONFIG_GSM | |
182 /* Number of 32 Khz clock */ | |
183 static UINT32 rtc_nb_32khz = 0; | |
184 | |
185 /* Number of high frequency clock */ | |
186 static UINT32 rtc_nb_hf = 0; | |
187 #endif | |
188 | |
189 static NU_HISR hisr; | |
190 char hisrStack[512]; | |
191 | |
192 // UINT16 toto = 0; tmp | |
193 | |
194 void HisrEntryFunc(void) | |
195 { | |
196 if (rtc_msg_alarm_event) | |
197 rvf_send_msg(rtc_env_ctrl_blk->addr_id, rtc_msg_alarm_event); | |
198 //char comp[15]; | |
199 /* sprintf(comp,"%d",toto); | |
200 LCDG_WriteString( 4, 0, " "); | |
201 LCDG_WriteString( 3, 0, (char*) comp); //tmp */ | |
202 } | |
203 | |
204 | |
205 /*---------------------------------------------------------------*/ | |
206 /* conv_bin DCB => B */ | |
207 /*---------------------------------------------------------------*/ | |
208 /* Parameters : value to be convert */ | |
209 /* Return : none */ | |
210 /* Functionality : BCD et binary conversion function */ | |
211 /*---------------------------------------------------------------*/ | |
212 | |
213 static UINT8 conv_bin(UINT8 value) | |
214 { | |
215 return (10*(value>>4) + ( value & 0x0f)); | |
216 } | |
217 | |
218 /*---------------------------------------------------------------*/ | |
219 /* conv_bcd B => DCB */ | |
220 /*---------------------------------------------------------------*/ | |
221 /* Parameters : value to be convert */ | |
222 /* Return : none */ | |
223 /* Functionality : Binary to BCD conversion function */ | |
224 /*---------------------------------------------------------------*/ | |
225 | |
226 static UINT8 conv_bcd(UINT8 value) | |
227 { | |
228 return ((value%10) | ( (value/10) <<4)); | |
229 } | |
230 | |
231 | |
232 | |
233 /*---------------------------------------------------------------*/ | |
234 /* format_date_available */ | |
235 /*---------------------------------------------------------------*/ | |
236 /* Parameters : T_RTC_DATE_TIME structure */ | |
237 /* Return : TRUE if format is available */ | |
238 /* FALSE else */ | |
239 /* Functionality : test if date and time format is available */ | |
240 /*---------------------------------------------------------------*/ | |
241 | |
242 BOOL format_date_available(T_RTC_DATE_TIME date_time) | |
243 { | |
244 UINT8 m; | |
245 | |
246 if (date_time.second < 0 || date_time.second > 59) | |
247 return FALSE; | |
248 if (date_time.minute < 0 || date_time.minute > 59) | |
249 return FALSE; | |
250 if (date_time.mode_12_hour == FALSE) | |
251 { | |
252 if (date_time.hour < 0 || date_time.hour > 23) | |
253 return FALSE; | |
254 } | |
255 else | |
256 if (date_time.hour < 1 || date_time.hour > 12) | |
257 return FALSE; | |
258 | |
259 if (date_time.month < 1 || date_time.month > 12) | |
260 return FALSE; | |
261 if (date_time.year < 0 || date_time.year > 99) | |
262 return FALSE; | |
263 if (date_time.wday < 0 || date_time.wday > 6) | |
264 return FALSE; | |
265 m = date_time.month; | |
266 if (m == 1||m == 3||m == 5||m == 7||m == 8||m == 10||m == 12) | |
267 { | |
268 if (date_time.day < 1 || date_time.day > 31) | |
269 return FALSE; | |
270 } | |
271 else | |
272 { | |
273 if (m == 4||m == 6||m == 9||m == 11) | |
274 { | |
275 if (date_time.day < 1 || date_time.day > 30) | |
276 return FALSE; | |
277 } | |
278 else | |
279 { | |
280 if (date_time.year%4) | |
281 { | |
282 if (date_time.day < 1 || date_time.day > 28) | |
283 return FALSE; | |
284 } | |
285 else | |
286 { | |
287 if (date_time.day < 1 || date_time.day > 29) | |
288 return FALSE; | |
289 } | |
290 } | |
291 } | |
292 return TRUE; | |
293 } | |
294 | |
295 | |
296 | |
297 /******************************************************************************* | |
298 * | |
299 * RTC_Initialize | |
300 * | |
301 ******************************************************************************/ | |
302 | |
303 | |
304 | |
305 T_RVF_RET RTC_Initialize(void) | |
306 { | |
307 T_RVF_MB_STATUS mb_status; | |
308 | |
309 /* Reserve memory for alarm event */ | |
310 mb_status = rvf_get_buf (rtc_env_ctrl_blk->prim_id, sizeof (T_RTC_ALARM ), (void **) &rtc_msg_alarm_event); | |
311 | |
312 if ((mb_status == RVF_GREEN) || (mb_status == RVF_YELLOW)) /* Memory allocation success */ | |
313 { | |
314 rtc_msg_alarm_event->os_hdr.msg_id = RTC_ALARM_EVT; | |
315 } | |
316 else | |
317 { | |
318 rtc_msg_alarm_event = NULL; | |
319 return RVF_MEMORY_ERR; | |
320 } | |
321 | |
322 rtc_env_ctrl_blk->msg_alarm_event = rtc_msg_alarm_event; | |
323 | |
324 | |
325 /* Start RTC module */ | |
326 if (RTC_RUN_STATE == 0) | |
327 RTC_START; | |
328 | |
329 /* Enable auto compensation */ | |
330 //*(volatile UINT8*) RTC_CTRL_REG |= RTC_AUTO_COMP; | |
331 /* Disable auto compensation */ | |
332 *(volatile UINT8*) RTC_CTRL_REG &= ~RTC_AUTO_COMP; | |
333 | |
334 /* For CHIPSET = 7, 9, 10 or 11, set analog baseband type */ | |
335 #if (((CHIPSET == 7) || (CHIPSET == 8) || (CHIPSET == 10) || (CHIPSET == 11)) && (ANALOG == 1)) | |
336 *(volatile UINT8*) RTC_CTRL_REG |= RTC_nDELTA_OMEGA; | |
337 #endif | |
338 | |
339 /* Activate periodic interrupt every minute*/ | |
340 /* Disable all IT before accessing register */ | |
341 rvf_disable(25); | |
342 | |
343 /* Enable IRQ9 and IRQ10 */ | |
344 #if (CHIPSET == 12) | |
345 F_INTH_ENABLE_ONE_IT(C_INTH_RTC_ALARM_IT); | |
346 #else | |
347 IQ_Unmask(IQ_RTC_ALARM); | |
348 #endif | |
349 | |
350 /* | |
351 * FreeCalypso change: the RTC periodic interrupt is used in | |
352 * conjunction with the ULPD gauging mechanism, and the latter | |
353 * appears to be controlled by some code in L1-land. | |
354 * Therefore, we are going to enable this interrupt only when | |
355 * building with L1 enabled. | |
356 */ | |
357 #if CONFIG_GSM | |
358 #if (CHIPSET == 12) | |
359 F_INTH_ENABLE_ONE_IT(C_INTH_RTC_TIMER_IT); | |
360 #else | |
361 IQ_Unmask(IQ_RTC_TIMER); | |
362 #endif | |
363 #endif | |
364 | |
365 while (RTC_TEST_BUSY); | |
366 RTC_SET_EVENT_TIMER(RTC_EVERY_MIN); /* Set timer every minute */ | |
367 #if CONFIG_GSM | |
368 RTC_ENABLE_IT_TIMER; | |
369 #else | |
370 RTC_DISABLE_IT_TIMER; | |
371 #endif | |
372 RTC_DISABLE_IT_ALARM; | |
373 | |
374 rvf_enable(); | |
375 | |
376 /* The stack is entirely filled with the pattern 0xFE. */ | |
377 memset (hisrStack, 0xFE, sizeof(hisrStack)); | |
378 | |
379 /* Temporary modification: create HISR to diplay compensation value */ | |
380 NU_Create_HISR(&hisr, "RTC_HISR", HisrEntryFunc, 2, hisrStack, sizeof(hisrStack)); // lowest prty | |
381 /* end temporary modification */ | |
382 | |
383 return RVF_OK; | |
384 } | |
385 /******************************************************************************* | |
386 * | |
387 * RTC_RtcReset | |
388 * | |
389 ******************************************************************************/ | |
390 | |
391 BOOL RTC_RtcReset(void) | |
392 { | |
393 | |
394 /* Read POWER UP bit to inform MMI of a possible RTC reset */ | |
395 if ( ((* (volatile UINT8*) RTC_STATUS_REG) & RTC_POWER_UP) ) | |
396 { | |
397 RTC_FREE_POWER_UP; | |
398 return TRUE; | |
399 } | |
400 else | |
401 return FALSE; | |
402 | |
403 } | |
404 | |
405 | |
406 /******************************************************************************* | |
407 * | |
408 * RTC_GetDateTime | |
409 * | |
410 ******************************************************************************/ | |
411 | |
412 T_RVF_RET RTC_GetDateTime(T_RTC_DATE_TIME* date_time) | |
413 { | |
414 UINT8 sec; | |
415 UINT8 min; | |
416 UINT8 hour; | |
417 UINT8 day; | |
418 UINT8 month; | |
419 UINT8 year; | |
420 UINT8 wday; | |
421 UINT8 hr_reg; | |
422 | |
423 /* Disable all IT before reading register */ | |
424 rvf_disable(25); | |
425 | |
426 if (RTC_TEST_BUSY) | |
427 { | |
428 rvf_enable(); | |
429 return RVF_NOT_READY; | |
430 } | |
431 else | |
432 { | |
433 day = * (volatile UINT8 *) RTC_DAYS_REG; | |
434 month = * (volatile UINT8 *) RTC_MONTHS_REG; | |
435 year = * (volatile UINT8 *) RTC_YEARS_REG; | |
436 wday = * (volatile UINT8 *) RTC_WEEK_REG; | |
437 sec = * (volatile UINT8 *) RTC_SECONDS_REG; | |
438 min = * (volatile UINT8 *) RTC_MINUTES_REG; | |
439 hr_reg = * (volatile UINT8 *) RTC_HOURS_REG; | |
440 rvf_enable(); | |
441 } | |
442 | |
443 hour = (0x7f & hr_reg); | |
444 | |
445 date_time->second = conv_bin(sec); | |
446 date_time->minute = conv_bin(min); | |
447 date_time->hour = conv_bin(hour); | |
448 | |
449 if ( (* (volatile UINT8 *)RTC_CTRL_REG & RTC_MODE_12_24 ) == 0) | |
450 { | |
451 /* 24 hour mode */ | |
452 date_time->mode_12_hour = FALSE; | |
453 date_time->PM_flag = FALSE; | |
454 } | |
455 else | |
456 { | |
457 /* 12 hour mode */ | |
458 date_time->mode_12_hour = TRUE; | |
459 if ((hr_reg & 0x80) == 0) | |
460 date_time->PM_flag = FALSE; | |
461 else | |
462 date_time->PM_flag = TRUE; | |
463 } | |
464 | |
465 date_time->day = conv_bin(day); | |
466 date_time->month = conv_bin(month); | |
467 date_time->year = conv_bin(year); | |
468 date_time->wday = conv_bin(wday); | |
469 | |
470 return RVF_OK; | |
471 } | |
472 | |
473 | |
474 /******************************************************************************* | |
475 * | |
476 * RTC_SetDateTime | |
477 * | |
478 ******************************************************************************/ | |
479 | |
480 T_RVF_RET RTC_SetDateTime(T_RTC_DATE_TIME date_time) | |
481 { | |
482 UINT8 sec; | |
483 UINT8 min; | |
484 UINT8 hour; | |
485 UINT8 day; | |
486 UINT8 month; | |
487 UINT8 year; | |
488 UINT8 wday; | |
489 | |
490 /* testing parameter range validity */ | |
491 if (!format_date_available(date_time)) | |
492 return RVF_INVALID_PARAMETER; | |
493 | |
494 | |
495 sec = conv_bcd(date_time.second); | |
496 min = conv_bcd(date_time.minute); | |
497 if (date_time.mode_12_hour == FALSE) | |
498 { | |
499 * (volatile UINT8 *)RTC_CTRL_REG &= ~RTC_MODE_12_24; | |
500 hour = conv_bcd(date_time.hour); | |
501 } | |
502 else | |
503 { | |
504 * (volatile UINT8 *)RTC_CTRL_REG |= RTC_MODE_12_24; | |
505 hour = conv_bcd(date_time.hour); | |
506 if (date_time.PM_flag != FALSE) | |
507 hour |= (0x80); | |
508 } | |
509 | |
510 day = conv_bcd(date_time.day); | |
511 month = conv_bcd(date_time.month); | |
512 year = conv_bcd(date_time.year); | |
513 wday = conv_bcd(date_time.wday); | |
514 | |
515 /* Disable all IT before reading register */ | |
516 rvf_disable(25); | |
517 | |
518 if (RTC_TEST_BUSY) | |
519 { | |
520 rvf_enable(); | |
521 return RVF_NOT_READY; | |
522 } | |
523 else | |
524 { | |
525 * (volatile UINT8 *) RTC_DAYS_REG = day; | |
526 * (volatile UINT8 *) RTC_MONTHS_REG = month; | |
527 * (volatile UINT8 *) RTC_YEARS_REG = year; | |
528 * (volatile UINT8 *) RTC_WEEK_REG = wday; | |
529 * (volatile UINT8 *) RTC_SECONDS_REG = sec; | |
530 * (volatile UINT8 *) RTC_MINUTES_REG = min; | |
531 * (volatile UINT8 *) RTC_HOURS_REG = hour; | |
532 | |
533 rvf_enable(); | |
534 } | |
535 | |
536 return RVF_OK; | |
537 } | |
538 | |
539 | |
540 /******************************************************************************* | |
541 * | |
542 * RTC_GetAlarm | |
543 * | |
544 ******************************************************************************/ | |
545 | |
546 T_RVF_RET RTC_GetAlarm(T_RTC_DATE_TIME* date_time) | |
547 { | |
548 date_time->second = rtc_date_time_alarm.second; | |
549 date_time->minute = rtc_date_time_alarm.minute; | |
550 date_time->hour = rtc_date_time_alarm.hour; | |
551 date_time->day = rtc_date_time_alarm.day; | |
552 date_time->month = rtc_date_time_alarm.month; | |
553 date_time->year = rtc_date_time_alarm.year; | |
554 date_time->wday = rtc_date_time_alarm.wday; | |
555 date_time->mode_12_hour = rtc_date_time_alarm.mode_12_hour; | |
556 date_time->PM_flag = rtc_date_time_alarm.PM_flag; | |
557 return RVF_OK; | |
558 } | |
559 | |
560 | |
561 /******************************************************************************* | |
562 * | |
563 * RTC_SetAlarm | |
564 * | |
565 ******************************************************************************/ | |
566 | |
567 T_RVF_RET RTC_SetAlarm(T_RTC_DATE_TIME date_time, T_RV_RETURN return_path) | |
568 { | |
569 UINT8 sec; | |
570 UINT8 min; | |
571 UINT8 hour; | |
572 UINT8 day; | |
573 UINT8 month; | |
574 UINT8 year; | |
575 UINT8 wday; | |
576 | |
577 | |
578 /* testing parameter range validity */ | |
579 if (!format_date_available(date_time)) | |
580 return RVF_INVALID_PARAMETER; | |
581 | |
582 sec = conv_bcd(date_time.second); | |
583 min = conv_bcd(date_time.minute); | |
584 | |
585 if (date_time.mode_12_hour == FALSE) | |
586 hour = conv_bcd(date_time.hour); | |
587 else | |
588 hour = conv_bcd(date_time.hour) | (date_time.PM_flag << 7); | |
589 | |
590 day = conv_bcd(date_time.day); | |
591 month = conv_bcd(date_time.month); | |
592 year = conv_bcd(date_time.year); | |
593 wday = conv_bcd(date_time.wday); | |
594 | |
595 | |
596 /* Disable all IT before reading register */ | |
597 rvf_disable(25); | |
598 | |
599 if (RTC_TEST_BUSY) | |
600 { | |
601 rvf_enable(); | |
602 return RVF_NOT_READY; | |
603 } | |
604 else | |
605 { | |
606 *(volatile UINT8*) RTC_ALARM_DAYS_REG = day; | |
607 *(volatile UINT8*) RTC_ALARM_MONTHS_REG = month; | |
608 *(volatile UINT8*) RTC_ALARM_YEARS_REG = year; | |
609 *(volatile UINT8*) RTC_ALARM_SECONDS_REG = sec; | |
610 *(volatile UINT8*) RTC_ALARM_MINUTES_REG = min; | |
611 *(volatile UINT8*) RTC_ALARM_HOURS_REG = hour; | |
612 | |
613 /* Enable alarm*/ | |
614 RTC_ENABLE_IT_ALARM; | |
615 rvf_enable(); | |
616 | |
617 /* Save callback */ | |
618 rtc_return_path.callback_func = return_path.callback_func; | |
619 rtc_return_path.addr_id = return_path.addr_id; | |
620 } | |
621 return RVF_OK; | |
622 } | |
623 | |
624 | |
625 /******************************************************************************* | |
626 * | |
627 * RTC_UnsetAlarm | |
628 * | |
629 ******************************************************************************/ | |
630 | |
631 T_RVF_RET RTC_UnsetAlarm(void) | |
632 { | |
633 /* Disable all IT before reading register */ | |
634 rvf_disable(25); | |
635 | |
636 if (RTC_TEST_BUSY) | |
637 { | |
638 rvf_enable(); | |
639 return RVF_NOT_READY; | |
640 } | |
641 else | |
642 { | |
643 /* Disable alarm*/ | |
644 RTC_DISABLE_IT_ALARM; | |
645 rvf_enable(); | |
646 } | |
647 return RVF_OK; | |
648 } | |
649 | |
650 | |
651 /******************************************************************************* | |
652 * | |
653 * RTC_Rounding30s | |
654 * | |
655 ******************************************************************************/ | |
656 | |
657 void RTC_Rounding30s(void) | |
658 { | |
659 *(UINT8*) RTC_CTRL_REG |= RTC_ROUND_30S; | |
660 } | |
661 | |
662 | |
663 /******************************************************************************* | |
664 * | |
665 * RTC_Set12HourMode | |
666 * | |
667 ******************************************************************************/ | |
668 | |
669 void RTC_Set12HourMode(BOOL Mode12Hour) | |
670 { | |
671 if ( Mode12Hour == FALSE) | |
672 * (volatile UINT8*) RTC_CTRL_REG &= 0xf7; | |
673 else | |
674 * (volatile UINT8*) RTC_CTRL_REG |= 0x08; | |
675 | |
676 } | |
677 | |
678 | |
679 /******************************************************************************* | |
680 * | |
681 * RTC_Is12HourMode | |
682 * | |
683 ******************************************************************************/ | |
684 | |
685 BOOL RTC_Is12HourMode(void) | |
686 { | |
687 if ( (*(volatile UINT8*) RTC_CTRL_REG & RTC_MODE_12_24) ) | |
688 return TRUE; | |
689 else | |
690 return FALSE; | |
691 } | |
692 | |
693 #if CONFIG_GSM | |
694 /******************************************************************************* | |
695 * | |
696 * RTC_ItTimerHandle | |
697 * | |
698 ******************************************************************************/ | |
699 | |
700 void RTC_ItTimerHandle(void) | |
701 { | |
702 static double compensation = 0; | |
703 static UINT8 nb_sample = 0; | |
704 double delta; | |
705 UINT16 value = 0; | |
706 INT16 tmp_value = 0; | |
707 UINT8 cmp[15]; | |
708 | |
709 /* Evaluate average on one hour max */ | |
710 if ( nb_sample < 60) | |
711 nb_sample++; | |
712 | |
713 /* perform calculation of auto compensation each minute and evaluate an | |
714 average on one hour */ | |
715 /* Number of 32 kHz clock lost in one second */ | |
716 /* Accurate operation is : delta = CLK_32 - rtc_nb_32khz*CLK_PLL/g_nb_hf */ | |
717 /* with CLK_32 = 32768 Hz and CLK_PLL depend on chipset */ | |
718 delta = RTC_CLOCK_32K - rtc_nb_32khz*RTC_CLOCK_HF/rtc_nb_hf; | |
719 | |
720 /* Number of 32 kHz clock lost in one hour */ | |
721 delta *= 3600.0; | |
722 | |
723 /* Average of the compensation to load */ | |
724 compensation = (compensation*(nb_sample-1) + delta)/nb_sample; | |
725 | |
726 if (compensation >= 0x7FFF) | |
727 tmp_value = 0x7FFE; | |
728 else if (compensation <= -0x7FFF) | |
729 tmp_value = -0x7FFE; | |
730 else | |
731 tmp_value = (INT16) compensation; | |
732 | |
733 if (tmp_value > 0) /* if 32 Khz clock is slow */ | |
734 value = tmp_value; | |
735 if (tmp_value < 0) /* if 32 Khz clock is fast */ | |
736 value = 0xFFFF + tmp_value + 1; | |
737 | |
738 /* Set value in compensation register */ | |
739 if (!RTC_TEST_BUSY) | |
740 { | |
741 *(volatile UINT8*) RTC_COMP_MSB_REG = (UINT8) (value >> 8); | |
742 *(volatile UINT8*) RTC_COMP_LSB_REG = (UINT8) (value & 0xFF); | |
743 } | |
744 /*toto = value; tmp*/ | |
745 /*NU_Activate_HISR(&hisr); tmp*/ | |
746 | |
747 } | |
748 #endif | |
749 | |
750 /******************************************************************************* | |
751 * | |
752 * RTC_ItAlarmHandle | |
753 * | |
754 ******************************************************************************/ | |
755 | |
756 void RTC_ItAlarmHandle(void) | |
757 { | |
758 /* Sending alarm event */ | |
759 /* Post alarm event in RTC mailbox */ | |
760 NU_Activate_HISR(&hisr); | |
761 | |
762 /*if (rtc_msg_alarm_event) | |
763 rvf_send_msg(rtc_env_ctrl_blk->task_id, rtc_env_ctrl_blk->mbox_id, rtc_msg_alarm_event);*/ | |
764 | |
765 /* Free alarm IT line */ | |
766 RTC_FREE_AL_ITLINE; | |
767 } | |
768 | |
769 #if CONFIG_GSM | |
770 /******************************************************************************* | |
771 * | |
772 * RTC_GaugingHandler | |
773 * | |
774 ******************************************************************************/ | |
775 void RTC_GaugingHandler(void) | |
776 { | |
777 /* Number of 32 Khz clock at the end of the gauging */ | |
778 rtc_nb_32khz = ((*(volatile UINT16 *)ULDP_COUNTER_32_MSB_REG) * 0x10000) + | |
779 (*(volatile UINT16 *)ULDP_COUNTER_32_LSB_REG); | |
780 | |
781 /* Number of high frequency clock at the end of the gauging */ | |
782 /* To convert in nbr of 13 Mhz clocks (5*13=65Mhz) */ | |
783 rtc_nb_hf = ( ((*(volatile UINT16 *)ULDP_COUNTER_HI_FREQ_MSB_REG) * 0x10000) + | |
784 (*(volatile UINT16 *)ULDP_COUNTER_HI_FREQ_LSB_REG) ); | |
785 } | |
786 #endif | |
787 | |
788 /******************************************************************************* | |
789 * | |
790 * RTC_ProcessAlarmEvent | |
791 * | |
792 ******************************************************************************/ | |
793 | |
794 void RTC_ProcessAlarmEvent(void) | |
795 { | |
796 T_RVF_MB_STATUS mb_status; | |
797 T_RTC_ALARM* msg_alarm; | |
798 | |
799 /* Call MMI */ | |
800 if (rtc_return_path.callback_func != NULL) | |
801 { | |
802 rtc_return_path.callback_func(NULL); | |
803 } | |
804 else | |
805 { | |
806 /* Reserve memory for alarm event */ | |
807 mb_status = rvf_get_buf (rtc_env_ctrl_blk->prim_id, sizeof (T_RTC_ALARM ), (void **) &msg_alarm); | |
808 | |
809 if ((mb_status == RVF_GREEN) || (mb_status == RVF_YELLOW)) /* Memory allocation success */ | |
810 { | |
811 msg_alarm->os_hdr.msg_id = RTC_ALARM_EVT; | |
812 /* Send event in the mailbox */ | |
813 rvf_send_msg(rtc_return_path.addr_id, msg_alarm); | |
814 } | |
815 else | |
816 { | |
817 rvf_send_trace("Memory allocation error",23, NULL_PARAM, RV_TRACE_LEVEL_ERROR, RTC_USE_ID ); | |
818 } | |
819 } | |
820 } |