1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
32
33
34
35
36
37
38
39
40
41
42
43
44
49
50
51
56
57
58
67
76
77
78
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
104
105
106
111
112
113
114
115
116
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
149
150
151
152
153
154
155
156
157
158
159
160
161
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
194
195
200
201
202
203
204
205
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
240
241
242
243
244
245
246
247
248
249
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
303
304
305
306
307
308
309
310
311
312
313
314
315
316
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
341
346
347
348
349
350
351
352
353
354
355
356
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
393
394
395
396
397
398
399
400
403
404
405
406
407
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
430
435
436
437
456
457
460
461
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define LSI_TIMEOUT_VALUE ((uint32_t)2)
#define LSE_TIMEOUT_VALUE ((uint32_t)5000)
#define RTC_CLOCK_SOURCE_LSI
#ifdef RTC_CLOCK_SOURCE_LSI
#define RTC_ASYNCH_PREDIV ((uint32_t)0x7F)
#define RTC_SYNCH_PREDIV ((uint32_t)0x00F9)
/* ... */#endif
#ifdef RTC_CLOCK_SOURCE_LSE
#define RTC_ASYNCH_PREDIV ((uint32_t)0x7F)
#define RTC_SYNCH_PREDIV ((uint32_t)0x00FF)
/* ... */#endif
Private define
uint8_t aShowTime[50] = {0};
uint8_t aShowDate[50] = {0};
#if (USE_TIMEOUT == 1)
uint32_t Timeout = 0;
#endif
Private variables
void SystemClock_Config(void);
void Configure_RTC(void);
void Configure_RTC_Alarm(void);
void Show_RTC_Calendar(void);
void LED_Init(void);
void LED_On(void);
void LED_Blinking(uint32_t Period);
Private function prototypes
/* ... */
int main(void)
{
SystemClock_Config();
LED_Init();
Configure_RTC();
Configure_RTC_Alarm();
while (1)
{
Show_RTC_Calendar();
}while (1) { ... }
}{ ... }
/* ... */
void Configure_RTC(void)
{
LL_RTC_InitTypeDef rtc_initstruct;
/* ... */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
LL_PWR_EnableBkUpAccess();
#ifdef RTC_CLOCK_SOURCE_LSE
if (LL_RCC_LSE_IsReady() == 0)
{
LL_RCC_ForceBackupDomainReset();
LL_RCC_ReleaseBackupDomainReset();
LL_RCC_LSE_Enable();
#if (USE_TIMEOUT == 1)
Timeout = LSE_TIMEOUT_VALUE;
#endif
while (LL_RCC_LSE_IsReady() != 1)
{
#if (USE_TIMEOUT == 1)
if (LL_SYSTICK_IsActiveCounterFlag())
{
Timeout --;
}if (LL_SYSTICK_IsActiveCounterFlag()) { ... }
if (Timeout == 0)
{
LED_Blinking(LED_BLINK_ERROR);
}if (Timeout == 0) { ... } /* ... */
#endif
}while (LL_RCC_LSE_IsReady() != 1) { ... }
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
LL_RCC_EnableRTC();
}if (LL_RCC_LSE_IsReady() == 0) { ... }
/* ... */#elif defined(RTC_CLOCK_SOURCE_LSI)
LL_RCC_LSI_Enable();
#if (USE_TIMEOUT == 1)
Timeout = LSI_TIMEOUT_VALUE;
#endif
while (LL_RCC_LSI_IsReady() != 1)
{
#if (USE_TIMEOUT == 1)
if (LL_SYSTICK_IsActiveCounterFlag())
{
Timeout --;
}if (LL_SYSTICK_IsActiveCounterFlag()) { ... }
if (Timeout == 0)
{
LED_Blinking(LED_BLINK_ERROR);
}if (Timeout == 0) { ... } /* ... */
#endif
}while (LL_RCC_LSI_IsReady() != 1) { ... }
LL_RCC_ForceBackupDomainReset();
LL_RCC_ReleaseBackupDomainReset();
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSI);
LL_RCC_EnableRTC();
/* ... */
#else
#error "configure clock for RTC"
#endif
if (LL_RTC_DeInit(RTC) != SUCCESS)
{
LED_Blinking(LED_BLINK_ERROR);
}if (LL_RTC_DeInit(RTC) != SUCCESS) { ... }
/* ... */
rtc_initstruct.HourFormat = LL_RTC_HOURFORMAT_AMPM;
rtc_initstruct.AsynchPrescaler = RTC_ASYNCH_PREDIV;
rtc_initstruct.SynchPrescaler = RTC_SYNCH_PREDIV;
if (LL_RTC_Init(RTC, &rtc_initstruct) != SUCCESS)
{
LED_Blinking(LED_BLINK_ERROR);
}if (LL_RTC_Init(RTC, &rtc_initstruct) != SUCCESS) { ... }
}{ ... }
/* ... */
void Configure_RTC_Alarm(void)
{
LL_RTC_DateTypeDef rtc_date_initstruct;
LL_RTC_TimeTypeDef rtc_time_initstruct;
LL_RTC_AlarmTypeDef rtc_alarm_initstruct;
rtc_date_initstruct.WeekDay = LL_RTC_WEEKDAY_FRIDAY;
rtc_date_initstruct.Day = 0x29;
rtc_date_initstruct.Month = LL_RTC_MONTH_DECEMBER;
rtc_date_initstruct.Year = 0x16;
if (LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BCD, &rtc_date_initstruct) != SUCCESS)
{
LED_Blinking(LED_BLINK_ERROR);
}if (LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BCD, &rtc_date_initstruct) != SUCCESS) { ... }
rtc_time_initstruct.TimeFormat = LL_RTC_TIME_FORMAT_PM;
rtc_time_initstruct.Hours = 0x11;
rtc_time_initstruct.Minutes = 0x59;
rtc_time_initstruct.Seconds = 0x55;
if (LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &rtc_time_initstruct) != SUCCESS)
{
LED_Blinking(LED_BLINK_ERROR);
}if (LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &rtc_time_initstruct) != SUCCESS) { ... }
rtc_alarm_initstruct.AlarmTime.TimeFormat = LL_RTC_ALMA_TIME_FORMAT_AM;
rtc_alarm_initstruct.AlarmTime.Hours = 0x12;
rtc_alarm_initstruct.AlarmTime.Minutes = 0x00;
rtc_alarm_initstruct.AlarmTime.Seconds = 0x25;
rtc_alarm_initstruct.AlarmMask = LL_RTC_ALMA_MASK_DATEWEEKDAY;
rtc_alarm_initstruct.AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE;
rtc_alarm_initstruct.AlarmDateWeekDay = 0x01;
if (LL_RTC_ALMA_Init(RTC, LL_RTC_FORMAT_BCD, &rtc_alarm_initstruct) != SUCCESS)
{
LED_Blinking(LED_BLINK_ERROR);
}if (LL_RTC_ALMA_Init(RTC, LL_RTC_FORMAT_BCD, &rtc_alarm_initstruct) != SUCCESS) { ... }
LL_RTC_DisableWriteProtection(RTC);
LL_RTC_ALMA_Enable(RTC);
LL_RTC_ClearFlag_ALRA(RTC);
LL_RTC_EnableIT_ALRA(RTC);
LL_RTC_EnableWriteProtection(RTC);
LL_EXTI_EnableIT_0_31(LL_EXTI_LINE_17);
LL_EXTI_EnableRisingTrig_0_31(LL_EXTI_LINE_17);
NVIC_SetPriority(RTC_Alarm_IRQn, 0x0F);
NVIC_EnableIRQ(RTC_Alarm_IRQn);
}{ ... }
/* ... */
void Show_RTC_Calendar(void)
{
sprintf((char*)aShowTime,"%.2d:%.2d:%.2d", __LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetHour(RTC)),
__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetMinute(RTC)),
__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetSecond(RTC)));
sprintf((char*)aShowDate,"%.2d-%.2d-%.2d", __LL_RTC_CONVERT_BCD2BIN(LL_RTC_DATE_GetMonth(RTC)),
__LL_RTC_CONVERT_BCD2BIN(LL_RTC_DATE_GetDay(RTC)),
2000 + __LL_RTC_CONVERT_BCD2BIN(LL_RTC_DATE_GetYear(RTC)));
}{ ... }
/* ... */
void LED_Init(void)
{
LED2_GPIO_CLK_ENABLE();
LL_GPIO_SetPinMode(LED2_GPIO_PORT, LED2_PIN, LL_GPIO_MODE_OUTPUT);
}{ ... }
/* ... */
void LED_On(void)
{
LL_GPIO_SetOutputPin(LED2_GPIO_PORT, LED2_PIN);
}{ ... }
/* ... */
void LED_Blinking(uint32_t Period)
{
while (1)
{
LL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
LL_mDelay(Period);
}while (1) { ... }
}{ ... }
/* ... */
void SystemClock_Config(void)
{
LL_RCC_HSE_EnableBypass();
LL_RCC_HSE_Enable();
while(LL_RCC_HSE_IsReady() != 1)
{
}while (LL_RCC_HSE_IsReady() != 1) { ... };
LL_FLASH_SetLatency(LL_FLASH_LATENCY_3);
LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_8, 400, LL_RCC_PLLP_DIV_4);
LL_RCC_PLL_Enable();
while(LL_RCC_PLL_IsReady() != 1)
{
}while (LL_RCC_PLL_IsReady() != 1) { ... };
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
{
}while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) { ... };
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
SysTick_Config(100000000 / 1000);
SystemCoreClock = 100000000;
}{ ... }
/* ... */
void Alarm_Callback(void)
{
LED_On();
}{ ... }
#ifdef USE_FULL_ASSERT
/* ... */
void assert_failed(uint8_t *file, uint32_t line)
{
/* ... */
while (1)
{
}while (1) { ... }
}assert_failed (uint8_t *file, uint32_t line) { ... }
/* ... */#endif
/* ... */
/* ... */