ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
esphome::storage::BlockDevice Class Referenceabstract

A device addressed in fixed size blocks, such as a flash chip, an SD card or a RAM buffer. More...

#include <block_device.h>

Inheritance diagram for esphome::storage::BlockDevice:
esphome::ram_disk::RamDisk

Public Member Functions

StorageError read (uint32_t block, uint32_t offset, void *buf, uint32_t len)
 
StorageError program (uint32_t block, uint32_t offset, const void *buf, uint32_t len)
 
StorageError erase (uint32_t block, uint32_t count)
 
StorageError sync ()
 Return once all earlier writes are stored on the media.
 
StorageError trim (uint32_t block, uint32_t count)
 Tell the device that the blocks no longer hold data it needs to keep.
 
const BlockGeometry & get_geometry () const
 
bool has_media () const
 
uint64_t get_capacity () const
 
uint8_t get_media_generation () const
 Changes each time media is inserted or removed, so a user can tell that its cached state is stale.
 
DeviceMutex & mutex ()
 

Protected Member Functions

virtual StorageError device_read (uint32_t block, uint32_t offset, void *buf, uint32_t len)=0
 
virtual StorageError device_program (uint32_t block, uint32_t offset, const void *buf, uint32_t len)=0
 
virtual StorageError device_erase (uint32_t block, uint32_t count)
 
virtual StorageError device_sync ()
 
virtual StorageError device_trim (uint32_t block, uint32_t count)
 
void media_changed_ ()
 
void dump_geometry_ (const char *tag) const
 
StorageError check_access_ (uint32_t block, uint32_t offset, uint32_t len, uint16_t align, bool write) const
 
StorageError check_range_ (uint32_t block, uint32_t count) const
 

Protected Attributes

BlockGeometry geometry_ {}
 
DeviceMutex mutex_
 
uint8_t media_generation_ {0}
 

Detailed Description

A device addressed in fixed size blocks, such as a flash chip, an SD card or a RAM buffer.

Callers use the public methods, which check the address range, alignment and read-only state once, so a driver only implements the transfer. An access may span several blocks.

With USE_STORAGE_LOCK (a filesystem used from other tasks) each transfer holds mutex(). A driver that touches the device from its own loop() or timers must hold it too. The getters do not lock: a caller on another task that needs the geometry to stay as it is holds mutex() while it uses it.

Definition at line 61 of file block_device.h.

Member Function Documentation

◆ check_access_()

StorageError esphome::storage::BlockDevice::check_access_ ( uint32_t block,
uint32_t offset,
uint32_t len,
uint16_t align,
bool write ) const
protected

Definition at line 20 of file block_device.cpp.

◆ check_range_()

StorageError esphome::storage::BlockDevice::check_range_ ( uint32_t block,
uint32_t count ) const
protected

Definition at line 34 of file block_device.cpp.

◆ device_erase()

virtual StorageError esphome::storage::BlockDevice::device_erase ( uint32_t block,
uint32_t count )
inlineprotectedvirtual

Definition at line 83 of file block_device.h.

◆ device_program()

virtual StorageError esphome::storage::BlockDevice::device_program ( uint32_t block,
uint32_t offset,
const void * buf,
uint32_t len )
protectedpure virtual

Implemented in esphome::ram_disk::RamDisk.

◆ device_read()

virtual StorageError esphome::storage::BlockDevice::device_read ( uint32_t block,
uint32_t offset,
void * buf,
uint32_t len )
protectedpure virtual

Implemented in esphome::ram_disk::RamDisk.

◆ device_sync()

virtual StorageError esphome::storage::BlockDevice::device_sync ( )
inlineprotectedvirtual

Definition at line 84 of file block_device.h.

◆ device_trim()

virtual StorageError esphome::storage::BlockDevice::device_trim ( uint32_t block,
uint32_t count )
inlineprotectedvirtual

Definition at line 85 of file block_device.h.

◆ dump_geometry_()

void esphome::storage::BlockDevice::dump_geometry_ ( const char * tag) const
protected

Definition at line 93 of file block_device.cpp.

◆ erase()

StorageError esphome::storage::BlockDevice::erase ( uint32_t block,
uint32_t count )

Definition at line 69 of file block_device.cpp.

◆ get_capacity()

uint64_t esphome::storage::BlockDevice::get_capacity ( ) const
inline

Definition at line 73 of file block_device.h.

◆ get_geometry()

const BlockGeometry & esphome::storage::BlockDevice::get_geometry ( ) const
inline

Definition at line 71 of file block_device.h.

◆ get_media_generation()

uint8_t esphome::storage::BlockDevice::get_media_generation ( ) const
inline

Changes each time media is inserted or removed, so a user can tell that its cached state is stale.

Definition at line 77 of file block_device.h.

◆ has_media()

bool esphome::storage::BlockDevice::has_media ( ) const
inline

Definition at line 72 of file block_device.h.

◆ media_changed_()

void esphome::storage::BlockDevice::media_changed_ ( )
inlineprotected

Definition at line 87 of file block_device.h.

◆ mutex()

DeviceMutex & esphome::storage::BlockDevice::mutex ( )
inline

Definition at line 78 of file block_device.h.

◆ program()

StorageError esphome::storage::BlockDevice::program ( uint32_t block,
uint32_t offset,
const void * buf,
uint32_t len )

Definition at line 58 of file block_device.cpp.

◆ read()

StorageError esphome::storage::BlockDevice::read ( uint32_t block,
uint32_t offset,
void * buf,
uint32_t len )

Definition at line 44 of file block_device.cpp.

◆ sync()

StorageError esphome::storage::BlockDevice::sync ( )

Return once all earlier writes are stored on the media.

Definition at line 78 of file block_device.cpp.

◆ trim()

StorageError esphome::storage::BlockDevice::trim ( uint32_t block,
uint32_t count )

Tell the device that the blocks no longer hold data it needs to keep.

Definition at line 85 of file block_device.cpp.

Field Documentation

◆ geometry_

BlockGeometry esphome::storage::BlockDevice::geometry_ {}
protected

Definition at line 93 of file block_device.h.

◆ media_generation_

uint8_t esphome::storage::BlockDevice::media_generation_ {0}
protected

Definition at line 95 of file block_device.h.

◆ mutex_

DeviceMutex esphome::storage::BlockDevice::mutex_
protected

Definition at line 94 of file block_device.h.


The documentation for this class was generated from the following files: