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