PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
rtc.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 /******************************************************************************/
57 /******************************************************************************/
58 /* Include files */
59 /******************************************************************************/
60 #include "pdl.h"
61 
62 #if (defined(PDL_PERIPHERAL_RTC_ACTIVE))
63 
69 
70 /******************************************************************************/
71 /* Local pre-processor symbols/macros ('#define') */
72 /******************************************************************************/
73 /* RTC time value */
74 #define RTC_YEAR_1900 (1900)
75 #define RTC_YEAR_2000 (2000)
76 #define RTC_SEC_MAX (59u)
77 #define RTC_MIN_MAX (59u)
78 #define RTC_HOUR_MAX (23u)
79 #define RTC_DAY_MAX (31u)
80 #define RTC_MONTH_MAX (12u)
81 #define RTC_DAYOFWEEK_MAX (6u)
82 #define RTC_YEAR_MAX (99u)
83 
84 /* Caluculate value */
85 #define RTC_4BIT (4u)
86 #define RTC_8BIT (8u)
87 #define RTC_16BIT (16u)
88 #define RTC_DIGIT_10 (10u)
89 #define RTC_2BYTES (2u)
90 #define RTC_4BYTES (4u)
91 
92 /* for WTCR20 */
93 #define RTC_WTCR20_PWRITE (0x20u)
94 #define RTC_WTCR20_PREAD (0x10u)
95 #define RTC_WTCR20_BWRITE (0x08u)
96 #define RTC_WTCR20_BREAD (0x04u)
97 #define RTC_WTCR20_CWRITE (0x02u)
98 #define RTC_WTCR20_CREAD (0x01u)
99 
100 /* Mask value */
101 #define RTC_MASK_LOW4BIT (0x0Fu)
102 #define RTC_MASK_HIGH4BIT (0xF0u)
103 
104 /* Error code for mktime */
105 #define RTC_ERR (-1)
106 
107 /******************************************************************************/
108 /* Global variable definitions (declared in header file with 'extern') */
109 /******************************************************************************/
110 
111 /******************************************************************************/
112 /* Local type definitions ('typedef') */
113 /******************************************************************************/
114 
115 /******************************************************************************/
116 /* Local function prototypes ('static') */
117 /******************************************************************************/
118 static void RtcToTm( stc_rtc_time_t* pstcRtcTime,
119  struct tm* pstcTime);
120 static uint8_t RtcBinToBcd(uint8_t u8BinValue);
121 static uint8_t RtcBcdToBin(uint8_t u8BcdValue);
122 static void RtcInitIrq(void);
123 static void RtcDeInitIrq(void);
124 static en_result_t RtcWaitTransComplete(void);
125 static en_result_t RtcTransToVbat(uint8_t u8Control);
126 
127 /******************************************************************************/
128 /* Local variable definitions ('static') */
129 /******************************************************************************/
131 static uint8_t u8CRead;
132 static uint32_t u32NvicData;
133 
134 /******************************************************************************/
135 /* Function implementation - global ('extern') and local ('static') */
136 /******************************************************************************/
137 
142 void RtcDisableNvic(void)
143 {
144  u32NvicData = NVIC->ISER[(uint32_t)((int32_t)RTC_IRQn) >> 5];
145  NVIC->ICER[((uint32_t)(RTC_IRQn) >> 5)] = (1 << ((uint32_t)(RTC_IRQn) & 0x1F));
146 }
147 
152 void RtcRestoreNvic(void)
153 {
154  NVIC->ISER[(uint32_t)((int32_t)RTC_IRQn) >> 5] = u32NvicData;
155 }
156 
157 
166 static void RtcToTm( stc_rtc_time_t* pstcRtcTime,
167  struct tm* pstcTime)
168 {
169  pstcTime->tm_year = (int)((RTC_YEAR_2000 + pstcRtcTime->u8Year) - RTC_YEAR_1900);
170  pstcTime->tm_mon = (int)(pstcRtcTime->u8Month - 1);
171  pstcTime->tm_mday = (int)(pstcRtcTime->u8Day);
172  pstcTime->tm_sec = (int)(pstcRtcTime->u8Second);
173  pstcTime->tm_min = (int)(pstcRtcTime->u8Minute);
174  pstcTime->tm_hour = (int)(pstcRtcTime->u8Hour);
175  pstcTime->tm_isdst = 0;
176 } /* RtcToTm */
177 
187 static uint8_t RtcBinToBcd(uint8_t u8BinValue)
188 {
189  uint8_t u8BcdValue;
190 
191  /* Ten's digit */
192  u8BcdValue = (uint8_t)((u8BinValue / RTC_DIGIT_10) << RTC_4BIT);
193 
194  /* One's digit */
195  u8BcdValue |= u8BinValue % RTC_DIGIT_10;
196 
197  return (u8BcdValue);
198 } /* RtcBinToBcd */
199 
209 static uint8_t RtcBcdToBin(uint8_t u8BcdValue)
210 {
211  uint8_t u8BinValue;
212 
213  /* Ten's digit */
214  u8BinValue = ((RTC_MASK_HIGH4BIT & u8BcdValue) >> RTC_4BIT) * RTC_DIGIT_10;
215 
216  /* One's digit */
217  u8BinValue += u8BcdValue & RTC_MASK_LOW4BIT;
218 
219  return (u8BinValue);
220 } /* RtcBcdToBin */
221 
228 static void RtcInitIrq(void)
229 {
230 #if (PDL_PERIPHERAL_ENABLE_RTC0 == PDL_ON)
231  NVIC_ClearPendingIRQ(RTC_IRQn);
232  NVIC_EnableIRQ(RTC_IRQn);
233  NVIC_SetPriority(RTC_IRQn, PDL_IRQ_LEVEL_RTC0);
234 #endif
235 } /* RtcInitIrq */
236 
243 static void RtcDeInitIrq(void)
244 {
245 #if (PDL_PERIPHERAL_ENABLE_RTC0 == PDL_ON)
246  NVIC_ClearPendingIRQ(RTC_IRQn);
247  NVIC_DisableIRQ(RTC_IRQn);
248  NVIC_SetPriority(RTC_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
249 #endif
250 } /* RtcDeInitIrq */
251 
261 {
262  en_result_t enResult = Ok;
263  uint32_t u32WaitCompleteTimeout;
264 
265  u32WaitCompleteTimeout = RTC_TRANS_POLLING_TIMEOUT;
266  while ((TRUE == FM4_RTC->WTCR10_f.TRANS) /* Wait until transmission is completed */
267  && (0 != u32WaitCompleteTimeout) /* If transmission is not completed even if it passes for a long time... */
268  )
269  {
271  u32WaitCompleteTimeout--;
272  }
273 
274  if (0 == u32WaitCompleteTimeout)
275  {
276  enResult = ErrorTimeout;
277  }
278 
279  return (enResult);
280 } /* RtcWaitTransComplete */
281 
292 static en_result_t RtcTransToVbat(uint8_t u8Control)
293 {
294  en_result_t enResult;
295 
296  /* Transmit to or from VBAT domain */
297  FM4_RTC->WTCR20 = u8Control;
298  /* Wait to complete transmission */
299  enResult = RtcWaitTransComplete();
300 
301  return (enResult);
302 } /* RtcTransToVbat */
303 
309 void RtcIrqHandler(void)
310 {
311  stc_rtc_wtcr12_field_t stcWtcr12;
312 
313  /* Local variables to prevent multiple volatile I/O read-out */
314  /* warining in function call 'pfnReadCallback(...)' */
315  uint8_t u8Second;
316  uint8_t u8Minute;
317  uint8_t u8Hour;
318  uint8_t u8Day;
319  uint8_t u8DayOfWeek;
320  uint8_t u8Month;
321  uint8_t u8Year;
322 
323  stcWtcr12 = FM4_RTC->WTCR12_f;
324 
325  /* Read Completion? */
326  if (TRUE == stcWtcr12.INTCRI)
327  {
328  /* Clear INTCRI Flag */
329  stcWtcr12.INTCRI = FALSE;
330 
331  if (NULL != stcRtcInternData.pfnReadCallback)
332  {
333  if (TRUE == u8CRead)
334  {
335  u8CRead = FALSE;
336  /* Convert to Binary */
337  u8Second = RtcBcdToBin(FM4_RTC->WTSR);
338  u8Minute = RtcBcdToBin(FM4_RTC->WTMIR);
339  u8Hour = RtcBcdToBin(FM4_RTC->WTHR);
340  u8Day = RtcBcdToBin(FM4_RTC->WTDR);
341  u8DayOfWeek = RtcBcdToBin(FM4_RTC->WTDW);
342  u8Month = RtcBcdToBin(FM4_RTC->WTMOR);
343  u8Year = RtcBcdToBin(FM4_RTC->WTYR);
344  /* Callback */
345  stcRtcInternData.pfnReadCallback(u8Second,
346  u8Minute,
347  u8Hour,
348  u8Day,
349  u8DayOfWeek,
350  u8Month,
351  u8Year);
352  }
353  }
354  }
355 
356  /* Timer writing error Interrupt? */
357  if (TRUE == stcWtcr12.INTERI)
358  {
359  /* Clear INTERI Flag */
360  stcWtcr12.INTERI = FALSE;
361 
362  if (NULL != stcRtcInternData.pfnTimeWrtErrCallback)
363  {
364  /* Callback */
365  stcRtcInternData.pfnTimeWrtErrCallback();
366  }
367  }
368 
369  /* Alarm Completion? */
370  if (TRUE == stcWtcr12.INTALI)
371  {
372  /* Clear INTALI Flag */
373  stcWtcr12.INTALI = FALSE;
374 
375  if (NULL != stcRtcInternData.pfnAlarmCallback)
376  {
377  /* Callback */
378  stcRtcInternData.pfnAlarmCallback();
379  }
380  }
381 
382  /* Timer Interrupt? */
383  if (TRUE == stcWtcr12.INTTMI)
384  {
385  /* Clear INTTMI Flag */
386  stcWtcr12.INTTMI = FALSE;
387 
388  if (NULL != stcRtcInternData.pfnTimerCallback)
389  {
390  /* Callback */
391  stcRtcInternData.pfnTimerCallback();
392  }
393  }
394 
395  /* 0.5-Second Interrupt? */
396  if (TRUE == stcWtcr12.INTSSI)
397  {
398  /* Clear INTSSI Flag */
399  stcWtcr12.INTSSI = FALSE;
400 
401  if (NULL != stcRtcInternData.pfnHalfSecondCallback)
402  {
403  /* Callback */
404  stcRtcInternData.pfnHalfSecondCallback();
405  }
406  }
407 
408  /* One Second Interrupt? */
409  if (TRUE == stcWtcr12.INTSI)
410  {
411  /* Clear INTSI Flag */
412  stcWtcr12.INTSI = FALSE;
413 
414  if (NULL != stcRtcInternData.pfnOneSecondCallback)
415  {
416  /* Callback */
417  stcRtcInternData.pfnOneSecondCallback();
418  }
419  }
420 
421  /* One Minute Interrupt? */
422  if (TRUE == stcWtcr12.INTMI)
423  {
424  /* Clear INTMI Flag */
425  stcWtcr12.INTMI = FALSE;
426 
427  if (NULL != stcRtcInternData.pfnOneMinuteCallback)
428  {
429  /* Callback */
430  stcRtcInternData.pfnOneMinuteCallback();
431  }
432  }
433 
434  /* One Hour Interrupt? */
435  if (TRUE == stcWtcr12.INTHI)
436  {
437  /* Clear INTHI Flag */
438  stcWtcr12.INTHI = FALSE;
439 
440  if (NULL != stcRtcInternData.pfnOneHourCallback)
441  {
442  /* Callback */
443  stcRtcInternData.pfnOneHourCallback();
444  }
445  }
446 
447  FM4_RTC->WTCR12_f = stcWtcr12;
448 } /* RtcIrqHandler */
449 
478  stc_rtc_time_t* pstcTime,
479  stc_rtc_alarm_t* pstcAlarm,
480  boolean_t bTouchNvic,
481  boolean_t bResetNoReInit
482  )
483 {
484  en_result_t enResult;
485 
486  /*
487  * If bDstbResetNoReInit is TRUE, don't do initialization when power on or
488  * deep standby reset occurs and RTC is aready running. However the interrupt
489  * callback and interrupt enable still need updating.
490  */
491  if((TRUE == bResetNoReInit) && (TRUE == FM4_RTC->WTCR10_f.ST))
492  {
493  /* Check for Interrupt enables */
494  if (0 != ((pstcConfig->u8AllInterrupts) & RTC_INTERRUPT_ENABLE_MASK))
495  {
496  /* No errors expected here (pointers checked previously) */
497  (void)Rtc_EnableDisableInterrupts(pstcConfig);
498 
499  if (TRUE == bTouchNvic)
500  {
501  RtcInitIrq();
502  }
503  }
504  /* Set-up callbacks */
505  stcRtcInternData.pfnReadCallback = pstcConfig->pfnReadCallback;
506  stcRtcInternData.pfnTimeWrtErrCallback = pstcConfig->pfnTimeWrtErrCallback;
507  stcRtcInternData.pfnAlarmCallback = pstcConfig->pfnAlarmCallback;
508  stcRtcInternData.pfnTimerCallback = pstcConfig->pfnTimerCallback;
509  stcRtcInternData.pfnHalfSecondCallback = pstcConfig->pfnHalfSecondCallback;
510  stcRtcInternData.pfnOneSecondCallback = pstcConfig->pfnOneSecondCallback;
511  stcRtcInternData.pfnOneMinuteCallback = pstcConfig->pfnOneMinuteCallback;
512  stcRtcInternData.pfnOneHourCallback = pstcConfig->pfnOneHourCallback;
513 
514  enResult = Ok;
515  }
516  /* Check for valid pointer */
517  else if (NULL == pstcConfig)
518  {
519  enResult = ErrorInvalidParameter;
520  }
521 
522  /* Wait to complete transmission */
523  else if (Ok != RtcWaitTransComplete())
524  {
525  enResult = ErrorTimeout;
526  }
527 
528  else
529  {
530  /* For initialization: Force all bits of WTCR10-13 (inclusive ST bit) */
531  /* to '0' (w/o previous check) */
532  FM4_RTC->WTCR10 = 0;
533  FM4_RTC->WTCR11 = 0;
534  FM4_RTC->WTCR12 = 0;
535  FM4_RTC->WTCR13 = 0;
536  FM4_RTC->WTCR20 = 0;
537  FM4_RTC->WTCR21 = 0;
538 
539  /* Check and Set Date and Time */
540  enResult = Rtc_SetDateTime(pstcTime);
541  if (Ok == enResult)
542  {
543  /* If Alarm is specified */
544  if (NULL != pstcAlarm)
545  {
546  /* Check and Set Alarm Date and Time */
547  enResult = Rtc_SetAlarmDateTime(pstcAlarm);
548  }
549  }
550 
551  if (Ok == enResult)
552  {
553  /* Check value for frequency correction value */
554  if (RTC_MAX_FREQ_CORR_VALUE < (pstcConfig->u16FreqCorrValue))
555  {
556  enResult = ErrorInvalidParameter;
557  }
558  }
559 
560  if (Ok == enResult)
561  {
562  /* Set value for frequency correction */
563  FM4_RTC->WTCAL0 = pstcConfig->u8FreqCorrValue0;
564  FM4_RTC->WTCAL1 = pstcConfig->u8FreqCorrValue1;
565 
566  /* Set Frequency correction enable */
567  if (TRUE == pstcConfig->bUseFreqCorr)
568  {
569  FM4_RTC->WTCALEN = 1;
570  }
571  else
572  {
573  FM4_RTC->WTCALEN = 0;
574  }
575 
576  /* Set Divider ratio */
577  switch (pstcConfig->u8DividerRatio)
578  {
579  case RtcDivRatio1:
580  case RtcDivRatio2:
581  case RtcDivRatio4:
582  case RtcDivRatio8:
583  case RtcDivRatio16:
584  case RtcDivRatio32:
585  case RtcDivRatio64:
586  case RtcDivRatio128:
587  case RtcDivRatio256:
588  case RtcDivRatio512:
589  case RtcDivRatio1024:
590  case RtcDivRatio2048:
591  case RtcDivRatio4096:
592  case RtcDivRatio8192:
593  case RtcDivRatio16384:
594  case RtcDivRatio32768:
595  FM4_RTC->WTDIV = pstcConfig->u8DividerRatio;
596  break;
597  default:
598  enResult = ErrorInvalidParameter;
599  break;
600  }
601  }
602 
603  if (Ok == enResult)
604  {
605  /* Set Dividor output enable */
606  if (TRUE == pstcConfig->bUseDivider)
607  {
608  FM4_RTC->WTDIVEN_f.WTDIVEN = TRUE;
609  }
610  else
611  {
612  FM4_RTC->WTDIVEN_f.WTDIVEN = FALSE;
613  }
614 
615  /* Set Frequency correction cycle setting */
616  if (RTC_MAX_FREQ_CORR_CYCLE_SET_VALUE < pstcConfig->u8FreqCorrCycle)
617  {
618  enResult = ErrorInvalidParameter;
619  }
620  }
621 
622  if (Ok == enResult)
623  {
624  FM4_RTC->WTCALPRD = pstcConfig->u8FreqCorrCycle;
625 
626  /* Set RTCCO output selection */
627  switch (pstcConfig->u8CoSignalDiv)
628  {
629  case RtcCoDiv1:
630  FM4_RTC->WTCOSEL_f.WTCOSEL = FALSE;
631  break;
632  case RtcCoDiv2:
633  FM4_RTC->WTCOSEL_f.WTCOSEL = TRUE;
634  break;
635  default:
636  enResult = ErrorInvalidParameter;
637  break;
638  }
639  }
640 
641  if (Ok == enResult)
642  {
643  /* Transmit to VBAT domain and wait to complete transmission */
644  enResult = RtcTransToVbat(RTC_WTCR20_PWRITE);
645  }
646 
647  if (Ok == enResult)
648  {
649  /* Setup Alarm Registers */
651  }
652 
653  if (Ok == enResult)
654  {
655  /* When interruption enables, callback may call before initialization. */
656  stcRtcInternData.pfnReadCallback = NULL;
657  stcRtcInternData.pfnTimeWrtErrCallback = NULL;
658  stcRtcInternData.pfnAlarmCallback = NULL;
659  stcRtcInternData.pfnTimerCallback = NULL;
660  stcRtcInternData.pfnHalfSecondCallback = NULL;
661  stcRtcInternData.pfnOneSecondCallback = NULL;
662  stcRtcInternData.pfnOneMinuteCallback = NULL;
663  stcRtcInternData.pfnOneHourCallback = NULL;
664  u8CRead = FALSE;
665 
666  /* Check for Interrupt enables */
667  if (0 != ((pstcConfig->u8AllInterrupts) & RTC_INTERRUPT_ENABLE_MASK))
668  {
669  /* No errors expected here (pointers checked previously) */
670  (void)Rtc_EnableDisableInterrupts(pstcConfig);
671 
672  if (TRUE == bTouchNvic)
673  {
674  RtcInitIrq();
675  }
676  }
677 
678  /* Set-up callbacks */
679  stcRtcInternData.pfnReadCallback = pstcConfig->pfnReadCallback;
680  stcRtcInternData.pfnTimeWrtErrCallback = pstcConfig->pfnTimeWrtErrCallback;
681  stcRtcInternData.pfnAlarmCallback = pstcConfig->pfnAlarmCallback;
682  stcRtcInternData.pfnTimerCallback = pstcConfig->pfnTimerCallback;
683  stcRtcInternData.pfnHalfSecondCallback = pstcConfig->pfnHalfSecondCallback;
684  stcRtcInternData.pfnOneSecondCallback = pstcConfig->pfnOneSecondCallback;
685  stcRtcInternData.pfnOneMinuteCallback = pstcConfig->pfnOneMinuteCallback;
686  stcRtcInternData.pfnOneHourCallback = pstcConfig->pfnOneHourCallback;
687 
688  /* Start RTC */
689  FM4_RTC->WTCR10_f.ST = TRUE;
690  /* Transmit to VBAT domain and wait to complete transmission */
691  enResult = RtcTransToVbat(RTC_WTCR20_CWRITE);
692  }
693  }
694 
695  return (enResult);
696 } /* Rtc_Init */
697 
709 {
710  return (boolean_t)FM4_RTC->WTCR10_f.ST;
711 }
712 
732 {
733  en_result_t enResult;
734  uint32_t u32BusyTimeout;
735  stc_rtc_wtcr12_field_t stcWtcr12;
736  stc_rtc_wtcr13_field_t stcWtcr13;
737 
738  /* Check for valid pointer */
739  if ((NULL == pstcRtcTime)
740  /* Check Second */
741  || (RTC_SEC_MAX < pstcRtcTime->u8Second)
742  /* Check Minute */
743  || (RTC_MIN_MAX < pstcRtcTime->u8Minute)
744  /* Check Hour */
745  || (RTC_HOUR_MAX < pstcRtcTime->u8Hour)
746  /* Check Day */
747  || ((RTC_DAY_MAX < pstcRtcTime->u8Day) || (0 == pstcRtcTime->u8Day))
748  /* Check Day of the week */
749  || (RTC_DAYOFWEEK_MAX < pstcRtcTime->u8DayOfWeek)
750  /* Check Month */
751  || ((RTC_MONTH_MAX < pstcRtcTime->u8Month) || (0 == pstcRtcTime->u8Month))
752  /* Check Year */
753  || ((RTC_YEAR_MAX < pstcRtcTime->u8Year) || (0 == pstcRtcTime->u8Year)))
754  {
755  enResult = ErrorInvalidParameter;
756  }
757 
758  else
759  {
760  /* Wait to complete transmission */
761  enResult = RtcWaitTransComplete();
762  if (Ok == enResult)
763  {
764  /* RTC running? */
765  if (TRUE == FM4_RTC->WTCR10_f.ST)
766  {
767  /* Backup Interrupt Settings */
768  stcWtcr13 = FM4_RTC->WTCR13_f;
769 
770  /* Wait to release busy */
771  u32BusyTimeout = RTC_BUSY_POLLING_TIMEOUT;
772  while (((TRUE == FM4_RTC->WTCR10_f.BUSY) || (TRUE == FM4_RTC->WTCR10_f.SCST)) &&
773  (0 != u32BusyTimeout)
774  )
775  {
777  u32BusyTimeout--;
778  }
779 
780  if (0 == u32BusyTimeout)
781  {
782  enResult = ErrorTimeout;
783  }
784 
785  if (Ok == enResult)
786  {
787  /* Next instructions must be free of interruptions */
788  RtcDisableNvic();
789 
790  /* Disable interrupt */
791  FM4_RTC->WTCR13 = 0;
792 
793  /* Clear every second interrupt */
794  stcWtcr12 = FM4_RTC->WTCR12_f;
795  stcWtcr12.INTSI = FALSE;
796  FM4_RTC->WTCR12_f = stcWtcr12;
797 
798  /* Enable every second interrupt */
799  FM4_RTC->WTCR13_f.INTSIE = TRUE;
800 
801  /* Wait to occur interrupt */
802  do
803  {
804  stcWtcr12 = FM4_RTC->WTCR12_f;
805  } while (0 == stcWtcr12.INTSI);
806 
807  /* Clear every second interrupt */
808  FM4_RTC->WTCR12_f.INTSI = 0;
809 
810  /* Stop 1-second pulse detection */
811  FM4_RTC->WTCR10_f.SCST = TRUE;
812 
813  /* Retrieve Interrupt Settings */
814  FM4_RTC->WTCR13_f = stcWtcr13;
815  }
816  }
817  /* RTC not running ... */
818  else
819  {
820  /* Next instructions must be free of interruptions */
821  RtcDisableNvic();
822  }
823 
824  if (Ok == enResult)
825  {
826  /* update RTC */
827  FM4_RTC->WTSR = RtcBinToBcd(pstcRtcTime->u8Second);
828  FM4_RTC->WTMIR = RtcBinToBcd(pstcRtcTime->u8Minute);
829  FM4_RTC->WTHR = RtcBinToBcd(pstcRtcTime->u8Hour);
830  FM4_RTC->WTDR = RtcBinToBcd(pstcRtcTime->u8Day);
831  FM4_RTC->WTDW = pstcRtcTime->u8DayOfWeek;
832  FM4_RTC->WTMOR = RtcBinToBcd(pstcRtcTime->u8Month);
833  FM4_RTC->WTYR = RtcBinToBcd(pstcRtcTime->u8Year);
834 
835  /* RTC running? */
836  if (TRUE == FM4_RTC->WTCR10_f.ST)
837  {
838  /* Enable 1-second pulse detection */
839  FM4_RTC->WTCR10_f.SCST = FALSE;
840  }
841 
842  /* Transmit to VBAT domain and wait to complete transmission */
843  enResult = RtcTransToVbat(RTC_WTCR20_CWRITE);
844 
845  /* Restore normal interupt handling */
846  RtcRestoreNvic();
847  }
848  }
849  }
850 
851  return (enResult);
852 } /* Rtc_SetDateTime */
853 
854 
869 {
870  en_result_t enResult;
871  stc_rtc_wtcr10_field_t stcWtcr10;
872  stc_rtc_wtcr12_field_t stcWtcr12;
873  stc_rtc_wtcr13_field_t stcWtcr13;
874 
875  /* Check for valid pointer */
876  if ((NULL == pstcRtcAlarm)
877  /* Check Alarm Day */
878  || ((RTC_DAY_MAX < pstcRtcAlarm->u8AlarmDay) || (0 == pstcRtcAlarm->u8AlarmDay))
879  /* Check Alarm Hour */
880  || (RTC_HOUR_MAX < pstcRtcAlarm->u8AlarmHour)
881  /* Check Alarm Minute */
882  || (RTC_MIN_MAX < pstcRtcAlarm->u8AlarmMinute)
883  /* Check Alarm Year */
884  || ((RTC_YEAR_MAX < pstcRtcAlarm->u8AlarmYear) || (0 == pstcRtcAlarm->u8AlarmYear))
885  /* Check Alarm Month */
886  || ((RTC_MONTH_MAX < pstcRtcAlarm->u8AlarmMonth) || (0 == pstcRtcAlarm->u8AlarmMonth)))
887  {
888  enResult = ErrorInvalidParameter;
889  }
890 
891  else
892  {
893  /* Wait to complete transmission */
894  enResult = RtcWaitTransComplete();
895  if (Ok == enResult)
896  {
897  /* Next instructions must be free of interruptions */
898  RtcDisableNvic();
899 
900  /* RTC is running? */
901  if (TRUE == FM4_RTC->WTCR10_f.ST)
902  {
903  stcWtcr10 = FM4_RTC->WTCR10_f; /* to retrieve ST bit */
904  stcWtcr13 = FM4_RTC->WTCR13_f; /* Backup Interrupt settings */
905 
906  FM4_RTC->WTCR10_f.ST = FALSE; /* RTC stops temporally */
907  /* Transmit to VBAT domain and wait to complete transmission */
908  enResult = RtcTransToVbat(RTC_WTCR20_CWRITE);
909  if (Ok == enResult)
910  {
911  /* Disable interrupt */
912  FM4_RTC->WTCR13 = 0;
913 
914  /* Clear read interrupt */
915  stcWtcr12 = FM4_RTC->WTCR12_f;
916  stcWtcr12.INTCRI = FALSE;
917  FM4_RTC->WTCR12_f = stcWtcr12;
918 
919  /* Enable read interrupt */
920  FM4_RTC->WTCR13_f.INTCRIE = TRUE;
921 
922  /* Transfer from VBAT domain and wait to complete transmission */
924  {
925  FM4_RTC->WTCR13_f = stcWtcr13; /* Retrieve Interrupt settings */
926  if (TRUE == stcWtcr10.ST) /* Retrieve RTC running */
927  {
928  FM4_RTC->WTCR10_f.ST = TRUE;
929  /* Transfer from VBAT domain and wait to complete transmission */
931  }
932  enResult = ErrorTimeout;
933  }
934  }
935 
936  if (Ok == enResult)
937  {
938  /* Disable interrupt */
939  FM4_RTC->WTCR13 = 0;
940 
941  /* Clear read interrupt and alarm interrupt */
942  stcWtcr12 = FM4_RTC->WTCR12_f;
943  stcWtcr12.INTCRI = FALSE;
944  stcWtcr12.INTSI = FALSE;
945  FM4_RTC->WTCR12_f = stcWtcr12;
946 
947  /* update RTC Alarm registers */
948  FM4_RTC->ALDR = RtcBinToBcd(pstcRtcAlarm->u8AlarmDay);
949  FM4_RTC->ALHR = RtcBinToBcd(pstcRtcAlarm->u8AlarmHour);
950  FM4_RTC->ALMIR = RtcBinToBcd(pstcRtcAlarm->u8AlarmMinute);
951  FM4_RTC->ALYR = RtcBinToBcd(pstcRtcAlarm->u8AlarmYear);
952  FM4_RTC->ALMOR = RtcBinToBcd(pstcRtcAlarm->u8AlarmMonth);
953 
954  /* Enable every second interrupt */
955  FM4_RTC->WTCR13_f.INTSIE = TRUE;
956 
957  /* Wait to occur every second interrupt */
958  do
959  {
960  stcWtcr12 = FM4_RTC->WTCR12_f;
961  } while (FALSE == stcWtcr12.INTSI);
962 
963  /* Call the interruption handler (Clear every second interrupt) */
964  RtcIrqHandler();
965 
966  /* Start RTC */
967  FM4_RTC->WTCR10_f.ST = TRUE;
968  /* Transmit to VBAT domain and wait to complete transmission */
969  enResult = RtcTransToVbat(RTC_WTCR20_CWRITE);
970 
971  FM4_RTC->WTCR13_f = stcWtcr13;
972  }
973  }
974  else
975  {
976  /* update RTC Alarm registers */
977  FM4_RTC->ALDR = RtcBinToBcd(pstcRtcAlarm->u8AlarmDay);
978  FM4_RTC->ALHR = RtcBinToBcd(pstcRtcAlarm->u8AlarmHour);
979  FM4_RTC->ALMIR = RtcBinToBcd(pstcRtcAlarm->u8AlarmMinute);
980  FM4_RTC->ALYR = RtcBinToBcd(pstcRtcAlarm->u8AlarmYear);
981  FM4_RTC->ALMOR = RtcBinToBcd(pstcRtcAlarm->u8AlarmMonth);
982 
983  /* Transmit to VBAT domain and wait to complete transmission */
984  enResult = RtcTransToVbat(RTC_WTCR20_CWRITE);
985  }
986  RtcRestoreNvic(); /* Restore normal interupt handling */
987  }
988  }
989 
990  return (enResult);
991 } /* Rtc_SetAlarmDateTime */
992 
1007 {
1008  stc_rtc_wtcr13_field_t stcWtcr13 = { 0 }; /* At first, set all interrupts to disabling */
1009  en_result_t enResult;
1010 
1011  enResult = ErrorInvalidParameter;
1012 
1013  /* Check for valid pointer */
1014  if (NULL != pstcConfig)
1015  {
1016  /* Enable counter value read completion interrupt */
1017  if (TRUE == pstcConfig->stcRtcInterruptEnable.ReadCompletionIrqEn)
1018  {
1019  stcWtcr13.INTCRIE = TRUE;
1020  }
1021 
1022  /* Enable timer rewrite error interrupt */
1024  {
1025  stcWtcr13.INTERIE = TRUE;
1026  }
1027 
1028  /* Enable alarm coincidence interrupt */
1029  if (TRUE == pstcConfig->stcRtcInterruptEnable.AlarmIrqEn)
1030  {
1031  stcWtcr13.INTALIE = TRUE;
1032  }
1033 
1034  /* Enable timer underflow detection interrupt */
1035  if (TRUE == pstcConfig->stcRtcInterruptEnable.TimerIrqEn)
1036  {
1037  stcWtcr13.INTTMIE = TRUE;
1038  }
1039 
1040  /* Enable every hour interrupt */
1041  if (TRUE == pstcConfig->stcRtcInterruptEnable.OneHourIrqEn)
1042  {
1043  stcWtcr13.INTHIE = TRUE;
1044  }
1045 
1046  /* Enable every minute interrupt */
1047  if (TRUE == pstcConfig->stcRtcInterruptEnable.OneMinuteIrqEn)
1048  {
1049  stcWtcr13.INTMIE = TRUE;
1050  }
1051 
1052  /* Enable every second interrupt */
1053  if (TRUE == pstcConfig->stcRtcInterruptEnable.OneSecondIrqEn)
1054  {
1055  stcWtcr13.INTSIE = TRUE;
1056  }
1057 
1058  /* Enable every 0.5 second interrupt */
1059  if (TRUE == pstcConfig->stcRtcInterruptEnable.HalfSecondIrqEn)
1060  {
1061  stcWtcr13.INTSSIE = TRUE;
1062  }
1063 
1064  FM4_RTC->WTCR13_f = stcWtcr13; /* Update WTCR13 */
1065 
1066  enResult = Ok;
1067  }
1068 
1069  return (enResult);
1070 } /* Rtc_EnableDisableInterrupts */
1071 
1087 {
1088  /* At first, set all alarms to disabling */
1089  stc_rtc_wtcr11_field_t stcWtcr11 = { 0 };
1090  en_result_t enResult;
1091 
1092  enResult = ErrorInvalidParameter;
1093 
1094  /* Check for valid pointer */
1095  if (NULL != pstcRtcAlarmEn)
1096  {
1097  /* Enable year alarm */
1098  if (TRUE == pstcRtcAlarmEn->AlarmYearEnable)
1099  {
1100  stcWtcr11.YEN = TRUE;
1101  }
1102 
1103  /* Enable month alarm */
1104  if (TRUE == pstcRtcAlarmEn->AlarmMonthEnable)
1105  {
1106  stcWtcr11.MOEN = TRUE;
1107  }
1108 
1109  /* Enable day alarm */
1110  if (TRUE == pstcRtcAlarmEn->AlarmDayEnable)
1111  {
1112  stcWtcr11.DEN = TRUE;
1113  }
1114 
1115  /* Enable hour alarm */
1116  if (TRUE == pstcRtcAlarmEn->AlarmHourEnable)
1117  {
1118  stcWtcr11.HEN = TRUE;
1119  }
1120 
1121  /* Enable minute alarm */
1122  if (TRUE == pstcRtcAlarmEn->AlarmMinuteEnable)
1123  {
1124  stcWtcr11.MIEN = TRUE;
1125  }
1126 
1127  FM4_RTC->WTCR11_f = stcWtcr11; /* Update WTCR11 */
1128 
1129  /* Transmit to VBAT domain and wait to complete transmission */
1130  enResult = RtcTransToVbat(RTC_WTCR20_CWRITE);
1131  }
1132 
1133  return (enResult);
1134 } /* Rtc_EnableDisableAlarmRegisters */
1135 
1155 {
1156  en_result_t enResult;
1157  uint8_t u8Wtcr13;
1158  uint8_t u8Wtcr12;
1159 
1160  enResult = ErrorInvalidParameter;
1161 
1162  /* Check for valid pointer */
1163  if (NULL != pstcRtcTime)
1164  {
1165  u8Wtcr13 = FM4_RTC->WTCR13; /* Save Interrupt Enable */
1166  FM4_RTC->WTCR13_f.INTCRIE = FALSE; /* Disable INTCRIE */
1167 
1168  /* Wait to complete recalling RTC setting from VBAT domain */
1169  enResult = RtcWaitTransComplete();
1170  if (Ok != enResult)
1171  {
1172  /* Restore Inerrupt Enable */
1173  FM4_RTC->WTCR13 = u8Wtcr13;
1174  }
1175  else
1176  {
1177  /* Transfer from VBAT domain and wait to complete transmission */
1178  enResult = RtcTransToVbat(RTC_WTCR20_CREAD);
1179  if (Ok != enResult)
1180  {
1181  /* Restore Inerrupt Enable */
1182  FM4_RTC->WTCR13 = u8Wtcr13;
1183  }
1184  }
1185 
1186  if (Ok == enResult)
1187  {
1188  /* Clear INTCRI flag */
1189  u8Wtcr12 = FM4_RTC->WTCR12;
1190  u8Wtcr12 &= ~(RTC_WTCR12_INTCRI_FLAG_POSITION);
1191  FM4_RTC->WTCR12 = u8Wtcr12;
1192 
1193  /* Store Date & Time in pointered structure */
1194  pstcRtcTime->u8Second = RtcBcdToBin(FM4_RTC->WTSR);
1195  pstcRtcTime->u8Minute = RtcBcdToBin(FM4_RTC->WTMIR);
1196  pstcRtcTime->u8Hour = RtcBcdToBin(FM4_RTC->WTHR);
1197  pstcRtcTime->u8Day = RtcBcdToBin(FM4_RTC->WTDR);
1198  pstcRtcTime->u8DayOfWeek = RtcBcdToBin(FM4_RTC->WTDW);
1199  pstcRtcTime->u8Month = RtcBcdToBin(FM4_RTC->WTMOR);
1200  pstcRtcTime->u8Year = RtcBcdToBin(FM4_RTC->WTYR);
1201 
1202  /* Restore Inerrupt Enable */
1203  FM4_RTC->WTCR13 = u8Wtcr13;
1204  }
1205  }
1206 
1207  return (enResult);
1208 } /* Rtc_ReadDateTimePolling */
1209 
1226 {
1227  en_result_t enResult;
1228 
1229  /* Check if a transmission via VBAT domain was started and did not finish yet ... */
1230  if (TRUE == FM4_RTC->WTCR10_f.TRANS)
1231  {
1232  enResult = ErrorNotReady;
1233  }
1234  else
1235  {
1236  /* Start request */
1237  u8CRead = TRUE;
1238  FM4_RTC->WTCR20 = RTC_WTCR20_CREAD;
1239  enResult = Ok;
1240  }
1241 
1242  return (enResult);
1243 }
1244 
1264 {
1265  en_result_t enResult;
1266  uint32_t u32WaitPollingTimeout;
1267  stc_rtc_wtcr12_field_t stcWtcr12;
1268  stc_rtc_wtcr13_field_t stcWtcr13;
1269  stc_rtc_wtcr21_field_t stcWtcr21;
1270 
1271  /* Check for valid pointer and get pointer to internal data struct and check */
1272  if ((NULL == pstcConfig)
1273  || (0 == pstcConfig->u32TimerValue)
1274  || (RTC_MAX_TIMER_SET_VALUE < pstcConfig->u32TimerValue)
1275  )
1276  {
1277  enResult = ErrorInvalidParameter;
1278  }
1279  else
1280  {
1281  /* Wait to complete transmission */
1282  enResult = RtcWaitTransComplete();
1283  if (Ok == enResult)
1284  {
1285  stcWtcr21 = FM4_RTC->WTCR21_f;
1286  /* At first, the RTC timer stops */
1287  FM4_RTC->WTCR21_f.TMST = FALSE;
1288  /* Transmit to VBAT domain and wait to complete transmission */
1289  enResult = RtcTransToVbat(RTC_WTCR20_CWRITE);
1290 
1291  /* Set timer counter control */
1292  if (TRUE == pstcConfig->bTimerIntervalEnable)
1293  {
1294  /* Operates at a specific interval */
1295  FM4_RTC->WTCR21_f.TMEN = TRUE;
1296  }
1297  else
1298  {
1299  /* Operates after a specific time */
1300  FM4_RTC->WTCR21_f.TMEN = FALSE;
1301  }
1302  /* Transmit to VBAT domain and wait to complete transmission */
1303  enResult = RtcTransToVbat(RTC_WTCR20_CWRITE);
1304  if (Ok != enResult)
1305  {
1306  /* Retrieve WTCR21 */
1307  FM4_RTC->WTCR21_f = stcWtcr21;
1308  /* Transmit to VBAT domain and wait to complete transmission */
1310  }
1311  }
1312  if (Ok == enResult)
1313  {
1314  /* Check the timer has stopped */
1315  u32WaitPollingTimeout = RTC_BUSY_POLLING_TIMEOUT;
1316  while((TRUE == FM4_RTC->WTCR21_f.TMRUN) && (0 != u32WaitPollingTimeout))
1317  {
1319  u32WaitPollingTimeout--;
1320  }
1321  if (0 == u32WaitPollingTimeout)
1322  {
1323  enResult = ErrorTimeout;
1324  }
1325  else
1326  {
1327  /* Next instructions must be free of interruptions */
1328  RtcDisableNvic();
1329  stcWtcr13 = FM4_RTC->WTCR13_f; /* Back up interrupt settings */
1330  if (TRUE == FM4_RTC->WTCR10_f.RUN) /* RTC running ? */
1331  {
1332  /* Disable interrupt */
1333  FM4_RTC->WTCR13 = 0;
1334 
1335  /* Clear every second interrupt */
1336  stcWtcr12 = FM4_RTC->WTCR12_f;
1337  stcWtcr12.INTSI = FALSE;
1338  FM4_RTC->WTCR12_f = stcWtcr12;
1339 
1340  /* Enable every second interrupt */
1341  FM4_RTC->WTCR13_f.INTSIE = TRUE;
1342 
1343  /* Wait to occur interrupt */
1344  do
1345  {
1346  stcWtcr12 = FM4_RTC->WTCR12_f;
1347  } while (0 == stcWtcr12.INTSI);
1348 
1349  /* Clear every second interrupt */
1350  FM4_RTC->WTCR12_f.INTSI = 0;
1351  }
1352 
1353 
1354  /* Set up new Value */
1355  FM4_RTC->WTTR0 = (uint8_t)(pstcConfig->u32TimerValue);
1356  FM4_RTC->WTTR1 = (uint8_t)((pstcConfig->u32TimerValue) >> RTC_8BIT);
1357  FM4_RTC->WTTR2 = (uint8_t)((pstcConfig->u32TimerValue) >> RTC_16BIT);
1358  /* Transmit to VBAT domain and wait to complete transmission */
1359  enResult = RtcTransToVbat(RTC_WTCR20_CWRITE);
1360 
1361  FM4_RTC->WTCR13_f = stcWtcr13; /* Retrieve interrupt settings */
1362  RtcRestoreNvic();
1363  }
1364  }
1365  }
1366 
1367  return (enResult);
1368 } /* Rtc_TimerSet */
1369 
1384 {
1385  en_result_t enResult;
1386  stc_rtc_wtcr21_field_t stcWtcr21;
1387 
1388  /* Back up WTCR21 */
1389  stcWtcr21 = FM4_RTC->WTCR21_f;
1390  /* Start the timer */
1391  FM4_RTC->WTCR21_f.TMST = TRUE;
1392  /* Transmit to VBAT domain and wait to complete transmission */
1393  enResult = RtcTransToVbat(RTC_WTCR20_CWRITE);
1394  if (Ok != enResult)
1395  {
1396  /* Retrieve WTCR21 */
1397  FM4_RTC->WTCR21_f = stcWtcr21;
1398  /* Transmit to VBAT domain and wait to complete transmission */
1400  }
1401 
1402  return (enResult);
1403 } /* Rtc_TimerStart */
1404 
1419 {
1420  en_result_t enResult;
1421  stc_rtc_wtcr21_field_t stcWtcr21;
1422 
1423  /* Back up WTCR21 */
1424  stcWtcr21 = FM4_RTC->WTCR21_f;
1425  /* Stop the timer */
1426  FM4_RTC->WTCR21_f.TMST = FALSE;
1427  /* Transmit to VBAT domain and wait to complete transmission */
1428  enResult = RtcTransToVbat(RTC_WTCR20_CWRITE);
1429  if (Ok != enResult)
1430  {
1431  /* Retrieve WTCR21 */
1432  FM4_RTC->WTCR21_f = stcWtcr21;
1433  /* Transmit to VBAT domain and wait to complete transmission */
1435  }
1436 
1437  return (enResult);
1438 } /* Rtc_TimerStop */
1439 
1451 {
1452  stc_rtc_wtcr21_field_t stcWtcr21;
1453  en_rtc_timer_status_t enStatus;
1454 
1455  stcWtcr21 = FM4_RTC->WTCR21_f;
1456 
1457  if (FALSE == stcWtcr21.TMRUN)
1458  {
1459  enStatus = RtcTimerNoOperation;
1460  }
1461  else
1462  {
1463  enStatus = RtcTimerInOperation;
1464  }
1465 
1466  return (enStatus);
1467 } /* Rtc_TimerStatusRead */
1468 
1480 {
1481  en_rtc_trans_status_t enStatus;
1482 
1483  if (FALSE == FM4_RTC->WTCR10_f.TRANS)
1484  {
1485  enStatus = RtcTransNoOperation;
1486  }
1487  else
1488  {
1489  enStatus = RtcTransInOperation;
1490  }
1491 
1492  return (enStatus);
1493 } /* Rtc_TransStatusRead */
1494 
1506 {
1507  en_result_t enResult;
1508 
1509  /* Wait to complete transmission */
1510  enResult = RtcWaitTransComplete();
1511  if (Ok == enResult)
1512  {
1513  RtcDeInitIrq();
1514 
1515  /* Set all registers of RTC instance to '0' */
1516  FM4_RTC->WTCR10 = 0;
1517  FM4_RTC->WTCR11 = 0;
1518  FM4_RTC->WTCR12 = 0;
1519  FM4_RTC->WTCR13 = 0;
1520  FM4_RTC->WTCR20 = 0;
1521  FM4_RTC->WTCR21 = 0;
1522  FM4_RTC->WTSR = 0;
1523  FM4_RTC->WTMIR = 0;
1524  FM4_RTC->WTHR = 0;
1525  FM4_RTC->WTDR = 0;
1526  FM4_RTC->WTDW = 0;
1527  FM4_RTC->WTMOR = 0;
1528  FM4_RTC->WTYR = 0;
1529  FM4_RTC->ALMIR = 0;
1530  FM4_RTC->ALHR = 0;
1531  FM4_RTC->ALDR = 0;
1532  FM4_RTC->ALMOR = 0;
1533  FM4_RTC->ALYR = 0;
1534  /* Transmit to VBAT domain and wait to complete transmission */
1535  enResult = RtcTransToVbat(RTC_WTCR20_CWRITE);
1536  if (Ok == enResult)
1537  {
1538  /* Set all registers of RTCCLK instance to '0' */
1539  FM4_RTC->WTCAL0 = 0;
1540  FM4_RTC->WTCAL1 = 0;
1541  FM4_RTC->WTCALEN = 0;
1542  FM4_RTC->WTDIV = 0;
1543  FM4_RTC->WTDIVEN = 0;
1544  FM4_RTC->WTCALPRD = 0;
1545  FM4_RTC->WTCOSEL = 0;
1546  /* Transmit to VBAT domain and wait to complete transmission */
1547  enResult = RtcTransToVbat(RTC_WTCR20_PWRITE);
1548  }
1549  }
1550 
1551  return (enResult);
1552 } /* Rtc_DeInit */
1553 
1565 time_t Rtc_GetRawTime(stc_rtc_time_t* pstcRtcTime)
1566 {
1567  time_t uiTime;
1568  struct tm stcTime;
1569 
1570  /* Check for NULL pointer */
1571  if (NULL == pstcRtcTime)
1572  {
1573  uiTime = (time_t)RTC_ERR;
1574  }
1575  else
1576  {
1577  /* Convert RTC time structure to time.h tm structure */
1578  RtcToTm(pstcRtcTime, &stcTime);
1579  uiTime = mktime(&stcTime);
1580  }
1581 
1582  /* Return raw time (UNIX time) */
1583  return (uiTime);
1584 } /* Rtc_GetRawTime */
1585 
1600 {
1601  en_result_t enResult;
1602  struct tm stcTime;
1603 
1604  enResult = ErrorInvalidParameter;
1605 
1606  /* Check for NULL pointer */
1607  if (NULL != pstcRtcTime)
1608  {
1609  enResult = Ok;
1610  /* Convert RTC time structure to time.h tm structure */
1611  RtcToTm(pstcRtcTime, &stcTime);
1612 
1613  /* Caluculated raw time (UNIX time) is error */
1614  if ((time_t)RTC_ERR == mktime(&stcTime))
1615  {
1616  enResult = ErrorInvalidParameter;
1617  }
1618 
1619  if (Ok == enResult)
1620  {
1621  /* Set caluculated the day of week */
1622  pstcRtcTime->u8DayOfWeek = (uint8_t)(stcTime.tm_wday);
1623  }
1624  }
1625 
1626  return (enResult);
1627 } /* Rtc_SetDayOfWeek */
1628 
1644  time_t tRawTime
1645  )
1646 {
1647  en_result_t enResult;
1648  struct tm* pstcTime;
1649 
1650  enResult = ErrorInvalidParameter;
1651 
1652  /* Check for NULL pointer */
1653  if (NULL != pstcRtcTime)
1654  {
1655  /* Get the pointer which converted to RTC time structure from raw time */
1656  pstcTime = localtime((const time_t*) &tRawTime);
1657 
1658  /* Un-success */
1659  if (NULL == pstcTime)
1660  {
1661  enResult = ErrorInvalidParameter;
1662  }
1663  else
1664  {
1665  pstcRtcTime->u8Year = (uint8_t)((pstcTime->tm_year + RTC_YEAR_1900) - RTC_YEAR_2000);
1666  pstcRtcTime->u8Month = (uint8_t)(pstcTime->tm_mon + 1);
1667  pstcRtcTime->u8Day = (uint8_t)(pstcTime->tm_mday);
1668  pstcRtcTime->u8Second = (uint8_t)(pstcTime->tm_sec);
1669  pstcRtcTime->u8Minute = (uint8_t)(pstcTime->tm_min);
1670  pstcRtcTime->u8Hour = (uint8_t)(pstcTime->tm_hour);
1671  pstcRtcTime->u8DayOfWeek = (uint8_t)(pstcTime->tm_wday);
1672  enResult = Ok;
1673  }
1674  }
1675 
1676  return (enResult);
1677 } /* Rtc_SetTime */
1678 
1694  uint8_t u8Area
1695  )
1696 {
1697  volatile uint8_t* pu8BkupRegAddr;
1698  en_result_t enResult;
1699 
1700  enResult = ErrorInvalidParameter;
1701  /* Check for valid pointer and register area */
1702  if (RtcBkupRegAreaMax > u8Area)
1703  {
1704  /* Wait to complete transmission */
1705  enResult = RtcWaitTransComplete();
1706  if (Ok == enResult)
1707  {
1708  pu8BkupRegAddr = (uint8_t *)(RTC_BKUP_REG_BASE + u8Area);
1709 
1710  *pu8BkupRegAddr = u8Data;
1711 
1712  /* Transmit to VBAT domain and wait to complete transmission */
1713  enResult = RtcTransToVbat(RTC_WTCR20_BWRITE);
1714  }
1715  }
1716 
1717  return (enResult);
1718 } /* Rtc_WriteBkup8 */
1719 
1736  uint8_t u8Area
1737  )
1738 {
1739  volatile uint16_t* pu16BkupRegAddr;
1740  en_result_t enResult;
1741 
1742  enResult = ErrorInvalidParameter;
1743 
1744  /* Check for valid pointer and register area */
1745  if ((RtcBkupRegAreaMax > u8Area)
1746  /* Check alignment */
1747  && (0 == (u8Area % RTC_2BYTES))
1748  )
1749  {
1750  /* Wait to complete transmission */
1751  enResult = RtcWaitTransComplete();
1752  if (Ok == enResult)
1753  {
1754  pu16BkupRegAddr = (uint16_t *)((uint8_t *)(RTC_BKUP_REG_BASE + u8Area));
1755 
1756  *pu16BkupRegAddr = u16Data;
1757 
1758  /* Transmit to VBAT domain and wait to complete transmission */
1759  enResult = RtcTransToVbat(RTC_WTCR20_BWRITE);
1760  }
1761  }
1762 
1763  return (enResult);
1764 } /* Rtc_WriteBkup16 */
1765 
1782  uint8_t u8Area
1783  )
1784 {
1785  volatile uint32_t* pu32BkupRegAddr;
1786  en_result_t enResult;
1787 
1788  enResult = ErrorInvalidParameter;
1789 
1790  /* Check for valid pointer and register area */
1791  if ((RtcBkupRegAreaMax > u8Area)
1792  /* Check alignment */
1793  && (0 == (u8Area % RTC_4BYTES))
1794  )
1795  {
1796  /* Wait to complete transmission */
1797  enResult = RtcWaitTransComplete();
1798  if (Ok == enResult)
1799  {
1800  pu32BkupRegAddr = (uint32_t *)((uint8_t *)(RTC_BKUP_REG_BASE + u8Area));
1801 
1802  *pu32BkupRegAddr = u32Data;
1803 
1804  /* Transmit to VBAT domain and wait to complete transmission */
1805  enResult = RtcTransToVbat(RTC_WTCR20_BWRITE);
1806  }
1807  }
1808 
1809  return (enResult);
1810 } /* Rtc_WriteBkup32 */
1811 
1824 uint8_t Rtc_ReadBkupReg8(uint8_t u8Area)
1825 {
1826  uint8_t u8RetVal = 0;
1827  volatile uint8_t* pu8BkupRegAddr;
1828 
1829  /* Check for valid pointer and register area */
1830  if (RtcBkupRegAreaMax > u8Area)
1831  {
1832  /* Wait to complete transmission */
1833  if (Ok == RtcWaitTransComplete())
1834  {
1835  /* Transmit from VBAT domain and wait to complete transmission */
1837  {
1838  pu8BkupRegAddr = (uint8_t *)(RTC_BKUP_REG_BASE + u8Area);
1839  u8RetVal = *pu8BkupRegAddr;
1840  }
1841  }
1842  }
1843 
1844  return (u8RetVal);
1845 } /* Rtc_WriteBkup8 */
1846 
1859 uint16_t Rtc_ReadBkupReg16(uint8_t u8Area)
1860 {
1861  uint16_t u16RetVal = 0;
1862  volatile uint16_t* pu16BkupRegAddr;
1863 
1864  /* Check for valid pointer and register area */
1865  if ((RtcBkupRegAreaMax > u8Area)
1866  /* Check alignment */
1867  && (0 == (u8Area % RTC_2BYTES))
1868  )
1869  {
1870  /* Wait to complete transmission */
1871  if (Ok == RtcWaitTransComplete())
1872  {
1873  /* Transmit from VBAT domain and wait to complete transmission */
1875  {
1876  pu16BkupRegAddr = (uint16_t *)((uint8_t *)(RTC_BKUP_REG_BASE + u8Area));
1877  u16RetVal = *pu16BkupRegAddr;
1878  }
1879  }
1880  }
1881 
1882  return (u16RetVal);
1883 } /* Rtc_WriteBkup16 */
1884 
1897 uint32_t Rtc_ReadBkupReg32(uint8_t u8Area)
1898 {
1899  uint32_t u32RetVal = 0;
1900  volatile uint32_t* pu32BkupRegAddr;
1901 
1902  /* Check for valid pointer and register area */
1903  if ((RtcBkupRegAreaMax > u8Area)
1904  /* Check alignment */
1905  && (0 == (u8Area % RTC_4BYTES))
1906  )
1907  {
1908  /* Wait to complete transmission */
1909  if (Ok == RtcWaitTransComplete())
1910  {
1911  /* Transmit from VBAT domain and wait to complete transmission */
1913  {
1914  pu32BkupRegAddr = (uint32_t *)((uint8_t *)(RTC_BKUP_REG_BASE + u8Area));
1915  u32RetVal = *pu32BkupRegAddr;
1916  }
1917  }
1918  }
1919 
1920  return (u32RetVal);
1921 } /* Rtc_WriteBkup32 */
1922 
1924 
1925 #endif /* #if (defined(PDL_PERIPHERAL_RTC_ACTIVE)) */
1926 /******************************************************************************/
1927 /* EOF (not truncated) */
1928 /******************************************************************************/