1 /**
2   ******************************************************************************
3   * @file    stm32f1xx_hal_timebase_rtc_alarm_template.c
4   * @author  MCD Application Team
5   * @brief   HAL time base based on the hardware RTC_ALARM.
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 to increment the seconds registers each 1ms
10   *           + The alarm is configured to assert an interrupt when the RTC reaches 1ms
11   *           + HAL_IncTick is called at each Alarm event and the time is reset to 00:00:00
12   *           + HSE (default), LSE or LSI can be selected as RTC clock source
13   *
14   ******************************************************************************
15   * @attention
16   *
17   * Copyright (c) 2017 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 'stm32f1xx_hal_timebase_rtc_alarm.c'
32     (#) Add this file and the RTC HAL drivers to your project and uncomment
33        HAL_RTC_MODULE_ENABLED define in stm32f1xx_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 stm32f1xx_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 "stm32f1xx_hal.h"
49 /** @addtogroup STM32F1xx_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 /* Private macro -------------------------------------------------------------*/
72 /* Private variables ---------------------------------------------------------*/
73 RTC_HandleTypeDef        hRTC_Handle;
74 /* Private function prototypes -----------------------------------------------*/
75 void RTC_Alarm_IRQHandler(void);
76 /* Private functions ---------------------------------------------------------*/
77 
78 /**
79   * @brief  This function configures the RTC_ALARMA as a time base source.
80   *         The time source is configured  to have 1ms time base with a dedicated
81   *         Tick interrupt priority.
82   * @note   This function is called  automatically at the beginning of program after
83   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
84   * @param  TickPriority Tick interrupt priority.
85   * @retval HAL status
86   */
HAL_InitTick(uint32_t TickPriority)87 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
88 {
89   __IO uint32_t counter = 0U;
90 
91   RCC_OscInitTypeDef        RCC_OscInitStruct;
92   RCC_PeriphCLKInitTypeDef  PeriphClkInitStruct;
93 
94 #ifdef RTC_CLOCK_SOURCE_LSE
95   /* Configue LSE as RTC clock soucre */
96   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
97   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
98   RCC_OscInitStruct.LSEState = RCC_LSE_ON;
99   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
100 #elif defined (RTC_CLOCK_SOURCE_LSI)
101   /* Configue LSI as RTC clock soucre */
102   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
103   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
104   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
105   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
106 #elif defined (RTC_CLOCK_SOURCE_HSE)
107   /* Configue HSE as RTC clock soucre */
108   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
109   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
110   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
111   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV128;
112 #else
113 #error Please select the RTC Clock source
114 #endif /* RTC_CLOCK_SOURCE_LSE */
115   status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
116   if (status == HAL_OK)
117   {
118     PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
119     status = HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
120     if (status == HAL_OK)
121     {
122       /* Enable RTC Clock */
123       __HAL_RCC_RTC_ENABLE();
124 
125       /* Configure RTC time base to 10Khz */
126       hRTC_Handle.Instance = RTC;
127       hRTC_Handle.Init.AsynchPrediv = (HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_RTC) / 10000) - 1;
128       hRTC_Handle.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
129       status = HAL_RTC_Init(&hRTC_Handle);
130     }
131   }
132   if (status == HAL_OK)
133   {
134     /* Disable the write protection for RTC registers */
135     __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
136 
137     /* Clear flag alarm A */
138     __HAL_RTC_ALARM_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF);
139 
140     counter = 0U;
141     /* Wait till RTC ALRAF flag is set and if Time out is reached exit */
142     while (__HAL_RTC_ALARM_GET_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF) != RESET)
143     {
144       if (counter++ == SystemCoreClock / 48U) /* Timeout = ~ 1s */
145       {
146         status = HAL_ERROR;
147       }
148     }
149   }
150   if (status == HAL_OK)
151   {
152     /* Set RTC COUNTER MSB word */
153     hRTC_Handle.Instance->ALRH = 0x00U;
154     /* Set RTC COUNTER LSB word */
155     hRTC_Handle.Instance->ALRL = 0x09U;
156 
157     /* RTC Alarm Interrupt Configuration: EXTI configuration */
158     __HAL_RTC_ALARM_EXTI_ENABLE_IT();
159     __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
160 
161     /* Clear Second and overflow flags */
162     CLEAR_BIT(hRTC_Handle.Instance->CRL, (RTC_FLAG_SEC | RTC_FLAG_OW));
163 
164     /* Set RTC COUNTER MSB word */
165     hRTC_Handle.Instance->CNTH = 0x00U;
166     /* Set RTC COUNTER LSB word */
167     hRTC_Handle.Instance->CNTL = 0x00U;
168 
169     /* Configure the Alarm interrupt */
170     __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
171 
172     /* Enable the write protection for RTC registers */
173     __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
174 
175     /* Wait till RTC is in INIT state and if Time out is reached exit */
176     counter = 0U;
177     while ((hRTC_Handle.Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
178     {
179       if (counter++ == SystemCoreClock / 48U) /* Timeout = ~ 1s */
180       {
181         status = HAL_ERROR;
182       }
183     }
184   }
185   if (status == HAL_OK)
186   {
187     /* Enable the RTC global Interrupt */
188     HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
189 
190     /* Configure the SysTick IRQ priority */
191     if (TickPriority < (1UL << __NVIC_PRIO_BITS))
192     {
193       HAL_NVIC_SetPriority(RTC_Alarm_IRQn, TickPriority ,0U);
194       uwTickPrio = TickPriority;
195     }
196     else
197     {
198       status = HAL_ERROR;
199     }
200   }
201 
202   return status;
203 }
204 
205 /**
206   * @brief  Suspend Tick increment.
207   * @note   Disable the tick increment by disabling RTC ALARM interrupt.
208   * @param  None
209   * @retval None
210   */
HAL_SuspendTick(void)211 void HAL_SuspendTick(void)
212 {
213   /* Disable RTC ALARM update Interrupt */
214   __HAL_RTC_ALARM_DISABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
215 }
216 
217 /**
218   * @brief  Resume Tick increment.
219   * @note   Enable the tick increment by Enabling RTC ALARM interrupt.
220   * @param  None
221   * @retval None
222   */
HAL_ResumeTick(void)223 void HAL_ResumeTick(void)
224 {
225   __IO uint32_t counter = 0U;
226 
227   /* Disable the write protection for RTC registers */
228   __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
229 
230   /* Set RTC COUNTER MSB word */
231   hRTC_Handle.Instance->CNTH = 0x00U;
232   /* Set RTC COUNTER LSB word */
233   hRTC_Handle.Instance->CNTL = 0x00U;
234 
235   /* Clear Second and overflow flags */
236   CLEAR_BIT(hRTC_Handle.Instance->CRL, (RTC_FLAG_SEC | RTC_FLAG_OW | RTC_FLAG_ALRAF));
237 
238   /* Enable RTC ALARM Update interrupt */
239   __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
240 
241   /* Enable the write protection for RTC registers */
242   __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
243 
244   /* Wait till RTC is in INIT state and if Time out is reached exit */
245   while ((hRTC_Handle.Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
246   {
247     if (counter++ == SystemCoreClock / 48U) /* Timeout = ~ 1s */
248     {
249       break;
250     }
251   }
252 }
253 
254 /**
255   * @brief  ALARM A Event Callback in non blocking mode
256   * @note   This function is called  when RTC_ALARM interrupt took place, inside
257   * RTC_ALARM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
258   * a global variable "uwTick" used as application time base.
259   * @param  hrtc RTC handle
260   * @retval None
261   */
HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef * hrtc)262 void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
263 {
264   __IO uint32_t counter = 0U;
265 
266   HAL_IncTick();
267 
268   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
269 
270   /* Set RTC COUNTER MSB word */
271   WRITE_REG(hrtc->Instance->CNTH, 0x00U);
272   /* Set RTC COUNTER LSB word */
273   WRITE_REG(hrtc->Instance->CNTL, 0x00U);
274 
275   /* Clear Second and overflow flags */
276   CLEAR_BIT(hrtc->Instance->CRL, (RTC_FLAG_SEC | RTC_FLAG_OW));
277 
278   /* Enable the write protection for RTC registers */
279   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
280 
281   /* Wait till RTC is in INIT state and if Time out is reached exit */
282   while ((hrtc->Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
283   {
284     if (counter++ == SystemCoreClock / 48U) /* Timeout = ~ 1s */
285     {
286       break;
287     }
288   }
289 }
290 
291 /**
292   * @brief  This function handles RTC ALARM interrupt request.
293   * @retval None
294   */
RTC_Alarm_IRQHandler(void)295 void RTC_Alarm_IRQHandler(void)
296 {
297   HAL_RTC_AlarmIRQHandler(&hRTC_Handle);
298 }
299 
300 /**
301   * @}
302   */
303 
304 /**
305   * @}
306   */
307