From b79f859d721ed8ec3c7ce88a3a80479600c1c1b7 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 25 Sep 2021 13:18:39 +0000 Subject: [PATCH] Add a simple example for PIO. --- rp2040-hal/examples/pio_blink.rs | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 rp2040-hal/examples/pio_blink.rs diff --git a/rp2040-hal/examples/pio_blink.rs b/rp2040-hal/examples/pio_blink.rs new file mode 100644 index 0000000..04940f3 --- /dev/null +++ b/rp2040-hal/examples/pio_blink.rs @@ -0,0 +1,65 @@ +//! Blinks the LED on a Pico board, using a PIO program +//! +//! This will blink an LED attached to GP25, which is the pin the Pico uses for the on-board LED. +#![no_std] +#![no_main] + +use cortex_m_rt::entry; +use hal::gpio::{FunctionPio0, Pin}; +use hal::pac; +use hal::sio::Sio; +use panic_halt as _; +use rp2040_hal as hal; + +#[link_section = ".boot2"] +#[used] +pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER; + +#[entry] +fn main() -> ! { + let mut pac = pac::Peripherals::take().unwrap(); + + let sio = Sio::new(pac.SIO); + let pins = hal::gpio::Pins::new( + pac.IO_BANK0, + pac.PADS_BANK0, + sio.gpio_bank0, + &mut pac.RESETS, + ); + + // configure LED pin for Pio0. + let _led: Pin<_, FunctionPio0> = pins.gpio25.into_mode(); + // PIN id for use inside of PIO + let led_pin_id = 25; + + // Define some simple PIO program. + const MAX_DELAY: u8 = 31; + let mut a = pio::Assembler::<32>::new(); + let mut wrap_target = a.label(); + let mut wrap_source = a.label(); + // Set pin as Out + a.set(pio::SetDestination::PINDIRS, 1); + a.bind(&mut wrap_target); + // Set pin low + a.set_with_delay(pio::SetDestination::PINS, 0, MAX_DELAY); + // Set pin high + a.set_with_delay(pio::SetDestination::PINS, 1, MAX_DELAY); + // NOP + a.bind(&mut wrap_source); + let program = a.assemble_with_wrap(wrap_source, wrap_target); + + // Initialize and start PIO + let pio = rp2040_hal::pio::PIO::new(pac.PIO0, &mut pac.RESETS); + let sm = &pio.state_machines()[0]; + let div = 0f32; // as slow as possible (0 is interpreted as 65536) + rp2040_hal::pio::PIOBuilder::default() + .with_program(&program) + .set_pins(led_pin_id, 1) + .clock_divisor(div) + .build(&pio, sm) + .unwrap(); + + // PIO runs in background, independently from CPU + #[allow(clippy::empty_loop)] + loop {} +}