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