Skip to content

Commit 676cc07

Browse files
Properly keep the SYSCFG module disabled unless access is needed
1 parent 0a1bb00 commit 676cc07

File tree

5 files changed

+34
-41
lines changed

5 files changed

+34
-41
lines changed

system/libstm32l4_dragonfly/stm32l4_dma.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,6 @@ typedef struct _stm32l4_dma_driver_t {
8080

8181
static stm32l4_dma_driver_t stm32l4_dma_driver;
8282

83-
static void stm32l4_dma_flash_sleep(void)
84-
{
85-
uint32_t o_flash, n_flash;
86-
87-
o_flash = stm32l4_dma_driver.flash;
88-
89-
do
90-
{
91-
n_flash = o_flash - 1;
92-
93-
if (n_flash == 0)
94-
{
95-
armv7m_atomic_and(&RCC->AHB1SMENR, ~RCC_AHB1SMENR_FLASHSMEN);
96-
}
97-
else
98-
{
99-
armv7m_atomic_or(&RCC->AHB1SMENR, RCC_AHB1SMENR_FLASHSMEN);
100-
}
101-
}
102-
while (!armv7m_atomic_compare_exchange(&stm32l4_dma_driver.flash, &o_flash, n_flash));
103-
}
104-
10583
static void stm32l4_dma_track(uint32_t address)
10684
{
10785
if (address < 0x10000000)

system/libstm32l4_dragonfly/stm32l4_exti.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ bool stm32l4_exti_resume(stm32l4_exti_t *exti, uint32_t mask)
155155
bool stm32l4_exti_notify(stm32l4_exti_t *exti, uint16_t pin, uint32_t control, stm32l4_exti_callback_t callback, void *context)
156156
{
157157
unsigned int mask, index, group;
158+
uint32_t apb2enr;
158159

159160
if (exti->state != EXTI_STATE_READY)
160161
{
@@ -192,7 +193,19 @@ bool stm32l4_exti_notify(stm32l4_exti_t *exti, uint16_t pin, uint32_t control, s
192193
armv7m_atomic_and(&EXTI->FTSR1, ~mask);
193194
}
194195

196+
apb2enr = RCC->APB2ENR;
197+
198+
if (!(apb2enr & RCC_APB2ENR_SYSCFGEN))
199+
{
200+
armv7m_atomic_or(&RCC->APB2ENR, RCC_APB2ENR_SYSCFGEN);
201+
}
202+
195203
armv7m_atomic_modify(&SYSCFG->EXTICR[index >> 2], (0x0000000f << ((index & 3) << 2)), (group << ((index & 3) << 2)));
204+
205+
if (!(apb2enr & RCC_APB2ENR_SYSCFGEN))
206+
{
207+
armv7m_atomic_and(&RCC->APB2ENR, ~RCC_APB2ENR_SYSCFGEN);
208+
}
196209

197210
armv7m_atomic_or(&exti->enables, mask);
198211

system/libstm32l4_dragonfly/stm32l4_i2c.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ bool stm32l4_i2c_disable(stm32l4_i2c_t *i2c)
860860
bool stm32l4_i2c_configure(stm32l4_i2c_t *i2c, uint32_t clock, uint32_t option)
861861
{
862862
I2C_TypeDef *I2C = i2c->I2C;
863-
uint32_t pin_scl, pin_sda, i2c_cr1, i2c_cr2, i2c_oar1, i2c_oar2, i2c_timingr, syscfg_cfgr1, count;
863+
uint32_t pin_scl, pin_sda, i2c_cr1, i2c_cr2, i2c_oar1, i2c_oar2, i2c_timingr, syscfg_cfgr1, apb2enr, count;
864864

865865
if ((i2c->state != I2C_STATE_READY) && (i2c->state != I2C_STATE_BUSY))
866866
{
@@ -966,6 +966,13 @@ bool stm32l4_i2c_configure(stm32l4_i2c_t *i2c, uint32_t clock, uint32_t option)
966966
syscfg_cfgr1 = SYSCFG_CFGR1_I2C3_FMP;
967967
break;
968968
}
969+
970+
apb2enr = RCC->APB2ENR;
971+
972+
if (!(apb2enr & RCC_APB2ENR_SYSCFGEN))
973+
{
974+
armv7m_atomic_or(&RCC->APB2ENR, RCC_APB2ENR_SYSCFGEN);
975+
}
969976

970977
if (i2c->clock == 1000000)
971978
{
@@ -976,6 +983,11 @@ bool stm32l4_i2c_configure(stm32l4_i2c_t *i2c, uint32_t clock, uint32_t option)
976983
armv7m_atomic_and(&SYSCFG->CFGR1, ~syscfg_cfgr1);
977984
}
978985

986+
if (!(apb2enr & RCC_APB2ENR_SYSCFGEN))
987+
{
988+
armv7m_atomic_and(&RCC->APB2ENR, ~RCC_APB2ENR_SYSCFGEN);
989+
}
990+
979991
I2C->TIMINGR = i2c_timingr;
980992
I2C->OAR2 = i2c_oar2;
981993
I2C->OAR1 = i2c_oar1;

system/libstm32l4_dragonfly/stm32l4_system.c

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -425,22 +425,16 @@ bool stm32l4_system_configure(uint32_t sysclk, uint32_t hclk, uint32_t pclk1, ui
425425
return false;
426426
}
427427

428-
/* Unlock SYSCFG (and leave it unlocked for EXTI use.
429-
*/
430-
431-
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
432-
433428
/* Detect LSE/HSE on the first pass.
434429
*/
435430

436431
if (stm32l4_system_device.lseclk == 0)
437432
{
438-
apb1enr1 = RCC->APB1ENR1;
433+
/* This is executed only on the very forst goaround, while interrupts are disabled.
434+
* So RCC does not need atomics.
435+
*/
439436

440-
if (!(apb1enr1 & RCC_APB1ENR1_PWREN))
441-
{
442-
armv7m_atomic_or(&RCC->APB1ENR1, RCC_APB1ENR1_PWREN);
443-
}
437+
RCC->APB1ENR1 |= RCC_APB1ENR1_PWREN;
444438

445439
PWR->CR1 |= PWR_CR1_DBP;
446440

@@ -454,13 +448,12 @@ bool stm32l4_system_configure(uint32_t sysclk, uint32_t hclk, uint32_t pclk1, ui
454448

455449
/* Switch to System Memory @ 0x00000000.
456450
*/
451+
452+
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
457453
SYSCFG->MEMRMP = (SYSCFG->MEMRMP & ~SYSCFG_MEMRMP_MEM_MODE) | SYSCFG_MEMRMP_MEM_MODE_0;
458454
RCC->APB2ENR &= ~RCC_APB2ENR_SYSCFGEN;
459455

460-
if (!(apb1enr1 & RCC_APB1ENR1_PWREN))
461-
{
462-
armv7m_atomic_and(&RCC->APB1ENR1, ~RCC_APB1ENR1_PWREN);
463-
}
456+
RCC->APB1ENR1 &= ~RCC_APB1ENR1_PWREN;
464457

465458
SCB->VTOR = 0;
466459

@@ -514,10 +507,7 @@ bool stm32l4_system_configure(uint32_t sysclk, uint32_t hclk, uint32_t pclk1, ui
514507
*/
515508
PWR->CR4 |= PWR_CR4_VBE;
516509

517-
if (!(apb1enr1 & RCC_APB1ENR1_PWREN))
518-
{
519-
armv7m_atomic_and(&RCC->APB1ENR1, ~RCC_APB1ENR1_PWREN);
520-
}
510+
RCC->APB1ENR1 &= ~RCC_APB1ENR1_PWREN;
521511
}
522512

523513
if (stm32l4_system_device.hseclk == 0)
2.13 KB
Binary file not shown.

0 commit comments

Comments
 (0)