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