Skip to content

Instantly share code, notes, and snippets.

@9names
Last active January 5, 2023 23:16
Show Gist options
  • Save 9names/1777d28d2cd5d18fffe894366a9dd052 to your computer and use it in GitHub Desktop.
Save 9names/1777d28d2cd5d18fffe894366a9dd052 to your computer and use it in GitHub Desktop.
watchdog spinlock test
//! Blinks the LED on a Pico board
//!
//! This will blink an LED attached to GP25, which is the pin the Pico uses for the on-board LED.
#![no_std]
#![no_main]
use cortex_m::prelude::_embedded_hal_watchdog_Watchdog;
use cortex_m_rt::entry;
use defmt::*;
use defmt_rtt as _;
use embedded_hal::digital::v2::OutputPin;
use panic_probe as _;
// Provide an alias for our BSP so we can switch targets quickly.
// Uncomment the BSP you included in Cargo.toml, the rest of the code does not need to change.
use rp_pico as bsp;
// use sparkfun_pro_micro_rp2040 as bsp;
use bsp::hal::{
clocks::{init_clocks_and_plls, Clock},
pac,
sio::Sio,
watchdog::Watchdog,
};
use embedded_hal::watchdog::WatchdogEnable;
use fugit::ExtU32;
#[entry]
fn main() -> ! {
info!("Program start");
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);
// External high-speed crystal on the pico board is 12Mhz
let external_xtal_freq_hz = 12_000_000u32;
let clocks = init_clocks_and_plls(
external_xtal_freq_hz,
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 led_pin = pins.gpio20.into_push_pull_output();
// Leave the LED in a known state for 5 seconds
led_pin.set_high().unwrap();
watchdog.start(2_100.millis());
let mut counter = 10;
while counter != 0 {
counter = counter - 1;
watchdog.feed();
delay.delay_ms(500);
}
// Trap if the spinlock isn't free. Set the LED in the opposite state
unsafe {
if (*pac::SIO::ptr()).spinlock[0].read().bits() == 0 {
info!("spinlock was active on startup!");
led_pin.set_low().unwrap();
loop {}
}
}
loop {
//watchdog.feed();
info!("on!");
led_pin.set_high().unwrap();
delay.delay_ms(500);
info!("off!");
led_pin.set_low().unwrap();
delay.delay_ms(500);
}
}
// End of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment