@@ -251,11 +251,18 @@ void stm32l4_system_periph_disable(unsigned int periph)
251
251
252
252
static void stm32l4_system_msi4_sysclk (void )
253
253
{
254
+ uint32_t apb1enr1 ;
255
+
254
256
FLASH -> ACR = FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_4WS ;
255
257
256
258
/* Select the proper voltage range */
257
259
258
- RCC -> APB1ENR1 |= RCC_APB1ENR1_PWREN ;
260
+ apb1enr1 = RCC -> APB1ENR1 ;
261
+
262
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
263
+ {
264
+ armv7m_atomic_or (& RCC -> APB1ENR1 , RCC_APB1ENR1_PWREN );
265
+ }
259
266
260
267
if (PWR -> CR1 & PWR_CR1_LPR )
261
268
{
@@ -272,6 +279,11 @@ static void stm32l4_system_msi4_sysclk(void)
272
279
{
273
280
}
274
281
282
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
283
+ {
284
+ armv7m_atomic_and (& RCC -> APB1ENR1 , ~RCC_APB1ENR1_PWREN );
285
+ }
286
+
275
287
/* Select the HSI as system clock source */
276
288
RCC -> CFGR = (RCC -> CFGR & ~RCC_CFGR_SW ) | RCC_CFGR_SW_HSI ;
277
289
@@ -355,7 +367,7 @@ void stm32l4_system_bootloader(void)
355
367
RCC -> AHB1ENR = RCC_AHB1ENR_FLASHEN ;
356
368
RCC -> AHB2ENR = 0 ;
357
369
RCC -> AHB3ENR = 0 ;
358
- RCC -> APB1ENR1 = RCC_APB1ENR1_PWREN ;
370
+ RCC -> APB1ENR1 = 0 ;
359
371
RCC -> APB1ENR2 = 0 ;
360
372
RCC -> APB2ENR = RCC_APB2ENR_SYSCFGEN ;
361
373
RCC -> CCIPR = 0 ;
@@ -371,8 +383,6 @@ void stm32l4_system_bootloader(void)
371
383
372
384
stm32l4_system_msi4_sysclk ();
373
385
374
- RCC -> APB1ENR1 &= ~RCC_APB1ENR1_PWREN ;
375
-
376
386
/* Disable and clear pending interrupts (80 vectors).
377
387
*/
378
388
SysTick -> CTRL = 0 ;
@@ -408,6 +418,7 @@ bool stm32l4_system_configure(uint32_t sysclk, uint32_t hclk, uint32_t pclk1, ui
408
418
{
409
419
uint32_t fclk , fvco , fpll , fpllout , mout , nout , rout , n , r ;
410
420
uint32_t count , msirange , hpre , ppre1 , ppre2 , latency ;
421
+ uint32_t apb1enr1 ;
411
422
412
423
if (!clk48 && ((sysclk <= 24000000 ) && stm32l4_system_device .clk48 ))
413
424
{
@@ -424,7 +435,12 @@ bool stm32l4_system_configure(uint32_t sysclk, uint32_t hclk, uint32_t pclk1, ui
424
435
425
436
if (stm32l4_system_device .lseclk == 0 )
426
437
{
427
- RCC -> APB1ENR1 |= RCC_APB1ENR1_PWREN ;
438
+ apb1enr1 = RCC -> APB1ENR1 ;
439
+
440
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
441
+ {
442
+ armv7m_atomic_or (& RCC -> APB1ENR1 , RCC_APB1ENR1_PWREN );
443
+ }
428
444
429
445
PWR -> CR1 |= PWR_CR1_DBP ;
430
446
@@ -441,6 +457,11 @@ bool stm32l4_system_configure(uint32_t sysclk, uint32_t hclk, uint32_t pclk1, ui
441
457
SYSCFG -> MEMRMP = (SYSCFG -> MEMRMP & ~SYSCFG_MEMRMP_MEM_MODE ) | SYSCFG_MEMRMP_MEM_MODE_0 ;
442
458
RCC -> APB2ENR &= ~RCC_APB2ENR_SYSCFGEN ;
443
459
460
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
461
+ {
462
+ armv7m_atomic_and (& RCC -> APB1ENR1 , ~RCC_APB1ENR1_PWREN );
463
+ }
464
+
444
465
SCB -> VTOR = 0 ;
445
466
446
467
/* This needs to be assembly code as GCC catches NULL
@@ -493,7 +514,10 @@ bool stm32l4_system_configure(uint32_t sysclk, uint32_t hclk, uint32_t pclk1, ui
493
514
*/
494
515
PWR -> CR4 |= PWR_CR4_VBE ;
495
516
496
- RCC -> APB1ENR1 &= ~RCC_APB1ENR1_PWREN ;
517
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
518
+ {
519
+ armv7m_atomic_and (& RCC -> APB1ENR1 , ~RCC_APB1ENR1_PWREN );
520
+ }
497
521
}
498
522
499
523
if (stm32l4_system_device .hseclk == 0 )
@@ -634,7 +658,12 @@ bool stm32l4_system_configure(uint32_t sysclk, uint32_t hclk, uint32_t pclk1, ui
634
658
/* First switch to HSI as system clock.
635
659
*/
636
660
637
- RCC -> APB1ENR1 |= RCC_APB1ENR1_PWREN ;
661
+ apb1enr1 = RCC -> APB1ENR1 ;
662
+
663
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
664
+ {
665
+ armv7m_atomic_or (& RCC -> APB1ENR1 , RCC_APB1ENR1_PWREN );
666
+ }
638
667
639
668
/* Select Range 1 to switch clocks */
640
669
@@ -850,7 +879,10 @@ bool stm32l4_system_configure(uint32_t sysclk, uint32_t hclk, uint32_t pclk1, ui
850
879
FLASH -> ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | latency ;
851
880
}
852
881
853
- RCC -> APB1ENR1 &= ~RCC_APB1ENR1_PWREN ;
882
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
883
+ {
884
+ armv7m_atomic_and (& RCC -> APB1ENR1 , ~RCC_APB1ENR1_PWREN );
885
+ }
854
886
855
887
stm32l4_system_device .sysclk = sysclk ;
856
888
stm32l4_system_device .hclk = hclk ;
@@ -952,6 +984,8 @@ uint32_t stm32l4_system_pclk2(void)
952
984
953
985
bool stm32l4_system_suspend (void )
954
986
{
987
+ uint32_t apb1enr1 ;
988
+
955
989
/* #### Add code here that calls STOP_ENTER, and if that fails calls out STOP_CANCEL */
956
990
957
991
/* Disable FLASH in sleep/deepsleep */
@@ -1017,11 +1051,19 @@ bool stm32l4_system_suspend(void)
1017
1051
1018
1052
/* Set up STOP1 */
1019
1053
1020
- RCC -> APB1ENR1 |= RCC_APB1ENR1_PWREN ;
1054
+ apb1enr1 = RCC -> APB1ENR1 ;
1021
1055
1056
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
1057
+ {
1058
+ armv7m_atomic_or (& RCC -> APB1ENR1 , RCC_APB1ENR1_PWREN );
1059
+ }
1060
+
1022
1061
PWR -> CR1 = (PWR -> CR1 & ~PWR_CR1_LPMS ) | PWR_CR1_LPMS_STOP1 ;
1023
1062
1024
- RCC -> APB1ENR1 &= ~RCC_APB1ENR1_PWREN ;
1063
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
1064
+ {
1065
+ armv7m_atomic_and (& RCC -> APB1ENR1 , ~RCC_APB1ENR1_PWREN );
1066
+ }
1025
1067
1026
1068
return true;
1027
1069
}
@@ -1102,16 +1144,28 @@ bool stm32l4_system_stop(void)
1102
1144
1103
1145
bool stm32l4_system_standby (void )
1104
1146
{
1105
- /* #### Add code here that calls STANDBY_ENTER, and if that fails calls out STANDBY_CANCEL */
1147
+ uint32_t apb1enr1 ;
1106
1148
1107
- RCC -> APB1ENR1 |= RCC_APB1ENR1_PWREN ;
1149
+ /* #### Add code here that calls STANDBY_ENTER, and if that fails calls out STANDBY_CANCEL */
1108
1150
1109
1151
/* ERRATA 2.1.15. WAR: Switch MSI to 4MHz before entering low power mode */
1110
1152
1111
1153
stm32l4_system_msi4_sysclk ();
1112
1154
1155
+ apb1enr1 = RCC -> APB1ENR1 ;
1156
+
1157
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
1158
+ {
1159
+ armv7m_atomic_or (& RCC -> APB1ENR1 , RCC_APB1ENR1_PWREN );
1160
+ }
1161
+
1113
1162
PWR -> CR1 = (PWR -> CR1 & ~PWR_CR1_LPMS ) | PWR_CR1_LPMS_STANDBY ;
1114
1163
1164
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
1165
+ {
1166
+ armv7m_atomic_and (& RCC -> APB1ENR1 , ~RCC_APB1ENR1_PWREN );
1167
+ }
1168
+
1115
1169
SCB -> SCR |= SCB_SCR_SLEEPDEEP_Msk ;
1116
1170
1117
1171
__WFI ();
@@ -1121,14 +1175,28 @@ bool stm32l4_system_standby(void)
1121
1175
1122
1176
bool stm32l4_system_shutdown (void )
1123
1177
{
1178
+ uint32_t apb1enr1 ;
1179
+
1124
1180
/* #### Add code here that calls SHUTDOWN_ENTER, and if that fails calls out SHUTDOWN_CANCEL */
1125
1181
1126
1182
/* ERRATA 2.1.15. WAR: Switch MSI to 4MHz before entering low power mode */
1127
1183
1128
1184
stm32l4_system_msi4_sysclk ();
1129
1185
1186
+ apb1enr1 = RCC -> APB1ENR1 ;
1187
+
1188
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
1189
+ {
1190
+ armv7m_atomic_or (& RCC -> APB1ENR1 , RCC_APB1ENR1_PWREN );
1191
+ }
1192
+
1130
1193
PWR -> CR1 = (PWR -> CR1 & ~PWR_CR1_LPMS ) | PWR_CR1_LPMS_SHUTDOWN ;
1131
1194
1195
+ if (!(apb1enr1 & RCC_APB1ENR1_PWREN ))
1196
+ {
1197
+ armv7m_atomic_and (& RCC -> APB1ENR1 , ~RCC_APB1ENR1_PWREN );
1198
+ }
1199
+
1132
1200
SCB -> SCR |= SCB_SCR_SLEEPDEEP_Msk ;
1133
1201
1134
1202
__WFI ();
0 commit comments