@@ -64,21 +64,26 @@ impl<B, C: Channel, T: Target> Transfer<B, C, T> {
6464 B : WriteBuffer + ' static ,
6565 T : OnChannel < C > ,
6666 {
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
6868 // we can use are its `WriteBuffer` methods. Hence the only `&mut self`
6969 // method we can call is `write_buffer`, which is allowed by
7070 // `WriteBuffer`'s safety requirements.
7171 let ( ptr, len) = unsafe { buffer. write_buffer ( ) } ;
7272 let len = crate :: expect!( u16 :: try_from( len) . ok( ) , "buffer is too large" ) ;
7373
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,
7575 // which is guaranteed to be safe for DMA.
7676 unsafe { channel. set_memory_address ( ptr as u32 , Increment :: Enable ) } ;
7777 channel. set_transfer_length ( len) ;
7878 channel. set_word_size :: < B :: Word > ( ) ;
7979 channel. set_direction ( Direction :: FromPeripheral ) ;
8080
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+ }
8287 }
8388
8489 /// Start a DMA read transfer.
@@ -91,21 +96,26 @@ impl<B, C: Channel, T: Target> Transfer<B, C, T> {
9196 B : ReadBuffer + ' static ,
9297 T : OnChannel < C > ,
9398 {
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
95100 // we can use are its `ReadBuffer` methods. Hence there are no
96101 // `&mut self` methods we can call, so we are safe according to
97102 // `ReadBuffer`'s safety requirements.
98103 let ( ptr, len) = unsafe { buffer. read_buffer ( ) } ;
99104 let len = crate :: expect!( u16 :: try_from( len) . ok( ) , "buffer is too large" ) ;
100105
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,
102107 // which is guaranteed to be safe for DMA.
103108 unsafe { channel. set_memory_address ( ptr as u32 , Increment :: Enable ) } ;
104109 channel. set_transfer_length ( len) ;
105110 channel. set_word_size :: < B :: Word > ( ) ;
106111 channel. set_direction ( Direction :: FromMemory ) ;
107112
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+ }
109119 }
110120
111121 /// # Safety
@@ -315,7 +325,10 @@ pub trait Channel: private::Channel {
315325 unsafe fn set_peripheral_address ( & mut self , address : u32 , inc : Increment ) {
316326 crate :: assert!( !self . is_enabled( ) ) ;
317327
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+ }
319332 self . ch ( ) . cr . modify ( |_, w| w. pinc ( ) . variant ( inc. into ( ) ) ) ;
320333 }
321334
@@ -335,7 +348,10 @@ pub trait Channel: private::Channel {
335348 unsafe fn set_memory_address ( & mut self , address : u32 , inc : Increment ) {
336349 crate :: assert!( !self . is_enabled( ) ) ;
337350
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+ }
339355 self . ch ( ) . cr . modify ( |_, w| w. minc ( ) . variant ( inc. into ( ) ) ) ;
340356 }
341357
@@ -500,35 +516,31 @@ macro_rules! dma {
500516
501517 impl private:: Channel for $Ci {
502518 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
504520 unsafe { & ( * $DMAx:: ptr( ) ) . $chi }
505521 }
506522 }
507523
508524 impl Channel for $Ci {
509525 fn is_event_triggered( & self , event: Event ) -> bool {
510- use Event :: * ;
511-
512- // NOTE(unsafe) atomic read
526+ // SAFETY: atomic read
513527 let flags = unsafe { ( * $DMAx:: ptr( ) ) . isr. read( ) } ;
514528 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( ) ,
519533 }
520534 }
521535
522536 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
526538 unsafe {
527539 ( * $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( ) ,
532544 } ) ;
533545 }
534546 }
0 commit comments