Skip to content

Commit c55092b

Browse files
committed
Extend the API macro to accept all arguments
1 parent dcf3f71 commit c55092b

File tree

3 files changed

+100
-15
lines changed

3 files changed

+100
-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

+43-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,
@@ -108,9 +112,9 @@ macro_rules! algorithm {
108112
1 => $crate::Function::Erase,
109113
2 => $crate::Function::Program,
110114
3 => $crate::Function::Verify,
111-
_ => panic!("This branch can only be reached if the host library sent an unknown function code.")
115+
_ => core::panic!("This branch can only be reached if the host library sent an unknown function code.")
112116
};
113-
match <$type as FlashAlgorithm>::new(addr, clock, function) {
117+
match <$type as $crate::FlashAlgorithm>::new(addr, clock, function) {
114118
Ok(inst) => {
115119
_ALGO_INSTANCE.as_mut_ptr().write(inst);
116120
_IS_INIT = true;
@@ -136,7 +140,7 @@ macro_rules! algorithm {
136140
return 1;
137141
}
138142
let this = &mut *_ALGO_INSTANCE.as_mut_ptr();
139-
match <$type as FlashAlgorithm>::erase_sector(this, addr) {
143+
match <$type as $crate::FlashAlgorithm>::erase_sector(this, addr) {
140144
Ok(()) => 0,
141145
Err(e) => e.get(),
142146
}
@@ -149,7 +153,7 @@ macro_rules! algorithm {
149153
}
150154
let this = &mut *_ALGO_INSTANCE.as_mut_ptr();
151155
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) {
156+
match <$type as $crate::FlashAlgorithm>::program_page(this, addr, data_slice) {
153157
Ok(()) => 0,
154158
Err(e) => e.get(),
155159
}
@@ -163,24 +167,24 @@ macro_rules! algorithm {
163167
#[link_section = "DeviceData"]
164168
pub static FlashDevice: FlashDeviceDescription = FlashDeviceDescription {
165169
// The version is never read by probe-rs and can be fixed.
166-
vers: 0x0,
170+
vers: 0x1,
167171
// The device name here can be customized but it really has no real use
168172
// appart from identifying the device the ELF is intended for which we have
169173
// in our YAML.
170-
dev_name: [0u8; 128],
174+
dev_name: $crate::arrayify_string($device_name),
171175
// The specification does not specify the values that can go here,
172176
// but this value means internal flash device.
173-
dev_type: 5,
177+
dev_type: $device_type,
174178
dev_addr: $flash_address,
175179
device_size: $flash_size,
176180
page_size: $page_size,
177181
_reserved: 0,
178182
// The empty state of a byte in flash.
179183
empty: $empty_value,
180184
// This value can be used to estimate the amount of time the flashing procedure takes worst case.
181-
program_time_out: 1000,
185+
program_time_out: $program_time_out,
182186
// This value can be used to estimate the amount of time the erasing procedure takes worst case.
183-
erase_time_out: 2000,
187+
erase_time_out: $erase_time_out,
184188
flash_sectors: [
185189
$(
186190
FlashSector {
@@ -200,7 +204,7 @@ macro_rules! algorithm {
200204
pub struct FlashDeviceDescription {
201205
vers: u16,
202206
dev_name: [u8; 128],
203-
dev_type: u16,
207+
dev_type: DeviceType,
204208
dev_addr: u32,
205209
device_size: u32,
206210
page_size: u32,
@@ -218,6 +222,17 @@ macro_rules! algorithm {
218222
size: u32,
219223
address: u32,
220224
}
225+
226+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
227+
#[repr(u16)]
228+
pub enum DeviceType {
229+
Unknown = 0,
230+
Onchip = 1,
231+
Ext8Bit = 2,
232+
Ext16Bit = 3,
233+
Ext32Bit = 4,
234+
ExtSpi = 5,
235+
}
221236
};
222237
}
223238

@@ -239,7 +254,7 @@ macro_rules! erase_chip {
239254
return 1;
240255
}
241256
let this = &mut *_ALGO_INSTANCE.as_mut_ptr();
242-
match <$type as FlashAlgorithm>::erase_all(this) {
257+
match <$type as $crate::FlashAlgorithm>::erase_all(this) {
243258
Ok(()) => 0,
244259
Err(e) => e.get(),
245260
}
@@ -267,13 +282,14 @@ macro_rules! verify {
267282
let this = &mut *_ALGO_INSTANCE.as_mut_ptr();
268283

269284
if data.is_null() {
270-
match <$type as FlashAlgorithm>::verify(this, addr, size, None) {
285+
match <$type as $crate::FlashAlgorithm>::verify(this, addr, size, None) {
271286
Ok(()) => 0,
272287
Err(e) => e.get(),
273288
}
274289
} else {
275290
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)) {
291+
match <$type as $crate::FlashAlgorithm>::verify(this, addr, size, Some(data_slice))
292+
{
277293
Ok(()) => 0,
278294
Err(e) => e.get(),
279295
}
@@ -286,5 +302,18 @@ macro_rules! verify {
286302
#[macro_export]
287303
macro_rules! count {
288304
() => (0usize);
289-
( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));
305+
( $x:tt $($xs:tt)* ) => (1usize + $crate::count!($($xs)*));
306+
}
307+
308+
pub const fn arrayify_string<const N: usize>(msg: &'static str) -> [u8; N] {
309+
let mut arr = [0u8; N];
310+
let mut idx = 0;
311+
let msg_bytes = msg.as_bytes();
312+
313+
while (idx < msg_bytes.len()) && (idx < N) {
314+
arr[idx] = msg_bytes[idx];
315+
idx += 1;
316+
}
317+
318+
arr
290319
}

0 commit comments

Comments
 (0)