mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-02 11:10:54 +03:00
refactor(spi_flash): move internal types to private headers and refactor initialization
This commit refactors the SPI flash component to improve encapsulation and
modularity by moving internal types and functions to private headers, and
reorganizing initialization code.
Key changes:
1. Move PSRAM frequency constraint macro from soc_caps.h to mspi_ll.h
- Rename SOC_SPI_MEM_PSRAM_FREQ_AXI_CONSTRAINED to
MSPI_TIMING_LL_PSRAM_FREQ_AXI_CONSTRAINED
- Move macro definition to chip-specific mspi_ll.h files (C5, C61, H4, P4, S31)
- Update usage in clk_utils.c and esp_flash_spi_init.c
- Remove old macro from all soc_caps.h files
2. Move internal types to private headers
- Move esp_flash_t structure to esp_private/esp_flash_types.h
- Move esp_flash_os_functions_t to esp_private/spi_flash_os.h
- Update all internal files to include private headers
- Keep forward declarations in public esp_flash.h
3. Move chip driver header to internal directory
- Move spi_flash_chip_driver.h to esp_flash_port/spi_flash_chip_driver.h
- Update all references to use new path
- Add esp_private/esp_flash_types.h include to the moved header
4. Refactor initialization functions
- Move init_flash from esp_system/startup_funcs.c to
spi_flash/esp_flash_spi_init.c
- Create new init_pm_flash_freq_limit function in startup_funcs.c
to call esp_pm_flash_freq_limit_init() conditionally
- Update system_init_fn.txt with new function locations
5. Improve API encapsulation
- Replace direct access to esp_flash_t->size in
esp_partition_register_external() with esp_flash_get_size() API
- Move esp_flash_is_quad_mode from inline function to regular function
in esp_flash_api.c
6. Update component dependencies
- Add esp_driver_gpio to spi_flash component PRIV_REQUIRES
- Remove unused includes and clean up header dependencies
These changes improve code organization by clearly separating public APIs
from internal implementation details, making the codebase more maintainable
and reducing the risk of breaking changes to internal structures.
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include <stdbool.h>
|
||||
#include "hal/spi_flash_types.h"
|
||||
#include "esp_blockdev.h"
|
||||
#include "esp_bit_defs.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -28,91 +29,6 @@ typedef struct {
|
||||
uint32_t size; ///< Size of the region
|
||||
} esp_flash_region_t;
|
||||
|
||||
/** @brief OS-level integration hooks for accessing flash chips inside a running OS
|
||||
*
|
||||
* It's in the public header because some instances should be allocated statically in the startup
|
||||
* code. May be updated according to hardware version and new flash chip feature requirements,
|
||||
* shouldn't be treated as public API.
|
||||
*
|
||||
* For advanced developers, you may replace some of them with your implementations at your own
|
||||
* risk.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* Flags for start function
|
||||
*/
|
||||
/** Limit CPU frequency during flash operations (ESP32-C5 only, 240MHz).
|
||||
*/
|
||||
#define ESP_FLASH_START_FLAG_LIMIT_CPU_FREQ BIT(0)
|
||||
/**
|
||||
* Called before commencing any flash operation. Does not need to be
|
||||
* recursive (ie is called at most once for each call to 'end').
|
||||
*/
|
||||
esp_err_t (*start)(void *arg, uint32_t flags);
|
||||
|
||||
/** Called after completing any flash operation. */
|
||||
esp_err_t (*end)(void *arg);
|
||||
|
||||
/** Called before any erase/write operations to check whether the region is limited by the OS */
|
||||
esp_err_t (*region_protected)(void* arg, size_t start_addr, size_t size);
|
||||
|
||||
/** Delay for at least 'us' microseconds. Called in between 'start' and 'end'. */
|
||||
esp_err_t (*delay_us)(void *arg, uint32_t us);
|
||||
|
||||
/** Called for get temp buffer when buffer from application cannot be directly read into/write from. */
|
||||
void *(*get_temp_buffer)(void* arg, size_t reqest_size, size_t* out_size);
|
||||
|
||||
/** Called for release temp buffer. */
|
||||
void (*release_temp_buffer)(void* arg, void *temp_buf);
|
||||
|
||||
#define SPI_FLASH_YIELD_REQ_YIELD BIT(0)
|
||||
#define SPI_FLASH_YIELD_REQ_SUSPEND BIT(1)
|
||||
|
||||
/** Yield to other tasks. Called during erase operations.
|
||||
* @return ESP_OK means yield needs to be called (got an event to handle), while ESP_ERR_TIMEOUT means skip yield.*/
|
||||
esp_err_t (*check_yield)(void *arg, uint32_t chip_status, uint32_t* out_request);
|
||||
|
||||
#define SPI_FLASH_YIELD_STA_RESUME BIT(2)
|
||||
|
||||
/** Yield to other tasks. Called during erase operations. */
|
||||
esp_err_t (*yield)(void *arg, uint32_t* out_status);
|
||||
|
||||
/** Called for get system time. */
|
||||
int64_t (*get_system_time)(void *arg);
|
||||
|
||||
#define SPI_FLASH_OS_IS_ERASING_STATUS_FLAG BIT(0)
|
||||
|
||||
/** Call to set flash operation status */
|
||||
void (*set_flash_op_status)(uint32_t op_status);
|
||||
|
||||
} esp_flash_os_functions_t;
|
||||
|
||||
/** @brief Structure to describe a SPI flash chip connected to the system.
|
||||
|
||||
Structure must be initialized before use (passed to esp_flash_init()). It's in the public
|
||||
header because some instances should be allocated statically in the startup code. May be
|
||||
updated according to hardware version and new flash chip feature requirements, shouldn't be
|
||||
treated as public API.
|
||||
|
||||
For advanced developers, you may replace some of them with your implementations at your own
|
||||
risk.
|
||||
*/
|
||||
struct esp_flash_t {
|
||||
spi_flash_host_inst_t* host; ///< Pointer to hardware-specific "host_driver" structure. Must be initialized before used.
|
||||
const spi_flash_chip_t *chip_drv; ///< Pointer to chip-model-specific "adapter" structure. If NULL, will be detected during initialisation.
|
||||
|
||||
const esp_flash_os_functions_t *os_func; ///< Pointer to os-specific hook structure. Call ``esp_flash_init_os_functions()`` to setup this field, after the host is properly initialized.
|
||||
void *os_func_data; ///< Pointer to argument for os-specific hooks. Left NULL and will be initialized with ``os_func``.
|
||||
|
||||
esp_flash_io_mode_t read_mode; ///< Configured SPI flash read mode. Set before ``esp_flash_init`` is called.
|
||||
uint32_t size; ///< Size of SPI flash in bytes. If 0, size will be detected during initialisation. Note: this stands for the size in the binary image header. If you want to get the flash physical size, please call `esp_flash_get_physical_size`.
|
||||
uint32_t chip_id; ///< Detected chip id.
|
||||
uint32_t busy :1; ///< This flag is used to verify chip's status.
|
||||
uint32_t hpm_dummy_ena :1; ///< This flag is used to verify whether flash works under HPM status.
|
||||
uint32_t reserved_flags :30; ///< reserved.
|
||||
int clock_source; ///< Clock source for GPSPI.
|
||||
};
|
||||
|
||||
|
||||
/** @brief Initialise SPI flash chip interface.
|
||||
*
|
||||
@@ -384,10 +300,7 @@ extern esp_flash_t *esp_flash_default_chip;
|
||||
*
|
||||
* @return true if flash works in quad mode, otherwise false
|
||||
*/
|
||||
static inline bool esp_flash_is_quad_mode(const esp_flash_t *chip)
|
||||
{
|
||||
return (chip->read_mode == SPI_FLASH_QIO) || (chip->read_mode == SPI_FLASH_QOUT);
|
||||
}
|
||||
bool esp_flash_is_quad_mode(const esp_flash_t *chip);
|
||||
|
||||
/*******************************************************************************
|
||||
* BDL Functions
|
||||
|
||||
Reference in New Issue
Block a user