diff --git a/build.sh b/build.sh index b0e5f67..7940c33 100755 --- a/build.sh +++ b/build.sh @@ -30,5 +30,6 @@ cat < Result<(), ErrorCode>; + /// Erase a range of memory. May only be called after init() with FUNCTION_ERASE + fn erase_range(&mut self, start_addr: u32, end_addr: u32) -> Result<(), ErrorCode>; + /// Program bytes. May only be called after init() with FUNCTION_PROGRAM fn program_page(&mut self, addr: u32, size: u32, data: *const u8) -> Result<(), ErrorCode>; } @@ -102,6 +105,25 @@ macro_rules! algo { Err(e) => e.get(), } } + /// Erase a range of memory on the flash chip. + /// Algo can use page/block erase commands to improve performance vs EraseSector + /// + /// # Safety + /// + /// Will erase all memory inside the address range. + /// Must pass a valid sector start and end address. + #[no_mangle] + #[link_section = ".entry"] + pub unsafe extern "C" fn EraseRange(start_addr: u32, end_addr: u32) -> u32 { + if !_IS_INIT { + return 1; + } + let this = &mut *_ALGO_INSTANCE.as_mut_ptr(); + match <$type as FlashAlgo>::erase_range(this, start_addr, end_addr) { + Ok(()) => 0, + Err(e) => e.get(), + } + } /// Write to a page on the flash chip. /// /// # Safety diff --git a/src/main.rs b/src/main.rs index d181a73..4ae8bfd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,6 +80,16 @@ impl FlashAlgo for RP2040Algo { Ok(()) } + fn erase_range(&mut self, start_addr: u32, end_addr: u32) -> Result<(), ErrorCode> { + (self.funcs.flash_range_erase)( + start_addr - FLASH_BASE, + end_addr - start_addr, + BLOCK_SIZE, + BLOCK_ERASE_CMD, + ); + Ok(()) + } + fn program_page(&mut self, addr: u32, size: u32, data: *const u8) -> Result<(), ErrorCode> { (self.funcs.flash_range_program)(addr - FLASH_BASE, data, size); Ok(())