Dimming LED Program with RP HAL
rp-hal is an Embedded-HAL for RP series microcontrollers, and can be used as an alternative to the Embassy framework for pico.
This example code is taken from rp235x-hal repo (It also includes additional examples beyond just the blink examples):
"https://github.com/rp-rs/rp-hal/tree/main/rp235x-hal-examples"
The main code
#![no_std] #![no_main] use embedded_hal::delay::DelayNs; use hal::block::ImageDef; use rp235x_hal as hal; // Traig for PWM use embedded_hal::pwm::SetDutyCycle; //Panic Handler use panic_probe as _; // Defmt Logging use defmt_rtt as _; /// Tell the Boot ROM about our application #[unsafe(link_section = ".start_block")] #[used] pub static IMAGE_DEF: ImageDef = hal::block::ImageDef::secure_exe(); /// External high-speed crystal on the Raspberry Pi Pico 2 board is 12 MHz. /// Adjust if your board has a different frequency const XTAL_FREQ_HZ: u32 = 12_000_000u32; /// The minimum PWM value (i.e. LED brightness) we want const LOW: u16 = 0; /// The maximum PWM value (i.e. LED brightness) we want const HIGH: u16 = 25000; #[hal::entry] fn main() -> ! { // Grab our singleton objects let mut pac = hal::pac::Peripherals::take().unwrap(); // Set up the watchdog driver - needed by the clock setup code let mut watchdog = hal::Watchdog::new(pac.WATCHDOG); // Configure the clocks // // The default is to generate a 125 MHz system clock let clocks = hal::clocks::init_clocks_and_plls( XTAL_FREQ_HZ, pac.XOSC, pac.CLOCKS, pac.PLL_SYS, pac.PLL_USB, &mut pac.RESETS, &mut watchdog, ) .ok() .unwrap(); // The single-cycle I/O block controls our GPIO pins let sio = hal::Sio::new(pac.SIO); // Set the pins up according to their function on this particular board let pins = hal::gpio::Pins::new( pac.IO_BANK0, pac.PADS_BANK0, sio.gpio_bank0, &mut pac.RESETS, ); // Init PWMs let mut pwm_slices = hal::pwm::Slices::new(pac.PWM, &mut pac.RESETS); // Configure PWM4 let pwm = &mut pwm_slices.pwm4; pwm.set_ph_correct(); pwm.enable(); // Output channel B on PWM4 to GPIO 25 let channel = &mut pwm.channel_b; channel.output_to(pins.gpio25); let mut timer = hal::Timer::new_timer0(pac.TIMER0, &mut pac.RESETS, &clocks); loop { for i in LOW..=HIGH { timer.delay_us(8); let _ = channel.set_duty_cycle(i); } for i in (LOW..=HIGH).rev() { timer.delay_us(8); let _ = channel.set_duty_cycle(i); } timer.delay_ms(500); } } // Program metadata for `picotool info`. // This isn't needed, but it's recomended to have these minimal entries. #[unsafe(link_section = ".bi_entries")] #[used] pub static PICOTOOL_ENTRIES: [hal::binary_info::EntryAddr; 5] = [ hal::binary_info::rp_cargo_bin_name!(), hal::binary_info::rp_cargo_version!(), hal::binary_info::rp_program_description!(c"your program description"), hal::binary_info::rp_cargo_homepage_url!(), hal::binary_info::rp_program_build_attribute!(), ]; // End of file
Clone the existing project
You can clone the blinky project I created and navigate to the led-dimming folder to run this version of the blink program:
git clone https://github.com/ImplFerris/pico2-rp-projects
cd pico2-projects/led-dimming