Skip to content

Commit

Permalink
Resolve some of the comments
Browse files Browse the repository at this point in the history
* Rename struct Timer to TimerDriver
* Fix typo in fn attach_timerX
* Remove OptionalOutputPin
  • Loading branch information
usbalbin committed Jan 25, 2023
1 parent d786f05 commit 4acbca7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
6 changes: 3 additions & 3 deletions examples/mcpwm-simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn main() -> anyhow::Result<()> {
use embedded_hal::delay::DelayUs;

use esp_idf_hal::delay::FreeRtos;
use esp_idf_hal::mcpwm::{OperatorConfig, Timer, TimerConfig};
use esp_idf_hal::mcpwm::{OperatorConfig, TimerConfig, TimerDriver};
use esp_idf_hal::prelude::Peripherals;

esp_idf_sys::link_patches();
Expand All @@ -71,11 +71,11 @@ fn main() -> anyhow::Result<()> {
let peripherals = Peripherals::take().unwrap();
let timer_config = TimerConfig::default().period_ticks(8_000); // 10kHz
let operator_config = OperatorConfig::default(peripherals.pins.gpio4, peripherals.pins.gpio5);
let timer = Timer::new(peripherals.mcpwm0.timer0, timer_config);
let timer = TimerDriver::new(peripherals.mcpwm0.timer0, timer_config);

let mut timer = timer
.into_connection()
.attatch_operator0(peripherals.mcpwm0.operator0, operator_config);
.attach_operator0(peripherals.mcpwm0.operator0, operator_config);

// Borrow references to the contained timer and operator
let (timer, operator, _, _) = timer.split();
Expand Down
2 changes: 1 addition & 1 deletion src/mcpwm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub use self::{
generator::{GeneratorAction, GeneratorConfig},
operator::{Operator, OPERATOR},
operator_config::OperatorConfig,
timer::{Timer, TimerConfig, TIMER},
timer::{TimerConfig, TimerDriver, TIMER},
timer_connection::TimerConnection,
};

Expand Down
6 changes: 3 additions & 3 deletions src/mcpwm/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl TimerConfig {
}
}

pub struct Timer<const N: u8, G: Group> {
pub struct TimerDriver<const N: u8, G: Group> {
_group: G,
handle: mcpwm_timer_handle_t,
_timer: TIMER<N, G>,
Expand All @@ -90,7 +90,7 @@ pub struct Timer<const N: u8, G: Group> {
period_peak: u16,
}

impl<const N: u8, G: Group> Timer<N, G> {
impl<const N: u8, G: Group> TimerDriver<N, G> {
pub fn new(timer: TIMER<N, G>, config: TimerConfig) -> Self {
let mut flags: mcpwm_timer_config_t__bindgen_ty_1 = Default::default();

Expand Down Expand Up @@ -186,7 +186,7 @@ impl<const N: u8, G: Group> Timer<N, G> {

// TODO: Should this be done in TimerConnection instead to ensure everything is taken down
// in the correct order?
impl<const N: u8, G: Group> Drop for Timer<N, G> {
impl<const N: u8, G: Group> Drop for TimerDriver<N, G> {
fn drop(&mut self) {
unsafe {
esp!(mcpwm_del_timer(self.handle)).unwrap();
Expand Down
19 changes: 7 additions & 12 deletions src/mcpwm/timer_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
comparator::OptionalCmpCfg,
generator::OptionalGenCfg,
operator::{self, NoOperator, OptionalOperator, OPERATOR},
timer::Timer,
timer::TimerDriver,
Operator, OperatorConfig,
};

Expand All @@ -16,14 +16,14 @@ where
O1: OptionalOperator<1, G>,
O2: OptionalOperator<2, G>,
{
timer: Timer<N, G>,
timer: TimerDriver<N, G>,
operator0: O0,
operator1: O1,
operator2: O2,
}

impl<const N: u8, G: Group> TimerConnection<N, G, NoOperator, NoOperator, NoOperator> {
pub(crate) fn new(timer: Timer<N, G>) -> Self {
pub(crate) fn new(timer: TimerDriver<N, G>) -> Self {
Self {
timer,
operator0: NoOperator,
Expand All @@ -46,7 +46,7 @@ impl<
O2: OptionalOperator<2, G>,
> TimerConnection<N, G, O0, O1, O2>
{
pub fn split(&mut self) -> (&mut Timer<N, G>, &mut O0, &mut O1, &mut O2) {
pub fn split(&mut self) -> (&mut TimerDriver<N, G>, &mut O0, &mut O1, &mut O2) {
(
&mut self.timer,
&mut self.operator0,
Expand All @@ -63,7 +63,7 @@ where
O2: OptionalOperator<2, G>,
{
#[allow(clippy::type_complexity)]
pub fn attatch_operator0<CMPX, CMPY, GENA, GENB>(
pub fn attach_operator0<CMPX, CMPY, GENA, GENB>(
self,
operator_handle: OPERATOR<0, G>,
operator_cfg: OperatorConfig<CMPX, CMPY, GENA, GENB>,
Expand Down Expand Up @@ -91,7 +91,7 @@ where
O2: OptionalOperator<2, G>,
{
#[allow(clippy::type_complexity)]
pub fn attatch_operator1<CMPX, CMPY, GENA, GENB>(
pub fn attach_operator1<CMPX, CMPY, GENA, GENB>(
self,
operator_handle: OPERATOR<1, G>,
operator_cfg: OperatorConfig<CMPX, CMPY, GENA, GENB>,
Expand Down Expand Up @@ -119,7 +119,7 @@ where
O1: OptionalOperator<1, G>,
{
#[allow(clippy::type_complexity)]
pub fn attatch_operator2<CMPX, CMPY, GENA, GENB>(
pub fn attach_operator2<CMPX, CMPY, GENA, GENB>(
self,
operator_handle: OPERATOR<2, G>,
operator_cfg: OperatorConfig<CMPX, CMPY, GENA, GENB>,
Expand Down Expand Up @@ -154,8 +154,3 @@ impl<const N: u8, G, O0, O1, O2> Drop for TimerConnection<N, G, O0, O1, O2>
}
}
*/

// TODO: Should this be moved somewhere else?
pub trait OptionalOutputPin {}

impl<P: crate::gpio::OutputPin> OptionalOutputPin for P {}
12 changes: 10 additions & 2 deletions src/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,17 @@ impl Peripherals {
can: can::CAN::new(),
#[cfg(not(feature = "riscv-ulp-hal"))]
ledc: ledc::LEDC::new(),
#[cfg(all(any(esp32, esp32s3), esp_idf_version_major = "5"))]
#[cfg(all(
any(esp32, esp32s3),
not(feature = "riscv-ulp-hal"),
esp_idf_version_major = "5"
))]
mcpwm0: mcpwm::MCPWM::<mcpwm::Group0>::new(),
#[cfg(all(any(esp32, esp32s3), esp_idf_version_major = "5"))]
#[cfg(all(
any(esp32, esp32s3),
not(feature = "riscv-ulp-hal"),
esp_idf_version_major = "5"
))]
mcpwm1: mcpwm::MCPWM::<mcpwm::Group1>::new(),
#[cfg(not(feature = "riscv-ulp-hal"))]
rmt: rmt::RMT::new(),
Expand Down

0 comments on commit 4acbca7

Please sign in to comment.