Building calibration load, fails to do things tho

Signed-off-by: fly <merspieler@alwaysdata.com>
This commit is contained in:
fly 2024-05-04 11:36:28 +02:00
parent 19c5b1c97e
commit 0f2a51299c

View file

@ -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::<u16>(&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::<Calibration>(&data[3..]) {
Ok(cal_data) => {
return *cal_data
},
Err(_e) => return cal,
}
},
Err(_e) => return cal,
};
}