esp-hal/examples/src/bin/flash_read_write.rs
Dániel Buga 7f8af8a651
Remove prelude (#2845)
* Remove prelude

* Changelog
2024-12-20 10:24:57 +00:00

51 lines
1.5 KiB
Rust

//! Writes and reads flash memory.
//!
//! Uses flash address 0x9000 (default NVS)
//! See https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/partition-tables.html#built-in-partition-tables
//% FEATURES: esp-storage
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
#![no_std]
#![no_main]
use embedded_storage::{ReadStorage, Storage};
use esp_backtrace as _;
use esp_hal::entry;
use esp_println::println;
use esp_storage::FlashStorage;
#[entry]
fn main() -> ! {
let mut bytes = [0u8; 32];
let mut flash = FlashStorage::new();
let flash_addr = 0x9000;
println!("Flash size = {}", flash.capacity());
println!();
flash.read(flash_addr, &mut bytes).unwrap();
println!("Read from {:x}: {:02x?}", flash_addr, &bytes[..32]);
bytes[0x00] = bytes[0x00].wrapping_add(1);
bytes[0x01] = bytes[0x01].wrapping_add(2);
bytes[0x02] = bytes[0x02].wrapping_add(3);
bytes[0x03] = bytes[0x03].wrapping_add(4);
bytes[0x04] = bytes[0x04].wrapping_add(1);
bytes[0x05] = bytes[0x05].wrapping_add(2);
bytes[0x06] = bytes[0x06].wrapping_add(3);
bytes[0x07] = bytes[0x07].wrapping_add(4);
flash.write(flash_addr, &bytes).unwrap();
println!("Written to {:x}: {:02x?}", flash_addr, &bytes[..32]);
let mut reread_bytes = [0u8; 32];
flash.read(flash_addr, &mut reread_bytes).unwrap();
println!("Read from {:x}: {:02x?}", flash_addr, &reread_bytes[..32]);
println!("Reset (CTRL-R in espflash) to re-read the persisted data.");
loop {}
}