Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Can I write multiple times without erase in between? #39

Closed
t-moe opened this issue Mar 6, 2024 · 5 comments
Closed

Can I write multiple times without erase in between? #39

t-moe opened this issue Mar 6, 2024 · 5 comments

Comments

@t-moe
Copy link

t-moe commented Mar 6, 2024

Currently the MultiwriteNorFlash trait is not implemented in this crate. Is this on purpose? I could not find out whether the underlying ROM function would actually allow this...

Thank you

@bjoernQ
Copy link
Contributor

bjoernQ commented Mar 6, 2024

I think we cannot implement embedded-storage-async in a reasonable way since the ROM functions are blocking (and also not much documentation is available besides what is in the esp-idf source)

We could try to implement things w/o the ROM functions which would give us all the freedom we want but that's a lot of effort I guess - there is not much documentation available (again - besides what is in esp-idf)

@t-moe
Copy link
Author

t-moe commented Mar 6, 2024

oh wait, I was unclear.

My question is not whether or not we can do async. Its more about whether or not it is safe to write multiple times, without erase. e.g. if the following is ok:

Writes to the same word twice are now allowed. The result is the logical AND of the previous data and the written data. That is, it is only possible to change 1 bits to 0 bits.

If power is lost during write:

Bits that were 1 on flash and are written to 1 are guaranteed to stay as 1
Bits that were 1 on flash and are written to 0 are undefined
Bits that were 0 on flash are guaranteed to stay as 0
Rest of the bits in the page are guaranteed to be unchanged


I'm using the excellent sequential-storage crate and wrap embedded-storage in a blocking adapter( BlockingAsync). In order to pop items, the flash implementation need to fulfill these properties ^^

@t-moe t-moe changed the title MultiwriteNorFlash trait? Can I write multiple times without erase in between? Mar 6, 2024
@bjoernQ
Copy link
Contributor

bjoernQ commented Mar 6, 2024

Ah ok - I was confused since the link was for embedded-storage-async. t.b.h. I haven't tested that but I guess it should work

@t-moe
Copy link
Author

t-moe commented Mar 6, 2024

Yeah sorry about that. I totally forgot that I'm using the wrapper and only wanted to know about the multiple writes.

Ok, but then you also dont have more information about the rom function and I'll have to do a small test....

Thank you for answering

@t-moe
Copy link
Author

t-moe commented Mar 8, 2024

I've validated this, and its indeed supported to write multiple times. The result is a bitwise-and. Perfect.

Test Code
#![no_std]
#![no_main]

use esp32c6_hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, Rng};
use esp_backtrace as _;
use esp_storage::FlashStorage;
use embedded_storage::nor_flash::NorFlash;
use embedded_storage::nor_flash::ReadNorFlash;
#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    let system = peripherals.SYSTEM.split();
    let _clocks = ClockControl::max(system.clock_control).freeze();
    let mut rng = Rng::new(peripherals.RNG);
    esp_println::logger::init_logger_from_env();


    let mut buf1 = [0u8; 4096];
    let mut buf2 = [0u8; 4096];
    let mut buf3 = [0u8; 4096];
    let mut buf4 = [0u8; 4096];
    rng.read(&mut buf1).unwrap();
    rng.read(&mut buf2).unwrap();
    for i in 0..4096 {
        buf3[i] = buf1[i] & buf2[i];
    }

    let mut fs = FlashStorage::new();
    fs.erase(0x9000, 0x9000+0x4000).unwrap();
    log::info!("Flash erased");
    fs.write(0x9000, &buf1).unwrap();
    log::info!("Wrote buf1");
    fs.write(0x9000, &buf2).unwrap();
    log::info!("Wrote buf2");

    fs.read(0x9000, &mut buf4).unwrap();
    log::info!("Read buf4 which should be buf1 & buf2 (bitwise and)");

    assert_eq!(buf3, buf4);
    log::info!("Success");

    loop {}
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants