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