Skip to content

Commit

Permalink
Use 4k sectors as blocks, unlock before writes
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Sep 27, 2023
1 parent 03efc10 commit 41b2be0
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions norfs-esp32s3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use norfs_driver::{
};

const WRITE_GRANULARITY: WriteGranularity = WriteGranularity::Bit;
const BLOCK_SIZE: usize = 65536;
const BLOCK_SIZE: usize = 4096;
const PAGE_SIZE: usize = 256;

#[cfg(feature = "critical-section")]
Expand Down Expand Up @@ -48,7 +48,8 @@ macro_rules! rom_fn {
rom_fn!(
fn esp_rom_spiflash_read(src_addr: u32, data: *mut u32, len: u32) -> i32 = 0x40000a20,
fn esp_rom_spiflash_unlock() -> i32 = 0x40000a2c,
fn esp_rom_spiflash_erase_block(block_number: u32) -> i32 = 0x40000a08,
//fn esp_rom_spiflash_erase_block(block_number: u32) -> i32 = 0x40000a08,
fn esp_rom_spiflash_erase_sector(block_number: u32) -> i32 = 0x400009fc,
fn esp_rom_spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 = 0x40000a14,
fn esp_rom_spiflash_read_user_cmd(status: *mut u32, cmd: u8) -> i32 = 0x40000a5c
);
Expand All @@ -58,6 +59,13 @@ pub trait InternalPartition {
const SIZE: usize;
}

struct UnlockToken<'a>(&'a mut bool);
impl Drop for UnlockToken<'_> {
fn drop(&mut self) {
*self.0 = false;
}
}

pub struct InternalDriver<P: InternalPartition> {
unlocked: bool,
_partition: P,
Expand All @@ -71,18 +79,18 @@ impl<P: InternalPartition> InternalDriver<P> {
}
}

fn unlock(&mut self) -> Result<(), MediumError> {
fn unlock(&mut self) -> Result<UnlockToken<'_>, MediumError> {
if !self.unlocked {
if esp_rom_spiflash_unlock() != 0 {
return Err(MediumError::Write);
}
self.unlocked = true;
}

Ok(())
Ok(UnlockToken(&mut self.unlocked))
}

async fn wait_idle(&mut self) -> Result<(), MediumError> {
async fn wait_idle() -> Result<(), MediumError> {
const SR_WIP: u32 = 1 << 0;

let mut status = 0x00;
Expand All @@ -105,13 +113,13 @@ impl<P: InternalPartition> AlignedStorage for InternalDriver<P> {
const WRITE_GRANULARITY: WriteGranularity = WRITE_GRANULARITY;

async fn erase(&mut self, block: usize) -> Result<(), MediumError> {
self.unlock()?;
let _token = self.unlock()?;

let offset = P::OFFSET / Self::BLOCK_SIZE;
let block = offset + block;

if esp_rom_spiflash_erase_block(block as u32) == 0 {
self.wait_idle().await
if esp_rom_spiflash_erase_sector(block as u32) == 0 {
Self::wait_idle().await
} else {
Err(MediumError::Erase)
}
Expand Down Expand Up @@ -141,13 +149,15 @@ impl<P: InternalPartition> AlignedStorage for InternalDriver<P> {
offset: usize,
data: &[u8],
) -> Result<(), MediumError> {
let _token = self.unlock()?;

let len = data.len() as u32;
let ptr = data.as_ptr().cast();

let offset = P::OFFSET + block * Self::BLOCK_SIZE + offset;

if esp_rom_spiflash_write(offset as u32, ptr, len) == 0 {
self.wait_idle().await
Self::wait_idle().await
} else {
Err(MediumError::Write)
}
Expand Down

0 comments on commit 41b2be0

Please sign in to comment.