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