1 ////////////////////////////////////////////////////////////////////////////////
2 /// @file     hal_rtc.c
3 /// @author   AE TEAM
4 /// @brief    THIS FILE PROVIDES ALL THE RTC FIRMWARE FUNCTIONS.
5 ////////////////////////////////////////////////////////////////////////////////
6 /// @attention
7 ///
8 /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
9 /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
10 /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
11 /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
12 /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
13 /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
14 ///
15 /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
16 ////////////////////////////////////////////////////////////////////////////////
17 
18 // Define to prevent recursive inclusion
19 #define _HAL_RTC_C_
20 
21 // Files includes
22 #include "hal_rtc.h"
23 
24 ////////////////////////////////////////////////////////////////////////////////
25 /// @addtogroup MM32_Hardware_Abstract_Layer
26 /// @{
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 /// @addtogroup RTC_HAL
30 /// @{
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 /// @addtogroup RTC_Exported_Functions
34 /// @{
35 
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// @brief  Enables or disables the specified RTC interrupts.
39 /// @param  it: specifies the RTC interrupts sources to be enabled or
40 ///         disabled.
41 ///         This parameter can be any combination of the following values:
42 /// @arg    RTC_IT_OW: Overflow interrupt
43 /// @arg    RTC_IT_ALR: Alarm interrupt
44 /// @arg    RTC_IT_SEC: Second interrupt
45 /// @param  state: new state of the specified RTC interrupts.
46 ///         This parameter can be: ENABLE or DISABLE.
47 /// @retval None.
48 ////////////////////////////////////////////////////////////////////////////////
RTC_ITConfig(RTC_IT_TypeDef it,FunctionalState state)49 void RTC_ITConfig(RTC_IT_TypeDef it, FunctionalState state)
50 {
51     (state == ENABLE) ? (RTC->CR |= it) : (RTC->CR &= (u16)~it);
52 //    RTC_WaitForLastTask();
53 }
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// @brief  Enters the RTC configuration mode.
57 /// @param  None.
58 /// @retval None.
59 ////////////////////////////////////////////////////////////////////////////////
RTC_EnterConfigMode(void)60 void RTC_EnterConfigMode(void)
61 {
62 //    PWR->CR |= PWR_CR_DBP;
63     RTC->CSR |= RTC_CSR_CNF;
64 }
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 /// @brief  Exits from the RTC configuration mode.
68 /// @param  None.
69 /// @retval None.
70 ////////////////////////////////////////////////////////////////////////////////
RTC_ExitConfigMode(void)71 void RTC_ExitConfigMode(void)
72 {
73     RTC->CSR &= ~RTC_CSR_CNF;
74     while (!(RTC->CSR & RTC_CSR_RTOFF));
75 //    PWR->CR  &= ~PWR_CR_DBP;
76 }
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// @brief  Gets the RTC counter value.
80 /// @param  None.
81 /// @retval RTC counter value.
82 ////////////////////////////////////////////////////////////////////////////////
RTC_GetCounter(void)83 u32 RTC_GetCounter(void)
84 {
85     u32 dat = RTC->CNTH << 16;
86     return (RTC->CNTL | dat);
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// @brief  Sets the RTC counter value.
91 /// @param  count: RTC counter new value.
92 /// @retval None.
93 ////////////////////////////////////////////////////////////////////////////////
RTC_SetCounter(u32 count)94 void RTC_SetCounter(u32 count)
95 {
96     RTC_EnterConfigMode();//RTC->CSR |= RTC_CSR_CNF;
97     RTC->CNTH = count >> 16;
98     RTC->CNTL = count;
99     RTC_ExitConfigMode();//RTC->CSR &= ~RTC_CSR_CNF;
100 //    while (!(RTC->CSR & RTC_CSR_RTOFF));
101 }
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// @brief  Sets the RTC prescaler value.
105 /// @param  prescaler: RTC prescaler new value.
106 /// @retval None.
107 ////////////////////////////////////////////////////////////////////////////////
RTC_SetPrescaler(u32 prescaler)108 void RTC_SetPrescaler(u32 prescaler)
109 {
110     RTC_EnterConfigMode();//RTC->CSR |= RTC_CSR_CNF;
111     RTC->PRLH = prescaler >> 16;
112     RTC->PRLL = prescaler;
113     RTC_ExitConfigMode();//RTC->CSR &= ~RTC_CSR_CNF;
114 //    while (!(RTC->CSR & RTC_CSR_RTOFF));
115 }
116 
117 ////////////////////////////////////////////////////////////////////////////////
118 /// @brief  Sets the RTC alarm value.
119 /// @param  alarm: RTC alarm new value.
120 /// @retval None.
121 ////////////////////////////////////////////////////////////////////////////////
RTC_SetAlarm(u32 alarm)122 void RTC_SetAlarm(u32 alarm)
123 {
124     RTC_EnterConfigMode();//RTC->CSR |= RTC_CSR_CNF;
125     RTC->ALRH = alarm >> 16;
126     RTC->ALRL = alarm;
127     RTC_ExitConfigMode();//RTC->CSR &= ~RTC_CSR_CNF;
128 //    while (!(RTC->CSR & RTC_CSR_RTOFF));
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// @brief  Gets the RTC divider value.
133 /// @param  None.
134 /// @retval RTC Divider value.
135 ////////////////////////////////////////////////////////////////////////////////
RTC_GetDivider(void)136 u32 RTC_GetDivider(void)
137 {
138     u32 dat = ((u32)(RTC->DIVH & RTC_DIVH_DIV) << 16);
139     return (RTC->DIVL | dat);
140 }
141 
142 ////////////////////////////////////////////////////////////////////////////////
143 /// @brief  Waits until last write operation on RTC registers has finished.
144 /// @note   This function must be called before any write to RTC registers.
145 /// @param  None.
146 /// @retval None.
147 ////////////////////////////////////////////////////////////////////////////////
RTC_WaitForLastTask(void)148 void RTC_WaitForLastTask(void)
149 {
150     while (!(RTC->CSR & RTC_CSR_RTOFF));
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// @brief  Waits until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL)
155 ///         are synchronized with RTC APB clock.
156 /// @note   This function must be called before any read operation after an APB
157 ///         reset or an APB clock stop.
158 /// @param  None.
159 /// @retval None.
160 ////////////////////////////////////////////////////////////////////////////////
RTC_WaitForSynchro(void)161 void RTC_WaitForSynchro(void)
162 {
163     RTC->CSR &= ~RTC_CSR_RSF;
164     while (!(RTC->CSR & RTC_CSR_RSF));
165 }
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// @brief  Checks whether the specified RTC flag is set or not.
169 /// @param  flag: specifies the flag to check.
170 ///         This parameter can be one the following values:
171 /// @arg    RTC_FLAG_RTOFF: RTC Operation OFF flag
172 /// @arg    RTC_FLAG_RSF: Registers Synchronized flag
173 /// @arg    RTC_FLAG_OW: Overflow flag
174 /// @arg    RTC_FLAG_ALR: Alarm flag
175 /// @arg    RTC_FLAG_SEC: Second flag
176 /// @retval The state of RTC_FLAG (SET or RESET).
177 /////////////////////////////////////////////////////////////////////////////////
RTC_GetFlagStatus(RTC_FLAG_TypeDef flag)178 FlagStatus RTC_GetFlagStatus(RTC_FLAG_TypeDef flag)
179 {
180     return  (FlagStatus)(RTC->CSR & flag);
181 }
182 
183 ////////////////////////////////////////////////////////////////////////////////
184 /// @brief  Clears the RTC's pending flags.
185 /// @param  flag: specifies the flag to clear.
186 ///         This parameter can be any combination of the following values:
187 /// @arg    RTC_FLAG_RSF: Registers Synchronized flag. This flag is cleared only
188 ///         after an APB reset or an APB Clock stop.
189 /// @arg    RTC_FLAG_OW: Overflow flag
190 /// @arg    RTC_FLAG_ALR: Alarm flag
191 /// @arg    RTC_FLAG_SEC: Second flag
192 /// @retval None.
193 ////////////////////////////////////////////////////////////////////////////////
RTC_ClearFlag(RTC_FLAG_TypeDef flag)194 void RTC_ClearFlag(RTC_FLAG_TypeDef flag)
195 {
196     RTC->CSR &= ~flag;
197 }
198 
199 ////////////////////////////////////////////////////////////////////////////////
200 /// @brief  Checks whether the specified RTC interrupt has occurred or not.
201 /// @param  it: specifies the RTC interrupts sources to check.
202 ///         This parameter can be one of the following values:
203 /// @arg    RTC_IT_OW: Overflow interrupt
204 /// @arg    RTC_IT_ALR: Alarm interrupt
205 /// @arg    RTC_IT_SEC: Second interrupt
206 /// @retval The state of the RTC_IT (SET or RESET).
207 ////////////////////////////////////////////////////////////////////////////////
RTC_GetITStatus(RTC_IT_TypeDef it)208 ITStatus RTC_GetITStatus(RTC_IT_TypeDef it)
209 {
210     return  (ITStatus)(RTC->CSR & it);
211 }
212 
213 ////////////////////////////////////////////////////////////////////////////////
214 /// @brief  Clears the RTC's interrupt pending bits.
215 /// @param  it: specifies the interrupt pending bit to clear.
216 ///         This parameter can be any combination of the following values:
217 /// @arg    RTC_IT_OW: Overflow interrupt
218 /// @arg    RTC_IT_ALR: Alarm interrupt
219 /// @arg    RTC_IT_SEC: Second interrupt
220 /// @retval None.
221 ////////////////////////////////////////////////////////////////////////////////
RTC_ClearITPendingBit(RTC_IT_TypeDef it)222 void RTC_ClearITPendingBit(RTC_IT_TypeDef it)
223 {
224 //    RTC_EnterConfigMode();//RTC->CSR |= RTC_CSR_CNF;
225     RTC->CSR &= ~it;
226 //    RTC_ExitConfigMode();//RTC->CSR &= ~RTC_CSR_CNF;
227 }
228 
229 
230 /// @}
231 
232 /// @}
233 
234 /// @}
235