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