Skip to content

Commit

Permalink
any ledc timer
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed Apr 16, 2023
1 parent f1f3b9b commit 5586ac7
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/ledc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ pub struct LedcTimerDriver<'d> {

impl<'d> LedcTimerDriver<'d> {
pub fn new<T: LedcTimer>(
_timer: impl Peripheral<P = T> + 'd,
timer: impl Peripheral<P = T> + 'd,
config: &config::TimerConfig,
) -> Result<Self, EspError> {
crate::into_ref!(timer);

let timer_config = ledc_timer_config_t {
speed_mode: config.speed_mode.into(),
timer_num: T::timer(),
timer_num: timer.timer(),
#[cfg(esp_idf_version_major = "4")]
__bindgen_anon_1: ledc_timer_config_t__bindgen_ty_1 {
duty_resolution: config.resolution.timer_bits(),
Expand All @@ -126,7 +128,7 @@ impl<'d> LedcTimerDriver<'d> {
esp!(unsafe { ledc_timer_config(&timer_config) })?;

Ok(Self {
timer: T::timer() as _,
timer: timer.timer() as _,
speed_mode: config.speed_mode,
max_duty: config.resolution.max_duty(),
_p: PhantomData,
Expand Down Expand Up @@ -495,7 +497,26 @@ mod chip {

/// LED Control peripheral timer
pub trait LedcTimer {
fn timer() -> ledc_timer_t;
fn timer(&self) -> ledc_timer_t;
}

pub struct AnyLedcTimer {
timer_id: ledc_timer_t,
}
impl AnyLedcTimer {
/// # Safety
///
/// Care should be taken not to instantiate this timer
/// if it is already instantiated and used elsewhere
pub unsafe fn new(timer_id: ledc_timer_t) -> Self {
Self { timer_id }
}
}
crate::impl_peripheral_trait!(AnyLedcTimer);
impl LedcTimer for AnyLedcTimer {
fn timer(&self) -> ledc_timer_t {
self.timer_id
}
}

/// LED Control peripheral output channel
Expand Down Expand Up @@ -528,10 +549,16 @@ mod chip {
crate::impl_peripheral!($instance);

impl LedcTimer for $instance {
fn timer() -> ledc_timer_t {
fn timer(&self) -> ledc_timer_t {
$timer
}
}

impl From<$instance> for AnyLedcTimer {
fn from(_: $instance) -> Self {
unsafe { Self::new($timer) }
}
}
};
}

Expand Down

0 comments on commit 5586ac7

Please sign in to comment.