From 3054576b029c2e099ed2fd701c60e17614b97cad Mon Sep 17 00:00:00 2001 From: Radu Date: Mon, 21 Oct 2024 17:33:00 +0300 Subject: [PATCH 1/2] Final update. --- boards/pico_explorer_base/src/main.rs | 11 +++++++++-- chips/rp2040/src/pio_pwm.rs | 6 ++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/boards/pico_explorer_base/src/main.rs b/boards/pico_explorer_base/src/main.rs index e5fc458f1c..cd13ec541a 100644 --- a/boards/pico_explorer_base/src/main.rs +++ b/boards/pico_explorer_base/src/main.rs @@ -35,7 +35,7 @@ use rp2040::clocks::{ SystemAuxiliaryClockSource, SystemClockSource, UsbAuxiliaryClockSource, }; use rp2040::gpio::{GpioFunction, RPGpio, RPGpioPin}; -use rp2040::pio::{PIONumber, Pio, SMNumber, StateMachineConfiguration}; +use rp2040::pio::Pio; use rp2040::pio_pwm::PioPwm; use rp2040::resets::Peripheral; use rp2040::spi::Spi; @@ -702,7 +702,14 @@ pub unsafe fn start() -> ( let pio_pwm = PioPwm::new(&mut pio); pio_pwm.set_clocks(&peripherals.clocks); - pio_pwm.start(&RPGpio::GPIO7, 12_500_000, 50).unwrap(); + // This will start a PWM with PIO with the set frequency and duty cycle on the specified pin. + // pio_pwm + // .start( + // &RPGpio::GPIO7, + // pio_pwm.get_maximum_frequency_hz() / 125000, /*1_000*/ + // pio_pwm.get_maximum_duty_cycle() / 2, + // ) + // .unwrap(); (board_kernel, pico_explorer_base, chip) } diff --git a/chips/rp2040/src/pio_pwm.rs b/chips/rp2040/src/pio_pwm.rs index c3367eaba3..ebdc26f60d 100644 --- a/chips/rp2040/src/pio_pwm.rs +++ b/chips/rp2040/src/pio_pwm.rs @@ -69,7 +69,7 @@ impl<'a> hil::pwm::Pwm for PioPwm<'a> { custom_config.side_set_opt_enable = true; custom_config.side_set_pindirs = false; let max_freq = self.get_maximum_frequency_hz(); - let pwm_period = (max_freq / frequency_hz) as u32; + let pwm_period = ((max_freq / frequency_hz) / 3) as u32; let sm_number = SMNumber::SM0; let duty_cycle = duty_cycle_percentage as u32; pio.pwm_program_init( @@ -94,10 +94,12 @@ impl<'a> hil::pwm::Pwm for PioPwm<'a> { } fn get_maximum_duty_cycle(&self) -> usize { - // being a percentage, max duty cycle is 100 + // being a percentage out of 10000, max duty cycle is 10000 10000 } + // For the rp2040, this will always return 125_000_000. Watch out as any value above + // 1_000_000 is not precise and WILL give modified frequency and duty cycle values. fn get_maximum_frequency_hz(&self) -> usize { self.clocks .unwrap_or_panic() From 00d99a81cd8aed2f7cff4afd4653fd5fddbce9b8 Mon Sep 17 00:00:00 2001 From: Radu Date: Tue, 22 Oct 2024 20:11:23 +0300 Subject: [PATCH 2/2] Fix unused import --- boards/pico_explorer_base/src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/boards/pico_explorer_base/src/main.rs b/boards/pico_explorer_base/src/main.rs index cd13ec541a..56d147626b 100644 --- a/boards/pico_explorer_base/src/main.rs +++ b/boards/pico_explorer_base/src/main.rs @@ -20,7 +20,6 @@ use components::led::LedsComponent; use enum_primitive::cast::FromPrimitive; use kernel::component::Component; use kernel::hil::led::LedHigh; -use kernel::hil::pwm::Pwm; use kernel::hil::usb::Client; use kernel::platform::{KernelResources, SyscallDriverLookup}; use kernel::scheduler::round_robin::RoundRobinSched;