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