Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring the watchdogs in the init config #2180

Merged
merged 16 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion esp-hal-embassy/MIGRATING-0.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ You no longer have to set up clocks and pass them to `esp_hal_embassy::init`.
prelude::*,
- system::SystemControl,
};

#[esp_hal_embassy::main]
async fn main(_spawner: Spawner) -> ! {
- let peripherals = Peripherals::take();
Expand Down
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Implement `TryFrom<u32>` for `ledc::timer::config::Duty` (#1984)
- Expose `RtcClock::get_xtal_freq` and `RtcClock::get_slow_freq` publically for all chips (#2183)
- TWAI support for ESP32-H2 (#2199)
- Added a way to configure watchdogs in `esp_hal::init` (#2180)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion esp-hal/MIGRATING-0.20.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ configure an input pin, and pass it to `set_edge_signal` or `set_ctrl_signal`.
- PcntInputConfig { pull: Pull::Up },
- ));
+ ch0.set_ctrl_signal(Input::new(io.pins.gpio4, Pull::Up));

- let mut pin_b = io.pins.gpio5;
- ch0.set_edge_signal(PcntSource::from_pin(
- &mut pin_b,
Expand Down
72 changes: 72 additions & 0 deletions esp-hal/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//! # Configuration
//!
//! ## Overview
//! This module contains the initial configuation for the system.
//!
//! ## Configuration
//! In the esp_hal::init method, we can configure different parameters for the
//! system:
//! - CPU clock configuration.
//! - Watchdog configuration.
//!
//! ## Example
//!
//! ### Default initialization
//!
//! ```rust, no_run
#![doc = crate::before_snippet!()]
//! let peripherals = esp_hal::init(esp_hal::Config::default());
//! # }
//! ```
//!
//! ### Custom initialization
//! ```rust, no_run
#![doc = crate::before_snippet!()]
//! let mut config = esp_hal::Config::default();
//! config.cpu_clock = CpuClock::max();
//! config.watchdog.rwdt =
//! esp_hal::config::WatchdogStatus::Enabled(fugit::MicrosDurationU64::millis(1000 as u64));
//! let peripherals = esp_hal::init(config);
//! # }
//! ```

use crate::clock::CpuClock;

/// Watchdog status.
#[derive(Default, PartialEq)]
pub enum WatchdogStatus {
/// Enables a watchdog timer with the specified timeout.
Enabled(fugit::MicrosDurationU64),
/// Disables the watchdog timer.
#[default]
Disabled,
}

/// Watchdogs configuration.
#[non_exhaustive]
#[derive(Default)]
pub struct WatchdogConfig {
#[cfg(not(any(esp32, esp32s2)))]
/// Enable the super watchdog timer, which is slightly less than one second.
pub swd: bool,
/// Configures the reset watchdog timer.
pub rwdt: WatchdogStatus,
/// Configures the timg0 watchdog timer.
pub timg0: WatchdogStatus,
#[cfg(timg1)]
/// Configures the timg1 watchdog timer.
///
/// By default, the bootloader does not enables this watchdog timer.
pub timg1: WatchdogStatus,
}

/// System configuration.
#[non_exhaustive]
#[doc(hidden)]
SergioGasquez marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Default)]
pub struct Config {
/// The CPU clock configuration.
pub cpu_clock: CpuClock,
/// Enable watchdog timer(s).
pub watchdog: WatchdogConfig,
}
63 changes: 44 additions & 19 deletions esp-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ pub mod analog;
pub mod assist_debug;
#[cfg(any(dport, hp_sys, pcr, system))]
pub mod clock;

pub mod config;
pub use config::Config;

#[cfg(any(xtensa, all(riscv, systimer)))]
pub mod delay;
#[cfg(any(gdma, pdma))]
Expand Down Expand Up @@ -727,35 +731,56 @@ macro_rules! before_snippet {
};
}

use crate::{
clock::{Clocks, CpuClock},
peripherals::Peripherals,
};

/// System configuration.
#[non_exhaustive]
#[derive(Default)]
pub struct Config {
/// The CPU clock configuration.
pub cpu_clock: CpuClock,
}
use crate::{clock::Clocks, config::WatchdogStatus, peripherals::Peripherals};

/// Initialize the system.
///
/// This function sets up the CPU clock and returns the peripherals and clocks.
/// This function sets up the CPU clock and watchdog, then, returns the
/// peripherals and clocks.
pub fn init(config: Config) -> Peripherals {
let mut peripherals = Peripherals::take();

// RTC domain must be enabled before we try to disable
let mut rtc = crate::rtc_cntl::Rtc::new(&mut peripherals.LPWR);

#[cfg(not(any(esp32, esp32s2)))]
rtc.swd.disable();
rtc.rwdt.disable();
if config.watchdog.swd {
rtc.swd.enable();
} else {
rtc.swd.disable();
}

unsafe {
crate::timer::timg::Wdt::<self::peripherals::TIMG0>::set_wdt_enabled(false);
#[cfg(timg1)]
crate::timer::timg::Wdt::<self::peripherals::TIMG1>::set_wdt_enabled(false);
match config.watchdog.rwdt {
WatchdogStatus::Enabled(duration) => {
rtc.rwdt.enable();
rtc.rwdt.set_timeout(duration);
}
WatchdogStatus::Disabled => {
rtc.rwdt.disable();
}
}

match config.watchdog.timg0 {
WatchdogStatus::Enabled(duration) => {
let mut timg0_wd = crate::timer::timg::Wdt::<self::peripherals::TIMG0>::new();
MabezDev marked this conversation as resolved.
Show resolved Hide resolved
timg0_wd.enable();
timg0_wd.set_timeout(duration);
}
WatchdogStatus::Disabled => {
crate::timer::timg::Wdt::<self::peripherals::TIMG0>::new().disable();
}
}

#[cfg(timg1)]
match config.watchdog.timg1 {
WatchdogStatus::Enabled(duration) => {
let mut timg1_wd = crate::timer::timg::Wdt::<self::peripherals::TIMG1>::new();
timg1_wd.enable();
timg1_wd.set_timeout(duration);
Comment on lines +790 to +792
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this play nice with

T::reset_peripheral();
?

}
WatchdogStatus::Disabled => {
crate::timer::timg::Wdt::<self::peripherals::TIMG1>::new().disable();
}
}

Clocks::init(config.cpu_clock);
Expand Down
5 changes: 5 additions & 0 deletions esp-hal/src/rtc_cntl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,11 @@ impl Swd {
Self
}

/// Enable the watchdog timer instance
pub fn enable(&mut self) {
self.set_enabled(true);
}

/// Disable the watchdog timer instance
pub fn disable(&mut self) {
self.set_enabled(false);
Expand Down
4 changes: 2 additions & 2 deletions esp-wifi/MIGRATING-0.9.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ You no longer have to set up clocks and pass them to `esp_wifi::initialize`.
initialize,
// ...
};

#[entry]
fn main() -> ! {
- let peripherals = Peripherals::take();
Expand Down Expand Up @@ -65,7 +65,7 @@ The size of the heap depends on what you are going to use esp-wifi for and if yo

E.g. when using `coex` you need around 92k. If not using `coex`, going lower than 72k you will observe some failed allocations but it might still work. Going even lower will make things fail.

If you see linker errors regarding undefined symbols for `esp_wifi_free_internal_heap` and `esp_wifi_allocate_from_internal_ram` you either want to opt-in to use the `esp-alloc` feature
If you see linker errors regarding undefined symbols for `esp_wifi_free_internal_heap` and `esp_wifi_allocate_from_internal_ram` you either want to opt-in to use the `esp-alloc` feature
or provide your own allocator (see below)

### Using your own allocator
Expand Down
4 changes: 4 additions & 0 deletions hil-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ harness = false
name = "i2c"
harness = false

[[test]]
name = "init"
harness = false

[[test]]
name = "i2s"
harness = false
Expand Down
110 changes: 110 additions & 0 deletions hil-test/tests/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//! Initialization tests

//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3

#![no_std]
#![no_main]

use esp_hal::{
config::WatchdogStatus,
delay::Delay,
prelude::*,
rtc_cntl::Rtc,
timer::timg::TimerGroup,
Config,
};
use hil_test as _;

#[cfg(test)]
#[embedded_test::tests]
mod tests {
use super::*;

#[test]
#[timeout(3)]
fn test_feeding_timg0_wdt() {
let peripherals = esp_hal::init({
SergioGasquez marked this conversation as resolved.
Show resolved Hide resolved
let mut config = Config::default();
config.watchdog.timg0 =
WatchdogStatus::Enabled(fugit::MicrosDurationU64::millis(500 as u64));
config
});

let timg0 = TimerGroup::new(peripherals.TIMG0);
MabezDev marked this conversation as resolved.
Show resolved Hide resolved
let mut wdt0 = timg0.wdt;
let delay = Delay::new();

for _ in 0..4 {
wdt0.feed();
delay.delay(250.millis());
}
}

#[test]
#[timeout(3)]
#[cfg(timg1)]
fn test_feeding_timg1_wdt() {
let peripherals = esp_hal::init({
let mut config = Config::default();
config.watchdog.timg1 =
WatchdogStatus::Enabled(fugit::MicrosDurationU64::millis(500 as u64));
config
});

let timg1 = TimerGroup::new(peripherals.TIMG0);
let mut wdt1 = timg1.wdt;
let delay = Delay::new();

for _ in 0..4 {
wdt1.feed();
delay.delay(250.millis());
}
}

#[test]
#[timeout(3)]
fn test_feeding_timg0_wdt_max_clock() {
let peripherals = esp_hal::init({
let mut config = Config::default();
config.cpu_clock = CpuClock::max();
config.watchdog.timg0 =
WatchdogStatus::Enabled(fugit::MicrosDurationU64::millis(500 as u64));
config
});

let timg0 = TimerGroup::new(peripherals.TIMG0);
let mut wdt0 = timg0.wdt;
let delay = Delay::new();

for _ in 0..4 {
wdt0.feed();
delay.delay(250.millis());
}
}

#[test]
#[timeout(4)]
fn test_feeding_rtc_wdt() {
let peripherals = esp_hal::init({
let mut config = Config::default();
config.watchdog.rwdt =
WatchdogStatus::Enabled(fugit::MicrosDurationU64::millis(3000 as u64));
config
});

let mut rtc = Rtc::new(peripherals.LPWR);
let delay = Delay::new();

rtc.rwdt.feed();
delay.delay(2500.millis());
}

#[test]
#[timeout(3)]
fn test_default_config() {
esp_hal::init(Config::default());

let delay = Delay::new();
delay.delay(2000.millis());
}
}
Loading