aboutsummaryrefslogtreecommitdiff
path: root/embassy-rp
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2024-10-22 07:52:03 +0000
committerGitHub <[email protected]>2024-10-22 07:52:03 +0000
commit0c22d4cccb6efa2e18db52f0df8333dda7ee3abe (patch)
tree3db87587d61b58a41b9f70d3b1cdd5b5ececec8d /embassy-rp
parent6da53f082fadb888f8cd909618ba57d207e80715 (diff)
parent548f11d5aef37d5ab1edac6970ceb7bee4300f4f (diff)
Merge pull request #3433 from 1-rafael-1/rp-pwm-embedded-hal-traits
embassy_rp: implement pwm traits from embedded_hal
Diffstat (limited to 'embassy-rp')
-rw-r--r--embassy-rp/Cargo.toml8
-rw-r--r--embassy-rp/src/pwm.rs41
2 files changed, 45 insertions, 4 deletions
diff --git a/embassy-rp/Cargo.toml b/embassy-rp/Cargo.toml
index 547d64e43..3e61641ff 100644
--- a/embassy-rp/Cargo.toml
+++ b/embassy-rp/Cargo.toml
@@ -118,18 +118,18 @@ embassy-usb-driver = {version = "0.1.0", path = "../embassy-usb-driver" }
118atomic-polyfill = "1.0.1" 118atomic-polyfill = "1.0.1"
119defmt = { version = "0.3", optional = true } 119defmt = { version = "0.3", optional = true }
120log = { version = "0.4.14", optional = true } 120log = { version = "0.4.14", optional = true }
121nb = "1.0.0" 121nb = "1.1.0"
122cfg-if = "1.0.0" 122cfg-if = "1.0.0"
123cortex-m-rt = ">=0.6.15,<0.8" 123cortex-m-rt = ">=0.6.15,<0.8"
124cortex-m = "0.7.6" 124cortex-m = "0.7.6"
125critical-section = "1.1" 125critical-section = "1.2.0"
126chrono = { version = "0.4", default-features = false, optional = true } 126chrono = { version = "0.4", default-features = false, optional = true }
127embedded-io = { version = "0.6.1" } 127embedded-io = { version = "0.6.1" }
128embedded-io-async = { version = "0.6.1" } 128embedded-io-async = { version = "0.6.1" }
129embedded-storage = { version = "0.3" } 129embedded-storage = { version = "0.3" }
130embedded-storage-async = { version = "0.4.1" } 130embedded-storage-async = { version = "0.4.1" }
131rand_core = "0.6.4" 131rand_core = "0.6.4"
132fixed = "1.23.1" 132fixed = "1.28.0"
133 133
134rp-pac = { git = "https://github.com/embassy-rs/rp-pac.git", rev = "a7f42d25517f7124ad3b4ed492dec8b0f50a0e6c", feature = ["rt"] } 134rp-pac = { git = "https://github.com/embassy-rs/rp-pac.git", rev = "a7f42d25517f7124ad3b4ed492dec8b0f50a0e6c", feature = ["rt"] }
135 135
@@ -141,7 +141,7 @@ embedded-hal-nb = { version = "1.0" }
141pio-proc = {version= "0.2" } 141pio-proc = {version= "0.2" }
142pio = {version= "0.2.1" } 142pio = {version= "0.2.1" }
143rp2040-boot2 = "0.3" 143rp2040-boot2 = "0.3"
144document-features = "0.2.7" 144document-features = "0.2.10"
145sha2-const-stable = "0.1" 145sha2-const-stable = "0.1"
146rp-binary-info = { version = "0.1.0", optional = true } 146rp-binary-info = { version = "0.1.0", optional = true }
147smart-leds = "0.4.0" 147smart-leds = "0.4.0"
diff --git a/embassy-rp/src/pwm.rs b/embassy-rp/src/pwm.rs
index 027f5504e..cfb99c569 100644
--- a/embassy-rp/src/pwm.rs
+++ b/embassy-rp/src/pwm.rs
@@ -1,6 +1,8 @@
1//! Pulse Width Modulation (PWM) 1//! Pulse Width Modulation (PWM)
2 2
3use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef}; 3use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};
4pub use embedded_hal_1::pwm::SetDutyCycle;
5use embedded_hal_1::pwm::{Error, ErrorKind, ErrorType};
4use fixed::traits::ToFixed; 6use fixed::traits::ToFixed;
5use fixed::FixedU16; 7use fixed::FixedU16;
6use pac::pwm::regs::{ChDiv, Intr}; 8use pac::pwm::regs::{ChDiv, Intr};
@@ -80,6 +82,21 @@ impl From<InputMode> for Divmode {
80 } 82 }
81} 83}
82 84
85/// PWM error.
86#[derive(Debug)]
87pub enum PwmError {
88 /// Invalid Duty Cycle.
89 InvalidDutyCycle,
90}
91
92impl Error for PwmError {
93 fn kind(&self) -> ErrorKind {
94 match self {
95 PwmError::InvalidDutyCycle => ErrorKind::Other,
96 }
97 }
98}
99
83/// PWM driver. 100/// PWM driver.
84pub struct Pwm<'d> { 101pub struct Pwm<'d> {
85 pin_a: Option<PeripheralRef<'d, AnyPin>>, 102 pin_a: Option<PeripheralRef<'d, AnyPin>>,
@@ -87,6 +104,30 @@ pub struct Pwm<'d> {
87 slice: usize, 104 slice: usize,
88} 105}
89 106
107impl<'d> ErrorType for Pwm<'d> {
108 type Error = PwmError;
109}
110
111impl<'d> SetDutyCycle for Pwm<'d> {
112 fn max_duty_cycle(&self) -> u16 {
113 pac::PWM.ch(self.slice).top().read().top()
114 }
115
116 fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
117 let max_duty = self.max_duty_cycle();
118 if duty > max_duty {
119 return Err(PwmError::InvalidDutyCycle);
120 }
121
122 let p = pac::PWM.ch(self.slice);
123 p.cc().modify(|w| {
124 w.set_a(duty);
125 w.set_b(duty);
126 });
127 Ok(())
128 }
129}
130
90impl<'d> Pwm<'d> { 131impl<'d> Pwm<'d> {
91 fn new_inner( 132 fn new_inner(
92 slice: usize, 133 slice: usize,