Skip to content

Commit

Permalink
Use the flash_algorithm crate.
Browse files Browse the repository at this point in the history
I wasn't sure what the error code should be for ROM Func load failure.
  • Loading branch information
thejpster committed Dec 9, 2023
1 parent 1d215be commit 10ab451
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 133 deletions.
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
124 changes: 0 additions & 124 deletions src/algo.rs

This file was deleted.

32 changes: 23 additions & 9 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,7 +49,16 @@ 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;
Expand All @@ -76,12 +81,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 Down

0 comments on commit 10ab451

Please sign in to comment.