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
45
50
51
52
57
58
59
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
108
109
110
111
112
113
114
115
116
117
118
119
120
121
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
154
155
156
157
158
159
160
161
162
163
164
165
166
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
201
202
203
204
205
206
207
208
209
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
236
237
238
239
240
241
242
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
293
294
295
296
297
298
304
305
306
307
308
309
310
311
312
313
314
327
328
329
330
331
336
337
338
339
340
341
342
343
344
345
346
353
354
355
356
357
358
359
360
361
362
363
376
377
378
379
384
385
386
387
388
389
390
391
392
393
394
395
396
397
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
422
427
428
429
430
431
432
433
434
435
436
437
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
474
475
476
477
478
479
480
481
484
485
486
487
488
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
511
516
517
518
537
538
541
542
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define LSI_TIMEOUT_VALUE ((uint32_t)2)
#define LSE_TIMEOUT_VALUE ((uint32_t)5000)
#define RTC_TIMEOUT_VALUE ((uint32_t)1000)
#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);
uint32_t Enter_RTC_InitMode(void);
uint32_t Exit_RTC_InitMode(void);
uint32_t WaitForSynchro_RTC(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_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
LL_RTC_DisableWriteProtection(RTC);
if (Enter_RTC_InitMode() != RTC_ERROR_NONE)
{
LED_Blinking(LED_BLINK_ERROR);
}if (Enter_RTC_InitMode() != RTC_ERROR_NONE) { ... }
LL_RTC_SetHourFormat(RTC, LL_RTC_HOURFORMAT_AMPM);
LL_RTC_SetAsynchPrescaler(RTC, RTC_ASYNCH_PREDIV);
LL_RTC_SetSynchPrescaler(RTC, RTC_SYNCH_PREDIV);
Exit_RTC_InitMode();
LL_RTC_EnableWriteProtection(RTC);
}{ ... }
/* ... */
void Configure_RTC_Alarm(void)
{
LL_RTC_DisableWriteProtection(RTC);
if (Enter_RTC_InitMode() != RTC_ERROR_NONE)
{
LED_Blinking(LED_BLINK_ERROR);
}if (Enter_RTC_InitMode() != RTC_ERROR_NONE) { ... }
LL_RTC_DATE_Config(RTC, LL_RTC_WEEKDAY_FRIDAY, 0x29, LL_RTC_MONTH_DECEMBER, 0x16);
LL_RTC_TIME_Config(RTC, LL_RTC_TIME_FORMAT_PM, 0x11, 0x59, 0x55);
/* ... */
LL_RTC_ALMA_ConfigTime(RTC, LL_RTC_ALMA_TIME_FORMAT_AM, 0x12, 0x00, 0x25);
LL_RTC_ALMA_SetMask(RTC, LL_RTC_ALMA_MASK_DATEWEEKDAY);
LL_RTC_ALMA_Enable(RTC);
LL_RTC_ClearFlag_ALRA(RTC);
LL_RTC_EnableIT_ALRA(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);
if (Exit_RTC_InitMode() != RTC_ERROR_NONE)
{
LED_Blinking(LED_BLINK_ERROR);
}if (Exit_RTC_InitMode() != RTC_ERROR_NONE) { ... }
LL_RTC_EnableWriteProtection(RTC);
}{ ... }
/* ... */
uint32_t Enter_RTC_InitMode(void)
{
LL_RTC_EnableInitMode(RTC);
#if (USE_TIMEOUT == 1)
Timeout = RTC_TIMEOUT_VALUE;
#endif
while (LL_RTC_IsActiveFlag_INIT(RTC) != 1)
{
#if (USE_TIMEOUT == 1)
if (LL_SYSTICK_IsActiveCounterFlag())
{
Timeout --;
}if (LL_SYSTICK_IsActiveCounterFlag()) { ... }
if (Timeout == 0)
{
return RTC_ERROR_TIMEOUT;
}if (Timeout == 0) { ... } /* ... */
#endif
}while (LL_RTC_IsActiveFlag_INIT(RTC) != 1) { ... }
return RTC_ERROR_NONE;
}{ ... }
/* ... */
uint32_t Exit_RTC_InitMode(void)
{
LL_RTC_DisableInitMode(RTC);
return (WaitForSynchro_RTC());
}{ ... }
/* ... */
uint32_t WaitForSynchro_RTC(void)
{
LL_RTC_ClearFlag_RS(RTC);
#if (USE_TIMEOUT == 1)
Timeout = RTC_TIMEOUT_VALUE;
#endif
while(LL_RTC_IsActiveFlag_RS(RTC) != 1)
{
#if (USE_TIMEOUT == 1)
if (LL_SYSTICK_IsActiveCounterFlag())
{
Timeout --;
}if (LL_SYSTICK_IsActiveCounterFlag()) { ... }
if (Timeout == 0)
{
return RTC_ERROR_TIMEOUT;
}if (Timeout == 0) { ... } /* ... */
#endif
}while (LL_RTC_IsActiveFlag_RS(RTC) != 1) { ... }
return RTC_ERROR_NONE;
}{ ... }
/* ... */
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
/* ... */
/* ... */