From c5826876a6d0a68dd86565b79c6ae7d178e3b08d Mon Sep 17 00:00:00 2001 From: linuswck Date: Tue, 3 Sep 2024 16:30:51 +0800 Subject: [PATCH] firmware: Add cmd to get hw_rev --- src/device/boot.rs | 7 ++++--- src/device/hw_rev.rs | 3 +++ src/main.rs | 3 ++- src/net/cmd_handler.rs | 31 ++++++++++++++++++++++++++++++- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/device/boot.rs b/src/device/boot.rs index c5f7c2e..b7b5f0a 100644 --- a/src/device/boot.rs +++ b/src/device/boot.rs @@ -9,7 +9,8 @@ use uom::si::{electric_current::{ampere, milliampere}, f32::ElectricCurrent}; use super::{gpio, sys_timer, usb}; -use crate::{device::flash_store::{self, FlashStore}, +use crate::{device::{flash_store::{self, FlashStore}, + hw_rev::HWRev}, laser_diode::{laser_diode::LdDrive, ld_ctrl::*}, net::net::{IpSettings, ServerHandle}, thermostat::{max1968::MAX1968, thermostat::Thermostat}, @@ -23,7 +24,7 @@ const WATCHDOG_PERIOD: u32 = 30000; pub fn bootup( mut core_perif: CorePeripherals, perif: Peripherals, -) -> (IndependentWatchdog, FlashStore, LdDrive, Thermostat) { +) -> (IndependentWatchdog, FlashStore, HWRev, LdDrive, Thermostat) { core_perif.SCB.enable_icache(); core_perif.SCB.enable_dcache(&mut core_perif.CPUID); @@ -119,5 +120,5 @@ pub fn bootup( info!("Kirdy setup complete"); - (wd, flash_store, laser, thermostat) + (wd, flash_store, hw_rev, laser, thermostat) } diff --git a/src/device/hw_rev.rs b/src/device/hw_rev.rs index ac835e6..37ae5fb 100644 --- a/src/device/hw_rev.rs +++ b/src/device/hw_rev.rs @@ -1,4 +1,6 @@ use crc::{Crc, CRC_24_BLE}; +use miniconf::Tree; +use serde::{Deserialize, Serialize}; use stm32f4xx_hal::{gpio::{Input, PE10, PE11, PE8, PE9}, signature}; @@ -11,6 +13,7 @@ pub struct HwRevPins { pub h3: PE11, } +#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)] pub struct HWRev { pub major: u8, pub minor: u8, diff --git a/src/main.rs b/src/main.rs index 842694a..0b95626 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,7 +64,7 @@ fn main() -> ! { let core_perif = CorePeripherals::take().unwrap(); let perif = Peripherals::take().unwrap(); - let (mut wd, mut flash_store, mut laser, mut thermostat) = bootup(core_perif, perif); + let (mut wd, mut flash_store, mut hw_rev, mut laser, mut thermostat) = bootup(core_perif, perif); let mut device_settings = DeviceSettings { ip_settings: IpSettings::default(), @@ -195,6 +195,7 @@ fn main() -> ! { eth_data_buffer, bytes, &mut socket, + &mut hw_rev, &mut laser, &mut thermostat, &mut state, diff --git a/src/net/cmd_handler.rs b/src/net/cmd_handler.rs index 51c13fd..3626198 100644 --- a/src/net/cmd_handler.rs +++ b/src/net/cmd_handler.rs @@ -10,7 +10,7 @@ use uom::si::{electric_current::{ampere, ElectricCurrent}, electrical_resistance::{ohm, ElectricalResistance}, power::{watt, Power}}; -use crate::{device::{dfu, sys_timer}, +use crate::{device::{dfu, hw_rev::HWRev, sys_timer}, laser_diode::{laser_diode::{LdDrive, LdSettingsSummary, StatusReport as LdStatusReport}, pd_mon_params::{self, ResponsitivityUnit}}, net::net, @@ -26,6 +26,7 @@ pub enum ResponseEnum { Reserved, Settings, Report, + HwRev, Acknowledge, InvalidDatatype, InvalidCmd, @@ -59,6 +60,7 @@ pub struct ResponseObj<'a> { enum DeviceCmd { #[default] Reserved, + GetHwRev, SetIPSettings, SetActiveReportMode, SetPdFinGain, @@ -173,6 +175,17 @@ pub struct SettingsSummaryObj { json: SettingsSummary, } +#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)] +pub struct HwRevType { + msg_type: ResponseEnum, + hw_rev: HWRev, +} + +#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)] +pub struct HwRevObj { + json: HwRevType, +} + pub fn send_response(buffer: &mut [u8], msg_type: ResponseEnum, msg: MsgType, socket: &mut SocketHandle) { let response = ResponseObj { json: Response { @@ -227,6 +240,18 @@ pub fn send_status_report( net::eth_send(buffer, num_bytes, *socket); } +pub fn send_hw_rev(buffer: &mut [u8], hw_rev_o: &mut HWRev, socket: &mut SocketHandle) { + let hw_rev = HwRevObj { + json: HwRevType { + msg_type: ResponseEnum::HwRev, + hw_rev: *hw_rev_o, + }, + }; + let mut num_bytes = hw_rev.get_json("/json", buffer).unwrap(); + buffer[num_bytes] = b'\n'; + num_bytes += 1; + net::eth_send(buffer, num_bytes, *socket); +} // Use a minimal struct for high speed cmd ctrl to reduce processing overhead #[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)] pub struct TecSetICmdJson { @@ -245,6 +270,7 @@ pub fn execute_cmd( buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHandle, + hw_rev: &mut HWRev, laser: &mut LdDrive, thermostat: &mut Thermostat, state: &mut State, @@ -349,6 +375,9 @@ pub fn execute_cmd( ); } }, + Some(DeviceCmd::GetHwRev) => { + send_hw_rev(buffer, hw_rev, socket); + } Some(DeviceCmd::GetStatusReport) => { send_status_report(buffer, laser, thermostat, socket); }