1 /**
2 ******************************************************************************
3 * @file stm32u5xx_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 * @attention
16 *
17 * Copyright (c) 2021 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 'stm32u5xx_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 stm32u5xx_hal_conf.h
34
35 [..]
36 (@) HAL RTC alarm and HAL RTC wakeup drivers can not 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 stm32u5xx_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 "stm32u5xx_hal.h"
49 /** @addtogroup STM32U5xx_HAL_Driver
50 * @{
51 */
52
53 /** @defgroup HAL_TimeBase_RTC_Alarm_Template HAL TimeBase RTC Alarm Template
54 * @{
55 */
56
57 /* Private typedef -----------------------------------------------------------*/
58 /* Private define ------------------------------------------------------------*/
59
60 /* Uncomment the line below to select the appropriate RTC Clock source for your application:
61 + RTC_CLOCK_SOURCE_HSE: can be selected for applications requiring timing precision.
62 + RTC_CLOCK_SOURCE_LSE: can be selected for applications with low constraint on timing
63 precision.
64 + RTC_CLOCK_SOURCE_LSI: can be selected for applications with low constraint on timing
65 precision.
66 */
67 /* #define RTC_CLOCK_SOURCE_HSE */
68 /* #define RTC_CLOCK_SOURCE_LSE */
69 #define RTC_CLOCK_SOURCE_LSI
70
71 /* The time base should be 1ms
72 Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
73 */
74 #if defined (RTC_CLOCK_SOURCE_HSE)
75 #define RTC_ASYNCH_PREDIV 99U
76 #define RTC_SYNCH_PREDIV 4U
77 #elif defined (RTC_CLOCK_SOURCE_LSE)
78 #define RTC_ASYNCH_PREDIV 0U
79 #define RTC_SYNCH_PREDIV 32U
80 #elif defined (RTC_CLOCK_SOURCE_LSI)
81 #define RTC_ASYNCH_PREDIV 0U
82 #define RTC_SYNCH_PREDIV 31U
83 #else
84 #error Please select the RTC Clock source
85 #endif /* RTC_CLOCK_SOURCE_LSE */
86
87 /* Private macro -------------------------------------------------------------*/
88 /* Private variables ---------------------------------------------------------*/
89 static RTC_HandleTypeDef hRTC_Handle;
90
91 /* Private function prototypes -----------------------------------------------*/
92 void RTC_IRQHandler(void);
93 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1U)
94 void TimeBase_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc);
95 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
96 /* Private functions ---------------------------------------------------------*/
97
98 /**
99 * @brief This function configures the RTC_ALARMA as a time base source.
100 * The time source is configured to have 1ms time base with a dedicated
101 * Tick interrupt priority.
102 * @note This function is called automatically at the beginning of program after
103 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
104 * @param TickPriority Tick interrupt priority.
105 * @retval HAL status
106 */
HAL_InitTick(uint32_t TickPriority)107 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
108 {
109 HAL_StatusTypeDef Status;
110
111 RCC_OscInitTypeDef RCC_OscInitStruct;
112 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
113
114 /* Disable bkup domain protection */
115 __HAL_RCC_PWR_CLK_ENABLE();
116 HAL_PWR_EnableBkUpAccess();
117
118 /* Force and Release the Backup domain reset */
119 __HAL_RCC_BACKUPRESET_FORCE();
120 __HAL_RCC_BACKUPRESET_RELEASE();
121
122 /* Enable RTC Clock */
123 __HAL_RCC_RTC_ENABLE();
124 __HAL_RCC_RTCAPB_CLK_ENABLE();
125
126 #if defined (RTC_CLOCK_SOURCE_LSE)
127 /* Configure LSE as RTC clock source */
128 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
129 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
130 RCC_OscInitStruct.LSEState = RCC_LSE_RTC_ONLY;
131 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
132 #elif defined (RTC_CLOCK_SOURCE_LSI)
133 /* Configure LSI as RTC clock source */
134 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
135 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
136 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
137 RCC_OscInitStruct.LSIDiv = RCC_LSI_DIV1;
138 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
139 #elif defined (RTC_CLOCK_SOURCE_HSE)
140 /* Configure HSE as RTC clock source */
141 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
142 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
143 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
144 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32;
145 #else
146 #error Please select the RTC Clock source
147 #endif /* RTC_CLOCK_SOURCE_LSE */
148
149 Status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
150
151 if (Status == HAL_OK)
152 {
153 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
154 Status = HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
155 }
156
157 if (Status == HAL_OK)
158 {
159 hRTC_Handle.Instance = RTC;
160 hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
161 hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
162 hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
163 hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
164 hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
165 hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
166 hRTC_Handle.Init.BinMode = RTC_BINARY_NONE;
167
168 Status = HAL_RTC_Init(&hRTC_Handle);
169
170 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1U)
171 HAL_RTC_RegisterCallback(&hRTC_Handle, HAL_RTC_WAKEUPTIMER_EVENT_CB_ID, TimeBase_RTCEx_WakeUpTimerEventCallback);
172 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
173 }
174
175 if (Status == HAL_OK)
176 {
177 Status = HAL_RTCEx_SetWakeUpTimer_IT(&hRTC_Handle, 0, RTC_WAKEUPCLOCK_CK_SPRE_16BITS, 0);
178 }
179
180 if (TickPriority < (1UL << __NVIC_PRIO_BITS))
181 {
182 /* Enable the RTC global Interrupt */
183 HAL_NVIC_SetPriority(RTC_IRQn, TickPriority, 0U);
184 uwTickPrio = TickPriority;
185 }
186 else
187 {
188 Status = HAL_ERROR;
189 }
190
191 HAL_NVIC_EnableIRQ(RTC_IRQn);
192
193 return Status;
194 }
195
196 /**
197 * @brief Suspend Tick increment.
198 * @note Disable the tick increment by disabling RTC_WKUP interrupt.
199 * @retval None
200 */
HAL_SuspendTick(void)201 void HAL_SuspendTick(void)
202 {
203 /* Disable the write protection for RTC registers */
204 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
205 /* Disable WAKE UP TIMER Interrupt */
206 __HAL_RTC_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle, RTC_IT_WUT);
207 /* Enable the write protection for RTC registers */
208 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
209 }
210
211 /**
212 * @brief Resume Tick increment.
213 * @note Enable the tick increment by Enabling RTC_WKUP interrupt.
214 * @retval None
215 */
HAL_ResumeTick(void)216 void HAL_ResumeTick(void)
217 {
218 /* Disable the write protection for RTC registers */
219 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
220 /* Enable WAKE UP TIMER interrupt */
221 __HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle, RTC_IT_WUT);
222 /* Enable the write protection for RTC registers */
223 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
224 }
225
226 /**
227 * @brief Wake Up Timer Event Callback in non blocking mode
228 * @note This function is called when RTC_WKUP interrupt took place, inside
229 * RTC_WKUP_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
230 * a global variable "uwTick" used as application time base.
231 * @param hrtc RTC handle
232 * @retval None
233 */
234 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1U)
TimeBase_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)235 void TimeBase_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
236 #else
237 void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
238 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
239 {
240 /* Prevent unused argument(s) compilation warning */
241 UNUSED(hrtc);
242
243 HAL_IncTick();
244 }
245
246 /**
247 * @brief This function handles WAKE UP TIMER interrupt request.
248 * @retval None
249 */
RTC_IRQHandler(void)250 void RTC_IRQHandler(void)
251 {
252 HAL_RTCEx_WakeUpTimerIRQHandler(&hRTC_Handle);
253 }
254
255 /**
256 * @}
257 */
258
259 /**
260 * @}
261 */
262