Skip to content

Commit 8af3d68

Browse files
authored
Merge pull request #6 from probe-rs/task/extend-api
Extend the API macro to accept all arguments
2 parents dcf3f71 + 1eb3932 commit 8af3d68

File tree

3 files changed

+102
-15
lines changed

3 files changed

+102
-15
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "flash-algorithm"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
edition = "2021"
55
readme = "README.md"
66
keywords = ["no-std", "embedded", "flashing"]

examples/basic.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use flash_algorithm::FlashAlgorithm;
5+
6+
struct Algorithm;
7+
8+
flash_algorithm::algorithm!(Algorithm, {
9+
device_name: "test",
10+
device_type: DeviceType::Onchip,
11+
flash_address: 0x0,
12+
flash_size: 0x0,
13+
page_size: 0x0,
14+
empty_value: 0xFF,
15+
program_time_out: 1000,
16+
erase_time_out: 2000,
17+
sectors: [{
18+
size: 0x0,
19+
address: 0x0,
20+
}]
21+
});
22+
23+
impl FlashAlgorithm for Algorithm {
24+
fn new(
25+
_address: u32,
26+
_clock: u32,
27+
_function: flash_algorithm::Function,
28+
) -> Result<Self, flash_algorithm::ErrorCode> {
29+
todo!()
30+
}
31+
32+
fn erase_all(&mut self) -> Result<(), flash_algorithm::ErrorCode> {
33+
todo!()
34+
}
35+
36+
fn erase_sector(&mut self, _address: u32) -> Result<(), flash_algorithm::ErrorCode> {
37+
todo!()
38+
}
39+
40+
fn program_page(
41+
&mut self,
42+
_address: u32,
43+
_data: &[u8],
44+
) -> Result<(), flash_algorithm::ErrorCode> {
45+
todo!()
46+
}
47+
48+
fn verify(
49+
&mut self,
50+
_address: u32,
51+
_size: u32,
52+
_data: Option<&[u8]>,
53+
) -> Result<(), flash_algorithm::ErrorCode> {
54+
todo!()
55+
}
56+
}

src/lib.rs

+45-14
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ pub enum Function {
8585
#[macro_export]
8686
macro_rules! algorithm {
8787
($type:ty, {
88+
device_name: $device_name:expr,
89+
device_type: $device_type:expr,
8890
flash_address: $flash_address:expr,
8991
flash_size: $flash_size:expr,
9092
page_size: $page_size:expr,
9193
empty_value: $empty_value:expr,
94+
program_time_out: $program_time_out:expr,
95+
erase_time_out: $erase_time_out:expr,
9296
sectors: [$({
9397
size: $size:expr,
9498
address: $address:expr,
@@ -97,6 +101,8 @@ macro_rules! algorithm {
97101
static mut _IS_INIT: bool = false;
98102
static mut _ALGO_INSTANCE: core::mem::MaybeUninit<$type> = core::mem::MaybeUninit::uninit();
99103

104+
core::arch::global_asm!(".section .PrgData, \"aw\"");
105+
100106
#[no_mangle]
101107
#[link_section = ".entry"]
102108
pub unsafe extern "C" fn Init(addr: u32, clock: u32, function: u32) -> u32 {
@@ -108,9 +114,9 @@ macro_rules! algorithm {
108114
1 => $crate::Function::Erase,
109115
2 => $crate::Function::Program,
110116
3 => $crate::Function::Verify,
111-
_ => panic!("This branch can only be reached if the host library sent an unknown function code.")
117+
_ => core::panic!("This branch can only be reached if the host library sent an unknown function code.")
112118
};
113-
match <$type as FlashAlgorithm>::new(addr, clock, function) {
119+
match <$type as $crate::FlashAlgorithm>::new(addr, clock, function) {
114120
Ok(inst) => {
115121
_ALGO_INSTANCE.as_mut_ptr().write(inst);
116122
_IS_INIT = true;
@@ -136,7 +142,7 @@ macro_rules! algorithm {
136142
return 1;
137143
}
138144
let this = &mut *_ALGO_INSTANCE.as_mut_ptr();
139-
match <$type as FlashAlgorithm>::erase_sector(this, addr) {
145+
match <$type as $crate::FlashAlgorithm>::erase_sector(this, addr) {
140146
Ok(()) => 0,
141147
Err(e) => e.get(),
142148
}
@@ -149,7 +155,7 @@ macro_rules! algorithm {
149155
}
150156
let this = &mut *_ALGO_INSTANCE.as_mut_ptr();
151157
let data_slice: &[u8] = unsafe { core::slice::from_raw_parts(data, size as usize) };
152-
match <$type as FlashAlgorithm>::program_page(this, addr, data_slice) {
158+
match <$type as $crate::FlashAlgorithm>::program_page(this, addr, data_slice) {
153159
Ok(()) => 0,
154160
Err(e) => e.get(),
155161
}
@@ -163,24 +169,24 @@ macro_rules! algorithm {
163169
#[link_section = "DeviceData"]
164170
pub static FlashDevice: FlashDeviceDescription = FlashDeviceDescription {
165171
// The version is never read by probe-rs and can be fixed.
166-
vers: 0x0,
172+
vers: 0x1,
167173
// The device name here can be customized but it really has no real use
168174
// appart from identifying the device the ELF is intended for which we have
169175
// in our YAML.
170-
dev_name: [0u8; 128],
176+
dev_name: $crate::arrayify_string($device_name),
171177
// The specification does not specify the values that can go here,
172178
// but this value means internal flash device.
173-
dev_type: 5,
179+
dev_type: $device_type,
174180
dev_addr: $flash_address,
175181
device_size: $flash_size,
176182
page_size: $page_size,
177183
_reserved: 0,
178184
// The empty state of a byte in flash.
179185
empty: $empty_value,
180186
// This value can be used to estimate the amount of time the flashing procedure takes worst case.
181-
program_time_out: 1000,
187+
program_time_out: $program_time_out,
182188
// This value can be used to estimate the amount of time the erasing procedure takes worst case.
183-
erase_time_out: 2000,
189+
erase_time_out: $erase_time_out,
184190
flash_sectors: [
185191
$(
186192
FlashSector {
@@ -200,7 +206,7 @@ macro_rules! algorithm {
200206
pub struct FlashDeviceDescription {
201207
vers: u16,
202208
dev_name: [u8; 128],
203-
dev_type: u16,
209+
dev_type: DeviceType,
204210
dev_addr: u32,
205211
device_size: u32,
206212
page_size: u32,
@@ -218,6 +224,17 @@ macro_rules! algorithm {
218224
size: u32,
219225
address: u32,
220226
}
227+
228+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
229+
#[repr(u16)]
230+
pub enum DeviceType {
231+
Unknown = 0,
232+
Onchip = 1,
233+
Ext8Bit = 2,
234+
Ext16Bit = 3,
235+
Ext32Bit = 4,
236+
ExtSpi = 5,
237+
}
221238
};
222239
}
223240

@@ -239,7 +256,7 @@ macro_rules! erase_chip {
239256
return 1;
240257
}
241258
let this = &mut *_ALGO_INSTANCE.as_mut_ptr();
242-
match <$type as FlashAlgorithm>::erase_all(this) {
259+
match <$type as $crate::FlashAlgorithm>::erase_all(this) {
243260
Ok(()) => 0,
244261
Err(e) => e.get(),
245262
}
@@ -267,13 +284,14 @@ macro_rules! verify {
267284
let this = &mut *_ALGO_INSTANCE.as_mut_ptr();
268285

269286
if data.is_null() {
270-
match <$type as FlashAlgorithm>::verify(this, addr, size, None) {
287+
match <$type as $crate::FlashAlgorithm>::verify(this, addr, size, None) {
271288
Ok(()) => 0,
272289
Err(e) => e.get(),
273290
}
274291
} else {
275292
let data_slice: &[u8] = unsafe { core::slice::from_raw_parts(data, size as usize) };
276-
match <$type as FlashAlgorithm>::verify(this, addr, size, Some(data_slice)) {
293+
match <$type as $crate::FlashAlgorithm>::verify(this, addr, size, Some(data_slice))
294+
{
277295
Ok(()) => 0,
278296
Err(e) => e.get(),
279297
}
@@ -286,5 +304,18 @@ macro_rules! verify {
286304
#[macro_export]
287305
macro_rules! count {
288306
() => (0usize);
289-
( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));
307+
( $x:tt $($xs:tt)* ) => (1usize + $crate::count!($($xs)*));
308+
}
309+
310+
pub const fn arrayify_string<const N: usize>(msg: &'static str) -> [u8; N] {
311+
let mut arr = [0u8; N];
312+
let mut idx = 0;
313+
let msg_bytes = msg.as_bytes();
314+
315+
while (idx < msg_bytes.len()) && (idx < N) {
316+
arr[idx] = msg_bytes[idx];
317+
idx += 1;
318+
}
319+
320+
arr
290321
}

0 commit comments

Comments
 (0)