1 /**
2 ******************************************************************************
3 * @file stm32wbxx_hal_timebase_rtc_wakeup_template.c
4 * @author MCD Application Team
5 * @brief HAL time base based on the hardware RTC_WAKEUP Template.
6 *
7 * This file overrides the native HAL time base functions (defined as weak)
8 * to use the RTC WAKEUP for the time base generation:
9 * + Initializes the RTC peripheral and configures the wakeup timer to be
10 * incremented each 1ms when uwTickFreq is set to default value, else
11 * 10 ms or 100 ms, depending of above global variable value.
12 * + HAL_IncTick is called inside the HAL_RTCEx_WakeUpTimerEventCallback
13 * + HSE (default), LSE or LSI can be selected as RTC clock source
14 ******************************************************************************
15 * @attention
16 *
17 * Copyright (c) 2019 STMicroelectronics.
18 * All rights reserved.
19 *
20 * This software is licensed under terms that can be found in the LICENSE file
21 * in the root directory of this software component.
22 * If no LICENSE file comes with this software, it is provided AS-IS.
23 *
24 ******************************************************************************
25 @verbatim
26 ==============================================================================
27 ##### How to use this driver #####
28 ==============================================================================
29 [..]
30 This file must be copied to the application folder and modified as follows:
31 (#) Rename it to 'stm32wbxx_hal_timebase_rtc_wakeup.c'
32 (#) Add this file and the RTC HAL drivers to your project and uncomment
33 HAL_RTC_MODULE_ENABLED define in stm32wbxx_hal_conf.h
34
35 [..]
36 (@) HAL RTC alarm and HAL RTC wakeup drivers can't be used with low power modes:
37 The wake up capability of the RTC may be intrusive in case of prior low power mode
38 configuration requiring different wake up sources.
39 Application/Example behavior is no more guaranteed
40 (@) The stm32wbxx_hal_timebase_tim use is recommended for the Applications/Examples
41 requiring low power modes
42
43 @endverbatim
44 ******************************************************************************
45 */
46
47 /* Includes ------------------------------------------------------------------*/
48 #include "stm32wbxx_hal.h"
49
50 /** @addtogroup STM32WBxx_HAL_Driver
51 * @{
52 */
53
54 /** @defgroup HAL_TimeBase_RTC_WakeUp_Template HAL TimeBase RTC WakeUp Template
55 * @{
56 */
57
58 /* Private typedef -----------------------------------------------------------*/
59 /* Private define ------------------------------------------------------------*/
60
61 /* Uncomment the line below to select the appropriate RTC Clock source for your application:
62 + RTC_CLOCK_SOURCE_HSE: can be selected for applications requiring timing precision.
63 + RTC_CLOCK_SOURCE_LSE: can be selected for applications with low constraint on timing
64 precision.
65 + RTC_CLOCK_SOURCE_LSI: can be selected for applications with low constraint on timing
66 precision.
67 */
68 /* #define RTC_CLOCK_SOURCE_HSE */
69 /* #define RTC_CLOCK_SOURCE_LSE */
70 /* #define RTC_CLOCK_SOURCE_LSI */
71
72 /* Maximize Asynchronous prescaler for low power consumption :
73 ck_apre = RTCCLK / (ASYNC prediv + 1)
74 ck_spre = ck_apre /(SYNC prediv + 1) = 1 Hz */
75 #if defined (RTC_CLOCK_SOURCE_LSE)
76 /* LSE Freq = 32.768 kHz RC */
77 #define RTC_ASYNCH_PREDIV 0x7Fu
78 #define RTC_SYNCH_PREDIV 0x00FFu
79 #elif defined (RTC_CLOCK_SOURCE_LSI)
80 /* LSI Freq = 32 kHz RC */
81 #define RTC_ASYNCH_PREDIV 0x7Fu
82 #define RTC_SYNCH_PREDIV 0x00FEu
83 #elif defined (RTC_CLOCK_SOURCE_HSE)
84 /* HSE Freq as RTCCLK = 32 MHz / 32 = 1 MHz */
85 #define RTC_ASYNCH_PREDIV 0x7Fu
86 #define RTC_SYNCH_PREDIV 0x1E83u
87 #endif /* RTC_CLOCK_SOURCE_LSE */
88
89 /* Private macro -------------------------------------------------------------*/
90 /* Private variables ---------------------------------------------------------*/
91 extern RTC_HandleTypeDef hRTC_Handle;
92 RTC_HandleTypeDef hRTC_Handle;
93
94 /* Private function prototypes -----------------------------------------------*/
95 void RTC_WKUP_IRQHandler(void);
96
97 /* Private functions ---------------------------------------------------------*/
98
99 /**
100 * @brief This function configures the RTC_WKUP as a time base source.
101 * The time source is configured to have 1ms time base with a dedicated
102 * Tick interrupt priority.
103 * Wakeup Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
104 = 1ms
105 * Wakeup Time = WakeupTimebase * WakeUpCounter (0 + 1)
106 = 1 ms
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 */
HAL_InitTick(uint32_t TickPriority)112 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
113 {
114 HAL_StatusTypeDef status = HAL_OK;
115 uint32_t wucounter;
116 RCC_OscInitTypeDef RCC_OscInitStruct;
117 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
118
119 /* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that don't take the value zero) */
120 if ((uint32_t)uwTickFreq != 0U)
121 {
122 /* Disable backup domeain protection */
123 HAL_PWR_EnableBkUpAccess();
124
125 /* Enable RTC APB clock gating */
126 __HAL_RCC_RTCAPB_CLK_ENABLE();
127
128 /* Disable the Wake-up Timer */
129 __HAL_RTC_WAKEUPTIMER_DISABLE(&hRTC_Handle);
130 /* In case of interrupt mode is used, the interrupt source must disabled */
131 __HAL_RTC_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle, RTC_IT_WUT);
132 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_WUTF);
133
134 /* Get RTC clock configuration */
135 HAL_RCCEx_GetPeriphCLKConfig(&PeriphClkInitStruct);
136
137 /*In case of RTC clock already enable, make sure it's the good one */
138 #if defined (RTC_CLOCK_SOURCE_LSE)
139 if ((PeriphClkInitStruct.RTCClockSelection == RCC_RTCCLKSOURCE_LSE)
140 && (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != 0x00u))
141 #elif defined (RTC_CLOCK_SOURCE_LSI)
142 if ((PeriphClkInitStruct.RTCClockSelection == RCC_RTCCLKSOURCE_LSI)
143 && (__HAL_RCC_GET_FLAG(RCC_FLAG_LSI1RDY) != 0x00u))
144 #elif defined (RTC_CLOCK_SOURCE_HSE)
145 if ((PeriphClkInitStruct.RTCClockSelection == RCC_RTCCLKSOURCE_HSE_DIV32)
146 && (__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != 0x00u))
147 #else
148 #error Please select the RTC Clock source
149 #endif /* RTC_CLOCK_SOURCE_LSE */
150 {
151 /* Do nothing */
152 }
153 else
154 {
155 #ifdef RTC_CLOCK_SOURCE_LSE
156 /* Configure LSE as RTC clock source */
157 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
158 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
159 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
160 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
161 #elif defined (RTC_CLOCK_SOURCE_LSI)
162 /* Configure LSI as RTC clock source */
163 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
164 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
165 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
166 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
167 #elif defined (RTC_CLOCK_SOURCE_HSE)
168 /* Configure HSE as RTC clock source */
169 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
170 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
171 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
172 /* Ensure that RTC is clocked by 1MHz */
173 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32;
174 #endif /* RTC_CLOCK_SOURCE_LSE */
175
176 /* Configure oscillator */
177 status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
178 if(status == HAL_OK)
179 {
180 /* Configure RTC clock source */
181 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
182 status = HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
183
184 /* Enable RTC Clock */
185 if(status == HAL_OK)
186 {
187 __HAL_RCC_RTC_ENABLE();
188 }
189 }
190 }
191
192 /* If RTC Clock configuration is ok */
193 if (status == HAL_OK)
194 {
195 /* No care of RTC init parameter here. Only needed if RTC is being used
196 for other features in same time: calendar, alarm, timestamp, etc... */
197 hRTC_Handle.Instance = RTC;
198 hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
199 hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
200 hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
201 hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
202 hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
203 hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
204 status = HAL_RTC_Init(&hRTC_Handle);
205
206 if(status == HAL_OK)
207 {
208 /* The time base should be of (uint32_t)uwTickFreq) ms. Tick counter
209 is incremented eachtime wakeup time reaches zero. Wakeup timer is
210 clocked on RTCCLK divided by 2. So downcounting counter has to be
211 set to (RTCCLK / 2) / (1000 / (uint32_t)uwTickFreq)) minus 1 */
212 #ifdef RTC_CLOCK_SOURCE_LSE
213 wucounter = LSE_VALUE;
214 #elif defined (RTC_CLOCK_SOURCE_LSI)
215 wucounter = LSI_VALUE;
216 #elif defined (RTC_CLOCK_SOURCE_HSE)
217 /* HSE input clock to RTC is divided by 32 */
218 wucounter = (HSE_VALUE >> 5);
219 #endif
220 wucounter = ((wucounter >> 1) / (1000U / (uint32_t)uwTickFreq)) -1u;
221 status = HAL_RTCEx_SetWakeUpTimer_IT(&hRTC_Handle, wucounter, RTC_WAKEUPCLOCK_RTCCLK_DIV2);
222
223 if(status == HAL_OK)
224 {
225 /* Enable the RTC global Interrupt */
226 HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
227 /* Configure the SysTick IRQ priority */
228 if (TickPriority < (1UL << __NVIC_PRIO_BITS))
229 {
230 HAL_NVIC_SetPriority(RTC_WKUP_IRQn, TickPriority, 0U);
231 uwTickPrio = TickPriority;
232 }
233 else
234 {
235 status = HAL_ERROR;
236 }
237 }
238 }
239 }
240 }
241 else
242 {
243 status = HAL_ERROR;
244 }
245 return status;
246 }
247
248 /**
249 * @brief Suspend Tick increment.
250 * @note Disable the tick increment by disabling RTC_WKUP interrupt.
251 * @retval None
252 */
HAL_SuspendTick(void)253 void HAL_SuspendTick(void)
254 {
255 /* Disable the write protection for RTC registers */
256 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
257 /* Disable WAKE UP TIMER Interrupt */
258 __HAL_RTC_WAKEUPTIMER_DISABLE_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 Resume Tick increment.
265 * @note Enable the tick increment by Enabling RTC_WKUP interrupt.
266 * @retval None
267 */
HAL_ResumeTick(void)268 void HAL_ResumeTick(void)
269 {
270 /* Disable the write protection for RTC registers */
271 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
272 /* Enable WAKE UP TIMER interrupt */
273 __HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle, RTC_IT_WUT);
274 /* Enable the write protection for RTC registers */
275 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
276 }
277
278 /**
279 * @brief Wake Up Timer Event Callback in non blocking mode
280 * @note This function is called when RTC_WKUP interrupt takes place, inside
281 * RTC_WKUP_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
282 * a global variable "uwTick" used as application time base.
283 * @param hrtc RTC handle
284 * @retval None
285 */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)286 void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
287 {
288 HAL_IncTick();
289 }
290
291 /**
292 * @brief This function handles WAKE UP TIMER interrupt request.
293 * @retval None
294 */
RTC_WKUP_IRQHandler(void)295 void RTC_WKUP_IRQHandler(void)
296 {
297 HAL_RTCEx_WakeUpTimerIRQHandler(&hRTC_Handle);
298 }
299
300 /**
301 * @}
302 */
303
304 /**
305 * @}
306 */
307