aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjorn <[email protected]>2024-10-09 10:12:43 -0700
committerBjorn <[email protected]>2024-10-09 10:12:43 -0700
commit22fe4932577cf261a561e33c2a8378c168fab1dd (patch)
tree4efdbf6e9e6c6809190dc5d0a7e401428019c023
parente47c031b671555f3fffe6b128cbb9d3f8bfec534 (diff)
Better docs and adding of release for PioPwm
-rw-r--r--embassy-rp/src/pio_programs/pwm.rs19
1 files changed, 14 insertions, 5 deletions
diff --git a/embassy-rp/src/pio_programs/pwm.rs b/embassy-rp/src/pio_programs/pwm.rs
index 8a0f3d5ee..7b3157877 100644
--- a/embassy-rp/src/pio_programs/pwm.rs
+++ b/embassy-rp/src/pio_programs/pwm.rs
@@ -1,6 +1,7 @@
1//! PIO backed PWM driver 1//! PIO backed PWM driver
2 2
3use core::time::Duration; 3use core::time::Duration;
4use crate::pio::Pin;
4 5
5use pio::InstructionOperands; 6use pio::InstructionOperands;
6 7
@@ -8,6 +9,7 @@ use crate::clocks;
8use crate::gpio::Level; 9use crate::gpio::Level;
9use crate::pio::{Common, Config, Direction, Instance, LoadedProgram, PioPin, StateMachine}; 10use crate::pio::{Common, Config, Direction, Instance, LoadedProgram, PioPin, StateMachine};
10 11
12/// This converts the duration provided into the number of cycles the PIO needs to run to make it take the same time
11fn to_pio_cycles(duration: Duration) -> u32 { 13fn to_pio_cycles(duration: Duration) -> u32 {
12 (clocks::clk_sys_freq() / 1_000_000) / 3 * duration.as_micros() as u32 // parentheses are required to prevent overflow 14 (clocks::clk_sys_freq() / 1_000_000) / 3 * duration.as_micros() as u32 // parentheses are required to prevent overflow
13} 15}
@@ -43,6 +45,7 @@ impl<'a, PIO: Instance> PioPwmProgram<'a, PIO> {
43/// Pio backed PWM output 45/// Pio backed PWM output
44pub struct PioPwm<'d, T: Instance, const SM: usize> { 46pub struct PioPwm<'d, T: Instance, const SM: usize> {
45 sm: StateMachine<'d, T, SM>, 47 sm: StateMachine<'d, T, SM>,
48 pin: Pin<'d, T>
46} 49}
47 50
48impl<'d, T: Instance, const SM: usize> PioPwm<'d, T, SM> { 51impl<'d, T: Instance, const SM: usize> PioPwm<'d, T, SM> {
@@ -62,20 +65,20 @@ impl<'d, T: Instance, const SM: usize> PioPwm<'d, T, SM> {
62 65
63 sm.set_config(&cfg); 66 sm.set_config(&cfg);
64 67
65 Self { sm } 68 Self { sm, pin }
66 } 69 }
67 70
68 /// Enable PWM output 71 /// Enable's the PIO program, continuing the wave generation from the PIO program.
69 pub fn start(&mut self) { 72 pub fn start(&mut self) {
70 self.sm.set_enable(true); 73 self.sm.set_enable(true);
71 } 74 }
72 75
73 /// Disable PWM output 76 /// Stops the PIO program, ceasing all signals from the PIN that were generated via PIO.
74 pub fn stop(&mut self) { 77 pub fn stop(&mut self) {
75 self.sm.set_enable(false); 78 self.sm.set_enable(false);
76 } 79 }
77 80
78 /// Set pwm period 81 /// Sets the pwm period, which is the length of time for each pio wave until reset.
79 pub fn set_period(&mut self, duration: Duration) { 82 pub fn set_period(&mut self, duration: Duration) {
80 let is_enabled = self.sm.is_enabled(); 83 let is_enabled = self.sm.is_enabled();
81 while !self.sm.tx().empty() {} // Make sure that the queue is empty 84 while !self.sm.tx().empty() {} // Make sure that the queue is empty
@@ -102,7 +105,8 @@ impl<'d, T: Instance, const SM: usize> PioPwm<'d, T, SM> {
102 } 105 }
103 } 106 }
104 107
105 fn set_level(&mut self, level: u32) { 108 /// Set the number of pio cycles to set the wave on high to.
109 pub fn set_level(&mut self, level: u32) {
106 self.sm.tx().push(level); 110 self.sm.tx().push(level);
107 } 111 }
108 112
@@ -110,4 +114,9 @@ impl<'d, T: Instance, const SM: usize> PioPwm<'d, T, SM> {
110 pub fn write(&mut self, duration: Duration) { 114 pub fn write(&mut self, duration: Duration) {
111 self.set_level(to_pio_cycles(duration)); 115 self.set_level(to_pio_cycles(duration));
112 } 116 }
117
118 // Return the state machine and pin.
119 pub fn release(self) -> (StateMachine<'d, T, SM>, Pin<'d, T>) {
120 (self.sm, self.pin)
121 }
113} 122}