Skip to content

Commit 90ec787

Browse files
committed
Use embedded-dma trait
1 parent 3ce300c commit 90ec787

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

nrf-hal-common/src/pwm.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::{
1919
target_constants::{SRAM_LOWER, SRAM_UPPER},
2020
time::*,
2121
};
22+
use embedded_dma::*;
2223

2324
/// A safe wrapper around the raw peripheral.
2425
#[derive(Debug)]
@@ -473,37 +474,29 @@ where
473474

474475
/// Loads a sequence buffer.
475476
/// NOTE: `buf` must live until the sequence is done playing, or it might play a corrupted sequence.
476-
pub fn load_seq(&self, seq: Seq, buf: &[u16]) -> Result<(), Error> {
477-
if (buf.as_ptr() as usize) < SRAM_LOWER || (buf.as_ptr() as usize) > SRAM_UPPER {
477+
pub fn load_seq<W, B>(&self, seq: Seq, buf: B) -> Result<(), Error>
478+
where
479+
B: ReadBuffer<Word = W>,
480+
{
481+
let (ptr, len) = unsafe { buf.read_buffer() };
482+
if (ptr as usize) < SRAM_LOWER || (ptr as usize) > SRAM_UPPER {
478483
return Err(Error::DMABufferNotInDataMemory);
479484
}
480485

481-
if buf.len() > 32_768 {
486+
if len > (1 << 15) / core::mem::size_of::<W>() {
482487
return Err(Error::BufferTooLong);
483488
}
484489

485490
compiler_fence(Ordering::SeqCst);
486491

487492
match seq {
488493
Seq::Seq0 => {
489-
self.pwm
490-
.seq0
491-
.ptr
492-
.write(|w| unsafe { w.bits(buf.as_ptr() as u32) });
493-
self.pwm
494-
.seq0
495-
.cnt
496-
.write(|w| unsafe { w.bits(buf.len() as u32) });
494+
self.pwm.seq0.ptr.write(|w| unsafe { w.bits(ptr as u32) });
495+
self.pwm.seq0.cnt.write(|w| unsafe { w.bits(len as u32) });
497496
}
498497
Seq::Seq1 => {
499-
self.pwm
500-
.seq1
501-
.ptr
502-
.write(|w| unsafe { w.bits(buf.as_ptr() as u32) });
503-
self.pwm
504-
.seq1
505-
.cnt
506-
.write(|w| unsafe { w.bits(buf.len() as u32) });
498+
self.pwm.seq1.ptr.write(|w| unsafe { w.bits(ptr as u32) });
499+
self.pwm.seq1.cnt.write(|w| unsafe { w.bits(len as u32) });
507500
}
508501
}
509502
Ok(())

0 commit comments

Comments
 (0)