1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
55
56
57
58
65
66
67
68
69
70
74
77
78
79
80
81
82
83
84
85
94
95
96
97
98
99
100
101
102
107
108
113
114
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
170
171
172
173
174
175
176
177
178
179
180
181
182
183
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
218
219
220
221
222
223
224
225
226
227
228
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
270
271
272
273
274
275
276
277
278
279
280
285
289
290
293
294
/* ... */
#include "stm32f4xx_hal.h"
/* ... */
/* ... */
Includes
/* ... */
#define RTC_CLOCK_SOURCE_HSE
#ifdef RTC_CLOCK_SOURCE_HSE
#define RTC_ASYNCH_PREDIV 99U
#define RTC_SYNCH_PREDIV 9U
#define RCC_RTCCLKSOURCE_1MHZ ((uint32_t)((uint32_t)RCC_BDCR_RTCSEL | (uint32_t)((HSE_VALUE/1000000U) << 16U)))
/* ... */#else
#define RTC_ASYNCH_PREDIV 0U
#define RTC_SYNCH_PREDIV 31U
/* ... */#endif
Private define
RTC_HandleTypeDef hRTC_Handle;
void RTC_Alarm_IRQHandler(void);
/* ... */
HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority)
{
__IO uint32_t counter = 0U;
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
#ifdef RTC_CLOCK_SOURCE_LSE
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;/* ... */
#elif defined (RTC_CLOCK_SOURCE_LSI)
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;/* ... */
#elif defined (RTC_CLOCK_SOURCE_HSE)
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_1MHZ;/* ... */
#else
#error Please select the RTC Clock source
#endif
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK)
{
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK)
{
__HAL_RCC_RTC_ENABLE();
/* ... */
hRTC_Handle.Instance = RTC;
hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
HAL_RTC_Init(&hRTC_Handle);
__HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
__HAL_RTC_ALARMA_DISABLE(&hRTC_Handle);
__HAL_RTC_ALARM_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF);
counter = 0U;
while(__HAL_RTC_ALARM_GET_FLAG(&hRTC_Handle, RTC_FLAG_ALRAWF) == RESET)
{
if(counter++ == SystemCoreClock /48U)
{
return HAL_ERROR;
...}
}while (__HAL_RTC_ALARM_GET_FLAG(&hRTC_Handle, RTC_FLAG_ALRAWF) == RESET) { ... }
hRTC_Handle.Instance->ALRMAR = (uint32_t)0x01U;
__HAL_RTC_ALARMA_ENABLE(&hRTC_Handle);
__HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
__HAL_RTC_ALARM_EXTI_ENABLE_IT();
__HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
if((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
{
hRTC_Handle.Instance->ISR = (uint32_t)RTC_INIT_MASK;
counter = 0U;
while((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
{
if(counter++ == SystemCoreClock /48U)
{
return HAL_ERROR;
...}
}while ((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET) { ... }
}if ((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET) { ... }
hRTC_Handle.Instance->DR = 0U;
hRTC_Handle.Instance->TR = 0U;
hRTC_Handle.Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
__HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, TickPriority, 0U);
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
return HAL_OK;
}if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK) { ... }
}if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { ... }
return HAL_ERROR;
}{ ... }
/* ... */
void HAL_SuspendTick(void)
{
__HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
__HAL_RTC_ALARM_DISABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
__HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
}{ ... }
/* ... */
void HAL_ResumeTick(void)
{
__HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
__HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
__HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
}{ ... }
/* ... */
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
__IO uint32_t counter = 0U;
HAL_IncTick();
__HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
{
if(counter++ == SystemCoreClock /48U)
{
break;
...}
}while ((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET) { ... }
hrtc->Instance->DR = 0U;
hrtc->Instance->TR = 0U;
hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
__HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
}{ ... }
/* ... */
void RTC_Alarm_IRQHandler(void)
{
HAL_RTC_AlarmIRQHandler(&hRTC_Handle);
}{ ... }
/* ... */
/* ... */