@@ -74,109 +74,60 @@ static const IRQn_Type stm32l4_dma_interrupt_table[16] = {
74
74
75
75
typedef struct _stm32l4_dma_driver_t {
76
76
volatile uint32_t mask ;
77
- #ifdef notyet
78
77
volatile uint32_t flash ;
79
- volatile uint32_t sram1 ;
80
- volatile uint32_t sram2 ;
81
- #endif
82
78
stm32l4_dma_t * instances [16 ];
83
79
} stm32l4_dma_driver_t ;
84
80
85
81
static stm32l4_dma_driver_t stm32l4_dma_driver ;
86
82
87
- static void stm32l4_dma_track ( uint8_t channel , uint32_t address )
83
+ static void stm32l4_dma_flash_sleep ( void )
88
84
{
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 ;
95
86
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 )
99
94
{
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 );
103
96
}
104
97
else
105
98
{
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 );
109
100
}
110
101
}
111
- #endif
102
+ while (! armv7m_atomic_compare_exchange ( & stm32l4_dma_driver . flash , & o_flash , n_flash ));
112
103
}
113
104
114
- static void stm32l4_dma_untrack ( uint8_t channel , uint32_t address )
105
+ static void stm32l4_dma_track ( uint32_t address )
115
106
{
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 )
120
108
{
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
+ }
124
114
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 )
141
120
{
142
- o_sram2 = stm32l4_dma_driver .sram2 ;
143
-
144
- do
121
+ if (__get_IPSR () == 0 )
145
122
{
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 );
156
124
}
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
164
126
{
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 ();
175
128
}
176
- while (!armv7m_atomic_compare_exchange (& stm32l4_dma_driver .sram1 , & o_sram1 , n_sram1 ));
177
129
}
178
130
}
179
- #endif
180
131
}
181
132
182
133
static void stm32l4_dma_interrupt (stm32l4_dma_t * dma )
@@ -246,6 +197,7 @@ void stm32l4_dma_destroy(stm32l4_dma_t *dma)
246
197
247
198
void stm32l4_dma_enable (stm32l4_dma_t * dma , stm32l4_dma_callback_t callback , void * context )
248
199
{
200
+ DMA_Channel_TypeDef * DMA = dma -> DMA ;
249
201
unsigned int shift ;
250
202
251
203
dma -> callback = callback ;
@@ -264,6 +216,8 @@ void stm32l4_dma_enable(stm32l4_dma_t *dma, stm32l4_dma_callback_t callback, voi
264
216
armv7m_atomic_modify (& DMA2_CSELR -> CSELR , (15 << shift ), (dma -> channel >> 4 ) << shift );
265
217
}
266
218
219
+ DMA -> CMAR = 0xffffffff ;
220
+
267
221
if (callback )
268
222
{
269
223
NVIC_EnableIRQ (dma -> interrupt );
@@ -272,7 +226,11 @@ void stm32l4_dma_enable(stm32l4_dma_t *dma, stm32l4_dma_callback_t callback, voi
272
226
273
227
void stm32l4_dma_disable (stm32l4_dma_t * dma )
274
228
{
229
+ DMA_Channel_TypeDef * DMA = dma -> DMA ;
230
+
275
231
NVIC_DisableIRQ (dma -> interrupt );
232
+
233
+ stm32l4_dma_untrack (DMA -> CMAR );
276
234
}
277
235
278
236
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
293
251
DMA2 -> IFCR = (15 << shift );
294
252
}
295
253
296
- dma -> size = xf_count ;
254
+ stm32l4_dma_untrack ( DMA -> CMAR ) ;
297
255
298
256
if (option & DMA_OPTION_MEMORY_TO_PERIPHERAL )
299
257
{
300
- stm32l4_dma_track (dma -> channel , rx_data );
258
+ stm32l4_dma_track (rx_data );
301
259
302
260
DMA -> CMAR = rx_data ;
303
261
DMA -> CPAR = tx_data ;
304
262
}
305
263
else
306
264
{
307
- stm32l4_dma_track (dma -> channel , ( uint32_t ) tx_data );
265
+ stm32l4_dma_track (tx_data );
308
266
309
267
DMA -> CMAR = tx_data ;
310
268
DMA -> CPAR = rx_data ;
311
269
}
312
270
271
+ dma -> size = xf_count ;
272
+
313
273
DMA -> CNDTR = xf_count ;
314
274
DMA -> CCR = option | DMA_CCR_EN ;
315
275
}
@@ -320,7 +280,9 @@ uint16_t stm32l4_dma_stop(stm32l4_dma_t *dma)
320
280
321
281
DMA -> CCR &= ~(DMA_CCR_EN | DMA_CCR_TCIE | DMA_CCR_HTIE | DMA_CCR_TEIE );
322
282
323
- stm32l4_dma_untrack (dma -> channel , DMA -> CMAR );
283
+ stm32l4_dma_untrack (DMA -> CMAR );
284
+
285
+ DMA -> CMAR = 0xffffffff ;
324
286
325
287
return dma -> size - (DMA -> CNDTR & 0xffff );
326
288
}
0 commit comments