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
46
57
58
59
60
67
68
69
70
71
72
76
79
80
81
84
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
115
116
121
122
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
164
165
166
167
168
169
170
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
221
222
223
224
225
226
227
228
229
230
231
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
259
260
265
269
270
273
274
/* ... */
#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;
Private variables
void RTC_WKUP_IRQHandler(void);
Private function prototypes
/* ... */
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_WAKEUPTIMER_DISABLE(&hRTC_Handle);
__HAL_RTC_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle,RTC_IT_WUT);
while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(&hRTC_Handle, RTC_FLAG_WUTWF) == RESET)
{
if(counter++ == SystemCoreClock /48U)
{
return HAL_ERROR;
}if (counter++ == SystemCoreClock /48U) { ... }
}while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(&hRTC_Handle, RTC_FLAG_WUTWF) == RESET) { ... }
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_WUTF);
hRTC_Handle.Instance->WUTR = (uint32_t)0U;
hRTC_Handle.Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
hRTC_Handle.Instance->CR |= (uint32_t)RTC_WAKEUPCLOCK_CK_SPRE_16BITS;
__HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
__HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
__HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle,RTC_IT_WUT);
__HAL_RTC_WAKEUPTIMER_ENABLE(&hRTC_Handle);
__HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
HAL_NVIC_SetPriority(RTC_WKUP_IRQn, TickPriority, 0U);
HAL_NVIC_EnableIRQ(RTC_WKUP_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_WAKEUPTIMER_DISABLE_IT(&hRTC_Handle, RTC_IT_WUT);
__HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
}{ ... }
/* ... */
void HAL_ResumeTick(void)
{
__HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
__HAL_RTC_WAKEUPTIMER_ENABLE_IT(&hRTC_Handle, RTC_IT_WUT);
__HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
}{ ... }
/* ... */
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
{
HAL_IncTick();
}{ ... }
/* ... */
void RTC_WKUP_IRQHandler(void)
{
HAL_RTCEx_WakeUpTimerIRQHandler(&hRTC_Handle);
}{ ... }
/* ... */
/* ... */