reformat early_settings interactions so that it only writes if data is actually changed

also abstract get/set off the early settings FLASH section, easier to maintain

still todo: make hosted mode work again
This commit is contained in:
Gianguido Sorà 2023-05-01 18:25:12 +02:00
parent b10a51a3bd
commit 42bc425ca9
2 changed files with 33 additions and 36 deletions

View file

@ -31,11 +31,34 @@ impl Slot {
} }
} }
// TODO(gsora): implement this so that it's a no-op on hosted mode
struct State { struct State {
settings_page: MemoryRange, settings_page: MemoryRange,
spinor: Spinor, spinor: Spinor,
} }
impl State {
fn set(&self, data: &[u8], slot: &Slot) {
let settings: &[u8] = self.settings_page.as_slice();
let new_data_u32 = u32::from_le_bytes(data.try_into().unwrap());
if new_data_u32 == self.get(slot) {
return;
}
self.spinor
.patch(settings, xous::EARLY_SETTINGS, &data, slot.offset)
.expect("couldn't patch slot data");
}
fn get(&self, slot: &Slot) -> u32 {
let settings: &[u8] = self.settings_page.as_slice();
u32::from_le_bytes(settings[slot.range()].try_into().unwrap())
}
}
fn main() -> ! { fn main() -> ! {
log_server::init_wait().unwrap(); log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Info); log::set_max_level(log::LevelFilter::Info);
@ -63,46 +86,20 @@ fn main() -> ! {
log::trace!("Message: {:?}", msg); log::trace!("Message: {:?}", msg);
match FromPrimitive::from_usize(msg.body.id()) { match FromPrimitive::from_usize(msg.body.id()) {
Some(Opcode::GetKeymap) => { Some(Opcode::GetKeymap) => {
let settings: &[u8] = state.settings_page.as_slice(); xous::return_scalar(msg.sender, state.get(&KEYMAP) as usize).unwrap();
let code = u32::from_le_bytes(settings[KEYMAP.range()].try_into().unwrap());
xous::return_scalar(msg.sender, code as usize).unwrap();
} }
Some(Opcode::SetKeymap) => msg_blocking_scalar_unpack!(msg, map, _, _, _, { Some(Opcode::SetKeymap) => msg_blocking_scalar_unpack!(msg, map, _, _, _, {
let code = (map as u32).to_le_bytes(); let code = (map as u32).to_le_bytes();
let settings: &[u8] = state.settings_page.as_slice(); state.set(&code, &KEYMAP);
state
.spinor
.patch(settings, xous::EARLY_SETTINGS, &code, KEYMAP.offset)
.expect("couldn't patch our keyboard code");
log::info!("writing early keymap: {}", map);
xous::return_scalar(msg.sender, 0).unwrap(); xous::return_scalar(msg.sender, 0).unwrap();
}), }),
Some(Opcode::SetEarlySleep) => msg_blocking_scalar_unpack!(msg, value, _, _, _, { Some(Opcode::SetEarlySleep) => msg_blocking_scalar_unpack!(msg, value, _, _, _, {
let code = (value as u32).to_le_bytes(); let code = (value as u32).to_le_bytes();
let settings: &[u8] = state.settings_page.as_slice(); state.set(&code, &EARLY_SLEEP);
state
.spinor
.patch(settings, xous::EARLY_SETTINGS, &code, EARLY_SLEEP.offset)
.expect("couldn't patch early reboot flag");
log::info!("writing must sleep on reboot: {}", value);
xous::return_scalar(msg.sender, 0).unwrap(); xous::return_scalar(msg.sender, 0).unwrap();
}), }),
Some(Opcode::EarlySleep) => { Some(Opcode::EarlySleep) => {
let settings: &[u8] = state.settings_page.as_slice(); xous::return_scalar(msg.sender, state.get(&EARLY_SLEEP) as usize).unwrap();
let value = u32::from_le_bytes(settings[EARLY_SLEEP.range()].try_into().unwrap());
log::info!("value read for early sleep: {}", value);
xous::return_scalar(msg.sender, value as usize).unwrap();
} }
_ => log::warn!("unrecognized opcode"), _ => log::warn!("unrecognized opcode"),
} }

View file

@ -630,10 +630,10 @@ fn wrapped_main() -> ! {
// get the last status // get the last status
let must_sleep = early_settings.early_sleep().unwrap(); let must_sleep = early_settings.early_sleep().unwrap();
// reset it for good measure
early_settings.set_early_sleep(false).unwrap();
if must_sleep { if must_sleep {
// reset it for good measure
early_settings.set_early_sleep(false).unwrap();
while ((llio.adc_vbus().unwrap() as u32) * 503) > 150_000 { while ((llio.adc_vbus().unwrap() as u32) * 503) > 150_000 {
modals.show_notification(t!("mainmenu.cant_sleep", xous::LANG), None).expect("couldn't notify that power is plugged in"); modals.show_notification(t!("mainmenu.cant_sleep", xous::LANG), None).expect("couldn't notify that power is plugged in");
} }