3131#include "stm32l4xx.h"
3232
3333#include "stm32l4_dma.h"
34+ #include "stm32l4_system.h"
3435
3536#include "armv7m.h"
3637
@@ -73,45 +74,28 @@ static const IRQn_Type stm32l4_dma_interrupt_table[16] = {
7374};
7475
7576typedef struct _stm32l4_dma_driver_t {
77+ stm32l4_dma_t * instances [16 ];
7678 volatile uint32_t mask ;
7779 volatile uint32_t flash ;
78- stm32l4_dma_t * instances [16 ];
80+ volatile uint32_t dma1 ;
81+ volatile uint32_t dma2 ;
7982} stm32l4_dma_driver_t ;
8083
8184static stm32l4_dma_driver_t stm32l4_dma_driver ;
8285
83- static void stm32l4_dma_track (uint32_t address )
86+ static inline void stm32l4_dma_track (uint32_t channel , uint32_t address )
8487{
8588 if (address < 0x10000000 )
8689 {
87- armv7m_atomic_add (& stm32l4_dma_driver .flash , 1 );
88-
89- armv7m_atomic_or (& RCC -> AHB1SMENR , RCC_AHB1SMENR_FLASHSMEN );
90+ stm32l4_system_periph_cond_wake (SYSTEM_PERIPH_FLASH , & stm32l4_dma_driver .flash , (1ul << channel ));
9091 }
9192}
9293
93- static void stm32l4_dma_untrack (uint32_t address )
94+ static inline void stm32l4_dma_untrack (uint32_t channel , uint32_t address )
9495{
95- uint32_t o_flash , n_flash ;
96-
9796 if (address < 0x10000000 )
9897 {
99- o_flash = stm32l4_dma_driver .flash ;
100-
101- do
102- {
103- n_flash = o_flash - 1 ;
104-
105- if (n_flash == 0 )
106- {
107- armv7m_atomic_and (& RCC -> AHB1SMENR , ~RCC_AHB1SMENR_FLASHSMEN );
108- }
109- else
110- {
111- armv7m_atomic_or (& RCC -> AHB1SMENR , RCC_AHB1SMENR_FLASHSMEN );
112- }
113- }
114- while (!armv7m_atomic_compare_exchange (& stm32l4_dma_driver .flash , & o_flash , n_flash ));
98+ stm32l4_system_periph_cond_sleep (SYSTEM_PERIPH_FLASH , & stm32l4_dma_driver .flash , (1ul << channel ));
11599 }
116100}
117101
@@ -192,12 +176,14 @@ void stm32l4_dma_enable(stm32l4_dma_t *dma, stm32l4_dma_callback_t callback, voi
192176
193177 if (!(dma -> channel & 8 ))
194178 {
195- armv7m_atomic_or (& RCC -> AHB1ENR , RCC_AHB1ENR_DMA1EN );
179+ stm32l4_system_periph_cond_enable (SYSTEM_PERIPH_DMA1 , & stm32l4_dma_driver .dma1 , (1ul << (dma -> channel & 7 )));
180+
196181 armv7m_atomic_modify (& DMA1_CSELR -> CSELR , (15 << shift ), (dma -> channel >> 4 ) << shift );
197182 }
198183 else
199184 {
200- armv7m_atomic_or (& RCC -> AHB1ENR , RCC_AHB1ENR_DMA2EN );
185+ stm32l4_system_periph_cond_enable (SYSTEM_PERIPH_DMA2 , & stm32l4_dma_driver .dma2 , (1ul << (dma -> channel & 7 )));
186+
201187 armv7m_atomic_modify (& DMA2_CSELR -> CSELR , (15 << shift ), (dma -> channel >> 4 ) << shift );
202188 }
203189
@@ -215,7 +201,16 @@ void stm32l4_dma_disable(stm32l4_dma_t *dma)
215201
216202 NVIC_DisableIRQ (dma -> interrupt );
217203
218- stm32l4_dma_untrack (DMA -> CMAR );
204+ stm32l4_dma_untrack (dma -> channel , DMA -> CMAR );
205+
206+ if (!(dma -> channel & 8 ))
207+ {
208+ stm32l4_system_periph_cond_disable (SYSTEM_PERIPH_DMA1 , & stm32l4_dma_driver .dma1 , (1ul << (dma -> channel & 7 )));
209+ }
210+ else
211+ {
212+ stm32l4_system_periph_cond_disable (SYSTEM_PERIPH_DMA2 , & stm32l4_dma_driver .dma2 , (1ul << (dma -> channel & 7 )));
213+ }
219214}
220215
221216void stm32l4_dma_start (stm32l4_dma_t * dma , uint32_t tx_data , uint32_t rx_data , uint16_t xf_count , uint32_t option )
@@ -236,18 +231,18 @@ void stm32l4_dma_start(stm32l4_dma_t *dma, uint32_t tx_data, uint32_t rx_data, u
236231 DMA2 -> IFCR = (15 << shift );
237232 }
238233
239- stm32l4_dma_untrack (DMA -> CMAR );
234+ stm32l4_dma_untrack (dma -> channel , DMA -> CMAR );
240235
241236 if (option & DMA_OPTION_MEMORY_TO_PERIPHERAL )
242237 {
243- stm32l4_dma_track (rx_data );
238+ stm32l4_dma_track (dma -> channel , rx_data );
244239
245240 DMA -> CMAR = rx_data ;
246241 DMA -> CPAR = tx_data ;
247242 }
248243 else
249244 {
250- stm32l4_dma_track (tx_data );
245+ stm32l4_dma_track (dma -> channel , tx_data );
251246
252247 DMA -> CMAR = tx_data ;
253248 DMA -> CPAR = rx_data ;
@@ -265,7 +260,7 @@ uint16_t stm32l4_dma_stop(stm32l4_dma_t *dma)
265260
266261 DMA -> CCR &= ~(DMA_CCR_EN | DMA_CCR_TCIE | DMA_CCR_HTIE | DMA_CCR_TEIE );
267262
268- stm32l4_dma_untrack (DMA -> CMAR );
263+ stm32l4_dma_untrack (dma -> channel , DMA -> CMAR );
269264
270265 DMA -> CMAR = 0xffffffff ;
271266
0 commit comments