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