From 7855909a47e736c42fa11bf333fdbcbf65bc1ddc Mon Sep 17 00:00:00 2001
From: Benjamin Koch <bbbsnowball@gmail.com>
Date: Sun, 11 Jun 2023 02:05:41 +0200
Subject: [PATCH] add pinout to program info

---
 firmware/rust1/src/bin/heizung.rs  | 34 ++++++++++++++++++++-
 firmware/rust1/src/program_info.rs | 49 +++++++++++++++++++++---------
 2 files changed, 67 insertions(+), 16 deletions(-)

diff --git a/firmware/rust1/src/bin/heizung.rs b/firmware/rust1/src/bin/heizung.rs
index 0d15bdf..9a0df6b 100644
--- a/firmware/rust1/src/bin/heizung.rs
+++ b/firmware/rust1/src/bin/heizung.rs
@@ -25,7 +25,7 @@ use heapless::String;
 
 use heizung::i2c_scan::{self, ScanResultForBus};
 use heizung::modbus_server::{ModbusServer, ModbusRegisters, ModbusErrorCode, ModbusAdressMatch, U16Pusher};
-use heizung::program_info::ProgramInfoBuilder;
+use heizung::program_info::{ProgramInfoBuilder, PinFunction};
 use heizung::rs485::RS485;
 use heizung::uf2updater::UF2UpdateHandler;
 use heizung::watchdog::WatchdogFixed;
@@ -728,5 +728,37 @@ pub static PROGRAM_INFO: [u8; 4096] = {
         .program_build_attribute(build_type)
         //FIXME should be based on what is selected for embassy-rp crate
         .boot2_name("boot2_w25q080")
+        .pins_with_func(PinFunction::UART, &[16, 17])
+        .pins_with_func(PinFunction::USB, &[10])
+        .pins_with_func(PinFunction::I2C, &[12, 13])
+        .pins_with_names(&[
+            (0, "DRIVE2"),
+            (1, "DRIVE1"),
+            (2, "WS2812 Ring around Display, white LED"),
+            (3, "Enable current measurement, yellow LED"),
+            (4, "blue LED"),
+            (5, "green LED"),
+            (6, "buzzer, red LED"),
+            (7, "MATRIX_IN1"),
+            (8, "USB type-c connector, SBU1"),
+            (9, "USB type-c connector, SBU2"),
+            (11, "Button ADDR"),
+            (14, "Red channel of WS2811"),
+            (15, "RS485 TX Enable"),
+        ])  // picotool doesn't support too many pins at once so add a break here
+        .pins_with_names(&[
+            (18, "MATRIX_IN2"),
+            (19, "REED Input 3"),
+            (20, "REED Input 4"),
+            (21, "REED Input 2"),
+            (22, "REED Input 1"),
+            (23, "MATRIX_IN3"),
+            (24, "DIGOUT1"),
+            (25, "DIGOUT2"),
+            (26, "Analog IN 1"),
+            (27, "Current Measurement Channel 2"),
+            (28, "Current Measurement Channel 1"),
+            (29, "VCC Measurement"),
+        ])
         .build()
 };
diff --git a/firmware/rust1/src/program_info.rs b/firmware/rust1/src/program_info.rs
index 503c5fe..d078355 100644
--- a/firmware/rust1/src/program_info.rs
+++ b/firmware/rust1/src/program_info.rs
@@ -9,6 +9,7 @@ pub struct ProgramInfoBuilder {
 
 #[repr(u16)]
 #[allow(non_camel_case_types)]
+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
 pub enum GroupFlags {
     NONE            = 0x0000,
     SHOW_IF_EMPTY   = 0x0001,  // default is to hide
@@ -17,6 +18,23 @@ pub enum GroupFlags {
     ADVANCED        = 0x0008,  // if set, then only shown in say info -a
 }
 
+#[repr(u8)]
+#[allow(non_camel_case_types)]
+#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
+pub enum PinFunction {
+    XIP = 0,
+    SPI = 1,
+    UART = 2,
+    I2C = 3,
+    PWM = 4,
+    SIO = 5,
+    PIO0 = 6,
+    PIO1 = 7,
+    GPCK = 8,
+    USB = 9,
+    NULL = 0xf,
+}
+
 const fn copy(mut buf: [u8; 4096], offset: usize, source: &[u8], source_offset: usize) -> [u8; 4096] {
     if source_offset >= source.len() {
         buf
@@ -247,17 +265,17 @@ impl ProgramInfoBuilder {
     #[allow(unused)] const BINARY_INFO_TYPE_PINS_WITH_NAMES: u16 = 9;
     #[allow(unused)] const BINARY_INFO_TYPE_NAMED_GROUP: u16 = 10;
 
-    #[allow(unused)] const BINARY_INFO_ID_RP_PROGRAM_NAME: u32 = 0x02031c86;
-    #[allow(unused)] const BINARY_INFO_ID_RP_PROGRAM_VERSION_STRING: u32 = 0x11a9bc3a;
-    #[allow(unused)] const BINARY_INFO_ID_RP_PROGRAM_BUILD_DATE_STRING: u32 = 0x9da22254;
-    #[allow(unused)] const BINARY_INFO_ID_RP_BINARY_END: u32 = 0x68f465de;
-    #[allow(unused)] const BINARY_INFO_ID_RP_PROGRAM_URL: u32 = 0x1856239a;
-    #[allow(unused)] const BINARY_INFO_ID_RP_PROGRAM_DESCRIPTION: u32 = 0xb6a07c19;
-    #[allow(unused)] const BINARY_INFO_ID_RP_PROGRAM_FEATURE: u32 = 0xa1f4b453;
-    #[allow(unused)] const BINARY_INFO_ID_RP_PROGRAM_BUILD_ATTRIBUTE: u32 = 0x4275f0d3;
-    #[allow(unused)] const BINARY_INFO_ID_RP_SDK_VERSION: u32 = 0x5360b3ab;
-    #[allow(unused)] const BINARY_INFO_ID_RP_PICO_BOARD: u32 = 0xb63cffbb;
-    #[allow(unused)] const BINARY_INFO_ID_RP_BOOT2_NAME: u32 = 0x7f8882e1;
+    const BINARY_INFO_ID_RP_PROGRAM_NAME: u32 = 0x02031c86;
+    const BINARY_INFO_ID_RP_PROGRAM_VERSION_STRING: u32 = 0x11a9bc3a;
+    const BINARY_INFO_ID_RP_PROGRAM_BUILD_DATE_STRING: u32 = 0x9da22254;
+    const BINARY_INFO_ID_RP_BINARY_END: u32 = 0x68f465de;
+    const BINARY_INFO_ID_RP_PROGRAM_URL: u32 = 0x1856239a;
+    const BINARY_INFO_ID_RP_PROGRAM_DESCRIPTION: u32 = 0xb6a07c19;
+    const BINARY_INFO_ID_RP_PROGRAM_FEATURE: u32 = 0xa1f4b453;
+    const BINARY_INFO_ID_RP_PROGRAM_BUILD_ATTRIBUTE: u32 = 0x4275f0d3;
+    const BINARY_INFO_ID_RP_SDK_VERSION: u32 = 0x5360b3ab;
+    const BINARY_INFO_ID_RP_PICO_BOARD: u32 = 0xb63cffbb;
+    const BINARY_INFO_ID_RP_BOOT2_NAME: u32 = 0x7f8882e1;
 
     pub const fn binary_end_address(self: Self, value: u32) -> Self {
         self.bi_int(Self::BINARY_INFO_TAG_RASPBERRY_PI, Self::BINARY_INFO_ID_RP_BINARY_END, value)
@@ -302,8 +320,8 @@ impl ProgramInfoBuilder {
             flags as u16, tag, id, label)
     }
 
-    pub const fn pins_with_func(self: Self, func: u8, pins: &[u8]) -> Self {
-        assert!(func < 16);
+    pub const fn pins_with_func(self: Self, func: PinFunction, pins: &[u8]) -> Self {
+        assert!((func as u8) < 16);
         assert!(pins.len() > 0 && pins.len() < 6);
 
         const BI_PINS_ENCODING_MULTI: u32 = 2;
@@ -325,8 +343,8 @@ impl ProgramInfoBuilder {
         self.bi_encoded_pins_with_func(encoded)
     }
 
-    pub const fn pin_range_with_func(self: Self, func: u8, pin_range: (u8, u8)) -> Self {
-        assert!(func < 16);
+    pub const fn pin_range_with_func(self: Self, func: PinFunction, pin_range: (u8, u8)) -> Self {
+        assert!((func as u8) < 16);
         let (plo, phi) = pin_range;
 
         const BI_PINS_ENCODING_RANGE: u32 = 1;
@@ -340,6 +358,7 @@ impl ProgramInfoBuilder {
             if offset >= pins.len() {
                 mask
             } else {
+                assert!(offset == 0 || pins[offset-1].0 < pins[offset].0, "Pins must be in ascending order");
                 collect_mask(mask | (1 << pins[offset].0), pins, offset+1)
             }
         }
-- 
GitLab