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