rp-hal-boards/rp2040-hal/examples/pio_blink.rs
Wilfried Chauveau 0e2b4cf7be
Add a method to allow setting the PIO's clock divisor without floats (#440)
* pio: Changes `PIOBuilder::clock_divisor` from f32 to fixed point)
* pio: mark clock_divisor as deprecated
2022-08-31 21:44:53 +01:00

76 lines
2.4 KiB
Rust

//! This example toggles the GPIO25 pin, using a PIO program.
//!
//! If a LED is connected to that pin, like on a Pico board, the LED should blink.
#![no_std]
#![no_main]
use hal::gpio::{FunctionPio0, Pin};
use hal::pac;
use hal::pio::PIOExt;
use hal::Sio;
use panic_halt as _;
use rp2040_hal as hal;
#[link_section = ".boot2"]
#[used]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_GENERIC_03H;
/// Entry point to our bare-metal application.
///
/// The `#[rp2040_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 RP2040 peripherals, then blinks an LED using the PIO peripheral.
#[rp2040_hal::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);
// Define begin of program loop
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);
// Define end of program loop
a.bind(&mut wrap_source);
// The labels wrap_target and wrap_source, as set above,
// define a loop which is executed repeatedly by the PIO
// state machine.
let program = a.assemble_with_wrap(wrap_source, wrap_target);
// Initialize and start PIO
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let installed = pio.install(&program).unwrap();
let (int, frac) = (0, 0); // as slow as possible (0 is interpreted as 65536)
let (sm, _, _) = rp2040_hal::pio::PIOBuilder::from_program(installed)
.set_pins(led_pin_id, 1)
.clock_divisor_fixed_point(int, frac)
.build(sm0);
sm.start();
// PIO runs in background, independently from CPU
loop {
cortex_m::asm::wfi();
}
}