mirror of
https://github.com/0xFEEDC0DE64/arduino-esp32.git
synced 2025-07-04 22:36:32 +02:00
IDF release/v4.0 08219f3cf
This commit is contained in:
308
tools/sdk/include/spi_flash/esp_flash.h
Normal file
308
tools/sdk/include/spi_flash/esp_flash.h
Normal file
@ -0,0 +1,308 @@
|
||||
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
#include "esp_err.h"
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "hal/spi_flash_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct spi_flash_chip_t;
|
||||
typedef struct spi_flash_chip_t spi_flash_chip_t;
|
||||
|
||||
typedef struct esp_flash_t esp_flash_t;
|
||||
|
||||
/** @brief Structure for describing a region of flash */
|
||||
typedef struct {
|
||||
uint32_t offset; ///< Start address of this region
|
||||
uint32_t size; ///< Size of the region
|
||||
} esp_flash_region_t;
|
||||
|
||||
/** OS-level integration hooks for accessing flash chips inside a running OS */
|
||||
typedef struct {
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/** 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 'ms' milliseconds. Called in between 'start' and 'end'. */
|
||||
esp_err_t (*delay_ms)(void *arg, unsigned ms);
|
||||
} 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()).
|
||||
*/
|
||||
struct esp_flash_t {
|
||||
spi_flash_host_driver_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.
|
||||
uint32_t chip_id; ///< Detected chip id.
|
||||
};
|
||||
|
||||
|
||||
/** @brief Initialise SPI flash chip interface.
|
||||
*
|
||||
* This function must be called before any other API functions are called for this chip.
|
||||
*
|
||||
* @note Only the ``host`` and ``read_mode`` fields of the chip structure must
|
||||
* be initialised before this function is called. Other fields may be
|
||||
* auto-detected if left set to zero or NULL.
|
||||
*
|
||||
* @note If the chip->drv pointer is NULL, chip chip_drv will be auto-detected
|
||||
* based on its manufacturer & product IDs. See
|
||||
* ``esp_flash_registered_flash_drivers`` pointer for details of this process.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @return ESP_OK on success, or a flash error code if initialisation fails.
|
||||
*/
|
||||
esp_err_t esp_flash_init(esp_flash_t *chip);
|
||||
|
||||
/**
|
||||
* Check if appropriate chip driver is set.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
*
|
||||
* @return true if set, otherwise false.
|
||||
*/
|
||||
bool esp_flash_chip_driver_initialized(const esp_flash_t *chip);
|
||||
|
||||
/** @brief Read flash ID via the common "RDID" SPI flash command.
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
|
||||
* @param[out] out_id Pointer to receive ID value.
|
||||
*
|
||||
* ID is a 24-bit value. Lower 16 bits of 'id' are the chip ID, upper 8 bits are the manufacturer ID.
|
||||
*
|
||||
* @return ESP_OK on success, or a flash error code if operation failed.
|
||||
*/
|
||||
esp_err_t esp_flash_read_id(esp_flash_t *chip, uint32_t *out_id);
|
||||
|
||||
/** @brief Detect flash size based on flash ID.
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
|
||||
* @param[out] out_size Detected size in bytes.
|
||||
*
|
||||
* @note Most flash chips use a common format for flash ID, where the lower 4 bits specify the size as a power of 2. If
|
||||
* the manufacturer doesn't follow this convention, the size may be incorrectly detected.
|
||||
*
|
||||
* @return ESP_OK on success, or a flash error code if operation failed.
|
||||
*/
|
||||
esp_err_t esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size);
|
||||
|
||||
/** @brief Erase flash chip contents
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
|
||||
*
|
||||
*
|
||||
* @return ESP_OK on success, or a flash error code if operation failed.
|
||||
*/
|
||||
esp_err_t esp_flash_erase_chip(esp_flash_t *chip);
|
||||
|
||||
/** @brief Erase a region of the flash chip
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
|
||||
* @param start Address to start erasing flash. Must be sector aligned.
|
||||
* @param len Length of region to erase. Must also be sector aligned.
|
||||
*
|
||||
* Sector size is specifyed in chip->drv->sector_size field (typically 4096 bytes.) ESP_ERR_INVALID_ARG will be
|
||||
* returned if the start & length are not a multiple of this size.
|
||||
*
|
||||
* Erase is performed using block (multi-sector) erases where possible (block size is specified in
|
||||
* chip->drv->block_erase_size field, typically 65536 bytes). Remaining sectors are erased using individual sector erase
|
||||
* commands.
|
||||
*
|
||||
* @return ESP_OK on success, or a flash error code if operation failed.
|
||||
*/
|
||||
esp_err_t esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len);
|
||||
|
||||
/** @brief Read if the entire chip is write protected
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
|
||||
* @param[out] write_protected Pointer to boolean, set to the value of the write protect flag.
|
||||
*
|
||||
* @note A correct result for this flag depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
|
||||
* field).
|
||||
*
|
||||
* @return ESP_OK on success, or a flash error code if operation failed.
|
||||
*/
|
||||
esp_err_t esp_flash_get_chip_write_protect(esp_flash_t *chip, bool *write_protected);
|
||||
|
||||
/** @brief Set write protection for the SPI flash chip
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
|
||||
* @param write_protect Boolean value for the write protect flag
|
||||
*
|
||||
* @note Correct behaviour of this function depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
|
||||
* field).
|
||||
*
|
||||
* Some SPI flash chips may require a power cycle before write protect status can be cleared. Otherwise,
|
||||
* write protection can be removed via a follow-up call to this function.
|
||||
*
|
||||
* @return ESP_OK on success, or a flash error code if operation failed.
|
||||
*/
|
||||
esp_err_t esp_flash_set_chip_write_protect(esp_flash_t *chip, bool write_protect);
|
||||
|
||||
|
||||
/** @brief Read the list of individually protectable regions of this SPI flash chip.
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
|
||||
* @param[out] out_regions Pointer to receive a pointer to the array of protectable regions of the chip.
|
||||
* @param[out] out_num_regions Pointer to an integer receiving the count of protectable regions in the array returned in 'regions'.
|
||||
*
|
||||
* @note Correct behaviour of this function depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
|
||||
* field).
|
||||
*
|
||||
* @return ESP_OK on success, or a flash error code if operation failed.
|
||||
*/
|
||||
esp_err_t esp_flash_get_protectable_regions(const esp_flash_t *chip, const esp_flash_region_t **out_regions, uint32_t *out_num_regions);
|
||||
|
||||
|
||||
/** @brief Detect if a region of the SPI flash chip is protected
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
|
||||
* @param region Pointer to a struct describing a protected region. This must match one of the regions returned from esp_flash_get_protectable_regions(...).
|
||||
* @param[out] out_protected Pointer to a flag which is set based on the protected status for this region.
|
||||
*
|
||||
* @note It is possible for this result to be false and write operations to still fail, if protection is enabled for the entire chip.
|
||||
*
|
||||
* @note Correct behaviour of this function depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
|
||||
* field).
|
||||
*
|
||||
* @return ESP_OK on success, or a flash error code if operation failed.
|
||||
*/
|
||||
esp_err_t esp_flash_get_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool *out_protected);
|
||||
|
||||
/** @brief Update the protected status for a region of the SPI flash chip
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
|
||||
* @param region Pointer to a struct describing a protected region. This must match one of the regions returned from esp_flash_get_protectable_regions(...).
|
||||
* @param protect Write protection flag to set.
|
||||
*
|
||||
* @note It is possible for the region protection flag to be cleared and write operations to still fail, if protection is enabled for the entire chip.
|
||||
*
|
||||
* @note Correct behaviour of this function depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
|
||||
* field).
|
||||
*
|
||||
* @return ESP_OK on success, or a flash error code if operation failed.
|
||||
*/
|
||||
esp_err_t esp_flash_set_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool protect);
|
||||
|
||||
/** @brief Read data from the SPI flash chip
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
|
||||
* @param buffer Pointer to a buffer where the data will be read. To get better performance, this should be in the DRAM and word aligned.
|
||||
* @param address Address on flash to read from. Must be less than chip->size field.
|
||||
* @param length Length (in bytes) of data to read.
|
||||
*
|
||||
* There are no alignment constraints on buffer, address or length.
|
||||
*
|
||||
* @note If on-chip flash encryption is used, this function returns raw (ie encrypted) data. Use the flash cache
|
||||
* to transparently decrypt data.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: success
|
||||
* - ESP_ERR_NO_MEM: the buffer is not valid, however failed to malloc on
|
||||
* the heap.
|
||||
* - or a flash error code if operation failed.
|
||||
*/
|
||||
esp_err_t esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length);
|
||||
|
||||
/** @brief Write data to the SPI flash chip
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
|
||||
* @param address Address on flash to write to. Must be previously erased (SPI NOR flash can only write bits 1->0).
|
||||
* @param buffer Pointer to a buffer with the data to write. To get better performance, this should be in the DRAM and word aligned.
|
||||
* @param length Length (in bytes) of data to write.
|
||||
*
|
||||
* There are no alignment constraints on buffer, address or length.
|
||||
*
|
||||
* @return ESP_OK on success, or a flash error code if operation failed.
|
||||
*/
|
||||
esp_err_t esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
|
||||
|
||||
/** @brief Encrypted and write data to the SPI flash chip using on-chip hardware flash encryption
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must be NULL (the main flash chip). For other chips, encrypted write is not supported.
|
||||
* @param address Address on flash to write to. 16 byte aligned. Must be previously erased (SPI NOR flash can only write bits 1->0).
|
||||
* @param buffer Pointer to a buffer with the data to write.
|
||||
* @param length Length (in bytes) of data to write. 16 byte aligned.
|
||||
*
|
||||
* @note Both address & length must be 16 byte aligned, as this is the encryption block size
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: on success
|
||||
* - ESP_ERR_NOT_SUPPORTED: encrypted write not supported for this chip.
|
||||
* - ESP_ERR_INVALID_ARG: Either the address, buffer or length is invalid.
|
||||
* - or other flash error code from spi_flash_write_encrypted().
|
||||
*/
|
||||
esp_err_t esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length);
|
||||
|
||||
/** @brief Read and decrypt data from the SPI flash chip using on-chip hardware flash encryption
|
||||
*
|
||||
* @param chip Pointer to identify flash chip. Must be NULL (the main flash chip). For other chips, encrypted read is not supported.
|
||||
* @param address Address on flash to read from.
|
||||
* @param out_buffer Pointer to a buffer for the data to read to.
|
||||
* @param length Length (in bytes) of data to read.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: on success
|
||||
* - ESP_ERR_NOT_SUPPORTED: encrypted read not supported for this chip.
|
||||
* - or other flash error code from spi_flash_read_encrypted().
|
||||
*/
|
||||
esp_err_t esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length);
|
||||
|
||||
/** @brief Pointer to the "default" SPI flash chip, ie the main chip attached to the MCU.
|
||||
|
||||
This chip is used if the 'chip' argument pass to esp_flash_xxx API functions is ever NULL.
|
||||
*/
|
||||
extern esp_flash_t *esp_flash_default_chip;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* Utility Functions
|
||||
******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief Returns true if chip is configured for Quad I/O or Quad Fast Read.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
100
tools/sdk/include/spi_flash/esp_flash_internal.h
Normal file
100
tools/sdk/include/spi_flash/esp_flash_internal.h
Normal file
@ -0,0 +1,100 @@
|
||||
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
#include "esp_err.h"
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include "esp_flash.h"
|
||||
|
||||
/** Internal API, don't use in the applications */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/** @brief Initialise the default SPI flash chip
|
||||
*
|
||||
* Called by OS startup code. You do not need to call this in your own applications.
|
||||
*/
|
||||
#ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
|
||||
#define esp_flash_init_default_chip(...) ({ESP_OK;})
|
||||
#else
|
||||
esp_err_t esp_flash_init_default_chip(void);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Enable OS-level SPI flash protections in IDF
|
||||
*
|
||||
* Called by OS startup code. You do not need to call this in your own applications.
|
||||
*
|
||||
* @return ESP_OK if success, otherwise failed. See return value of ``esp_flash_init_os_functions``.
|
||||
*/
|
||||
#ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
|
||||
#define esp_flash_app_init(...) ({ESP_OK;})
|
||||
#else
|
||||
esp_err_t esp_flash_app_init(void);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Disable OS-level SPI flash protections in IDF
|
||||
*
|
||||
* Called by the IDF internal code (e.g. coredump). You do not need to call this in your own applications.
|
||||
*
|
||||
* @return always ESP_OK.
|
||||
*/
|
||||
#ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
|
||||
#define esp_flash_app_disable_protect(...) ({ESP_OK;})
|
||||
#else
|
||||
esp_err_t esp_flash_app_disable_protect(bool disable);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Initialize OS-level functions for a specific chip.
|
||||
*
|
||||
* @param chip The chip to init os functions.
|
||||
* @param host_id Which SPI host to use, 1 for SPI1, 2 for SPI2 (HSPI), 3 for SPI3 (VSPI)
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - ESP_ERR_INVALID_ARG if host_id is invalid
|
||||
*/
|
||||
esp_err_t esp_flash_init_os_functions(esp_flash_t *chip, int host_id);
|
||||
|
||||
/**
|
||||
* Initialize OS-level functions for the main flash chip.
|
||||
*
|
||||
* @param chip The chip to init os functions. Only pointer to the default chip is supported now.
|
||||
*
|
||||
* @return always ESP_OK
|
||||
*/
|
||||
esp_err_t esp_flash_app_init_os_functions(esp_flash_t* chip);
|
||||
|
||||
/**
|
||||
* Disable OS-level functions for the main flash chip during special phases (e.g. coredump)
|
||||
*
|
||||
* @param chip The chip to init os functions. Only "esp_flash_default_chip" is supported now.
|
||||
*
|
||||
* @return always ESP_OK
|
||||
*/
|
||||
esp_err_t esp_flash_app_disable_os_functions(esp_flash_t* chip);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
55
tools/sdk/include/spi_flash/esp_flash_spi_init.h
Normal file
55
tools/sdk/include/spi_flash/esp_flash_spi_init.h
Normal file
@ -0,0 +1,55 @@
|
||||
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hal/spi_types.h"
|
||||
#include "esp_flash.h"
|
||||
|
||||
/// Configurations for the SPI Flash to init
|
||||
typedef struct {
|
||||
spi_host_device_t host_id; ///< Bus to use
|
||||
int cs_id; ///< CS pin (signal) to use
|
||||
int cs_io_num; ///< GPIO pin to output the CS signal
|
||||
esp_flash_io_mode_t io_mode; ///< IO mode to read from the Flash
|
||||
esp_flash_speed_t speed; ///< Speed of the Flash clock
|
||||
int input_delay_ns; ///< Input delay of the data pins, in ns. Set to 0 if unknown.
|
||||
} esp_flash_spi_device_config_t;
|
||||
|
||||
/**
|
||||
* Add a SPI Flash device onto the SPI bus.
|
||||
*
|
||||
* The bus should be already initialized by ``spi_bus_initialization``.
|
||||
*
|
||||
* @param out_chip Pointer to hold the initialized chip.
|
||||
* @param config Configuration of the chips to initialize.
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_ARG: out_chip is NULL, or some field in the config is invalid.
|
||||
* - ESP_ERR_NO_MEM: failed to allocate memory for the chip structures.
|
||||
* - ESP_OK: success.
|
||||
*/
|
||||
esp_err_t spi_bus_add_flash_device(esp_flash_t **out_chip, const esp_flash_spi_device_config_t *config);
|
||||
|
||||
/**
|
||||
* Remove a SPI Flash device from the SPI bus.
|
||||
*
|
||||
* @param chip The flash device to remove.
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_ARG: The chip is invalid.
|
||||
* - ESP_OK: success.
|
||||
*/
|
||||
esp_err_t spi_bus_remove_flash_device(esp_flash_t *chip);
|
||||
|
@ -19,8 +19,10 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "esp_err.h"
|
||||
#include "esp_flash.h"
|
||||
#include "esp_spi_flash.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@ -98,6 +100,7 @@ typedef struct esp_partition_iterator_opaque_* esp_partition_iterator_t;
|
||||
* However, this is the format used by this API.
|
||||
*/
|
||||
typedef struct {
|
||||
esp_flash_t* flash_chip; /*!< SPI flash chip on which the partition resides */
|
||||
esp_partition_type_t type; /*!< partition type (app/data) */
|
||||
esp_partition_subtype_t subtype; /*!< partition subtype */
|
||||
uint32_t address; /*!< starting address of the partition in flash */
|
||||
@ -246,8 +249,8 @@ esp_err_t esp_partition_write(const esp_partition_t* partition,
|
||||
* @param partition Pointer to partition structure obtained using
|
||||
* esp_partition_find_first or esp_partition_get.
|
||||
* Must be non-NULL.
|
||||
* @param start_addr Address where erase operation should start. Must be aligned
|
||||
* to 4 kilobytes.
|
||||
* @param offset Offset from the beginning of partition where erase operation
|
||||
* should start. Must be aligned to 4 kilobytes.
|
||||
* @param size Size of the range which should be erased, in bytes.
|
||||
* Must be divisible by 4 kilobytes.
|
||||
*
|
||||
@ -257,7 +260,7 @@ esp_err_t esp_partition_write(const esp_partition_t* partition,
|
||||
* or one of error codes from lower-level flash driver.
|
||||
*/
|
||||
esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
|
||||
uint32_t start_addr, uint32_t size);
|
||||
size_t offset, size_t size);
|
||||
|
||||
/**
|
||||
* @brief Configure MMU to map partition into data memory
|
||||
@ -284,7 +287,7 @@ esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
|
||||
*
|
||||
* @return ESP_OK, if successful
|
||||
*/
|
||||
esp_err_t esp_partition_mmap(const esp_partition_t* partition, uint32_t offset, uint32_t size,
|
||||
esp_err_t esp_partition_mmap(const esp_partition_t* partition, size_t offset, size_t size,
|
||||
spi_flash_mmap_memory_t memory,
|
||||
const void** out_ptr, spi_flash_mmap_handle_t* out_handle);
|
||||
|
||||
@ -320,6 +323,43 @@ esp_err_t esp_partition_get_sha256(const esp_partition_t *partition, uint8_t *sh
|
||||
*/
|
||||
bool esp_partition_check_identity(const esp_partition_t *partition_1, const esp_partition_t *partition_2);
|
||||
|
||||
/**
|
||||
* @brief Register a partition on an external flash chip
|
||||
*
|
||||
* This API allows designating certain areas of external flash chips (identified by the esp_flash_t structure)
|
||||
* as partitions. This allows using them with components which access SPI flash through the esp_partition API.
|
||||
*
|
||||
* @param flash_chip Pointer to the structure identifying the flash chip
|
||||
* @param offset Address in bytes, where the partition starts
|
||||
* @param size Size of the partition in bytes
|
||||
* @param label Partition name
|
||||
* @param type One of the partition types (ESP_PARTITION_TYPE_*). Note that applications can not be booted from external flash
|
||||
* chips, so using ESP_PARTITION_TYPE_APP is not supported.
|
||||
* @param subtype One of the partition subtypes (ESP_PARTITION_SUBTYPE_*)
|
||||
* @param[out] out_partition Output, if non-NULL, receives the pointer to the resulting esp_partition_t structure
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_NOT_SUPPORTED if CONFIG_CONFIG_SPI_FLASH_USE_LEGACY_IMPL is enabled
|
||||
* - ESP_ERR_NO_MEM if memory allocation has failed
|
||||
* - ESP_ERR_INVALID_ARG if the new partition overlaps another partition on the same flash chip
|
||||
* - ESP_ERR_INVALID_SIZE if the partition doesn't fit into the flash chip size
|
||||
*/
|
||||
esp_err_t esp_partition_register_external(esp_flash_t* flash_chip, size_t offset, size_t size,
|
||||
const char* label, esp_partition_type_t type, esp_partition_subtype_t subtype,
|
||||
const esp_partition_t** out_partition);
|
||||
|
||||
/**
|
||||
* @brief Deregister the partition previously registered using esp_partition_register_external
|
||||
* @param partition pointer to the partition structure obtained from esp_partition_register_external,
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_NOT_FOUND if the partition pointer is not found
|
||||
* - ESP_ERR_INVALID_ARG if the partition comes from the partition table
|
||||
* - ESP_ERR_INVALID_ARG if the partition was not registered using
|
||||
* esp_partition_register_external function.
|
||||
*/
|
||||
esp_err_t esp_partition_deregister_external(const esp_partition_t* partition);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -25,7 +25,6 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ESP_ERR_FLASH_BASE 0x10010
|
||||
#define ESP_ERR_FLASH_OP_FAIL (ESP_ERR_FLASH_BASE + 1)
|
||||
#define ESP_ERR_FLASH_OP_TIMEOUT (ESP_ERR_FLASH_BASE + 2)
|
||||
|
||||
@ -36,7 +35,7 @@ extern "C" {
|
||||
/**
|
||||
* @brief Initialize SPI flash access driver
|
||||
*
|
||||
* This function must be called exactly once, before any other
|
||||
* This function must be called exactly once, before any other
|
||||
* spi_flash_* functions are called.
|
||||
* Currently this function is called from startup code. There is
|
||||
* no need to call it from application code.
|
||||
@ -336,7 +335,7 @@ typedef bool (*spi_flash_is_safe_write_address_t)(size_t addr, size_t size);
|
||||
* - 'op_unlock' unlocks access to flash API internal data.
|
||||
* These two functions are recursive and can be used around the outside of multiple calls to
|
||||
* 'start' & 'end', in order to create atomic multi-part flash operations.
|
||||
* 3) When CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_ALLOWED is disabled, flash writing/erasing
|
||||
* 3) When CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED is disabled, flash writing/erasing
|
||||
* API checks for addresses provided by user to avoid corruption of critical flash regions
|
||||
* (bootloader, partition table, running application etc.).
|
||||
*
|
||||
@ -354,7 +353,7 @@ typedef struct {
|
||||
spi_flash_guard_end_func_t end; /**< critical section end function. */
|
||||
spi_flash_op_lock_func_t op_lock; /**< flash access API lock function.*/
|
||||
spi_flash_op_unlock_func_t op_unlock; /**< flash access API unlock function.*/
|
||||
#if !CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_ALLOWED
|
||||
#if !CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
|
||||
spi_flash_is_safe_write_address_t is_safe_write_address; /**< checks flash write addresses.*/
|
||||
#endif
|
||||
} spi_flash_guard_funcs_t;
|
||||
|
101
tools/sdk/include/spi_flash/memspi_host_driver.h
Normal file
101
tools/sdk/include/spi_flash/memspi_host_driver.h
Normal file
@ -0,0 +1,101 @@
|
||||
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
#include "hal/spi_flash_hal.h"
|
||||
|
||||
/** Default configuration for the memspi (high speed version) */
|
||||
#define ESP_FLASH_DEFAULT_HOST_DRIVER() (spi_flash_host_driver_t) { \
|
||||
.dev_config = spi_flash_hal_device_config, \
|
||||
.common_command = spi_flash_hal_common_command, \
|
||||
.read_id = memspi_host_read_id_hs, \
|
||||
.erase_chip = spi_flash_hal_erase_chip, \
|
||||
.erase_sector = spi_flash_hal_erase_sector, \
|
||||
.erase_block = spi_flash_hal_erase_block, \
|
||||
.read_status = memspi_host_read_status_hs, \
|
||||
.set_write_protect = spi_flash_hal_set_write_protect, \
|
||||
.supports_direct_write = spi_flash_hal_supports_direct_write, \
|
||||
.supports_direct_read = spi_flash_hal_supports_direct_read, \
|
||||
.program_page = spi_flash_hal_program_page, \
|
||||
.max_write_bytes = SPI_FLASH_HAL_MAX_WRITE_BYTES, \
|
||||
.read = spi_flash_hal_read, \
|
||||
.max_read_bytes = SPI_FLASH_HAL_MAX_READ_BYTES, \
|
||||
.host_idle = spi_flash_hal_host_idle, \
|
||||
.configure_host_io_mode = spi_flash_hal_configure_host_io_mode, \
|
||||
.poll_cmd_done = spi_flash_hal_poll_cmd_done, \
|
||||
.flush_cache = memspi_host_flush_cache, \
|
||||
}
|
||||
|
||||
/// configuration for the memspi host
|
||||
typedef spi_flash_memspi_config_t memspi_host_config_t;
|
||||
/// context for the memspi host
|
||||
typedef spi_flash_memspi_data_t memspi_host_data_t;
|
||||
|
||||
/**
|
||||
* Initialize the memory SPI host.
|
||||
*
|
||||
* @param host Pointer to the host structure.
|
||||
* @param data Pointer to allocated space to hold the context of host driver.
|
||||
* @param cfg Pointer to configuration structure
|
||||
*
|
||||
* @return always return ESP_OK
|
||||
*/
|
||||
esp_err_t memspi_host_init_pointers(spi_flash_host_driver_t *host, memspi_host_data_t *data, const memspi_host_config_t *cfg);
|
||||
|
||||
/*******************************************************************************
|
||||
* NOTICE
|
||||
* Rest part of this file are part of the HAL layer
|
||||
* The HAL is not public api, don't use in application code.
|
||||
* See readme.md in soc/include/hal/readme.md
|
||||
******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief Read the Status Register read from RDSR (05h).
|
||||
*
|
||||
* High speed implementation of RDID through memspi interface relying on the
|
||||
* ``common_command``.
|
||||
*
|
||||
* @param driver The driver context.
|
||||
* @param id Output of the read ID from the slave.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: if success
|
||||
* - ESP_ERR_FLASH_NO_RESPONSE: if no response from chip
|
||||
* - or other cases from ``spi_hal_common_command``
|
||||
*/
|
||||
esp_err_t memspi_host_read_id_hs(spi_flash_host_driver_t *driver, uint32_t *id);
|
||||
|
||||
/**
|
||||
* High speed implementation of RDSR through memspi interface relying on the
|
||||
* ``common_command``.
|
||||
*
|
||||
* @param driver The driver context.
|
||||
* @param id Output of the read ID from the slave.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: if success
|
||||
* - or other cases from ``spi_hal_common_command``
|
||||
*/
|
||||
esp_err_t memspi_host_read_status_hs(spi_flash_host_driver_t *driver, uint8_t *out_sr);
|
||||
|
||||
/**
|
||||
* Flush the cache (if needed) after the contents are modified.
|
||||
*
|
||||
* @param driver The driver context.
|
||||
* @param addr Start address of the modified region
|
||||
* @param size Size of the region modified.
|
||||
*
|
||||
* @return always ESP_OK.
|
||||
*/
|
||||
esp_err_t memspi_host_flush_cache(spi_flash_host_driver_t* driver, uint32_t addr, uint32_t size);
|
168
tools/sdk/include/spi_flash/spi_flash_chip_driver.h
Normal file
168
tools/sdk/include/spi_flash/spi_flash_chip_driver.h
Normal file
@ -0,0 +1,168 @@
|
||||
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
#include "esp_flash.h"
|
||||
|
||||
struct esp_flash_t;
|
||||
typedef struct esp_flash_t esp_flash_t;
|
||||
|
||||
typedef struct spi_flash_chip_t spi_flash_chip_t;
|
||||
/** @brief SPI flash chip driver definition structure.
|
||||
*
|
||||
* The chip driver structure contains chip-specific pointers to functions to perform SPI flash operations, and some
|
||||
* chip-specific numeric values.
|
||||
*
|
||||
* @note This is not a public API. These functions are called from the public API (declared in
|
||||
* esp_flash.h). They assume the caller has already validated arguments and enabled relevant protections
|
||||
* (disabling flash cache, prevent concurrent SPI access, etc.)
|
||||
*
|
||||
* Do not call chip driver functions directly in other contexts.
|
||||
*
|
||||
* A generic driver for generic chips and its related operations are defined in
|
||||
* spi_flash_chip_generic.h which can be used as building blocks for written
|
||||
* new/specific SPI flash chip drivers.
|
||||
*
|
||||
* @note All of these functions may be called with SPI flash cache disabled, so must only ever access IRAM/DRAM/ROM.
|
||||
*/
|
||||
struct spi_flash_chip_t {
|
||||
const char *name; ///< Name of the chip driver
|
||||
/* Probe to detect if a supported SPI flash chip is found.
|
||||
*
|
||||
* Attempts to configure 'chip' with these operations and probes for a matching SPI flash chip.
|
||||
*
|
||||
* Auto-detection of a SPI flash chip calls this function in turn on each registered driver (see esp_flash_registered_flash_drivers).
|
||||
*
|
||||
* ID - as read by spi_flash_generic_read_id() - is supplied so each probe
|
||||
* function doesn't need to unnecessarily read ID, but probe is permitted
|
||||
* to interrogate flash in any non-destructive way.
|
||||
*
|
||||
* It is permissible for the driver to modify the 'chip' structure if probing succeeds (specifically, to assign something to the
|
||||
* driver_data pointer if that is useful for the driver.)
|
||||
*
|
||||
* @return ESP_OK if probing was successful, an error otherwise. Driver may
|
||||
* assume that returning ESP_OK means it has claimed this chip.
|
||||
*/
|
||||
esp_err_t (*probe)(esp_flash_t *chip, uint32_t flash_id);
|
||||
|
||||
esp_err_t (*reset)(esp_flash_t *chip);
|
||||
|
||||
|
||||
/* Detect SPI flash size
|
||||
*
|
||||
* Interrogate the chip to detect its size.
|
||||
*/
|
||||
esp_err_t (*detect_size)(esp_flash_t *chip, uint32_t *size);
|
||||
|
||||
/* Erase the entire chip
|
||||
|
||||
Caller has verified the chip is not write protected.
|
||||
*/
|
||||
esp_err_t (*erase_chip)(esp_flash_t *chip);
|
||||
|
||||
/* Erase a sector of the chip. Sector size is specified in the 'sector_size' field.
|
||||
|
||||
sector_address is an offset in bytes.
|
||||
|
||||
Caller has verified that this sector should be non-write-protected.
|
||||
*/
|
||||
esp_err_t (*erase_sector)(esp_flash_t *chip, uint32_t sector_address);
|
||||
|
||||
/* Erase a multi-sector block of the chip. Block size is specified in the 'block_erase_size' field.
|
||||
sector_address is an offset in bytes.
|
||||
|
||||
Caller has verified that this block should be non-write-protected.
|
||||
*/
|
||||
esp_err_t (*erase_block)(esp_flash_t *chip, uint32_t block_address);
|
||||
|
||||
uint32_t sector_size; /* Sector is minimum erase size */
|
||||
uint32_t block_erase_size; /* Optimal (fastest) block size for multi-sector erases on this chip */
|
||||
|
||||
/* Read the write protect status of the entire chip. */
|
||||
esp_err_t (*get_chip_write_protect)(esp_flash_t *chip, bool *out_write_protected);
|
||||
|
||||
/* Set the write protect status of the entire chip. */
|
||||
esp_err_t (*set_chip_write_protect)(esp_flash_t *chip, bool chip_write_protect);
|
||||
|
||||
/* Number of individually write protectable regions on this chip. Range 0-63. */
|
||||
uint8_t num_protectable_regions;
|
||||
/* Pointer to an array describing each protectable region. Should have num_protectable_regions elements. */
|
||||
const esp_flash_region_t *protectable_regions;
|
||||
/* Get a bitmask describing all protectable regions on the chip. Each bit represents one entry in the
|
||||
protectable_regions array, ie bit (1<<N) is set then the region at array entry N is write protected. */
|
||||
esp_err_t (*get_protected_regions)(esp_flash_t *chip, uint64_t *regions);
|
||||
|
||||
/* Set protectable regions on the chip. Each bit represents on entry in the protectable regions array. */
|
||||
esp_err_t (*set_protected_regions)(esp_flash_t *chip, uint64_t regions);
|
||||
|
||||
/* Read data from the chip.
|
||||
*
|
||||
* Before calling this function, the caller will have called chip->drv->set_read_mode(chip) in order to configure the chip's read mode correctly.
|
||||
*/
|
||||
esp_err_t (*read)(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length);
|
||||
|
||||
/* Write any amount of data to the chip.
|
||||
*/
|
||||
esp_err_t (*write)(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
|
||||
|
||||
|
||||
/* Use the page program command to write data to the chip.
|
||||
*
|
||||
* This function is expected to be called by chip->drv->write (if the
|
||||
* chip->drv->write implementation doesn't call it then it can be left as NULL.)
|
||||
*
|
||||
* - The length argument supplied to this function is at most 'page_size' bytes.
|
||||
*
|
||||
* - The region between 'address' and 'address + length' will not cross a page_size aligned boundary (the write
|
||||
* implementation is expected to split such a write into two before calling page_program.)
|
||||
*/
|
||||
esp_err_t (*program_page)(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
|
||||
|
||||
/* Page size as written by the page_program function. Usually 256 bytes. */
|
||||
uint32_t page_size;
|
||||
|
||||
/* Perform an encrypted write to the chip, using internal flash encryption hardware. */
|
||||
esp_err_t (*write_encrypted)(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
|
||||
|
||||
|
||||
/* Wait for the SPI flash chip to be idle (any write operation to be complete.) This function is both called from the higher-level API functions, and from other functions in this structure.
|
||||
|
||||
timeout_ms should be a timeout (in milliseconds) before the function returns ESP_ERR_TIMEOUT. This is useful to avoid hanging
|
||||
if the chip is otherwise unresponsive (ie returns all 0xFF or similar.)
|
||||
*/
|
||||
esp_err_t (*wait_idle)(esp_flash_t *chip, unsigned timeout_ms);
|
||||
|
||||
/* Configure both the SPI host and the chip for the read mode specified in chip->read_mode.
|
||||
*
|
||||
* This function is called by the higher-level API before the 'read' function is called.
|
||||
*
|
||||
* Can return ESP_ERR_FLASH_UNSUPPORTED_HOST or ESP_ERR_FLASH_UNSUPPORTED_CHIP if the specified mode is unsupported.
|
||||
*/
|
||||
esp_err_t (*set_io_mode)(esp_flash_t *chip);
|
||||
|
||||
/*
|
||||
* Get whether the Quad Enable (QE) is set. (*out_io_mode)=SPI_FLASH_QOUT if
|
||||
* enabled, otherwise disabled
|
||||
*/
|
||||
esp_err_t (*get_io_mode)(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode);
|
||||
};
|
||||
|
||||
/* Pointer to an array of pointers to all known drivers for flash chips. This array is used
|
||||
by esp_flash_init() to detect the flash chip driver, if none is supplied by the caller.
|
||||
|
||||
Array is terminated with a NULL pointer.
|
||||
|
||||
This pointer can be overwritten with a pointer to a new array, to update the list of known flash chips.
|
||||
*/
|
||||
extern const spi_flash_chip_t **esp_flash_registered_chips;
|
32
tools/sdk/include/spi_flash/spi_flash_chip_gd.h
Normal file
32
tools/sdk/include/spi_flash/spi_flash_chip_gd.h
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "esp_flash.h"
|
||||
#include "spi_flash_chip_driver.h"
|
||||
|
||||
|
||||
/**
|
||||
* GD (GigaDevice) SPI flash chip_drv, uses all the above functions for its operations. In
|
||||
* default autodetection, this is used as a catchall if a more specific chip_drv
|
||||
* is not found.
|
||||
*
|
||||
* Note that this is for GD chips with product ID 40H (GD25Q) and 60H (GD25LQ). The chip diver uses
|
||||
* different commands to write the SR2 register according to the chip ID. For GD25Q40 - GD25Q16
|
||||
* chips, and GD25LQ chips, WRSR (01H) command is used; while WRSR2 (31H) is used for GD25Q32 -
|
||||
* GD25Q127 chips.
|
||||
*/
|
||||
extern const spi_flash_chip_t esp_flash_chip_gd;
|
370
tools/sdk/include/spi_flash/spi_flash_chip_generic.h
Normal file
370
tools/sdk/include/spi_flash/spi_flash_chip_generic.h
Normal file
@ -0,0 +1,370 @@
|
||||
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "esp_flash.h"
|
||||
#include "spi_flash_chip_driver.h"
|
||||
|
||||
|
||||
/*
|
||||
* The 'chip_generic' SPI flash operations are a lowest common subset of SPI
|
||||
* flash commands, that work across most chips.
|
||||
*
|
||||
* These can be used as-is via the esp_flash_common_chip_driver chip_drv, or
|
||||
* they can be used as "base chip_drv" functions when creating a new
|
||||
* spi_flash_host_driver_t chip_drv structure.
|
||||
*
|
||||
* All of the functions in this header are internal functions, not part of a
|
||||
* public API. See esp_flash.h for the public API.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Generic probe function
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @param flash_id expected manufacture id.
|
||||
*
|
||||
* @return ESP_OK if the id read from chip->drv_read_id matches (always).
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_probe(esp_flash_t *chip, uint32_t flash_id);
|
||||
|
||||
/**
|
||||
* @brief Generic reset function
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
*
|
||||
* @return ESP_OK if sending success, or error code passed from ``common_command`` or ``wait_idle`` functions of host driver.
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_reset(esp_flash_t *chip);
|
||||
|
||||
/**
|
||||
* @brief Generic size detection function
|
||||
*
|
||||
* Tries to detect the size of chip by using the lower 4 bits of the chip->drv->read_id result = N, and assuming size is 2 ^ N.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @param size Output of the detected size
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - ESP_ERR_FLASH_UNSUPPORTED_CHIP if the manufacturer id is not correct, which may means an error in the reading
|
||||
* - or other error passed from the ``read_id`` function of host driver
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_detect_size(esp_flash_t *chip, uint32_t *size);
|
||||
|
||||
/**
|
||||
* @brief Erase chip by using the generic erase chip command.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - or other error passed from the ``set_write_protect``, ``wait_idle`` or ``erase_chip`` function of host driver
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_erase_chip(esp_flash_t *chip);
|
||||
|
||||
/**
|
||||
* @brief Erase sector by using the generic sector erase command.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @param start_address Start address of the sector to erase
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - or other error passed from the ``set_write_protect``, ``wait_idle`` or ``erase_sector`` function of host driver
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_erase_sector(esp_flash_t *chip, uint32_t start_address);
|
||||
|
||||
/**
|
||||
* @brief Erase block by the generic 64KB block erase command
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @param start_address Start address of the block to erase
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - or other error passed from the ``set_write_protect``, ``wait_idle`` or ``erase_block`` function of host driver
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_erase_block(esp_flash_t *chip, uint32_t start_address);
|
||||
|
||||
/**
|
||||
* @brief Read from flash by using a read command that matches the programmed
|
||||
* read mode.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @param buffer Buffer to hold the data read from flash
|
||||
* @param address Start address of the data on the flash
|
||||
* @param length Length to read
|
||||
*
|
||||
* @return always ESP_OK currently
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length);
|
||||
|
||||
/**
|
||||
* @brief Perform a page program using the page program command.
|
||||
*
|
||||
* @note Length of each call should not excced the limitation in
|
||||
* ``chip->host->max_write_bytes``. This function is called in
|
||||
* ``spi_flash_chip_generic_write`` recursively until the whole page is
|
||||
* programmed. Strongly suggest to call ``spi_flash_chip_generic_write``
|
||||
* instead.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @param buffer Buffer holding the data to program
|
||||
* @param address Start address to write to flash
|
||||
* @param length Length to write, no longer than ``chip->host->max_write_bytes``.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - or other error passed from the ``wait_idle`` or ``program_page`` function of host driver
|
||||
*/
|
||||
esp_err_t
|
||||
spi_flash_chip_generic_page_program(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
|
||||
|
||||
/**
|
||||
* @brief Perform a generic write. Split the write buffer into page program
|
||||
* operations, and call chip->chip_drv->page-program() for each.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @param buffer Buffer holding the data to program
|
||||
* @param address Start address to write to flash
|
||||
* @param length Length to write
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - or other error passed from the ``wait_idle`` or ``program_page`` function of host driver
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
|
||||
|
||||
/**
|
||||
* @brief Perform a write using on-chip flash encryption. Not implemented yet.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @param buffer Buffer holding the data to program
|
||||
* @param address Start address to write to flash
|
||||
* @param length Length to write
|
||||
*
|
||||
* @return always ESP_ERR_FLASH_UNSUPPORTED_HOST.
|
||||
*/
|
||||
esp_err_t
|
||||
spi_flash_chip_generic_write_encrypted(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
|
||||
|
||||
/**
|
||||
* @brief Send the write enable or write disable command and verify the expected bit (1) in
|
||||
* the status register is set.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @param write_protect true to enable write protection, false to send write enable.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - or other error passed from the ``wait_idle``, ``read_status`` or
|
||||
* ``set_write_protect`` function of host driver
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_set_write_protect(esp_flash_t *chip, bool write_protect);
|
||||
|
||||
/**
|
||||
* @brief Check whether WEL (write enable latch) bit is set in the Status Register read from RDSR.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @param out_write_protect Output of whether the write protect is set.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - or other error passed from the ``read_status`` function of host driver
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_get_write_protect(esp_flash_t *chip, bool *out_write_protect);
|
||||
|
||||
/**
|
||||
* @brief Read flash status via the RDSR command and wait for bit 0 (write in
|
||||
* progress bit) to be cleared.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @param timeout_ms Time to wait before timeout, in ms.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - ESP_ERR_TIMEOUT if not idle before timeout
|
||||
* - or other error passed from the ``wait_idle`` or ``read_status`` function of host driver
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_wait_idle(esp_flash_t *chip, uint32_t timeout_ms);
|
||||
|
||||
/**
|
||||
* @brief Set the specified SPI read mode according to the data in the chip
|
||||
* context. Set quad enable status register bit if needed.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - ESP_ERR_TIMEOUT if not idle before timeout
|
||||
* - or other error passed from the ``set_write_protect`` or ``common_command`` function of host driver
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_set_io_mode(esp_flash_t *chip);
|
||||
|
||||
/**
|
||||
* Get whether the Quad Enable (QE) is set.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
* @param out_quad_mode Pointer to store the output mode.
|
||||
* - SPI_FLASH_QOUT: QE is enabled
|
||||
* - otherwise: QE is disabled
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - or other error passed from the ``common_command`` function of host driver
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_quad_mode);
|
||||
|
||||
/**
|
||||
* Generic SPI flash chip_drv, uses all the above functions for its operations.
|
||||
* In default autodetection, this is used as a catchall if a more specific
|
||||
* chip_drv is not found.
|
||||
*/
|
||||
extern const spi_flash_chip_t esp_flash_chip_generic;
|
||||
|
||||
/*******************************************************************************
|
||||
* Utilities
|
||||
*******************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief Wait for the SPI host hardware state machine to be idle.
|
||||
*
|
||||
* This isn't a flash chip_drv operation, but it's called by
|
||||
* spi_flash_chip_generic_wait_idle() and may be useful when implementing
|
||||
* alternative drivers.
|
||||
*
|
||||
* timeout_ms will be decremented if the function needs to wait until the host hardware is idle.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - ESP_ERR_TIMEOUT if not idle before timeout
|
||||
* - or other error passed from the ``set_write_protect`` or ``common_command`` function of host driver
|
||||
*/
|
||||
esp_err_t spi_flash_generic_wait_host_idle(esp_flash_t *chip, uint32_t *timeout_ms);
|
||||
|
||||
/// Function pointer type for reading status register with QE bit.
|
||||
typedef esp_err_t (*esp_flash_rdsr_func_t)(esp_flash_t* chip, uint32_t* out_sr);
|
||||
|
||||
/**
|
||||
* Use RDSR2 (35H) to read bit 15-8 of the SR, and RDSR (05H) to read bit 7-0.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use.
|
||||
* @param out_sr Pointer to buffer to hold the status register, 16 bits.
|
||||
*
|
||||
* @return ESP_OK if success, otherwise error code passed from the
|
||||
* `common_command` function of the host driver.
|
||||
*/
|
||||
esp_err_t spi_flash_common_read_status_16b_rdsr_rdsr2(esp_flash_t* chip, uint32_t* out_sr);
|
||||
|
||||
/**
|
||||
* Use RDSR2 (35H) to read bit 15-8 of the SR.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use.
|
||||
* @param out_sr Pointer to buffer to hold the status register, 8 bits.
|
||||
*
|
||||
* @return ESP_OK if success, otherwise error code passed from the
|
||||
* `common_command` function of the host driver.
|
||||
*/
|
||||
esp_err_t spi_flash_common_read_status_8b_rdsr2(esp_flash_t* chip, uint32_t* out_sr);
|
||||
|
||||
/**
|
||||
* Use RDSR (05H) to read bit 7-0 of the SR.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use.
|
||||
* @param out_sr Pointer to buffer to hold the status register, 8 bits.
|
||||
*
|
||||
* @return ESP_OK if success, otherwise error code passed from the
|
||||
* `common_command` function of the host driver.
|
||||
*/
|
||||
esp_err_t spi_flash_common_read_status_8b_rdsr(esp_flash_t* chip, uint32_t* out_sr);
|
||||
|
||||
/// Function pointer type for writing status register with QE bit.
|
||||
typedef esp_err_t (*esp_flash_wrsr_func_t)(esp_flash_t* chip, uint32_t sr);
|
||||
|
||||
/**
|
||||
* Use WRSR (01H) to write bit 7-0 of the SR.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use.
|
||||
* @param sr Value of the status register to write, 8 bits.
|
||||
*
|
||||
* @return ESP_OK if success, otherwise error code passed from the
|
||||
* `common_command` function of the host driver.
|
||||
*/
|
||||
esp_err_t spi_flash_common_write_status_8b_wrsr(esp_flash_t* chip, uint32_t sr);
|
||||
|
||||
/**
|
||||
* Use WRSR (01H) to write bit 15-0 of the SR.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use.
|
||||
* @param sr Value of the status register to write, 16 bits.
|
||||
*
|
||||
* @return ESP_OK if success, otherwise error code passed from the
|
||||
* `common_command` function of the host driver.
|
||||
*/
|
||||
esp_err_t spi_flash_common_write_status_16b_wrsr(esp_flash_t* chip, uint32_t sr);
|
||||
|
||||
/**
|
||||
* Use WRSR2 (31H) to write bit 15-8 of the SR.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use.
|
||||
* @param sr Value of the status register to write, 8 bits.
|
||||
*
|
||||
* @return ESP_OK if success, otherwise error code passed from the
|
||||
* `common_command` function of the host driver.
|
||||
*/
|
||||
esp_err_t spi_flash_common_write_status_8b_wrsr2(esp_flash_t* chip, uint32_t sr);
|
||||
|
||||
/**
|
||||
* @brief Utility function for set_read_mode chip_drv function. If required,
|
||||
* set and check the QE bit in the flash chip to enable the QIO/QOUT mode.
|
||||
*
|
||||
* Most chip QE enable follows a common pattern, though commands to read/write
|
||||
* the status register may be different, as well as the position of QE bit.
|
||||
*
|
||||
* Registers to actually do Quad transtions and command to be sent in reading
|
||||
* should also be configured via
|
||||
* spi_flash_chip_generic_config_host_io_mode().
|
||||
*
|
||||
* Note that the bit length and qe position of wrsr_func, rdsr_func and
|
||||
* qe_sr_bit should be consistent.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use.
|
||||
* @param wrsr_func Function pointer for writing the status register
|
||||
* @param rdsr_func Function pointer for reading the status register
|
||||
* @param qe_sr_bit status with the qe bit only.
|
||||
*
|
||||
* @return always ESP_OK (currently).
|
||||
*/
|
||||
esp_err_t spi_flash_common_set_io_mode(esp_flash_t *chip, esp_flash_wrsr_func_t wrsr_func, esp_flash_rdsr_func_t rdsr_func, uint32_t qe_sr_bit);
|
||||
|
||||
/**
|
||||
* @brief Configure the host registers to use the specified read mode set in
|
||||
* the ``chip->read_mode``.
|
||||
*
|
||||
* Usually called in chip_drv read() functions before actual reading
|
||||
* transactions. Also prepare the command to be sent in read functions.
|
||||
*
|
||||
* @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if success
|
||||
* - ESP_ERR_FLASH_NOT_INITIALISED if chip not initialized properly
|
||||
* - or other error passed from the ``configure_host_mode`` function of host driver
|
||||
*/
|
||||
esp_err_t spi_flash_chip_generic_config_host_io_mode(esp_flash_t *chip);
|
27
tools/sdk/include/spi_flash/spi_flash_chip_issi.h
Normal file
27
tools/sdk/include/spi_flash/spi_flash_chip_issi.h
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "esp_flash.h"
|
||||
#include "spi_flash_chip_driver.h"
|
||||
|
||||
|
||||
/**
|
||||
* ISSI SPI flash chip_drv, uses all the above functions for its operations. In
|
||||
* default autodetection, this is used as a catchall if a more specific chip_drv
|
||||
* is not found.
|
||||
*/
|
||||
extern const spi_flash_chip_t esp_flash_chip_issi;
|
Reference in New Issue
Block a user