1 /**
2 ******************************************************************************
3 * @file stm32wb0x_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) 2024 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 'stm32wb0x_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 boardname_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 stm32wb0x_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 "stm32wb0x_hal.h"
49
50 /** @addtogroup STM32WB0x_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_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_LSE
68 /* #define RTC_CLOCK_SOURCE_LSI */
69
70 #define RTC_ASYNCH_PREDIV 0U
71 #define RTC_SYNCH_PREDIV 31U
72
73 /* Private macro -------------------------------------------------------------*/
74 /* Private variables ---------------------------------------------------------*/
75 extern RTC_HandleTypeDef hRTC_Handle;
76 RTC_HandleTypeDef hRTC_Handle;
77
78 /* Private function prototypes -----------------------------------------------*/
79 void RTC_IRQHandler(void);
80
81 /* Private functions ---------------------------------------------------------*/
82
83 /**
84 * @brief This function configures the RTC_WKUP as a time base source.
85 * The time source is configured to have 1ms time base with a dedicated
86 * Tick interrupt priority.
87 * Wakeup Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
88 = 1ms
89 * Wakeup Time = WakeupTimebase * WakeUpCounter (0 + 1)
90 = 1 ms
91 * @note This function is called automatically at the beginning of program after
92 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
93 * @param TickPriority: Tick interrupt priority.
94 * @retval HAL status
95 */
HAL_InitTick(uint32_t TickPriority)96 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
97 {
98 __IO uint32_t counter = 0U;
99
100 RCC_OscInitTypeDef RCC_OscInitStruct;
101
102 #ifdef RTC_CLOCK_SOURCE_LSE
103 /* Configure LSE as RTC clock source */
104 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
105 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
106 #elif defined (RTC_CLOCK_SOURCE_LSI)
107 /* Configure LSI as RTC clock source */
108 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
109 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
110 #error Please select the RTC Clock source
111 #endif /* RTC_CLOCK_SOURCE_LSE */
112
113 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK)
114 {
115 /* Enable RTC Clock */
116 __HAL_RCC_RTC_CLK_ENABLE();
117 /* The time base should be 1ms
118 Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
119 HSE as RTC clock
120 Time base = ((99 + 1) * (9 + 1)) / 1Mhz
121 = 1ms
122 LSE as RTC clock
123 Time base = ((31 + 1) * (0 + 1)) / 32.768Khz
124 = ~1ms
125 LSI as RTC clock
126 Time base = ((31 + 1) * (0 + 1)) / 32Khz
127 = 1ms
128 */
129 hRTC_Handle.Instance = RTC;
130 hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
131 hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
132 hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
133 hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
134 hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
135 if (HAL_RTC_Init(&hRTC_Handle) != HAL_OK)
136 {
137 return HAL_ERROR;
138 }
139
140 /* Disable the write protection for RTC registers */
141 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
142
143 /* Disable the Wake-up Timer */
144 __HAL_RTC_WAKEUPTIMER_DISABLE(&hRTC_Handle);
145
146 /* In case of interrupt mode is used, the interrupt source must disabled */
147 __HAL_RTC_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle, RTC_IT_WUT);
148
149 /* Wait till RTC WUTWF flag is set */
150 while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(&hRTC_Handle, RTC_FLAG_WUTWF) == 0U)
151 {
152 if (counter++ == (SystemCoreClock / 48U))
153 {
154 return HAL_ERROR;
155 }
156 }
157
158 /* Clear RTC Wake Up timer Flag */
159 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_WUTF);
160
161 /* Configure the Wake-up Timer counter */
162 hRTC_Handle.Instance->WUTR = 0U;
163
164 /* Clear the Wake-up Timer clock source bits in CR register */
165 hRTC_Handle.Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
166
167 /* Configure the clock source */
168 hRTC_Handle.Instance->CR |= (uint32_t)RTC_WAKEUPCLOCK_CK_SPRE_16BITS;
169
170 /* Configure the Interrupt in the RTC_CR register */
171 __HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle, RTC_IT_WUT);
172
173 /* Enable the Wake-up Timer */
174 __HAL_RTC_WAKEUPTIMER_ENABLE(&hRTC_Handle);
175
176 /* Enable the write protection for RTC registers */
177 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
178
179 HAL_NVIC_SetPriority(RTC_IRQn, TickPriority, 0);
180 HAL_NVIC_EnableIRQ(RTC_IRQn);
181 return HAL_OK;
182 }
183
184 return HAL_ERROR;
185 }
186
187 /**
188 * @brief Suspend Tick increment.
189 * @note Disable the tick increment by disabling RTC_WKUP interrupt.
190 * @retval None
191 */
HAL_SuspendTick(void)192 void HAL_SuspendTick(void)
193 {
194 /* Disable the write protection for RTC registers */
195 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
196 /* Disable WAKE UP TIMER Interrupt */
197 __HAL_RTC_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle, RTC_IT_WUT);
198 /* Enable the write protection for RTC registers */
199 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
200 }
201
202 /**
203 * @brief Resume Tick increment.
204 * @note Enable the tick increment by Enabling RTC_WKUP interrupt.
205 * @retval None
206 */
HAL_ResumeTick(void)207 void HAL_ResumeTick(void)
208 {
209 /* Disable the write protection for RTC registers */
210 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
211 /* Enable WAKE UP TIMER interrupt */
212 __HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle, RTC_IT_WUT);
213 /* Enable the write protection for RTC registers */
214 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
215 }
216
217 /**
218 * @brief Wake Up Timer Event Callback in non blocking mode
219 * @note This function is called when RTC_WKUP interrupt took place, inside
220 * RTC_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
221 * a global variable "uwTick" used as application time base.
222 * @param hrtc : RTC handle
223 * @retval None
224 */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)225 void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
226 {
227 HAL_IncTick();
228 }
229
230 /**
231 * @brief This function handles WAKE UP TIMER interrupt request.
232 * @retval None
233 */
RTC_IRQHandler(void)234 void RTC_IRQHandler(void)
235 {
236 HAL_RTCEx_WakeUpTimerIRQHandler(&hRTC_Handle);
237 }
238
239 /**
240 * @}
241 */
242
243 /**
244 * @}
245 */
246