1 /**
2   ******************************************************************************
3   * @file    stm32h7rsxx_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 'stm32h7rsxx_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 stm32h7rsxx_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 stm32h7rsxx_hal_timebase_tim use is recommended for the Applications/Examples
41           requiring low power modes
42   @endverbatim
43   ******************************************************************************
44   */
45 
46 /* Includes ------------------------------------------------------------------*/
47 #include "stm32h7rsxx_hal.h"
48 
49 /** @addtogroup STM32H7RSxx_HAL_Driver
50   * @{
51   */
52 
53 /** @defgroup HAL_TimeBase_RTC_WakeUp_Template  HAL TimeBase RTC WakeUp 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     HSE as RTC clock
74      Time base = ((99 + 1) * (4 + 1)) / 500kHz
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        4U
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_HSE */
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 /* USE_HAL_RTC_REGISTER_CALLBACKS */
105 /* Private functions ---------------------------------------------------------*/
106 
107 /**
108   * @brief  This function configures the RTC wakeup timer 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_PWR_EnableBkUpAccess();
125 
126   /* Force and Release the Backup domain reset */
127   __HAL_RCC_BACKUPRESET_FORCE();
128   __HAL_RCC_BACKUPRESET_RELEASE();
129 
130   /* Enable RTC Clock */
131   __HAL_RCC_RTC_ENABLE();
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.LSEState = RCC_LSE_ON;
138   RCC_OscInitStruct.PLL1.PLLState = RCC_PLL_NONE;
139   RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;
140   RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;
141   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
142 #elif defined (RTC_CLOCK_SOURCE_LSI)
143   /* Configure LSI as RTC clock source */
144   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
145   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
146   RCC_OscInitStruct.PLL1.PLLState = RCC_PLL_NONE;
147   RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;
148   RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;
149   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
150 #elif defined (RTC_CLOCK_SOURCE_HSE)
151   /* Configure HSE as RTC clock source */
152   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
153   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
154   RCC_OscInitStruct.PLL1.PLLState = RCC_PLL_NONE;
155   RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;
156   RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;
157   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV48;
158 #else
159 #error Please select the RTC Clock source
160 #endif /* RTC_CLOCK_SOURCE_LSE */
161 
162   Status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
163 
164   if (Status == HAL_OK)
165   {
166     PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
167     Status = HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
168   }
169 
170   if (Status == HAL_OK)
171   {
172     hRTC_Handle.Instance = RTC;
173     hRTC_Handle.Init.HourFormat     = RTC_HOURFORMAT_24;
174     hRTC_Handle.Init.AsynchPrediv   = RTC_ASYNCH_PREDIV;
175     hRTC_Handle.Init.SynchPrediv    = RTC_SYNCH_PREDIV;
176     hRTC_Handle.Init.OutPut         = RTC_OUTPUT_DISABLE;
177     hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
178     hRTC_Handle.Init.OutPutType     = RTC_OUTPUT_TYPE_OPENDRAIN;
179     hRTC_Handle.Init.BinMode        = RTC_BINARY_NONE;
180 
181     Status = HAL_RTC_Init(&hRTC_Handle);
182 
183 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1U)
184     HAL_RTC_RegisterCallback(&hRTC_Handle, HAL_RTC_WAKEUPTIMER_EVENT_CB_ID, TimeBase_RTCEx_WakeUpTimerEventCallback);
185 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
186   }
187 
188   if (Status == HAL_OK)
189   {
190     uint32_t WakeUpCounter = ((uint32_t)uwTickFreq / (uint32_t)HAL_TICK_FREQ_1KHZ) - 1U;
191     Status = HAL_RTCEx_SetWakeUpTimer_IT(&hRTC_Handle, WakeUpCounter, RTC_WAKEUPCLOCK_CK_SPRE_16BITS, 0);
192   }
193 
194   if (TickPriority < (1UL << __NVIC_PRIO_BITS))
195   {
196     /* Enable the RTC global Interrupt */
197     HAL_NVIC_SetPriority(RTC_IRQn, TickPriority, 0U);
198     uwTickPrio = TickPriority;
199   }
200   else
201   {
202     Status = HAL_ERROR;
203   }
204 
205   HAL_NVIC_EnableIRQ(RTC_IRQn);
206 
207   return Status;
208 }
209 
210 /**
211   * @brief  Suspend Tick increment.
212   * @note   Disable the tick increment by disabling RTC_WKUP interrupt.
213   * @retval None
214   */
HAL_SuspendTick(void)215 void HAL_SuspendTick(void)
216 {
217   /* Disable the write protection for RTC registers */
218   __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
219   /* Disable RTC WAKE UP TIMER Interrupt */
220   __HAL_RTC_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle, RTC_IT_WUT);
221   /* Enable the write protection for RTC registers */
222   __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
223 }
224 
225 /**
226   * @brief  Resume Tick increment.
227   * @note   Enable the tick increment by enabling RTC_WKUP interrupt.
228   * @retval None
229   */
HAL_ResumeTick(void)230 void HAL_ResumeTick(void)
231 {
232   /* Disable the write protection for RTC registers */
233   __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
234   /* Enable RTC WAKE UP TIMER  interrupt */
235   __HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle, RTC_IT_WUT);
236   /* Enable the write protection for RTC registers */
237   __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
238 }
239 
240 /**
241   * @brief  Wake Up Timer Event Callback in non blocking mode
242   * @note   This function is called  when RTC_WKUP interrupt took place, inside
243   * RTC_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
244   * a global variable "uwTick" used as application time base.
245   * @param  hrtc RTC handle
246   * @retval None
247   */
248 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1U)
TimeBase_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)249 void TimeBase_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
250 #else
251 void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
252 #endif
253 {
254   /* Prevent unused argument(s) compilation warning */
255   UNUSED(hrtc);
256 
257   HAL_IncTick();
258 }
259 
260 /**
261   * @brief  This function handles RTC WAKE UP TIMER interrupt request.
262   * @retval None
263   */
RTC_IRQHandler(void)264 void RTC_IRQHandler(void)
265 {
266   HAL_RTCEx_WakeUpTimerIRQHandler(&hRTC_Handle);
267 }
268 
269 /**
270   * @}
271   */
272 
273 /**
274   * @}
275   */
276