Skip to content

Commit

Permalink
remove mock flash write_aligned
Browse files Browse the repository at this point in the history
  • Loading branch information
lulf committed Jan 7, 2024
1 parent 642aa2a commit 3ed4c96
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
28 changes: 19 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub mod mock_flash;
/// Many flashes have 4-byte or 1-byte words.
const MAX_WORD_SIZE: usize = 32;

// Type representing buffer aligned to 4 byte boundary.
#[repr(align(4))]
pub(crate) struct AlignedBuf<const SIZE: usize>(pub(crate) [u8; SIZE]);

async fn try_general_repair<S: NorFlash>(
flash: &mut S,
flash_range: Range<u32>,
Expand Down Expand Up @@ -55,10 +59,6 @@ async fn try_general_repair<S: NorFlash>(
Ok(())
}

/// Align the given buffer to the word size of the flash.
#[repr(align(4))]
struct AlignedBuf([u8; MAX_WORD_SIZE]);

/// Find the first page that is in the given page state.
///
/// The search starts at starting_page_index (and wraps around back to 0 if required)
Expand Down Expand Up @@ -390,6 +390,16 @@ mod tests {

type MockFlash = mock_flash::MockFlashBase<4, 4, 64>;

fn write_aligned(
flash: &mut MockFlash,
offset: u32,
bytes: &[u8],
) -> Result<(), mock_flash::MockFlashError> {
let mut buf = AlignedBuf([0; 256]);
buf.0[..bytes.len()].copy_from_slice(bytes);
block_on(flash.write(offset, &buf.0[..bytes.len()]))
}

#[test]
fn test_find_pages() {
// Page setup:
Expand All @@ -400,13 +410,13 @@ mod tests {

let mut flash = MockFlash::default();
// Page 0 markers
block_on(flash.write_aligned::<256>(0x000, &[MARKER, 0, 0, 0])).unwrap();
block_on(flash.write_aligned::<256>(0x100 - 4, &[0, 0, 0, MARKER])).unwrap();
write_aligned(&mut flash, 0x000, &[MARKER, 0, 0, 0]).unwrap();
write_aligned(&mut flash, 0x100 - 4, &[0, 0, 0, MARKER]).unwrap();
// Page 1 markers
block_on(flash.write_aligned::<256>(0x100, &[MARKER, 0, 0, 0])).unwrap();
block_on(flash.write_aligned::<256>(0x200 - 4, &[0, 0, 0, MARKER])).unwrap();
write_aligned(&mut flash, 0x100, &[MARKER, 0, 0, 0]).unwrap();
write_aligned(&mut flash, 0x200 - 4, &[0, 0, 0, MARKER]).unwrap();
// Page 2 markers
block_on(flash.write_aligned::<256>(0x200, &[MARKER, 0, 0, 0])).unwrap();
write_aligned(&mut flash, 0x200, &[MARKER, 0, 0, 0]).unwrap();

assert_eq!(
block_on(find_first_page(
Expand Down
13 changes: 0 additions & 13 deletions src/mock_flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,6 @@ impl<const PAGES: usize, const BYTES_PER_WORD: usize, const PAGE_WORDS: usize>
}
}

pub(crate) async fn write_aligned<const SIZE: usize>(
&mut self,
offset: u32,
bytes: &[u8],
) -> Result<(), MockFlashError> {
#[repr(align(4))]
struct AlignedBuf<const SIZE: usize>([u8; SIZE]);

let mut buf = AlignedBuf([0; SIZE]);
buf.0[..bytes.len()].copy_from_slice(bytes);
self.write(offset, &buf.0[..bytes.len()]).await
}

#[cfg(feature = "_test")]
/// Print all items in flash to the returned string
pub fn print_items(&mut self) -> String {
Expand Down

0 comments on commit 3ed4c96

Please sign in to comment.