comparison gsm-fw/bsp/rtc/rtc_functions.c @ 159:3c42e6e5fc04

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