2023-02-01 00:48:33 +01:00
|
|
|
//! Pulse Width Modulation (PWM) driver.
|
|
|
|
|
2021-05-12 04:56:11 +02:00
|
|
|
#![macro_use]
|
|
|
|
|
|
|
|
use core::sync::atomic::{compiler_fence, Ordering};
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2022-07-23 14:27:45 +02:00
|
|
|
use embassy_hal_common::{into_ref, PeripheralRef};
|
2021-05-12 04:56:11 +02:00
|
|
|
|
|
|
|
use crate::gpio::sealed::Pin as _;
|
2022-02-12 01:04:01 +01:00
|
|
|
use crate::gpio::{AnyPin, Pin as GpioPin, PselBits};
|
2021-11-13 23:03:10 +01:00
|
|
|
use crate::ppi::{Event, Task};
|
2021-10-26 09:37:52 +02:00
|
|
|
use crate::util::slice_in_ram_or;
|
2023-06-08 16:08:40 +02:00
|
|
|
use crate::{interrupt, pac, Peripheral};
|
2021-05-12 04:56:11 +02:00
|
|
|
|
2021-11-11 03:43:47 +01: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 02:25:44 +01:00
|
|
|
pub struct SimplePwm<'d, T: Instance> {
|
2022-07-23 15:13:47 +02:00
|
|
|
_peri: PeripheralRef<'d, T>,
|
2021-11-10 20:32:28 +01:00
|
|
|
duty: [u16; 4],
|
2022-07-23 14:00:19 +02:00
|
|
|
ch0: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
ch1: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
ch2: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
ch3: Option<PeripheralRef<'d, AnyPin>>,
|
2021-05-12 04:56:11 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 03:44:57 +01: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.
|
2022-01-30 06:21:23 +01:00
|
|
|
pub struct SequencePwm<'d, T: Instance> {
|
2022-07-23 15:13:47 +02:00
|
|
|
_peri: PeripheralRef<'d, T>,
|
2022-07-23 14:00:19 +02:00
|
|
|
ch0: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
ch1: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
ch2: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
ch3: Option<PeripheralRef<'d, AnyPin>>,
|
2021-10-26 09:37:52 +02:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// PWM error
|
2021-11-04 02:37:54 +01: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.
|
2023-02-01 00:48:33 +01:00
|
|
|
BufferNotInRAM,
|
2021-11-04 02:37:54 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 08:06:42 +01:00
|
|
|
const MAX_SEQUENCE_LEN: usize = 32767;
|
|
|
|
|
2022-01-30 06:21:23 +01:00
|
|
|
impl<'d, T: Instance> SequencePwm<'d, T> {
|
2022-02-12 01:04:01 +01:00
|
|
|
/// Create a new 1-channel PWM
|
2021-05-12 04:56:11 +02:00
|
|
|
#[allow(unused_unsafe)]
|
2022-02-12 01:04:01 +01:00
|
|
|
pub fn new_1ch(
|
2022-07-23 14:00:19 +02:00
|
|
|
pwm: impl Peripheral<P = T> + 'd,
|
|
|
|
ch0: impl Peripheral<P = impl GpioPin> + 'd,
|
2022-02-12 01:04:01 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Result<Self, Error> {
|
2022-07-23 14:27:45 +02:00
|
|
|
into_ref!(ch0);
|
|
|
|
Self::new_inner(pwm, Some(ch0.map_into()), None, None, None, config)
|
2022-02-12 01:04:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new 2-channel PWM
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
pub fn new_2ch(
|
2022-07-23 14:00:19 +02:00
|
|
|
pwm: impl Peripheral<P = T> + 'd,
|
|
|
|
ch0: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
ch1: impl Peripheral<P = impl GpioPin> + 'd,
|
2022-02-12 01:04:01 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Result<Self, Error> {
|
2022-07-23 14:27:45 +02:00
|
|
|
into_ref!(ch0, ch1);
|
|
|
|
Self::new_inner(pwm, Some(ch0.map_into()), Some(ch1.map_into()), None, None, config)
|
2022-02-12 01:04:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new 3-channel PWM
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
pub fn new_3ch(
|
2022-07-23 14:00:19 +02:00
|
|
|
pwm: impl Peripheral<P = T> + 'd,
|
|
|
|
ch0: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
ch1: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
ch2: impl Peripheral<P = impl GpioPin> + 'd,
|
2022-02-12 01:04:01 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Result<Self, Error> {
|
2022-07-23 14:27:45 +02:00
|
|
|
into_ref!(ch0, ch1, ch2);
|
|
|
|
Self::new_inner(
|
|
|
|
pwm,
|
|
|
|
Some(ch0.map_into()),
|
|
|
|
Some(ch1.map_into()),
|
|
|
|
Some(ch2.map_into()),
|
|
|
|
None,
|
|
|
|
config,
|
|
|
|
)
|
2022-02-12 01:04:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new 4-channel PWM
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
pub fn new_4ch(
|
2022-07-23 14:00:19 +02:00
|
|
|
pwm: impl Peripheral<P = T> + 'd,
|
|
|
|
ch0: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
ch1: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
ch2: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
ch3: impl Peripheral<P = impl GpioPin> + 'd,
|
2022-01-25 08:06:42 +01:00
|
|
|
config: Config,
|
2021-11-02 03:11:37 +01:00
|
|
|
) -> Result<Self, Error> {
|
2022-07-23 14:27:45 +02:00
|
|
|
into_ref!(ch0, ch1, ch2, ch3);
|
|
|
|
Self::new_inner(
|
|
|
|
pwm,
|
|
|
|
Some(ch0.map_into()),
|
|
|
|
Some(ch1.map_into()),
|
|
|
|
Some(ch2.map_into()),
|
|
|
|
Some(ch3.map_into()),
|
|
|
|
config,
|
|
|
|
)
|
2022-02-12 01:04:01 +01:00
|
|
|
}
|
2021-05-12 04:56:11 +02:00
|
|
|
|
2022-02-12 01:04:01 +01:00
|
|
|
fn new_inner(
|
2022-07-23 14:00:19 +02:00
|
|
|
_pwm: impl Peripheral<P = T> + 'd,
|
|
|
|
ch0: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
ch1: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
ch2: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
ch3: Option<PeripheralRef<'d, AnyPin>>,
|
2022-02-12 01:04:01 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Result<Self, Error> {
|
2022-07-23 15:13:47 +02:00
|
|
|
into_ref!(_pwm);
|
|
|
|
|
2021-05-12 04:56:11 +02:00
|
|
|
let r = T::regs();
|
|
|
|
|
2022-02-12 01:04:01 +01:00
|
|
|
if let Some(pin) = &ch0 {
|
2021-05-15 00:02:35 +02:00
|
|
|
pin.set_low();
|
2021-05-12 04:56:11 +02:00
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
2022-02-12 01:04:01 +01:00
|
|
|
if let Some(pin) = &ch1 {
|
2021-05-15 00:02:35 +02:00
|
|
|
pin.set_low();
|
2021-05-12 04:56:11 +02:00
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
2022-02-12 01:04:01 +01:00
|
|
|
if let Some(pin) = &ch2 {
|
2021-05-15 00:02:35 +02:00
|
|
|
pin.set_low();
|
2021-05-12 04:56:11 +02:00
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
2022-02-12 01:04:01 +01:00
|
|
|
if let Some(pin) = &ch3 {
|
2021-05-15 00:02:35 +02:00
|
|
|
pin.set_low();
|
2021-05-12 04:56:11 +02:00
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
2021-11-01 16:54:07 +01:00
|
|
|
|
2021-05-12 04:56:11 +02: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 17:37:34 +01:00
|
|
|
r.shorts.reset();
|
2021-11-13 23:03:10 +01:00
|
|
|
r.events_stopped.reset();
|
|
|
|
r.events_loopsdone.reset();
|
|
|
|
r.events_seqend[0].reset();
|
|
|
|
r.events_seqend[1].reset();
|
|
|
|
r.events_pwmperiodend.reset();
|
|
|
|
r.events_seqstarted[0].reset();
|
|
|
|
r.events_seqstarted[1].reset();
|
2021-05-12 04:56:11 +02:00
|
|
|
|
2021-11-04 02:15:42 +01:00
|
|
|
r.decoder.write(|w| {
|
|
|
|
w.load().bits(config.sequence_load as u8);
|
|
|
|
w.mode().refresh_count()
|
|
|
|
});
|
|
|
|
|
2021-11-11 02:53:21 +01:00
|
|
|
r.mode.write(|w| match config.counter_mode {
|
|
|
|
CounterMode::UpAndDown => w.updown().up_and_down(),
|
|
|
|
CounterMode::Up => w.updown().up(),
|
|
|
|
});
|
2022-06-12 22:15:44 +02:00
|
|
|
r.prescaler.write(|w| w.prescaler().bits(config.prescaler as u8));
|
|
|
|
r.countertop.write(|w| unsafe { w.countertop().bits(config.max_duty) });
|
2021-11-04 02:15:42 +01:00
|
|
|
|
2021-11-02 04:18:24 +01:00
|
|
|
Ok(Self {
|
2022-07-23 15:13:47 +02:00
|
|
|
_peri: _pwm,
|
2022-02-12 01:04:01 +01:00
|
|
|
ch0,
|
|
|
|
ch1,
|
|
|
|
ch2,
|
|
|
|
ch3,
|
2021-11-02 04:18:24 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-13 23:03:10 +01:00
|
|
|
/// Returns reference to `Stopped` event endpoint for PPI.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn event_stopped(&self) -> Event {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
Event::from_reg(&r.events_stopped)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns reference to `LoopsDone` event endpoint for PPI.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn event_loops_done(&self) -> Event {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
Event::from_reg(&r.events_loopsdone)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns reference to `PwmPeriodEnd` event endpoint for PPI.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn event_pwm_period_end(&self) -> Event {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
Event::from_reg(&r.events_pwmperiodend)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns reference to `Seq0 End` event endpoint for PPI.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn event_seq_end(&self) -> Event {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
Event::from_reg(&r.events_seqend[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns reference to `Seq1 End` event endpoint for PPI.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn event_seq1_end(&self) -> Event {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
Event::from_reg(&r.events_seqend[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns reference to `Seq0 Started` event endpoint for PPI.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn event_seq0_started(&self) -> Event {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
Event::from_reg(&r.events_seqstarted[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns reference to `Seq1 Started` event endpoint for PPI.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn event_seq1_started(&self) -> Event {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
Event::from_reg(&r.events_seqstarted[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns reference to `Seq0 Start` task endpoint for PPI.
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Interacting with the sequence while it runs puts it in an unknown state
|
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn task_start_seq0(&self) -> Task {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
Task::from_reg(&r.tasks_seqstart[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns reference to `Seq1 Started` task endpoint for PPI.
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Interacting with the sequence while it runs puts it in an unknown state
|
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn task_start_seq1(&self) -> Task {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
Task::from_reg(&r.tasks_seqstart[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns reference to `NextStep` task endpoint for PPI.
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Interacting with the sequence while it runs puts it in an unknown state
|
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn task_next_step(&self) -> Task {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
Task::from_reg(&r.tasks_nextstep)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns reference to `Stop` task endpoint for PPI.
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Interacting with the sequence while it runs puts it in an unknown state
|
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn task_stop(&self) -> Task {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
Task::from_reg(&r.tasks_stop)
|
|
|
|
}
|
2021-11-02 03:11:37 +01:00
|
|
|
}
|
|
|
|
|
2022-01-30 06:21:23 +01:00
|
|
|
impl<'a, T: Instance> Drop for SequencePwm<'a, T> {
|
2021-11-02 03:11:37 +01:00
|
|
|
fn drop(&mut self) {
|
2021-11-11 01:19:03 +01:00
|
|
|
let r = T::regs();
|
|
|
|
|
2021-11-10 19:02:43 +01:00
|
|
|
if let Some(pin) = &self.ch0 {
|
|
|
|
pin.set_low();
|
2021-11-14 00:41:44 +01:00
|
|
|
pin.conf().reset();
|
|
|
|
r.psel.out[0].reset();
|
2021-11-10 19:02:43 +01:00
|
|
|
}
|
|
|
|
if let Some(pin) = &self.ch1 {
|
|
|
|
pin.set_low();
|
2021-11-14 00:41:44 +01:00
|
|
|
pin.conf().reset();
|
|
|
|
r.psel.out[1].reset();
|
2021-11-10 19:02:43 +01:00
|
|
|
}
|
|
|
|
if let Some(pin) = &self.ch2 {
|
|
|
|
pin.set_low();
|
2021-11-14 00:41:44 +01:00
|
|
|
pin.conf().reset();
|
|
|
|
r.psel.out[2].reset();
|
2021-11-10 19:02:43 +01:00
|
|
|
}
|
|
|
|
if let Some(pin) = &self.ch3 {
|
|
|
|
pin.set_low();
|
2021-11-14 00:41:44 +01:00
|
|
|
pin.conf().reset();
|
|
|
|
r.psel.out[3].reset();
|
2021-11-10 19:02:43 +01:00
|
|
|
}
|
2021-11-02 03:11:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:26:09 +01:00
|
|
|
/// Configuration for the PWM as a whole.
|
2021-11-04 02:37:54 +01:00
|
|
|
#[non_exhaustive]
|
2022-01-25 08:06:42 +01:00
|
|
|
pub struct Config {
|
2021-11-02 03:11:37 +01: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 06:47:57 +01:00
|
|
|
pub max_duty: u16,
|
2021-11-02 03:11:37 +01: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,
|
2022-01-25 08:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
|
|
|
counter_mode: CounterMode::Up,
|
|
|
|
max_duty: 1000,
|
|
|
|
prescaler: Prescaler::Div16,
|
|
|
|
sequence_load: SequenceLoad::Common,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:26:09 +01:00
|
|
|
/// Configuration per sequence
|
2022-01-25 08:06:42 +01:00
|
|
|
#[non_exhaustive]
|
2022-01-28 03:38:20 +01:00
|
|
|
#[derive(Clone)]
|
2022-01-25 08:06:42 +01:00
|
|
|
pub struct SequenceConfig {
|
2021-11-11 02:52:29 +01:00
|
|
|
/// Number of PWM periods to delay between each sequence sample
|
2021-11-02 03:11:37 +01:00
|
|
|
pub refresh: u32,
|
2021-11-11 02:52:29 +01:00
|
|
|
/// Number of PWM periods after the sequence ends before starting the next sequence
|
2021-11-02 03:11:37 +01:00
|
|
|
pub end_delay: u32,
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:37:54 +01:00
|
|
|
impl Default for SequenceConfig {
|
|
|
|
fn default() -> SequenceConfig {
|
|
|
|
SequenceConfig {
|
|
|
|
refresh: 0,
|
|
|
|
end_delay: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:26:09 +01:00
|
|
|
/// A composition of a sequence buffer and its configuration.
|
2022-01-28 03:38:20 +01:00
|
|
|
#[non_exhaustive]
|
2022-02-04 03:04:55 +01:00
|
|
|
pub struct Sequence<'s> {
|
2022-01-28 03:38:20 +01:00
|
|
|
/// The words comprising the sequence. Must not exceed 32767 words.
|
2022-02-04 03:04:55 +01:00
|
|
|
pub words: &'s [u16],
|
2022-01-28 03:38:20 +01:00
|
|
|
/// Configuration associated with the sequence.
|
|
|
|
pub config: SequenceConfig,
|
|
|
|
}
|
|
|
|
|
2022-02-04 03:04:55 +01:00
|
|
|
impl<'s> Sequence<'s> {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Create a new `Sequence`
|
2022-02-04 03:04:55 +01:00
|
|
|
pub fn new(words: &'s [u16], config: SequenceConfig) -> Self {
|
2022-01-30 06:21:23 +01:00
|
|
|
Self { words, config }
|
2022-01-28 03:38:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:11:15 +01:00
|
|
|
/// A single sequence that can be started and stopped.
|
|
|
|
/// Takes at one sequence along with its configuration.
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct SingleSequencer<'d, 's, T: Instance> {
|
2023-02-01 00:48:33 +01:00
|
|
|
sequencer: Sequencer<'d, 's, T>,
|
2022-02-04 09:11:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, 's, T: Instance> SingleSequencer<'d, 's, T> {
|
|
|
|
/// Create a new sequencer
|
2022-02-04 22:05:23 +01:00
|
|
|
pub fn new(pwm: &'s mut SequencePwm<'d, T>, words: &'s [u16], config: SequenceConfig) -> Self {
|
2022-02-04 09:11:15 +01:00
|
|
|
Self {
|
2022-02-04 22:05:23 +01:00
|
|
|
sequencer: Sequencer::new(pwm, Sequence::new(words, config), None),
|
2022-02-04 09:11:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start or restart playback.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn start(&self, times: SingleSequenceMode) -> Result<(), Error> {
|
|
|
|
let (start_seq, times) = match times {
|
|
|
|
SingleSequenceMode::Times(n) if n == 1 => (StartSequence::One, SequenceMode::Loop(1)),
|
2022-06-12 22:15:44 +02:00
|
|
|
SingleSequenceMode::Times(n) if n & 1 == 1 => (StartSequence::One, SequenceMode::Loop((n / 2) + 1)),
|
2022-02-04 09:11:15 +01:00
|
|
|
SingleSequenceMode::Times(n) => (StartSequence::Zero, SequenceMode::Loop(n / 2)),
|
|
|
|
SingleSequenceMode::Infinite => (StartSequence::Zero, SequenceMode::Infinite),
|
|
|
|
};
|
|
|
|
self.sequencer.start(start_seq, times)
|
|
|
|
}
|
2022-02-04 09:14:24 +01:00
|
|
|
|
|
|
|
/// Stop playback. Disables the peripheral. Does NOT clear the last duty
|
|
|
|
/// cycle from the pin. Returns any sequences previously provided to
|
|
|
|
/// `start` so that they may be further mutated.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn stop(&self) {
|
|
|
|
self.sequencer.stop();
|
|
|
|
}
|
2022-02-04 09:11:15 +01:00
|
|
|
}
|
|
|
|
|
2022-02-04 03:04:55 +01:00
|
|
|
/// A composition of sequences that can be started and stopped.
|
|
|
|
/// Takes at least one sequence along with its configuration.
|
|
|
|
/// Optionally takes a second sequence and its configuration.
|
|
|
|
/// In the case where no second sequence is provided then the first sequence
|
|
|
|
/// is used.
|
|
|
|
#[non_exhaustive]
|
2022-02-04 09:11:15 +01:00
|
|
|
pub struct Sequencer<'d, 's, T: Instance> {
|
|
|
|
_pwm: &'s mut SequencePwm<'d, T>,
|
2022-02-04 03:04:55 +01:00
|
|
|
sequence0: Sequence<'s>,
|
|
|
|
sequence1: Option<Sequence<'s>>,
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:11:15 +01:00
|
|
|
impl<'d, 's, T: Instance> Sequencer<'d, 's, T> {
|
|
|
|
/// Create a new double sequence. In the absence of sequence 1, sequence 0
|
|
|
|
/// will be used twice in the one loop.
|
2022-06-12 22:15:44 +02:00
|
|
|
pub fn new(pwm: &'s mut SequencePwm<'d, T>, sequence0: Sequence<'s>, sequence1: Option<Sequence<'s>>) -> Self {
|
2022-02-04 09:11:15 +01:00
|
|
|
Sequencer {
|
|
|
|
_pwm: pwm,
|
2022-02-04 03:04:55 +01:00
|
|
|
sequence0,
|
|
|
|
sequence1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start or restart playback. The sequence mode applies to both sequences combined as one.
|
|
|
|
#[inline(always)]
|
2022-02-04 09:11:15 +01:00
|
|
|
pub fn start(&self, start_seq: StartSequence, times: SequenceMode) -> Result<(), Error> {
|
2022-02-04 03:04:55 +01:00
|
|
|
let sequence0 = &self.sequence0;
|
|
|
|
let alt_sequence = self.sequence1.as_ref().unwrap_or(&self.sequence0);
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
slice_in_ram_or(sequence0.words, Error::BufferNotInRAM)?;
|
|
|
|
slice_in_ram_or(alt_sequence.words, Error::BufferNotInRAM)?;
|
2022-02-04 03:04:55 +01:00
|
|
|
|
|
|
|
if sequence0.words.len() > MAX_SEQUENCE_LEN || alt_sequence.words.len() > MAX_SEQUENCE_LEN {
|
|
|
|
return Err(Error::SequenceTooLong);
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:11:15 +01:00
|
|
|
if let SequenceMode::Loop(0) = times {
|
2022-02-04 03:04:55 +01:00
|
|
|
return Err(Error::SequenceTimesAtLeastOne);
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = self.stop();
|
|
|
|
|
|
|
|
let r = T::regs();
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
r.seq0.refresh.write(|w| unsafe { w.bits(sequence0.config.refresh) });
|
|
|
|
r.seq0.enddelay.write(|w| unsafe { w.bits(sequence0.config.end_delay) });
|
|
|
|
r.seq0.ptr.write(|w| unsafe { w.bits(sequence0.words.as_ptr() as u32) });
|
|
|
|
r.seq0.cnt.write(|w| unsafe { w.bits(sequence0.words.len() as u32) });
|
2022-02-04 03:04:55 +01:00
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
r.seq1.refresh.write(|w| unsafe { w.bits(alt_sequence.config.refresh) });
|
2022-02-04 03:04:55 +01:00
|
|
|
r.seq1
|
|
|
|
.enddelay
|
|
|
|
.write(|w| unsafe { w.bits(alt_sequence.config.end_delay) });
|
|
|
|
r.seq1
|
|
|
|
.ptr
|
|
|
|
.write(|w| unsafe { w.bits(alt_sequence.words.as_ptr() as u32) });
|
2022-06-12 22:15:44 +02:00
|
|
|
r.seq1.cnt.write(|w| unsafe { w.bits(alt_sequence.words.len() as u32) });
|
2022-02-04 03:04:55 +01:00
|
|
|
|
|
|
|
r.enable.write(|w| w.enable().enabled());
|
|
|
|
|
|
|
|
// defensive before seqstart
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
let seqstart_index = if start_seq == StartSequence::One { 1 } else { 0 };
|
2022-02-04 09:11:15 +01:00
|
|
|
|
2022-02-04 03:04:55 +01:00
|
|
|
match times {
|
|
|
|
// just the one time, no loop count
|
2022-02-04 09:11:15 +01:00
|
|
|
SequenceMode::Loop(n) => {
|
|
|
|
r.loop_.write(|w| unsafe { w.cnt().bits(n) });
|
2022-02-04 03:04:55 +01:00
|
|
|
}
|
|
|
|
// to play infinitely, repeat the sequence one time, then have loops done self trigger seq0 again
|
|
|
|
SequenceMode::Infinite => {
|
|
|
|
r.loop_.write(|w| unsafe { w.cnt().bits(0x1) });
|
|
|
|
r.shorts.write(|w| w.loopsdone_seqstart0().enabled());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:11:15 +01:00
|
|
|
// tasks_seqstart() doesn't exist in all svds so write its bit instead
|
|
|
|
r.tasks_seqstart[seqstart_index].write(|w| unsafe { w.bits(0x01) });
|
|
|
|
|
2022-02-04 03:04:55 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop playback. Disables the peripheral. Does NOT clear the last duty
|
|
|
|
/// cycle from the pin. Returns any sequences previously provided to
|
|
|
|
/// `start` so that they may be further mutated.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn stop(&self) {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
r.shorts.reset();
|
|
|
|
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
// tasks_stop() doesn't exist in all svds so write its bit instead
|
|
|
|
r.tasks_stop.write(|w| unsafe { w.bits(0x01) });
|
|
|
|
|
|
|
|
r.enable.write(|w| w.enable().disabled());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:11:15 +01:00
|
|
|
impl<'d, 's, T: Instance> Drop for Sequencer<'d, 's, T> {
|
2022-02-04 03:04:55 +01:00
|
|
|
fn drop(&mut self) {
|
|
|
|
let _ = self.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:11:15 +01:00
|
|
|
/// How many times to run a single sequence
|
2021-11-04 02:37:54 +01:00
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
2022-02-04 09:11:15 +01:00
|
|
|
pub enum SingleSequenceMode {
|
|
|
|
/// Run a single sequence n Times total.
|
2021-11-04 02:37:54 +01:00
|
|
|
Times(u16),
|
2022-01-30 06:21:23 +01:00
|
|
|
/// Repeat until `stop` is called.
|
2021-11-04 02:37:54 +01:00
|
|
|
Infinite,
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:11:15 +01:00
|
|
|
/// Which sequence to start a loop with
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
|
|
|
pub enum StartSequence {
|
|
|
|
/// Start with Sequence 0
|
|
|
|
Zero,
|
|
|
|
/// Start with Sequence 1
|
|
|
|
One,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// How many loops to run two sequences
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
|
|
|
pub enum SequenceMode {
|
|
|
|
/// Run two sequences n loops i.e. (n * (seq0 + seq1.unwrap_or(seq0)))
|
|
|
|
Loop(u16),
|
|
|
|
/// Repeat until `stop` is called.
|
|
|
|
Infinite,
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:37:54 +01:00
|
|
|
/// PWM Base clock is system clock (16MHz) divided by prescaler
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
|
|
|
|
pub enum Prescaler {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Divide by 1
|
2021-11-04 02:37:54 +01:00
|
|
|
Div1,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Divide by 2
|
2021-11-04 02:37:54 +01:00
|
|
|
Div2,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Divide by 4
|
2021-11-04 02:37:54 +01:00
|
|
|
Div4,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Divide by 8
|
2021-11-04 02:37:54 +01:00
|
|
|
Div8,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Divide by 16
|
2021-11-04 02:37:54 +01:00
|
|
|
Div16,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Divide by 32
|
2021-11-04 02:37:54 +01:00
|
|
|
Div32,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Divide by 64
|
2021-11-04 02:37:54 +01:00
|
|
|
Div64,
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Divide by 128
|
2021-11-04 02:37:54 +01:00
|
|
|
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 01:30:59 +01:00
|
|
|
/// Selects up mode or up-and-down mode for the counter
|
2021-11-04 02:37:54 +01: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 03:11:37 +01:00
|
|
|
}
|
|
|
|
|
2021-11-04 02:25:44 +01:00
|
|
|
impl<'d, T: Instance> SimplePwm<'d, T> {
|
2022-02-12 01:04:01 +01:00
|
|
|
/// Create a new 1-channel PWM
|
2021-11-02 03:11:37 +01:00
|
|
|
#[allow(unused_unsafe)]
|
2022-07-23 14:00:19 +02:00
|
|
|
pub fn new_1ch(pwm: impl Peripheral<P = T> + 'd, ch0: impl Peripheral<P = impl GpioPin> + 'd) -> Self {
|
2022-07-03 23:16:10 +02:00
|
|
|
unsafe {
|
2022-07-23 14:27:45 +02:00
|
|
|
into_ref!(ch0);
|
|
|
|
Self::new_inner(pwm, Some(ch0.map_into()), None, None, None)
|
2022-07-03 23:16:10 +02:00
|
|
|
}
|
2022-02-12 01:04:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new 2-channel PWM
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
pub fn new_2ch(
|
2022-07-23 14:00:19 +02:00
|
|
|
pwm: impl Peripheral<P = T> + 'd,
|
|
|
|
ch0: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
ch1: impl Peripheral<P = impl GpioPin> + 'd,
|
2022-02-12 01:04:01 +01:00
|
|
|
) -> Self {
|
2022-07-23 14:27:45 +02:00
|
|
|
into_ref!(ch0, ch1);
|
|
|
|
Self::new_inner(pwm, Some(ch0.map_into()), Some(ch1.map_into()), None, None)
|
2022-02-12 01:04:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new 3-channel PWM
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
pub fn new_3ch(
|
2022-07-23 14:00:19 +02:00
|
|
|
pwm: impl Peripheral<P = T> + 'd,
|
|
|
|
ch0: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
ch1: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
ch2: impl Peripheral<P = impl GpioPin> + 'd,
|
2022-02-12 01:04:01 +01:00
|
|
|
) -> Self {
|
2022-07-03 23:16:10 +02:00
|
|
|
unsafe {
|
2022-07-23 14:27:45 +02:00
|
|
|
into_ref!(ch0, ch1, ch2);
|
|
|
|
Self::new_inner(
|
|
|
|
pwm,
|
|
|
|
Some(ch0.map_into()),
|
|
|
|
Some(ch1.map_into()),
|
|
|
|
Some(ch2.map_into()),
|
|
|
|
None,
|
|
|
|
)
|
2022-07-03 23:16:10 +02:00
|
|
|
}
|
2022-02-12 01:04:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new 4-channel PWM
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
pub fn new_4ch(
|
2022-07-23 14:00:19 +02:00
|
|
|
pwm: impl Peripheral<P = T> + 'd,
|
|
|
|
ch0: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
ch1: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
ch2: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
ch3: impl Peripheral<P = impl GpioPin> + 'd,
|
2021-11-02 03:11:37 +01:00
|
|
|
) -> Self {
|
2022-07-03 23:16:10 +02:00
|
|
|
unsafe {
|
2022-07-23 14:27:45 +02:00
|
|
|
into_ref!(ch0, ch1, ch2, ch3);
|
|
|
|
Self::new_inner(
|
|
|
|
pwm,
|
|
|
|
Some(ch0.map_into()),
|
|
|
|
Some(ch1.map_into()),
|
|
|
|
Some(ch2.map_into()),
|
|
|
|
Some(ch3.map_into()),
|
|
|
|
)
|
2022-07-03 23:16:10 +02:00
|
|
|
}
|
2022-02-12 01:04:01 +01:00
|
|
|
}
|
2021-11-02 03:11:37 +01:00
|
|
|
|
2022-02-12 01:04:01 +01:00
|
|
|
fn new_inner(
|
2022-07-23 14:00:19 +02:00
|
|
|
_pwm: impl Peripheral<P = T> + 'd,
|
|
|
|
ch0: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
ch1: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
ch2: Option<PeripheralRef<'d, AnyPin>>,
|
|
|
|
ch3: Option<PeripheralRef<'d, AnyPin>>,
|
2022-02-12 01:04:01 +01:00
|
|
|
) -> Self {
|
2022-07-23 15:13:47 +02:00
|
|
|
into_ref!(_pwm);
|
|
|
|
|
2021-11-02 03:11:37 +01:00
|
|
|
let r = T::regs();
|
|
|
|
|
2022-02-12 01:04:01 +01:00
|
|
|
if let Some(pin) = &ch0 {
|
2021-11-02 03:11:37 +01:00
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
2022-02-12 01:04:01 +01:00
|
|
|
if let Some(pin) = &ch1 {
|
2021-11-02 03:11:37 +01:00
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
2022-02-12 01:04:01 +01:00
|
|
|
if let Some(pin) = &ch2 {
|
2021-11-02 03:11:37 +01:00
|
|
|
pin.set_low();
|
|
|
|
pin.conf().write(|w| w.dir().output());
|
|
|
|
}
|
2022-02-12 01:04:01 +01:00
|
|
|
if let Some(pin) = &ch3 {
|
2021-11-02 03:11:37 +01:00
|
|
|
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 20:32:28 +01:00
|
|
|
let pwm = Self {
|
2022-07-23 15:13:47 +02:00
|
|
|
_peri: _pwm,
|
2022-02-12 01:04:01 +01:00
|
|
|
ch0,
|
|
|
|
ch1,
|
|
|
|
ch2,
|
|
|
|
ch3,
|
2021-11-10 20:32:28 +01:00
|
|
|
duty: [0; 4],
|
|
|
|
};
|
|
|
|
|
2021-11-02 03:11:37 +01:00
|
|
|
// Disable all interrupts
|
|
|
|
r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) });
|
|
|
|
r.shorts.reset();
|
|
|
|
|
|
|
|
// Enable
|
|
|
|
r.enable.write(|w| w.enable().enabled());
|
|
|
|
|
2022-06-12 22:15:44 +02:00
|
|
|
r.seq0.ptr.write(|w| unsafe { w.bits((&pwm.duty).as_ptr() as u32) });
|
2021-11-10 20:32:28 +01:00
|
|
|
|
2021-11-02 03:11:37 +01: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 20:32:28 +01:00
|
|
|
pwm
|
2021-10-26 09:37:52 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 00:02:50 +02:00
|
|
|
/// Enables the PWM generator.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn enable(&self) {
|
|
|
|
let r = T::regs();
|
|
|
|
r.enable.write(|w| w.enable().enabled());
|
|
|
|
}
|
|
|
|
|
2021-11-14 00:38:35 +01:00
|
|
|
/// Disables the PWM generator. Does NOT clear the last duty cycle from the pin.
|
2021-05-15 00:02:50 +02:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn disable(&self) {
|
|
|
|
let r = T::regs();
|
|
|
|
r.enable.write(|w| w.enable().disabled());
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:56:11 +02:00
|
|
|
/// Sets duty cycle (15 bit) for a PWM channel.
|
2021-11-10 20:32:28 +01:00
|
|
|
pub fn set_duty(&mut self, channel: usize, duty: u16) {
|
2021-11-02 19:56:01 +01:00
|
|
|
let r = T::regs();
|
2021-11-10 20:32:28 +01:00
|
|
|
|
|
|
|
self.duty[channel] = duty & 0x7FFF;
|
|
|
|
|
2021-11-12 16:55:20 +01:00
|
|
|
// reload ptr in case self was moved
|
2022-06-12 22:15:44 +02:00
|
|
|
r.seq0.ptr.write(|w| unsafe { w.bits((&self.duty).as_ptr() as u32) });
|
2021-11-12 16:55:20 +01:00
|
|
|
|
2021-11-10 20:34:41 +01:00
|
|
|
// defensive before seqstart
|
2021-05-12 04:56:11 +02:00
|
|
|
compiler_fence(Ordering::SeqCst);
|
2021-11-01 21:00:50 +01:00
|
|
|
|
2021-11-14 00:38:35 +01:00
|
|
|
r.events_seqend[0].reset();
|
|
|
|
|
2021-11-02 19:56:01 +01:00
|
|
|
// tasks_seqstart() doesn't exist in all svds so write its bit instead
|
2021-11-01 21:00:50 +01:00
|
|
|
r.tasks_seqstart[0].write(|w| unsafe { w.bits(1) });
|
2021-11-10 20:34:41 +01:00
|
|
|
|
2021-11-12 06:50:07 +01: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 21:01:39 +01:00
|
|
|
while r.events_seqend[0].read().bits() == 0 {}
|
2021-05-12 04:56:11 +02: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 09:37:52 +02:00
|
|
|
/// Gets the PWM clock prescaler.
|
2021-05-12 04:56:11 +02: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 02:25:44 +01:00
|
|
|
impl<'a, T: Instance> Drop for SimplePwm<'a, T> {
|
2021-05-12 04:56:11 +02:00
|
|
|
fn drop(&mut self) {
|
2021-11-11 01:19:03 +01:00
|
|
|
let r = T::regs();
|
|
|
|
|
2021-10-31 01:16:10 +02:00
|
|
|
self.disable();
|
2021-05-12 04:56:11 +02:00
|
|
|
|
2021-11-10 19:02:43 +01:00
|
|
|
if let Some(pin) = &self.ch0 {
|
|
|
|
pin.set_low();
|
2021-11-14 00:41:44 +01:00
|
|
|
pin.conf().reset();
|
|
|
|
r.psel.out[0].reset();
|
2021-11-10 19:02:43 +01:00
|
|
|
}
|
|
|
|
if let Some(pin) = &self.ch1 {
|
|
|
|
pin.set_low();
|
2021-11-14 00:41:44 +01:00
|
|
|
pin.conf().reset();
|
|
|
|
r.psel.out[1].reset();
|
2021-11-10 19:02:43 +01:00
|
|
|
}
|
|
|
|
if let Some(pin) = &self.ch2 {
|
|
|
|
pin.set_low();
|
2021-11-14 00:41:44 +01:00
|
|
|
pin.conf().reset();
|
|
|
|
r.psel.out[2].reset();
|
2021-11-10 19:02:43 +01:00
|
|
|
}
|
|
|
|
if let Some(pin) = &self.ch3 {
|
|
|
|
pin.set_low();
|
2021-11-14 00:41:44 +01:00
|
|
|
pin.conf().reset();
|
|
|
|
r.psel.out[3].reset();
|
2021-11-10 19:02:43 +01:00
|
|
|
}
|
2021-05-12 04:56:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub trait Instance {
|
|
|
|
fn regs() -> &'static pac::pwm0::RegisterBlock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 00:48:33 +01:00
|
|
|
/// PWM peripheral instance.
|
2022-07-23 14:00:19 +02:00
|
|
|
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static {
|
2023-02-01 00:48:33 +01:00
|
|
|
/// Interrupt for this peripheral.
|
2023-06-08 16:08:40 +02:00
|
|
|
type Interrupt: interrupt::typelevel::Interrupt;
|
2021-05-12 04:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-06-08 16:08:40 +02:00
|
|
|
type Interrupt = crate::interrupt::typelevel::$irq;
|
2021-05-12 04:56:11 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|