diff --git a/Cargo.toml b/Cargo.toml index cecf914..ca22b36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "flash-algorithm" -version = "0.5.0" +version = "0.6.0" edition = "2021" readme = "README.md" keywords = ["no-std", "embedded", "flashing"] @@ -14,4 +14,5 @@ description = "A crate to write CMSIS-DAP flash algorithms for flashing embedded default = ["erase-chip", "panic-handler"] erase-chip = [] panic-handler = [] +read-flash = [] verify = [] diff --git a/src/lib.rs b/src/lib.rs index a414479..0e0e421 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,6 +75,15 @@ pub trait FlashAlgorithm: Sized + 'static { /// * `data` - The data to compare with. #[cfg(feature = "verify")] fn verify(&mut self, address: u32, size: u32, data: Option<&[u8]>) -> Result<(), ErrorCode>; + + /// Read flash. + /// + /// # Arguments + /// + /// * `address` - The start address of the flash to read. + /// * `data` - The data. + #[cfg(feature = "read-flash")] + fn read_flash(&mut self, address: u32, data: &mut [u8]) -> Result<(), ErrorCode>; } #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] @@ -167,6 +176,7 @@ macro_rules! algorithm { } } $crate::erase_chip!($type); + $crate::read_flash!($type); $crate::verify!($type); #[allow(non_upper_case_globals)] @@ -270,6 +280,33 @@ macro_rules! erase_chip { }; } +#[doc(hidden)] +#[macro_export] +#[cfg(not(feature = "read-flash"))] +macro_rules! read_flash { + ($type:ty) => {}; +} +#[doc(hidden)] +#[macro_export] +#[cfg(feature = "read-flash")] +macro_rules! read_flash { + ($type:ty) => { + #[no_mangle] + #[link_section = ".entry"] + pub unsafe extern "C" fn ReadFlash(addr: u32, size: u32, data: *mut u8) -> u32 { + if !_IS_INIT { + return 1; + } + let this = &mut *_ALGO_INSTANCE.as_mut_ptr(); + let data_slice: &mut [u8] = unsafe { core::slice::from_raw_parts_mut(data, size as usize) }; + match <$type as $crate::FlashAlgorithm>::read_flash(this, addr, data_slice) { + Ok(()) => 0, + Err(e) => e.get(), + } + } + }; +} + #[doc(hidden)] #[macro_export] #[cfg(not(feature = "verify"))]