]> begriffs open source - cmsis-freertos/blob - Demo/ARM7_STR75x_IAR/STLibrary/src/75x_rtc.c
Initial commit
[cmsis-freertos] / Demo / ARM7_STR75x_IAR / STLibrary / src / 75x_rtc.c
1 /******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
2 * File Name          : 75x_rtc.c
3 * Author             : MCD Application Team
4 * Date First Issued  : 03/10/2006
5 * Description        : This file provides all the RTC software functions.
6 ********************************************************************************
7 * History:
8 * 07/17/2006 : V1.0
9 * 03/10/2006 : V0.1
10 ********************************************************************************
11 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
13 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
14 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
15 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
16 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *******************************************************************************/
18
19 /* Includes ------------------------------------------------------------------*/
20 #include "75x_rtc.h"
21 #include "75x_mrcc.h"
22
23 /* Private typedef -----------------------------------------------------------*/
24 /* Private define ------------------------------------------------------------*/
25 #define RTC_CNF_Enable_Mask      0x0010      /* Configuration Flag Enable Mask */
26 #define RTC_CNF_Disable_Mask     0xFFEF      /* Configuration Flag Disable Mask */
27 #define RTC_LSB_Mask             0x0000FFFF  /* RTC LSB Mask */
28 #define RTC_MSB_Mask             0xFFFF0000  /* RTC MSB Mask */
29 #define RTC_Prescaler_MSB_Mask   0x000F0000  /* RTC Prescaler MSB Mask */
30
31 /* Private macro -------------------------------------------------------------*/
32 /* Private variables ---------------------------------------------------------*/
33 /* Private function prototypes -----------------------------------------------*/
34 /* Private functions ---------------------------------------------------------*/
35 /*******************************************************************************
36 * Function Name  : RTC_DeInit
37 * Description    : Deinitializes the RTC peripheral registers to their
38 *                  default reset values.
39 * Input          : None
40 * Output         : None
41 * Return         : None
42 *******************************************************************************/
43 void RTC_DeInit(void)
44 {
45   MRCC_PeripheralSWResetConfig(MRCC_Peripheral_RTC,ENABLE);
46   MRCC_PeripheralSWResetConfig(MRCC_Peripheral_RTC,DISABLE);
47 }
48
49 /*******************************************************************************
50 * Function Name  : RTC_ITConfig
51 * Description    : Enables or disables the specified RTC interrupts.
52 * Input          : - RTC_IT: specifies the RTC interrupts sources to be enabled
53 *                    or disabled.
54 *                    This parameter can be a combination of one or more of the
55 *                    following values:
56 *                       - RTC_IT_Overflow: Overflow interrupt
57 *                       - RTC_IT_Alarm: Alarm interrupt
58 *                       - RTC_IT_Second: Second interrupt
59 *                 - NewState: new state of the specified RTC interrupts.
60 *                   This parameter can be: ENABLE or DISABLE.
61 * Output         : None
62 * Return         : None
63 *******************************************************************************/
64 void RTC_ITConfig(u16 RTC_IT, FunctionalState NewState)
65 {
66   if(NewState == ENABLE)
67   {
68     RTC->CRH |= RTC_IT;
69   }
70   else
71   {
72     RTC->CRH &= ~RTC_IT;
73   }
74 }
75
76 /*******************************************************************************
77 * Function Name  : RTC_EnterConfigMode
78 * Description    : Enters the RTC configuration mode.
79 * Input          : None
80 * Output         : None
81 * Return         : None
82 *******************************************************************************/
83 void RTC_EnterConfigMode(void)
84 {
85   /* Set the CNF flag to enter in the Configuration Mode */
86   RTC->CRL |= RTC_CNF_Enable_Mask;
87 }
88
89 /*******************************************************************************
90 * Function Name  : RTC_ExitConfigMode
91 * Description    : Exits from the RTC configuration mode.
92 * Input          : None
93 * Output         : None
94 * Return         : None
95 *******************************************************************************/
96 void RTC_ExitConfigMode(void)
97 {
98   /* Reset the CNF flag to exit from the Configuration Mode */
99   RTC->CRL &= RTC_CNF_Disable_Mask;
100 }
101
102 /*******************************************************************************
103 * Function Name  : RTC_GetCounter
104 * Description    : Gets the RTC counter value.
105 * Input          : None
106 * Output         : None
107 * Return         : RTC counter value.
108 *******************************************************************************/
109 u32 RTC_GetCounter(void)
110 {
111   u16 Tmp = 0;
112   Tmp = RTC->CNTL;
113   
114   return (((u32)RTC->CNTH << 16 ) |Tmp) ;
115 }
116
117 /*******************************************************************************
118 * Function Name  : RTC_SetCounter
119 * Description    : Sets the RTC counter value.
120 * Input          : RTC counter new value.
121 * Output         : None
122 * Return         : None
123 *******************************************************************************/
124 void RTC_SetCounter(u32 CounterValue)
125 {
126   RTC_EnterConfigMode();
127   
128 /* COUNTER Config ------------------------------------------------------------*/
129   /* Set RTC COUNTER MSB word */
130   RTC->CNTH =(CounterValue & RTC_MSB_Mask) >> 16;
131   /* Set RTC COUNTER LSB word */
132   RTC->CNTL =(CounterValue & RTC_LSB_Mask);
133   
134   RTC_ExitConfigMode();
135 }
136
137 /*******************************************************************************
138 * Function Name  : RTC_SetPrescaler
139 * Description    : Sets the RTC prescaler value.
140 * Input          : RTC prescaler new value.
141 * Output         : None
142 * Return         : None
143 *******************************************************************************/
144 void RTC_SetPrescaler(u32 PrescalerValue)
145 {
146   RTC_EnterConfigMode();
147   
148 /* PRESCALER Config ----------------------------------------------------------*/
149   /* Set RTC PRESCALER MSB word */
150   RTC->PRLH = (PrescalerValue & RTC_Prescaler_MSB_Mask) >> 16;
151   /* Set RTC PRESCALER LSB word */
152   RTC->PRLL = (PrescalerValue & RTC_LSB_Mask);
153   
154   RTC_ExitConfigMode();
155 }
156
157 /*******************************************************************************
158 * Function Name  : RTC_GetPrescaler
159 * Description    : Gets the RTC prescaler value.
160 * Input          : None
161 * Output         : None
162 * Return         : RTC prescaler value.
163 *******************************************************************************/
164 u32 RTC_GetPrescaler(void)
165 {
166   u16 Tmp = 0;
167   Tmp = RTC->PRLL;
168   
169   return (((u32)(RTC->PRLH & 0x000F) << 16 ) | Tmp);
170 }
171
172 /*******************************************************************************
173 * Function Name  : RTC_SetAlarm
174 * Description    : Sets the RTC alarm value.
175 * Input          : RTC alarm new value.
176 * Output         : None
177 * Return         : None
178 *******************************************************************************/
179 void RTC_SetAlarm(u32 AlarmValue)
180 {
181   RTC_EnterConfigMode();
182   
183 /* ALARM Config --------------------------------------------------------------*/
184   /* Set the ALARM MSB word */
185   RTC->ALRH = (AlarmValue & RTC_MSB_Mask) >> 16;
186   /* Set the ALARM LSB word */
187   RTC->ALRL = (AlarmValue & RTC_LSB_Mask);
188   
189   RTC_ExitConfigMode();
190 }
191
192 /*******************************************************************************
193 * Function Name  : RTC_GetDivider
194 * Description    : Gets the RTC divider value.
195 * Input          : None
196 * Output         : None
197 * Return         : RTC Divider value.
198 *******************************************************************************/
199 u32 RTC_GetDivider(void)
200 {
201   u16 Tmp = 0;
202   Tmp = RTC->DIVL ;
203   return (((u32)(RTC->DIVH & 0x000F) << 16 ) | Tmp);
204 }
205
206 /*******************************************************************************
207 * Function Name  : RTC_WaitForLastTask
208 * Description    : Waits until last write operation on RTC registers has finished.
209 *                  This function must be called before any write to RTC registers.
210 * Input          : None
211 * Output         : None
212 * Return         : None
213 *******************************************************************************/
214 void RTC_WaitForLastTask(void)
215 {
216   /* Loop until RTOFF flag is set */
217   while ((RTC->CRL & RTC_FLAG_RTOFF) == RESET);
218 }
219
220 /*******************************************************************************
221 * Function Name  : RTC_WaitForSynchro
222 * Description    : Waits until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL) 
223 *                  are synchronized with RTC APB clock.
224 *                  This function must be called before any read operation after 
225 *                  an APB reset or an APB clock stop.
226 * Input          : None
227 * Output         : None
228 * Return         : None
229 *******************************************************************************/
230 void RTC_WaitForSynchro(void)
231 {
232   /* Clear RSF flag */
233   RTC->CRL &= ~RTC_FLAG_RSF;
234   
235   /* Loop until RSF flag is set */
236   while((RTC->CRL & RTC_FLAG_RSF)== RESET);
237 }
238
239 /*******************************************************************************
240 * Function Name  : RTC_GetFlagStatus
241 * Description    : Checks whether the specified RTC flag is set or not.
242 * Input          : RTC_FLAG: specifies the flag to check.
243 *                  This parameter can be one the following values:
244 *                        - RTC_FLAG_RTOFF: RTC Operation OFF flag
245 *                        - RTC_FLAG_RSF: Registers Synchronized flag
246 *                        - RTC_FLAG_Overflow: Overflow interrupt flag
247 *                        - RTC_FLAG_Alarm: Alarm interrupt flag
248 *                        - RTC_FLAG_Second: Second interrupt flag
249 * Output         : None
250 * Return         : The new state of RTC_FLAG (SET or RESET).
251 *******************************************************************************/
252 FlagStatus RTC_GetFlagStatus(u16 RTC_FLAG)
253 {
254   if((RTC->CRL & RTC_FLAG) != RESET)
255   {
256     return SET;
257   }
258   else
259   {
260     return RESET;
261   }
262 }
263
264 /*******************************************************************************
265 * Function Name  : RTC_ClearFlag
266 * Description    : Clears the RTC\92s pending flags.
267 * Input          : RTC_FLAG: specifies the flag to clear.
268 *                    This parameter can be a combination of one or more of
269 *                    the following values:
270 *                        - RTC_FLAG_RSF: Registers Synchronized flag. This flag
271 *                          is cleared only after an APB reset or an APB Clock stop.
272 *                        - RTC_FLAG_Overflow: Overflow interrupt flag
273 *                        - RTC_FLAG_Alarm: Alarm interrupt flag
274 *                        - RTC_FLAG_Second: Second interrupt flag
275 * Output         : None
276 * Return         : None
277 *******************************************************************************/
278 void RTC_ClearFlag(u16 RTC_FLAG)
279 {
280   /* Clear the coressponding RTC flag */
281   RTC->CRL &= ~RTC_FLAG;
282 }
283
284 /*******************************************************************************
285 * Function Name  : RTC_GetITStatus
286 * Description    : Checks whether the specified RTC interrupt has occured or not.
287 * Input          : RTC_IT: specifies the RTC interrupts sources to check.
288 *                   This parameter can be a combination of one or more of
289 *                   the following values:
290 *                       - RTC_IT_Overflow: Overflow interrupt
291 *                       - RTC_IT_Alarm: Alarm interrupt
292 *                       - RTC_IT_Second: Second interrupt
293 * Output         : None
294 * Return         : The new state of the RTC_IT (SET or RESET).
295 *******************************************************************************/
296 ITStatus RTC_GetITStatus(u16 RTC_IT)
297 {
298   if(((RTC->CRH & RTC_IT) != RESET)&& ((RTC->CRL & RTC_IT) != RESET))
299   {
300     return SET;
301   }
302   else
303   {
304     return RESET;
305   }
306 }
307
308 /*******************************************************************************
309 * Function Name  : RTC_ClearITPendingBit
310 * Description    : Clears the RTC\92s interrupt pending bits.
311 * Input          : RTC_IT: specifies the interrupt pending bit to clear. 
312 *                   This parameter can be any combination of one or more of
313 *                   the following values:
314 *                       - RTC_IT_Overflow: Overflow interrupt
315 *                       - RTC_IT_Alarm: Alarm interrupt
316 *                       - RTC_IT_Second: Second interrupt
317 * Output         : None
318 * Return         : None
319 *******************************************************************************/
320 void RTC_ClearITPendingBit(u16 RTC_IT)
321 {
322   /* Clear the coressponding RTC pending bit */
323   RTC->CRL &= ~RTC_IT;
324 }
325
326 /******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/