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
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
20 /* Includes ------------------------------------------------------------------*/
\r
21 #include "stm32f10x_rtc.h"
\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
31 /* Private macro -------------------------------------------------------------*/
\r
32 /* Private variables ---------------------------------------------------------*/
\r
33 /* Private function prototypes -----------------------------------------------*/
\r
34 /* Private functions ---------------------------------------------------------*/
\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
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
49 *******************************************************************************/
\r
50 void RTC_ITConfig(u16 RTC_IT, FunctionalState NewState)
\r
52 /* Check the parameters */
\r
53 assert(IS_RTC_IT(RTC_IT));
\r
54 assert(IS_FUNCTIONAL_STATE(NewState));
\r
56 if (NewState != DISABLE)
\r
62 RTC->CRH &= (u16)~RTC_IT;
\r
66 /*******************************************************************************
\r
67 * Function Name : RTC_EnterConfigMode
\r
68 * Description : Enters the RTC configuration mode.
\r
72 *******************************************************************************/
\r
73 void RTC_EnterConfigMode(void)
\r
75 /* Set the CNF flag to enter in the Configuration Mode */
\r
76 RTC->CRL |= CRL_CNF_Set;
\r
79 /*******************************************************************************
\r
80 * Function Name : RTC_ExitConfigMode
\r
81 * Description : Exits from the RTC configuration mode.
\r
85 *******************************************************************************/
\r
86 void RTC_ExitConfigMode(void)
\r
88 /* Reset the CNF flag to exit from the Configuration Mode */
\r
89 RTC->CRL &= CRL_CNF_Reset;
\r
92 /*******************************************************************************
\r
93 * Function Name : RTC_GetCounter
\r
94 * Description : Gets the RTC counter value.
\r
97 * Return : RTC counter value.
\r
98 *******************************************************************************/
\r
99 u32 RTC_GetCounter(void)
\r
104 return (((u32)RTC->CNTH << 16 ) | tmp) ;
\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
113 *******************************************************************************/
\r
114 void RTC_SetCounter(u32 CounterValue)
\r
116 RTC_EnterConfigMode();
\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
123 RTC_ExitConfigMode();
\r
126 /*******************************************************************************
\r
127 * Function Name : RTC_GetPrescaler
\r
128 * Description : Gets the RTC prescaler value.
\r
131 * Return : RTC prescaler value.
\r
132 *******************************************************************************/
\r
133 u32 RTC_GetPrescaler(void)
\r
137 tmp = ((u32)RTC->PRLH & (u32)0x000F) << 0x10;
\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
149 *******************************************************************************/
\r
150 void RTC_SetPrescaler(u32 PrescalerValue)
\r
152 /* Check the parameters */
\r
153 assert(IS_RTC_PRESCALER(PrescalerValue));
\r
155 RTC_EnterConfigMode();
\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
162 RTC_ExitConfigMode();
\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
171 *******************************************************************************/
\r
172 void RTC_SetAlarm(u32 AlarmValue)
\r
174 RTC_EnterConfigMode();
\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
181 RTC_ExitConfigMode();
\r
184 /*******************************************************************************
\r
185 * Function Name : RTC_GetDivider
\r
186 * Description : Gets the RTC divider value.
\r
189 * Return : RTC Divider value.
\r
190 *******************************************************************************/
\r
191 u32 RTC_GetDivider(void)
\r
195 tmp = ((u32)RTC->DIVH & (u32)0x000F) << 0x10;
\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
208 *******************************************************************************/
\r
209 void RTC_WaitForLastTask(void)
\r
211 /* Loop until RTOFF flag is set */
\r
212 while ((RTC->CRL & RTC_FLAG_RTOFF) == (u16)RESET)
\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
226 *******************************************************************************/
\r
227 void RTC_WaitForSynchro(void)
\r
229 /* Clear RSF flag */
\r
230 RTC->CRL &= (u16)~RTC_FLAG_RSF;
\r
232 /* Loop until RSF flag is set */
\r
233 while ((RTC->CRL & RTC_FLAG_RSF) == (u16)RESET)
\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
249 * Return : The new state of RTC_FLAG (SET or RESET).
\r
250 *******************************************************************************/
\r
251 FlagStatus RTC_GetFlagStatus(u16 RTC_FLAG)
\r
253 FlagStatus bitstatus = RESET;
\r
255 /* Check the parameters */
\r
256 assert(IS_RTC_GET_FLAG(RTC_FLAG));
\r
258 if ((RTC->CRL & RTC_FLAG) != (u16)RESET)
\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
281 *******************************************************************************/
\r
282 void RTC_ClearFlag(u16 RTC_FLAG)
\r
284 /* Check the parameters */
\r
285 assert(IS_RTC_CLEAR_FLAG(RTC_FLAG));
\r
287 /* Clear the coressponding RTC flag */
\r
288 RTC->CRL &= (u16)~RTC_FLAG;
\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
300 * Return : The new state of the RTC_IT (SET or RESET).
\r
301 *******************************************************************************/
\r
302 ITStatus RTC_GetITStatus(u16 RTC_IT)
\r
304 ITStatus bitstatus = RESET;
\r
306 /* Check the parameters */
\r
307 assert(IS_RTC_GET_IT(RTC_IT));
\r
309 bitstatus = (ITStatus)((RTC->CRL & RTC_IT) != (u16)RESET);
\r
311 if (((RTC->CRH & RTC_IT) != (u16)RESET) && bitstatus)
\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
332 *******************************************************************************/
\r
333 void RTC_ClearITPendingBit(u16 RTC_IT)
\r
335 /* Check the parameters */
\r
336 assert(IS_RTC_IT(RTC_IT));
\r
338 /* Clear the coressponding RTC pending bit */
\r
339 RTC->CRL &= (u16)~RTC_IT;
\r
342 /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
\r