Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cmd to erase multiple sectors #20

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ cat <<EOF
pc_uninit: $(sym UnInit)
pc_program_page: $(sym ProgramPage)
pc_erase_sector: $(sym EraseSector)
pc_erase_range: $(sym EraseRange)
pc_erase_all: $(sym EraseChip)
EOF
22 changes: 22 additions & 0 deletions src/algo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub trait FlashAlgo: Sized + 'static {
/// Erase sector. May only be called after init() with FUNCTION_ERASE
fn erase_sector(&mut self, addr: u32) -> 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>;
}
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
Loading