Building calibration load, fails to do things tho
Signed-off-by: fly <merspieler@alwaysdata.com>
This commit is contained in:
parent
19c5b1c97e
commit
0f2a51299c
1 changed files with 34 additions and 12 deletions
|
@ -125,7 +125,10 @@ fn main() -> ! {
|
||||||
let mut adc1 = adc::Adc::adc1(p.ADC1, clocks);
|
let mut adc1 = adc::Adc::adc1(p.ADC1, clocks);
|
||||||
|
|
||||||
// ====================== Calibration ===============
|
// ====================== 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 =================
|
// ====================== Pin setup =================
|
||||||
let mut input_pins = MyPins {
|
let mut input_pins = MyPins {
|
||||||
|
@ -144,12 +147,6 @@ fn main() -> ! {
|
||||||
pwm.enable(Channel::C1);
|
pwm.enable(Channel::C1);
|
||||||
let pwm_max = pwm.get_max_duty(); //48000 in our case
|
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 =================
|
// ====================== Main loop =================
|
||||||
loop {
|
loop {
|
||||||
|
@ -256,7 +253,10 @@ fn save_calibration(flash: &mut FlashWriter, cal: &Calibration) -> bool {
|
||||||
let encoded_calibration_data = bytes_of(cal);
|
let encoded_calibration_data = bytes_of(cal);
|
||||||
data[3..][..encoded_calibration_data.len()].copy_from_slice(encoded_calibration_data);
|
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) {
|
match flash.write(FLASH_START + 64512, &data) {
|
||||||
Ok(_ret) => return true,
|
Ok(_ret) => return true,
|
||||||
Err(_e) => return false,
|
Err(_e) => return false,
|
||||||
|
@ -264,9 +264,31 @@ fn save_calibration(flash: &mut FlashWriter, cal: &Calibration) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load calibration to flash
|
// Load calibration to flash
|
||||||
fn load_calibration() -> Calibration {
|
fn load_calibration(flash: &mut FlashWriter) -> Calibration {
|
||||||
let cal = Calibration::new();
|
let cal = Calibration::new();
|
||||||
// TODO reject loading if version is off or no calibration data found
|
match flash.read(FLASH_START + 64512, 1024) {
|
||||||
// TODO return calibration data
|
Ok(data) => {
|
||||||
|
// Check if data is available and return early if not suitable
|
||||||
|
if data[2] != 1 {
|
||||||
return cal;
|
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,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue