2021-05-12 02:56:11 +00:00
|
|
|
#![macro_use]
|
|
|
|
|
|
|
|
use core::marker::PhantomData;
|
|
|
|
use core::sync::atomic::{compiler_fence, Ordering};
|
|
|
|
use embassy::util::Unborrow;
|
2021-11-10 20:01:39 +00:00
|
|
|
use embassy_hal_common::unborrow;
|
2021-05-12 02:56:11 +00:00
|
|
|
|
|
|
|
use crate::gpio::sealed::Pin as _;
|
2021-11-10 19:29:13 +00:00
|
|
|
use crate::gpio::{AnyPin, OptionalPin as GpioOptionalPin};
|
2021-05-12 02:56:11 +00:00
|
|
|
use crate::interrupt::Interrupt;
|
2021-11-01 08:15:04 +00:00
|
|
|
use crate::pac;
|
2021-10-26 07:37:52 +00:00
|
|
|
use crate::util::slice_in_ram_or;
|
2021-05-12 02:56:11 +00:00
|
|
|
|
2021-11-11 02:43:47 +00:00
|
|
|
/// SimplePwm is the traditional pwm interface you're probably used to, allowing
|
|
|
|
/// to simply set a duty cycle across up to four channels.
|
2021-11-04 01:25:44 +00:00
|
|
|
pub struct SimplePwm<'d, T: Instance> {
|
2021-05-12 02:56:11 +00:00
|
|
|
phantom: PhantomData<&'d mut T>,
|
2021-11-10 19:32:28 +00:00
|
|
|
duty: [u16; 4],
|
2021-11-10 18:02:43 +00:00
|
|
|
ch0: Option<AnyPin>,
|
|
|
|
ch1: Option<AnyPin>,
|
|
|
|
ch2: Option<AnyPin>,
|
|
|
|
ch3: Option<AnyPin>,
|
2021-05-12 02:56:11 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 02:44:57 +00:00
|
|
|
/// 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.
|
2021-11-12 02:49:41 +00:00
|
|
|
pub struct SequencePwm<'d, T: Instance> {
|
2021-11-02 02:11:37 +00:00
|
|
|
phantom: PhantomData<&'d mut T>,
|
2021-11-10 18:02:43 +00:00
|
|
|
ch0: Option<AnyPin>,
|
|
|
|
ch1: Option<AnyPin>,
|
|
|
|
ch2: Option<AnyPin>,
|
|
|
|
ch3: Option<AnyPin>,
|
2021-10-26 07:37:52 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 01:37:54 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum Error {
|
|
|
|
/// Max Sequence size is 32767
|
|
|
|
SequenceTooLong,
|
|
|
|
/// Min Sequence count is 1
|
|
|
|
SequenceTimesAtLeastOne,
|
|
|
|
/// EasyDMA can only read from data memory, read only buffers in flash will fail.
|
|
|
|
DMABufferNotInDataMemory,
|
|
|
|
}
|
|
|
|
|
2021-11-12 02:49:41 +00:00
|
|
|
impl<'d, T: Instance> SequencePwm<'d, T> {
|
2021-11-11 02:43:47 +00:00
|
|
|
/// Creates the interface to a `SequencePwm`.
|
2021-11-01 16:37:34 +00:00
|
|
|
///
|
2021-11-02 03:18:24 +00:00
|
|
|
/// Must be started by calling `start`
|
2021-05-12 02:56:11 +00:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
2021-11-01 16:37:34 +00:00
|
|
|
/// The returned API is safe unless you use `mem::forget` (or similar safe
|
|
|
|
/// mechanisms) on stack allocated buffers which which have been passed to
|
2021-11-04 01:25:44 +00:00
|
|
|
/// [`new()`](SequencePwm::new).
|
2021-05-12 02:56:11 +00:00
|
|
|
#[allow(unused_unsafe)]
|
2021-11-12 02:14:07 +00:00
|
|
|
pub fn new(
|
2021-05-17 10:23:04 +00:00
|
|
|
_pwm: impl Unborrow<Target = T> + 'd,
|
2021-05-12 02:56:11 +00:00
|
|
|
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,
|
2021-11-02 02:11:37 +00:00
|
|
|
config: SequenceConfig,
|
2021-11-12 03:10:34 +00:00
|
|
|
sequence: &'d [u16],
|
2021-11-02 02:11:37 +00:00
|
|
|
) -> Result<Self, Error> {
|
2021-11-12 02:49:41 +00:00
|
|
|
slice_in_ram_or(sequence, Error::DMABufferNotInDataMemory)?;
|
2021-11-02 02:11:37 +00:00
|
|
|
|
2021-11-12 02:49:41 +00:00
|
|
|
if sequence.len() > 32767 {
|
2021-11-02 02:11:37 +00:00
|
|
|
return Err(Error::SequenceTooLong);
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:23:04 +00:00
|
|
|
unborrow!(ch0, ch1, ch2, ch3);
|
2021-05-12 02:56:11 +00:00
|
|
|
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
if let Some(pin) = ch0.pin_mut() {
|
2021-05-14 22:02:35 +00:00
|
|
|
pin.set_low();
|
2021-05-12 02:56:11 +00:00
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
|
|
|
if let Some(pin) = ch1.pin_mut() {
|
2021-05-14 22:02:35 +00:00
|
|
|
pin.set_low();
|
2021-05-12 02:56:11 +00:00
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
|
|
|
if let Some(pin) = ch2.pin_mut() {
|
2021-05-14 22:02:35 +00:00
|
|
|
pin.set_low();
|
2021-05-12 02:56:11 +00:00
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
|
|
|
if let Some(pin) = ch3.pin_mut() {
|
2021-05-14 22:02:35 +00:00
|
|
|
pin.set_low();
|
2021-05-12 02:56:11 +00:00
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
2021-11-01 15:54:07 +00:00
|
|
|
|
|
|
|
// if NoPin provided writes disconnected (top bit 1) 0x80000000 else
|
|
|
|
// writes pin number ex 13 (0x0D) which is connected (top bit 0)
|
2021-05-12 02:56:11 +00:00
|
|
|
r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) });
|
|
|
|
r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) });
|
|
|
|
r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) });
|
|
|
|
r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) });
|
|
|
|
|
|
|
|
// Disable all interrupts
|
|
|
|
r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) });
|
2021-11-01 16:37:34 +00:00
|
|
|
r.shorts.reset();
|
2021-05-12 02:56:11 +00:00
|
|
|
|
2021-10-26 07:37:52 +00:00
|
|
|
r.seq0
|
|
|
|
.ptr
|
2021-11-04 01:37:54 +00:00
|
|
|
.write(|w| unsafe { w.bits(sequence.as_ptr() as u32) });
|
2021-11-12 02:49:41 +00:00
|
|
|
r.seq0
|
|
|
|
.cnt
|
|
|
|
.write(|w| unsafe { w.bits(sequence.len() as u32) });
|
2021-10-30 23:16:10 +00:00
|
|
|
r.seq0.refresh.write(|w| unsafe { w.bits(config.refresh) });
|
2021-10-26 07:37:52 +00:00
|
|
|
r.seq0
|
|
|
|
.enddelay
|
2021-11-01 08:20:01 +00:00
|
|
|
.write(|w| unsafe { w.bits(config.end_delay) });
|
2021-10-26 07:37:52 +00:00
|
|
|
|
|
|
|
r.seq1
|
|
|
|
.ptr
|
2021-11-04 01:37:54 +00:00
|
|
|
.write(|w| unsafe { w.bits(sequence.as_ptr() as u32) });
|
2021-10-26 07:37:52 +00:00
|
|
|
r.seq1
|
|
|
|
.cnt
|
2021-11-04 01:37:54 +00:00
|
|
|
.write(|w| unsafe { w.bits(sequence.len() as u32) });
|
2021-10-30 23:16:10 +00:00
|
|
|
r.seq1.refresh.write(|w| unsafe { w.bits(config.refresh) });
|
2021-10-26 07:37:52 +00:00
|
|
|
r.seq1
|
|
|
|
.enddelay
|
2021-11-01 08:20:01 +00:00
|
|
|
.write(|w| unsafe { w.bits(config.end_delay) });
|
2021-10-26 07:37:52 +00:00
|
|
|
|
2021-11-04 01:15:42 +00:00
|
|
|
r.decoder.write(|w| {
|
|
|
|
w.load().bits(config.sequence_load as u8);
|
|
|
|
w.mode().refresh_count()
|
|
|
|
});
|
|
|
|
|
2021-11-11 01:53:21 +00:00
|
|
|
r.mode.write(|w| match config.counter_mode {
|
|
|
|
CounterMode::UpAndDown => w.updown().up_and_down(),
|
|
|
|
CounterMode::Up => w.updown().up(),
|
|
|
|
});
|
2021-11-04 01:15:42 +00:00
|
|
|
r.prescaler
|
|
|
|
.write(|w| w.prescaler().bits(config.prescaler as u8));
|
|
|
|
r.countertop
|
2021-11-12 05:47:57 +00:00
|
|
|
.write(|w| unsafe { w.countertop().bits(config.max_duty) });
|
2021-11-04 01:15:42 +00:00
|
|
|
|
2021-11-02 03:18:24 +00:00
|
|
|
Ok(Self {
|
|
|
|
phantom: PhantomData,
|
2021-11-10 18:02:43 +00:00
|
|
|
ch0: ch0.degrade_optional(),
|
|
|
|
ch1: ch1.degrade_optional(),
|
|
|
|
ch2: ch2.degrade_optional(),
|
|
|
|
ch3: ch3.degrade_optional(),
|
2021-11-02 03:18:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start or restart playback
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn start(&self, times: SequenceMode) -> Result<(), Error> {
|
|
|
|
if let SequenceMode::Times(0) = times {
|
2021-11-02 17:57:01 +00:00
|
|
|
return Err(Error::SequenceTimesAtLeastOne);
|
2021-11-02 03:18:24 +00:00
|
|
|
}
|
|
|
|
let r = T::regs();
|
|
|
|
|
2021-11-02 18:56:01 +00:00
|
|
|
self.stop();
|
2021-11-02 03:18:24 +00:00
|
|
|
|
|
|
|
r.enable.write(|w| w.enable().enabled());
|
|
|
|
|
2021-11-10 19:34:41 +00:00
|
|
|
// defensive before seqstart
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
2021-11-02 03:18:24 +00:00
|
|
|
match times {
|
2021-11-01 07:21:17 +00:00
|
|
|
// just the one time, no loop count
|
2021-11-01 15:54:07 +00:00
|
|
|
SequenceMode::Times(1) => {
|
2021-10-30 23:16:10 +00:00
|
|
|
r.loop_.write(|w| w.cnt().disabled());
|
2021-11-02 18:56:01 +00:00
|
|
|
// tasks_seqstart() doesn't exist in all svds so write its bit instead
|
2021-10-30 23:16:10 +00:00
|
|
|
r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) });
|
|
|
|
}
|
2021-11-01 07:21:17 +00:00
|
|
|
// loop count is how many times to play BOTH sequences
|
2021-11-01 15:45:07 +00:00
|
|
|
// 2 total (1 x 2)
|
|
|
|
// 3 total, (2 x 2) - 1
|
2021-11-01 15:54:07 +00:00
|
|
|
SequenceMode::Times(n) => {
|
2021-11-01 15:45:07 +00:00
|
|
|
let odd = n & 1 == 1;
|
|
|
|
let times = if odd { (n / 2) + 1 } else { n / 2 };
|
|
|
|
|
2021-10-30 23:16:10 +00:00
|
|
|
r.loop_.write(|w| unsafe { w.cnt().bits(times) });
|
|
|
|
|
2021-11-01 20:16:24 +00:00
|
|
|
// we can subtract 1 by starting at seq1 instead of seq0
|
2021-11-01 15:45:07 +00:00
|
|
|
if odd {
|
2021-11-02 18:56:01 +00:00
|
|
|
// tasks_seqstart() doesn't exist in all svds so write its bit instead
|
2021-11-01 15:45:07 +00:00
|
|
|
r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) });
|
2021-10-30 23:16:10 +00:00
|
|
|
} else {
|
2021-11-02 18:56:01 +00:00
|
|
|
// tasks_seqstart() doesn't exist in all svds so write its bit instead
|
2021-11-01 15:45:07 +00:00
|
|
|
r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) });
|
2021-10-30 23:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 07:30:42 +00:00
|
|
|
// to play infinitely, repeat the sequence one time, then have loops done self trigger seq0 again
|
2021-11-01 15:54:07 +00:00
|
|
|
SequenceMode::Infinite => {
|
2021-10-30 23:16:10 +00:00
|
|
|
r.loop_.write(|w| unsafe { w.cnt().bits(0x1) });
|
2021-11-01 07:30:42 +00:00
|
|
|
r.shorts.write(|w| w.loopsdone_seqstart0().enabled());
|
2021-11-10 19:34:41 +00:00
|
|
|
|
2021-11-02 18:56:01 +00:00
|
|
|
// tasks_seqstart() doesn't exist in all svds so write its bit instead
|
2021-11-01 07:30:42 +00:00
|
|
|
r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) });
|
2021-10-30 23:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-02 02:11:37 +00:00
|
|
|
|
2021-11-02 03:18:24 +00:00
|
|
|
Ok(())
|
2021-11-02 02:11:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 02:56:36 +00:00
|
|
|
/// Stop playback.
|
2021-11-02 02:11:37 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn stop(&self) {
|
|
|
|
let r = T::regs();
|
|
|
|
|
2021-11-12 06:46:27 +00:00
|
|
|
r.enable.write(|w| w.enable().disabled());
|
|
|
|
|
2021-11-02 02:11:37 +00:00
|
|
|
r.shorts.reset();
|
|
|
|
|
2021-11-10 19:34:41 +00:00
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
2021-11-02 18:56:01 +00:00
|
|
|
// tasks_stop() doesn't exist in all svds so write its bit instead
|
2021-11-02 02:11:37 +00:00
|
|
|
r.tasks_stop.write(|w| unsafe { w.bits(0x01) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 02:49:41 +00:00
|
|
|
impl<'a, T: Instance> Drop for SequencePwm<'a, T> {
|
2021-11-02 02:11:37 +00:00
|
|
|
fn drop(&mut self) {
|
2021-11-11 00:19:03 +00:00
|
|
|
let r = T::regs();
|
|
|
|
|
2021-11-02 02:11:37 +00:00
|
|
|
self.stop();
|
|
|
|
|
2021-11-10 18:02:43 +00:00
|
|
|
if let Some(pin) = &self.ch0 {
|
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w);
|
2021-11-11 00:19:03 +00:00
|
|
|
r.psel.out[0].write(|w| unsafe { w.bits(0x80000000) });
|
2021-11-10 18:02:43 +00:00
|
|
|
}
|
|
|
|
if let Some(pin) = &self.ch1 {
|
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w);
|
2021-11-11 00:19:03 +00:00
|
|
|
r.psel.out[1].write(|w| unsafe { w.bits(0x80000000) });
|
2021-11-10 18:02:43 +00:00
|
|
|
}
|
|
|
|
if let Some(pin) = &self.ch2 {
|
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w);
|
2021-11-11 00:19:03 +00:00
|
|
|
r.psel.out[2].write(|w| unsafe { w.bits(0x80000000) });
|
2021-11-10 18:02:43 +00:00
|
|
|
}
|
|
|
|
if let Some(pin) = &self.ch3 {
|
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w);
|
2021-11-11 00:19:03 +00:00
|
|
|
r.psel.out[3].write(|w| unsafe { w.bits(0x80000000) });
|
2021-11-10 18:02:43 +00:00
|
|
|
}
|
2021-11-02 02:11:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 00:30:59 +00:00
|
|
|
/// Configure an infinite looping sequence for `SequencePwm`
|
2021-11-04 01:37:54 +00:00
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct SequenceConfig {
|
2021-11-02 02:11:37 +00:00
|
|
|
/// Selects up mode or up-and-down mode for the counter
|
|
|
|
pub counter_mode: CounterMode,
|
|
|
|
/// Top value to be compared against buffer values
|
2021-11-12 05:47:57 +00:00
|
|
|
pub max_duty: u16,
|
2021-11-02 02:11:37 +00:00
|
|
|
/// Configuration for PWM_CLK
|
|
|
|
pub prescaler: Prescaler,
|
|
|
|
/// How a sequence is read from RAM and is spread to the compare register
|
|
|
|
pub sequence_load: SequenceLoad,
|
2021-11-11 01:52:29 +00:00
|
|
|
/// Number of PWM periods to delay between each sequence sample
|
2021-11-02 02:11:37 +00:00
|
|
|
pub refresh: u32,
|
2021-11-11 01:52:29 +00:00
|
|
|
/// Number of PWM periods after the sequence ends before starting the next sequence
|
2021-11-02 02:11:37 +00:00
|
|
|
pub end_delay: u32,
|
|
|
|
}
|
|
|
|
|
2021-11-04 01:37:54 +00:00
|
|
|
impl Default for SequenceConfig {
|
|
|
|
fn default() -> SequenceConfig {
|
|
|
|
SequenceConfig {
|
|
|
|
counter_mode: CounterMode::Up,
|
2021-11-12 05:47:57 +00:00
|
|
|
max_duty: 1000,
|
2021-11-04 01:37:54 +00:00
|
|
|
prescaler: Prescaler::Div16,
|
|
|
|
sequence_load: SequenceLoad::Common,
|
|
|
|
refresh: 0,
|
|
|
|
end_delay: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 00:30:59 +00:00
|
|
|
/// How many times to run the sequence
|
2021-11-04 01:37:54 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
|
|
|
pub enum SequenceMode {
|
|
|
|
/// Run sequence n Times total
|
|
|
|
Times(u16),
|
|
|
|
/// Repeat until `stop` is called.
|
|
|
|
Infinite,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PWM Base clock is system clock (16MHz) divided by prescaler
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
|
|
|
pub enum Prescaler {
|
|
|
|
Div1,
|
|
|
|
Div2,
|
|
|
|
Div4,
|
|
|
|
Div8,
|
|
|
|
Div16,
|
|
|
|
Div32,
|
|
|
|
Div64,
|
|
|
|
Div128,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// How the sequence values are distributed across the channels
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
|
|
|
pub enum SequenceLoad {
|
|
|
|
/// Provided sequence will be used across all channels
|
|
|
|
Common,
|
|
|
|
/// Provided sequence contains grouped values for each channel ex:
|
|
|
|
/// [ch0_0_and_ch1_0, ch2_0_and_ch3_0, ... ch0_n_and_ch1_n, ch2_n_and_ch3_n]
|
|
|
|
Grouped,
|
|
|
|
/// Provided sequence contains individual values for each channel ex:
|
|
|
|
/// [ch0_0, ch1_0, ch2_0, ch3_0... ch0_n, ch1_n, ch2_n, ch3_n]
|
|
|
|
Individual,
|
|
|
|
/// Similar to Individual mode, but only three channels are used. The fourth
|
|
|
|
/// value is loaded into the pulse generator counter as its top value.
|
|
|
|
Waveform,
|
|
|
|
}
|
|
|
|
|
2021-11-11 00:30:59 +00:00
|
|
|
/// Selects up mode or up-and-down mode for the counter
|
2021-11-04 01:37:54 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
|
|
|
pub enum CounterMode {
|
|
|
|
/// Up counter (edge-aligned PWM duty cycle)
|
|
|
|
Up,
|
|
|
|
/// Up and down counter (center-aligned PWM duty cycle)
|
|
|
|
UpAndDown,
|
2021-11-02 02:11:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 01:25:44 +00:00
|
|
|
impl<'d, T: Instance> SimplePwm<'d, T> {
|
2021-11-11 02:43:47 +00:00
|
|
|
/// Creates the interface to a `SimplePwm`
|
2021-11-02 02:11:37 +00:00
|
|
|
///
|
2021-11-11 01:52:29 +00:00
|
|
|
/// Defaults the freq to 1Mhz, max_duty 1000, duty 0, up mode, and pins low.
|
2021-11-02 02:11:37 +00:00
|
|
|
/// Must be started by calling `set_duty`
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The returned API is safe unless you use `mem::forget` (or similar safe
|
|
|
|
/// mechanisms) on stack allocated buffers which which have been passed to
|
2021-11-04 01:25:44 +00:00
|
|
|
/// [`new()`](SimplePwm::new).
|
2021-11-02 02:11:37 +00:00
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
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,
|
|
|
|
) -> Self {
|
|
|
|
unborrow!(ch0, ch1, ch2, ch3);
|
|
|
|
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
if let Some(pin) = ch0.pin_mut() {
|
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
|
|
|
if let Some(pin) = ch1.pin_mut() {
|
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
|
|
|
if let Some(pin) = ch2.pin_mut() {
|
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
|
|
|
if let Some(pin) = ch3.pin_mut() {
|
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
|
|
|
|
|
|
|
// if NoPin provided writes disconnected (top bit 1) 0x80000000 else
|
|
|
|
// writes pin number ex 13 (0x0D) which is connected (top bit 0)
|
|
|
|
r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) });
|
|
|
|
r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) });
|
|
|
|
r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) });
|
|
|
|
r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) });
|
|
|
|
|
2021-11-10 19:32:28 +00:00
|
|
|
let pwm = Self {
|
|
|
|
phantom: PhantomData,
|
|
|
|
ch0: ch0.degrade_optional(),
|
|
|
|
ch1: ch1.degrade_optional(),
|
|
|
|
ch2: ch2.degrade_optional(),
|
|
|
|
ch3: ch3.degrade_optional(),
|
|
|
|
duty: [0; 4],
|
|
|
|
};
|
|
|
|
|
2021-11-02 02:11:37 +00:00
|
|
|
// Disable all interrupts
|
|
|
|
r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) });
|
|
|
|
r.shorts.reset();
|
|
|
|
|
|
|
|
// Enable
|
|
|
|
r.enable.write(|w| w.enable().enabled());
|
|
|
|
|
|
|
|
r.seq0
|
|
|
|
.ptr
|
2021-11-12 05:50:07 +00:00
|
|
|
.write(|w| unsafe { w.bits((&pwm.duty).as_ptr() as u32) });
|
2021-11-10 19:32:28 +00:00
|
|
|
|
2021-11-02 02:11:37 +00:00
|
|
|
r.seq0.cnt.write(|w| unsafe { w.bits(4) });
|
|
|
|
r.seq0.refresh.write(|w| unsafe { w.bits(0) });
|
|
|
|
r.seq0.enddelay.write(|w| unsafe { w.bits(0) });
|
|
|
|
|
|
|
|
r.decoder.write(|w| {
|
|
|
|
w.load().individual();
|
|
|
|
w.mode().refresh_count()
|
|
|
|
});
|
|
|
|
r.mode.write(|w| w.updown().up());
|
|
|
|
r.prescaler.write(|w| w.prescaler().div_16());
|
|
|
|
r.countertop.write(|w| unsafe { w.countertop().bits(1000) });
|
|
|
|
r.loop_.write(|w| w.cnt().disabled());
|
|
|
|
|
2021-11-10 19:32:28 +00:00
|
|
|
pwm
|
2021-10-26 07:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop playback
|
|
|
|
#[inline(always)]
|
2021-11-02 02:11:37 +00:00
|
|
|
pub fn stop(&self) {
|
2021-10-26 07:37:52 +00:00
|
|
|
let r = T::regs();
|
|
|
|
|
2021-11-01 16:37:34 +00:00
|
|
|
r.shorts.reset();
|
2021-10-26 07:37:52 +00:00
|
|
|
|
2021-11-10 19:34:41 +00:00
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
2021-11-02 18:56:01 +00:00
|
|
|
// tasks_stop() doesn't exist in all svds so write its bit instead
|
2021-10-26 07:37:52 +00:00
|
|
|
r.tasks_stop.write(|w| unsafe { w.bits(0x01) });
|
|
|
|
}
|
|
|
|
|
2021-05-14 22:02:50 +00:00
|
|
|
/// Enables the PWM generator.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn enable(&self) {
|
|
|
|
let r = T::regs();
|
|
|
|
r.enable.write(|w| w.enable().enabled());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Disables the PWM generator.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn disable(&self) {
|
|
|
|
let r = T::regs();
|
|
|
|
r.enable.write(|w| w.enable().disabled());
|
|
|
|
}
|
|
|
|
|
2021-05-12 02:56:11 +00:00
|
|
|
/// Sets duty cycle (15 bit) for a PWM channel.
|
2021-11-10 19:32:28 +00:00
|
|
|
pub fn set_duty(&mut self, channel: usize, duty: u16) {
|
2021-11-02 18:56:01 +00:00
|
|
|
let r = T::regs();
|
2021-11-10 19:32:28 +00:00
|
|
|
|
|
|
|
self.duty[channel] = duty & 0x7FFF;
|
|
|
|
|
2021-11-10 19:34:41 +00:00
|
|
|
// defensive before seqstart
|
2021-05-12 02:56:11 +00:00
|
|
|
compiler_fence(Ordering::SeqCst);
|
2021-11-01 20:00:50 +00:00
|
|
|
|
2021-11-02 18:56:01 +00:00
|
|
|
// tasks_seqstart() doesn't exist in all svds so write its bit instead
|
2021-11-01 20:00:50 +00:00
|
|
|
r.tasks_seqstart[0].write(|w| unsafe { w.bits(1) });
|
2021-11-10 19:34:41 +00:00
|
|
|
|
2021-11-12 05:50:07 +00:00
|
|
|
// defensive wait until waveform is loaded after seqstart so set_duty
|
|
|
|
// can't be called again while dma is still reading
|
2021-11-10 20:01:39 +00:00
|
|
|
while r.events_seqend[0].read().bits() == 0 {}
|
2021-11-10 19:34:41 +00:00
|
|
|
r.events_seqend[0].write(|w| w);
|
2021-05-12 02:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the PWM clock prescaler.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn set_prescaler(&self, div: Prescaler) {
|
|
|
|
T::regs().prescaler.write(|w| w.prescaler().bits(div as u8));
|
|
|
|
}
|
|
|
|
|
2021-10-26 07:37:52 +00:00
|
|
|
/// Gets the PWM clock prescaler.
|
2021-05-12 02:56:11 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn prescaler(&self) -> Prescaler {
|
|
|
|
match T::regs().prescaler.read().prescaler().bits() {
|
|
|
|
0 => Prescaler::Div1,
|
|
|
|
1 => Prescaler::Div2,
|
|
|
|
2 => Prescaler::Div4,
|
|
|
|
3 => Prescaler::Div8,
|
|
|
|
4 => Prescaler::Div16,
|
|
|
|
5 => Prescaler::Div32,
|
|
|
|
6 => Prescaler::Div64,
|
|
|
|
7 => Prescaler::Div128,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the maximum duty cycle value.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn set_max_duty(&self, duty: u16) {
|
|
|
|
T::regs()
|
|
|
|
.countertop
|
|
|
|
.write(|w| unsafe { w.countertop().bits(duty.min(32767u16)) });
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the maximum duty cycle value.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn max_duty(&self) -> u16 {
|
|
|
|
T::regs().countertop.read().countertop().bits()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the PWM output frequency.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn set_period(&self, freq: u32) {
|
|
|
|
let clk = 16_000_000u32 >> (self.prescaler() as u8);
|
|
|
|
let duty = clk / freq;
|
|
|
|
self.set_max_duty(duty.min(32767) as u16);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the PWM output frequency.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn period(&self) -> u32 {
|
|
|
|
let clk = 16_000_000u32 >> (self.prescaler() as u8);
|
|
|
|
let max_duty = self.max_duty() as u32;
|
|
|
|
clk / max_duty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 01:25:44 +00:00
|
|
|
impl<'a, T: Instance> Drop for SimplePwm<'a, T> {
|
2021-05-12 02:56:11 +00:00
|
|
|
fn drop(&mut self) {
|
2021-11-11 00:19:03 +00:00
|
|
|
let r = T::regs();
|
|
|
|
|
2021-11-02 02:11:37 +00:00
|
|
|
self.stop();
|
2021-10-30 23:16:10 +00:00
|
|
|
self.disable();
|
2021-05-12 02:56:11 +00:00
|
|
|
|
2021-11-10 18:02:43 +00:00
|
|
|
if let Some(pin) = &self.ch0 {
|
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w);
|
2021-11-11 00:19:03 +00:00
|
|
|
r.psel.out[0].write(|w| unsafe { w.bits(0x80000000) });
|
2021-11-10 18:02:43 +00:00
|
|
|
}
|
|
|
|
if let Some(pin) = &self.ch1 {
|
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w);
|
2021-11-11 00:19:03 +00:00
|
|
|
r.psel.out[1].write(|w| unsafe { w.bits(0x80000000) });
|
2021-11-10 18:02:43 +00:00
|
|
|
}
|
|
|
|
if let Some(pin) = &self.ch2 {
|
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w);
|
2021-11-11 00:19:03 +00:00
|
|
|
r.psel.out[2].write(|w| unsafe { w.bits(0x80000000) });
|
2021-11-10 18:02:43 +00:00
|
|
|
}
|
|
|
|
if let Some(pin) = &self.ch3 {
|
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w);
|
2021-11-11 00:19:03 +00:00
|
|
|
r.psel.out[3].write(|w| unsafe { w.bits(0x80000000) });
|
2021-11-10 18:02:43 +00:00
|
|
|
}
|
2021-05-12 02:56:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub trait Instance {
|
|
|
|
fn regs() -> &'static pac::pwm0::RegisterBlock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 22:05:32 +00:00
|
|
|
pub trait Instance: Unborrow<Target = Self> + sealed::Instance + 'static {
|
2021-05-12 02:56:11 +00:00
|
|
|
type Interrupt: Interrupt;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_pwm {
|
|
|
|
($type:ident, $pac_type:ident, $irq:ident) => {
|
|
|
|
impl crate::pwm::sealed::Instance for peripherals::$type {
|
|
|
|
fn regs() -> &'static pac::pwm0::RegisterBlock {
|
|
|
|
unsafe { &*pac::$pac_type::ptr() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl crate::pwm::Instance for peripherals::$type {
|
|
|
|
type Interrupt = crate::interrupt::$irq;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|