Skip to content

Commit 877d21b

Browse files
authored
Merge pull request #8 from toxxin/add-read-flash
add missing ReadFlash(optional) function
2 parents 007e610 + 947a6b7 commit 877d21b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "flash-algorithm"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
edition = "2021"
55
readme = "README.md"
66
keywords = ["no-std", "embedded", "flashing"]
@@ -14,4 +14,5 @@ description = "A crate to write CMSIS-DAP flash algorithms for flashing embedded
1414
default = ["erase-chip", "panic-handler"]
1515
erase-chip = []
1616
panic-handler = []
17+
read-flash = []
1718
verify = []

src/lib.rs

+37
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ pub trait FlashAlgorithm: Sized + 'static {
7575
/// * `data` - The data to compare with.
7676
#[cfg(feature = "verify")]
7777
fn verify(&mut self, address: u32, size: u32, data: Option<&[u8]>) -> Result<(), ErrorCode>;
78+
79+
/// Read flash.
80+
///
81+
/// # Arguments
82+
///
83+
/// * `address` - The start address of the flash to read.
84+
/// * `data` - The data.
85+
#[cfg(feature = "read-flash")]
86+
fn read_flash(&mut self, address: u32, data: &mut [u8]) -> Result<(), ErrorCode>;
7887
}
7988

8089
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
@@ -167,6 +176,7 @@ macro_rules! algorithm {
167176
}
168177
}
169178
$crate::erase_chip!($type);
179+
$crate::read_flash!($type);
170180
$crate::verify!($type);
171181

172182
#[allow(non_upper_case_globals)]
@@ -270,6 +280,33 @@ macro_rules! erase_chip {
270280
};
271281
}
272282

283+
#[doc(hidden)]
284+
#[macro_export]
285+
#[cfg(not(feature = "read-flash"))]
286+
macro_rules! read_flash {
287+
($type:ty) => {};
288+
}
289+
#[doc(hidden)]
290+
#[macro_export]
291+
#[cfg(feature = "read-flash")]
292+
macro_rules! read_flash {
293+
($type:ty) => {
294+
#[no_mangle]
295+
#[link_section = ".entry"]
296+
pub unsafe extern "C" fn ReadFlash(addr: u32, size: u32, data: *mut u8) -> u32 {
297+
if !_IS_INIT {
298+
return 1;
299+
}
300+
let this = &mut *_ALGO_INSTANCE.as_mut_ptr();
301+
let data_slice: &mut [u8] = unsafe { core::slice::from_raw_parts_mut(data, size as usize) };
302+
match <$type as $crate::FlashAlgorithm>::read_flash(this, addr, data_slice) {
303+
Ok(()) => 0,
304+
Err(e) => e.get(),
305+
}
306+
}
307+
};
308+
}
309+
273310
#[doc(hidden)]
274311
#[macro_export]
275312
#[cfg(not(feature = "verify"))]

0 commit comments

Comments
 (0)