Skip to content

Commit ff54dfa

Browse files
committed
Improve FlashStorage documentation
1 parent b1aedf6 commit ff54dfa

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

esp-storage/src/common.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ impl<'d> Flash<'d> {
6666
#[derive(Debug)]
6767
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6868
/// Flash storage abstraction.
69+
///
70+
/// This can write to any location on the SPI Flash.
71+
/// It is recommended to create a
72+
#[doc = concat!("[partition](https://docs.espressif.com/projects/esp-idf/en/latest/", esp_hal::chip!(), "/api-guides/partition-tables.html)")]
73+
/// to store any app data.
6974
pub struct FlashStorage<'d> {
7075
pub(crate) capacity: usize,
7176
unlocked: bool,

esp-storage/src/nor_flash.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ impl NorFlash for FlashStorage<'_> {
140140
const WRITE_SIZE: usize = Self::WORD_SIZE as _;
141141
const ERASE_SIZE: usize = Self::SECTOR_SIZE as _;
142142

143+
/// Writes `bytes` to the flash storage at `offset`.
144+
///
145+
/// Note that NOR flash is only able to flip bits from 1 to 0. Attempting to
146+
/// flip a bit from 0 to 1 will silently fail. It may be necessary to call
147+
/// [`FlashStorage::erase`] before writing to ensure all bits being written
148+
/// to are 1.
149+
///
150+
/// Requirements:
151+
/// - `offset` must be aligned to a word (4 bytes).
152+
/// - `bytes.len()` must be a multiple of the word size.
153+
///
154+
/// Requirements not being met will lead to an error.
155+
///
156+
/// See [`NorFlash::write`] for more information.
143157
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
144158
const WS: u32 = FlashStorage::WORD_SIZE;
145159
self.check_alignment::<{ WS }>(offset, bytes.len())?;
@@ -174,6 +188,15 @@ impl NorFlash for FlashStorage<'_> {
174188
Ok(())
175189
}
176190

191+
/// Erases sectors at bytes `[from..to]`
192+
///
193+
/// Requirements:
194+
/// - `from` must be aligned to a sector (4096 bytes).
195+
/// - `to - from` must be a multiple of the sector size.
196+
///
197+
/// Requirements not being met will lead to an error.
198+
///
199+
/// See [`NorFlash::erase`] for more information.
177200
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
178201
let len = (to - from) as _;
179202
const SZ: u32 = FlashStorage::SECTOR_SIZE;

0 commit comments

Comments
 (0)