Skip to content

Commit 982ce17

Browse files
Disable FLASH in sleep mode, unless a DMA from/to FLASH is active
1 parent f18af8d commit 982ce17

File tree

3 files changed

+45
-81
lines changed

3 files changed

+45
-81
lines changed

system/libstm32l4_dragonfly/stm32l4_dma.c

Lines changed: 43 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -74,109 +74,60 @@ static const IRQn_Type stm32l4_dma_interrupt_table[16] = {
7474

7575
typedef struct _stm32l4_dma_driver_t {
7676
volatile uint32_t mask;
77-
#ifdef notyet
7877
volatile uint32_t flash;
79-
volatile uint32_t sram1;
80-
volatile uint32_t sram2;
81-
#endif
8278
stm32l4_dma_t *instances[16];
8379
} stm32l4_dma_driver_t;
8480

8581
static stm32l4_dma_driver_t stm32l4_dma_driver;
8682

87-
static void stm32l4_dma_track(uint8_t channel, uint32_t address)
83+
static void stm32l4_dma_flash_sleep(void)
8884
{
89-
#ifdef notyet
90-
if (address < 0x40000000)
91-
{
92-
if (address < 0x10000000)
93-
{
94-
armv7m_atomic_add(&stm32l4_dma_driver.flash, 1);
85+
uint32_t o_flash, n_flash;
9586

96-
armv7m_atomic_or(&RCC->AHB1SMENR, RCC_AHB1SMENR_FLASHSMEN);
97-
}
98-
else if (address < 0x20000000)
87+
o_flash = stm32l4_dma_driver.flash;
88+
89+
do
90+
{
91+
n_flash = o_flash - 1;
92+
93+
if (n_flash == 0)
9994
{
100-
armv7m_atomic_add(&stm32l4_dma_driver.sram2, 1);
101-
102-
armv7m_atomic_or(&RCC->AHB2SMENR, RCC_AHB2SMENR_SRAM2SMEN);
95+
armv7m_atomic_and(&RCC->AHB1SMENR, ~RCC_AHB1SMENR_FLASHSMEN);
10396
}
10497
else
10598
{
106-
armv7m_atomic_add(&stm32l4_dma_driver.sram1, 1);
107-
108-
armv7m_atomic_or(&RCC->AHB1SMENR, RCC_AHB1SMENR_SRAM1SMEN);
99+
armv7m_atomic_or(&RCC->AHB1SMENR, RCC_AHB1SMENR_FLASHSMEN);
109100
}
110101
}
111-
#endif
102+
while (!armv7m_atomic_compare_exchange(&stm32l4_dma_driver.flash, &o_flash, n_flash));
112103
}
113104

114-
static void stm32l4_dma_untrack(uint8_t channel, uint32_t address)
105+
static void stm32l4_dma_track(uint32_t address)
115106
{
116-
#ifdef notyet
117-
uint32_t o_flash, o_sram1, o_sram2, n_flash, n_sram1, n_sram2;
118-
119-
if (address < 0x40000000)
107+
if (address < 0x10000000)
120108
{
121-
if (address < 0x10000000)
122-
{
123-
o_flash = stm32l4_dma_driver.flash;
109+
armv7m_atomic_add(&stm32l4_dma_driver.flash, 1);
110+
111+
armv7m_atomic_or(&RCC->AHB1SMENR, RCC_AHB1SMENR_FLASHSMEN);
112+
}
113+
}
124114

125-
do
126-
{
127-
n_flash = o_flash - 1;
128-
129-
if (n_flash == 0)
130-
{
131-
armv7m_atomic_and(&RCC->AHB1SMENR, ~RCC_AHB1SMENR_FLASHSMEN);
132-
}
133-
else
134-
{
135-
armv7m_atomic_or(&RCC->AHB1SMENR, RCC_AHB1SMENR_FLASHSMEN);
136-
}
137-
}
138-
while (!armv7m_atomic_compare_exchange(&stm32l4_dma_driver.flash, &o_flash, n_flash));
139-
}
140-
else if (address < 0x20000000)
115+
static void stm32l4_dma_untrack(uint32_t address)
116+
{
117+
if (address < 0x10000000)
118+
{
119+
if (stm32l4_dma_driver.flash == 1)
141120
{
142-
o_sram2 = stm32l4_dma_driver.sram2;
143-
144-
do
121+
if (__get_IPSR() == 0)
145122
{
146-
n_sram2 = o_sram2 - 1;
147-
148-
if (n_sram2 == 0)
149-
{
150-
armv7m_atomic_and(&RCC->AHB2SMENR, ~RCC_AHB2SMENR_SRAM2SMEN);
151-
}
152-
else
153-
{
154-
armv7m_atomic_or(&RCC->AHB2SMENR, RCC_AHB2SMENR_SRAM2SMEN);
155-
}
123+
armv7m_svcall_0((uint32_t)&stm32l4_dma_flash_sleep);
156124
}
157-
while (!armv7m_atomic_compare_exchange(&stm32l4_dma_driver.sram2, &o_sram2, n_sram2));
158-
}
159-
else
160-
{
161-
o_sram1 = stm32l4_dma_driver.sram1;
162-
163-
do
125+
else
164126
{
165-
n_sram1 = o_sram1 - 1;
166-
167-
if (n_sram1 == 0)
168-
{
169-
armv7m_atomic_and(&RCC->AHB1SMENR, ~RCC_AHB1SMENR_SRAM1SMEN);
170-
}
171-
else
172-
{
173-
armv7m_atomic_or(&RCC->AHB1SMENR, RCC_AHB1SMENR_SRAM1SMEN);
174-
}
127+
stm32l4_dma_flash_sleep();
175128
}
176-
while (!armv7m_atomic_compare_exchange(&stm32l4_dma_driver.sram1, &o_sram1, n_sram1));
177129
}
178130
}
179-
#endif
180131
}
181132

182133
static void stm32l4_dma_interrupt(stm32l4_dma_t *dma)
@@ -246,6 +197,7 @@ void stm32l4_dma_destroy(stm32l4_dma_t *dma)
246197

247198
void stm32l4_dma_enable(stm32l4_dma_t *dma, stm32l4_dma_callback_t callback, void *context)
248199
{
200+
DMA_Channel_TypeDef *DMA = dma->DMA;
249201
unsigned int shift;
250202

251203
dma->callback = callback;
@@ -264,6 +216,8 @@ void stm32l4_dma_enable(stm32l4_dma_t *dma, stm32l4_dma_callback_t callback, voi
264216
armv7m_atomic_modify(&DMA2_CSELR->CSELR, (15 << shift), (dma->channel >> 4) << shift);
265217
}
266218

219+
DMA->CMAR = 0xffffffff;
220+
267221
if (callback)
268222
{
269223
NVIC_EnableIRQ(dma->interrupt);
@@ -272,7 +226,11 @@ void stm32l4_dma_enable(stm32l4_dma_t *dma, stm32l4_dma_callback_t callback, voi
272226

273227
void stm32l4_dma_disable(stm32l4_dma_t *dma)
274228
{
229+
DMA_Channel_TypeDef *DMA = dma->DMA;
230+
275231
NVIC_DisableIRQ(dma->interrupt);
232+
233+
stm32l4_dma_untrack(DMA->CMAR);
276234
}
277235

278236
void stm32l4_dma_start(stm32l4_dma_t *dma, uint32_t tx_data, uint32_t rx_data, uint16_t xf_count, uint32_t option)
@@ -293,23 +251,25 @@ void stm32l4_dma_start(stm32l4_dma_t *dma, uint32_t tx_data, uint32_t rx_data, u
293251
DMA2->IFCR = (15 << shift);
294252
}
295253

296-
dma->size = xf_count;
254+
stm32l4_dma_untrack(DMA->CMAR);
297255

298256
if (option & DMA_OPTION_MEMORY_TO_PERIPHERAL)
299257
{
300-
stm32l4_dma_track(dma->channel, rx_data);
258+
stm32l4_dma_track(rx_data);
301259

302260
DMA->CMAR = rx_data;
303261
DMA->CPAR = tx_data;
304262
}
305263
else
306264
{
307-
stm32l4_dma_track(dma->channel, (uint32_t)tx_data);
265+
stm32l4_dma_track(tx_data);
308266

309267
DMA->CMAR = tx_data;
310268
DMA->CPAR = rx_data;
311269
}
312270

271+
dma->size = xf_count;
272+
313273
DMA->CNDTR = xf_count;
314274
DMA->CCR = option | DMA_CCR_EN;
315275
}
@@ -320,7 +280,9 @@ uint16_t stm32l4_dma_stop(stm32l4_dma_t *dma)
320280

321281
DMA->CCR &= ~(DMA_CCR_EN | DMA_CCR_TCIE | DMA_CCR_HTIE | DMA_CCR_TEIE);
322282

323-
stm32l4_dma_untrack(dma->channel, DMA->CMAR);
283+
stm32l4_dma_untrack(DMA->CMAR);
284+
285+
DMA->CMAR = 0xffffffff;
324286

325287
return dma->size - (DMA->CNDTR & 0xffff);
326288
}

system/libstm32l4_dragonfly/system_stm32l4xx.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ void SystemInit(void)
223223
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
224224
SYSCFG->MEMRMP = (SYSCFG->MEMRMP & ~SYSCFG_MEMRMP_MEM_MODE);
225225
RCC->APB2ENR &= ~RCC_APB2ENR_SYSCFGEN;
226+
227+
RCC->AHB1SMENR &= ~RCC_AHB1SMENR_FLASHSMEN;
226228

227229
/* Configure the Vector Table location add offset address ------------------*/
228230
#ifdef VECT_TAB_SRAM
4.72 KB
Binary file not shown.

0 commit comments

Comments
 (0)