1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_hal_timebase_rtc_alarm_template.c
4   * @author  MCD Application Team
5   * @brief   HAL time base based on the hardware RTC_ALARM Template.
6   *
7   *          This file override the native HAL time base functions (defined as weak)
8   *          to use the RTC ALARM for time base generation:
9   *           + Intializes the RTC peripheral with required prescalers value for each
10   *             RTC clock source (HSE, LSE or LSI)
11   *           + The alarm is configured to assert an interrupt when the RTC reaches 1ms
12   *           + HAL_IncTick is called at each Alarm event and the time is reset to 00:00:00
13   *           + HSE (default), LSE or LSI can be selected as RTC clock source
14   *
15   ******************************************************************************
16   * @attention
17   *
18   * Copyright (c) 2016 STMicroelectronics.
19   * All rights reserved.
20   *
21   * This software is licensed under terms that can be found in the LICENSE file
22   * in the root directory of this software component.
23   * If no LICENSE file comes with this software, it is provided AS-IS.
24   *
25   ******************************************************************************
26   @verbatim
27   ==============================================================================
28                         ##### How to use this driver #####
29   ==============================================================================
30     [..]
31     This file must be copied to the application folder and modified as follows:
32     (#) Rename it to 'stm32f2xx_hal_timebase_rtc_alarm.c'
33     (#) Add this file and the RTC HAL drivers to your project and uncomment
34         HAL_RTC_MODULE_ENABLED define in stm32f2xx_hal_conf.h
35 
36     [..]
37     (@) HAL RTC alarm and HAL RTC wakeup drivers can�t be used with low power modes:
38         The wake up capability of the RTC may be intrusive in case of prior low power mode
39         configuration requiring different wake up sources.
40         Application/Example behavior is no more guaranteed
41     (@) The stm32f2xx_hal_timebase_tim use is recommended for the Applications/Examples
42         requiring low power modes
43 
44   @endverbatim
45   ******************************************************************************
46   */
47 
48 /* Includes ------------------------------------------------------------------*/
49 #include "stm32f2xx_hal.h"
50 
51 /** @addtogroup STM32F2xx_HAL_Driver
52   * @{
53   */
54 
55 /** @defgroup HAL_TimeBase_RTC_Alarm_Template  HAL TimeBase RTC Alarm Template
56   * @{
57   */
58 
59 /* Private typedef -----------------------------------------------------------*/
60 /* Private define ------------------------------------------------------------*/
61 
62 /* Uncomment the line below to select the appropriate RTC Clock source for your application:
63   + RTC_CLOCK_SOURCE_HSE: can be selected for applications requiring timing precision.
64   + RTC_CLOCK_SOURCE_LSE: can be selected for applications with low constraint on timing
65                           precision.
66   + RTC_CLOCK_SOURCE_LSI: can be selected for applications with low constraint on timing
67                           precision.
68   */
69 #define RTC_CLOCK_SOURCE_HSE
70 /* #define RTC_CLOCK_SOURCE_LSE */
71 /* #define RTC_CLOCK_SOURCE_LSI */
72 
73 #ifdef RTC_CLOCK_SOURCE_HSE
74   #define RTC_ASYNCH_PREDIV       9U
75   #define RTC_SYNCH_PREDIV        9U
76   #define RCC_RTCCLKSOURCE_1MHZ   ((uint32_t)((uint32_t)RCC_BDCR_RTCSEL | (uint32_t)((HSE_VALUE/1000000U) << 16U)))
77 #elif defined (RTC_CLOCK_SOURCE_LSE)
78   #define RTC_ASYNCH_PREDIV       2U
79   #define RTC_SYNCH_PREDIV        0U
80 #else /* RTC_CLOCK_SOURCE_LSI */
81   #define RTC_ASYNCH_PREDIV       1U
82   #define RTC_SYNCH_PREDIV        0U
83 #endif /* RTC_CLOCK_SOURCE_HSE */
84 
85 /* Private macro -------------------------------------------------------------*/
86 /* Private variables ---------------------------------------------------------*/
87 RTC_HandleTypeDef        hRTC_Handle;
88 /* Private function prototypes -----------------------------------------------*/
89 void RTC_Alarm_IRQHandler(void);
90 /* Private functions ---------------------------------------------------------*/
91 
92 /**
93   * @brief  This function configures the RTC_ALARMA as a time base source.
94   *         The time source is configured  to have 1ms time base with a dedicated
95   *         Tick interrupt priority.
96   * @note   This function is called  automatically at the beginning of program after
97   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
98   * @param  TickPriority Tick interrupt priority.
99   * @retval HAL status
100   */
HAL_InitTick(uint32_t TickPriority)101 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
102 {
103   __IO uint32_t counter = 0U;
104 
105   RCC_OscInitTypeDef        RCC_OscInitStruct;
106   RCC_PeriphCLKInitTypeDef  PeriphClkInitStruct;
107   HAL_StatusTypeDef     status;
108 
109 #ifdef RTC_CLOCK_SOURCE_LSE
110   /* Configue LSE as RTC clock soucre */
111   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
112   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
113   RCC_OscInitStruct.LSEState = RCC_LSE_ON;
114   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
115 #elif defined (RTC_CLOCK_SOURCE_LSI)
116   /* Configue LSI as RTC clock soucre */
117   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
118   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
119   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
120   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
121 #elif defined (RTC_CLOCK_SOURCE_HSE)
122   /* Configue HSE as RTC clock soucre */
123   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
124   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
125   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
126   /* Ensure that RTC is clocked by 1MHz */
127   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_1MHZ;
128 #else
129 #error Please select the RTC Clock source
130 #endif /* RTC_CLOCK_SOURCE_LSE */
131 
132   status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
133   if (status == HAL_OK)
134   {
135     PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
136     status = HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
137   }
138   if (status == HAL_OK)
139   {
140     /* Enable RTC Clock */
141     __HAL_RCC_RTC_ENABLE();
142 
143     /* The time base should be 1ms
144        Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1) * ALARM_VALUE) / RTC_CLOCK
145        HSE as RTC clock
146          Time base = ((9 + 1) * (9 + 1) * 10) / 1MHz
147                    = 1ms
148        LSE as RTC clock
149          Time base = ((2 + 1) * (0 + 1) * 11) / 32.768KHz
150                    = ~1ms
151        LSI as RTC clock
152          Time base = ((1 + 1) * (0 + 1) * 16) / 32KHz
153                    = 1ms
154     */
155     hRTC_Handle.Instance = RTC;
156     hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
157     hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
158     hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
159     hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
160     hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
161     hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
162     status = HAL_RTC_Init(&hRTC_Handle);
163   }
164   if (status == HAL_OK)
165   {
166     /* Disable the write protection for RTC registers */
167     __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
168 
169     /* Disable the Alarm A interrupt */
170     __HAL_RTC_ALARMA_DISABLE(&hRTC_Handle);
171 
172     /* Clear flag alarm A */
173     __HAL_RTC_ALARM_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF);
174 
175     counter = 0U;
176     /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
177     while (__HAL_RTC_ALARM_GET_FLAG(&hRTC_Handle, RTC_FLAG_ALRAWF) == RESET)
178     {
179       if (counter++ == SystemCoreClock / 48U) /* Timeout = ~ 1s */
180       {
181         status = HAL_ERROR;
182       }
183     }
184   }
185   if (status == HAL_OK)
186   {
187     /* Set alarm value for each RTC clock source */
188 #ifdef RTC_CLOCK_SOURCE_HSE
189     hRTC_Handle.Instance->ALRMAR = 0x10U;
190 #elif defined (RTC_CLOCK_SOURCE_LSE)
191     hRTC_Handle.Instance->ALRMAR = 0x11U;
192 #else
193     hRTC_Handle.Instance->ALRMAR = 0x16U;
194 #endif
195 
196     /* Configure the Alarm state: Enable Alarm */
197     __HAL_RTC_ALARMA_ENABLE(&hRTC_Handle);
198 
199     /* Configure the Alarm interrupt */
200     __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
201 
202     /* RTC Alarm Interrupt Configuration: EXTI configuration */
203     __HAL_RTC_ALARM_EXTI_ENABLE_IT();
204     __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
205 
206     /* Check if the Initialization mode is set */
207     if ((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
208     {
209       /* Set the Initialization mode */
210       hRTC_Handle.Instance->ISR = (uint32_t)RTC_INIT_MASK;
211       counter = 0U;
212       while ((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
213       {
214         if (counter++ == SystemCoreClock / 48U) /* Timeout = ~ 1s */
215         {
216           status = HAL_ERROR;
217         }
218       }
219     }
220   }
221   if (status == HAL_OK)
222   {
223     hRTC_Handle.Instance->DR = 0U;
224     hRTC_Handle.Instance->TR = 0U;
225 
226     hRTC_Handle.Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
227 
228     /* Enable the write protection for RTC registers */
229     __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
230 
231     /* Enable the RTC Alarm Interrupt */
232     HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
233 
234     /* Configure the SysTick IRQ priority */
235     if (TickPriority < (1UL << __NVIC_PRIO_BITS))
236     {
237       HAL_NVIC_SetPriority(RTC_Alarm_IRQn, TickPriority, 0U);
238       uwTickPrio = TickPriority;
239     }
240     else
241     {
242       status = HAL_ERROR;
243     }
244 
245   }
246   return status;
247 }
248 
249 /**
250   * @brief  Suspend Tick increment.
251   * @note   Disable the tick increment by disabling RTC ALARM interrupt.
252   * @param  None
253   * @retval None
254   */
HAL_SuspendTick(void)255 void HAL_SuspendTick(void)
256 {
257   /* Disable the write protection for RTC registers */
258   __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
259   /* Disable RTC ALARM update Interrupt */
260   __HAL_RTC_ALARM_DISABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
261   /* Enable the write protection for RTC registers */
262   __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
263 }
264 
265 /**
266   * @brief  Resume Tick increment.
267   * @note   Enable the tick increment by Enabling RTC ALARM interrupt.
268   * @param  None
269   * @retval None
270   */
HAL_ResumeTick(void)271 void HAL_ResumeTick(void)
272 {
273   /* Disable the write protection for RTC registers */
274   __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
275   /* Enable RTC ALARM Update interrupt */
276   __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
277   /* Enable the write protection for RTC registers */
278   __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
279 }
280 
281 /**
282   * @brief  ALARM A Event Callback in non blocking mode
283   * @note   This function is called  when RTC_ALARM interrupt took place, inside
284   * RTC_ALARM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
285   * a global variable "uwTick" used as application time base.
286   * @param  hrtc RTC handle
287   * @retval None
288   */
HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef * hrtc)289 void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
290 {
291   __IO uint32_t counter = 0U;
292 
293   HAL_IncTick();
294 
295   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
296 
297   /* Set the Initialization mode */
298   hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
299 
300   while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
301   {
302     if(counter++ == SystemCoreClock /48U) /* Timeout = ~ 1s */
303     {
304       break;
305     }
306   }
307 
308   hrtc->Instance->DR = 0U;
309   hrtc->Instance->TR = 0U;
310 
311   hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
312 
313   /* Enable the write protection for RTC registers */
314   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
315 }
316 
317 /**
318   * @brief  This function handles RTC ALARM interrupt request.
319   * @param  None
320   * @retval None
321   */
RTC_Alarm_IRQHandler(void)322 void RTC_Alarm_IRQHandler(void)
323 {
324   HAL_RTC_AlarmIRQHandler(&hRTC_Handle);
325 }
326 
327 /**
328   * @}
329   */
330 
331 /**
332   * @}
333   */
334 
335