Skip to content

Commit c4c5497

Browse files
committed
Mark new as unsafe; add doc comment for DmaChannel
1 parent 48acbcd commit c4c5497

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/gpdma.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,20 @@ impl<DMA: Instance> DmaChannels<DMA> {
231231
/// Splits the DMA peripheral into channels.
232232
pub(super) fn new(_regs: DMA, rec: DMA::Rec) -> Self {
233233
let _ = rec.reset().enable();
234-
Self(
235-
DmaChannel0::new(),
236-
DmaChannel1::new(),
237-
DmaChannel2::new(),
238-
DmaChannel3::new(),
239-
DmaChannel4::new(),
240-
DmaChannel5::new(),
241-
DmaChannel6::new(),
242-
DmaChannel7::new(),
243-
)
234+
// Safety: The channels are only initialized for user code here once, so no copies can be
235+
// made available outside this module.
236+
unsafe {
237+
Self(
238+
DmaChannel0::new_unsafe(),
239+
DmaChannel1::new_unsafe(),
240+
DmaChannel2::new_unsafe(),
241+
DmaChannel3::new_unsafe(),
242+
DmaChannel4::new_unsafe(),
243+
DmaChannel5::new_unsafe(),
244+
DmaChannel6::new_unsafe(),
245+
DmaChannel7::new_unsafe(),
246+
)
247+
}
244248
}
245249
}
246250

src/gpdma/ch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ where
131131
DMA: Instance,
132132
CH: ChannelRegs,
133133
{
134-
pub(super) fn new() -> Self {
134+
pub(super) unsafe fn new_unsafe() -> Self {
135135
DmaChannelRef {
136136
_dma: PhantomData,
137137
_ch: PhantomData,

src/gpdma/future.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ use super::{
3939
DmaTransfer, Error, Instance, Word,
4040
};
4141

42-
// This trait is defined to be bound by ChannelWaker when futures are enabled via the
42+
/// DmaChannel trait provides the API contract that all GPDMA channels exposed to the user
43+
/// implement.
44+
// Note: This trait is defined to be bound by ChannelWaker when futures are enabled via the
4345
// `gpdma-futures` feature. This is to ensure that the waker associated with a channel can
4446
// be accessed from the interrupt handler and the DmaTransfer `poll` function.
4547
// Specifically, this alternate definition is needed to ensure that the DmaChannel implementations
@@ -150,6 +152,7 @@ where
150152
{
151153
#[inline(always)]
152154
fn handle_interrupt() {
155+
// Safety:
153156
// This creates a DmaChannelRef instance for channel N, which is be a duplicate to the
154157
// DmaChannelRef instance that is held as a mutable reference in the DmaTransfer struct
155158
// while a transfer is in progress. However, it is only used to disable the transfer
@@ -159,7 +162,7 @@ where
159162
// When the DmaTransfer struct is dropped, interrupts are disabled for the channel,
160163
// preventing this interrupt from being triggered. Interrupts are only enabled when the
161164
// transfer is awaited (calling IntoFuture::into_future).
162-
let mut ch = Self::new();
165+
let mut ch = unsafe { Self::new_unsafe() };
163166

164167
// This is a single volatile write to the channel's interrupt status register to disable
165168
// interrupts for the channel so this interrupt doesn't trigger again while the transfer

0 commit comments

Comments
 (0)