From 61d284ff2c7486c3c858c80d1416d202b51821c8 Mon Sep 17 00:00:00 2001
From: Benjamin Koch <bbbsnowball@gmail.com>
Date: Tue, 30 May 2023 04:54:39 +0200
Subject: [PATCH] use blinky for testing firmware download

---
 firmware/rust1/build.sh                       |  5 ++
 .../rust1/download_firmware_via_modbus.py     |  3 +-
 firmware/rust1/src/bin/blinky.rs              | 12 +++-
 firmware/rust1/src/bin/heizung.rs             | 37 +------------
 firmware/rust1/src/lib.rs                     |  1 +
 firmware/rust1/src/watchdog.rs                | 55 +++++++++++++++++++
 6 files changed, 75 insertions(+), 38 deletions(-)
 create mode 100644 firmware/rust1/src/watchdog.rs

diff --git a/firmware/rust1/build.sh b/firmware/rust1/build.sh
index 2aa4549..86f5cde 100755
--- a/firmware/rust1/build.sh
+++ b/firmware/rust1/build.sh
@@ -22,11 +22,16 @@ cargo build --bin heizung
 cargo objdump --bin heizung -- -xd >target/thumbv6m-none-eabi/debug/heizung.map
 cargo build --bin heizung --release
 cargo objdump --bin heizung --release -- -xd >target/thumbv6m-none-eabi/release/heizung.map
+cargo build --bin blinky --release
+cargo objdump --bin blinky --release -- -xd >target/thumbv6m-none-eabi/release/blinky.map
 ./.cargo-tools/bin/elf2uf2-rs target/thumbv6m-none-eabi/debug/heizung
 ./.cargo-tools/bin/elf2uf2-rs target/thumbv6m-none-eabi/release/heizung
+./.cargo-tools/bin/elf2uf2-rs target/thumbv6m-none-eabi/release/blinky
 
 cat ../rust1-bootloader/target/thumbv6m-none-eabi/release/heizung-bootloader.uf2 target/thumbv6m-none-eabi/debug/heizung.uf2 >heizung-debug.uf2
 cat ../rust1-bootloader/target/thumbv6m-none-eabi/release/heizung-bootloader.uf2 target/thumbv6m-none-eabi/release/heizung.uf2 >heizung-release.uf2
+cat ../rust1-bootloader/target/thumbv6m-none-eabi/release/heizung-bootloader.uf2 target/thumbv6m-none-eabi/release/blinky.uf2 >blinky-release.uf2
 
 size target/thumbv6m-none-eabi/debug/heizung
 size target/thumbv6m-none-eabi/release/heizung
+size target/thumbv6m-none-eabi/release/blinky
diff --git a/firmware/rust1/download_firmware_via_modbus.py b/firmware/rust1/download_firmware_via_modbus.py
index a1f287a..d498aa9 100644
--- a/firmware/rust1/download_firmware_via_modbus.py
+++ b/firmware/rust1/download_firmware_via_modbus.py
@@ -15,7 +15,8 @@ from uf2 import *
 
 PORT = '/dev/ttyUSB0'
 DEVICE_ADDR = 1
-FIRMWARE = "./heizung-release.uf2"
+#FIRMWARE = "./heizung-release.uf2"
+FIRMWARE = "./blinky-release.uf2"
 ACTIVE_PARTITION_ADDRESS = 0x10007000
 
 logger = logging.getLogger()
diff --git a/firmware/rust1/src/bin/blinky.rs b/firmware/rust1/src/bin/blinky.rs
index cda87ec..a53b64b 100644
--- a/firmware/rust1/src/bin/blinky.rs
+++ b/firmware/rust1/src/bin/blinky.rs
@@ -4,17 +4,25 @@
 
 use defmt::*;
 use embassy_executor::Spawner;
-use embassy_rp::gpio;
+use embassy_rp::{gpio, peripherals, watchdog::Watchdog};
 use embassy_time::{Duration, Timer};
 use gpio::{Level, Output};
 use {defmt_rtt as _, panic_probe as _};
 use heizung as _;  // for clear_bootloader_state
+use heizung::watchdog::WatchdogFixed;
+
+#[embassy_executor::task]
+async fn dogfeeder(watchdog: peripherals::WATCHDOG) {
+    WatchdogFixed::new(watchdog).start_and_feed_continuously().await;
+}
 
 #[embassy_executor::main]
-async fn main(_spawner: Spawner) {
+async fn main(spawner: Spawner) {
     let p = embassy_rp::init(Default::default());
     let mut led = Output::new(p.PIN_4, Level::Low);
 
+    unwrap!(spawner.spawn(dogfeeder(p.WATCHDOG)));
+
     loop {
         info!("led on!");
         led.set_high();
diff --git a/firmware/rust1/src/bin/heizung.rs b/firmware/rust1/src/bin/heizung.rs
index e14ef8b..a164576 100644
--- a/firmware/rust1/src/bin/heizung.rs
+++ b/firmware/rust1/src/bin/heizung.rs
@@ -10,7 +10,6 @@ use embassy_executor::Spawner;
 //use embassy_futures::select::*;
 use embassy_rp::adc::{self, Adc};
 use embassy_rp::peripherals::{*, self};
-use embassy_rp::watchdog::Watchdog;
 use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
 use embassy_sync::mutex::Mutex;
 use embassy_time::{Duration, Timer, Instant};
@@ -28,6 +27,7 @@ use heizung::i2c_scan::{self, ScanResultForBus};
 use heizung::modbus_server::{ModbusServer, ModbusRegisters, ModbusErrorCode, ModbusAdressMatch, U16Pusher};
 use heizung::rs485::RS485;
 use heizung::uf2updater::UF2UpdateHandler;
+use heizung::watchdog::WatchdogFixed;
 
 #[embassy_executor::task]
 async fn i2c_task(mut i2c_peri: embassy_rp::peripherals::I2C0,
@@ -563,42 +563,9 @@ impl<'a> ModbusRegisters for ModBusRegs<'a> {
     }
 }
 
-// The pause bits are on by default after reset but I think Watchdog::enable()
-// disables them so let's enable them, again.
-fn watchdog_pause_on_debug(_watchdog: &mut Watchdog) {
-    // This turns off the watchdog - no good! :-/
-    //_watchdog.pause_on_debug(true);
-
-    use embassy_rp::pac;
-    let watchdog = pac::WATCHDOG;
-    //SAFETY: We know which values have been set by WatchdogFlash / Watchdog.
-    unsafe {
-        // We use modify() to keep the enable bit at its current value.
-        watchdog.ctrl().modify(|w| {
-            w.set_pause_dbg0(true);
-            w.set_pause_dbg1(true);
-            w.set_pause_jtag(true);
-        });
-    }
-}
-
 #[embassy_executor::task]
 async fn dogfeeder(watchdog: peripherals::WATCHDOG) {
-    let mut watchdog = Watchdog::new(watchdog);
-    //watchdog.start(Duration::from_millis(500));
-
-    // Use the maximum value and turn on "pause on debug" to not interfere with
-    // probe-rs-cli's flash programming (which doesn't seem to turn off or feed
-    // the watchdog). This seems to work well enough but if it doesn't, manually
-    // put the board into bootloader mode before programming flash.
-    watchdog.start(Duration::from_secs(8));
-    watchdog_pause_on_debug(&mut watchdog);
-
-    loop {
-        watchdog.feed();
-        //Timer::after(Duration::from_millis(200)).await;
-        Timer::after(Duration::from_millis(2000)).await;
-    }
+    WatchdogFixed::new(watchdog).start_and_feed_continuously().await;
 }
 
 //#[embassy_executor::main]
diff --git a/firmware/rust1/src/lib.rs b/firmware/rust1/src/lib.rs
index 1340495..da50916 100644
--- a/firmware/rust1/src/lib.rs
+++ b/firmware/rust1/src/lib.rs
@@ -7,3 +7,4 @@ pub mod modbus_server;
 pub mod clear_bootloader_state;
 pub mod uf2;
 pub mod uf2updater;
+pub mod watchdog;
diff --git a/firmware/rust1/src/watchdog.rs b/firmware/rust1/src/watchdog.rs
new file mode 100644
index 0000000..b711e5b
--- /dev/null
+++ b/firmware/rust1/src/watchdog.rs
@@ -0,0 +1,55 @@
+use embassy_rp::{watchdog::Watchdog, peripherals};
+use embassy_time::{Duration, Timer};
+
+
+// The pause bits are on by default after reset but I think Watchdog::enable()
+// disables them so let's enable them, again.
+fn watchdog_pause_on_debug(_watchdog: &mut Watchdog) {
+    // This turns off the watchdog - no good! :-/
+    //_watchdog.pause_on_debug(true);
+
+    use embassy_rp::pac;
+    let watchdog = pac::WATCHDOG;
+    //SAFETY: We know which values have been set by WatchdogFlash / Watchdog.
+    unsafe {
+        // We use modify() to keep the enable bit at its current value.
+        watchdog.ctrl().modify(|w| {
+            w.set_pause_dbg0(true);
+            w.set_pause_dbg1(true);
+            w.set_pause_jtag(true);
+        });
+    }
+}
+
+pub struct WatchdogFixed {
+    inner: Watchdog,
+}
+
+impl WatchdogFixed {
+    pub fn new(watchdog: peripherals::WATCHDOG) -> Self {
+        WatchdogFixed {
+            inner: Watchdog::new(watchdog)
+        }
+    }
+
+    pub fn start(self: &mut Self) {
+        // Use the maximum value and turn on "pause on debug" to not interfere with
+        // probe-rs-cli's flash programming (which doesn't seem to turn off or feed
+        // the watchdog). This seems to work well enough but if it doesn't, manually
+        // put the board into bootloader mode before programming flash.
+        self.inner.start(Duration::from_secs(8));
+        watchdog_pause_on_debug(&mut self.inner);    
+    }
+
+    pub fn feed(self: &mut Self) {
+        self.inner.feed();
+    }
+
+    pub async fn start_and_feed_continuously(self: &mut Self) {
+        self.start();
+        loop {
+            self.feed();
+            Timer::after(Duration::from_secs(2)).await;
+        }
+    }
+}
-- 
GitLab