1 /******************************************************************************
2 * @brief    Real-ETMe counter (RTC) driver source code.
3 *
4 ******************************************************************************/
5 #include "common.h"
6 #include "rtc.h"
7 
8 /******************************************************************************
9 * Global variables
10 ******************************************************************************/
11 
12 /******************************************************************************
13 * Constants and macros
14 ******************************************************************************/
15 
16 /******************************************************************************
17 * Local types
18 ******************************************************************************/
19 
20 /******************************************************************************
21 * Local function prototypes
22 ******************************************************************************/
23 
24 /******************************************************************************
25 * Local variables
26 ******************************************************************************/
27 /*!
28  * @brief global variable to store RTC callbacks.
29  *
30  */
31 RTC_CallbackType RTC_Callback[1] = {(RTC_CallbackType)NULL};    /*!< RTC initial callback */
32 
33 /******************************************************************************
34 * Local functions
35 ******************************************************************************/
36 void RTC_Isr(void);
37 
38 /******************************************************************************
39 * Global functions
40 ******************************************************************************/
41 
42 /******************************************************************************
43 * define RTC APIs
44 *
45 *//*! @addtogroup rtc_api_list
46 * @{
47 *******************************************************************************/
48 
49 /*****************************************************************************//*!
50 *
51 * @brief inital RTC module
52 *
53 * @param[in] pConfig point to configuration
54 *
55 * @return none
56 *
57 * @ Pass/ Fail criteria: none
58 *****************************************************************************/
RTC_Init(RTC_ConfigType * pConfig)59 void RTC_Init(RTC_ConfigType *pConfig)
60 {
61     uint16_t    u16Clocksource, u16Prescler;
62     uint16_t    u16ModVal;
63 
64     u16Clocksource =0;
65     u16Prescler    =0;
66     u16ModVal      =0;
67 
68     SIM->SCGC     |= SIM_SCGC_RTC_MASK;
69 
70     u16ModVal      = pConfig->u16ModuloValue;
71     RTC_SetModulo(u16ModVal);
72 
73     if (pConfig->bRTCOut)
74     {
75 
76         RTC->SC= RTC_SC_RTCO_MASK;
77     }
78 
79     if (pConfig->bInterruptEn)
80     {
81          NVIC_EnableIRQ(RTC_IRQn);
82          RTC_EnableInt();
83     }
84     else
85     {
86         NVIC_DisableIRQ(RTC_IRQn);
87     }
88 
89     if (pConfig->bFlag)
90     {
91         RTC_ClrFlags();
92     }
93 
94     u16Clocksource = pConfig->bClockSource;
95     u16Prescler    = pConfig->bClockPresaler;
96 
97     RTC_SetClock(u16Clocksource,u16Prescler );
98 }
99 
100 
101 
102 /*****************************************************************************//*!
103 *
104 * @brief set call back function for rtc module
105 *
106 * @param[in] pfnCallback point to call back function
107 *
108 * @return none
109 *
110 * @ Pass/ Fail criteria: none
111 *****************************************************************************/
RTC_SetCallback(RTC_CallbackType pfnCallback)112 void RTC_SetCallback(RTC_CallbackType pfnCallback)
113 {
114   RTC_Callback[0] = pfnCallback;
115 }
116 
117 
118 /*****************************************************************************//*!
119 *
120 * @brief de-initialize rtc module , reset rtc register
121 *
122 * @param none
123 *
124 * @return none
125 *
126 * @ Pass/ Fail criteria: none
127 *****************************************************************************/
RTC_DeInit(void)128 void RTC_DeInit(void)
129 {
130     NVIC_DisableIRQ(RTC_IRQn);
131     RTC->MOD = 0;
132     while(RTC->MOD);
133 
134     if(RTC_GetFlags())
135     {
136         RTC_ClrFlags();
137     }
138 
139     RTC->SC = 0;
140     while(RTC->SC);
141     SIM->SCGC &= ~SIM_SCGC_RTC_MASK;
142 }
143 
144 /*! @} End of rtc_api_list                                                   */
145 
146 /*****************************************************************************//*!
147 *
148 * @brief RTC module interrupt service routine
149 *
150 * @param none
151 *
152 * @return none
153 *
154 * @ Pass/ Fail criteria: none
155 *****************************************************************************/
RTC_Isr(void)156 void RTC_Isr(void)
157 {
158     RTC_ClrFlags();
159     if (RTC_Callback[0])
160     {
161         RTC_Callback[0]();
162     }
163 }
164 
165 
166