Improve flash operation logging and serialization padding
- Added detailed logging for flash write and erase operations. - Ensured serialized save data is aligned to 4-byte boundaries.
This commit is contained in:
@@ -2,7 +2,7 @@ use alloc::vec::Vec;
|
|||||||
use bincode::{Decode, Encode};
|
use bincode::{Decode, Encode};
|
||||||
use embedded_savegame::storage::{Flash, Storage};
|
use embedded_savegame::storage::{Flash, Storage};
|
||||||
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
|
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
|
||||||
use esp_bootloader_esp_idf::partitions::{Error as PartitionError, FlashRegion};
|
use esp_bootloader_esp_idf::partitions::{Error as PartitionError, Error, FlashRegion};
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
@@ -58,7 +58,16 @@ impl Flash for SavegameFlashAdapter<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn write(&mut self, addr: u32, data: &mut [u8]) -> Result<(), Self::Error> {
|
fn write(&mut self, addr: u32, data: &mut [u8]) -> Result<(), Self::Error> {
|
||||||
NorFlash::write(self.region, addr, data).map_err(SavegameFlashError)
|
info!(
|
||||||
|
"Relative writing to flash at 0x{:x} with length {}",
|
||||||
|
addr,
|
||||||
|
data.len()
|
||||||
|
);
|
||||||
|
let error = NorFlash::write(self.region, addr, data);
|
||||||
|
if error.is_err() {
|
||||||
|
error!("error {:?}", error.unwrap_err())
|
||||||
|
}
|
||||||
|
error.map_err(SavegameFlashError)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Erase one full slot at `addr`.
|
/// Erase one full slot at `addr`.
|
||||||
@@ -74,6 +83,11 @@ impl Flash for SavegameFlashAdapter<'_> {
|
|||||||
let end = addr + SAVEGAME_SLOT_SIZE as u32;
|
let end = addr + SAVEGAME_SLOT_SIZE as u32;
|
||||||
let aligned_end = end.div_ceil(ERASE_SIZE) * ERASE_SIZE;
|
let aligned_end = end.div_ceil(ERASE_SIZE) * ERASE_SIZE;
|
||||||
|
|
||||||
|
info!(
|
||||||
|
"Relative erasing flash at 0x{:x} (aligned to 0x{:x}-0x{:x})",
|
||||||
|
addr, aligned_start, aligned_end
|
||||||
|
);
|
||||||
|
|
||||||
if aligned_start != addr || aligned_end != end {
|
if aligned_start != addr || aligned_end != end {
|
||||||
log::warn!("Flash erase address not aligned: addr=0x{:x}, slot_size=0x{:x}. Aligned to 0x{:x}-0x{:x}", addr, SAVEGAME_SLOT_SIZE, aligned_start, aligned_end);
|
log::warn!("Flash erase address not aligned: addr=0x{:x}, slot_size=0x{:x}. Aligned to 0x{:x}-0x{:x}", addr, SAVEGAME_SLOT_SIZE, aligned_start, aligned_end);
|
||||||
}
|
}
|
||||||
@@ -125,7 +139,14 @@ impl SavegameManager {
|
|||||||
data: data.to_vec(),
|
data: data.to_vec(),
|
||||||
};
|
};
|
||||||
let mut serialized = bincode::encode_to_vec(&wrapper, bincode::config::standard())?;
|
let mut serialized = bincode::encode_to_vec(&wrapper, bincode::config::standard())?;
|
||||||
info!("Serialized config with size {}", serialized.len());
|
|
||||||
|
// Flash storage often requires length to be a multiple of 4.
|
||||||
|
let padding = (4 - (serialized.len() % 4)) % 4;
|
||||||
|
if padding > 0 {
|
||||||
|
serialized.extend_from_slice(&[0u8; 4][..padding]);
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("Serialized config with size {} (padded)", serialized.len());
|
||||||
self.storage.append(&mut serialized)?;
|
self.storage.append(&mut serialized)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user