From 245abdcf6ba7d29cde7dfe5c6aebbbd653582668 Mon Sep 17 00:00:00 2001 From: fly Date: Mon, 13 May 2024 23:34:20 +0200 Subject: [PATCH] Working saving and loading of callibration Signed-off-by: fly --- Pedestal/firmware/src/main.rs | 39 ++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/Pedestal/firmware/src/main.rs b/Pedestal/firmware/src/main.rs index 6dc73c4..d83499e 100644 --- a/Pedestal/firmware/src/main.rs +++ b/Pedestal/firmware/src/main.rs @@ -26,7 +26,7 @@ use usb_device::prelude::*; use usbd_human_interface_device::prelude::*; use crate::device::{CustomConfig, CustomInputReport}; -use bytemuck::{bytes_of, try_from_bytes, Pod, Zeroable}; +use bytemuck::{bytes_of, bytes_of_mut, try_from_bytes, Pod, Zeroable}; // Set layout version const FLASH_LAYOUT_VERSION: u16 = 0; @@ -196,8 +196,10 @@ fn main() -> ! { cal.integ_lt.max = CalibrationData::ADC_MAX; } cal.integ_lt.factor = calculate_factor(cal.integ_lt.min, cal.integ_lt.max); - save_calibration(&mut flash_writer, &cal); - pwm.set_duty(Channel::C1, pwm_max); + let save_success = save_calibration(&mut flash_writer, &cal); + if save_success { + pwm.set_duty(Channel::C1, pwm_max); + } } calibration_active = false; calibration_min_done = false; @@ -254,10 +256,10 @@ fn save_calibration(flash: &mut FlashWriter, cal: &Calibration) -> bool { data[3..][..encoded_calibration_data.len()].copy_from_slice(encoded_calibration_data); // 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) { + flash.change_verification(false); + flash.erase(64512, 1024).unwrap(); + flash.change_verification(true); + match flash.write(64512, &data) { Ok(_ret) => return true, Err(_e) => return false, }; @@ -265,8 +267,8 @@ fn save_calibration(flash: &mut FlashWriter, cal: &Calibration) -> bool { // Load calibration to flash fn load_calibration(flash: &mut FlashWriter) -> Calibration { - let cal = Calibration::new(); - match flash.read(FLASH_START + 64512, 1024) { + let mut cal = Calibration::new(); + match flash.read(64512, 1023) { Ok(data) => { // Check if data is available and return early if not suitable if data[2] != 1 { @@ -279,16 +281,19 @@ fn load_calibration(flash: &mut FlashWriter) -> Calibration { return cal } }, - Err(_e) => 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, - } + let dummy = bytes_of(&cal); + let dummy2 = &data[3..][..dummy.len()]; + let mut dummy3 = bytes_of_mut(&mut cal); + dummy3.copy_from_slice(&dummy2); + return cal; + }, + Err(_e) => { + return cal }, - Err(_e) => return cal, }; }