Skip to content

Commit

Permalink
Only place the flash operations in ram, add CS to read
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 9, 2023
1 parent e74397c commit 6ca0a41
Showing 1 changed file with 37 additions and 40 deletions.
77 changes: 37 additions & 40 deletions norfs-esp32s3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Drop for UnlockToken<'_> {
}

#[link_section = ".rwtext"]
#[inline(never)]
#[inline(always)]
fn wait_idle() -> Result<(), MediumError> {
const SR_WIP: u32 = 1 << 0;

Expand All @@ -83,8 +83,11 @@ fn wait_idle() -> Result<(), MediumError> {
}

#[link_section = ".rwtext"]
fn write_impl(offset: usize, ptr: *const u32, len: usize) -> Result<(), MediumError> {
#[inline(never)]
fn write_impl(offset: usize, data: &[u8]) -> Result<(), MediumError> {
maybe_with_critical_section(|| {
let len = data.len();
let ptr = data.as_ptr().cast();
if esp_rom_spiflash_write(offset as u32, ptr, len as u32) == 0 {
wait_idle()
} else {
Expand All @@ -94,6 +97,7 @@ fn write_impl(offset: usize, ptr: *const u32, len: usize) -> Result<(), MediumEr
}

#[link_section = ".rwtext"]
#[inline(never)]
fn erase_sector_impl(sector: usize) -> Result<(), MediumError> {
maybe_with_critical_section(|| {
if esp_rom_spiflash_erase_sector(sector as u32) == 0 {
Expand All @@ -105,6 +109,7 @@ fn erase_sector_impl(sector: usize) -> Result<(), MediumError> {
}

#[link_section = ".rwtext"]
#[inline(never)]
fn erase_block_impl(block: usize) -> Result<(), MediumError> {
maybe_with_critical_section(|| {
if esp_rom_spiflash_erase_block(block as u32) == 0 {
Expand All @@ -115,6 +120,20 @@ fn erase_block_impl(block: usize) -> Result<(), MediumError> {
})
}

#[link_section = ".rwtext"]
#[inline(never)]
fn read_impl(offset: usize, data: &mut [u8]) -> Result<(), MediumError> {
maybe_with_critical_section(|| {
let len = data.len() as u32;
let ptr = data.as_mut_ptr().cast();
if esp_rom_spiflash_read(offset as u32, ptr, len) == 0 {
Ok(())
} else {
Err(MediumError::Read)
}
})
}

pub struct InternalDriver<P: InternalPartition> {
unlocked: bool,
_partition: P,
Expand All @@ -138,6 +157,10 @@ impl<P: InternalPartition> InternalDriver<P> {

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

fn offset(block: usize, offset: usize) -> usize {
P::OFFSET + block * Self::BLOCK_SIZE + offset
}
}

impl<P: InternalPartition> AlignedStorage for InternalDriver<P> {
Expand All @@ -153,7 +176,6 @@ impl<P: InternalPartition> AlignedStorage for InternalDriver<P> {
let block = offset + block;

let result = erase_block_impl(block);

yield_now().await;

result
Expand All @@ -165,23 +187,14 @@ impl<P: InternalPartition> AlignedStorage for InternalDriver<P> {
offset: usize,
data: &mut [u8],
) -> Result<(), MediumError> {
let len = data.len() as u32;
let ptr = data.as_mut_ptr().cast();

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

let result = if esp_rom_spiflash_read(offset as u32, ptr, len) == 0 {
Ok(())
} else {
Err(MediumError::Read)
};
let offset = Self::offset(block, offset);

let result = read_impl(offset, data);
yield_now().await;

result
}

#[link_section = ".rwtext"]
async fn write_aligned(
&mut self,
block: usize,
Expand All @@ -190,13 +203,8 @@ impl<P: InternalPartition> AlignedStorage for InternalDriver<P> {
) -> Result<(), MediumError> {
let _token = self.unlock()?;

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

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

let result = write_impl(offset, ptr, len);

let offset = Self::offset(block, offset);
let result = write_impl(offset, data);
yield_now().await;

result
Expand Down Expand Up @@ -226,6 +234,10 @@ impl<P: InternalPartition> SmallInternalDriver<P> {

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

fn offset(block: usize, offset: usize) -> usize {
P::OFFSET + block * Self::BLOCK_SIZE + offset
}
}

impl<P: InternalPartition> AlignedStorage for SmallInternalDriver<P> {
Expand All @@ -241,7 +253,6 @@ impl<P: InternalPartition> AlignedStorage for SmallInternalDriver<P> {
let block = offset + block;

let result = erase_sector_impl(block);

yield_now().await;

result
Expand All @@ -253,17 +264,8 @@ impl<P: InternalPartition> AlignedStorage for SmallInternalDriver<P> {
offset: usize,
data: &mut [u8],
) -> Result<(), MediumError> {
let len = data.len() as u32;
let ptr = data.as_mut_ptr().cast();

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

let result = if esp_rom_spiflash_read(offset as u32, ptr, len) == 0 {
Ok(())
} else {
Err(MediumError::Read)
};

let offset = Self::offset(block, offset);
let result = read_impl(offset, data);
yield_now().await;

result
Expand All @@ -277,13 +279,8 @@ impl<P: InternalPartition> AlignedStorage for SmallInternalDriver<P> {
) -> Result<(), MediumError> {
let _token = self.unlock()?;

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

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

let result = write_impl(offset, ptr, len);

let offset = Self::offset(block, offset);
let result = write_impl(offset, data);
yield_now().await;

result
Expand Down

0 comments on commit 6ca0a41

Please sign in to comment.