From 0f2a51299c778062c9934271c73054752c59799d Mon Sep 17 00:00:00 2001 From: fly Date: Sat, 4 May 2024 11:36:28 +0200 Subject: [PATCH] Building calibration load, fails to do things tho Signed-off-by: fly --- Pedestal/firmware/src/main.rs | 46 ++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/Pedestal/firmware/src/main.rs b/Pedestal/firmware/src/main.rs index 6fb5ba8..6dc73c4 100644 --- a/Pedestal/firmware/src/main.rs +++ b/Pedestal/firmware/src/main.rs @@ -125,7 +125,10 @@ fn main() -> ! { let mut adc1 = adc::Adc::adc1(p.ADC1, clocks); // ====================== Calibration =============== - let mut cal = load_calibration(); + let mut calibration_active = false; + let mut calibration_min_done = false; + let mut flash_writer = flash.writer(SectorSize::Sz1K, FlashSize::Sz64K); + let mut cal = load_calibration(&mut flash_writer); // ====================== Pin setup ================= let mut input_pins = MyPins { @@ -144,12 +147,6 @@ fn main() -> ! { pwm.enable(Channel::C1); let pwm_max = pwm.get_max_duty(); //48000 in our case - // ====================== Calibration things ======== - let mut calibration_active = false; - let mut calibration_min_done = false; - - // ====================== Calibration things ======== - let mut flash_writer = flash.writer(SectorSize::Sz1K, FlashSize::Sz64K); // ====================== Main loop ================= loop { @@ -256,7 +253,10 @@ fn save_calibration(flash: &mut FlashWriter, cal: &Calibration) -> bool { let encoded_calibration_data = bytes_of(cal); data[3..][..encoded_calibration_data.len()].copy_from_slice(encoded_calibration_data); - let test = flash.erase(FLASH_START + 64512, 1024); + // Verify deactivation due to bug, see https://github.com/stm32-rs/stm32f1xx-hal/issues/330 +// flash.change_verification(false); +// flash.erase(FLASH_START + 64512, 1024).unwrap(); +// flash.change_verification(true); match flash.write(FLASH_START + 64512, &data) { Ok(_ret) => return true, Err(_e) => return false, @@ -264,9 +264,31 @@ fn save_calibration(flash: &mut FlashWriter, cal: &Calibration) -> bool { } // Load calibration to flash -fn load_calibration() -> Calibration { +fn load_calibration(flash: &mut FlashWriter) -> Calibration { let cal = Calibration::new(); - // TODO reject loading if version is off or no calibration data found - // TODO return calibration data - return cal; + match flash.read(FLASH_START + 64512, 1024) { + Ok(data) => { + // Check if data is available and return early if not suitable + if data[2] != 1 { + return cal; + } + // Check if data is in compatible version and return early if not suitable + match try_from_bytes::(&data[0..2]) { + Ok(flash_version) => { + if flash_version != &FLASH_LAYOUT_VERSION { + return cal + } + }, + Err(_e) => return cal, + } + // Load calibration data + match try_from_bytes::(&data[3..]) { + Ok(cal_data) => { + return *cal_data + }, + Err(_e) => return cal, + } + }, + Err(_e) => return cal, + }; }