Skip to content

Commit 3338159

Browse files
committed
Add the flash device description structures
1 parent 6c9b089 commit 3338159

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
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.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55
readme = "README.md"
66
keywords = ["no-std", "embedded", "flashing"]

memory.x

+24
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ SECTIONS {
3535
. = ALIGN(4);
3636
}
3737

38+
/* Section for data, specified by flashloader standard. */
39+
PrgData : {
40+
*(.data .data.*)
41+
*(.sdata .sdata.*)
42+
}
43+
44+
PrgData : {
45+
/* Zero-initialized data */
46+
*(.bss .bss.*)
47+
*(.sbss .sbss.*)
48+
49+
*(COMMON)
50+
}
51+
52+
/* Description of the flash algorithm */
53+
DeviceData . : {
54+
/* The device data content is only for external tools,
55+
* and usually not referenced by the code.
56+
*
57+
* The KEEP statement ensures it's not removed by accident.
58+
*/
59+
KEEP(*(DeviceData))
60+
}
61+
3862
/DISCARD/ : {
3963
/* Unused exception related info that only wastes space */
4064
*(.ARM.exidx);

src/lib.rs

+78-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ pub enum Function {
6464
/// and checking the flash algorithm initialization status.
6565
#[macro_export]
6666
macro_rules! algorithm {
67-
($type:ty) => {
67+
($type:ty, {
68+
flash_address: $flash_address:expr,
69+
flash_size: $flash_size:expr,
70+
page_size: $page_size:expr,
71+
sectors: [$({
72+
size: $size:expr,
73+
address: $address:expr,
74+
}),+]
75+
}) => {
6876
static mut _IS_INIT: bool = false;
6977
static mut _ALGO_INSTANCE: MaybeUninit<$type> = MaybeUninit::uninit();
7078

@@ -136,5 +144,74 @@ macro_rules! algorithm {
136144
Err(e) => e.get(),
137145
}
138146
}
147+
148+
#[allow(non_upper_case_globals)]
149+
#[no_mangle]
150+
#[used]
151+
#[link_section = "DeviceData"]
152+
pub static FlashDevice: FlashDeviceDescription = FlashDeviceDescription {
153+
// The version is never read by probe-rs and can be fixed.
154+
vers: 0x0,
155+
// The device name here can be customized but it really has no real use
156+
// appart from identifying the device the ELF is intended for which we have
157+
// in our YAML.
158+
dev_name: [0u8; 128],
159+
// The specification does not specify the values that can go here,
160+
// but this value means internal flash device.
161+
dev_type: 5,
162+
dev_addr: $flash_address,
163+
device_size: $flash_size,
164+
page_size: $page_size,
165+
_reserved: 0,
166+
// The empty state of a byte in flash.
167+
empty: 0xff,
168+
// This value can be used to estimate the amount of time the flashing procedure takes worst case.
169+
program_time_out: 1000,
170+
// This value can be used to estimate the amount of time the erasing procedure takes worst case.
171+
erase_time_out: 2000,
172+
flash_sectors: [
173+
$(
174+
FlashSector {
175+
size: $size,
176+
address: $address,
177+
}
178+
),+,
179+
// This marks the end of the flash sector list.
180+
FlashSector {
181+
size: 0xffff_ffff,
182+
address: 0xffff_ffff,
183+
}
184+
],
185+
};
186+
187+
#[repr(C)]
188+
pub struct FlashDeviceDescription {
189+
vers: u16,
190+
dev_name: [u8; 128],
191+
dev_type: u16,
192+
dev_addr: u32,
193+
device_size: u32,
194+
page_size: u32,
195+
_reserved: u32,
196+
empty: u8,
197+
program_time_out: u32,
198+
erase_time_out: u32,
199+
200+
flash_sectors: [FlashSector; $crate::count!($($size)*) + 1],
201+
}
202+
203+
#[repr(C)]
204+
#[derive(Copy, Clone)]
205+
pub struct FlashSector {
206+
size: u32,
207+
address: u32,
208+
}
139209
};
140210
}
211+
212+
#[doc(hidden)]
213+
#[macro_export]
214+
macro_rules! count {
215+
() => (0usize);
216+
( $x:tt $($xs:tt)* ) => (1usize + count!($($xs)*));
217+
}

0 commit comments

Comments
 (0)