Skip to content

Implement Pwm from embedded-hal#246 #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ features = ["unproven"]
version = "=1.0.0-alpha.2"
package = "embedded-hal"

[dependencies.embedded-hal-ryankurte]
git = "https://github.com/ryankurte/embedded-hal.git"
branch = "fix/pwm-channel-by-ref"
package = "embedded-hal"

[dependencies.lpc82x-pac]
optional = true
version = "0.7.0"
Expand Down
25 changes: 19 additions & 6 deletions examples/ctimer_blink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

extern crate panic_rtt_target;

use embedded_hal_ryankurte::pwm::Pwm as _;
use lpc8xx_hal::{
cortex_m_rt::entry, ctimer::Channels123, delay::Delay, prelude::*,
CorePeripherals, Peripherals,
Expand Down Expand Up @@ -54,18 +55,30 @@ fn main() -> ! {
for period in periods.clone().rev() {
ctimer.set_period(period);

ctimer.set_duty(Channels123::Channel1, period / 8);
ctimer.set_duty(Channels123::Channel2, period / 4);
ctimer.set_duty(Channels123::Channel3, period / 2);
ctimer
.try_set_duty(&Channels123::Channel1, period / 8)
.unwrap();
ctimer
.try_set_duty(&Channels123::Channel2, period / 4)
.unwrap();
ctimer
.try_set_duty(&Channels123::Channel3, period / 2)
.unwrap();

delay.delay_ms(period / 12_000);
}
for period in periods.clone() {
ctimer.set_period(period);

ctimer.set_duty(Channels123::Channel1, period / 8);
ctimer.set_duty(Channels123::Channel2, period / 4);
ctimer.set_duty(Channels123::Channel3, period / 2);
ctimer
.try_set_duty(&Channels123::Channel1, period / 8)
.unwrap();
ctimer
.try_set_duty(&Channels123::Channel2, period / 4)
.unwrap();
ctimer
.try_set_duty(&Channels123::Channel3, period / 2)
.unwrap();

delay.delay_ms(period / 12_000);
}
Expand Down
202 changes: 202 additions & 0 deletions src/ctimer/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::convert::Infallible;

use embedded_hal::{Pwm, PwmPin as _};
use embedded_hal_alpha::pwm::{Pwm as PwmAlpha, PwmPin as _};
use embedded_hal_ryankurte::pwm::Pwm as PwmRyankurte;

use crate::{
init_state::{Disabled, Enabled},
Expand Down Expand Up @@ -565,6 +566,207 @@ impl PwmAlpha for CTIMER<Enabled, Attached, Attached, Attached> {
}
}

impl PwmRyankurte for CTIMER<Enabled, Attached, Detached, Detached> {
type Error = Infallible;
type Channel = Channels1;
type Time = u32;
type Duty = u32;

fn try_disable(
&mut self,
channel: &Self::Channel,
) -> Result<(), Self::Error> {
match channel {
Self::Channel::Channel1 => self.channels.channel1.try_disable(),
}
}

fn try_enable(
&mut self,
channel: &Self::Channel,
) -> Result<(), Self::Error> {
match channel {
Self::Channel::Channel1 => self.channels.channel1.try_enable(),
}
}

fn try_get_period(&self) -> Result<Self::Time, Self::Error> {
Ok(self.get_period())
}

fn try_get_duty(
&self,
channel: &Self::Channel,
) -> Result<Self::Duty, Self::Error> {
match channel {
Self::Channel::Channel1 => self.channels.channel1.try_get_duty(),
}
}

fn try_get_max_duty(&self) -> Result<Self::Duty, Self::Error> {
Ok(self.get_max_duty())
}

fn try_set_duty(
&mut self,
channel: &Self::Channel,
duty: Self::Duty,
) -> Result<(), Self::Error> {
match channel {
Self::Channel::Channel1 => {
self.channels.channel1.try_set_duty(duty)
}
}
}

fn try_set_period<P>(&mut self, period: P) -> Result<(), Self::Error>
where
P: Into<Self::Time>,
{
Ok(self.set_period(period.into()))
}
}

impl PwmRyankurte for CTIMER<Enabled, Attached, Attached, Detached> {
type Error = Infallible;
type Channel = Channels12;
type Time = u32;
type Duty = u32;

fn try_disable(
&mut self,
channel: &Self::Channel,
) -> Result<(), Self::Error> {
match channel {
Self::Channel::Channel1 => self.channels.channel1.try_disable(),
Self::Channel::Channel2 => self.channels.channel2.try_disable(),
}
}

fn try_enable(
&mut self,
channel: &Self::Channel,
) -> Result<(), Self::Error> {
match channel {
Self::Channel::Channel1 => self.channels.channel1.try_enable(),
Self::Channel::Channel2 => self.channels.channel2.try_enable(),
}
}

fn try_get_period(&self) -> Result<Self::Time, Self::Error> {
Ok(self.get_period())
}

fn try_get_duty(
&self,
channel: &Self::Channel,
) -> Result<Self::Duty, Self::Error> {
match channel {
Self::Channel::Channel1 => self.channels.channel1.try_get_duty(),
Self::Channel::Channel2 => self.channels.channel2.try_get_duty(),
}
}

fn try_get_max_duty(&self) -> Result<Self::Duty, Self::Error> {
Ok(self.get_max_duty())
}

fn try_set_duty(
&mut self,
channel: &Self::Channel,
duty: Self::Duty,
) -> Result<(), Self::Error> {
match channel {
Self::Channel::Channel1 => {
self.channels.channel1.try_set_duty(duty)
}
Self::Channel::Channel2 => {
self.channels.channel2.try_set_duty(duty)
}
}
}

fn try_set_period<P>(&mut self, period: P) -> Result<(), Self::Error>
where
P: Into<Self::Time>,
{
Ok(self.set_period(period.into()))
}
}

impl PwmRyankurte for CTIMER<Enabled, Attached, Attached, Attached> {
type Error = Infallible;
type Channel = Channels123;
type Time = u32;
type Duty = u32;

fn try_disable(
&mut self,
channel: &Self::Channel,
) -> Result<(), Self::Error> {
match channel {
Self::Channel::Channel1 => self.channels.channel1.try_disable(),
Self::Channel::Channel2 => self.channels.channel2.try_disable(),
Self::Channel::Channel3 => self.channels.channel3.try_disable(),
}
}

fn try_enable(
&mut self,
channel: &Self::Channel,
) -> Result<(), Self::Error> {
match channel {
Self::Channel::Channel1 => self.channels.channel1.try_enable(),
Self::Channel::Channel2 => self.channels.channel2.try_enable(),
Self::Channel::Channel3 => self.channels.channel3.try_enable(),
}
}

fn try_get_period(&self) -> Result<Self::Time, Self::Error> {
Ok(self.get_period())
}

fn try_get_duty(
&self,
channel: &Self::Channel,
) -> Result<Self::Duty, Self::Error> {
match channel {
Self::Channel::Channel1 => self.channels.channel1.try_get_duty(),
Self::Channel::Channel2 => self.channels.channel2.try_get_duty(),
Self::Channel::Channel3 => self.channels.channel3.try_get_duty(),
}
}

fn try_get_max_duty(&self) -> Result<Self::Duty, Self::Error> {
Ok(self.get_max_duty())
}

fn try_set_duty(
&mut self,
channel: &Self::Channel,
duty: Self::Duty,
) -> Result<(), Self::Error> {
match channel {
Self::Channel::Channel1 => {
self.channels.channel1.try_set_duty(duty)
}
Self::Channel::Channel2 => {
self.channels.channel2.try_set_duty(duty)
}
Self::Channel::Channel3 => {
self.channels.channel3.try_set_duty(duty)
}
}
}

fn try_set_period<P>(&mut self, period: P) -> Result<(), Self::Error>
where
P: Into<Self::Time>,
{
Ok(self.set_period(period.into()))
}
}

/// The available channels, if only channel 1 is attached
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Channels1 {
Expand Down