1 /**
2   ******************************************************************************
3   * @file    stm32f0xx_hal_timebase_rtc_wakeup_template.c
4   * @brief   HAL time base based on the hardware RTC_WAKEUP Template.
5   *
6   *          This file overrides the native HAL time base functions (defined as weak)
7   *          to use the RTC WAKEUP for the time base generation:
8   *           + Initializes the RTC peripheral and configures the wakeup timer to be
9   *             incremented each 1ms
10   *           + The wakeup feature is configured to assert an interrupt each 1ms
11   *           + HAL_IncTick is called inside the HAL_RTCEx_WakeUpTimerEventCallback
12   *           + HSE (default), LSE or LSI can be selected as RTC clock source
13   *
14   ******************************************************************************
15   * @attention
16   *
17   * Copyright (c) 2016 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 'stm32f0xx_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 stm32f0xx_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 stm32f0xx_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 "stm32f0xx_hal.h"
49 /** @addtogroup STM32F0xx_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 #if defined(RTC_CLOCK_SOURCE_HSE)
72   #define RTC_ASYNCH_PREDIV       49U
73   #define RTC_SYNCH_PREDIV        4U
74 #elif defined(RTC_CLOCK_SOURCE_LSE)
75   #define RTC_ASYNCH_PREDIV       0U
76   #define RTC_SYNCH_PREDIV        31U
77 #else        /* CLOCK_SOURCE_LSI */
78   #define RTC_ASYNCH_PREDIV       0U
79   #define RTC_SYNCH_PREDIV        39U
80 #endif       /* RTC_CLOCK_SOURCE_HSE */
81 
82 /* Private macro -------------------------------------------------------------*/
83 /* Private variables ---------------------------------------------------------*/
84 RTC_HandleTypeDef        hRTC_Handle;
85 
86 /* Private function prototypes -----------------------------------------------*/
87 void RTC_IRQHandler(void);
88 
89 /* Private functions ---------------------------------------------------------*/
90 
91 /**
92   * @brief  This function configures the RTC_WKUP as a time base source.
93   *         The time source is configured  to have 1ms time base with a dedicated
94   *         Tick interrupt priority.
95   *         Wakeup Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
96                              = 1ms
97   *         Wakeup Time = WakeupTimebase * WakeUpCounter (0 + 1)
98                         = 1 ms
99   * @note   This function is called  automatically at the beginning of program after
100   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
101   * @param  TickPriority Tick interrupt priority.
102   * @retval HAL status
103   */
HAL_InitTick(uint32_t TickPriority)104 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
105 {
106   __IO uint32_t counter = 0U;
107 
108   RCC_OscInitTypeDef        RCC_OscInitStruct;
109   RCC_PeriphCLKInitTypeDef  PeriphClkInitStruct;
110   HAL_StatusTypeDef     status;
111 
112 #ifdef RTC_CLOCK_SOURCE_LSE
113   /* Configue LSE as RTC clock soucre */
114   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
115   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
116   RCC_OscInitStruct.LSEState = RCC_LSE_ON;
117   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
118 #elif defined (RTC_CLOCK_SOURCE_LSI)
119   /* Configue LSI as RTC clock soucre */
120   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
121   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
122   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
123   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
124 #elif defined (RTC_CLOCK_SOURCE_HSE)
125   /* Configue HSE as RTC clock soucre */
126   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
127   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
128   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
129   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32;
130 #else
131 #error Please select the RTC Clock source
132 #endif /* RTC_CLOCK_SOURCE_LSE */
133 
134   status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
135   if (status == HAL_OK)
136   {
137     PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
138     status = HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
139   }
140   if (status == HAL_OK)
141   {
142     /* Enable RTC Clock */
143     __HAL_RCC_RTC_ENABLE();
144     /* The time base should be 1ms
145        Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
146        HSE/32 as RTC clock and HSE 8MHz
147          Time base = ((49 + 1) * (4 + 1)) / 250kHz
148                    = 1ms
149        LSE as RTC clock
150          Time base = ((31 + 1) * (0 + 1)) / 32.768Khz
151                    = ~1ms
152        LSI as RTC clock
153          Time base = ((39 + 1) * (0 + 1)) / 40Khz
154                    = 1ms
155     */
156     hRTC_Handle.Instance = RTC;
157     hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
158     hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
159     hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
160     hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
161     hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
162     hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
163     status = HAL_RTC_Init(&hRTC_Handle);
164   }
165   if (status == HAL_OK)
166   {
167     /* Disable the write protection for RTC registers */
168     __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
169 
170     /* Disable the Wake-up Timer */
171     __HAL_RTC_WAKEUPTIMER_DISABLE(&hRTC_Handle);
172 
173     /* In case of interrupt mode is used, the interrupt source must disabled */
174     __HAL_RTC_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle,RTC_IT_WUT);
175 
176     /* Wait till RTC WUTWF flag is set  */
177     while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(&hRTC_Handle, RTC_FLAG_WUTWF) == RESET)
178     {
179       if (counter++ == (SystemCoreClock /48U))
180       {
181         status = HAL_ERROR;
182       }
183     }
184   }
185   if (status == HAL_OK)
186   {
187     /* Clear PWR wake up Flag */
188     __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
189 
190     /* Clear RTC Wake Up timer Flag */
191     __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_WUTF);
192 
193     /* Configure the Wake-up Timer counter */
194     hRTC_Handle.Instance->WUTR = 0U;
195 
196     /* Clear the Wake-up Timer clock source bits in CR register */
197     hRTC_Handle.Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
198 
199     /* Configure the clock source */
200     hRTC_Handle.Instance->CR |= (uint32_t)RTC_WAKEUPCLOCK_CK_SPRE_16BITS;
201 
202     /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
203     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
204 
205     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
206 
207     /* Configure the Interrupt in the RTC_CR register */
208     __HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle, RTC_IT_WUT);
209 
210     /* Enable the Wake-up Timer */
211     __HAL_RTC_WAKEUPTIMER_ENABLE(&hRTC_Handle);
212 
213     /* Enable the write protection for RTC registers */
214     __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
215 
216     /* Enable the RTC global Interrupt */
217     HAL_NVIC_EnableIRQ(RTC_IRQn);
218 
219     /* Configure the SysTick IRQ priority */
220     if (TickPriority < (1UL << __NVIC_PRIO_BITS))
221     {
222       HAL_NVIC_SetPriority(RTC_IRQn, TickPriority, 0U);
223       uwTickPrio = TickPriority;
224     }
225     else
226     {
227       status = HAL_ERROR;
228     }
229   }
230   return status;
231 }
232 
233 /**
234   * @brief  Suspend Tick increment.
235   * @note   Disable the tick increment by disabling RTC_WKUP interrupt.
236   * @param  None
237   * @retval None
238   */
HAL_SuspendTick(void)239 void HAL_SuspendTick(void)
240 {
241   /* Disable the write protection for RTC registers */
242   __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
243   /* Disable WAKE UP TIMER Interrupt */
244   __HAL_RTC_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle, RTC_IT_WUT);
245   /* Enable the write protection for RTC registers */
246   __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
247 }
248 
249 /**
250   * @brief  Resume Tick increment.
251   * @note   Enable the tick increment by Enabling RTC_WKUP interrupt.
252   * @param  None
253   * @retval None
254   */
HAL_ResumeTick(void)255 void HAL_ResumeTick(void)
256 {
257   /* Disable the write protection for RTC registers */
258   __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
259   /* Enable  WAKE UP TIMER  interrupt */
260   __HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle, RTC_IT_WUT);
261   /* Enable the write protection for RTC registers */
262   __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
263 }
264 
265 /**
266   * @brief  Wake Up Timer Event Callback in non blocking mode
267   * @note   This function is called  when RTC_WKUP interrupt took place, inside
268   * RTC_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
269   * a global variable "uwTick" used as application time base.
270   * @param  hrtc RTC handle
271   * @retval None
272   */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)273 void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
274 {
275   HAL_IncTick();
276 }
277 
278 /**
279   * @brief  This function handles  WAKE UP TIMER  interrupt request.
280   * @param  None
281   * @retval None
282   */
RTC_IRQHandler(void)283 void RTC_IRQHandler(void)
284 {
285   HAL_RTCEx_WakeUpTimerIRQHandler(&hRTC_Handle);
286 }
287 
288 /**
289   * @}
290   */
291 
292 /**
293   * @}
294   */
295 
296