Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions esp-hal-common/src/ledc/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,18 @@ pub enum Number {
pub mod config {
use crate::ledc::timer::{TimerIFace, TimerSpeed};

#[derive(Copy, Clone)]
pub enum PinConfig {
PushPull,
OpenDrain,
}

/// Channel configuration
#[derive(Copy, Clone)]
pub struct Config<'a, S: TimerSpeed> {
pub timer: &'a dyn TimerIFace<S>,
pub duty_pct: u8,
pub pin_config: PinConfig,
}
}

Expand All @@ -67,6 +74,7 @@ pub trait ChannelHW<O: OutputPin> {
/// Configure Channel HW except for the duty which is set via
/// [`Self::set_duty_hw`].
fn configure_hw(&mut self) -> Result<(), Error>;
fn configure_hw_with_pin_config(&mut self, cfg: config::PinConfig) -> Result<(), Error>;

/// Set channel duty HW
fn set_duty_hw(&self, duty: u32);
Expand Down Expand Up @@ -103,7 +111,7 @@ where
self.timer = Some(config.timer);

self.set_duty(config.duty_pct)?;
self.configure_hw()?;
self.configure_hw_with_pin_config(config.pin_config)?;

Ok(())
}
Expand Down Expand Up @@ -298,12 +306,18 @@ where
/// Configure Channel HW except for the duty which is set via
/// [`Self::set_duty_hw`].
fn configure_hw(&mut self) -> Result<(), Error> {
self.configure_hw_with_pin_config(config::PinConfig::PushPull)
}
fn configure_hw_with_pin_config(&mut self, cfg: config::PinConfig) -> Result<(), Error> {
if let Some(timer) = self.timer {
if !timer.is_configured() {
return Err(Error::Timer);
}

self.output_pin.set_to_push_pull_output();
match cfg {
config::PinConfig::PushPull => self.output_pin.set_to_push_pull_output(),
config::PinConfig::OpenDrain => self.output_pin.set_to_open_drain_output(),
};

let timer_number = timer.get_number() as u8;
match self.number {
Expand Down Expand Up @@ -377,12 +391,22 @@ where
{
/// Configure Channel HW
fn configure_hw(&mut self) -> Result<(), Error> {
self.configure_hw_with_pin_config(config::PinConfig::PushPull)
}
fn configure_hw_with_pin_config(&mut self, cfg: config::PinConfig) -> Result<(), Error> {
if let Some(timer) = self.timer {
if !timer.is_configured() {
return Err(Error::Timer);
}

self.output_pin.set_to_push_pull_output();
match cfg {
config::PinConfig::PushPull => {
self.output_pin.set_to_push_pull_output();
}
config::PinConfig::OpenDrain => {
self.output_pin.set_to_open_drain_output();
}
}

let timer_number = timer.get_number() as u8;
match self.number {
Expand Down
1 change: 1 addition & 0 deletions esp32-hal/examples/ledc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn main() -> ! {
.configure(channel::config::Config {
timer: &hstimer0,
duty_pct: 10,
pin_config: channel::config::PinConfig::PushPull,
})
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions esp32c2-hal/examples/ledc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fn main() -> ! {
.configure(channel::config::Config {
timer: &lstimer0,
duty_pct: 90,
pin_config: channel::config::PinConfig::PushPull,
})
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions esp32c3-hal/examples/ledc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ fn main() -> ! {
.configure(channel::config::Config {
timer: &lstimer0,
duty_pct: 10,
pin_config: channel::config::PinConfig::PushPull,
})
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions esp32c6-hal/examples/ledc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ fn main() -> ! {
.configure(channel::config::Config {
timer: &lstimer0,
duty_pct: 10,
pin_config: channel::config::PinConfig::PushPull,
})
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions esp32s2-hal/examples/ledc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn main() -> ! {
.configure(channel::config::Config {
timer: &lstimer0,
duty_pct: 10,
pin_config: channel::config::PinConfig::PushPull,
})
.unwrap();

Expand Down
1 change: 1 addition & 0 deletions esp32s3-hal/examples/ledc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fn main() -> ! {
.configure(channel::config::Config {
timer: &lstimer0,
duty_pct: 10,
pin_config: channel::config::PinConfig::PushPull,
})
.unwrap();

Expand Down