aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorrafael <[email protected]>2024-10-21 22:42:18 +0200
committerrafael <[email protected]>2024-10-21 22:42:18 +0200
commit14e69309ebe25a76f67c38411c7a80a4d83da5ed (patch)
treede14c04891b7f148565cae50d81b5e9f3015675e /examples
parent8dfc9ba1a3e3f69aedf5bce748783fb6a8f5e92e (diff)
add pwm frequency to examples
Diffstat (limited to 'examples')
-rw-r--r--examples/rp/src/bin/pwm.rs10
-rw-r--r--examples/rp23/src/bin/pwm.rs10
-rw-r--r--examples/rp23/src/bin/pwm_tb6612fng_motor_driver.rs17
3 files changed, 23 insertions, 14 deletions
diff --git a/examples/rp/src/bin/pwm.rs b/examples/rp/src/bin/pwm.rs
index 862a7da22..791b88b5b 100644
--- a/examples/rp/src/bin/pwm.rs
+++ b/examples/rp/src/bin/pwm.rs
@@ -45,8 +45,14 @@ async fn pwm_set_config(slice4: PWM_SLICE4, pin25: PIN_25) {
45/// Using GP4 in Slice2, make sure to use an appropriate resistor. 45/// Using GP4 in Slice2, make sure to use an appropriate resistor.
46#[embassy_executor::task] 46#[embassy_executor::task]
47async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) { 47async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
48 // If we aim for a specific frequency, here is how we can calculate the top value.
49 // The top value sets the period of the PWM cycle, so a counter goes from 0 to top and then wraps around to 0.
50 // Every such wraparound is one PWM cycle. So here is how we get 25KHz:
48 let mut c = Config::default(); 51 let mut c = Config::default();
49 c.top = 32_768; 52 let pwm_freq = 25_000; // Hz, our desired frequency
53 let clock_freq = embassy_rp::clocks::clk_sys_freq();
54 c.top = (clock_freq / pwm_freq) as u16 - 1;
55
50 let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone()); 56 let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
51 57
52 loop { 58 loop {
@@ -59,7 +65,7 @@ async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
59 Timer::after_secs(1).await; 65 Timer::after_secs(1).await;
60 66
61 // 25% duty cycle. Expressed as 32768/4 = 8192. 67 // 25% duty cycle. Expressed as 32768/4 = 8192.
62 pwm.set_duty_cycle(8_192).unwrap(); 68 pwm.set_duty_cycle(c.top / 4).unwrap();
63 Timer::after_secs(1).await; 69 Timer::after_secs(1).await;
64 70
65 // 0% duty cycle, fully off. 71 // 0% duty cycle, fully off.
diff --git a/examples/rp23/src/bin/pwm.rs b/examples/rp23/src/bin/pwm.rs
index 838eee625..5a4457158 100644
--- a/examples/rp23/src/bin/pwm.rs
+++ b/examples/rp23/src/bin/pwm.rs
@@ -50,8 +50,14 @@ async fn pwm_set_config(slice4: PWM_SLICE4, pin25: PIN_25) {
50/// Using GP4 in Slice2, make sure to use an appropriate resistor. 50/// Using GP4 in Slice2, make sure to use an appropriate resistor.
51#[embassy_executor::task] 51#[embassy_executor::task]
52async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) { 52async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
53 // If we aim for a specific frequency, here is how we can calculate the top value.
54 // The top value sets the period of the PWM cycle, so a counter goes from 0 to top and then wraps around to 0.
55 // Every such wraparound is one PWM cycle. So here is how we get 25KHz:
53 let mut c = Config::default(); 56 let mut c = Config::default();
54 c.top = 32_768; 57 let pwm_freq = 25_000; // Hz, our desired frequency
58 let clock_freq = embassy_rp::clocks::clk_sys_freq();
59 c.top = (clock_freq / pwm_freq) as u16 - 1;
60
55 let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone()); 61 let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
56 62
57 loop { 63 loop {
@@ -64,7 +70,7 @@ async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
64 Timer::after_secs(1).await; 70 Timer::after_secs(1).await;
65 71
66 // 25% duty cycle. Expressed as 32768/4 = 8192. 72 // 25% duty cycle. Expressed as 32768/4 = 8192.
67 pwm.set_duty_cycle(8_192).unwrap(); 73 pwm.set_duty_cycle(c.top / 4).unwrap();
68 Timer::after_secs(1).await; 74 Timer::after_secs(1).await;
69 75
70 // 0% duty cycle, fully off. 76 // 0% duty cycle, fully off.
diff --git a/examples/rp23/src/bin/pwm_tb6612fng_motor_driver.rs b/examples/rp23/src/bin/pwm_tb6612fng_motor_driver.rs
index 6c3a8998c..263b551de 100644
--- a/examples/rp23/src/bin/pwm_tb6612fng_motor_driver.rs
+++ b/examples/rp23/src/bin/pwm_tb6612fng_motor_driver.rs
@@ -16,12 +16,6 @@ use embassy_time::{Duration, Timer};
16use tb6612fng::{DriveCommand, Motor, Tb6612fng}; 16use tb6612fng::{DriveCommand, Motor, Tb6612fng};
17use {defmt_rtt as _, panic_probe as _}; 17use {defmt_rtt as _, panic_probe as _};
18 18
19/// Maximum PWM value (fully on)
20const PWM_MAX: u16 = 50000;
21
22/// Minimum PWM value (fully off)
23const PWM_MIN: u16 = 0;
24
25#[link_section = ".start_block"] 19#[link_section = ".start_block"]
26#[used] 20#[used]
27pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe(); 21pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe();
@@ -46,6 +40,11 @@ async fn main(_spawner: Spawner) {
46 let s = split_resources!(p); 40 let s = split_resources!(p);
47 let r = s.motor; 41 let r = s.motor;
48 42
43 // we want a PWM frequency of 25KHz
44 let pwm_freq = 25_000; // Hz, our desired frequency
45 let clock_freq = embassy_rp::clocks::clk_sys_freq();
46 let period = (clock_freq / pwm_freq) as u16 - 1;
47
49 // we need a standby output and two motors to construct a full TB6612FNG 48 // we need a standby output and two motors to construct a full TB6612FNG
50 49
51 // standby pin 50 // standby pin
@@ -55,8 +54,7 @@ async fn main(_spawner: Spawner) {
55 let left_fwd = gpio::Output::new(r.left_forward_pin, gpio::Level::Low); 54 let left_fwd = gpio::Output::new(r.left_forward_pin, gpio::Level::Low);
56 let left_bckw = gpio::Output::new(r.left_backward_pin, gpio::Level::Low); 55 let left_bckw = gpio::Output::new(r.left_backward_pin, gpio::Level::Low);
57 let mut left_speed = pwm::Config::default(); 56 let mut left_speed = pwm::Config::default();
58 left_speed.top = PWM_MAX; 57 left_speed.top = period;
59 left_speed.compare_a = PWM_MIN;
60 let left_pwm = pwm::Pwm::new_output_a(r.left_slice, r.left_pwm_pin, left_speed); 58 let left_pwm = pwm::Pwm::new_output_a(r.left_slice, r.left_pwm_pin, left_speed);
61 let left_motor = Motor::new(left_fwd, left_bckw, left_pwm).unwrap(); 59 let left_motor = Motor::new(left_fwd, left_bckw, left_pwm).unwrap();
62 60
@@ -64,8 +62,7 @@ async fn main(_spawner: Spawner) {
64 let right_fwd = gpio::Output::new(r.right_forward_pin, gpio::Level::Low); 62 let right_fwd = gpio::Output::new(r.right_forward_pin, gpio::Level::Low);
65 let right_bckw = gpio::Output::new(r.right_backward_pin, gpio::Level::Low); 63 let right_bckw = gpio::Output::new(r.right_backward_pin, gpio::Level::Low);
66 let mut right_speed = pwm::Config::default(); 64 let mut right_speed = pwm::Config::default();
67 right_speed.top = PWM_MAX; 65 right_speed.top = period;
68 right_speed.compare_b = PWM_MIN;
69 let right_pwm = pwm::Pwm::new_output_b(r.right_slice, r.right_pwm_pin, right_speed); 66 let right_pwm = pwm::Pwm::new_output_b(r.right_slice, r.right_pwm_pin, right_speed);
70 let right_motor = Motor::new(right_fwd, right_bckw, right_pwm).unwrap(); 67 let right_motor = Motor::new(right_fwd, right_bckw, right_pwm).unwrap();
71 68