2 ******************************************************************************
\r
3 * @file stm32f10x_rtc.c
\r
4 * @author MCD Application Team
\r
6 * @date 11-March-2011
\r
7 * @brief This file provides all the RTC firmware functions.
\r
8 ******************************************************************************
\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
18 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
\r
19 ******************************************************************************
\r
22 /* Includes ------------------------------------------------------------------*/
\r
23 #include "stm32f10x_rtc.h"
\r
25 /** @addtogroup STM32F10x_StdPeriph_Driver
\r
30 * @brief RTC driver modules
\r
34 /** @defgroup RTC_Private_TypesDefinitions
\r
41 /** @defgroup RTC_Private_Defines
\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
51 /** @defgroup RTC_Private_Macros
\r
59 /** @defgroup RTC_Private_Variables
\r
67 /** @defgroup RTC_Private_FunctionPrototypes
\r
75 /** @defgroup RTC_Private_Functions
\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
90 void RTC_ITConfig(uint16_t RTC_IT, FunctionalState NewState)
\r
92 /* Check the parameters */
\r
93 assert_param(IS_RTC_IT(RTC_IT));
\r
94 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
96 if (NewState != DISABLE)
\r
102 RTC->CRH &= (uint16_t)~RTC_IT;
\r
107 * @brief Enters the RTC configuration mode.
\r
111 void RTC_EnterConfigMode(void)
\r
113 /* Set the CNF flag to enter in the Configuration Mode */
\r
114 RTC->CRL |= RTC_CRL_CNF;
\r
118 * @brief Exits from the RTC configuration mode.
\r
122 void RTC_ExitConfigMode(void)
\r
124 /* Reset the CNF flag to exit from the Configuration Mode */
\r
125 RTC->CRL &= (uint16_t)~((uint16_t)RTC_CRL_CNF);
\r
129 * @brief Gets the RTC counter value.
\r
131 * @retval RTC counter value.
\r
133 uint32_t RTC_GetCounter(void)
\r
137 return (((uint32_t)RTC->CNTH << 16 ) | tmp) ;
\r
141 * @brief Sets the RTC counter value.
\r
142 * @param CounterValue: RTC counter new value.
\r
145 void RTC_SetCounter(uint32_t CounterValue)
\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
156 * @brief Sets the RTC prescaler value.
\r
157 * @param PrescalerValue: RTC prescaler new value.
\r
160 void RTC_SetPrescaler(uint32_t PrescalerValue)
\r
162 /* Check the parameters */
\r
163 assert_param(IS_RTC_PRESCALER(PrescalerValue));
\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
174 * @brief Sets the RTC alarm value.
\r
175 * @param AlarmValue: RTC alarm new value.
\r
178 void RTC_SetAlarm(uint32_t AlarmValue)
\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
189 * @brief Gets the RTC divider value.
\r
191 * @retval RTC Divider value.
\r
193 uint32_t RTC_GetDivider(void)
\r
195 uint32_t tmp = 0x00;
\r
196 tmp = ((uint32_t)RTC->DIVH & (uint32_t)0x000F) << 16;
\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
207 void RTC_WaitForLastTask(void)
\r
209 /* Loop until RTOFF flag is set */
\r
210 while ((RTC->CRL & RTC_FLAG_RTOFF) == (uint16_t)RESET)
\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
223 void RTC_WaitForSynchro(void)
\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
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
244 FlagStatus RTC_GetFlagStatus(uint16_t RTC_FLAG)
\r
246 FlagStatus bitstatus = RESET;
\r
248 /* Check the parameters */
\r
249 assert_param(IS_RTC_GET_FLAG(RTC_FLAG));
\r
251 if ((RTC->CRL & RTC_FLAG) != (uint16_t)RESET)
\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
273 void RTC_ClearFlag(uint16_t RTC_FLAG)
\r
275 /* Check the parameters */
\r
276 assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG));
\r
278 /* Clear the corresponding RTC flag */
\r
279 RTC->CRL &= (uint16_t)~RTC_FLAG;
\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
291 ITStatus RTC_GetITStatus(uint16_t RTC_IT)
\r
293 ITStatus bitstatus = RESET;
\r
294 /* Check the parameters */
\r
295 assert_param(IS_RTC_GET_IT(RTC_IT));
\r
297 bitstatus = (ITStatus)(RTC->CRL & RTC_IT);
\r
298 if (((RTC->CRH & RTC_IT) != (uint16_t)RESET) && (bitstatus != (uint16_t)RESET))
\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
318 void RTC_ClearITPendingBit(uint16_t RTC_IT)
\r
320 /* Check the parameters */
\r
321 assert_param(IS_RTC_IT(RTC_IT));
\r
323 /* Clear the corresponding RTC pending bit */
\r
324 RTC->CRL &= (uint16_t)~RTC_IT;
\r
339 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
\r