2021-09-27 22:36:23 +10:00
|
|
|
//! This example toggles the GPIO25 pin, using a PIO program.
|
2021-09-25 23:18:39 +10:00
|
|
|
//!
|
2021-09-27 22:36:23 +10:00
|
|
|
//! If a LED is connected to that pin, like on a Pico board, the LED should blink.
|
2021-09-25 23:18:39 +10:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
|
|
|
use hal::gpio::{FunctionPio0, Pin};
|
|
|
|
use hal::pac;
|
2021-09-29 05:48:05 +10:00
|
|
|
use hal::pio::PIOExt;
|
2021-12-04 15:52:46 +11:00
|
|
|
use hal::Sio;
|
2021-09-25 23:18:39 +10:00
|
|
|
use panic_halt as _;
|
|
|
|
use rp2040_hal as hal;
|
|
|
|
|
2022-09-02 15:38:58 +10:00
|
|
|
/// The linker will place this boot block at the start of our program image. We
|
|
|
|
/// need this to help the ROM bootloader get our code up and running.
|
|
|
|
/// Note: This boot block is not necessary when using a rp-hal based BSP
|
|
|
|
/// as the BSPs already perform this step.
|
2021-09-25 23:18:39 +10:00
|
|
|
#[link_section = ".boot2"]
|
|
|
|
#[used]
|
2022-08-11 21:56:36 +10:00
|
|
|
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_GENERIC_03H;
|
2021-09-25 23:18:39 +10:00
|
|
|
|
2022-08-22 04:01:45 +10:00
|
|
|
/// 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]
|
2021-09-25 23:18:39 +10:00
|
|
|
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);
|
2021-09-26 23:51:48 +10:00
|
|
|
// Define begin of program loop
|
2021-09-25 23:18:39 +10:00
|
|
|
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);
|
2021-09-26 23:51:48 +10:00
|
|
|
// Define end of program loop
|
2021-09-25 23:18:39 +10:00
|
|
|
a.bind(&mut wrap_source);
|
2021-09-26 23:51:48 +10:00
|
|
|
// The labels wrap_target and wrap_source, as set above,
|
|
|
|
// define a loop which is executed repeatedly by the PIO
|
|
|
|
// state machine.
|
2021-09-25 23:18:39 +10:00
|
|
|
let program = a.assemble_with_wrap(wrap_source, wrap_target);
|
|
|
|
|
|
|
|
// Initialize and start PIO
|
2021-09-29 05:48:05 +10:00
|
|
|
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
|
|
|
|
let installed = pio.install(&program).unwrap();
|
2022-09-01 06:44:53 +10:00
|
|
|
let (int, frac) = (0, 0); // as slow as possible (0 is interpreted as 65536)
|
2021-09-30 17:07:15 +10:00
|
|
|
let (sm, _, _) = rp2040_hal::pio::PIOBuilder::from_program(installed)
|
2021-09-25 23:18:39 +10:00
|
|
|
.set_pins(led_pin_id, 1)
|
2022-09-01 06:44:53 +10:00
|
|
|
.clock_divisor_fixed_point(int, frac)
|
2021-09-29 05:48:05 +10:00
|
|
|
.build(sm0);
|
|
|
|
sm.start();
|
2021-09-25 23:18:39 +10:00
|
|
|
|
|
|
|
// PIO runs in background, independently from CPU
|
2022-08-02 00:50:48 +10:00
|
|
|
loop {
|
|
|
|
cortex_m::asm::wfi();
|
|
|
|
}
|
2021-09-25 23:18:39 +10:00
|
|
|
}
|