comparison Common/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_timebase_rtc_alarm_template.c @ 128:c78bcbd5deda FlipDisplay

Added current STM32 standandard libraries in version independend folder structure
author Ideenmodellierer
date Sun, 17 Feb 2019 21:12:22 +0100
parents
children
comparison
equal deleted inserted replaced
127:1369f8660eaa 128:c78bcbd5deda
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_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 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 @verbatim
14 ==============================================================================
15 ##### How to use this driver #####
16 ==============================================================================
17 [..]
18 This file must be copied to the application folder and modified as follows:
19 (#) Rename it to 'stm32f4xx_hal_timebase_rtc_alarm.c'
20 (#) Add this file and the RTC HAL drivers to your project and uncomment
21 HAL_RTC_MODULE_ENABLED define in stm32f4xx_hal_conf.h
22
23 [..]
24 (@) HAL RTC alarm and HAL RTC wakeup drivers can’t be used with low power modes:
25 The wake up capability of the RTC may be intrusive in case of prior low power mode
26 configuration requiring different wake up sources.
27 Application/Example behavior is no more guaranteed
28 (@) The stm32f4xx_hal_timebase_tim use is recommended for the Applications/Examples
29 requiring low power modes
30
31 @endverbatim
32 ******************************************************************************
33 * @attention
34 *
35 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
36 *
37 * Redistribution and use in source and binary forms, with or without modification,
38 * are permitted provided that the following conditions are met:
39 * 1. Redistributions of source code must retain the above copyright notice,
40 * this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright notice,
42 * this list of conditions and the following disclaimer in the documentation
43 * and/or other materials provided with the distribution.
44 * 3. Neither the name of STMicroelectronics nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
54 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
55 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 *
59 ******************************************************************************
60 */
61
62 /* Includes ------------------------------------------------------------------*/
63 #include "stm32f4xx_hal.h"
64 /** @addtogroup STM32F4xx_HAL_Driver
65 * @{
66 */
67
68 /** @defgroup HAL_TimeBase_RTC_Alarm_Template HAL TimeBase RTC Alarm Template
69 * @{
70 */
71
72 /* Private typedef -----------------------------------------------------------*/
73 /* Private define ------------------------------------------------------------*/
74
75 /* Uncomment the line below to select the appropriate RTC Clock source for your application:
76 + RTC_CLOCK_SOURCE_HSE: can be selected for applications requiring timing precision.
77 + RTC_CLOCK_SOURCE_LSE: can be selected for applications with low constraint on timing
78 precision.
79 + RTC_CLOCK_SOURCE_LSI: can be selected for applications with low constraint on timing
80 precision.
81 */
82 #define RTC_CLOCK_SOURCE_HSE
83 /* #define RTC_CLOCK_SOURCE_LSE */
84 /* #define RTC_CLOCK_SOURCE_LSI */
85
86 #ifdef RTC_CLOCK_SOURCE_HSE
87 #define RTC_ASYNCH_PREDIV 99U
88 #define RTC_SYNCH_PREDIV 9U
89 #define RCC_RTCCLKSOURCE_1MHZ ((uint32_t)((uint32_t)RCC_BDCR_RTCSEL | (uint32_t)((HSE_VALUE/1000000U) << 16U)))
90 #else /* RTC_CLOCK_SOURCE_LSE || RTC_CLOCK_SOURCE_LSI */
91 #define RTC_ASYNCH_PREDIV 0U
92 #define RTC_SYNCH_PREDIV 31U
93 #endif /* RTC_CLOCK_SOURCE_HSE */
94
95 /* Private macro -------------------------------------------------------------*/
96 /* Private variables ---------------------------------------------------------*/
97 RTC_HandleTypeDef hRTC_Handle;
98 /* Private function prototypes -----------------------------------------------*/
99 void RTC_Alarm_IRQHandler(void);
100 /* Private functions ---------------------------------------------------------*/
101
102 /**
103 * @brief This function configures the RTC_ALARMA as a time base source.
104 * The time source is configured to have 1ms time base with a dedicated
105 * Tick interrupt priority.
106 * @note This function is called automatically at the beginning of program after
107 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
108 * @param TickPriority Tick interrupt priority.
109 * @retval HAL status
110 */
111 HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority)
112 {
113 __IO uint32_t counter = 0U;
114
115 RCC_OscInitTypeDef RCC_OscInitStruct;
116 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
117
118 #ifdef RTC_CLOCK_SOURCE_LSE
119 /* Configue LSE as RTC clock soucre */
120 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
121 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
122 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
123 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
124 #elif defined (RTC_CLOCK_SOURCE_LSI)
125 /* Configue LSI as RTC clock soucre */
126 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
127 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
128 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
129 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
130 #elif defined (RTC_CLOCK_SOURCE_HSE)
131 /* Configue HSE as RTC clock soucre */
132 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
133 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
134 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
135 /* Ensure that RTC is clocked by 1MHz */
136 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_1MHZ;
137 #else
138 #error Please select the RTC Clock source
139 #endif /* RTC_CLOCK_SOURCE_LSE */
140
141 if(HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK)
142 {
143 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
144 if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK)
145 {
146 /* Enable RTC Clock */
147 __HAL_RCC_RTC_ENABLE();
148 /* The time base should be 1ms
149 Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
150 HSE as RTC clock
151 Time base = ((99 + 1) * (9 + 1)) / 1MHz
152 = 1ms
153 LSE as RTC clock
154 Time base = ((31 + 1) * (0 + 1)) / 32.768KHz
155 = ~1ms
156 LSI as RTC clock
157 Time base = ((31 + 1) * (0 + 1)) / 32KHz
158 = 1ms
159 */
160 hRTC_Handle.Instance = RTC;
161 hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
162 hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
163 hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
164 hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
165 hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
166 hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
167 HAL_RTC_Init(&hRTC_Handle);
168
169 /* Disable the write protection for RTC registers */
170 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
171
172 /* Disable the Alarm A interrupt */
173 __HAL_RTC_ALARMA_DISABLE(&hRTC_Handle);
174
175 /* Clear flag alarm A */
176 __HAL_RTC_ALARM_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF);
177
178 counter = 0U;
179 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
180 while(__HAL_RTC_ALARM_GET_FLAG(&hRTC_Handle, RTC_FLAG_ALRAWF) == RESET)
181 {
182 if(counter++ == (SystemCoreClock /48U)) /* Timeout = ~ 1s */
183 {
184 return HAL_ERROR;
185 }
186 }
187
188 hRTC_Handle.Instance->ALRMAR = (uint32_t)0x01U;
189
190 /* Configure the Alarm state: Enable Alarm */
191 __HAL_RTC_ALARMA_ENABLE(&hRTC_Handle);
192 /* Configure the Alarm interrupt */
193 __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
194
195 /* RTC Alarm Interrupt Configuration: EXTI configuration */
196 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
197 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
198
199 /* Check if the Initialization mode is set */
200 if((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
201 {
202 /* Set the Initialization mode */
203 hRTC_Handle.Instance->ISR = (uint32_t)RTC_INIT_MASK;
204 counter = 0U;
205 while((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
206 {
207 if(counter++ == (SystemCoreClock /48U)) /* Timeout = ~ 1s */
208 {
209 return HAL_ERROR;
210 }
211 }
212 }
213 hRTC_Handle.Instance->DR = 0U;
214 hRTC_Handle.Instance->TR = 0U;
215
216 hRTC_Handle.Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
217
218 /* Enable the write protection for RTC registers */
219 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
220
221 HAL_NVIC_SetPriority(RTC_Alarm_IRQn, TickPriority, 0U);
222 HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
223 return HAL_OK;
224 }
225 }
226 return HAL_ERROR;
227 }
228
229 /**
230 * @brief Suspend Tick increment.
231 * @note Disable the tick increment by disabling RTC ALARM interrupt.
232 * @retval None
233 */
234 void HAL_SuspendTick(void)
235 {
236 /* Disable the write protection for RTC registers */
237 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
238 /* Disable RTC ALARM update Interrupt */
239 __HAL_RTC_ALARM_DISABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
240 /* Enable the write protection for RTC registers */
241 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
242 }
243
244 /**
245 * @brief Resume Tick increment.
246 * @note Enable the tick increment by Enabling RTC ALARM interrupt.
247 * @retval None
248 */
249 void HAL_ResumeTick(void)
250 {
251 /* Disable the write protection for RTC registers */
252 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
253 /* Enable RTC ALARM Update interrupt */
254 __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
255 /* Enable the write protection for RTC registers */
256 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
257 }
258
259 /**
260 * @brief ALARM A Event Callback in non blocking mode
261 * @note This function is called when RTC_ALARM interrupt took place, inside
262 * RTC_ALARM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
263 * a global variable "uwTick" used as application time base.
264 * @param hrtc RTC handle
265 * @retval None
266 */
267 void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
268 {
269 __IO uint32_t counter = 0U;
270
271 HAL_IncTick();
272
273 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
274
275 /* Set the Initialization mode */
276 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
277
278 while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
279 {
280 if(counter++ == (SystemCoreClock /48U)) /* Timeout = ~ 1s */
281 {
282 break;
283 }
284 }
285
286 hrtc->Instance->DR = 0U;
287 hrtc->Instance->TR = 0U;
288
289 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
290
291 /* Enable the write protection for RTC registers */
292 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
293 }
294
295 /**
296 * @brief This function handles RTC ALARM interrupt request.
297 * @retval None
298 */
299 void RTC_Alarm_IRQHandler(void)
300 {
301 HAL_RTC_AlarmIRQHandler(&hRTC_Handle);
302 }
303
304 /**
305 * @}
306 */
307
308 /**
309 * @}
310 */
311
312 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/