From 70e6bae41a1cf967bbcde5dbfc8b865f1b8937f5 Mon Sep 17 00:00:00 2001 From: Jacob Weaver Date: Mon, 16 Dec 2024 15:55:03 -0600 Subject: [PATCH] added rp2040 stuff --- Cargo.toml | 2 ++ apps/hs/Cargo.toml | 5 +++- apps/hs/src/infograbber.rs | 6 +++++ apps/hs/src/watchdog.rs | 43 ++++++++++++++++++++++++++++++ builds/rp_pico_example/Cargo.toml | 3 ++- builds/rp_pico_example/src/main.rs | 36 +++++++++++++++++++++---- rfe/Cargo.toml | 3 +++ rfe/src/time.rs | 25 +++++++++++++++++ 8 files changed, 116 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 96068ef..e6a4fd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,5 @@ bincode = { version = "2.0.0-rc.3", default-features = false, features = [ "derive", ] } simple_logger = "5.0.0" +rp2040-hal = "0.10.2" +rp2040-pac = "0.6.0" diff --git a/apps/hs/Cargo.toml b/apps/hs/Cargo.toml index 4287ae7..fb8a482 100644 --- a/apps/hs/Cargo.toml +++ b/apps/hs/Cargo.toml @@ -7,9 +7,10 @@ edition = "2021" path = "src/hs.rs" [features] -default = [] +default = ["rp2040"] std = ["dep:sysinfo"] nix = ["dep:watchdog-device"] +rp2040 = ["dep:rp2040-hal", "dep:rp2040-pac"] [dependencies] rfe = { path = "../../rfe" } @@ -21,3 +22,5 @@ sysinfo = { version = "0.32.0", default-features = false, optional = true, featu "disk", "system", ] } +rp2040-hal = { workspace = true, optional = true } +rp2040-pac = { workspace = true, optional = true } diff --git a/apps/hs/src/infograbber.rs b/apps/hs/src/infograbber.rs index 171e7c8..c54d005 100644 --- a/apps/hs/src/infograbber.rs +++ b/apps/hs/src/infograbber.rs @@ -75,6 +75,12 @@ mod infograbber_std { pub use infograbber_std::*; pub struct StubSystemInfoGrabber; +impl StubSystemInfoGrabber { + pub fn new() -> Self { + Self {} + } +} + impl SystemInfoGrabber for StubSystemInfoGrabber { fn check_cpu_usage(&mut self) -> Vec { Vec::new() diff --git a/apps/hs/src/watchdog.rs b/apps/hs/src/watchdog.rs index 277b6d3..5327bfa 100644 --- a/apps/hs/src/watchdog.rs +++ b/apps/hs/src/watchdog.rs @@ -82,3 +82,46 @@ impl<'a> Watchdog for WatchdogRef<'a> { } } } + +#[cfg(feature = "rp2040")] +mod watchdog_rp2040 { + use rp2040_hal::Watchdog; + + pub struct Rp2040Watchdog { + wd: Watchdog, + time: u32, + } + + impl Rp2040Watchdog { + pub fn new(wd: Watchdog) -> Self { + Self { + wd, + time: 10 * 1000000, + } + } + } + + impl super::Watchdog for Rp2040Watchdog { + fn enable(&mut self) { + self.wd + .start(rp2040_hal::fugit::Duration::::from_ticks( + self.time, + )); + } + + fn disable(&mut self) { + self.wd.disable(); + } + + fn set_timeout(&mut self, time: i32) { + self.time = time as u32 * 1000000; + } + + fn feed(&mut self) { + self.wd.feed(); + } + } +} + +#[cfg(feature = "rp2040")] +pub use watchdog_rp2040::*; diff --git a/builds/rp_pico_example/Cargo.toml b/builds/rp_pico_example/Cargo.toml index 8d27eb7..1668703 100644 --- a/builds/rp_pico_example/Cargo.toml +++ b/builds/rp_pico_example/Cargo.toml @@ -5,8 +5,9 @@ edition = "2021" [dependencies] embassy-usb-logger = { git = "https://github.com/embassy-rs/embassy.git" } -rfe = { path = "../../rfe", default-features = false } +rfe = { path = "../../rfe", default-features = false, features = ["rp2040"] } to = { path = "../../apps/to", default-features = false } +hs = { path = "../../apps/hs", default-features = false, features = ["rp2040"] } anyhow.workspace = true anyhow.default_features = false embedded-alloc = "0.6.0" diff --git a/builds/rp_pico_example/src/main.rs b/builds/rp_pico_example/src/main.rs index e02e886..2e394b1 100644 --- a/builds/rp_pico_example/src/main.rs +++ b/builds/rp_pico_example/src/main.rs @@ -16,6 +16,7 @@ mod app { }; use embedded_hal::digital::v2::OutputPin; use fugit::Duration; + use hs::{Hs, HsConfig, Rp2040Watchdog, StubSystemInfoGrabber}; use log::info; use msg::{ExampleHk, Instance, MsgKind, MsgPacket, TargetMsg, TlmSetItem, ToTlmSet}; use rfe::{connector::Connector, Rate, *}; @@ -30,7 +31,7 @@ mod app { use embedded_alloc::LlffHeap as Heap; use panic_halt as _; use rp_pico::XOSC_CRYSTAL_FREQ; - use time::SchTimeDriver; + use time::Rp2040TimeDriver; extern crate alloc; use alloc::vec; use alloc::vec::Vec; @@ -96,6 +97,8 @@ mod app { struct Local { led: Pin, PullNone>, usb: Option>, + wd: Option, + time_driver: Option, } struct UsbBinding; @@ -107,7 +110,7 @@ mod app { // Configure the clocks, watchdog - The default is to generate a 125 MHz system clock Mono::start(ctx.device.TIMER, &mut ctx.device.RESETS); // default rp2040 clock-rate is 125MHz let mut watchdog = Watchdog::new(ctx.device.WATCHDOG); - let _clocks = clocks::init_clocks_and_plls( + let clocks = clocks::init_clocks_and_plls( XOSC_CRYSTAL_FREQ, ctx.device.XOSC, ctx.device.CLOCKS, @@ -138,6 +141,12 @@ mod app { .into_pull_type::() .into_push_pull_output(); + let time_driver = Rp2040TimeDriver::new( + unsafe { rp_pico::pac::TIMER::steal() }, + &mut ctx.device.RESETS, + &clocks, + ); + // Spawn heartbeat task blink_instance::spawn().ok(); usb_logger::spawn().ok(); @@ -148,6 +157,8 @@ mod app { Local { led, usb: Some(driver), + wd: Some(watchdog), + time_driver: Some(time_driver), }, ) } @@ -165,7 +176,7 @@ mod app { } } - #[task(local = [led], priority = 1)] + #[task(local = [led, wd, time_driver], priority = 1)] async fn blink_instance(mut ctx: blink_instance::Context) { let mut blink_app = BlinkApp { led_pin: &mut ctx.local.led, @@ -188,13 +199,28 @@ mod app { }, ); let mut to = To::new(&mut log_connector, tlmsets); - let time_driver = SchTimeDriver::new(); + let mut wd = Rp2040Watchdog::new(ctx.local.wd.take().unwrap()); + let mut grabber = StubSystemInfoGrabber::new(); + let mut hs = Hs::new( + HsConfig { + cpu_checks: false, + fs_checks: false, + mem_checks: false, + temp_checks: false, + watchdog_enable: true, + watchdog_timeout: 10, + }, + &mut grabber, + Some(&mut wd), + ); + let time_driver = ctx.local.time_driver.take().unwrap(); let mut instance = RfeInstance::new(Instance::Example, &time_driver); instance.add_app("blink_app", &mut blink_app).unwrap(); instance.add_app("to", &mut to).unwrap(); + instance.add_app("hs", &mut hs).unwrap(); let mut next_time = Mono::now() + Duration::::from_ticks(10000); - + loop { instance.run(); Mono::delay_until(next_time).await; diff --git a/rfe/Cargo.toml b/rfe/Cargo.toml index 6a787b9..d5cf4a7 100644 --- a/rfe/Cargo.toml +++ b/rfe/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [features] default = [] std = ["dep:mio"] +rp2040 = ["dep:rp2040-hal", "dep:rp2040-pac"] to_csv = [] [dependencies] @@ -14,4 +15,6 @@ anyhow.workspace = true hashbrown.workspace = true log.workspace = true mio = { version = "1.0.2", features = ["net", "os-poll"], optional = true } +rp2040-hal = { workspace = true, optional = true } +rp2040-pac = { workspace = true, optional = true } macros.path = "macros" diff --git a/rfe/src/time.rs b/rfe/src/time.rs index ea6d6ce..f56caf8 100644 --- a/rfe/src/time.rs +++ b/rfe/src/time.rs @@ -43,3 +43,28 @@ impl TimeDriver for SchTimeDriver { time_data.sch_counter + time_data.time_offset } } + +#[cfg(feature = "rp2040")] +pub struct Rp2040TimeDriver { + timer: rp2040_hal::timer::Timer, +} + +#[cfg(feature = "rp2040")] +impl Rp2040TimeDriver { + pub fn new( + timer: rp2040_pac::TIMER, + resets: &mut rp2040_pac::RESETS, + clocks: &rp2040_hal::clocks::ClocksManager, + ) -> Self { + Self { + timer: rp2040_hal::timer::Timer::new(timer, resets, clocks), + } + } +} + +#[cfg(feature = "rp2040")] +impl TimeDriver for Rp2040TimeDriver { + fn get_system_time(&self, time_data: TimeData) -> Timestamp { + self.timer.get_counter().ticks() + time_data.time_offset + } +}