Working saving and loading of callibration
Signed-off-by: fly <merspieler@alwaysdata.com>
This commit is contained in:
parent
0f2a51299c
commit
245abdcf6b
1 changed files with 22 additions and 17 deletions
|
@ -26,7 +26,7 @@ use usb_device::prelude::*;
|
||||||
use usbd_human_interface_device::prelude::*;
|
use usbd_human_interface_device::prelude::*;
|
||||||
|
|
||||||
use crate::device::{CustomConfig, CustomInputReport};
|
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
|
// Set layout version
|
||||||
const FLASH_LAYOUT_VERSION: u16 = 0;
|
const FLASH_LAYOUT_VERSION: u16 = 0;
|
||||||
|
@ -196,9 +196,11 @@ fn main() -> ! {
|
||||||
cal.integ_lt.max = CalibrationData::ADC_MAX;
|
cal.integ_lt.max = CalibrationData::ADC_MAX;
|
||||||
}
|
}
|
||||||
cal.integ_lt.factor = calculate_factor(cal.integ_lt.min, cal.integ_lt.max);
|
cal.integ_lt.factor = calculate_factor(cal.integ_lt.min, cal.integ_lt.max);
|
||||||
save_calibration(&mut flash_writer, &cal);
|
let save_success = save_calibration(&mut flash_writer, &cal);
|
||||||
|
if save_success {
|
||||||
pwm.set_duty(Channel::C1, pwm_max);
|
pwm.set_duty(Channel::C1, pwm_max);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
calibration_active = false;
|
calibration_active = false;
|
||||||
calibration_min_done = 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);
|
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
|
// Verify deactivation due to bug, see https://github.com/stm32-rs/stm32f1xx-hal/issues/330
|
||||||
// flash.change_verification(false);
|
flash.change_verification(false);
|
||||||
// flash.erase(FLASH_START + 64512, 1024).unwrap();
|
flash.erase(64512, 1024).unwrap();
|
||||||
// flash.change_verification(true);
|
flash.change_verification(true);
|
||||||
match flash.write(FLASH_START + 64512, &data) {
|
match flash.write(64512, &data) {
|
||||||
Ok(_ret) => return true,
|
Ok(_ret) => return true,
|
||||||
Err(_e) => return false,
|
Err(_e) => return false,
|
||||||
};
|
};
|
||||||
|
@ -265,8 +267,8 @@ fn save_calibration(flash: &mut FlashWriter, cal: &Calibration) -> bool {
|
||||||
|
|
||||||
// Load calibration to flash
|
// Load calibration to flash
|
||||||
fn load_calibration(flash: &mut FlashWriter) -> Calibration {
|
fn load_calibration(flash: &mut FlashWriter) -> Calibration {
|
||||||
let cal = Calibration::new();
|
let mut cal = Calibration::new();
|
||||||
match flash.read(FLASH_START + 64512, 1024) {
|
match flash.read(64512, 1023) {
|
||||||
Ok(data) => {
|
Ok(data) => {
|
||||||
// Check if data is available and return early if not suitable
|
// Check if data is available and return early if not suitable
|
||||||
if data[2] != 1 {
|
if data[2] != 1 {
|
||||||
|
@ -279,16 +281,19 @@ fn load_calibration(flash: &mut FlashWriter) -> Calibration {
|
||||||
return cal
|
return cal
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(_e) => return cal,
|
Err(_e) => {
|
||||||
|
return cal
|
||||||
|
},
|
||||||
}
|
}
|
||||||
// Load calibration data
|
// Load calibration data
|
||||||
match try_from_bytes::<Calibration>(&data[3..]) {
|
let dummy = bytes_of(&cal);
|
||||||
Ok(cal_data) => {
|
let dummy2 = &data[3..][..dummy.len()];
|
||||||
return *cal_data
|
let mut dummy3 = bytes_of_mut(&mut cal);
|
||||||
|
dummy3.copy_from_slice(&dummy2);
|
||||||
|
return cal;
|
||||||
},
|
},
|
||||||
Err(_e) => return cal,
|
Err(_e) => {
|
||||||
}
|
return cal
|
||||||
},
|
},
|
||||||
Err(_e) => return cal,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue