Blink (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"
It adjusts the LED's brightness by changing the duty cycle(will be explained in pwm page) at regular intervals, creating a dimming effect.
The main code
Here is the complete code. Don't worry if you don't fully understand it yet. On the following pages, we'll dive into each concept in detail. We'll start by taking action, then explore how it works and the ideas behind it.
//! # PWM Blink Example //! //! If you have an LED connected to pin 25, it will fade the LED using the PWM //! peripheral. //! //! It may need to be adapted to your particular board layout and/or pin assignment. //! //! See the `Cargo.toml` file for Copyright and license details. #![no_std] #![no_main] // Ensure we halt the program on panic (if we don't mention this crate it won't // be linked) use panic_halt as _; // Alias for our HAL crate use rp235x_hal as hal; // Some things we need use embedded_hal::delay::DelayNs; use embedded_hal::pwm::SetDutyCycle; /// Tell the Boot ROM about our application #[link_section = ".start_block"] #[used] pub static IMAGE_DEF: hal::block::ImageDef = hal::block::ImageDef::secure_exe(); /// 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; /// 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; /// Entry point to our bare-metal application. /// /// The `#[hal::entry]` macro ensures the Cortex-M start-up code calls this function /// as soon as all global variables and the spinlock are initialised. /// /// The function configures the rp235x peripherals, then fades the LED in an /// infinite loop. #[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, ); // The delay object lets us wait for specified amounts of time (in // milliseconds) let mut delay = hal::Timer::new_timer0(pac.TIMER0, &mut pac.RESETS, &clocks); // 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); // Infinite loop, fading LED up and down loop { // Ramp brightness up for i in LOW..=HIGH { delay.delay_us(8); let _ = channel.set_duty_cycle(i); } // Ramp brightness down for i in (LOW..=HIGH).rev() { delay.delay_us(8); let _ = channel.set_duty_cycle(i); } delay.delay_ms(500); } } /// Program metadata for `picotool info` #[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"PWM Blinky Example"), 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 blinky
folder to run this version of the blink program:
git clone https://github.com/ImplFerris/pico2-rp-projects
cd pico2-projects/blinky
How to Run?
You refer the "Running The Program" section