@@ -64,21 +64,26 @@ impl<B, C: Channel, T: Target> Transfer<B, C, T> {
64
64
B : WriteBuffer + ' static ,
65
65
T : OnChannel < C > ,
66
66
{
67
- // NOTE(unsafe) We don't know the concrete type of `buffer` here, all
67
+ // SAFETY: We don't know the concrete type of `buffer` here, all
68
68
// we can use are its `WriteBuffer` methods. Hence the only `&mut self`
69
69
// method we can call is `write_buffer`, which is allowed by
70
70
// `WriteBuffer`'s safety requirements.
71
71
let ( ptr, len) = unsafe { buffer. write_buffer ( ) } ;
72
72
let len = crate :: expect!( u16 :: try_from( len) . ok( ) , "buffer is too large" ) ;
73
73
74
- // NOTE(unsafe) We are using the address of a 'static WriteBuffer here,
74
+ // SAFETY: We are using the address of a 'static WriteBuffer here,
75
75
// which is guaranteed to be safe for DMA.
76
76
unsafe { channel. set_memory_address ( ptr as u32 , Increment :: Enable ) } ;
77
77
channel. set_transfer_length ( len) ;
78
78
channel. set_word_size :: < B :: Word > ( ) ;
79
79
channel. set_direction ( Direction :: FromPeripheral ) ;
80
80
81
- unsafe { Self :: start ( buffer, channel, target) }
81
+ // SAFTEY: we take ownership of the buffer, which is 'static as well, so it lives long
82
+ // enough (at least longer that the DMA transfer itself)
83
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
84
+ unsafe {
85
+ Self :: start ( buffer, channel, target)
86
+ }
82
87
}
83
88
84
89
/// Start a DMA read transfer.
@@ -91,21 +96,26 @@ impl<B, C: Channel, T: Target> Transfer<B, C, T> {
91
96
B : ReadBuffer + ' static ,
92
97
T : OnChannel < C > ,
93
98
{
94
- // NOTE(unsafe) We don't know the concrete type of `buffer` here, all
99
+ // SAFETY: We don't know the concrete type of `buffer` here, all
95
100
// we can use are its `ReadBuffer` methods. Hence there are no
96
101
// `&mut self` methods we can call, so we are safe according to
97
102
// `ReadBuffer`'s safety requirements.
98
103
let ( ptr, len) = unsafe { buffer. read_buffer ( ) } ;
99
104
let len = crate :: expect!( u16 :: try_from( len) . ok( ) , "buffer is too large" ) ;
100
105
101
- // NOTE(unsafe) We are using the address of a 'static ReadBuffer here,
106
+ // SAFETY: We are using the address of a 'static ReadBuffer here,
102
107
// which is guaranteed to be safe for DMA.
103
108
unsafe { channel. set_memory_address ( ptr as u32 , Increment :: Enable ) } ;
104
109
channel. set_transfer_length ( len) ;
105
110
channel. set_word_size :: < B :: Word > ( ) ;
106
111
channel. set_direction ( Direction :: FromMemory ) ;
107
112
108
- unsafe { Self :: start ( buffer, channel, target) }
113
+ // SAFTEY: We take ownership of the buffer, which is 'static as well, so it lives long
114
+ // enough (at least longer that the DMA transfer itself)
115
+ #[ allow( clippy:: undocumented_unsafe_blocks) ]
116
+ unsafe {
117
+ Self :: start ( buffer, channel, target)
118
+ }
109
119
}
110
120
111
121
/// # Safety
@@ -315,7 +325,10 @@ pub trait Channel: private::Channel {
315
325
unsafe fn set_peripheral_address ( & mut self , address : u32 , inc : Increment ) {
316
326
crate :: assert!( !self . is_enabled( ) ) ;
317
327
318
- self . ch ( ) . par . write ( |w| w. pa ( ) . bits ( address) ) ;
328
+ // SAFETY: If the caller does ensure, that address is valid address, this should be safe
329
+ unsafe {
330
+ self . ch ( ) . par . write ( |w| w. pa ( ) . bits ( address) ) ;
331
+ }
319
332
self . ch ( ) . cr . modify ( |_, w| w. pinc ( ) . variant ( inc. into ( ) ) ) ;
320
333
}
321
334
@@ -335,7 +348,10 @@ pub trait Channel: private::Channel {
335
348
unsafe fn set_memory_address ( & mut self , address : u32 , inc : Increment ) {
336
349
crate :: assert!( !self . is_enabled( ) ) ;
337
350
338
- self . ch ( ) . mar . write ( |w| w. ma ( ) . bits ( address) ) ;
351
+ // SAFETY: If the caller does ensure, that address is valid address, this should be safe
352
+ unsafe {
353
+ self . ch ( ) . mar . write ( |w| w. ma ( ) . bits ( address) ) ;
354
+ }
339
355
self . ch ( ) . cr . modify ( |_, w| w. minc ( ) . variant ( inc. into ( ) ) ) ;
340
356
}
341
357
@@ -500,35 +516,31 @@ macro_rules! dma {
500
516
501
517
impl private:: Channel for $Ci {
502
518
fn ch( & self ) -> & pac:: dma1:: CH {
503
- // NOTE(unsafe) $Ci grants exclusive access to this register
519
+ // SAFETY: $Ci grants exclusive access to this register
504
520
unsafe { & ( * $DMAx:: ptr( ) ) . $chi }
505
521
}
506
522
}
507
523
508
524
impl Channel for $Ci {
509
525
fn is_event_triggered( & self , event: Event ) -> bool {
510
- use Event :: * ;
511
-
512
- // NOTE(unsafe) atomic read
526
+ // SAFETY: atomic read
513
527
let flags = unsafe { ( * $DMAx:: ptr( ) ) . isr. read( ) } ;
514
528
match event {
515
- HalfTransfer => flags. $htifi( ) . bit_is_set( ) ,
516
- TransferComplete => flags. $tcifi( ) . bit_is_set( ) ,
517
- TransferError => flags. $teifi( ) . bit_is_set( ) ,
518
- Any => flags. $gifi( ) . bit_is_set( ) ,
529
+ Event :: HalfTransfer => flags. $htifi( ) . bit_is_set( ) ,
530
+ Event :: TransferComplete => flags. $tcifi( ) . bit_is_set( ) ,
531
+ Event :: TransferError => flags. $teifi( ) . bit_is_set( ) ,
532
+ Event :: Any => flags. $gifi( ) . bit_is_set( ) ,
519
533
}
520
534
}
521
535
522
536
fn clear_event( & mut self , event: Event ) {
523
- use Event :: * ;
524
-
525
- // NOTE(unsafe) atomic write to a stateless register
537
+ // SAFETY: atomic write to a stateless register
526
538
unsafe {
527
539
( * $DMAx:: ptr( ) ) . ifcr. write( |w| match event {
528
- HalfTransfer => w. $chtifi( ) . set_bit( ) ,
529
- TransferComplete => w. $ctcifi( ) . set_bit( ) ,
530
- TransferError => w. $cteifi( ) . set_bit( ) ,
531
- Any => w. $cgifi( ) . set_bit( ) ,
540
+ Event :: HalfTransfer => w. $chtifi( ) . set_bit( ) ,
541
+ Event :: TransferComplete => w. $ctcifi( ) . set_bit( ) ,
542
+ Event :: TransferError => w. $cteifi( ) . set_bit( ) ,
543
+ Event :: Any => w. $cgifi( ) . set_bit( ) ,
532
544
} ) ;
533
545
}
534
546
}
0 commit comments