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

Use flash algorithm, add linker sections, remove erase_all #21

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "0.1.0"

[dependencies]
cortex-m = "0.7.0"
flash-algorithm = "0.4.0"

# this lets you use `cargo fix`!
[[bin]]
Expand Down
24 changes: 24 additions & 0 deletions link.x
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ SECTIONS {
. = ALIGN(4);
}

/* Section for data, specified by flashloader standard. */
PrgData : {
/*
* We're explicitly putting a single object here (PRGDATA_Start in main.c) as this is required by some tools.
* It is not used by this algorithm
*/
KEEP(*(PrgData))

. = ALIGN(4);
}

/* Description of the flash algorithm */
DevDscr . : {
/* The device data content is only for external tools,
* and usually not referenced by the code.
* All rules have exceptions: device data is used by this flash algo.
*
* The KEEP statement ensures it's not removed by accident.
*/
KEEP(*(DeviceData))

. = ALIGN(4);
}

/DISCARD/ : {
/* Unused exception related info that only wastes space */
*(.ARM.exidx);
Expand Down
124 changes: 0 additions & 124 deletions src/algo.rs

This file was deleted.

47 changes: 35 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#![no_std]
#![no_main]

mod algo;

use core::mem::MaybeUninit;

use self::algo::*;
use flash_algorithm::*;

fn find_func<T>(tag: [u8; 2]) -> Option<T> {
let tag = u16::from_le_bytes(tag) as u32;
Expand Down Expand Up @@ -53,15 +49,23 @@ struct RP2040Algo {
funcs: ROMFuncs,
}

algo!(RP2040Algo);
algorithm!(RP2040Algo, {
flash_address: 0x1000_0000,
flash_size: 0x0100_0000,
page_size: 0x100,
empty_value: 0xFF,
sectors: [{
size: 0x1000,
address: 0x10000000,
}]
});

const BLOCK_SIZE: u32 = 65536;
const SECTOR_SIZE: u32 = 4096;
const BLOCK_ERASE_CMD: u8 = 0xd8;
const FLASH_BASE: u32 = 0x1000_0000;

impl FlashAlgo for RP2040Algo {
fn new(_address: u32, _clock: u32, _function: u32) -> Result<Self, ErrorCode> {
impl FlashAlgorithm for RP2040Algo {
fn new(_address: u32, _clock: u32, _function: Function) -> Result<Self, ErrorCode> {
let Some(funcs) = ROMFuncs::load() else {
return Err(ErrorCode::new(1).unwrap());
};
Expand All @@ -76,12 +80,21 @@ impl FlashAlgo for RP2040Algo {
}

fn erase_sector(&mut self, addr: u32) -> Result<(), ErrorCode> {
(self.funcs.flash_range_erase)(addr - FLASH_BASE, SECTOR_SIZE, BLOCK_SIZE, BLOCK_ERASE_CMD);
(self.funcs.flash_range_erase)(
addr - FlashDevice.dev_addr,
SECTOR_SIZE,
BLOCK_SIZE,
BLOCK_ERASE_CMD,
);
Ok(())
}

fn program_page(&mut self, addr: u32, size: u32, data: *const u8) -> Result<(), ErrorCode> {
(self.funcs.flash_range_program)(addr - FLASH_BASE, data, size);
fn program_page(&mut self, addr: u32, data: &[u8]) -> Result<(), ErrorCode> {
(self.funcs.flash_range_program)(
addr - FlashDevice.dev_addr,
data.as_ptr(),
data.len() as u32,
);
Ok(())
}
}
Expand All @@ -92,3 +105,13 @@ impl Drop for RP2040Algo {
(self.funcs.flash_enter_cmd_xip)();
}
}

/// Some tools (eg Segger's debugger) require the PrgData section to exist in the target binary
///
/// They scan the flashloader binary for this symbol to determine the section location
/// If they cannot find it, the tool exits. This variable serves no other purpose
#[allow(non_upper_case_globals)]
#[no_mangle]
#[used]
#[link_section = "PrgData"]
pub static mut PRGDATA_Start: usize = 0;
Loading