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