@@ -18,7 +18,7 @@ use core::{
18
18
use embedded_dma:: { StaticReadBuffer , StaticWriteBuffer } ;
19
19
20
20
use crate :: pac:: RCC ;
21
- use crate :: rcc;
21
+ use crate :: { pac , rcc} ;
22
22
23
23
pub mod traits;
24
24
use traits:: {
@@ -211,6 +211,29 @@ impl<DMA, const S: u8> StreamX<DMA, S> {
211
211
}
212
212
}
213
213
214
+ impl < DMA : Instance , const S : u8 > StreamX < DMA , S > {
215
+ #[ cfg( not( any(
216
+ feature = "stm32f411" ,
217
+ feature = "stm32f413" ,
218
+ feature = "stm32f423" ,
219
+ feature = "stm32f410"
220
+ ) ) ) ]
221
+ #[ inline( always) ]
222
+ unsafe fn st ( ) -> & ' static pac:: dma2:: ST {
223
+ & ( * DMA :: ptr ( ) ) . st [ S as usize ]
224
+ }
225
+ #[ cfg( any(
226
+ feature = "stm32f411" ,
227
+ feature = "stm32f413" ,
228
+ feature = "stm32f423" ,
229
+ feature = "stm32f410"
230
+ ) ) ]
231
+ #[ inline( always) ]
232
+ unsafe fn st ( ) -> & ' static pac:: dma1:: ST {
233
+ & ( * DMA :: ptr ( ) ) . st [ S as usize ]
234
+ }
235
+ }
236
+
214
237
/// Stream 0 on the DMA controller.
215
238
pub type Stream0 < DMA > = StreamX < DMA , 0 > ;
216
239
/// Stream 1 on the DMA controller.
@@ -238,25 +261,25 @@ impl<DMA> Sealed for StreamX<DMA, 6> {}
238
261
impl < DMA > Sealed for StreamX < DMA , 7 > { }
239
262
240
263
/// Alias for a tuple with all DMA streams.
241
- pub struct StreamsTuple < T > (
242
- pub StreamX < T , 0 > ,
243
- pub StreamX < T , 1 > ,
244
- pub StreamX < T , 2 > ,
245
- pub StreamX < T , 3 > ,
246
- pub StreamX < T , 4 > ,
247
- pub StreamX < T , 5 > ,
248
- pub StreamX < T , 6 > ,
249
- pub StreamX < T , 7 > ,
264
+ pub struct StreamsTuple < DMA > (
265
+ pub StreamX < DMA , 0 > ,
266
+ pub StreamX < DMA , 1 > ,
267
+ pub StreamX < DMA , 2 > ,
268
+ pub StreamX < DMA , 3 > ,
269
+ pub StreamX < DMA , 4 > ,
270
+ pub StreamX < DMA , 5 > ,
271
+ pub StreamX < DMA , 6 > ,
272
+ pub StreamX < DMA , 7 > ,
250
273
) ;
251
274
252
- impl < T : rcc:: Enable + rcc:: Reset > StreamsTuple < T > {
275
+ impl < DMA : rcc:: Enable + rcc:: Reset > StreamsTuple < DMA > {
253
276
/// Splits the DMA peripheral into streams.
254
- pub fn new ( _regs : T ) -> Self {
277
+ pub fn new ( _regs : DMA ) -> Self {
255
278
unsafe {
256
279
//NOTE(unsafe) this reference will only be used for atomic writes with no side effects
257
280
let rcc = & ( * RCC :: ptr ( ) ) ;
258
- T :: enable ( rcc) ;
259
- T :: reset ( rcc) ;
281
+ DMA :: enable ( rcc) ;
282
+ DMA :: reset ( rcc) ;
260
283
}
261
284
Self (
262
285
StreamX :: new ( ) ,
@@ -279,84 +302,63 @@ where
279
302
280
303
#[ inline( always) ]
281
304
fn set_peripheral_address ( & mut self , value : u32 ) {
282
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
283
- let dma = unsafe { & * I :: ptr ( ) } ;
284
- dma. st [ Self :: NUMBER ]
305
+ unsafe { Self :: st ( ) }
285
306
. par
286
307
. write ( |w| unsafe { w. pa ( ) . bits ( value) } ) ;
287
308
}
288
309
289
310
#[ inline( always) ]
290
311
fn set_memory_address ( & mut self , value : u32 ) {
291
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
292
- let dma = unsafe { & * I :: ptr ( ) } ;
293
- dma. st [ Self :: NUMBER ]
312
+ unsafe { Self :: st ( ) }
294
313
. m0ar
295
314
. write ( |w| unsafe { w. m0a ( ) . bits ( value) } ) ;
296
315
}
297
316
298
317
#[ inline( always) ]
299
318
fn get_memory_address ( & self ) -> u32 {
300
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
301
- let dma = unsafe { & * I :: ptr ( ) } ;
302
- dma. st [ Self :: NUMBER ] . m0ar . read ( ) . m0a ( ) . bits ( )
319
+ unsafe { Self :: st ( ) } . m0ar . read ( ) . m0a ( ) . bits ( )
303
320
}
304
321
305
322
#[ inline( always) ]
306
323
fn set_memory_double_buffer_address ( & mut self , value : u32 ) {
307
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
308
- let dma = unsafe { & * I :: ptr ( ) } ;
309
- dma. st [ Self :: NUMBER ]
324
+ unsafe { Self :: st ( ) }
310
325
. m1ar
311
326
. write ( |w| unsafe { w. m1a ( ) . bits ( value) } ) ;
312
327
}
313
328
314
329
#[ inline( always) ]
315
330
fn get_memory_double_buffer_address ( & self ) -> u32 {
316
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
317
- let dma = unsafe { & * I :: ptr ( ) } ;
318
- dma. st [ Self :: NUMBER ] . m1ar . read ( ) . m1a ( ) . bits ( )
331
+ unsafe { Self :: st ( ) } . m1ar . read ( ) . m1a ( ) . bits ( )
319
332
}
320
333
321
334
#[ inline( always) ]
322
335
fn set_number_of_transfers ( & mut self , value : u16 ) {
323
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
324
- let dma = unsafe { & * I :: ptr ( ) } ;
325
- dma. st [ Self :: NUMBER ] . ndtr . write ( |w| w. ndt ( ) . bits ( value) ) ;
336
+ unsafe { Self :: st ( ) } . ndtr . write ( |w| w. ndt ( ) . bits ( value) ) ;
326
337
}
327
338
328
339
#[ inline( always) ]
329
340
fn get_number_of_transfers ( ) -> u16 {
330
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
331
- let dma = unsafe { & * I :: ptr ( ) } ;
332
- dma. st [ Self :: NUMBER ] . ndtr . read ( ) . ndt ( ) . bits ( )
341
+ unsafe { Self :: st ( ) } . ndtr . read ( ) . ndt ( ) . bits ( )
333
342
}
334
343
335
344
#[ inline( always) ]
336
345
unsafe fn enable ( & mut self ) {
337
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
338
- let dma = & * I :: ptr ( ) ;
339
- dma. st [ Self :: NUMBER ] . cr . modify ( |_, w| w. en ( ) . set_bit ( ) ) ;
346
+ Self :: st ( ) . cr . modify ( |_, w| w. en ( ) . set_bit ( ) ) ;
340
347
}
341
348
342
349
#[ inline( always) ]
343
350
fn is_enabled ( ) -> bool {
344
- //NOTE(unsafe) Atomic read with no side effects
345
- let dma = unsafe { & * I :: ptr ( ) } ;
346
- dma. st [ Self :: NUMBER ] . cr . read ( ) . en ( ) . bit_is_set ( )
351
+ unsafe { Self :: st ( ) } . cr . read ( ) . en ( ) . bit_is_set ( )
347
352
}
348
353
349
354
fn disable ( & mut self ) {
350
355
if Self :: is_enabled ( ) {
351
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
352
- let dma = unsafe { & * I :: ptr ( ) } ;
353
-
354
356
// Aborting an on-going transfer might cause interrupts to fire, disable
355
357
// them
356
358
let ( tc, ht, te, dm) = Self :: get_interrupts_enable ( ) ;
357
359
self . set_interrupts_enable ( false , false , false , false ) ;
358
360
359
- dma . st [ Self :: NUMBER ] . cr . modify ( |_, w| w. en ( ) . clear_bit ( ) ) ;
361
+ unsafe { Self :: st ( ) } . cr . modify ( |_, w| w. en ( ) . clear_bit ( ) ) ;
360
362
while Self :: is_enabled ( ) { }
361
363
362
364
self . clear_interrupts ( ) ;
@@ -366,59 +368,45 @@ where
366
368
367
369
#[ inline( always) ]
368
370
fn set_channel < C : Channel > ( & mut self , channel : C ) {
369
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
370
- let dma = unsafe { & * I :: ptr ( ) } ;
371
- dma. st [ Self :: NUMBER ]
371
+ unsafe { Self :: st ( ) }
372
372
. cr
373
373
. modify ( |_, w| w. chsel ( ) . bits ( channel. bits ( ) ) ) ;
374
374
}
375
375
376
376
#[ inline( always) ]
377
377
fn set_priority ( & mut self , priority : config:: Priority ) {
378
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
379
- let dma = unsafe { & * I :: ptr ( ) } ;
380
- dma. st [ Self :: NUMBER ]
378
+ unsafe { Self :: st ( ) }
381
379
. cr
382
380
. modify ( |_, w| w. pl ( ) . bits ( priority. bits ( ) ) ) ;
383
381
}
384
382
385
383
#[ inline( always) ]
386
384
unsafe fn set_memory_size ( & mut self , size : u8 ) {
387
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
388
- let dma = & * I :: ptr ( ) ;
389
- dma. st [ Self :: NUMBER ] . cr . modify ( |_, w| w. msize ( ) . bits ( size) ) ;
385
+ Self :: st ( ) . cr . modify ( |_, w| w. msize ( ) . bits ( size) ) ;
390
386
}
391
387
392
388
#[ inline( always) ]
393
389
unsafe fn set_peripheral_size ( & mut self , size : u8 ) {
394
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
395
- let dma = & * I :: ptr ( ) ;
396
- dma. st [ Self :: NUMBER ] . cr . modify ( |_, w| w. psize ( ) . bits ( size) ) ;
390
+ Self :: st ( ) . cr . modify ( |_, w| w. psize ( ) . bits ( size) ) ;
397
391
}
398
392
399
393
#[ inline( always) ]
400
394
fn set_memory_increment ( & mut self , increment : bool ) {
401
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
402
- let dma = unsafe { & * I :: ptr ( ) } ;
403
- dma. st [ Self :: NUMBER ]
395
+ unsafe { Self :: st ( ) }
404
396
. cr
405
397
. modify ( |_, w| w. minc ( ) . bit ( increment) ) ;
406
398
}
407
399
408
400
#[ inline( always) ]
409
401
fn set_peripheral_increment ( & mut self , increment : bool ) {
410
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
411
- let dma = unsafe { & * I :: ptr ( ) } ;
412
- dma. st [ Self :: NUMBER ]
402
+ unsafe { Self :: st ( ) }
413
403
. cr
414
404
. modify ( |_, w| w. pinc ( ) . bit ( increment) ) ;
415
405
}
416
406
417
407
#[ inline( always) ]
418
408
fn set_direction < D : Direction > ( & mut self , direction : D ) {
419
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
420
- let dma = unsafe { & * I :: ptr ( ) } ;
421
- dma. st [ Self :: NUMBER ]
409
+ unsafe { Self :: st ( ) }
422
410
. cr
423
411
. modify ( |_, w| unsafe { w. dir ( ) . bits ( direction. bits ( ) ) } ) ;
424
412
}
@@ -431,9 +419,7 @@ where
431
419
transfer_error : bool ,
432
420
direct_mode_error : bool ,
433
421
) {
434
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
435
- let dma = unsafe { & * I :: ptr ( ) } ;
436
- dma. st [ Self :: NUMBER ] . cr . modify ( |_, w| {
422
+ unsafe { Self :: st ( ) } . cr . modify ( |_, w| {
437
423
w. tcie ( )
438
424
. bit ( transfer_complete)
439
425
. htie ( )
@@ -447,9 +433,7 @@ where
447
433
448
434
#[ inline( always) ]
449
435
fn get_interrupts_enable ( ) -> ( bool , bool , bool , bool ) {
450
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
451
- let dma = unsafe { & * I :: ptr ( ) } ;
452
- let cr = dma. st [ Self :: NUMBER ] . cr . read ( ) ;
436
+ let cr = unsafe { Self :: st ( ) } . cr . read ( ) ;
453
437
(
454
438
cr. tcie ( ) . bit_is_set ( ) ,
455
439
cr. htie ( ) . bit_is_set ( ) ,
@@ -460,106 +444,82 @@ where
460
444
461
445
#[ inline( always) ]
462
446
fn set_transfer_complete_interrupt_enable ( & mut self , transfer_complete_interrupt : bool ) {
463
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
464
- let dma = unsafe { & * I :: ptr ( ) } ;
465
- dma. st [ Self :: NUMBER ]
447
+ unsafe { Self :: st ( ) }
466
448
. cr
467
449
. modify ( |_, w| w. tcie ( ) . bit ( transfer_complete_interrupt) ) ;
468
450
}
469
451
470
452
#[ inline( always) ]
471
453
fn set_half_transfer_interrupt_enable ( & mut self , half_transfer_interrupt : bool ) {
472
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
473
- let dma = unsafe { & * I :: ptr ( ) } ;
474
- dma. st [ Self :: NUMBER ]
454
+ unsafe { Self :: st ( ) }
475
455
. cr
476
456
. modify ( |_, w| w. htie ( ) . bit ( half_transfer_interrupt) ) ;
477
457
}
478
458
479
459
#[ inline( always) ]
480
460
fn set_transfer_error_interrupt_enable ( & mut self , transfer_error_interrupt : bool ) {
481
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
482
- let dma = unsafe { & * I :: ptr ( ) } ;
483
- dma. st [ Self :: NUMBER ]
461
+ unsafe { Self :: st ( ) }
484
462
. cr
485
463
. modify ( |_, w| w. teie ( ) . bit ( transfer_error_interrupt) ) ;
486
464
}
487
465
488
466
#[ inline( always) ]
489
467
fn set_direct_mode_error_interrupt_enable ( & mut self , direct_mode_error_interrupt : bool ) {
490
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
491
- let dma = unsafe { & * I :: ptr ( ) } ;
492
- dma. st [ Self :: NUMBER ]
468
+ unsafe { Self :: st ( ) }
493
469
. cr
494
470
. modify ( |_, w| w. dmeie ( ) . bit ( direct_mode_error_interrupt) ) ;
495
471
}
496
472
497
473
#[ inline( always) ]
498
474
fn set_fifo_error_interrupt_enable ( & mut self , fifo_error_interrupt : bool ) {
499
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
500
- let dma = unsafe { & * I :: ptr ( ) } ;
501
- dma. st [ Self :: NUMBER ]
475
+ unsafe { Self :: st ( ) }
502
476
. fcr
503
477
. modify ( |_, w| w. feie ( ) . bit ( fifo_error_interrupt) ) ;
504
478
}
505
479
506
480
#[ inline( always) ]
507
481
fn set_double_buffer ( & mut self , double_buffer : bool ) {
508
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
509
- let dma = unsafe { & * I :: ptr ( ) } ;
510
- dma. st [ Self :: NUMBER ]
482
+ unsafe { Self :: st ( ) }
511
483
. cr
512
484
. modify ( |_, w| w. dbm ( ) . bit ( double_buffer) ) ;
513
485
}
514
486
515
487
#[ inline( always) ]
516
488
fn set_fifo_threshold ( & mut self , fifo_threshold : config:: FifoThreshold ) {
517
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
518
- let dma = unsafe { & * I :: ptr ( ) } ;
519
- dma. st [ Self :: NUMBER ]
489
+ unsafe { Self :: st ( ) }
520
490
. fcr
521
491
. modify ( |_, w| w. fth ( ) . bits ( fifo_threshold. bits ( ) ) ) ;
522
492
}
523
493
524
494
#[ inline( always) ]
525
495
fn set_fifo_enable ( & mut self , fifo_enable : bool ) {
526
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
527
- let dma = unsafe { & * I :: ptr ( ) } ;
528
496
//Register is actually direct mode disable rather than fifo enable
529
- dma . st [ Self :: NUMBER ]
497
+ unsafe { Self :: st ( ) }
530
498
. fcr
531
499
. modify ( |_, w| w. dmdis ( ) . bit ( fifo_enable) ) ;
532
500
}
533
501
534
502
#[ inline( always) ]
535
503
fn set_memory_burst ( & mut self , memory_burst : config:: BurstMode ) {
536
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
537
- let dma = unsafe { & * I :: ptr ( ) } ;
538
- dma. st [ Self :: NUMBER ]
504
+ unsafe { Self :: st ( ) }
539
505
. cr
540
506
. modify ( |_, w| w. mburst ( ) . bits ( memory_burst. bits ( ) ) ) ;
541
507
}
542
508
543
509
#[ inline( always) ]
544
510
fn set_peripheral_burst ( & mut self , peripheral_burst : config:: BurstMode ) {
545
- //NOTE(unsafe) We only access the registers that belongs to the StreamX
546
- let dma = unsafe { & * I :: ptr ( ) } ;
547
- dma. st [ Self :: NUMBER ]
511
+ unsafe { Self :: st ( ) }
548
512
. cr
549
513
. modify ( |_, w| w. pburst ( ) . bits ( peripheral_burst. bits ( ) ) ) ;
550
514
}
551
515
552
516
#[ inline( always) ]
553
517
fn fifo_level ( ) -> FifoLevel {
554
- //NOTE(unsafe) Atomic read with no side effects
555
- let dma = unsafe { & * I :: ptr ( ) } ;
556
- dma. st [ Self :: NUMBER ] . fcr . read ( ) . fs ( ) . bits ( ) . into ( )
518
+ unsafe { Self :: st ( ) } . fcr . read ( ) . fs ( ) . bits ( ) . into ( )
557
519
}
558
520
559
521
fn current_buffer ( ) -> CurrentBuffer {
560
- //NOTE(unsafe) Atomic read with no side effects
561
- let dma = unsafe { & * I :: ptr ( ) } ;
562
- if dma. st [ Self :: NUMBER ] . cr . read ( ) . ct ( ) . bit_is_set ( ) {
522
+ if unsafe { Self :: st ( ) } . cr . read ( ) . ct ( ) . bit_is_set ( ) {
563
523
CurrentBuffer :: DoubleBuffer
564
524
} else {
565
525
CurrentBuffer :: FirstBuffer
0 commit comments