Skip to content

Commit

Permalink
Add custom halt function that restarts the system after a delay
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Aug 22, 2024
1 parent 372c692 commit 67af0d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion firmware/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ embedded-graphics = "0.8"
embedded-hal = "1.0"
embedded-hal-async = "1.0"
embedded-hal-bus = { version = "0.2", features = ["async"] }
esp-backtrace = { version = "0.13", features = ["esp32c3", "panic-handler", "exception-handler", "println"] }
esp-backtrace = { version = "0.13", features = ["esp32c3", "custom-halt", "panic-handler", "exception-handler", "println"] }
esp-hal = { version = "0.19", features = ["esp32c3", "async", "log"] }
esp-hal-embassy = { version = "0.2", features = ["esp32c3", "log"] }
esp-println = { version = "0.10", features = ["esp32c3", "log"] }
Expand Down
24 changes: 24 additions & 0 deletions firmware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ use esp_hal::i2c::I2C;
use esp_hal::peripherals::Peripherals;
use esp_hal::prelude::*;
use esp_hal::rng::Rng;
use esp_hal::rtc_cntl::Rtc;
use esp_hal::system::SystemControl;
use esp_hal::timer::systimer::SystemTimer;
use esp_hal::timer::timg::TimerGroup;
use esp_hal::timer::{ErasedTimer, OneShotTimer, PeriodicTimer};
use esp_println::println;
use log::info;

// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
Expand All @@ -65,6 +67,28 @@ macro_rules! mk_static {
static VERSION_STR: &str = concat!("v", env!("CARGO_PKG_VERSION"));
static GIT_SHA_STR: &str = env!("GIT_SHORT_SHA");

/// Custom halt function for esp-backtrace. Called after panic was handled and should halt
/// or restart the system.
#[export_name = "custom_halt"]
unsafe fn halt() -> ! {
// System may be in any state at this time, thus everything is unsafe here. Stealing the
// peripherals handle allows us to try to notify the user about this abnormal state and
// restart the system. Any error should be ignored.
let peripherals = Peripherals::steal();

// TODO: Steal display driver and show a panic message to the user

// Restart automatically after a delay
println!("Restarting in 10 seconds...");
let mut rtc = Rtc::new(peripherals.LPWR, None);
rtc.rwdt.set_timeout(10_000.millis());
rtc.rwdt.unlisten();
rtc.rwdt.enable();
loop {
esp_hal::riscv::asm::wfi();
}
}

#[main]
async fn main(_spawner: Spawner) {
let peripherals = Peripherals::take();
Expand Down

0 comments on commit 67af0d8

Please sign in to comment.