From 620961f7b5578e90283494f3e99429bde66974ff Mon Sep 17 00:00:00 2001
From: Benjamin Koch <bbbsnowball@gmail.com>
Date: Sun, 28 May 2023 04:03:58 +0200
Subject: [PATCH] clear bootloader state when downloading application

---
 firmware/rust1/build.rs                      | 6 ++++++
 firmware/rust1/clear-bootloader-state.x      | 7 +++++++
 firmware/rust1/src/bin/blinky.rs             | 1 +
 firmware/rust1/src/clear_bootloader_state.rs | 6 ++++++
 firmware/rust1/src/lib.rs                    | 1 +
 5 files changed, 21 insertions(+)
 create mode 100644 firmware/rust1/clear-bootloader-state.x
 create mode 100644 firmware/rust1/src/clear_bootloader_state.rs

diff --git a/firmware/rust1/build.rs b/firmware/rust1/build.rs
index f64efb0..52730bc 100644
--- a/firmware/rust1/build.rs
+++ b/firmware/rust1/build.rs
@@ -21,6 +21,10 @@ fn main() {
         .unwrap()
         .write_all(include_bytes!("memory.x"))
         .unwrap();
+    File::create(out.join("clear-bootloader-state.x"))
+        .unwrap()
+        .write_all(include_bytes!("clear-bootloader-state.x"))
+        .unwrap();
     println!("cargo:rustc-link-search={}", out.display());
 
     // By default, Cargo will re-run a build script whenever
@@ -28,9 +32,11 @@ fn main() {
     // here, we ensure the build script is only re-run when
     // `memory.x` is changed.
     println!("cargo:rerun-if-changed=memory.x");
+    println!("cargo:rerun-if-changed=clear-bootloader-state.x");
 
     println!("cargo:rustc-link-arg-bins=--nmagic");
     println!("cargo:rustc-link-arg-bins=-Tlink.x");
     //println!("cargo:rustc-link-arg-bins=-Tlink-rp.x");  // This is for boot2 but the bootloader handles that for us.
     println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
+    println!("cargo:rustc-link-arg-bins=-Tclear-bootloader-state.x");
 }
diff --git a/firmware/rust1/clear-bootloader-state.x b/firmware/rust1/clear-bootloader-state.x
new file mode 100644
index 0000000..c798754
--- /dev/null
+++ b/firmware/rust1/clear-bootloader-state.x
@@ -0,0 +1,7 @@
+SECTIONS
+{
+  .clear_bootloader_state :
+  {
+    KEEP(*(.clear_bootloader_state .clear_bootloader_state.*))
+  } > BOOTLOADER_STATE =0xff
+}
diff --git a/firmware/rust1/src/bin/blinky.rs b/firmware/rust1/src/bin/blinky.rs
index e97dca9..cda87ec 100644
--- a/firmware/rust1/src/bin/blinky.rs
+++ b/firmware/rust1/src/bin/blinky.rs
@@ -8,6 +8,7 @@ use embassy_rp::gpio;
 use embassy_time::{Duration, Timer};
 use gpio::{Level, Output};
 use {defmt_rtt as _, panic_probe as _};
+use heizung as _;  // for clear_bootloader_state
 
 #[embassy_executor::main]
 async fn main(_spawner: Spawner) {
diff --git a/firmware/rust1/src/clear_bootloader_state.rs b/firmware/rust1/src/clear_bootloader_state.rs
new file mode 100644
index 0000000..61895f0
--- /dev/null
+++ b/firmware/rust1/src/clear_bootloader_state.rs
@@ -0,0 +1,6 @@
+
+// Clear bootloader state partition when we download this program
+// via some other way (e.g. UF2 or pico-probe-rs).
+#[used]
+#[link_section = ".clear_bootloader_state"]
+pub static CLEAR_BOOTLOADER_STATE: [u8; 4096] = [0xff; 4096];
diff --git a/firmware/rust1/src/lib.rs b/firmware/rust1/src/lib.rs
index 0af2093..a22b737 100644
--- a/firmware/rust1/src/lib.rs
+++ b/firmware/rust1/src/lib.rs
@@ -4,3 +4,4 @@ pub mod dont_abort;
 pub mod i2c_scan;
 pub mod rs485;
 pub mod modbus_server;
+pub mod clear_bootloader_state;
-- 
GitLab