Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to include FlashDevice device description #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ name = "flash-algo"
test = false
bench = false

[features]
device_description = []

[profile.dev]
codegen-units = 1
debug = 2
Expand Down
19 changes: 18 additions & 1 deletion link.x
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SECTIONS {

*(.sdata)
*(.sdata.*)

*(.bss)
*(.bss.*)

Expand All @@ -35,6 +35,23 @@ SECTIONS {
. = ALIGN(4);
}

/*
* Adding PrgData section in order to satisfy tools that need it.
*/
PrgData : {
KEEP(*(.PrgData))
KEEP(*(.PrgData.*))

. = ALIGN(4);
}

DevDscr : {
KEEP(*(.DevDscr))
KEEP(*(.DevDscr.*))

. = ALIGN(4);
}

/DISCARD/ : {
/* Unused exception related info that only wastes space */
*(.ARM.exidx);
Expand Down
3 changes: 2 additions & 1 deletion src/algo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![macro_use]


use core::num::NonZeroU32;

#[panic_handler]
Expand Down Expand Up @@ -34,7 +33,9 @@ pub trait FlashAlgo: Sized + 'static {
#[macro_export]
macro_rules! algo {
($type:ty) => {
#[no_mangle]
static mut _IS_INIT: bool = false;
#[no_mangle]
static mut _ALGO_INSTANCE: MaybeUninit<$type> = MaybeUninit::uninit();

#[no_mangle]
Expand Down
47 changes: 47 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,53 @@ const PAGE_SIZE: u32 = 256;
const BLOCK_ERASE_CMD: u8 = 0xd8;
const FLASH_BASE: u32 = 0x1000_0000;

#[repr(C)]
pub struct FlashDevice {
pub version: u16,
pub device_name: [u8; 128],
pub device_type: u16,
pub device_address: u32,
pub size_device: u32,
pub size_page: u32,
pub reserved: u32,
pub val_empty: u8,
pub timeout_program: u32,
pub timeout_erase: u32,
pub sectors: [u32; 4],
}

#[cfg(feature = "device_description")]
#[no_mangle]
#[link_section = ".PrgData"]
pub static dummy: u32 = 0;

#[cfg(feature = "device_description")]
#[no_mangle]
#[link_section = ".DevDscr"]
pub static FlashDevice: FlashDevice = FlashDevice {
version: 1, // Version 1.01
device_name: [
0x52, 0x61, 0x73, 0x70, 0x65, 0x72, 0x72, 0x79, 0x20, 0x50, 0x69, 0x20, 0x52, 0x50, 0x32,
0x30, 0x34, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
], // "Rasperry Pi RP2040"
device_type: 5, // External SPI
device_address: FLASH_BASE, // Default device start address
size_device: 16 * 1024 * 1024, // Total Size of device (16 MiB)
size_page: PAGE_SIZE, // Programming page size
reserved: 0, // Must be zero
val_empty: 0xFF, // Content of erase memory
timeout_program: 500, // 500 ms
timeout_erase: 5000, // 5 s
sectors: [SECTOR_SIZE, FLASH_BASE, 0xFFFFFFFF, 0xFFFFFFFF],
};

impl FlashAlgo for RP2040Algo {
fn new(_address: u32, _clock: u32, _function: u32) -> Result<Self, ErrorCode> {
let funcs = ROMFuncs::load();
Expand Down