VSF Documented
esp_flash.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2026 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18/*
19 * Clean-room re-implementation of ESP-IDF public API "esp_flash.h".
20 *
21 * Baseline: ESP-IDF v5.x public API. Only the surface area that
22 * real-world application / middleware code actually touches is exposed:
23 * read / write / erase_region / erase_chip / get_size / get_chip_id /
24 * plus the default-chip singleton.
25 *
26 * This shim layers directly on the same vk_mal_t "root mal" that the
27 * esp_partition port already consumes (see esp_partition_port.c). The
28 * full ESP-IDF chip stack (spi_flash_host_driver_t + chip_driver_t +
29 * os_functions) is not modelled: there is no physical SPI controller
30 * in the host environment.
31 *
32 * Encrypted variants (read_encrypted / write_encrypted) simply alias
33 * the plain variants; the shim has no flash-encryption layer.
34 *
35 * mmap is intentionally NOT exposed here. It is a cache/MMU facility
36 * on real targets and only meaningful for the main flash; applications
37 * that need a direct pointer should use esp_partition_mmap() which is
38 * already wired through the root mal's VSF_MAL_LOCAL_BUFFER capability.
39 */
40
41#ifndef __VSF_ESPIDF_ESP_FLASH_H__
42#define __VSF_ESPIDF_ESP_FLASH_H__
43
44/*============================ INCLUDES ======================================*/
45
46#include <stdint.h>
47#include <stdbool.h>
48#include <stddef.h>
49
50#include "esp_err.h"
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56/*============================ MACROS ========================================*/
57
58/* esp_flash-specific error codes. Values follow ESP-IDF v5.x. */
59#define ESP_ERR_FLASH_NOT_INITIALISED (ESP_ERR_FLASH_BASE + 1)
60#define ESP_ERR_FLASH_UNSUPPORTED_HOST (ESP_ERR_FLASH_BASE + 2)
61#define ESP_ERR_FLASH_UNSUPPORTED_CHIP (ESP_ERR_FLASH_BASE + 3)
62#define ESP_ERR_FLASH_PROTECTED (ESP_ERR_FLASH_BASE + 4)
63
64/*============================ TYPES =========================================*/
65
66/* Forward declarations. The root mal is opaque at this header. */
67struct vk_mal_t;
68
69/* SPI flash I/O mode hint. Tracked as a field on esp_flash_t for API
70 * compatibility; the shim ignores the value. */
71typedef enum {
82
83/* Runtime descriptor of one flash chip. Field layout matches the public
84 * portion of the ESP-IDF struct; application code only reads size / chip_id.
85 *
86 * Shim-specific fields:
87 * mal vk_mal_t backing the whole chip address space. Caller MUST
88 * keep this mal valid for the lifetime of the chip descriptor.
89 */
90typedef struct esp_flash_t {
91 void *host; /* unused; kept for ABI parity */
92 void *chip_drv; /* unused; kept for ABI parity */
93 void *os_func; /* unused; kept for ABI parity */
95
97 uint32_t size; /* chip size in bytes */
98 uint32_t chip_id; /* JEDEC ID or user-supplied */
99 uint32_t busy; /* kept for ABI parity, always 0 */
100
101 /* Opaque pointer to the backing vk_mal_t. Callers must NOT dereference
102 * this field directly; it is exposed to keep esp_flash_t a complete
103 * type so static allocation and struct embedding work. */
104 void *mal;
106
107/* Process-wide default chip handle. Points at the same root_mal that
108 * vsf_espidf_init() was given via vsf_espidf_cfg_t::partition.root_mal.
109 * NULL until the esp_flash sub-system has been initialised. All
110 * esp_flash_*() entry points accept a chip == NULL shorthand meaning
111 * "use the default chip".
112 */
114
115/*============================ PROTOTYPES ====================================*/
116
117/* --- Lifecycle -------------------------------------------------------- */
118
119/* Probe a chip descriptor. On real ESP-IDF this queries the physical flash
120 * for its JEDEC ID, unique ID, SFDP tables etc.; in this shim it is a
121 * validation pass: the caller-supplied esp_flash_t must have `mal` set and
122 * `size` must fit inside the backing mal. Returns ESP_OK on success. */
124
125/* No-op in this shim (present for ABI parity with ESP-IDF). Returns
126 * ESP_OK unconditionally. */
127extern esp_err_t esp_flash_init_os_functions(esp_flash_t *chip, int host_id,
128 int *out_attached_id);
130
131/* --- Identity --------------------------------------------------------- */
132
133/* Retrieve the flash chip ID. chip == NULL selects the default chip. */
134extern esp_err_t esp_flash_read_id(esp_flash_t *chip, uint32_t *out_id);
135
136/* Retrieve the flash chip size in bytes. Equivalent to chip->size.
137 * chip == NULL selects the default chip. */
138extern esp_err_t esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size);
139
140/* On real ESP-IDF this returns the chip's physical size even when the
141 * configured size is smaller. In this shim it always matches get_size().
142 */
144 uint32_t *out_size);
145
146/* --- I/O -------------------------------------------------------------- */
147
148/* Synchronous read / write / erase against the chip's address space.
149 * All offsets are absolute chip addresses; bounds are validated against
150 * chip->size. Must be called from a stack-owner thread (vsf_thread_t):
151 * the underlying vk_mal_t I/O is a peda subcall and only short-circuits
152 * to synchronous completion in that context. */
153extern esp_err_t esp_flash_read(esp_flash_t *chip, void *buffer,
154 uint32_t address, uint32_t length);
155extern esp_err_t esp_flash_write(esp_flash_t *chip, const void *buffer,
156 uint32_t address, uint32_t length);
158 uint32_t start, uint32_t len);
160
161/* Encrypted variants: alias to plain read/write. Provided for API
162 * compatibility only -- there is no flash-encryption layer in this shim.
163 */
165 void *out_buffer, uint32_t length);
167 const void *buffer, uint32_t length);
168
169#ifdef __cplusplus
170}
171#endif
172
173#endif /* __VSF_ESPIDF_ESP_FLASH_H__ */
mal class
Definition vsf_mal.h:168
int esp_err_t
Definition esp_err.h:41
esp_err_t esp_flash_init_os_functions(esp_flash_t *chip, int host_id, int *out_attached_id)
Definition esp_flash_port.c:160
esp_err_t esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
Definition esp_flash_port.c:213
esp_err_t esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
Definition esp_flash_port.c:251
esp_err_t esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size)
Definition esp_flash_port.c:192
esp_err_t esp_flash_get_physical_size(esp_flash_t *chip, uint32_t *out_size)
Definition esp_flash_port.c:205
esp_err_t esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length)
Definition esp_flash_port.c:285
esp_err_t esp_flash_init(esp_flash_t *chip)
Definition esp_flash_port.c:135
esp_err_t esp_flash_read_id(esp_flash_t *chip, uint32_t *out_id)
Definition esp_flash_port.c:179
esp_err_t esp_flash_deinit_os_functions(esp_flash_t *chip)
Definition esp_flash_port.c:171
esp_err_t esp_flash_erase_chip(esp_flash_t *chip)
Definition esp_flash_port.c:274
esp_err_t esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
Definition esp_flash_port.c:232
esp_err_t esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length)
Definition esp_flash_port.c:291
esp_flash_t * esp_flash_default_chip
Definition esp_flash_port.c:66
esp_flash_io_mode_t
Definition esp_flash.h:71
@ SPI_FLASH_SLOWRD
Definition esp_flash.h:72
@ SPI_FLASH_OPI_STR
Definition esp_flash.h:78
@ SPI_FLASH_OPI_DTR
Definition esp_flash.h:79
@ SPI_FLASH_FASTRD
Definition esp_flash.h:73
@ SPI_FLASH_DOUT
Definition esp_flash.h:74
@ SPI_FLASH_DIO
Definition esp_flash.h:75
@ SPI_FLASH_READ_MODE_MAX
Definition esp_flash.h:80
@ SPI_FLASH_QOUT
Definition esp_flash.h:76
@ SPI_FLASH_QIO
Definition esp_flash.h:77
unsigned uint32_t
Definition stdint.h:9
Definition esp_flash.h:90
void * os_func
Definition esp_flash.h:93
uint32_t size
Definition esp_flash.h:97
void * chip_drv
Definition esp_flash.h:92
void * mal
Definition esp_flash.h:104
uint32_t chip_id
Definition esp_flash.h:98
void * os_func_data
Definition esp_flash.h:94
esp_flash_io_mode_t read_mode
Definition esp_flash.h:96
uint32_t busy
Definition esp_flash.h:99
void * host
Definition esp_flash.h:91
uint_fast8_t length
Definition vsf_pbuf.c:38
Generated from commit: vsfteam/vsf@015f4d1