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