Skip to content

Commit 5b03fb8

Browse files
committed
Add feature to include FlashDevice device description
1 parent 1bf17aa commit 5b03fb8

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ name = "flash-algo"
1414
test = false
1515
bench = false
1616

17+
[features]
18+
device_description = []
19+
1720
[profile.dev]
1821
codegen-units = 1
1922
debug = 2

link.x

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ SECTIONS {
2525

2626
*(.sdata)
2727
*(.sdata.*)
28-
28+
2929
*(.bss)
3030
*(.bss.*)
3131

@@ -35,6 +35,17 @@ SECTIONS {
3535
. = ALIGN(4);
3636
}
3737

38+
/*
39+
* The device description is usually in DevDscr section, but adding it to
40+
* PrgData in order to satisfy tools that need this section.
41+
*/
42+
PrgData : {
43+
KEEP(*(.DevDscr))
44+
KEEP(*(.DevDscr.*))
45+
46+
. = ALIGN(4);
47+
}
48+
3849
/DISCARD/ : {
3950
/* Unused exception related info that only wastes space */
4051
*(.ARM.exidx);

src/algo.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![macro_use]
22

3-
43
use core::num::NonZeroU32;
54

65
#[panic_handler]
@@ -34,7 +33,9 @@ pub trait FlashAlgo: Sized + 'static {
3433
#[macro_export]
3534
macro_rules! algo {
3635
($type:ty) => {
36+
#[no_mangle]
3737
static mut _IS_INIT: bool = false;
38+
#[no_mangle]
3839
static mut _ALGO_INSTANCE: MaybeUninit<$type> = MaybeUninit::uninit();
3940

4041
#[no_mangle]

src/main.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,48 @@ const PAGE_SIZE: u32 = 256;
6363
const BLOCK_ERASE_CMD: u8 = 0xd8;
6464
const FLASH_BASE: u32 = 0x1000_0000;
6565

66+
#[repr(C)]
67+
pub struct FlashDevice {
68+
pub version: u16,
69+
pub device_name: [u8; 128],
70+
pub device_type: u16,
71+
pub device_address: u32,
72+
pub size_device: u32,
73+
pub size_page: u32,
74+
pub reserved: u32,
75+
pub val_empty: u8,
76+
pub timeout_program: u32,
77+
pub timeout_erase: u32,
78+
pub sectors: [u32; 4],
79+
}
80+
81+
#[cfg(feature = "device_description")]
82+
#[no_mangle]
83+
#[link_section = ".DevDscr"]
84+
pub static FlashDevice: FlashDevice = FlashDevice {
85+
version: 1, // Version 1.01
86+
device_name: [
87+
0x52, 0x61, 0x73, 0x70, 0x65, 0x72, 0x72, 0x79, 0x20, 0x50, 0x69, 0x20, 0x52, 0x50, 0x32,
88+
0x30, 0x34, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96+
], // "Rasperry Pi RP2040"
97+
device_type: 5, // External SPI
98+
device_address: FLASH_BASE, // Default device start address
99+
size_device: 16 * 1024 * 1024, // Total Size of device (16 MiB)
100+
size_page: PAGE_SIZE, // Programming page size
101+
reserved: 0, // Must be zero
102+
val_empty: 0xFF, // Content of erase memory
103+
timeout_program: 500, // 500 ms
104+
timeout_erase: 5000, // 5 s
105+
sectors: [SECTOR_SIZE, FLASH_BASE, 0xFFFFFFFF, 0xFFFFFFFF],
106+
};
107+
66108
impl FlashAlgo for RP2040Algo {
67109
fn new(_address: u32, _clock: u32, _function: u32) -> Result<Self, ErrorCode> {
68110
let funcs = ROMFuncs::load();

0 commit comments

Comments
 (0)