STM32F4xx HAL Documentation
Hardware Abstraction Layer for STM32F4 familiy
Loading...
Searching...
No Matches
stm32f4xx_hal_timebase_rtc_wakeup_template.c
Go to the documentation of this file.
1
48/* Includes ------------------------------------------------------------------*/
49#include "stm32f4xx_hal.h"
58/* Private typedef -----------------------------------------------------------*/
59/* Private define ------------------------------------------------------------*/
60
61/* Uncomment the line below to select the appropriate RTC Clock source for your application:
62 + RTC_CLOCK_SOURCE_HSE: can be selected for applications requiring timing precision.
63 + RTC_CLOCK_SOURCE_LSE: can be selected for applications with low constraint on timing
64 precision.
65 + RTC_CLOCK_SOURCE_LSI: can be selected for applications with low constraint on timing
66 precision.
67 */
68#define RTC_CLOCK_SOURCE_HSE
69/* #define RTC_CLOCK_SOURCE_LSE */
70/* #define RTC_CLOCK_SOURCE_LSI */
71
72#ifdef RTC_CLOCK_SOURCE_HSE
73 #define RTC_ASYNCH_PREDIV 99U
74 #define RTC_SYNCH_PREDIV 9U
75 #define RCC_RTCCLKSOURCE_1MHZ ((uint32_t)((uint32_t)RCC_BDCR_RTCSEL | (uint32_t)((HSE_VALUE/1000000U) << 16U)))
76#else /* RTC_CLOCK_SOURCE_LSE || RTC_CLOCK_SOURCE_LSI */
77 #define RTC_ASYNCH_PREDIV 0U
78 #define RTC_SYNCH_PREDIV 31U
79#endif /* RTC_CLOCK_SOURCE_HSE */
80
81/* Private macro -------------------------------------------------------------*/
82/* Private variables ---------------------------------------------------------*/
84
85/* Private function prototypes -----------------------------------------------*/
86void RTC_WKUP_IRQHandler(void);
87
88/* Private functions ---------------------------------------------------------*/
89
103HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority)
104{
105 __IO uint32_t counter = 0U;
106
107 RCC_OscInitTypeDef RCC_OscInitStruct;
108 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
109 HAL_StatusTypeDef status;
110
111#ifdef RTC_CLOCK_SOURCE_LSE
112 /* Configure LSE as RTC clock source */
113 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
114 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
115 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
116 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
117#elif defined (RTC_CLOCK_SOURCE_LSI)
118 /* Configure LSI as RTC clock source */
119 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
120 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
121 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
122 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
123#elif defined (RTC_CLOCK_SOURCE_HSE)
124 /* Configure HSE as RTC clock source */
125 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
126 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
127 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
128 /* Ensure that RTC is clocked by 1MHz */
129 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_1MHZ;
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 */
144 /* The time base should be 1ms
145 Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
146 HSE as RTC clock
147 Time base = ((99 + 1) * (9 + 1)) / 1Mhz
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 = ((31 + 1) * (0 + 1)) / 32Khz
154 = 1ms
155 */
156 hRTC_Handle.Instance = RTC;
163 status = HAL_RTC_Init(&hRTC_Handle);
164 }
165 if (status == HAL_OK)
166 {
167 /* Disable the write protection for RTC registers */
169
170 /* Disable the Wake-up Timer */
172
173 /* In case of interrupt mode is used, the interrupt source must disabled */
175
176 /* Wait till RTC WUTWF flag is set */
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 */
189
190 /* Clear RTC Wake Up timer Flag */
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 */
201
202 /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
204
206
207 /* Configure the Interrupt in the RTC_CR register */
209
210 /* Enable the Wake-up Timer */
212
213 /* Enable the write protection for RTC registers */
215
216 /* Enable the RTC global Interrupt */
217 HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
218
219 /* Configure the SysTick IRQ priority */
220 if (TickPriority < (1UL << __NVIC_PRIO_BITS))
221 {
222 HAL_NVIC_SetPriority(RTC_WKUP_IRQn, TickPriority, 0U);
223 uwTickPrio = TickPriority;
224 }
225 else
226 {
227 status = HAL_ERROR;
228 }
229 }
230 return status;
231}
232
239{
240 /* Disable the write protection for RTC registers */
242 /* Disable WAKE UP TIMER Interrupt */
244 /* Enable the write protection for RTC registers */
246}
247
254{
255 /* Disable the write protection for RTC registers */
257 /* Enable WAKE UP TIMER interrupt */
259 /* Enable the write protection for RTC registers */
261}
262
275
284
void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
Sets the priority of an interrupt.
void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
Enables a device specific interrupt in the NVIC interrupt controller.
void HAL_IncTick(void)
This function is called to increment a global variable "uwTick" used as application time base.
uint32_t uwTickPrio
void HAL_ResumeTick(void)
Resume Tick increment.
void RTC_WKUP_IRQHandler(void)
This function handles WAKE UP TIMER interrupt request.
RTC_HandleTypeDef hRTC_Handle
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
This function configures the RTC_WKUP as a time base source. The time source is configured to have 1m...
void HAL_SuspendTick(void)
Suspend Tick increment.
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
Wake Up Timer Event Callback in non blocking mode.
#define __HAL_PWR_CLEAR_FLAG(__FLAG__)
Clear the PWR's pending flags.
#define PWR_FLAG_WU
HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
Initializes the RCC extended peripherals clocks according to the specified parameters in the RCC_Peri...
#define RCC_PERIPHCLK_RTC
HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
Initializes the RCC Oscillators according to the specified parameters in the RCC_OscInitTypeDef.
#define RCC_HSE_ON
#define __HAL_RCC_RTC_ENABLE()
Macros to enable or disable the RTC clock.
#define RCC_LSE_ON
#define RCC_LSI_ON
#define RCC_OSCILLATORTYPE_HSE
#define RCC_OSCILLATORTYPE_LSI
#define RCC_OSCILLATORTYPE_LSE
#define RCC_PLL_NONE
#define RCC_RTCCLKSOURCE_LSE
#define RCC_RTCCLKSOURCE_LSI
void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
Handles Wakeup Timer interrupt request.
#define __HAL_RTC_WAKEUPTIMER_DISABLE(__HANDLE__)
Disable the RTC Wakeup Timer peripheral.
#define __HAL_RTC_WAKEUPTIMER_GET_FLAG(__HANDLE__, __FLAG__)
Get the selected RTC Wakeup Timer's flag status.
#define __HAL_RTC_WAKEUPTIMER_DISABLE_IT(__HANDLE__, __INTERRUPT__)
Disable the RTC Wakeup Timer interrupt.
#define __HAL_RTC_WAKEUPTIMER_ENABLE(__HANDLE__)
Enable the RTC WakeUp Timer peripheral.
#define __HAL_RTC_WAKEUPTIMER_ENABLE_IT(__HANDLE__, __INTERRUPT__)
Enable the RTC Wakeup Timer interrupt.
#define __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(__HANDLE__, __FLAG__)
Clear the RTC Wakeup timer's pending flags.
#define __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE()
Enable rising edge trigger on the RTC Wakeup Timer associated EXTI line.
#define __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT()
Enable interrupt on the RTC Wakeup Timer associated EXTI line.
#define RTC_WAKEUPCLOCK_CK_SPRE_16BITS
HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
Initializes the RTC peripheral.
#define __HAL_RTC_WRITEPROTECTION_ENABLE(__HANDLE__)
Enable the write protection for RTC registers.
#define __HAL_RTC_WRITEPROTECTION_DISABLE(__HANDLE__)
Disable the write protection for RTC registers.
#define RTC_FLAG_WUTWF
#define RTC_FLAG_WUTF
#define RTC_HOURFORMAT_24
#define RTC_IT_WUT
#define RTC_OUTPUT_POLARITY_HIGH
#define RTC_OUTPUT_TYPE_OPENDRAIN
#define RTC_OUTPUT_DISABLE
This file contains all the functions prototypes for the HAL module driver.
HAL_StatusTypeDef
HAL Status structures definition
@ HAL_ERROR
@ HAL_OK
RCC Internal/External Oscillator (HSE, HSI, LSE and LSI) configuration structure definition.
RCC_PLLInitTypeDef PLL
RCC extended clocks structure definition.
RTC Handle Structure definition.
RTC_InitTypeDef Init