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