pwm_sequence consume buffer

This commit is contained in:
Jacob Rosenthal 2021-11-11 19:14:07 -07:00
parent 4112759a8f
commit ec66fcd01a
2 changed files with 9 additions and 17 deletions

View File

@ -24,7 +24,7 @@ pub struct SimplePwm<'d, T: Instance> {
/// SequencePwm allows you to offload the updating of a sequence of duty cycles
/// to up to four channels, as well as repeat that sequence n times.
pub struct SequencePwm<'d, T: Instance> {
pub struct SequencePwm<'d, T: Instance, const N: usize> {
phantom: PhantomData<&'d mut T>,
ch0: Option<AnyPin>,
ch1: Option<AnyPin>,
@ -44,7 +44,7 @@ pub enum Error {
DMABufferNotInDataMemory,
}
impl<'d, T: Instance> SequencePwm<'d, T> {
impl<'d, T: Instance, const N: usize> SequencePwm<'d, T, N> {
/// Creates the interface to a `SequencePwm`.
///
/// Must be started by calling `start`
@ -55,18 +55,18 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
/// mechanisms) on stack allocated buffers which which have been passed to
/// [`new()`](SequencePwm::new).
#[allow(unused_unsafe)]
pub fn new<'a>(
pub fn new(
_pwm: impl Unborrow<Target = T> + 'd,
ch0: impl Unborrow<Target = impl GpioOptionalPin> + 'd,
ch1: impl Unborrow<Target = impl GpioOptionalPin> + 'd,
ch2: impl Unborrow<Target = impl GpioOptionalPin> + 'd,
ch3: impl Unborrow<Target = impl GpioOptionalPin> + 'd,
config: SequenceConfig,
sequence: &'a [u16],
sequence: [u16; N],
) -> Result<Self, Error> {
slice_in_ram_or(sequence, Error::DMABufferNotInDataMemory)?;
slice_in_ram_or(&sequence, Error::DMABufferNotInDataMemory)?;
if sequence.len() > 32767 {
if N > 32767 {
return Err(Error::SequenceTooLong);
}
@ -108,9 +108,7 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
r.seq0
.ptr
.write(|w| unsafe { w.bits(sequence.as_ptr() as u32) });
r.seq0
.cnt
.write(|w| unsafe { w.bits(sequence.len() as u32) });
r.seq0.cnt.write(|w| unsafe { w.bits(N as u32) });
r.seq0.refresh.write(|w| unsafe { w.bits(config.refresh) });
r.seq0
.enddelay
@ -224,7 +222,7 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
}
}
impl<'a, T: Instance> Drop for SequencePwm<'a, T> {
impl<'a, T: Instance, const N: usize> Drop for SequencePwm<'a, T, N> {
fn drop(&mut self) {
let r = T::regs();

View File

@ -98,13 +98,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
config.refresh = 3;
let pwm = unwrap!(SequencePwm::new(
p.PWM0,
p.P0_13,
NoPin,
NoPin,
NoPin,
config,
&seq_values
p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config, seq_values
));
let _ = pwm.start(SequenceMode::Infinite);
info!("pwm started!");