Skip to content

Commit

Permalink
Toggle pin with delay example
Browse files Browse the repository at this point in the history
  • Loading branch information
sivakov512 committed Nov 3, 2023
1 parent c459db3 commit f1c1d94
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 51 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
62 changes: 62 additions & 0 deletions examples/toggle_pin.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
52 changes: 1 addition & 51 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}

0 comments on commit f1c1d94

Please sign in to comment.