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
46
47
48
53
54
55
60
61
62
69
78
79
80
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
126
127
128
133
134
135
136
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
169
170
171
172
173
174
175
176
177
178
179
180
181
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
237
238
242
243
244
245
246
247
248
249
250
255
256
257
258
259
260
261
262
264
265
266
267
268
269
270
271
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
331
332
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
362
363
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
399
400
401
402
403
404
405
406
409
410
411
412
413
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
436
441
442
443
462
463
466
467
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define RTC_WUT_TIME ((uint32_t)5)
#define LSI_TIMEOUT_VALUE ((uint32_t)100)
#define LSE_TIMEOUT_VALUE ((uint32_t)5000)
#define RTC_TIMEOUT_VALUE ((uint32_t)1000)
#define RTC_CLOCK_SOURCE_LSI
5 defines
#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
__IO uint8_t ubButtonPress = 0;
#if (USE_TIMEOUT == 1)
uint32_t Timeout = 0;
#endif
Private variables
void SystemClock_Config(void);
void Configure_RTC(void);
void EnterStandbyMode(void);
void LED_Init(void);
void LED_Blinking(uint32_t Period);
void UserButton_Init(void);
void WaitForUserButtonPress(void);
Private function prototypes
/* ... */
int main(void)
{
SystemClock_Config();
LED_Init();
UserButton_Init();
Configure_RTC();
if(LL_PWR_IsActiveFlag_SB() != 1)
{
WaitForUserButtonPress();
EnterStandbyMode();
}if (LL_PWR_IsActiveFlag_SB() != 1) { ... }
else
{
LL_PWR_ClearFlag_SB();
LL_RTC_ClearFlag_WUT(RTC);
LED_Blinking(LED_BLINK_SLOW);
}else { ... }
while (1)
{
}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);
LL_RTC_SetAsynchPrescaler(RTC, RTC_ASYNCH_PREDIV);
LL_RTC_SetSynchPrescaler(RTC, RTC_SYNCH_PREDIV);
LL_RTC_WAKEUP_Disable(RTC);
#if (USE_TIMEOUT == 1)
Timeout = RTC_TIMEOUT_VALUE;
#endif
while (LL_RTC_IsActiveFlag_WUTW(RTC) != 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_RTC_IsActiveFlag_WUTW(RTC) != 1) { ... }
/* ... */
LL_RTC_WAKEUP_SetAutoReload(RTC, RTC_WUT_TIME);
LL_RTC_WAKEUP_SetClock(RTC, LL_RTC_WAKEUPCLOCK_CKSPRE);
LL_RTC_EnableWriteProtection(RTC);
}{ ... }
/* ... */
void EnterStandbyMode(void)
{
LL_RTC_DisableWriteProtection(RTC);
/* ... */
LL_RTC_EnableIT_WUT(RTC);
LL_RTC_WAKEUP_Enable(RTC);
LL_RTC_EnableWriteProtection(RTC);
/* ... */
LL_RTC_ClearFlag_WUT(RTC);
LL_PWR_SetPowerMode(LL_PWR_MODE_STANDBY);
LL_LPM_EnableDeepSleep();
#if defined ( __CC_ARM)
__force_stores();
#endif
__WFI();
}{ ... }
/* ... */
void LED_Init(void)
{
LED2_GPIO_CLK_ENABLE();
LL_GPIO_SetPinMode(LED2_GPIO_PORT, LED2_PIN, LL_GPIO_MODE_OUTPUT);
}{ ... }
/* ... */
void LED_Blinking(uint32_t Period)
{
while (1)
{
LL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
LL_mDelay(Period);
}while (1) { ... }
}{ ... }
/* ... */
void UserButton_Init(void)
{
USER_BUTTON_GPIO_CLK_ENABLE();
LL_GPIO_SetPinMode(USER_BUTTON_GPIO_PORT, USER_BUTTON_PIN, LL_GPIO_MODE_INPUT);
LL_GPIO_SetPinPull(USER_BUTTON_GPIO_PORT, USER_BUTTON_PIN, LL_GPIO_PULL_NO);
USER_BUTTON_SYSCFG_SET_EXTI();
USER_BUTTON_EXTI_LINE_ENABLE();
USER_BUTTON_EXTI_FALLING_TRIG_ENABLE();
NVIC_EnableIRQ(USER_BUTTON_EXTI_IRQn);
NVIC_SetPriority(USER_BUTTON_EXTI_IRQn,0x03);
}{ ... }
/* ... */
void WaitForUserButtonPress(void)
{
while (ubButtonPress == 0)
{
LL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
LL_mDelay(LED_BLINK_FAST);
}while (ubButtonPress == 0) { ... }
}{ ... }
/* ... */
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 UserButton_Callback(void)
{
ubButtonPress = 1;
}{ ... }
#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
/* ... */
/* ... */