#![macro_use]
use core::sync::atomic::{Ordering, compiler_fence};
use embassy_hal_internal::{Peri, PeripheralType};
use crate::gpio::{AnyPin, DISCONNECTED, Level, OutputDrive, Pin as GpioPin, PselBits, SealedPin as _, convert_drive};
use crate::pac::gpio::vals as gpiovals;
use crate::pac::pwm::vals;
use crate::ppi::{Event, Task};
use crate::util::slice_in_ram_or;
use crate::{interrupt, pac};
pub struct SimplePwm<'d> {
r: pac::pwm::Pwm,
duty: [DutyCycle; 4],
ch0: Option<Peri<'d, AnyPin>>,
ch1: Option<Peri<'d, AnyPin>>,
ch2: Option<Peri<'d, AnyPin>>,
ch3: Option<Peri<'d, AnyPin>>,
}
pub struct SequencePwm<'d> {
r: pac::pwm::Pwm,
ch0: Option<Peri<'d, AnyPin>>,
ch1: Option<Peri<'d, AnyPin>>,
ch2: Option<Peri<'d, AnyPin>>,
ch3: Option<Peri<'d, AnyPin>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum Error {
SequenceTooLong,
SequenceTimesAtLeastOne,
BufferNotInRAM,
}
const MAX_SEQUENCE_LEN: usize = 32767;
pub const PWM_CLK_HZ: u32 = 16_000_000;
impl<'d> SequencePwm<'d> {
pub fn new_1ch<T: Instance>(pwm: Peri<'d, T>, ch0: Peri<'d, impl GpioPin>, config: Config) -> Result<Self, Error> {
Self::new_inner(pwm, Some(ch0.into()), None, None, None, config)
}
pub fn new_2ch<T: Instance>(
pwm: Peri<'d, T>,
ch0: Peri<'d, impl GpioPin>,
ch1: Peri<'d, impl GpioPin>,
config: Config,
) -> Result<Self, Error> {
Self::new_inner(pwm, Some(ch0.into()), Some(ch1.into()), None, None, config)
}
pub fn new_3ch<T: Instance>(
pwm: Peri<'d, T>,
ch0: Peri<'d, impl GpioPin>,
ch1: Peri<'d, impl GpioPin>,
ch2: Peri<'d, impl GpioPin>,
config: Config,
) -> Result<Self, Error> {
Self::new_inner(pwm, Some(ch0.into()), Some(ch1.into()), Some(ch2.into()), None, config)
}
pub fn new_4ch<T: Instance>(
pwm: Peri<'d, T>,
ch0: Peri<'d, impl GpioPin>,
ch1: Peri<'d, impl GpioPin>,
ch2: Peri<'d, impl GpioPin>,
ch3: Peri<'d, impl GpioPin>,
config: Config,
) -> Result<Self, Error> {
Self::new_inner(
pwm,
Some(ch0.into()),
Some(ch1.into()),
Some(ch2.into()),
Some(ch3.into()),
config,
)
}
fn new_inner<T: Instance>(
_pwm: Peri<'d, T>,
ch0: Option<Peri<'d, AnyPin>>,
ch1: Option<Peri<'d, AnyPin>>,
ch2: Option<Peri<'d, AnyPin>>,
ch3: Option<Peri<'d, AnyPin>>,
config: Config,
) -> Result<Self, Error> {
let r = T::regs();
let channels = [
(&ch0, config.ch0_drive, config.ch0_idle_level),
(&ch1, config.ch1_drive, config.ch1_idle_level),
(&ch2, config.ch2_drive, config.ch2_idle_level),
(&ch3, config.ch3_drive, config.ch3_idle_level),
];
for (i, (pin, drive, idle_level)) in channels.into_iter().enumerate() {
if let Some(pin) = pin {
match idle_level {
Level::Low => pin.set_low(),
Level::High => pin.set_high(),
}
pin.conf().write(|w| {
w.set_dir(gpiovals::Dir::OUTPUT);
w.set_input(gpiovals::Input::DISCONNECT);
convert_drive(w, drive);
});
}
r.psel().out(i).write_value(pin.psel_bits());
}
r.intenclr().write(|w| w.0 = 0xFFFF_FFFF);
r.shorts().write(|_| ());
r.events_stopped().write_value(0);
r.events_loopsdone().write_value(0);
r.events_seqend(0).write_value(0);
r.events_seqend(1).write_value(0);
r.events_pwmperiodend().write_value(0);
r.events_seqstarted(0).write_value(0);
r.events_seqstarted(1).write_value(0);
r.decoder().write(|w| {
w.set_load(vals::Load::from_bits(config.sequence_load as u8));
w.set_mode(vals::Mode::REFRESH_COUNT);
});
r.mode().write(|w| match config.counter_mode {
CounterMode::UpAndDown => w.set_updown(vals::Updown::UP_AND_DOWN),
CounterMode::Up => w.set_updown(vals::Updown::UP),
});
r.prescaler()
.write(|w| w.set_prescaler(vals::Prescaler::from_bits(config.prescaler as u8)));
r.countertop().write(|w| w.set_countertop(config.max_duty));
Ok(Self { r, ch0, ch1, ch2, ch3 })
}
#[inline(always)]
pub fn event_stopped(&self) -> Event<'d> {
Event::from_reg(self.r.events_stopped())
}
#[inline(always)]
pub fn event_loops_done(&self) -> Event<'d> {
Event::from_reg(self.r.events_loopsdone())
}
#[inline(always)]
pub fn event_pwm_period_end(&self) -> Event<'d> {
Event::from_reg(self.r.events_pwmperiodend())
}
#[inline(always)]
pub fn event_seq_end(&self) -> Event<'d> {
Event::from_reg(self.r.events_seqend(0))
}
#[inline(always)]
pub fn event_seq1_end(&self) -> Event<'d> {
Event::from_reg(self.r.events_seqend(1))
}
#[inline(always)]
pub fn event_seq0_started(&self) -> Event<'d> {
Event::from_reg(self.r.events_seqstarted(0))
}
#[inline(always)]
pub fn event_seq1_started(&self) -> Event<'d> {
Event::from_reg(self.r.events_seqstarted(1))
}
#[inline(always)]
pub unsafe fn task_start_seq0(&self) -> Task<'d> {
Task::from_reg(self.r.tasks_dma().seq(0).start())
}
#[inline(always)]
pub unsafe fn task_start_seq1(&self) -> Task<'d> {
Task::from_reg(self.r.tasks_dma().seq(1).start())
}
#[inline(always)]
pub unsafe fn task_next_step(&self) -> Task<'d> {
Task::from_reg(self.r.tasks_nextstep())
}
#[inline(always)]
pub unsafe fn task_stop(&self) -> Task<'d> {
Task::from_reg(self.r.tasks_stop())
}
}
impl<'a> Drop for SequencePwm<'a> {
fn drop(&mut self) {
if let Some(pin) = &self.ch0 {
pin.set_low();
pin.conf().write(|_| ());
self.r.psel().out(0).write_value(DISCONNECTED);
}
if let Some(pin) = &self.ch1 {
pin.set_low();
pin.conf().write(|_| ());
self.r.psel().out(1).write_value(DISCONNECTED);
}
if let Some(pin) = &self.ch2 {
pin.set_low();
pin.conf().write(|_| ());
self.r.psel().out(2).write_value(DISCONNECTED);
}
if let Some(pin) = &self.ch3 {
pin.set_low();
pin.conf().write(|_| ());
self.r.psel().out(3).write_value(DISCONNECTED);
}
self.r.enable().write(|w| w.set_enable(false));
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub struct Config {
pub counter_mode: CounterMode,
pub max_duty: u16,
pub prescaler: Prescaler,
pub sequence_load: SequenceLoad,
pub ch0_drive: OutputDrive,
pub ch1_drive: OutputDrive,
pub ch2_drive: OutputDrive,
pub ch3_drive: OutputDrive,
pub ch0_idle_level: Level,
pub ch1_idle_level: Level,
pub ch2_idle_level: Level,
pub ch3_idle_level: Level,
}
impl Default for Config {
fn default() -> Self {
Self {
counter_mode: CounterMode::Up,
max_duty: 1000,
prescaler: Prescaler::Div16,
sequence_load: SequenceLoad::Common,
ch0_drive: OutputDrive::Standard,
ch1_drive: OutputDrive::Standard,
ch2_drive: OutputDrive::Standard,
ch3_drive: OutputDrive::Standard,
ch0_idle_level: Level::Low,
ch1_idle_level: Level::Low,
ch2_idle_level: Level::Low,
ch3_idle_level: Level::Low,
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub struct SimpleConfig {
pub counter_mode: CounterMode,
pub max_duty: u16,
pub prescaler: Prescaler,
pub ch0_drive: OutputDrive,
pub ch1_drive: OutputDrive,
pub ch2_drive: OutputDrive,
pub ch3_drive: OutputDrive,
pub ch0_idle_level: Level,
pub ch1_idle_level: Level,
pub ch2_idle_level: Level,
pub ch3_idle_level: Level,
}
impl Default for SimpleConfig {
fn default() -> Self {
Self {
counter_mode: CounterMode::Up,
max_duty: 1000,
prescaler: Prescaler::Div16,
ch0_drive: OutputDrive::Standard,
ch1_drive: OutputDrive::Standard,
ch2_drive: OutputDrive::Standard,
ch3_drive: OutputDrive::Standard,
ch0_idle_level: Level::Low,
ch1_idle_level: Level::Low,
ch2_idle_level: Level::Low,
ch3_idle_level: Level::Low,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SequenceConfig {
pub refresh: u32,
pub end_delay: u32,
}
impl Default for SequenceConfig {
fn default() -> SequenceConfig {
SequenceConfig {
refresh: 0,
end_delay: 0,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Sequence<'s> {
pub words: &'s [u16],
pub config: SequenceConfig,
}
impl<'s> Sequence<'s> {
pub fn new(words: &'s [u16], config: SequenceConfig) -> Self {
Self { words, config }
}
}
#[non_exhaustive]
pub struct SingleSequencer<'d, 's> {
sequencer: Sequencer<'d, 's>,
}
impl<'d, 's> SingleSequencer<'d, 's> {
pub fn new(pwm: &'s mut SequencePwm<'d>, words: &'s [u16], config: SequenceConfig) -> Self {
Self {
sequencer: Sequencer::new(pwm, Sequence::new(words, config), None),
}
}
#[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)),
SingleSequenceMode::Times(n) if n & 1 == 1 => (StartSequence::One, SequenceMode::Loop((n / 2) + 1)),
SingleSequenceMode::Times(n) => (StartSequence::Zero, SequenceMode::Loop(n / 2)),
SingleSequenceMode::Infinite => (StartSequence::Zero, SequenceMode::Infinite),
};
self.sequencer.start(start_seq, times)
}
#[inline(always)]
pub fn stop(&self) {
self.sequencer.stop();
}
}
#[non_exhaustive]
pub struct Sequencer<'d, 's> {
_pwm: &'s mut SequencePwm<'d>,
sequence0: Sequence<'s>,
sequence1: Option<Sequence<'s>>,
}
#[cfg(feature = "_nrf54l")]
fn pwmseq(r: pac::pwm::Pwm, n: usize) -> pac::pwm::PwmSeq {
r.seq(n)
}
#[cfg(not(feature = "_nrf54l"))]
fn pwmseq(r: pac::pwm::Pwm, n: usize) -> pac::pwm::DmaSeq {
r.dma().seq(n)
}
#[cfg(feature = "_nrf54l")]
const CNT_UNIT: u32 = 2;
#[cfg(not(feature = "_nrf54l"))]
const CNT_UNIT: u32 = 1;
impl<'d, 's> Sequencer<'d, 's> {
pub fn new(pwm: &'s mut SequencePwm<'d>, sequence0: Sequence<'s>, sequence1: Option<Sequence<'s>>) -> Self {
Sequencer {
_pwm: pwm,
sequence0,
sequence1,
}
}
#[inline(always)]
pub fn start(&self, start_seq: StartSequence, times: SequenceMode) -> Result<(), Error> {
let sequence0 = &self.sequence0;
let alt_sequence = self.sequence1.as_ref().unwrap_or(&self.sequence0);
slice_in_ram_or(sequence0.words, Error::BufferNotInRAM)?;
slice_in_ram_or(alt_sequence.words, Error::BufferNotInRAM)?;
if sequence0.words.len() > MAX_SEQUENCE_LEN || alt_sequence.words.len() > MAX_SEQUENCE_LEN {
return Err(Error::SequenceTooLong);
}
if let SequenceMode::Loop(0) = times {
return Err(Error::SequenceTimesAtLeastOne);
}
self.stop();
let r = self._pwm.r;
pwmseq(r, 0).refresh().write(|w| w.0 = sequence0.config.refresh);
pwmseq(r, 0).enddelay().write(|w| w.0 = sequence0.config.end_delay);
r.dma().seq(0).ptr().write_value(sequence0.words.as_ptr() as u32);
r.dma()
.seq(0)
.maxcnt()
.write(|w| w.0 = sequence0.words.len() as u32 * CNT_UNIT);
pwmseq(r, 1).refresh().write(|w| w.0 = alt_sequence.config.refresh);
pwmseq(r, 1).enddelay().write(|w| w.0 = alt_sequence.config.end_delay);
r.dma().seq(1).ptr().write_value(alt_sequence.words.as_ptr() as u32);
r.dma()
.seq(1)
.maxcnt()
.write(|w| w.0 = alt_sequence.words.len() as u32 * CNT_UNIT);
r.enable().write(|w| w.set_enable(true));
compiler_fence(Ordering::SeqCst);
let seqstart_index = if start_seq == StartSequence::One { 1 } else { 0 };
match times {
SequenceMode::Loop(n) => {
r.loop_().write(|w| w.set_cnt(vals::LoopCnt::from_bits(n)));
}
SequenceMode::Infinite => {
r.loop_().write(|w| w.set_cnt(vals::LoopCnt::from_bits(1)));
r.shorts().write(|w| w.set_loopsdone_dma_seq0_start(true));
}
}
r.tasks_dma().seq(seqstart_index).start().write_value(1);
Ok(())
}
#[inline(always)]
pub fn stop(&self) {
let r = self._pwm.r;
r.shorts().write(|_| ());
compiler_fence(Ordering::SeqCst);
r.tasks_stop().write_value(1);
r.enable().write(|w| w.set_enable(false));
}
}
impl<'d, 's> Drop for Sequencer<'d, 's> {
fn drop(&mut self) {
self.stop();
}
}
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SingleSequenceMode {
Times(u16),
Infinite,
}
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum StartSequence {
Zero,
One,
}
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SequenceMode {
Loop(u16),
Infinite,
}
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Prescaler {
Div1,
Div2,
Div4,
Div8,
Div16,
Div32,
Div64,
Div128,
}
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SequenceLoad {
Common,
Grouped,
Individual,
Waveform,
}
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum CounterMode {
Up,
UpAndDown,
}
#[repr(transparent)]
#[derive(Eq, PartialEq, Clone, Copy)]
pub struct DutyCycle {
raw: u16,
}
impl DutyCycle {
pub const fn normal(value: u16) -> Self {
let raw = value & 0x7FFF;
Self { raw }
}
pub const fn inverted(value: u16) -> Self {
let raw = value | 0x8000;
Self { raw }
}
#[must_use = "this function return a new object, it does not modify self"]
pub const fn with_inverted(self, inverted_polarity: bool) -> Self {
if inverted_polarity {
Self::inverted(self.value())
} else {
Self::normal(self.value())
}
}
pub const fn value(&self) -> u16 {
self.raw & 0x7FFF
}
pub const fn is_inverted(&self) -> bool {
self.raw & 0x8000 != 0
}
}
impl core::fmt::Debug for DutyCycle {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.debug_struct("DutyCycle")
.field("value", &self.value())
.field("inverted", &self.is_inverted())
.finish()
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for DutyCycle {
fn format(&self, f: defmt::Formatter) {
defmt::write!(
f,
"DutyCycle {{ value: {=u16}, inverted: {=bool} }}",
self.value(),
self.is_inverted(),
);
}
}
impl<'d> SimplePwm<'d> {
pub fn new_1ch<T: Instance>(pwm: Peri<'d, T>, ch0: Peri<'d, impl GpioPin>, config: &SimpleConfig) -> Self {
Self::new_inner(pwm, Some(ch0.into()), None, None, None, config)
}
pub fn new_2ch<T: Instance>(
pwm: Peri<'d, T>,
ch0: Peri<'d, impl GpioPin>,
ch1: Peri<'d, impl GpioPin>,
config: &SimpleConfig,
) -> Self {
Self::new_inner(pwm, Some(ch0.into()), Some(ch1.into()), None, None, config)
}
pub fn new_3ch<T: Instance>(
pwm: Peri<'d, T>,
ch0: Peri<'d, impl GpioPin>,
ch1: Peri<'d, impl GpioPin>,
ch2: Peri<'d, impl GpioPin>,
config: &SimpleConfig,
) -> Self {
Self::new_inner(pwm, Some(ch0.into()), Some(ch1.into()), Some(ch2.into()), None, config)
}
pub fn new_4ch<T: Instance>(
pwm: Peri<'d, T>,
ch0: Peri<'d, impl GpioPin>,
ch1: Peri<'d, impl GpioPin>,
ch2: Peri<'d, impl GpioPin>,
ch3: Peri<'d, impl GpioPin>,
config: &SimpleConfig,
) -> Self {
Self::new_inner(
pwm,
Some(ch0.into()),
Some(ch1.into()),
Some(ch2.into()),
Some(ch3.into()),
config,
)
}
fn new_inner<T: Instance>(
_pwm: Peri<'d, T>,
ch0: Option<Peri<'d, AnyPin>>,
ch1: Option<Peri<'d, AnyPin>>,
ch2: Option<Peri<'d, AnyPin>>,
ch3: Option<Peri<'d, AnyPin>>,
config: &SimpleConfig,
) -> Self {
let r = T::regs();
let channels = [
(&ch0, config.ch0_drive, config.ch0_idle_level),
(&ch1, config.ch1_drive, config.ch1_idle_level),
(&ch2, config.ch2_drive, config.ch2_idle_level),
(&ch3, config.ch3_drive, config.ch3_idle_level),
];
for (i, (pin, drive, idle_level)) in channels.into_iter().enumerate() {
if let Some(pin) = pin {
match idle_level {
Level::Low => pin.set_low(),
Level::High => pin.set_high(),
}
pin.conf().write(|w| {
w.set_dir(gpiovals::Dir::OUTPUT);
w.set_input(gpiovals::Input::DISCONNECT);
convert_drive(w, drive);
});
}
r.psel().out(i).write_value(pin.psel_bits());
}
let pwm = Self {
r,
ch0,
ch1,
ch2,
ch3,
duty: [const { DutyCycle::normal(0) }; 4],
};
r.intenclr().write(|w| w.0 = 0xFFFF_FFFF);
r.shorts().write(|_| ());
r.enable().write(|w| w.set_enable(true));
r.dma().seq(0).ptr().write_value((pwm.duty).as_ptr() as u32);
r.dma().seq(0).maxcnt().write(|w| w.0 = 4 * CNT_UNIT);
pwmseq(r, 0).refresh().write(|w| w.0 = 0);
pwmseq(r, 0).enddelay().write(|w| w.0 = 0);
r.decoder().write(|w| {
w.set_load(vals::Load::INDIVIDUAL);
w.set_mode(vals::Mode::REFRESH_COUNT);
});
r.mode().write(|w| match config.counter_mode {
CounterMode::UpAndDown => w.set_updown(vals::Updown::UP_AND_DOWN),
CounterMode::Up => w.set_updown(vals::Updown::UP),
});
r.prescaler()
.write(|w| w.set_prescaler(vals::Prescaler::from_bits(config.prescaler as u8)));
r.countertop().write(|w| w.set_countertop(config.max_duty));
r.loop_().write(|w| w.set_cnt(vals::LoopCnt::DISABLED));
pwm
}
#[inline(always)]
pub fn is_enabled(&self) -> bool {
self.r.enable().read().enable()
}
#[inline(always)]
pub fn enable(&self) {
self.r.enable().write(|w| w.set_enable(true));
}
#[inline(always)]
pub fn disable(&self) {
self.r.enable().write(|w| w.set_enable(false));
}
pub fn duty(&self, channel: usize) -> DutyCycle {
self.duty[channel]
}
pub fn set_duty(&mut self, channel: usize, duty: DutyCycle) {
self.duty[channel] = duty;
self.sync_duty_cyles_to_peripheral();
}
pub fn set_all_duties(&mut self, duty: [DutyCycle; 4]) {
self.duty = duty;
self.sync_duty_cyles_to_peripheral();
}
fn sync_duty_cyles_to_peripheral(&self) {
self.r.dma().seq(0).ptr().write_value((self.duty).as_ptr() as u32);
compiler_fence(Ordering::SeqCst);
self.r.events_seqend(0).write_value(0);
self.r.tasks_dma().seq(0).start().write_value(1);
if self.is_enabled() {
while self.r.events_seqend(0).read() == 0 {}
}
}
#[inline(always)]
pub fn set_prescaler(&self, div: Prescaler) {
self.r
.prescaler()
.write(|w| w.set_prescaler(vals::Prescaler::from_bits(div as u8)));
}
#[inline(always)]
pub fn prescaler(&self) -> Prescaler {
match self.r.prescaler().read().prescaler().to_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!(),
}
}
#[inline(always)]
pub fn set_max_duty(&self, duty: u16) {
self.r.countertop().write(|w| w.set_countertop(duty.min(32767u16)));
}
#[inline(always)]
pub fn max_duty(&self) -> u16 {
self.r.countertop().read().countertop()
}
#[inline(always)]
pub fn set_period(&self, freq: u32) {
let clk = PWM_CLK_HZ >> (self.prescaler() as u8);
let duty = clk / freq;
self.set_max_duty(duty.min(32767) as u16);
}
#[inline(always)]
pub fn period(&self) -> u32 {
let clk = PWM_CLK_HZ >> (self.prescaler() as u8);
let max_duty = self.max_duty() as u32;
clk / max_duty
}
#[inline(always)]
pub fn set_ch0_drive(&self, drive: OutputDrive) {
if let Some(pin) = &self.ch0 {
pin.conf().modify(|w| convert_drive(w, drive));
}
}
#[inline(always)]
pub fn set_ch1_drive(&self, drive: OutputDrive) {
if let Some(pin) = &self.ch1 {
pin.conf().modify(|w| convert_drive(w, drive));
}
}
#[inline(always)]
pub fn set_ch2_drive(&self, drive: OutputDrive) {
if let Some(pin) = &self.ch2 {
pin.conf().modify(|w| convert_drive(w, drive));
}
}
#[inline(always)]
pub fn set_ch3_drive(&self, drive: OutputDrive) {
if let Some(pin) = &self.ch3 {
pin.conf().modify(|w| convert_drive(w, drive));
}
}
}
impl<'a> Drop for SimplePwm<'a> {
fn drop(&mut self) {
let r = &self.r;
self.disable();
if let Some(pin) = &self.ch0 {
pin.set_low();
pin.conf().write(|_| ());
r.psel().out(0).write_value(DISCONNECTED);
}
if let Some(pin) = &self.ch1 {
pin.set_low();
pin.conf().write(|_| ());
r.psel().out(1).write_value(DISCONNECTED);
}
if let Some(pin) = &self.ch2 {
pin.set_low();
pin.conf().write(|_| ());
r.psel().out(2).write_value(DISCONNECTED);
}
if let Some(pin) = &self.ch3 {
pin.set_low();
pin.conf().write(|_| ());
r.psel().out(3).write_value(DISCONNECTED);
}
}
}
pub(crate) trait SealedInstance {
fn regs() -> pac::pwm::Pwm;
}
#[allow(private_bounds)]
pub trait Instance: SealedInstance + PeripheralType + 'static {
type Interrupt: interrupt::typelevel::Interrupt;
}
macro_rules! impl_pwm {
($type:ident, $pac_type:ident, $irq:ident) => {
impl crate::pwm::SealedInstance for peripherals::$type {
fn regs() -> pac::pwm::Pwm {
pac::$pac_type
}
}
impl crate::pwm::Instance for peripherals::$type {
type Interrupt = crate::interrupt::typelevel::$irq;
}
};
}