Skip to content

Commit 336a8c4

Browse files
committed
Use mutable reference to channels when creating transfers
1 parent 793a9d5 commit 336a8c4

File tree

4 files changed

+156
-148
lines changed

4 files changed

+156
-148
lines changed

examples/dma.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ fn main() -> ! {
3535
let source_copy = unsafe { &*(src.as_ptr() as *const [u8; 40]) };
3636
let dest_copy = unsafe { &*(dest.as_ptr() as *const [u8; 40]) };
3737

38-
let channel = channels.0;
38+
let mut channel = channels.0;
3939
let config = DmaConfig::new();
40-
let transfer = DmaTransfer::memory_to_memory(config, &channel, src, dest);
40+
let mut transfer =
41+
DmaTransfer::memory_to_memory(config, &mut channel, src, dest);
4142
transfer.start().unwrap();
4243
transfer.wait_for_transfer_complete().unwrap();
4344
assert_eq!(source_copy, dest_copy);
@@ -54,7 +55,8 @@ fn main() -> ! {
5455
.swap_destination_half_word_byte_order(),
5556
);
5657

57-
let transfer = DmaTransfer::memory_to_memory(config, &channel, src, dest);
58+
let mut transfer =
59+
DmaTransfer::memory_to_memory(config, &mut channel, src, dest);
5860

5961
transfer.start().unwrap();
6062
transfer.wait_for_transfer_complete().unwrap();
@@ -68,7 +70,8 @@ fn main() -> ! {
6870
let config = DmaConfig::new().with_data_transform(
6971
DataTransform::builder().left_align_right_truncate(),
7072
);
71-
let transfer = DmaTransfer::memory_to_memory(config, &channel, src, dest);
73+
let mut transfer =
74+
DmaTransfer::memory_to_memory(config, &mut channel, src, dest);
7275

7376
transfer.start().unwrap();
7477
transfer.wait_for_transfer_complete().unwrap();
@@ -83,7 +86,8 @@ fn main() -> ! {
8386

8487
let config =
8588
DmaConfig::new().with_data_transform(DataTransform::builder().unpack());
86-
let transfer = DmaTransfer::memory_to_memory(config, &channel, src, dest);
89+
let mut transfer =
90+
DmaTransfer::memory_to_memory(config, &mut channel, src, dest);
8791

8892
transfer.start().unwrap();
8993
transfer.wait_for_transfer_complete().unwrap();
@@ -103,7 +107,8 @@ fn main() -> ! {
103107

104108
let config =
105109
DmaConfig::new().with_data_transform(DataTransform::builder().pack());
106-
let transfer = DmaTransfer::memory_to_memory(config, &channel, src, dest);
110+
let mut transfer =
111+
DmaTransfer::memory_to_memory(config, &mut channel, src, dest);
107112

108113
transfer.start().unwrap();
109114
transfer.wait_for_transfer_complete().unwrap();

src/gpdma.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,12 @@ pub struct DmaTransfer<'a, CH>
249249
where
250250
CH: DmaChannel,
251251
{
252-
channel: &'a CH,
252+
channel: &'a mut CH,
253253
}
254254

255255
impl<'a, CH: DmaChannel> DmaTransfer<'a, CH> {
256256
fn new<S, D, T>(
257-
channel: &'a CH,
257+
channel: &'a mut CH,
258258
config: DmaConfig<T, S, D>,
259259
src_ptr: *const S,
260260
dest_ptr: *mut D,
@@ -279,7 +279,7 @@ impl<'a, CH: DmaChannel> DmaTransfer<'a, CH> {
279279
/// provided.
280280
pub fn memory_to_memory<S, D>(
281281
config: DmaConfig<MemoryToMemory, S::Word, D::Word>,
282-
channel: &'a CH,
282+
channel: &'a mut CH,
283283
source: S,
284284
mut destination: D,
285285
) -> Self
@@ -321,7 +321,7 @@ impl<'a, CH: DmaChannel> DmaTransfer<'a, CH> {
321321
/// peripheral provided.
322322
pub fn memory_to_peripheral<S, D>(
323323
config: DmaConfig<MemoryToPeripheral, S::Word, D::Word>,
324-
channel: &'a CH,
324+
channel: &'a mut CH,
325325
source: S,
326326
mut destination: D,
327327
) -> Self
@@ -343,7 +343,7 @@ impl<'a, CH: DmaChannel> DmaTransfer<'a, CH> {
343343
/// destination buffer provided.
344344
pub fn peripheral_to_memory<S, D>(
345345
config: DmaConfig<PeripheralToMemory, S::Word, D::Word>,
346-
channel: &'a CH,
346+
channel: &'a mut CH,
347347
source: S,
348348
mut destination: D,
349349
) -> Self
@@ -367,7 +367,7 @@ impl<'a, CH: DmaChannel> DmaTransfer<'a, CH> {
367367
/// provided.
368368
pub fn peripheral_to_peripheral<S, D, T>(
369369
config: DmaConfig<PeripheralToPeripheral<T>, S::Word, D::Word>,
370-
channel: &'a CH,
370+
channel: &'a mut CH,
371371
source: S,
372372
mut destination: D,
373373
) -> Self
@@ -414,7 +414,7 @@ impl<'a, CH: DmaChannel> DmaTransfer<'a, CH> {
414414
}
415415

416416
impl<'a, CH: DmaChannel> DmaTransfer<'a, CH> {
417-
fn start_transfer_internal(&self) {
417+
fn start_transfer_internal(&mut self) {
418418
// Preserve the instruction and bus ordering of preceding buffer access
419419
// to the subsequent access by the DMA peripheral due to enabling it.
420420
fence(Ordering::SeqCst);
@@ -424,13 +424,13 @@ impl<'a, CH: DmaChannel> DmaTransfer<'a, CH> {
424424

425425
/// Start a transfer. Does not block waiting for the transfer to start and does not check for
426426
/// errors starting the transfer
427-
pub fn start_nonblocking(&self) {
427+
pub fn start_nonblocking(&mut self) {
428428
self.start_transfer_internal();
429429
}
430430

431431
/// Start a transfer and block waiting for it to start. Returns an error if one occurred
432432
/// starting the transfer.
433-
pub fn start(&self) -> Result<(), Error> {
433+
pub fn start(&mut self) -> Result<(), Error> {
434434
self.start_nonblocking();
435435
self.channel.wait_for_transfer_started()
436436
}
@@ -505,24 +505,18 @@ impl<'a, CH: DmaChannel> DmaTransfer<'a, CH> {
505505

506506
/// Blocks waiting for the half transfer complete event. Returns an error if one occurred during
507507
/// the transfer.
508-
pub fn wait_for_half_transfer_complete(self) -> Result<(), Error> {
509-
let result = self.channel.wait_for_half_transfer_complete();
510-
// Preserve the instruction and bus sequence of the preceding operation and
511-
// the subsequent buffer access.
512-
fence(Ordering::SeqCst);
513-
514-
core::mem::forget(self); // Prevents self from being dropped and attempting to abort
515-
result
508+
pub fn wait_for_half_transfer_complete(&mut self) -> Result<(), Error> {
509+
self.channel.wait_for_half_transfer_complete()
516510
}
517511

518512
/// Enable interrupts for this transfer. This will enable the transfer complete and half
519513
/// transfer complete interrupts, as well as error interrupts.
520-
pub fn enable_interrupts(&self) {
514+
pub fn enable_interrupts(&mut self) {
521515
self.channel.enable_transfer_interrupts();
522516
}
523517

524518
/// Disable interrupts for this transfer.
525-
pub fn disable_interrupts(&self) {
519+
pub fn disable_interrupts(&mut self) {
526520
self.channel.disable_transfer_interrupts();
527521
}
528522

0 commit comments

Comments
 (0)