forked from stm32duino/STM32LowPower
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlow_power.c
More file actions
791 lines (734 loc) · 25.6 KB
/
low_power.c
File metadata and controls
791 lines (734 loc) · 25.6 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
289
290
291
292
293
294
295
296
297
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/**
******************************************************************************
* @file LowPower.c
* @author Frederic Pillon
* @brief Provides a Low Power interface
*
******************************************************************************
* @attention
*
* Copyright (c) 2020-2021, STMicroelectronics
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
#include "Arduino.h"
#include "low_power.h"
#include "stm32yyxx_ll_pwr.h"
#if defined(HAL_PWR_MODULE_ENABLED) && !defined(HAL_PWR_MODULE_ONLY)
#if defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY) \
&& (defined(UART_IT_WUF) || defined(LPUART1_BASE))
#define UART_WKUP_SUPPORT
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if defined(UART_WKUP_SUPPORT)
/* Save UART handler for callback */
static UART_HandleTypeDef *WakeUpUart = NULL;
#endif
/* Save callback pointer */
static void (*WakeUpUartCb)(void) = NULL;
#if defined(PWR_WAKEUP_SELECT_0) || defined(PWR_WAKEUP1_SOURCE_SELECTION_0)
typedef struct {
PinName pin;
uint32_t wkup_pin;
uint32_t selection;
#if defined(PWR_WAKEUP1_SOURCE_SELECTION_0)
uint32_t low;
#endif
} WakePinSel;
const WakePinSel WakeupPinSel[] = {
#if defined(PWR_WAKEUP_SELECT_0)
/* PWR_WAKEUP_SELECT_0 */
{PA_0, PWR_WAKEUP_LINE1, PWR_WAKEUP_SELECT_0},
{PA_4, PWR_WAKEUP_LINE2, PWR_WAKEUP_SELECT_0},
{PE_6, PWR_WAKEUP_LINE3, PWR_WAKEUP_SELECT_0},
{PA_2, PWR_WAKEUP_LINE4, PWR_WAKEUP_SELECT_0},
{PC_5, PWR_WAKEUP_LINE5, PWR_WAKEUP_SELECT_0},
{PB_5, PWR_WAKEUP_LINE6, PWR_WAKEUP_SELECT_0},
{PB_15, PWR_WAKEUP_LINE7, PWR_WAKEUP_SELECT_0},
/* PWR_WAKEUP_SELECT_1 */
{PB_2, PWR_WAKEUP_LINE1, PWR_WAKEUP_SELECT_1},
{PC_13, PWR_WAKEUP_LINE2, PWR_WAKEUP_SELECT_1},
{PA_1, PWR_WAKEUP_LINE3, PWR_WAKEUP_SELECT_1},
{PB_1, PWR_WAKEUP_LINE4, PWR_WAKEUP_SELECT_1},
{PA_3, PWR_WAKEUP_LINE5, PWR_WAKEUP_SELECT_1},
{PA_5, PWR_WAKEUP_LINE6, PWR_WAKEUP_SELECT_1},
{PA_6, PWR_WAKEUP_LINE7, PWR_WAKEUP_SELECT_1},
{PA_7, PWR_WAKEUP_LINE8, PWR_WAKEUP_SELECT_1},
/* PWR_WAKEUP_SELECT_2 */
{PE_4, PWR_WAKEUP_LINE1, PWR_WAKEUP_SELECT_2},
{PE_5, PWR_WAKEUP_LINE2, PWR_WAKEUP_SELECT_2},
{PB_6, PWR_WAKEUP_LINE3, PWR_WAKEUP_SELECT_2},
{PB_7, PWR_WAKEUP_LINE4, PWR_WAKEUP_SELECT_2},
{PB_8, PWR_WAKEUP_LINE5, PWR_WAKEUP_SELECT_2},
{PE_7, PWR_WAKEUP_LINE6, PWR_WAKEUP_SELECT_2},
{PE_8, PWR_WAKEUP_LINE7, PWR_WAKEUP_SELECT_2},
{PB_10, PWR_WAKEUP_LINE8, PWR_WAKEUP_SELECT_2},
{NC, 0, 0}
#else /* PWR_WAKEUP_SELECT_0 */
/* PWR_WAKEUPx_SOURCE_SELECTION_0 */
{PA_0, PWR_WAKEUP_PIN1, PWR_WAKEUP1_SOURCE_SELECTION_0, PWR_WAKEUP1_POLARITY_LOW},
#if defined(PWR_WAKEUP2_SOURCE_SELECTION_0)
{PA_4, PWR_WAKEUP_PIN2, PWR_WAKEUP2_SOURCE_SELECTION_0, PWR_WAKEUP2_POLARITY_LOW},
#endif
#if defined(PWR_WAKEUP3_SOURCE_SELECTION_0)
{PE_6, PWR_WAKEUP_PIN3, PWR_WAKEUP3_SOURCE_SELECTION_0, PWR_WAKEUP3_POLARITY_LOW},
#endif
{PA_2, PWR_WAKEUP_PIN4, PWR_WAKEUP4_SOURCE_SELECTION_0, PWR_WAKEUP4_POLARITY_LOW},
#if defined(PWR_WAKEUP5_SOURCE_SELECTION_0)
{PC_5, PWR_WAKEUP_PIN5, PWR_WAKEUP5_SOURCE_SELECTION_0, PWR_WAKEUP5_POLARITY_LOW},
#endif
#if defined(PWR_WAKEUP8_SOURCE_SELECTION_0)
/* STM32U5xx */
{PB_5, PWR_WAKEUP_PIN6, PWR_WAKEUP6_SOURCE_SELECTION_0, PWR_WAKEUP6_POLARITY_LOW},
{PB_15, PWR_WAKEUP_PIN7, PWR_WAKEUP7_SOURCE_SELECTION_0, PWR_WAKEUP7_POLARITY_LOW},
{PF_2, PWR_WAKEUP_PIN8, PWR_WAKEUP8_SOURCE_SELECTION_0, PWR_WAKEUP8_POLARITY_LOW},
#else
/* STM32WBAxx */
{PA_12, PWR_WAKEUP_PIN6, PWR_WAKEUP6_SOURCE_SELECTION_0, PWR_WAKEUP6_POLARITY_LOW},
{PB_14, PWR_WAKEUP_PIN7, PWR_WAKEUP7_SOURCE_SELECTION_0, PWR_WAKEUP7_POLARITY_LOW},
#endif
/* PWR_WAKEUPx_SOURCE_SELECTION_1 */
#if defined(PWR_WAKEUP1_SOURCE_SELECTION_1)
{PB_2, PWR_WAKEUP_PIN1, PWR_WAKEUP1_SOURCE_SELECTION_1, PWR_WAKEUP1_POLARITY_LOW},
#endif
#if defined(PWR_WAKEUP2_SOURCE_SELECTION_1)
{PC_13, PWR_WAKEUP_PIN2, PWR_WAKEUP2_SOURCE_SELECTION_1, PWR_WAKEUP2_POLARITY_LOW},
#endif
{PA_1, PWR_WAKEUP_PIN3, PWR_WAKEUP3_SOURCE_SELECTION_1, PWR_WAKEUP3_POLARITY_LOW},
#if defined(PWR_WAKEUP4_SOURCE_SELECTION_1)
{PB_1, PWR_WAKEUP_PIN4, PWR_WAKEUP4_SOURCE_SELECTION_1, PWR_WAKEUP4_POLARITY_LOW},
#endif
#if defined(PWR_WAKEUP5_SOURCE_SELECTION_1)
{PA_3, PWR_WAKEUP_PIN5, PWR_WAKEUP5_SOURCE_SELECTION_1, PWR_WAKEUP5_POLARITY_LOW},
#endif
{PA_5, PWR_WAKEUP_PIN6, PWR_WAKEUP6_SOURCE_SELECTION_1, PWR_WAKEUP6_POLARITY_LOW},
{PA_6, PWR_WAKEUP_PIN7, PWR_WAKEUP7_SOURCE_SELECTION_1, PWR_WAKEUP7_POLARITY_LOW},
{PA_7, PWR_WAKEUP_PIN8, PWR_WAKEUP8_SOURCE_SELECTION_1, PWR_WAKEUP8_POLARITY_LOW},
/* PWR_WAKEUPx_SOURCE_SELECTION_2 */
#if defined(PWR_WAKEUP1_SOURCE_SELECTION_2)
{PE_4, PWR_WAKEUP_PIN1, PWR_WAKEUP1_SOURCE_SELECTION_2, PWR_WAKEUP1_POLARITY_LOW},
#endif
#if defined(PWR_WAKEUP2_SOURCE_SELECTION_2)
{PE_5, PWR_WAKEUP_PIN2, PWR_WAKEUP2_SOURCE_SELECTION_2, PWR_WAKEUP2_POLARITY_LOW},
#endif
#if defined(PWR_WAKEUP3_SOURCE_SELECTION_2)
{PB_6, PWR_WAKEUP_PIN3, PWR_WAKEUP3_SOURCE_SELECTION_2, PWR_WAKEUP3_POLARITY_LOW},
#endif
#if defined(PWR_WAKEUP4_SOURCE_SELECTION_2)
{PB_7, PWR_WAKEUP_PIN4, PWR_WAKEUP4_SOURCE_SELECTION_2, PWR_WAKEUP4_POLARITY_LOW}, /* Only for STM32U5 */
#endif
#if defined(PWR_WAKEUP5_SOURCE_SELECTION_2)
{PB_7, PWR_WAKEUP_PIN5, PWR_WAKEUP5_SOURCE_SELECTION_2, PWR_WAKEUP5_POLARITY_LOW}, /* Only for STM32WBA */
{PB_8, PWR_WAKEUP_PIN5, PWR_WAKEUP5_SOURCE_SELECTION_2, PWR_WAKEUP5_POLARITY_LOW}, /* Only for STM32U5 */
#endif
#if defined(PWR_WAKEUP6_SOURCE_SELECTION_2)
{PE_7, PWR_WAKEUP_PIN6, PWR_WAKEUP6_SOURCE_SELECTION_2, PWR_WAKEUP6_POLARITY_LOW},
#endif
#if defined(PWR_WAKEUP7_SOURCE_SELECTION_2)
{PE_8, PWR_WAKEUP_PIN7, PWR_WAKEUP7_SOURCE_SELECTION_2, PWR_WAKEUP7_POLARITY_LOW},
#endif
#if defined(PWR_WAKEUP8_SOURCE_SELECTION_2)
{PB_9, PWR_WAKEUP_PIN8, PWR_WAKEUP8_SOURCE_SELECTION_2, PWR_WAKEUP8_POLARITY_LOW}, /* Only for STM32WBA */
{PB_10, PWR_WAKEUP_PIN8, PWR_WAKEUP8_SOURCE_SELECTION_2, PWR_WAKEUP8_POLARITY_LOW}, /* Only for STM32U5 */
#endif
{NC, 0, 0, 0}
#endif /* PWR_WAKEUP_SELECT_0 */
};
#endif /* PWR_WAKEUP_SELECT_0 || PWR_WAKEUP1_SOURCE_SELECTION_0 */
#if defined(PWR_FLAG_WUF)
#define PWR_FLAG_WU PWR_FLAG_WUF
#elif defined(PWR_WAKEUP_ALL_FLAG)
#define PWR_FLAG_WU PWR_WAKEUP_ALL_FLAG
#endif
#if defined(PWR_FLAG_SBF)
#define PWR_FLAG_SB PWR_FLAG_SBF
#endif
/**
* @brief Initialize low power mode
* @param None
* @retval None
*/
void LowPower_init()
{
#if defined(__HAL_RCC_PWR_CLK_ENABLE)
/* Enable Power Clock */
__HAL_RCC_PWR_CLK_ENABLE();
#endif
#if defined(PWR_CR_DBP) || defined(PWR_CR1_DBP) || defined(PWR_DBPR_DBP)
/* Allow access to Backup domain */
HAL_PWR_EnableBkUpAccess();
#endif
#ifdef __HAL_RCC_WAKEUPSTOP_CLK_CONFIG
/* Ensure that HSI is wake-up system clock */
__HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI);
#endif
/* Check if the system was resumed from StandBy mode */
if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET) {
/* Clear Standby flag */
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
}
/* Clear all related wakeup flags */
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
}
/**
* @brief Configure a pin as wakeup source if compatible.
* @param pin: pin to configure
* @param mode: pin mode (edge or state). The configuration have to be compatible.
* @retval None
*/
void LowPower_EnableWakeUpPin(uint32_t pin, uint32_t mode)
{
PinName p = digitalPinToPinName(pin);
#if !defined(PWR_WAKEUP_SELECT_0) && !defined(PWR_WAKEUP1_SOURCE_SELECTION_0)
#if !defined(PWR_WAKEUP_PIN1_HIGH)
UNUSED(mode);
#endif
uint32_t wkup_pin = 0;
if (p != NC) {
#if defined(PWR_WAKEUP_PIN1)
if (p == SYS_WKUP1) {
wkup_pin = PWR_WAKEUP_PIN1;
#ifdef PWR_WAKEUP_PIN1_HIGH
if (mode != RISING) {
wkup_pin = PWR_WAKEUP_PIN1_LOW;
}
#endif
}
#endif /* PWR_WAKEUP_PIN1 */
#ifdef PWR_WAKEUP_PIN2
if (p == SYS_WKUP2) {
wkup_pin = PWR_WAKEUP_PIN2;
#ifdef PWR_WAKEUP_PIN2_HIGH
if (mode != RISING) {
wkup_pin = PWR_WAKEUP_PIN2_LOW;
}
#endif
}
#endif /* PWR_WAKEUP_PIN2 */
#ifdef PWR_WAKEUP_PIN3
if (p == SYS_WKUP3) {
wkup_pin = PWR_WAKEUP_PIN3;
#ifdef PWR_WAKEUP_PIN3_HIGH
if (mode != RISING) {
wkup_pin = PWR_WAKEUP_PIN3_LOW;
}
#endif
}
#endif /* PWR_WAKEUP_PIN3 */
#ifdef PWR_WAKEUP_PIN4
if (p == SYS_WKUP4) {
wkup_pin = PWR_WAKEUP_PIN4;
#ifdef PWR_WAKEUP_PIN4_HIGH
if (mode != RISING) {
wkup_pin = PWR_WAKEUP_PIN4_LOW;
}
#endif
}
#endif /* PWR_WAKEUP_PIN4 */
#ifdef PWR_WAKEUP_PIN5
if (p == SYS_WKUP5) {
wkup_pin = PWR_WAKEUP_PIN5;
#ifdef PWR_WAKEUP_PIN5_HIGH
if (mode != RISING) {
wkup_pin = PWR_WAKEUP_PIN5_LOW;
}
#endif
}
#endif /* PWR_WAKEUP_PIN5 */
#ifdef PWR_WAKEUP_PIN6
if (p == SYS_WKUP6) {
wkup_pin = PWR_WAKEUP_PIN6;
#ifdef PWR_WAKEUP_PIN6_HIGH
if (mode != RISING) {
wkup_pin = PWR_WAKEUP_PIN6_LOW;
}
#endif
}
#endif /* PWR_WAKEUP_PIN6 */
#ifdef PWR_WAKEUP_PIN7
if (p == SYS_WKUP7) {
wkup_pin = PWR_WAKEUP_PIN7;
#ifdef PWR_WAKEUP_PIN7_HIGH
if (mode != RISING) {
wkup_pin = PWR_WAKEUP_PIN7_LOW;
}
#endif
}
#endif /* PWR_WAKEUP_PIN7 */
#ifdef PWR_WAKEUP_PIN8
if (p == SYS_WKUP8) {
wkup_pin = PWR_WAKEUP_PIN8;
#ifdef PWR_WAKEUP_PIN8_HIGH
if (mode != RISING) {
wkup_pin = PWR_WAKEUP_PIN8_LOW;
}
#endif
}
#endif /* PWR_WAKEUP_PIN8 */
if (IS_PWR_WAKEUP_PIN(wkup_pin)) {
HAL_PWR_EnableWakeUpPin(wkup_pin);
}
}
#else
/* Two cases:
* 1- STM32U5 and STM32WBA provides a compatible way to configure
* the wakeup pin using HAL_PWR_EnableWakeUpPin but only selection 0
* pins are managed.
* 2- STM32U3 uses HAL_PWR_EnableWakeUpLine.
*/
/* First find index of the pin if valid */
WakePinSel *WakeupPinSel_idx = (WakePinSel *) WakeupPinSel;
/* Read through PinMapAnalogSwitch array */
while (WakeupPinSel_idx->pin != NC) {
/* Check whether pin is or is associated to dualpad Analog Input */
if (WakeupPinSel_idx->pin == p) {
break;
}
WakeupPinSel_idx ++;
}
if (WakeupPinSel_idx->pin != NC) {
#if defined(PWR_WAKEUP1_SOURCE_SELECTION_0)
uint32_t wkup_pin = WakeupPinSel_idx->wkup_pin | WakeupPinSel_idx->selection;
if (mode != RISING) {
wkup_pin |= PWR_WAKEUP1_POLARITY_LOW;
}
if (IS_PWR_WAKEUP_PIN(wkup_pin)) {
HAL_PWR_EnableWakeUpPin(wkup_pin);
}
#else
if (IS_PWR_WAKEUP_LINE(WakeupPinSel_idx->wkup_pin)) {
HAL_PWR_EnableWakeUpLine(WakeupPinSel_idx->wkup_pin, WakeupPinSel_idx->selection, (mode != RISING) ? PWR_WAKEUP_POLARITY_LOW : PWR_WAKEUP_POLARITY_HIGH);
/* Enable the NVIC for Wake-up pin */
HAL_NVIC_SetPriority(PWR_IRQn, 0, 0x00);
HAL_NVIC_EnableIRQ(PWR_IRQn);
}
#endif /* PWR_WAKEUP1_SOURCE_SELECTION_0 */
}
#endif /* !PWR_WAKEUP_SELECT_0 && !PWR_WAKEUP1_SOURCE_SELECTION_0 */
}
#if defined(PWR_CSR_REGLPF)
/**
* @brief For STM32L0 and STM32L1, running in LowPower Sleep requires
* to slow down frequency to MSI range1.
* @retval None
*/
void SystemClock_Decrease(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {};
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = 0;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_1;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
Error_Handler();
}
}
#elif defined(STM32L4xx) || defined(STM32L5xx) || defined(STM32WBxx) || defined(STM32WLxx)
/**
* @brief For STM32L4, STM32L5, STM32WB and STM32WL
* running in LowPower Sleep requires to slow down frequency to 2MHz max.
* @retval None
*/
void SystemClock_Decrease(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {};
/** Configure the main internal regulator output voltage
*/
#if (defined(STM32WBxx) && defined(PWR_CR1_VOS)) || !defined(STM32WBxx)
#if defined(STM32L4xx) || defined(STM32WBxx)
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
#elif defined(STM32L5xx) || defined(STM32WLxx)
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2) != HAL_OK)
#endif
{
Error_Handler();
}
#endif
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU and buses clocks
*/
#if defined(STM32WBxx)
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK4 | RCC_CLOCKTYPE_HCLK2
| RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1;
#elif defined(STM32WLxx)
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3 | RCC_CLOCKTYPE_HCLK
| RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1
| RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.AHBCLK3Divider = RCC_SYSCLK_DIV1;
#elif defined(STM32L4xx)
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
#endif
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
Error_Handler();
}
}
#elif defined(STM32G0xx) || defined(STM32G4xx) || defined(STM32U0xx)
/**
* @brief For STM32G0 and STM32G4
* running in LowPower Sleep requires to slow down frequency to 2MHz max.
* @retval None
*/
void SystemClock_Decrease(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
#if defined(STM32G0xx)
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
#endif
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
#if defined(STM32G4xx)
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
#elif defined(STM32G0xx) || defined(STM32U0xx)
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1;
#endif
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV8;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
Error_Handler();
}
}
#endif
/**
* @brief Enable the sleep mode.
* @param None
* @retval None
*/
void LowPower_sleep(uint32_t regulator)
{
#if defined(PWR_LOWPOWERREGULATOR_ON) && \
(defined(PWR_CSR_REGLPF) || defined(PWR_SR2_REGLPF))
// When LowPower regulator sleep mode is used, it is necessary to decrease CPU Frequency
if (regulator == PWR_LOWPOWERREGULATOR_ON) {
SystemClock_Decrease();
}
#endif
/*
* Suspend Tick increment to prevent wakeup by Systick interrupt.
* Otherwise the Systick interrupt will wake up the device within
* 1ms (HAL time base)
*/
HAL_SuspendTick();
/* Enter Sleep Mode , wake up is done once User push-button is pressed */
HAL_PWR_EnterSLEEPMode(regulator, PWR_SLEEPENTRY_WFI);
#if defined(PWR_LOWPOWERREGULATOR_ON) && \
(defined(PWR_CSR_REGLPF) || defined(PWR_SR2_REGLPF))
// In case of LowPower Regulator used for sleep, restore Main regulator on exit
if (regulator == PWR_LOWPOWERREGULATOR_ON) {
#if defined(__HAL_RCC_PWR_CLK_ENABLE)
__HAL_RCC_PWR_CLK_ENABLE();
#endif
HAL_PWREx_DisableLowPowerRunMode();
// Restore systemClock which has been decreased by SystemClock_Decrease()
SystemClock_Config();
}
#endif
/* Resume Tick interrupt if disabled prior to SLEEP mode entry */
HAL_ResumeTick();
if (WakeUpUartCb != NULL) {
WakeUpUartCb();
}
}
/**
* @brief Enable the stop mode.
* @param obj : pointer to serial_t structure
* @retval None
*/
void LowPower_stop(serial_t *obj)
{
__disable_irq();
#if defined(UART_WKUP_SUPPORT)
if (WakeUpUart != NULL) {
HAL_UARTEx_EnableStopMode(WakeUpUart);
}
#endif
#if defined(PWR_CR_ULP)
/* Enable Ultra low power mode */
HAL_PWREx_EnableUltraLowPower();
#endif
#if defined(PWR_CR1_ULPMEN) || defined(PWR_CR3_ULPMEN) || \
(defined(PWR_CR3_ENULP) && defined(STM32U0xx))
/* Enable Ultra low power mode */
HAL_PWREx_EnableUltraLowPowerMode();
#endif
#if defined(PWR_CR_FWU)
/* Enable the fast wake up from Ultra low power mode */
HAL_PWREx_EnableFastWakeUp();
#endif
#ifdef __HAL_RCC_WAKEUPSTOP_CLK_CONFIG
/* Select MSI or HSI as system clock source after Wake Up from Stop mode */
#ifdef RCC_STOP_WAKEUPCLOCK_MSI
__HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI);
#else
__HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI);
#endif
#endif
#if defined(STM32WBxx)
/* Set low-power mode of CPU2 */
/* Note: Typically, action performed by CPU2 on a dual core application.
Since this example is single core, perform it by CPU1. */
/* Note: On STM32WB, both CPU1 and CPU2 must be in low-power mode
to set the entire System in low-power mode, corresponding to
the deepest low-power mode possible.
For example, CPU1 in Stop2 mode and CPU2 in Shutdown mode
will make system enter in Stop2 mode. */
#if defined(LL_PWR_MODE_STOP2)
LL_C2_PWR_SetPowerMode(LL_PWR_MODE_STOP2);
#else
LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
#endif
#endif
/* Enter Stop mode */
#if defined(UART_WKUP_SUPPORT) && (defined(PWR_CPUCR_RETDS_CD) || \
defined(LL_PWR_MODE_STOP2))
if ((WakeUpUart == NULL)
|| (WakeUpUart->Instance == (USART_TypeDef *)LPUART1_BASE)
#ifdef LPUART2_BASE
|| (WakeUpUart->Instance == (USART_TypeDef *)LPUART2_BASE)
#endif
#ifdef LPUART3_BASE
|| (WakeUpUart->Instance == (USART_TypeDef *)LPUART3_BASE)
#endif
) {
#if defined(PWR_CR1_RRSTP)
// STM32L4+ must keep SRAM3 content when entering STOP2 lowpower mode
HAL_PWREx_EnableSRAM3ContentRetention();
#endif /* PWR_CR1_RRSTP */
// STM32L4xx and STM32H7xx supports STOP2 mode which halves consumption
#if defined(STM32H7xx)
#if defined(PWR_LOWPOWERREGULATOR_ON)
HAL_PWREx_EnterSTOP2Mode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
#else
HAL_PWREx_EnterSTOP2Mode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
#endif
HAL_PWREx_EnterSTOP2Mode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
#else
HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
#endif
} else
#endif
{
#if defined(PWR_LOWPOWERREGULATOR_ON)
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
#else
HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
#endif
}
/* Exit Stop mode reset clocks */
SystemClock_ConfigFromStop();
#if defined(UART_WKUP_SUPPORT)
if (WakeUpUart != NULL) {
/* In case of WakeUp from UART, reset its clock source to HSI */
uart_config_lowpower(obj);
HAL_UARTEx_DisableStopMode(WakeUpUart);
}
#else
UNUSED(obj);
#endif
__enable_irq();
HAL_Delay(10);
if (WakeUpUartCb != NULL) {
WakeUpUartCb();
}
}
/**
* @brief Enable the standby mode. The board reset when leaves this mode.
* @param None
* @retval None
*/
void LowPower_standby()
{
__disable_irq();
/* Clear wakeup flags */
#if defined(PWR_FLAG_WU)
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
#elif defined(PWR_CPUCR_CSSF)
__HAL_PWR_CLEAR_FLAG(PWR_CPUCR_CSSF);
#elif defined(PWR_MPUCR_CSSF)
__HAL_PWR_CLEAR_FLAG(PWR_MPUCR_CSSF);
#endif
#if defined(PWR_CR_ULP)
/* Enable Ultra low power mode */
HAL_PWREx_EnableUltraLowPower();
#endif
#if defined(PWR_CR_FWU)
/* Enable the fast wake up from Ultra low power mode */
HAL_PWREx_EnableFastWakeUp();
#endif
#if defined(STM32WBxx)
/* Set low-power mode of CPU2 */
/* Note: Typically, action performed by CPU2 on a dual core application.
Since this example is single core, perform it by CPU1. */
/* Note: On STM32WB, both CPU1 and CPU2 must be in low-power mode
to set the entire System in low-power mode, corresponding to
the deepest low-power mode possible.
For example, CPU1 in Stop2 mode and CPU2 in Shutdown mode
will make system enter in Stop2 mode. */
LL_C2_PWR_SetPowerMode(LL_PWR_MODE_STANDBY);
#endif
HAL_PWR_EnterSTANDBYMode();
}
/**
* @brief Enable the shutdown mode.The board reset when leaves this mode.
* If shutdown mode not available, use standby mode instead.
* @param boolean true if RTC is configured, in that case LSE is required
* @retval None
*/
void LowPower_shutdown(bool isRTC)
{
__disable_irq();
/* Clear wakeup flags */
#if defined(PWR_FLAG_WU)
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
#elif defined(PWR_CPUCR_CSSF)
__HAL_PWR_CLEAR_FLAG(PWR_CPUCR_CSSF);
#elif defined(PWR_MPUCR_CSSF)
__HAL_PWR_CLEAR_FLAG(PWR_MPUCR_CSSF);
#endif
#if defined(STM32WBxx)
/* Set low-power mode of CPU2 */
/* Note: Typically, action performed by CPU2 on a dual core application.
Since this example is single core, perform it by CPU1. */
/* Note: On STM32WB, both CPU1 and CPU2 must be in low-power mode
to set the entire System in low-power mode, corresponding to
the deepest low-power mode possible.
For example, CPU1 in Stop2 mode and CPU2 in Shutdown mode
will make system enter in Stop2 mode. */
LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
#endif
#if defined(LL_PWR_SHUTDOWN_MODE) || defined(LL_PWR_MODE_SHUTDOWN)
/* LSE must be on to use shutdown mode within RTC else fallback to standby */
if ((!isRTC) || (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == SET)) {
#if defined(STM32U0xx)
HAL_PWR_EnterSHUTDOWNMode();
#else
HAL_PWREx_EnterSHUTDOWNMode();
#endif
} else
#else
UNUSED(isRTC);
#endif
{
LowPower_standby();
}
}
/**
* @brief Configure the UART as a wakeup source. A callback can be called when
* the chip leaves the low power mode. See board datasheet to check
* with which low power mode the UART is compatible.
* Warning This function will change UART clock source to HSI
* @param serial: pointer to serial
* @param FuncPtr: pointer to callback
* @retval None
*/
void LowPower_EnableWakeUpUart(serial_t *serial, void (*FuncPtr)(void))
{
#if defined(UART_WKUP_SUPPORT)
#ifdef IS_UART_WAKEUP_SELECTION
UART_WakeUpTypeDef WakeUpSelection;
if (serial == NULL) {
return;
}
/* Save Uart handler and Serial object */
WakeUpUart = &(serial->handle);
/* make sure that no UART transfer is on-going */
while (__HAL_UART_GET_FLAG(WakeUpUart, USART_ISR_BUSY) == SET);
/* make sure that UART is ready to receive
* (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */
while (__HAL_UART_GET_FLAG(WakeUpUart, USART_ISR_REACK) == RESET);
/* set the wake-up event:
* specify wake-up on RXNE flag
*/
WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_READDATA_NONEMPTY;
HAL_UARTEx_StopModeWakeUpSourceConfig(WakeUpUart, WakeUpSelection);
#endif
#if defined(UART_IT_WUF)
/* Enable the UART Wake UP from STOPx mode Interrupt */
__HAL_UART_ENABLE_IT(WakeUpUart, UART_IT_WUF);
#endif
#else
UNUSED(serial);
#endif
/* Save callback */
WakeUpUartCb = FuncPtr;
}
/**
* @brief Configures system clock and system IP clocks after wake-up from STOP
* @note Weaked function which can be redefined by user at the sketch level.
* By default, call 'SystemClock_Config()'.
* @param None
* @retval None
*/
WEAK void SystemClock_ConfigFromStop(void)
{
configIPClock();
SystemClock_Config();
}
#ifdef __cplusplus
}
#endif
#endif /* HAL_PWR_MODULE_ENABLED && !HAL_PWR_MODULE_ONLY */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/