STM32F4xx HAL Documentation
Hardware Abstraction Layer for STM32F4 familiy
Loading...
Searching...
No Matches
stm32f4xx_hal_timebase_rtc_alarm_template.c
Go to the documentation of this file.
1
47/* Includes ------------------------------------------------------------------*/
48#include "stm32f4xx_hal.h"
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#ifdef RTC_CLOCK_SOURCE_HSE
72 #define RTC_ASYNCH_PREDIV 99U
73 #define RTC_SYNCH_PREDIV 9U
74 #define RCC_RTCCLKSOURCE_1MHZ ((uint32_t)((uint32_t)RCC_BDCR_RTCSEL | (uint32_t)((HSE_VALUE/1000000U) << 16U)))
75#else /* RTC_CLOCK_SOURCE_LSE || RTC_CLOCK_SOURCE_LSI */
76 #define RTC_ASYNCH_PREDIV 0U
77 #define RTC_SYNCH_PREDIV 31U
78#endif /* RTC_CLOCK_SOURCE_HSE */
79
80/* Private macro -------------------------------------------------------------*/
81/* Private variables ---------------------------------------------------------*/
83/* Private function prototypes -----------------------------------------------*/
84void RTC_Alarm_IRQHandler(void);
85/* Private functions ---------------------------------------------------------*/
86
96HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
97{
98 __IO uint32_t counter = 0U;
99
100 RCC_OscInitTypeDef RCC_OscInitStruct;
101 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
102 HAL_StatusTypeDef status;
103
104#ifdef RTC_CLOCK_SOURCE_LSE
105 /* Configure LSE as RTC clock source */
106 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
107 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
108 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
109 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
110#elif defined (RTC_CLOCK_SOURCE_LSI)
111 /* Configure LSI as RTC clock source */
112 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
113 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
114 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
115 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
116#elif defined (RTC_CLOCK_SOURCE_HSE)
117 /* Configure HSE as RTC clock source */
118 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
119 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
120 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
121 /* Ensure that RTC is clocked by 1MHz */
122 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_1MHZ;
123#else
124#error Please select the RTC Clock source
125#endif /* RTC_CLOCK_SOURCE_LSE */
126
127 status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
128 if (status == HAL_OK)
129 {
130 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
131 status = HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
132 }
133 if (status == HAL_OK)
134 {
135 /* Enable RTC Clock */
137 /* The time base should be 1ms
138 Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
139 HSE as RTC clock
140 Time base = ((99 + 1) * (9 + 1)) / 1MHz
141 = 1ms
142 LSE as RTC clock
143 Time base = ((31 + 1) * (0 + 1)) / 32.768KHz
144 = ~1ms
145 LSI as RTC clock
146 Time base = ((31 + 1) * (0 + 1)) / 32KHz
147 = 1ms
148 */
149 hRTC_Handle.Instance = RTC;
156 status = HAL_RTC_Init(&hRTC_Handle);
157 }
158 if (status == HAL_OK)
159 {
160 /* Disable the write protection for RTC registers */
162
163 /* Disable the Alarm A interrupt */
165
166 /* Clear flag alarm A */
168
169 counter = 0U;
170 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
172 {
173 if (counter++ == (SystemCoreClock / 48U)) /* Timeout = ~ 1s */
174 {
175 status = HAL_ERROR;
176 }
177 }
178 }
179 if (status == HAL_OK)
180 {
181 hRTC_Handle.Instance->ALRMAR = (uint32_t)0x01U;
182
183 /* Configure the Alarm state: Enable Alarm */
185 /* Configure the Alarm interrupt */
187
188 /* RTC Alarm Interrupt Configuration: EXTI configuration */
191
192 /* Check if the Initialization mode is set */
193 if ((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
194 {
195 /* Set the Initialization mode */
196 hRTC_Handle.Instance->ISR = (uint32_t)RTC_INIT_MASK;
197 counter = 0U;
198 while ((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
199 {
200 if (counter++ == (SystemCoreClock / 48U)) /* Timeout = ~ 1s */
201 {
202 status = HAL_ERROR;
203 }
204 }
205 }
206 }
207 if (status == HAL_OK)
208 {
209 hRTC_Handle.Instance->DR = 0U;
210 hRTC_Handle.Instance->TR = 0U;
211
212 hRTC_Handle.Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
213
214 /* Enable the write protection for RTC registers */
216
217 /* Enable the RTC Alarm Interrupt */
218 HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
219
220 /* Configure the SysTick IRQ priority */
221 if (TickPriority < (1UL << __NVIC_PRIO_BITS))
222 {
223 HAL_NVIC_SetPriority(RTC_Alarm_IRQn, TickPriority, 0U);
224 uwTickPrio = TickPriority;
225 }
226 else
227 {
228 status = HAL_ERROR;
229 }
230
231 }
232 return status;
233}
234
241{
242 /* Disable the write protection for RTC registers */
244 /* Disable RTC ALARM update Interrupt */
246 /* Enable the write protection for RTC registers */
248}
249
256{
257 /* Disable the write protection for RTC registers */
259 /* Enable RTC ALARM Update interrupt */
261 /* Enable the write protection for RTC registers */
263}
264
274{
275 __IO uint32_t counter = 0U;
276
277 HAL_IncTick();
278
280
281 /* Set the Initialization mode */
282 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
283
284 while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
285 {
286 if(counter++ == (SystemCoreClock /48U)) /* Timeout = ~ 1s */
287 {
288 break;
289 }
290 }
291
292 hrtc->Instance->DR = 0U;
293 hrtc->Instance->TR = 0U;
294
295 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
296
297 /* Enable the write protection for RTC registers */
299}
300
309
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_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
ALARM A Event Callback in non blocking mode.
void HAL_ResumeTick(void)
Resume Tick increment.
void RTC_Alarm_IRQHandler(void)
This function handles RTC ALARM interrupt request.
RTC_HandleTypeDef hRTC_Handle
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
This function configures the RTC_ALARMA as a time base source. The time source is configured to have ...
void HAL_SuspendTick(void)
Suspend Tick increment.
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
HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
Initializes the RTC peripheral.
void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
Handles Alarm interrupt request.
#define __HAL_RTC_ALARM_EXTI_ENABLE_IT()
Enable interrupt on the RTC Alarm associated EXTI line.
#define __HAL_RTC_ALARMA_DISABLE(__HANDLE__)
Disable the RTC ALARMA peripheral.
#define __HAL_RTC_ALARMA_ENABLE(__HANDLE__)
Enable the RTC ALARMA peripheral.
#define __HAL_RTC_ALARM_CLEAR_FLAG(__HANDLE__, __FLAG__)
Clear the RTC Alarm's pending flags.
#define __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE()
Enable rising edge trigger on the RTC Alarm associated EXTI line.
#define __HAL_RTC_WRITEPROTECTION_ENABLE(__HANDLE__)
Enable the write protection for RTC registers.
#define __HAL_RTC_ALARM_GET_FLAG(__HANDLE__, __FLAG__)
Get the selected RTC Alarm's flag status.
#define __HAL_RTC_ALARM_ENABLE_IT(__HANDLE__, __INTERRUPT__)
Enable the RTC Alarm interrupt.
#define __HAL_RTC_ALARM_DISABLE_IT(__HANDLE__, __INTERRUPT__)
Disable the RTC Alarm interrupt.
#define __HAL_RTC_WRITEPROTECTION_DISABLE(__HANDLE__)
Disable the write protection for RTC registers.
#define RTC_FLAG_ALRAWF
#define RTC_FLAG_ALRAF
#define RTC_HOURFORMAT_24
#define RTC_IT_ALRA
#define RTC_OUTPUT_POLARITY_HIGH
#define RTC_OUTPUT_TYPE_OPENDRAIN
#define RTC_OUTPUT_DISABLE
#define RTC_INIT_MASK
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