rp-hal-boards/boards/pico/examples/pico_gpio_in_out.rs
2021-09-27 17:30:50 +01:00

82 lines
2.3 KiB
Rust

//! # Pico GPIO In/Out Example
//!
//! Toggles the LED based on GPIO input.
//!
//! This will control an LED on GP25 based on a button hooked up to GP15. The
//! button should cause the line to be grounded, as the input pin is pulled high
//! internally by this example. When the button is pressed, the LED will turn
//! off.
//!
//! See the `Cargo.toml` file for Copyright and licence details.
#![no_std]
#![no_main]
// The macro for our start-up function
use cortex_m_rt::entry;
// GPIO traits
use embedded_hal::digital::v2::{InputPin, OutputPin};
// Ensure we halt the program on panic (if we don't mention this crate it won't
// be linked)
use panic_halt as _;
// A shorter alias for the Peripheral Access Crate, which provides low-level
// register access
use pico::hal::pac;
// A shorter alias for the Hardware Abstraction Layer, which provides
// higher-level drivers.
use pico::hal;
//// 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.
#[link_section = ".boot2"]
#[used]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER;
/// Entry point to our bare-metal application.
///
/// The `#[entry]` macro ensures the Cortex-M start-up code calls this function
/// as soon as all global variables are initialised.
///
/// The function configures the RP2040 peripherals, then just reads the button
/// and sets the LED appropriately.
#[entry]
fn main() -> ! {
// Grab our singleton objects
let mut pac = pac::Peripherals::take().unwrap();
// Note - we don't do any clock set-up in this example. The RP2040 will run
// at it's default clock speed.
// The single-cycle I/O block controls our GPIO pins
let sio = hal::sio::Sio::new(pac.SIO);
// Set the pins up according to their function on this particular board
let pins = pico::Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);
// Our LED output
let mut led_pin = pins.led.into_push_pull_output();
// Our button input
let button_pin = pins.bootsel.into_pull_down_input();
// Run forever, setting the LED according to the button
loop {
if button_pin.is_low().unwrap() {
led_pin.set_high().unwrap();
} else {
led_pin.set_low().unwrap();
}
}
}
// End of file