rp-hal-boards/rp2040-hal/examples/gpio_in_out.rs
Jennifer Wilcox 2c3a0956fa Add input support, examples, SIO/PADS ownership
Sorry this is a large commit :(

This adds support for input pins, including pulling them high or low.

It also adds two examples: the start of a classic blinky LED example, and an example for reading input.
2021-04-24 16:18:57 -05:00

37 lines
979 B
Rust

//! Toggle LED based on GPIO input
//!
//! This will control an LED on GP25 based on a button hooked up to GP15. The button should be tied
//! to ground, as the input pin is pulled high internally by this example. When the button is
//! pressed, the LED will turn off.
#![no_std]
#![no_main]
use panic_halt as _;
use cortex_m_rt::entry;
#[link_section = ".boot2"]
#[used]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use rp2040_hal::prelude::*;
#[entry]
fn main() -> ! {
let mut pac = rp2040_pac::Peripherals::take().unwrap();
let pins = pac.IO_BANK0.split(pac.PADS_BANK0, pac.SIO, &mut pac.RESETS);
let mut led_pin = pins.gpio25.into_output();
let mut button_pin = pins.gpio15.into_input();
button_pin.pull_high();
loop {
if button_pin.is_high().unwrap() {
led_pin.set_high().unwrap();
} else {
led_pin.set_low().unwrap();
}
}
}