2021-09-21 19:47:43 +10:00
|
|
|
//! # Pico GPIO In/Out Example
|
2021-07-26 20:24:58 +10:00
|
|
|
//!
|
2021-09-21 19:47:43 +10:00
|
|
|
//! 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.
|
|
|
|
|
2021-07-26 20:24:58 +10:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
// The macro for our start-up function
|
2022-04-10 21:54:56 +10:00
|
|
|
use rp_pico::entry;
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// GPIO traits
|
2021-07-26 20:24:58 +10:00
|
|
|
use embedded_hal::digital::v2::{InputPin, OutputPin};
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
|
|
|
// be linked)
|
2021-07-26 20:24:58 +10:00
|
|
|
use panic_halt as _;
|
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
// A shorter alias for the Peripheral Access Crate, which provides low-level
|
|
|
|
// register access
|
2021-12-23 22:18:52 +11:00
|
|
|
use rp_pico::hal::pac;
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// A shorter alias for the Hardware Abstraction Layer, which provides
|
|
|
|
// higher-level drivers.
|
2021-12-23 22:18:52 +11:00
|
|
|
use rp_pico::hal;
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
/// 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.
|
2021-07-26 20:24:58 +10:00
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
2021-09-21 19:47:43 +10:00
|
|
|
// Grab our singleton objects
|
2021-07-26 20:24:58 +10:00
|
|
|
let mut pac = pac::Peripherals::take().unwrap();
|
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
// 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
|
2021-12-04 15:52:46 +11:00
|
|
|
let sio = hal::Sio::new(pac.SIO);
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// Set the pins up according to their function on this particular board
|
2021-12-23 22:18:52 +11:00
|
|
|
let pins = rp_pico::Pins::new(
|
2021-07-26 20:24:58 +10:00
|
|
|
pac.IO_BANK0,
|
|
|
|
pac.PADS_BANK0,
|
|
|
|
sio.gpio_bank0,
|
|
|
|
&mut pac.RESETS,
|
|
|
|
);
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// Our LED output
|
2021-07-26 20:24:58 +10:00
|
|
|
let mut led_pin = pins.led.into_push_pull_output();
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// Our button input
|
2021-11-17 09:47:30 +11:00
|
|
|
let button_pin = pins.gpio15.into_pull_up_input();
|
2021-07-26 20:24:58 +10:00
|
|
|
|
2021-09-21 19:47:43 +10:00
|
|
|
// Run forever, setting the LED according to the button
|
2021-07-26 20:24:58 +10:00
|
|
|
loop {
|
|
|
|
if button_pin.is_low().unwrap() {
|
|
|
|
led_pin.set_high().unwrap();
|
|
|
|
} else {
|
|
|
|
led_pin.set_low().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 19:47:43 +10:00
|
|
|
|
|
|
|
// End of file
|