提交 | 用户 | age
|
483170
|
1 |
/** |
Q |
2 |
****************************************************************************** |
|
3 |
* @file stm32f0xx_hal_timebase_rtc_wakeup_template.c |
|
4 |
* @brief HAL time base based on the hardware RTC_WAKEUP Template. |
|
5 |
* |
|
6 |
* This file overrides the native HAL time base functions (defined as weak) |
|
7 |
* to use the RTC WAKEUP for the time base generation: |
|
8 |
* + Intializes the RTC peripheral and configures the wakeup timer to be |
|
9 |
* incremented each 1ms |
|
10 |
* + The wakeup feature is configured to assert an interrupt each 1ms |
|
11 |
* + HAL_IncTick is called inside the HAL_RTCEx_WakeUpTimerEventCallback |
|
12 |
* + HSE (default), LSE or LSI can be selected as RTC clock source |
|
13 |
@verbatim |
|
14 |
============================================================================== |
|
15 |
##### How to use this driver ##### |
|
16 |
============================================================================== |
|
17 |
[..] |
|
18 |
This file must be copied to the application folder and modified as follows: |
|
19 |
(#) Rename it to 'stm32f0xx_hal_timebase_rtc_wakeup.c' |
|
20 |
(#) Add this file and the RTC HAL drivers to your project and uncomment |
|
21 |
HAL_RTC_MODULE_ENABLED define in stm32f0xx_hal_conf.h |
|
22 |
|
|
23 |
[..] |
|
24 |
(@) HAL RTC alarm and HAL RTC wakeup drivers can抰 be used with low power modes: |
|
25 |
The wake up capability of the RTC may be intrusive in case of prior low power mode |
|
26 |
configuration requiring different wake up sources. |
|
27 |
Application/Example behavior is no more guaranteed |
|
28 |
(@) The stm32f0xx_hal_timebase_tim use is recommended for the Applications/Examples |
|
29 |
requiring low power modes |
|
30 |
|
|
31 |
@endverbatim |
|
32 |
****************************************************************************** |
|
33 |
* @attention |
|
34 |
* |
|
35 |
* <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
|
36 |
* |
|
37 |
* Redistribution and use in source and binary forms, with or without modification, |
|
38 |
* are permitted provided that the following conditions are met: |
|
39 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
40 |
* this list of conditions and the following disclaimer. |
|
41 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
42 |
* this list of conditions and the following disclaimer in the documentation |
|
43 |
* and/or other materials provided with the distribution. |
|
44 |
* 3. Neither the name of STMicroelectronics nor the names of its contributors |
|
45 |
* may be used to endorse or promote products derived from this software |
|
46 |
* without specific prior written permission. |
|
47 |
* |
|
48 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
49 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
50 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
51 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
|
52 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
53 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
54 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
55 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
56 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
57 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
58 |
* |
|
59 |
****************************************************************************** |
|
60 |
*/ |
|
61 |
|
|
62 |
/* Includes ------------------------------------------------------------------*/ |
|
63 |
#include "stm32f0xx_hal.h" |
|
64 |
/** @addtogroup STM32F0xx_HAL_Driver |
|
65 |
* @{ |
|
66 |
*/ |
|
67 |
|
|
68 |
/** @defgroup HAL_TimeBase_RTC_WakeUp_Template HAL TimeBase RTC WakeUp Template |
|
69 |
* @{ |
|
70 |
*/ |
|
71 |
|
|
72 |
/* Private typedef -----------------------------------------------------------*/ |
|
73 |
/* Private define ------------------------------------------------------------*/ |
|
74 |
|
|
75 |
/* Uncomment the line below to select the appropriate RTC Clock source for your application: |
|
76 |
+ RTC_CLOCK_SOURCE_HSE: can be selected for applications requiring timing precision. |
|
77 |
+ RTC_CLOCK_SOURCE_LSE: can be selected for applications with low constraint on timing |
|
78 |
precision. |
|
79 |
+ RTC_CLOCK_SOURCE_LSI: can be selected for applications with low constraint on timing |
|
80 |
precision. |
|
81 |
*/ |
|
82 |
#define RTC_CLOCK_SOURCE_HSE |
|
83 |
/* #define RTC_CLOCK_SOURCE_LSE */ |
|
84 |
/* #define RTC_CLOCK_SOURCE_LSI */ |
|
85 |
|
|
86 |
#if defined(RTC_CLOCK_SOURCE_HSE) |
|
87 |
#define RTC_ASYNCH_PREDIV 49U |
|
88 |
#define RTC_SYNCH_PREDIV 4U |
|
89 |
#elif defined(RTC_CLOCK_SOURCE_LSE) |
|
90 |
#define RTC_ASYNCH_PREDIV 0U |
|
91 |
#define RTC_SYNCH_PREDIV 31U |
|
92 |
#else /* CLOCK_SOURCE_LSI */ |
|
93 |
#define RTC_ASYNCH_PREDIV 0U |
|
94 |
#define RTC_SYNCH_PREDIV 39U |
|
95 |
#endif /* RTC_CLOCK_SOURCE_HSE */ |
|
96 |
|
|
97 |
/* Private macro -------------------------------------------------------------*/ |
|
98 |
/* Private variables ---------------------------------------------------------*/ |
|
99 |
RTC_HandleTypeDef hRTC_Handle; |
|
100 |
|
|
101 |
/* Private function prototypes -----------------------------------------------*/ |
|
102 |
void RTC_IRQHandler(void); |
|
103 |
|
|
104 |
/* Private functions ---------------------------------------------------------*/ |
|
105 |
|
|
106 |
/** |
|
107 |
* @brief This function configures the RTC_WKUP as a time base source. |
|
108 |
* The time source is configured to have 1ms time base with a dedicated |
|
109 |
* Tick interrupt priority. |
|
110 |
* Wakeup Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK |
|
111 |
= 1ms |
|
112 |
* Wakeup Time = WakeupTimebase * WakeUpCounter (0 + 1) |
|
113 |
= 1 ms |
|
114 |
* @note This function is called automatically at the beginning of program after |
|
115 |
* reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). |
|
116 |
* @param TickPriority Tick interrupt priority. |
|
117 |
* @retval HAL status |
|
118 |
*/ |
|
119 |
HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority) |
|
120 |
{ |
|
121 |
__IO uint32_t counter = 0U; |
|
122 |
|
|
123 |
RCC_OscInitTypeDef RCC_OscInitStruct; |
|
124 |
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; |
|
125 |
|
|
126 |
#ifdef RTC_CLOCK_SOURCE_LSE |
|
127 |
/* Configue LSE as RTC clock soucre */ |
|
128 |
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; |
|
129 |
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
|
130 |
RCC_OscInitStruct.LSEState = RCC_LSE_ON; |
|
131 |
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; |
|
132 |
#elif defined (RTC_CLOCK_SOURCE_LSI) |
|
133 |
/* Configue LSI as RTC clock soucre */ |
|
134 |
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; |
|
135 |
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
|
136 |
RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
|
137 |
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; |
|
138 |
#elif defined (RTC_CLOCK_SOURCE_HSE) |
|
139 |
/* Configue HSE as RTC clock soucre */ |
|
140 |
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
|
141 |
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
|
142 |
RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
|
143 |
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32; |
|
144 |
#else |
|
145 |
#error Please select the RTC Clock source |
|
146 |
#endif /* RTC_CLOCK_SOURCE_LSE */ |
|
147 |
|
|
148 |
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) |
|
149 |
{ |
|
150 |
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
|
151 |
if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK) |
|
152 |
{ |
|
153 |
/* Enable RTC Clock */ |
|
154 |
__HAL_RCC_RTC_ENABLE(); |
|
155 |
/* The time base should be 1ms |
|
156 |
Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK |
|
157 |
HSE/32 as RTC clock and HSE 8MHz |
|
158 |
Time base = ((49 + 1) * (4 + 1)) / 250kHz |
|
159 |
= 1ms |
|
160 |
LSE as RTC clock |
|
161 |
Time base = ((31 + 1) * (0 + 1)) / 32.768Khz |
|
162 |
= ~1ms |
|
163 |
LSI as RTC clock |
|
164 |
Time base = ((39 + 1) * (0 + 1)) / 40Khz |
|
165 |
= 1ms |
|
166 |
*/ |
|
167 |
hRTC_Handle.Instance = RTC; |
|
168 |
hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24; |
|
169 |
hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; |
|
170 |
hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV; |
|
171 |
hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE; |
|
172 |
hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
|
173 |
hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
|
174 |
HAL_RTC_Init(&hRTC_Handle); |
|
175 |
|
|
176 |
/* Disable the write protection for RTC registers */ |
|
177 |
__HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle); |
|
178 |
|
|
179 |
/* Disable the Wake-up Timer */ |
|
180 |
__HAL_RTC_WAKEUPTIMER_DISABLE(&hRTC_Handle); |
|
181 |
|
|
182 |
/* In case of interrupt mode is used, the interrupt source must disabled */ |
|
183 |
__HAL_RTC_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle,RTC_IT_WUT); |
|
184 |
|
|
185 |
/* Wait till RTC WUTWF flag is set */ |
|
186 |
while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(&hRTC_Handle, RTC_FLAG_WUTWF) == RESET) |
|
187 |
{ |
|
188 |
if(counter++ == (SystemCoreClock /48U)) |
|
189 |
{ |
|
190 |
return HAL_ERROR; |
|
191 |
} |
|
192 |
} |
|
193 |
|
|
194 |
/* Clear PWR wake up Flag */ |
|
195 |
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); |
|
196 |
|
|
197 |
/* Clear RTC Wake Up timer Flag */ |
|
198 |
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_WUTF); |
|
199 |
|
|
200 |
/* Configure the Wake-up Timer counter */ |
|
201 |
hRTC_Handle.Instance->WUTR = 0U; |
|
202 |
|
|
203 |
/* Clear the Wake-up Timer clock source bits in CR register */ |
|
204 |
hRTC_Handle.Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL; |
|
205 |
|
|
206 |
/* Configure the clock source */ |
|
207 |
hRTC_Handle.Instance->CR |= (uint32_t)RTC_WAKEUPCLOCK_CK_SPRE_16BITS; |
|
208 |
|
|
209 |
/* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */ |
|
210 |
__HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT(); |
|
211 |
|
|
212 |
__HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE(); |
|
213 |
|
|
214 |
/* Configure the Interrupt in the RTC_CR register */ |
|
215 |
__HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle,RTC_IT_WUT); |
|
216 |
|
|
217 |
/* Enable the Wake-up Timer */ |
|
218 |
__HAL_RTC_WAKEUPTIMER_ENABLE(&hRTC_Handle); |
|
219 |
|
|
220 |
/* Enable the write protection for RTC registers */ |
|
221 |
__HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle); |
|
222 |
|
|
223 |
HAL_NVIC_SetPriority(RTC_IRQn, TickPriority, 0U); |
|
224 |
HAL_NVIC_EnableIRQ(RTC_IRQn); |
|
225 |
return HAL_OK; |
|
226 |
} |
|
227 |
} |
|
228 |
return HAL_ERROR; |
|
229 |
} |
|
230 |
|
|
231 |
/** |
|
232 |
* @brief Suspend Tick increment. |
|
233 |
* @note Disable the tick increment by disabling RTC_WKUP interrupt. |
|
234 |
* @param None |
|
235 |
* @retval None |
|
236 |
*/ |
|
237 |
void HAL_SuspendTick(void) |
|
238 |
{ |
|
239 |
/* Disable the write protection for RTC registers */ |
|
240 |
__HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle); |
|
241 |
/* Disable WAKE UP TIMER Interrupt */ |
|
242 |
__HAL_RTC_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle, RTC_IT_WUT); |
|
243 |
/* Enable the write protection for RTC registers */ |
|
244 |
__HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle); |
|
245 |
} |
|
246 |
|
|
247 |
/** |
|
248 |
* @brief Resume Tick increment. |
|
249 |
* @note Enable the tick increment by Enabling RTC_WKUP interrupt. |
|
250 |
* @param None |
|
251 |
* @retval None |
|
252 |
*/ |
|
253 |
void HAL_ResumeTick(void) |
|
254 |
{ |
|
255 |
/* Disable the write protection for RTC registers */ |
|
256 |
__HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle); |
|
257 |
/* Enable WAKE UP TIMER interrupt */ |
|
258 |
__HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle, RTC_IT_WUT); |
|
259 |
/* Enable the write protection for RTC registers */ |
|
260 |
__HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle); |
|
261 |
} |
|
262 |
|
|
263 |
/** |
|
264 |
* @brief Wake Up Timer Event Callback in non blocking mode |
|
265 |
* @note This function is called when RTC_WKUP interrupt took place, inside |
|
266 |
* RTC_IRQHandler(). It makes a direct call to HAL_IncTick() to increment |
|
267 |
* a global variable "uwTick" used as application time base. |
|
268 |
* @param hrtc RTC handle |
|
269 |
* @retval None |
|
270 |
*/ |
|
271 |
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) |
|
272 |
{ |
|
273 |
HAL_IncTick(); |
|
274 |
} |
|
275 |
|
|
276 |
/** |
|
277 |
* @brief This function handles WAKE UP TIMER interrupt request. |
|
278 |
* @param None |
|
279 |
* @retval None |
|
280 |
*/ |
|
281 |
void RTC_IRQHandler(void) |
|
282 |
{ |
|
283 |
HAL_RTCEx_WakeUpTimerIRQHandler(&hRTC_Handle); |
|
284 |
} |
|
285 |
|
|
286 |
/** |
|
287 |
* @} |
|
288 |
*/ |
|
289 |
|
|
290 |
/** |
|
291 |
* @} |
|
292 |
*/ |
|
293 |
|
|
294 |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |