From 0c20b0873107e64d6b6421dd3fadb6f862ee2f4b Mon Sep 17 00:00:00 2001 From: Mathias Brossard Date: Sat, 19 Feb 2022 23:42:53 -0600 Subject: [PATCH 1/4] Add feature to include FlashDevice device description --- Cargo.toml | 3 +++ link.x | 13 ++++++++++++- src/main.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 6a8e4e8..ff3b735 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,9 @@ name = "flash-algo" test = false bench = false +[features] +device_description = [] + [profile.dev] codegen-units = 1 debug = 2 diff --git a/link.x b/link.x index f2f2ee4..d966546 100644 --- a/link.x +++ b/link.x @@ -25,7 +25,7 @@ SECTIONS { *(.sdata) *(.sdata.*) - + *(.bss) *(.bss.*) @@ -35,6 +35,17 @@ SECTIONS { . = ALIGN(4); } + /* + * The device description is usually in DevDscr section, but adding it to + * PrgData in order to satisfy tools that need this section. + */ + PrgData : { + KEEP(*(.DevDscr)) + KEEP(*(.DevDscr.*)) + + . = ALIGN(4); + } + /DISCARD/ : { /* Unused exception related info that only wastes space */ *(.ARM.exidx); diff --git a/src/main.rs b/src/main.rs index 0a3555c..d8f706c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -152,6 +152,49 @@ algorithm!(RP2Algo, { const BLOCK_SIZE: u32 = 65536; const SECTOR_SIZE: u32 = 4096; 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 = ".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 FlashAlgorithm for RP2Algo { fn new(_address: u32, _clock: u32, _function: Function) -> Result { From c839f212f863f94e316f61b764537b11839bc206 Mon Sep 17 00:00:00 2001 From: Mathias Brossard Date: Sun, 20 Feb 2022 00:17:31 -0600 Subject: [PATCH 2/4] Add dummy PrgData section and move device description to DevDescr section --- link.x | 10 ++++++++-- src/main.rs | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/link.x b/link.x index d966546..630e43d 100644 --- a/link.x +++ b/link.x @@ -36,10 +36,16 @@ SECTIONS { } /* - * The device description is usually in DevDscr section, but adding it to - * PrgData in order to satisfy tools that need this section. + * Adding PrgData section in order to satisfy tools that need it. */ PrgData : { + KEEP(*(.PrgData)) + KEEP(*(.PrgData.*)) + + . = ALIGN(4); + } + + DevDscr : { KEEP(*(.DevDscr)) KEEP(*(.DevDscr.*)) diff --git a/src/main.rs b/src/main.rs index d8f706c..80f6430 100644 --- a/src/main.rs +++ b/src/main.rs @@ -169,6 +169,11 @@ pub struct FlashDevice { 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"] From b2bfad7d5090bd0b7dc27533c3e1eb9a50fadcd0 Mon Sep 17 00:00:00 2001 From: Mathias Brossard Date: Thu, 28 Nov 2024 11:55:15 -0600 Subject: [PATCH 3/4] Port functionality to updated flash-algorithm crate --- Cargo.toml | 5 +---- link.x | 13 ++++++++----- src/main.rs | 52 ++++------------------------------------------------ 3 files changed, 13 insertions(+), 57 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ff3b735..d0684b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ version = "0.1.0" [dependencies] cortex-m = "0.7.0" -flash-algorithm = { version = "0.4.0", default-features = false, features = [ +flash-algorithm = { version = "0.5.0", default-features = false, features = [ "panic-handler", ] } @@ -17,9 +17,6 @@ name = "flash-algo" test = false bench = false -[features] -device_description = [] - [profile.dev] codegen-units = 1 debug = 2 diff --git a/link.x b/link.x index 630e43d..dd08a85 100644 --- a/link.x +++ b/link.x @@ -45,11 +45,14 @@ SECTIONS { . = ALIGN(4); } - DevDscr : { - KEEP(*(.DevDscr)) - KEEP(*(.DevDscr.*)) - - . = ALIGN(4); + /* Description of the flash algorithm */ + DeviceData . : { + /* The device data content is only for external tools, + * and usually not referenced by the code. + * + * The KEEP statement ensures it's not removed by accident. + */ + KEEP(*(DeviceData)) } /DISCARD/ : { diff --git a/src/main.rs b/src/main.rs index 80f6430..690cdf2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -139,10 +139,14 @@ struct RP2Algo { } algorithm!(RP2Algo, { + device_name: "Raspberry Pi RP2", + device_type: DeviceType::ExtSpi, flash_address: 0x1000_0000, flash_size: 0x0100_0000, page_size: 0x100, empty_value: 0xFF, + program_time_out: 500, // 500 ms + erase_time_out: 5000, // 5 s sectors: [{ size: 0x1000, address: 0x10000000, @@ -152,54 +156,6 @@ algorithm!(RP2Algo, { const BLOCK_SIZE: u32 = 65536; const SECTOR_SIZE: u32 = 4096; 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 FlashAlgorithm for RP2Algo { fn new(_address: u32, _clock: u32, _function: Function) -> Result { From 416e74a536caa31477a6b974b5127a5d680a5ca9 Mon Sep 17 00:00:00 2001 From: Mathias Brossard Date: Thu, 19 Dec 2024 20:41:47 -0600 Subject: [PATCH 4/4] Fix warning related to inline-threshold parameter --- .cargo/config.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 8b2fc36..649d486 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -5,7 +5,7 @@ rustflags = [ "-C", "link-arg=-Tlink.x", # Code-size optimizations. - "-C", "inline-threshold=5", + "-Cllvm-args=--inline-threshold=5", "-C", "no-vectorize-loops", "-C", "force-frame-pointers=no",