ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
block_device.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <mutex>
5
8#include "esphome/core/log.h"
9
10namespace esphome::storage {
11
20
21enum BlockFlag : uint8_t {
22 BLOCK_FLAG_NEEDS_ERASE = 1 << 0, // Programming can only clear bits; erase sets a block to 0xFF
26};
27
30 uint32_t block_size{0}; // Erase unit in bytes
32 uint16_t read_size{1}; // Reads must be a multiple of this, at an offset that is a multiple of it
33 uint16_t program_size{1}; // As read_size, for writes
34 uint8_t flags{0};
35};
36
37const LogString *storage_error_to_string(StorageError error);
38
40struct NullMutex {
41 void lock() {}
42 bool try_lock() { return true; }
43 void unlock() {}
44};
45
46#ifdef USE_STORAGE_LOCK
48#else
50#endif
51
62 public:
63 StorageError read(uint32_t block, uint32_t offset, void *buf, uint32_t len);
64 StorageError program(uint32_t block, uint32_t offset, const void *buf, uint32_t len);
70
71 const BlockGeometry &get_geometry() const { return this->geometry_; }
72 bool has_media() const { return this->geometry_.block_count != 0; }
73 uint64_t get_capacity() const {
74 return static_cast<uint64_t>(this->geometry_.block_size) * this->geometry_.block_count;
75 }
77 uint8_t get_media_generation() const { return this->media_generation_; }
78 DeviceMutex &mutex() { return this->mutex_; }
79
80 protected:
81 virtual StorageError device_read(uint32_t block, uint32_t offset, void *buf, uint32_t len) = 0;
82 virtual StorageError device_program(uint32_t block, uint32_t offset, const void *buf, uint32_t len) = 0;
86
88 void dump_geometry_(const char *tag) const;
89
90 StorageError check_access_(uint32_t block, uint32_t offset, uint32_t len, uint16_t align, bool write) const;
92
96};
97
98} // namespace esphome::storage
Mutex implementation, with API based on the unavailable std::mutex.
Definition helpers.h:2455
A device addressed in fixed size blocks, such as a flash chip, an SD card or a RAM buffer.
StorageError sync()
Return once all earlier writes are stored on the media.
virtual StorageError device_program(uint32_t block, uint32_t offset, const void *buf, uint32_t len)=0
StorageError trim(uint32_t block, uint32_t count)
Tell the device that the blocks no longer hold data it needs to keep.
StorageError check_access_(uint32_t block, uint32_t offset, uint32_t len, uint16_t align, bool write) const
virtual StorageError device_read(uint32_t block, uint32_t offset, void *buf, uint32_t len)=0
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.
StorageError read(uint32_t block, uint32_t offset, void *buf, uint32_t len)
void dump_geometry_(const char *tag) const
virtual StorageError device_trim(uint32_t block, uint32_t count)
StorageError check_range_(uint32_t block, uint32_t count) const
uint64_t get_capacity() const
StorageError program(uint32_t block, uint32_t offset, const void *buf, uint32_t len)
const BlockGeometry & get_geometry() const
virtual StorageError device_erase(uint32_t block, uint32_t count)
StorageError erase(uint32_t block, uint32_t count)
virtual StorageError device_sync()
unsigned count
Definition gif.cpp:51
const LogString * storage_error_to_string(StorageError error)
const void size_t len
Definition hal.h:68
void const char * tag
Definition log.h:110
static void uint32_t
The shape of a block device. A block_count of 0 means no media is present.
Stands in for a mutex where nothing runs on other tasks.