From f1c1d942378bcf968200201c138a39499a02961a Mon Sep 17 00:00:00 2001 From: Nikita Sivakov Date: Sat, 4 Nov 2023 00:03:06 +0100 Subject: [PATCH] Toggle pin with delay example --- Cargo.toml | 1 + examples/toggle_pin.rs | 62 ++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 52 +---------------------------------- 3 files changed, 64 insertions(+), 51 deletions(-) create mode 100644 examples/toggle_pin.rs diff --git a/Cargo.toml b/Cargo.toml index 1ad2451..6b6f009 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ cortex-m-rt = "0.7" defmt = "0.3" defmt-rtt = "0.4" embedded-hal = { version = "0.2", features = ["unproven"] } +fugit = "0.3" panic-probe = { version = "0.3", features = ["print-defmt"] } # Board support for RP Pico rp-pico = "0.8" diff --git a/examples/toggle_pin.rs b/examples/toggle_pin.rs new file mode 100644 index 0000000..56a5a6e --- /dev/null +++ b/examples/toggle_pin.rs @@ -0,0 +1,62 @@ +#![no_main] +#![no_std] + +use rp_pico as bsp; + +use bsp::entry; +use defmt_rtt as _; +use panic_probe as _; + +use bsp::hal::{ + clocks::{init_clocks_and_plls, Clock}, + pac, + sio::Sio, + watchdog::Watchdog, +}; +use defmt::info; +use embedded_hal::digital::v2::OutputPin; + +const DELAY_MS: u32 = 2_000; + +#[entry] +fn main() -> ! { + info!("Running toggle_pin program"); + + let mut pac = pac::Peripherals::take().unwrap(); + let core = pac::CorePeripherals::take().unwrap(); + let mut watchdog = Watchdog::new(pac.WATCHDOG); + let sio = Sio::new(pac.SIO); + + let clocks = init_clocks_and_plls( + bsp::XOSC_CRYSTAL_FREQ, + pac.XOSC, + pac.CLOCKS, + pac.PLL_SYS, + pac.PLL_USB, + &mut pac.RESETS, + &mut watchdog, + ) + .ok() + .unwrap(); + + let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz()); + + let pins = bsp::Pins::new( + pac.IO_BANK0, + pac.PADS_BANK0, + sio.gpio_bank0, + &mut pac.RESETS, + ); + + let mut pin = pins.gpio15.into_push_pull_output(); + + let mut state = false; + loop { + info!("Toogle pin"); + + state = !state; + pin.set_state(state.into()).unwrap(); + + delay.delay_ms(DELAY_MS); + } +} diff --git a/src/main.rs b/src/main.rs index 7835782..c7fc27f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,57 +7,7 @@ use bsp::entry; use defmt_rtt as _; use panic_probe as _; -use bsp::hal::{ - clocks::{init_clocks_and_plls, Clock}, - pac, - sio::Sio, - watchdog::Watchdog, -}; -use defmt::info; -use embedded_hal::digital::v2::OutputPin; - -const DELAY_MS: u32 = 2_000; - #[entry] fn main() -> ! { - info!("Running program"); - - let mut pac = pac::Peripherals::take().unwrap(); - let core = pac::CorePeripherals::take().unwrap(); - let mut watchdog = Watchdog::new(pac.WATCHDOG); - let sio = Sio::new(pac.SIO); - - let clocks = init_clocks_and_plls( - bsp::XOSC_CRYSTAL_FREQ, - pac.XOSC, - pac.CLOCKS, - pac.PLL_SYS, - pac.PLL_USB, - &mut pac.RESETS, - &mut watchdog, - ) - .ok() - .unwrap(); - - let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz()); - - let pins = bsp::Pins::new( - pac.IO_BANK0, - pac.PADS_BANK0, - sio.gpio_bank0, - &mut pac.RESETS, - ); - - let mut pin = pins.gpio15.into_push_pull_output(); - - let mut state = false; - loop { - state = !state; - - info!("Turn pin power on: {:?}", state); - pin.set_state(state.into()).unwrap(); - - info!("Sleep for {:?} ms", DELAY_MS); - delay.delay_ms(DELAY_MS); - } + loop {} }