From 3dd1cfea18b65d647d9b412a6fb9458c207d75de Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Tue, 28 Sep 2021 14:12:56 +0800 Subject: [PATCH 1/5] spi_flash: refactor spi_flash.h to decline duplicated code --- components/app_update/test/test_switch_ota.c | 6 +- .../src/bootloader_common.c | 14 +- .../src/bootloader_common_loader.c | 7 +- .../bootloader_support/src/bootloader_flash.c | 15 +- .../src/bootloader_flash_config_esp32.c | 1 + .../src/bootloader_flash_config_esp32c3.c | 1 + .../src/bootloader_flash_config_esp32h2.c | 1 + .../src/bootloader_flash_config_esp32s2.c | 1 + .../src/bootloader_flash_config_esp32s3.c | 1 + .../src/bootloader_utility.c | 6 +- .../src/esp32/bootloader_esp32.c | 2 +- .../src/esp32c3/bootloader_esp32c3.c | 3 +- .../src/esp32h2/bootloader_esp32h2.c | 3 +- .../src/esp32s2/bootloader_esp32s2.c | 2 +- .../src/esp32s3/bootloader_esp32s3.c | 2 +- .../bootloader_support/src/flash_partitions.c | 14 +- .../bootloader_support/src/flash_qio_mode.c | 14 +- .../esp_hw_support/port/esp32/spiram_psram.c | 1 + .../port/esp32s2/spiram_psram.c | 1 + .../port/esp32s3/opiram_psram.c | 1 + .../port/esp32s3/spiram_psram.c | 1 + .../esp_rom/include/esp32/rom/spi_flash.h | 451 +--------------- .../esp_rom/include/esp32c3/rom/spi_flash.h | 483 +---------------- .../esp_rom/include/esp32h2/rom/spi_flash.h | 475 +---------------- .../esp_rom/include/esp32s2/rom/opi_flash.h | 1 + .../esp_rom/include/esp32s2/rom/spi_flash.h | 471 +---------------- .../esp_rom/include/esp32s3/rom/opi_flash.h | 1 + .../esp_rom/include/esp32s3/rom/spi_flash.h | 490 +----------------- components/esp_rom/include/esp_rom_spiflash.h | 468 +++++++++++++++++ components/esp_system/port/cpu_start.c | 12 +- components/spi_flash/cache_utils.c | 6 +- components/spi_flash/esp32/flash_ops_esp32.c | 19 +- .../spi_flash/esp32/spi_flash_rom_patch.c | 19 +- .../spi_flash/esp32c3/flash_ops_esp32c3.c | 19 +- .../spi_flash/esp32c3/spi_flash_rom_patch.c | 19 +- .../spi_flash/esp32h2/flash_ops_esp32h2.c | 19 +- .../spi_flash/esp32h2/spi_flash_rom_patch.c | 19 +- .../spi_flash/esp32s2/flash_ops_esp32s2.c | 21 +- .../spi_flash/esp32s2/spi_flash_rom_patch.c | 19 +- .../spi_flash/esp32s3/flash_ops_esp32s3.c | 19 +- .../esp32s3/spi_flash_oct_flash_init.c | 1 + .../spi_flash/esp32s3/spi_timing_config.h | 1 + components/spi_flash/esp_flash_api.c | 12 +- components/spi_flash/esp_flash_spi_init.c | 15 +- components/spi_flash/flash_mmap.c | 6 +- components/spi_flash/flash_ops.c | 5 +- .../include/esp_private/spi_flash_os.h | 10 +- components/spi_flash/sim/SpiFlash.h | 19 +- components/spi_flash/sim/flash_mock.cpp | 1 + components/spi_flash/sim/flash_mock_util.c | 1 + .../spi_flash/test/test_large_flash_writes.c | 20 +- components/spi_flash/test/test_read_write.c | 9 +- components/spi_flash/test/test_spi_flash.c | 10 +- components/spiffs/esp_spiffs.c | 12 +- tools/ci/check_copyright_ignore.txt | 16 - 55 files changed, 621 insertions(+), 2645 deletions(-) create mode 100644 components/esp_rom/include/esp_rom_spiflash.h diff --git a/components/app_update/test/test_switch_ota.c b/components/app_update/test/test_switch_ota.c index afdd6be8e1..7ccdfc78f5 100644 --- a/components/app_update/test/test_switch_ota.c +++ b/components/app_update/test/test_switch_ota.c @@ -12,11 +12,7 @@ #include "string.h" #include "sdkconfig.h" -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#endif +#include "esp_rom_spiflash.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" diff --git a/components/bootloader_support/src/bootloader_common.c b/components/bootloader_support/src/bootloader_common.c index 9cdcc6a975..e4594f2fdf 100644 --- a/components/bootloader_support/src/bootloader_common.c +++ b/components/bootloader_support/src/bootloader_common.c @@ -9,19 +9,7 @@ #include "sdkconfig.h" #include "esp_err.h" #include "esp_log.h" -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP8684 -#include "esp8684/rom/spi_flash.h" -#endif +#include "esp_rom_spiflash.h" #include "esp_rom_crc.h" #include "esp_rom_gpio.h" #include "esp_rom_sys.h" diff --git a/components/bootloader_support/src/bootloader_common_loader.c b/components/bootloader_support/src/bootloader_common_loader.c index ba905370d3..7d064dc4ef 100644 --- a/components/bootloader_support/src/bootloader_common_loader.c +++ b/components/bootloader_support/src/bootloader_common_loader.c @@ -8,12 +8,7 @@ #include "sdkconfig.h" #include "esp_err.h" #include "esp_log.h" -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#include "esp32s2/rom/ets_sys.h" -#endif +#include "esp_rom_spiflash.h" #include "esp_rom_crc.h" #include "esp_rom_gpio.h" #include "esp_flash_partitions.h" diff --git a/components/bootloader_support/src/bootloader_flash.c b/components/bootloader_support/src/bootloader_flash.c index 6c331fc6a7..d038fc525d 100644 --- a/components/bootloader_support/src/bootloader_flash.c +++ b/components/bootloader_support/src/bootloader_flash.c @@ -23,19 +23,7 @@ # define SPIFLASH SPIMEM1 #endif -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP8684 -#include "esp8684/rom/spi_flash.h" -#endif +#include "esp_rom_spiflash.h" #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH #define ENCRYPTION_IS_VIRTUAL 1 @@ -150,6 +138,7 @@ esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size) #include "esp8684/rom/cache.h" #include "soc/cache_memory.h" #endif +#include "esp_rom_spiflash.h" static const char *TAG = "bootloader_flash"; #if CONFIG_IDF_TARGET_ESP32 diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32.c b/components/bootloader_support/src/bootloader_flash_config_esp32.c index 40e5be2a7a..e7d12178fc 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32.c +++ b/components/bootloader_support/src/bootloader_flash_config_esp32.c @@ -21,6 +21,7 @@ #include "flash_qio_mode.h" #include "bootloader_common.h" #include "bootloader_flash_config.h" +#include "esp_rom_spiflash.h" void bootloader_flash_update_id(void) { diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32c3.c b/components/bootloader_support/src/bootloader_flash_config_esp32c3.c index 61ec2de7cc..afea291240 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32c3.c +++ b/components/bootloader_support/src/bootloader_flash_config_esp32c3.c @@ -20,6 +20,7 @@ #include "flash_qio_mode.h" #include "bootloader_flash_config.h" #include "bootloader_common.h" +#include "esp_rom_spiflash.h" #define FLASH_IO_MATRIX_DUMMY_40M 0 #define FLASH_IO_MATRIX_DUMMY_80M 0 diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32h2.c b/components/bootloader_support/src/bootloader_flash_config_esp32h2.c index 547e86767f..9fa9364b89 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32h2.c +++ b/components/bootloader_support/src/bootloader_flash_config_esp32h2.c @@ -20,6 +20,7 @@ #include "flash_qio_mode.h" #include "bootloader_flash_config.h" #include "bootloader_common.h" +#include "esp_rom_spiflash.h" #define FLASH_IO_MATRIX_DUMMY_40M 0 #define FLASH_IO_MATRIX_DUMMY_80M 0 diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32s2.c b/components/bootloader_support/src/bootloader_flash_config_esp32s2.c index 1959b1b38e..73bccd895f 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32s2.c +++ b/components/bootloader_support/src/bootloader_flash_config_esp32s2.c @@ -17,6 +17,7 @@ #include "flash_qio_mode.h" #include "bootloader_flash_config.h" #include "bootloader_common.h" +#include "esp_rom_spiflash.h" #define FLASH_IO_MATRIX_DUMMY_40M 0 #define FLASH_IO_MATRIX_DUMMY_80M 0 diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32s3.c b/components/bootloader_support/src/bootloader_flash_config_esp32s3.c index 357de775a5..b8f37b0f82 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32s3.c +++ b/components/bootloader_support/src/bootloader_flash_config_esp32s3.c @@ -17,6 +17,7 @@ #include "flash_qio_mode.h" #include "bootloader_flash_config.h" #include "bootloader_common.h" +#include "esp_rom_spiflash.h" #define FLASH_IO_MATRIX_DUMMY_40M 0 #define FLASH_IO_MATRIX_DUMMY_80M 0 diff --git a/components/bootloader_support/src/bootloader_utility.c b/components/bootloader_support/src/bootloader_utility.c index e6e44179b9..5399756b58 100644 --- a/components/bootloader_support/src/bootloader_utility.c +++ b/components/bootloader_support/src/bootloader_utility.c @@ -17,17 +17,14 @@ #if CONFIG_IDF_TARGET_ESP32 #include "soc/dport_reg.h" #include "esp32/rom/cache.h" -#include "esp32/rom/spi_flash.h" #include "esp32/rom/secure_boot.h" #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/rom/cache.h" -#include "esp32s2/rom/spi_flash.h" #include "esp32s2/rom/secure_boot.h" #include "soc/extmem_reg.h" #include "soc/cache_memory.h" #elif CONFIG_IDF_TARGET_ESP32S3 #include "esp32s3/rom/cache.h" -#include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/secure_boot.h" #include "soc/extmem_reg.h" #include "soc/cache_memory.h" @@ -35,7 +32,6 @@ #include "esp32c3/rom/cache.h" #include "esp32c3/rom/efuse.h" #include "esp32c3/rom/ets_sys.h" -#include "esp32c3/rom/spi_flash.h" #include "esp32c3/rom/crc.h" #include "esp32c3/rom/uart.h" #include "esp32c3/rom/gpio.h" @@ -46,7 +42,6 @@ #include "esp32h2/rom/cache.h" #include "esp32h2/rom/efuse.h" #include "esp32h2/rom/ets_sys.h" -#include "esp32h2/rom/spi_flash.h" #include "esp32h2/rom/crc.h" #include "esp32h2/rom/uart.h" #include "esp32h2/rom/gpio.h" @@ -68,6 +63,7 @@ #else // CONFIG_IDF_TARGET_* #error "Unsupported IDF_TARGET" #endif +#include "esp_rom_spiflash.h" #include "soc/soc.h" #include "esp_cpu.h" diff --git a/components/bootloader_support/src/esp32/bootloader_esp32.c b/components/bootloader_support/src/esp32/bootloader_esp32.c index c2632074ef..a42f30ebd7 100644 --- a/components/bootloader_support/src/esp32/bootloader_esp32.c +++ b/components/bootloader_support/src/esp32/bootloader_esp32.c @@ -32,7 +32,7 @@ #include "esp_rom_gpio.h" #include "esp_rom_efuse.h" #include "esp_rom_sys.h" -#include "esp32/rom/spi_flash.h" +#include "esp_rom_spiflash.h" #include "esp_efuse.h" static const char *TAG = "boot.esp32"; diff --git a/components/bootloader_support/src/esp32c3/bootloader_esp32c3.c b/components/bootloader_support/src/esp32c3/bootloader_esp32c3.c index 3e926a6e2d..f21d610769 100644 --- a/components/bootloader_support/src/esp32c3/bootloader_esp32c3.c +++ b/components/bootloader_support/src/esp32c3/bootloader_esp32c3.c @@ -13,6 +13,7 @@ #include "esp_rom_efuse.h" #include "esp_rom_uart.h" #include "esp_rom_sys.h" +#include "esp_rom_spiflash.h" #include "soc/efuse_reg.h" #include "soc/gpio_sig_map.h" #include "soc/io_mux_reg.h" @@ -24,10 +25,8 @@ #include "soc/io_mux_reg.h" #include "soc/system_reg.h" #include "esp32c3/rom/efuse.h" -#include "esp32c3/rom/spi_flash.h" #include "esp32c3/rom/cache.h" #include "esp32c3/rom/ets_sys.h" -#include "esp32c3/rom/spi_flash.h" #include "bootloader_common.h" #include "bootloader_init.h" #include "bootloader_clock.h" diff --git a/components/bootloader_support/src/esp32h2/bootloader_esp32h2.c b/components/bootloader_support/src/esp32h2/bootloader_esp32h2.c index eb317c370f..e28214439d 100644 --- a/components/bootloader_support/src/esp32h2/bootloader_esp32h2.c +++ b/components/bootloader_support/src/esp32h2/bootloader_esp32h2.c @@ -13,6 +13,7 @@ #include "esp_rom_efuse.h" #include "esp_rom_uart.h" #include "esp_rom_sys.h" +#include "esp_rom_spiflash.h" #include "soc/efuse_reg.h" #include "soc/gpio_sig_map.h" #include "soc/io_mux_reg.h" @@ -24,10 +25,8 @@ #include "soc/io_mux_reg.h" #include "soc/system_reg.h" #include "esp32h2/rom/efuse.h" -#include "esp32h2/rom/spi_flash.h" #include "esp32h2/rom/cache.h" #include "esp32h2/rom/ets_sys.h" -#include "esp32h2/rom/spi_flash.h" #include "bootloader_common.h" #include "bootloader_init.h" #include "bootloader_clock.h" diff --git a/components/bootloader_support/src/esp32s2/bootloader_esp32s2.c b/components/bootloader_support/src/esp32s2/bootloader_esp32s2.c index ea28e055bb..0a7f85efd8 100644 --- a/components/bootloader_support/src/esp32s2/bootloader_esp32s2.c +++ b/components/bootloader_support/src/esp32s2/bootloader_esp32s2.c @@ -22,8 +22,8 @@ #include "esp_rom_gpio.h" #include "esp_rom_efuse.h" #include "esp_rom_sys.h" +#include "esp_rom_spiflash.h" #include "esp32s2/rom/cache.h" -#include "esp32s2/rom/spi_flash.h" #include "esp_attr.h" #include "esp_log.h" diff --git a/components/bootloader_support/src/esp32s3/bootloader_esp32s3.c b/components/bootloader_support/src/esp32s3/bootloader_esp32s3.c index 0aff0d5c9d..3ed1614802 100644 --- a/components/bootloader_support/src/esp32s3/bootloader_esp32s3.c +++ b/components/bootloader_support/src/esp32s3/bootloader_esp32s3.c @@ -23,7 +23,7 @@ #include "esp_rom_gpio.h" #include "esp_rom_efuse.h" #include "esp_rom_sys.h" -#include "esp32s3/rom/spi_flash.h" +#include "esp_rom_spiflash.h" #include "esp32s3/rom/cache.h" #include "esp32s3/rom/rtc.h" diff --git a/components/bootloader_support/src/flash_partitions.c b/components/bootloader_support/src/flash_partitions.c index 3289906e82..d043941c81 100644 --- a/components/bootloader_support/src/flash_partitions.c +++ b/components/bootloader_support/src/flash_partitions.c @@ -7,19 +7,7 @@ #include "esp_flash_partitions.h" #include "esp_log.h" #include "esp_rom_md5.h" -#if CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP8684 -#include "esp8684/rom/spi_flash.h" -#else -#include "esp32/rom/spi_flash.h" -#endif +#include "esp_rom_spiflash.h" static const char *TAG = "flash_parts"; diff --git a/components/bootloader_support/src/flash_qio_mode.c b/components/bootloader_support/src/flash_qio_mode.c index 1d7a4e78f3..42b65f6421 100644 --- a/components/bootloader_support/src/flash_qio_mode.c +++ b/components/bootloader_support/src/flash_qio_mode.c @@ -12,20 +12,8 @@ #include "esp_log.h" #include "esp_err.h" #include "esp_rom_efuse.h" +#include "esp_rom_spiflash.h" #include "flash_qio_mode.h" -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP8684 -#include "esp8684/rom/spi_flash.h" -#endif #include "soc/efuse_periph.h" #include "soc/io_mux_reg.h" diff --git a/components/esp_hw_support/port/esp32/spiram_psram.c b/components/esp_hw_support/port/esp32/spiram_psram.c index 35ca3b90e7..817f7fba3d 100644 --- a/components/esp_hw_support/port/esp32/spiram_psram.c +++ b/components/esp_hw_support/port/esp32/spiram_psram.c @@ -31,6 +31,7 @@ #include "bootloader_common.h" #include "esp_rom_gpio.h" #include "bootloader_flash_config.h" +#include "esp_rom_spiflash.h" #if CONFIG_SPIRAM #include "soc/rtc.h" diff --git a/components/esp_hw_support/port/esp32s2/spiram_psram.c b/components/esp_hw_support/port/esp32s2/spiram_psram.c index 886e08b045..2c0acebc85 100644 --- a/components/esp_hw_support/port/esp32s2/spiram_psram.c +++ b/components/esp_hw_support/port/esp32s2/spiram_psram.c @@ -33,6 +33,7 @@ #include "driver/spi_common.h" #include "esp_private/periph_ctrl.h" #include "bootloader_common.h" +#include "esp_rom_spiflash.h" #if CONFIG_SPIRAM #include "soc/rtc.h" diff --git a/components/esp_hw_support/port/esp32s3/opiram_psram.c b/components/esp_hw_support/port/esp32s3/opiram_psram.c index efcecdbc6b..d29d564a36 100644 --- a/components/esp_hw_support/port/esp32s3/opiram_psram.c +++ b/components/esp_hw_support/port/esp32s3/opiram_psram.c @@ -24,6 +24,7 @@ #include "driver/gpio.h" #include "driver/spi_common.h" #include "esp_private/periph_ctrl.h" +#include "esp_rom_spiflash.h" #if CONFIG_SPIRAM_MODE_OCT #include "soc/rtc.h" diff --git a/components/esp_hw_support/port/esp32s3/spiram_psram.c b/components/esp_hw_support/port/esp32s3/spiram_psram.c index 9ca43271ec..22129d8940 100644 --- a/components/esp_hw_support/port/esp32s3/spiram_psram.c +++ b/components/esp_hw_support/port/esp32s3/spiram_psram.c @@ -35,6 +35,7 @@ #include "driver/spi_common.h" #include "esp_private/periph_ctrl.h" #include "bootloader_common.h" +#include "esp_rom_spiflash.h" #if CONFIG_SPIRAM_MODE_QUAD #include "soc/rtc.h" diff --git a/components/esp_rom/include/esp32/rom/spi_flash.h b/components/esp_rom/include/esp32/rom/spi_flash.h index 500ba300cb..db70b1ba90 100644 --- a/components/esp_rom/include/esp32/rom/spi_flash.h +++ b/components/esp_rom/include/esp32/rom/spi_flash.h @@ -1,16 +1,8 @@ -// Copyright 2010-2016 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. +/* + * SPDX-FileCopyrightText: 2010-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef _ROM_SPI_FLASH_H_ #define _ROM_SPI_FLASH_H_ @@ -30,14 +22,6 @@ extern "C" { #endif -/** \defgroup spi_flash_apis, spi flash operation related apis - * @brief spi_flash apis - */ - -/** @addtogroup spi_flash_apis - * @{ - */ - /************************************************************* * Note ************************************************************* @@ -131,431 +115,6 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 -typedef enum { - ESP_ROM_SPIFLASH_QIO_MODE = 0, - ESP_ROM_SPIFLASH_QOUT_MODE, - ESP_ROM_SPIFLASH_DIO_MODE, - ESP_ROM_SPIFLASH_DOUT_MODE, - ESP_ROM_SPIFLASH_FASTRD_MODE, - ESP_ROM_SPIFLASH_SLOWRD_MODE -} esp_rom_spiflash_read_mode_t; - -typedef enum { - ESP_ROM_SPIFLASH_RESULT_OK, - ESP_ROM_SPIFLASH_RESULT_ERR, - ESP_ROM_SPIFLASH_RESULT_TIMEOUT -} esp_rom_spiflash_result_t; - -typedef struct { - uint32_t device_id; - uint32_t chip_size; // chip size in bytes - uint32_t block_size; - uint32_t sector_size; - uint32_t page_size; - uint32_t status_mask; -} esp_rom_spiflash_chip_t; - -typedef struct { - uint8_t data_length; - uint8_t read_cmd0; - uint8_t read_cmd1; - uint8_t write_cmd; - uint16_t data_mask; - uint16_t data; -} esp_rom_spiflash_common_cmd_t; - -/** - * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. - * Please do not call this function in SDK. - * - * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). - * - * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. - * - * @return None - */ -void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); - -/** - * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); - -/** - * @brief Set SPI Flash pad drivers. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid - * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. - * Values usually read from falsh by rom code, function usually callde by rom code. - * if value with bit(3) set, the value is valid, bit[2:0] is the real value. - * - * @return None - */ -void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); - -/** - * @brief Select SPI Flash function for pads. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); - -/** - * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t legacy: In legacy mode, more SPI command is used in line. - * - * @return None - */ -void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); - -/** - * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t *status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); - -/** - * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t *status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); - -/** - * @brief Write status to Flash status register. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t status_value : Value to . - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); - -/** - * @brief Use a command to Read Flash status register. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t*status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); - -/** - * @brief Config SPI Flash read mode when init. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. - * - * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : config error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); - -/** - * @brief Config SPI Flash clock divisor. - * Please do not call this function in SDK. - * - * @param uint8_t freqdiv: clock divisor. - * - * @param uint8_t spi: 0 for SPI0, 1 for SPI1. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : config error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); - -/** - * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. - * - * @return uint16_t 0 : do not send command any more. - * 1 : go to the next command. - * n > 1 : skip (n - 1) commands. - */ -uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); - -/** - * @brief Unlock SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); - -/** - * @brief SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Lock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Lock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Lock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_lock(void); - -/** - * @brief Update SPI Flash parameter. - * Please do not call this function in SDK. - * - * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. - * - * @param uint32_t chip_size : The Flash size. - * - * @param uint32_t block_size : The Flash block size. - * - * @param uint32_t sector_size : The Flash sector size. - * - * @param uint32_t page_size : The Flash page size. - * - * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, - uint32_t sector_size, uint32_t page_size, uint32_t status_mask); - -/** - * @brief Erase whole flash chip. - * Please do not call this function in SDK. - * - * @param None - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); - -/** - * @brief Erase a 64KB block of flash - * Uses SPI flash command D8H. - * Please do not call this function in SDK. - * - * @param uint32_t block_num : Which block to erase. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); - -/** - * @brief Erase a sector of flash. - * Uses SPI flash command 20H. - * Please do not call this function in SDK. - * - * @param uint32_t sector_num : Which sector to erase. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); - -/** - * @brief Erase some sectors. - * Please do not call this function in SDK. - * - * @param uint32_t start_addr : Start addr to erase, should be sector aligned. - * - * @param uint32_t area_len : Length to erase, should be sector aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); - -/** - * @brief Write Data to Flash, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. - * - * @param const uint32_t *src : The pointer to data which is to write. - * - * @param uint32_t len : Length to write, should be 4 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); - -/** - * @brief Read Data from Flash, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. - * - * @param uint32_t *dest : The buf to read the data. - * - * @param uint32_t len : Length to read, should be 4 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); - -/** - * @brief SPI1 go into encrypto mode. - * Please do not call this function in SDK. - * - * @param None - * - * @return None - */ -void esp_rom_spiflash_write_encrypted_enable(void); - -/** - * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. - * - * @param uint32_t *data : The pointer to data which is to write. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); - -/** - * @brief SPI1 go out of encrypto mode. - * Please do not call this function in SDK. - * - * @param None - * - * @return None - */ -void esp_rom_spiflash_write_encrypted_disable(void); - -/** - * @brief Write data to flash with transparent encryption. - * @note Sectors to be written should already be erased. - * - * @note Please do not call this function in SDK. - * - * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. - * - * @param uint32_t *data : The pointer to data to write. Note, this pointer must - * be 32 bit aligned and the content of the data will be - * modified by the encryption function. - * - * @param uint32_t len : Length to write, should be 32 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. - * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); - - -/** @brief Wait until SPI flash write operation is complete - * - * @note Please do not call this function in SDK. - * - * Reads the Write In Progress bit of the SPI flash status register, - * repeats until this bit is zero (indicating write complete). - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete - * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); - - -/** @brief Enable Quad I/O pin functions - * - * @note Please do not call this function in SDK. - * - * Sets the HD & WP pin functions for Quad I/O modes, based on the - * efuse SPI pin configuration. - * - * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. - * - * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). - * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. - * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. - * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used - * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). - * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. - */ -void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); - -/** - * @brief Clear WEL bit unconditionally. - * - * @return always ESP_ROM_SPIFLASH_RESULT_OK - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); - -/** @brief Global esp_rom_spiflash_chip_t structure used by ROM functions - * - */ -extern esp_rom_spiflash_chip_t g_rom_flashchip; - extern uint8_t g_rom_spiflash_dummy_len_plus[]; /** diff --git a/components/esp_rom/include/esp32c3/rom/spi_flash.h b/components/esp_rom/include/esp32c3/rom/spi_flash.h index b591d4000f..83f330680d 100644 --- a/components/esp_rom/include/esp32c3/rom/spi_flash.h +++ b/components/esp_rom/include/esp32c3/rom/spi_flash.h @@ -1,16 +1,8 @@ -// Copyright 2020 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. +/* + * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef _ROM_SPI_FLASH_H_ #define _ROM_SPI_FLASH_H_ @@ -24,14 +16,6 @@ extern "C" { #endif -/** \defgroup spi_flash_apis, spi flash operation related apis - * @brief spi_flash apis - */ - -/** @addtogroup spi_flash_apis - * @{ - */ - #define PERIPHS_SPI_FLASH_CMD SPI_MEM_CMD_REG(1) #define PERIPHS_SPI_FLASH_ADDR SPI_MEM_ADDR_REG(1) #define PERIPHS_SPI_FLASH_CTRL SPI_MEM_CTRL_REG(1) @@ -87,430 +71,10 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 typedef enum { - ESP_ROM_SPIFLASH_QIO_MODE = 0, - ESP_ROM_SPIFLASH_QOUT_MODE, - ESP_ROM_SPIFLASH_DIO_MODE, - ESP_ROM_SPIFLASH_DOUT_MODE, - ESP_ROM_SPIFLASH_FASTRD_MODE, - ESP_ROM_SPIFLASH_SLOWRD_MODE -} esp_rom_spiflash_read_mode_t; - -typedef enum { - ESP_ROM_SPIFLASH_RESULT_OK, - ESP_ROM_SPIFLASH_RESULT_ERR, - ESP_ROM_SPIFLASH_RESULT_TIMEOUT -} esp_rom_spiflash_result_t; - -typedef struct { - uint32_t device_id; - uint32_t chip_size; // chip size in bytes - uint32_t block_size; - uint32_t sector_size; - uint32_t page_size; - uint32_t status_mask; -} esp_rom_spiflash_chip_t; - -typedef struct { - uint8_t data_length; - uint8_t read_cmd0; - uint8_t read_cmd1; - uint8_t write_cmd; - uint16_t data_mask; - uint16_t data; -} esp_rom_spiflash_common_cmd_t; - -/** - * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. - * Please do not call this function in SDK. - * - * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). - * - * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. - * - * @return None - */ -void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); - -/** - * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); - -/** - * @brief Set SPI Flash pad drivers. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid - * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. - * Values usually read from falsh by rom code, function usually callde by rom code. - * if value with bit(3) set, the value is valid, bit[2:0] is the real value. - * - * @return None - */ -void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); - -/** - * @brief Select SPI Flash function for pads. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); - -/** - * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t legacy: In legacy mode, more SPI command is used in line. - * - * @return None - */ -void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); - -/** - * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t *status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); - -/** - * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t *status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); - -/** - * @brief Write status to Falsh status register. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t status_value : Value to . - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); - -/** - * @brief Use a command to Read Flash status register. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t*status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); - -/** - * @brief Config SPI Flash read mode when init. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. - * - * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : config error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); - -/** - * @brief Config SPI Flash clock divisor. - * Please do not call this function in SDK. - * - * @param uint8_t freqdiv: clock divisor. - * - * @param uint8_t spi: 0 for SPI0, 1 for SPI1. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : config error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); - -/** - * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. - * - * @return uint16_t 0 : do not send command any more. - * 1 : go to the next command. - * n > 1 : skip (n - 1) commands. - */ -uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); - -/** - * @brief Unlock SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); - -/** - * @brief SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Lock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Lock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Lock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_lock(void); - -/** - * @brief Update SPI Flash parameter. - * Please do not call this function in SDK. - * - * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. - * - * @param uint32_t chip_size : The Flash size. - * - * @param uint32_t block_size : The Flash block size. - * - * @param uint32_t sector_size : The Flash sector size. - * - * @param uint32_t page_size : The Flash page size. - * - * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, - uint32_t sector_size, uint32_t page_size, uint32_t status_mask); - -/** - * @brief Erase whole flash chip. - * Please do not call this function in SDK. - * - * @param None - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); - -/** - * @brief Erase a 64KB block of flash - * Uses SPI flash command D8H. - * Please do not call this function in SDK. - * - * @param uint32_t block_num : Which block to erase. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); - -/** - * @brief Erase a sector of flash. - * Uses SPI flash command 20H. - * Please do not call this function in SDK. - * - * @param uint32_t sector_num : Which sector to erase. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); - -/** - * @brief Erase some sectors. - * Please do not call this function in SDK. - * - * @param uint32_t start_addr : Start addr to erase, should be sector aligned. - * - * @param uint32_t area_len : Length to erase, should be sector aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); - -/** - * @brief Write Data to Flash, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. - * - * @param const uint32_t *src : The pointer to data which is to write. - * - * @param uint32_t len : Length to write, should be 4 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); - -/** - * @brief Read Data from Flash, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. - * - * @param uint32_t *dest : The buf to read the data. - * - * @param uint32_t len : Length to read, should be 4 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); - -/** - * @brief SPI1 go into encrypto mode. - * Please do not call this function in SDK. - * - * @param None - * - * @return None - */ -void esp_rom_spiflash_write_encrypted_enable(void); - -/** - * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. - * - * @param uint32_t *data : The pointer to data which is to write. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); - -/** - * @brief SPI1 go out of encrypto mode. - * Please do not call this function in SDK. - * - * @param None - * - * @return None - */ -void esp_rom_spiflash_write_encrypted_disable(void); - -/** - * @brief Write data to flash with transparent encryption. - * @note Sectors to be written should already be erased. - * - * @note Please do not call this function in SDK. - * - * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. - * - * @param uint32_t *data : The pointer to data to write. Note, this pointer must - * be 32 bit aligned and the content of the data will be - * modified by the encryption function. - * - * @param uint32_t len : Length to write, should be 32 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. - * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); - - -/* TODO: figure out how to map these to their new names */ -typedef enum { - SPI_ENCRYPT_DESTINATION_FLASH, -} SpiEncryptDest; - -typedef esp_rom_spiflash_result_t SpiFlashOpResult; - -SpiFlashOpResult SPI_Encrypt_Write(uint32_t flash_addr, const void *data, uint32_t len); -SpiFlashOpResult SPI_Encrypt_Write_Dest(SpiEncryptDest dest, uint32_t flash_addr, const void *data, uint32_t len); -void SPI_Write_Encrypt_Enable(void); -void SPI_Write_Encrypt_Disable(void); - -/** @brief Wait until SPI flash write operation is complete - * - * @note Please do not call this function in SDK. - * - * Reads the Write In Progress bit of the SPI flash status register, - * repeats until this bit is zero (indicating write complete). - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete - * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); - - -/** @brief Enable Quad I/O pin functions - * - * @note Please do not call this function in SDK. - * - * Sets the HD & WP pin functions for Quad I/O modes, based on the - * efuse SPI pin configuration. - * - * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. - * - * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). - * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. - * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. - * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used - * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). - * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. - */ -void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); - + SPI_FLASH_RESULT_OK, + SPI_FLASH_RESULT_ERR, + SPI_FLASH_RESULT_TIMEOUT +} SpiFlashOpResult; typedef void (* spi_flash_func_t)(void); typedef SpiFlashOpResult (* spi_flash_op_t)(void); @@ -534,35 +98,6 @@ typedef struct { spi_flash_op_t wait_idle; } spiflash_legacy_funcs_t; - -extern const spiflash_legacy_funcs_t *rom_spiflash_legacy_funcs; - -/** @brief Global ROM spiflash data, as used by legacy - SPI flash functions -*/ -typedef struct { - esp_rom_spiflash_chip_t chip; - uint8_t dummy_len_plus[3]; - uint8_t sig_matrix; -} spiflash_legacy_data_t; - -extern spiflash_legacy_data_t *rom_spiflash_legacy_data; - -/* Defines to make the C3 ROM legacvy data access compatible with previous chips */ -#define g_rom_flashchip (rom_spiflash_legacy_data->chip) -#define g_rom_spiflash_dummy_len_plus (rom_spiflash_legacy_data->dummy_len_plus) - -/** - * @brief Clear WEL bit unconditionally. - * - * @return always ESP_ROM_SPIFLASH_RESULT_OK - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); - -/** - * @} - */ - #ifdef __cplusplus } #endif diff --git a/components/esp_rom/include/esp32h2/rom/spi_flash.h b/components/esp_rom/include/esp32h2/rom/spi_flash.h index 139aee5066..9f3a1dab8c 100644 --- a/components/esp_rom/include/esp32h2/rom/spi_flash.h +++ b/components/esp_rom/include/esp32h2/rom/spi_flash.h @@ -1,16 +1,8 @@ -// Copyright 2020 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. +/* + * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef _ROM_SPI_FLASH_H_ #define _ROM_SPI_FLASH_H_ @@ -24,14 +16,6 @@ extern "C" { #endif -/** \defgroup spi_flash_apis, spi flash operation related apis - * @brief spi_flash apis - */ - -/** @addtogroup spi_flash_apis - * @{ - */ - #define PERIPHS_SPI_FLASH_CMD SPI_MEM_CMD_REG(1) #define PERIPHS_SPI_FLASH_ADDR SPI_MEM_ADDR_REG(1) #define PERIPHS_SPI_FLASH_CTRL SPI_MEM_CTRL_REG(1) @@ -87,430 +71,10 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 typedef enum { - ESP_ROM_SPIFLASH_QIO_MODE = 0, - ESP_ROM_SPIFLASH_QOUT_MODE, - ESP_ROM_SPIFLASH_DIO_MODE, - ESP_ROM_SPIFLASH_DOUT_MODE, - ESP_ROM_SPIFLASH_FASTRD_MODE, - ESP_ROM_SPIFLASH_SLOWRD_MODE -} esp_rom_spiflash_read_mode_t; - -typedef enum { - ESP_ROM_SPIFLASH_RESULT_OK, - ESP_ROM_SPIFLASH_RESULT_ERR, - ESP_ROM_SPIFLASH_RESULT_TIMEOUT -} esp_rom_spiflash_result_t; - -typedef struct { - uint32_t device_id; - uint32_t chip_size; // chip size in bytes - uint32_t block_size; - uint32_t sector_size; - uint32_t page_size; - uint32_t status_mask; -} esp_rom_spiflash_chip_t; - -typedef struct { - uint8_t data_length; - uint8_t read_cmd0; - uint8_t read_cmd1; - uint8_t write_cmd; - uint16_t data_mask; - uint16_t data; -} esp_rom_spiflash_common_cmd_t; - -/** - * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. - * Please do not call this function in SDK. - * - * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). - * - * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. - * - * @return None - */ -void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); - -/** - * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); - -/** - * @brief Set SPI Flash pad drivers. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid - * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. - * Values usually read from falsh by rom code, function usually callde by rom code. - * if value with bit(3) set, the value is valid, bit[2:0] is the real value. - * - * @return None - */ -void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); - -/** - * @brief Select SPI Flash function for pads. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); - -/** - * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t legacy: In legacy mode, more SPI command is used in line. - * - * @return None - */ -void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); - -/** - * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t *status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); - -/** - * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t *status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); - -/** - * @brief Write status to Falsh status register. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t status_value : Value to . - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); - -/** - * @brief Use a command to Read Flash status register. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t*status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); - -/** - * @brief Config SPI Flash read mode when init. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. - * - * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : config error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); - -/** - * @brief Config SPI Flash clock divisor. - * Please do not call this function in SDK. - * - * @param uint8_t freqdiv: clock divisor. - * - * @param uint8_t spi: 0 for SPI0, 1 for SPI1. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : config error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); - -/** - * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. - * - * @return uint16_t 0 : do not send command any more. - * 1 : go to the next command. - * n > 1 : skip (n - 1) commands. - */ -uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); - -/** - * @brief Unlock SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); - -/** - * @brief SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Lock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Lock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Lock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_lock(void); - -/** - * @brief Update SPI Flash parameter. - * Please do not call this function in SDK. - * - * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. - * - * @param uint32_t chip_size : The Flash size. - * - * @param uint32_t block_size : The Flash block size. - * - * @param uint32_t sector_size : The Flash sector size. - * - * @param uint32_t page_size : The Flash page size. - * - * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, - uint32_t sector_size, uint32_t page_size, uint32_t status_mask); - -/** - * @brief Erase whole flash chip. - * Please do not call this function in SDK. - * - * @param None - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); - -/** - * @brief Erase a 64KB block of flash - * Uses SPI flash command D8H. - * Please do not call this function in SDK. - * - * @param uint32_t block_num : Which block to erase. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); - -/** - * @brief Erase a sector of flash. - * Uses SPI flash command 20H. - * Please do not call this function in SDK. - * - * @param uint32_t sector_num : Which sector to erase. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); - -/** - * @brief Erase some sectors. - * Please do not call this function in SDK. - * - * @param uint32_t start_addr : Start addr to erase, should be sector aligned. - * - * @param uint32_t area_len : Length to erase, should be sector aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); - -/** - * @brief Write Data to Flash, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. - * - * @param const uint32_t *src : The pointer to data which is to write. - * - * @param uint32_t len : Length to write, should be 4 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); - -/** - * @brief Read Data from Flash, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. - * - * @param uint32_t *dest : The buf to read the data. - * - * @param uint32_t len : Length to read, should be 4 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); - -/** - * @brief SPI1 go into encrypto mode. - * Please do not call this function in SDK. - * - * @param None - * - * @return None - */ -void esp_rom_spiflash_write_encrypted_enable(void); - -/** - * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. - * - * @param uint32_t *data : The pointer to data which is to write. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); - -/** - * @brief SPI1 go out of encrypto mode. - * Please do not call this function in SDK. - * - * @param None - * - * @return None - */ -void esp_rom_spiflash_write_encrypted_disable(void); - -/** - * @brief Write data to flash with transparent encryption. - * @note Sectors to be written should already be erased. - * - * @note Please do not call this function in SDK. - * - * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. - * - * @param uint32_t *data : The pointer to data to write. Note, this pointer must - * be 32 bit aligned and the content of the data will be - * modified by the encryption function. - * - * @param uint32_t len : Length to write, should be 32 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. - * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); - - -/* TODO: figure out how to map these to their new names */ -typedef enum { - SPI_ENCRYPT_DESTINATION_FLASH, -} SpiEncryptDest; - -typedef esp_rom_spiflash_result_t SpiFlashOpResult; - -SpiFlashOpResult SPI_Encrypt_Write(uint32_t flash_addr, const void *data, uint32_t len); -SpiFlashOpResult SPI_Encrypt_Write_Dest(SpiEncryptDest dest, uint32_t flash_addr, const void *data, uint32_t len); -void SPI_Write_Encrypt_Enable(void); -void SPI_Write_Encrypt_Disable(void); - -/** @brief Wait until SPI flash write operation is complete - * - * @note Please do not call this function in SDK. - * - * Reads the Write In Progress bit of the SPI flash status register, - * repeats until this bit is zero (indicating write complete). - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete - * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); - - -/** @brief Enable Quad I/O pin functions - * - * @note Please do not call this function in SDK. - * - * Sets the HD & WP pin functions for Quad I/O modes, based on the - * efuse SPI pin configuration. - * - * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. - * - * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). - * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. - * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. - * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used - * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). - * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. - */ -void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); - + SPI_FLASH_RESULT_OK, + SPI_FLASH_RESULT_ERR, + SPI_FLASH_RESULT_TIMEOUT +} SpiFlashOpResult; typedef void (* spi_flash_func_t)(void); typedef SpiFlashOpResult (* spi_flash_op_t)(void); @@ -537,27 +101,6 @@ typedef struct { extern const spiflash_legacy_funcs_t *rom_spiflash_legacy_funcs; -/** @brief Global ROM spiflash data, as used by legacy - SPI flash functions -*/ -typedef struct { - esp_rom_spiflash_chip_t chip; - uint8_t dummy_len_plus[3]; - uint8_t sig_matrix; -} spiflash_legacy_data_t; - -extern spiflash_legacy_data_t *rom_spiflash_legacy_data; - -/* Defines to make the H2 ROM legacvy data access compatible with previous chips */ -#define g_rom_flashchip (rom_spiflash_legacy_data->chip) -#define g_rom_spiflash_dummy_len_plus (rom_spiflash_legacy_data->dummy_len_plus) - -/** - * @brief Clear WEL bit unconditionally. - * - * @return always ESP_ROM_SPIFLASH_RESULT_OK - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); /** * @} diff --git a/components/esp_rom/include/esp32s2/rom/opi_flash.h b/components/esp_rom/include/esp32s2/rom/opi_flash.h index bb209f67f0..9aa0b97c23 100644 --- a/components/esp_rom/include/esp32s2/rom/opi_flash.h +++ b/components/esp_rom/include/esp32s2/rom/opi_flash.h @@ -9,6 +9,7 @@ #include #include #include "spi_flash.h" +#include "esp_rom_spiflash.h" #ifdef __cplusplus extern "C" { diff --git a/components/esp_rom/include/esp32s2/rom/spi_flash.h b/components/esp_rom/include/esp32s2/rom/spi_flash.h index beb2fcdf30..c34e95cb90 100644 --- a/components/esp_rom/include/esp32s2/rom/spi_flash.h +++ b/components/esp_rom/include/esp32s2/rom/spi_flash.h @@ -1,16 +1,8 @@ -// Copyright 2010-2016 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. +/* + * SPDX-FileCopyrightText: 2010-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef _ROM_SPI_FLASH_H_ #define _ROM_SPI_FLASH_H_ @@ -29,14 +21,6 @@ extern "C" { #endif -/** \defgroup spi_flash_apis, spi flash operation related apis - * @brief spi_flash apis - */ - -/** @addtogroup spi_flash_apis - * @{ - */ - /************************************************************* * Note ************************************************************* @@ -123,447 +107,10 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 typedef enum { - ESP_ROM_SPIFLASH_QIO_MODE = 0, - ESP_ROM_SPIFLASH_QOUT_MODE, - ESP_ROM_SPIFLASH_DIO_MODE, - ESP_ROM_SPIFLASH_DOUT_MODE, - ESP_ROM_SPIFLASH_FASTRD_MODE, - ESP_ROM_SPIFLASH_SLOWRD_MODE, - ESP_ROM_SPIFLASH_OPI_STR_MODE, - ESP_ROM_SPIFLASH_OPI_DTR_MODE, - ESP_ROM_SPIFLASH_OOUT_MODE, - ESP_ROM_SPIFLASH_OIO_STR_MODE, - ESP_ROM_SPIFLASH_OIO_DTR_MODE, -} esp_rom_spiflash_read_mode_t; - -typedef enum { - ESP_ROM_SPIFLASH_RESULT_OK, - ESP_ROM_SPIFLASH_RESULT_ERR, - ESP_ROM_SPIFLASH_RESULT_TIMEOUT -} esp_rom_spiflash_result_t; - -typedef struct { - uint32_t device_id; - uint32_t chip_size; // chip size in bytes - uint32_t block_size; - uint32_t sector_size; - uint32_t page_size; - uint32_t status_mask; -} esp_rom_spiflash_chip_t; - -typedef struct { - uint8_t data_length; - uint8_t read_cmd0; - uint8_t read_cmd1; - uint8_t write_cmd; - uint16_t data_mask; - uint16_t data; -} esp_rom_spiflash_common_cmd_t; - -/** - * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. - * Please do not call this function in SDK. - * - * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). - * - * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. - * - * @return None - */ -void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); - -/** - * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); - -/** - * @brief Set SPI Flash pad drivers. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid - * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. - * Values usually read from falsh by rom code, function usually callde by rom code. - * if value with bit(3) set, the value is valid, bit[2:0] is the real value. - * - * @return None - */ -void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); - -/** - * @brief Select SPI Flash function for pads. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); - -/** - * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t legacy: In legacy mode, more SPI command is used in line. - * - * @return None - */ -void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); - -/** - * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t *status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); - -/** - * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t *status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); - -/** - * @brief Write status to Falsh status register. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t status_value : Value to . - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); - -/** - * @brief Use a command to Read Flash status register. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t*status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); - -/** - * @brief Config SPI Flash read mode when init. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. - * - * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : config error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); - -/** - * @brief Config SPI Flash clock divisor. - * Please do not call this function in SDK. - * - * @param uint8_t freqdiv: clock divisor. - * - * @param uint8_t spi: 0 for SPI0, 1 for SPI1. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : config error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); - -/** - * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. - * - * @return uint16_t 0 : do not send command any more. - * 1 : go to the next command. - * n > 1 : skip (n - 1) commands. - */ -uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); - -/** - * @brief Unlock SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); - -/** - * @brief SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Lock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Lock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Lock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_lock(void); - -/** - * @brief Update SPI Flash parameter. - * Please do not call this function in SDK. - * - * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. - * - * @param uint32_t chip_size : The Flash size. - * - * @param uint32_t block_size : The Flash block size. - * - * @param uint32_t sector_size : The Flash sector size. - * - * @param uint32_t page_size : The Flash page size. - * - * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, - uint32_t sector_size, uint32_t page_size, uint32_t status_mask); - -/** - * @brief Erase whole flash chip. - * Please do not call this function in SDK. - * - * @param None - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); - -/** - * @brief Erase a 64KB block of flash - * Uses SPI flash command D8H. - * Please do not call this function in SDK. - * - * @param uint32_t block_num : Which block to erase. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); - -/** - * @brief Erase a sector of flash. - * Uses SPI flash command 20H. - * Please do not call this function in SDK. - * - * @param uint32_t sector_num : Which sector to erase. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); - -/** - * @brief Erase some sectors. - * Please do not call this function in SDK. - * - * @param uint32_t start_addr : Start addr to erase, should be sector aligned. - * - * @param uint32_t area_len : Length to erase, should be sector aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); - -/** - * @brief Write Data to Flash, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. - * - * @param const uint32_t *src : The pointer to data which is to write. - * - * @param uint32_t len : Length to write, should be 4 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); - -/** - * @brief Read Data from Flash, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. - * - * @param uint32_t *dest : The buf to read the data. - * - * @param uint32_t len : Length to read, should be 4 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); - -/** - * @brief SPI1 go into encrypto mode. - * Please do not call this function in SDK. - * - * @param None - * - * @return None - */ -void esp_rom_spiflash_write_encrypted_enable(void); - -/** - * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. - * - * @param uint32_t *data : The pointer to data which is to write. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); - -/** - * @brief SPI1 go out of encrypto mode. - * Please do not call this function in SDK. - * - * @param None - * - * @return None - */ -void esp_rom_spiflash_write_encrypted_disable(void); - -/** - * @brief Write data to flash with transparent encryption. - * @note Sectors to be written should already be erased. - * - * @note Please do not call this function in SDK. - * - * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. - * - * @param uint32_t *data : The pointer to data to write. Note, this pointer must - * be 32 bit aligned and the content of the data will be - * modified by the encryption function. - * - * @param uint32_t len : Length to write, should be 32 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. - * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); - - -/* TODO: figure out how to map these to their new names */ -typedef enum { - SPI_ENCRYPT_DESTINATION_FLASH, - SPI_ENCRYPT_DESTINATION_PSRAM, -} SpiEncryptDest; - -typedef esp_rom_spiflash_result_t SpiFlashOpResult; - -SpiFlashOpResult SPI_Encrypt_Write(uint32_t flash_addr, const void* data, uint32_t len); -SpiFlashOpResult SPI_Encrypt_Write_Dest(SpiEncryptDest dest, uint32_t flash_addr, const void* data, uint32_t len); -void SPI_Write_Encrypt_Enable(void); -void SPI_Write_Encrypt_Disable(void); - -/** @brief Wait until SPI flash write operation is complete - * - * @note Please do not call this function in SDK. - * - * Reads the Write In Progress bit of the SPI flash status register, - * repeats until this bit is zero (indicating write complete). - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete - * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); - - -/** @brief Enable Quad I/O pin functions - * - * @note Please do not call this function in SDK. - * - * Sets the HD & WP pin functions for Quad I/O modes, based on the - * efuse SPI pin configuration. - * - * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. - * - * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). - * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. - * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. - * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used - * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). - * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. - */ -void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); - -/** - * @brief Clear WEL bit unconditionally. - * - * @return always ESP_ROM_SPIFLASH_RESULT_OK - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); - -/** @brief Global esp_rom_spiflash_chip_t structure used by ROM functions - * - */ -extern esp_rom_spiflash_chip_t g_rom_flashchip; + SPI_FLASH_RESULT_OK, + SPI_FLASH_RESULT_ERR, + SPI_FLASH_RESULT_TIMEOUT +} SpiFlashOpResult; extern uint8_t g_rom_spiflash_dummy_len_plus[]; diff --git a/components/esp_rom/include/esp32s3/rom/opi_flash.h b/components/esp_rom/include/esp32s3/rom/opi_flash.h index 8976e7b53e..539314eb03 100644 --- a/components/esp_rom/include/esp32s3/rom/opi_flash.h +++ b/components/esp_rom/include/esp32s3/rom/opi_flash.h @@ -10,6 +10,7 @@ #include #include #include "spi_flash.h" +#include "esp_rom_spiflash.h" #ifdef __cplusplus extern "C" { diff --git a/components/esp_rom/include/esp32s3/rom/spi_flash.h b/components/esp_rom/include/esp32s3/rom/spi_flash.h index 293306c9b9..a070f450c1 100644 --- a/components/esp_rom/include/esp32s3/rom/spi_flash.h +++ b/components/esp_rom/include/esp32s3/rom/spi_flash.h @@ -1,16 +1,8 @@ -// Copyright 2010-2020 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. +/* + * SPDX-FileCopyrightText: 2010-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once #include @@ -21,14 +13,6 @@ extern "C" { #endif -/** \defgroup spi_flash_apis, spi flash operation related apis - * @brief spi_flash apis - */ - -/** @addtogroup spi_flash_apis - * @{ - */ - /************************************************************* * Note ************************************************************* @@ -114,52 +98,21 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 -typedef enum { - ESP_ROM_SPIFLASH_QIO_MODE = 0, - ESP_ROM_SPIFLASH_QOUT_MODE, - ESP_ROM_SPIFLASH_DIO_MODE, - ESP_ROM_SPIFLASH_DOUT_MODE, - ESP_ROM_SPIFLASH_FASTRD_MODE, - ESP_ROM_SPIFLASH_SLOWRD_MODE, - ESP_ROM_SPIFLASH_OPI_STR_MODE, - ESP_ROM_SPIFLASH_OPI_DTR_MODE, - ESP_ROM_SPIFLASH_OOUT_MODE, - ESP_ROM_SPIFLASH_OIO_STR_MODE, - ESP_ROM_SPIFLASH_OIO_DTR_MODE, -} esp_rom_spiflash_read_mode_t; typedef enum { - ESP_ROM_SPIFLASH_RESULT_OK, - ESP_ROM_SPIFLASH_RESULT_ERR, - ESP_ROM_SPIFLASH_RESULT_TIMEOUT -} esp_rom_spiflash_result_t; - -typedef struct { - uint32_t device_id; - uint32_t chip_size; // chip size in bytes - uint32_t block_size; - uint32_t sector_size; - uint32_t page_size; - uint32_t status_mask; -} esp_rom_spiflash_chip_t; - -typedef struct { - uint8_t data_length; - uint8_t read_cmd0; - uint8_t read_cmd1; - uint8_t write_cmd; - uint16_t data_mask; - uint16_t data; -} esp_rom_spiflash_common_cmd_t; + SPI_FLASH_RESULT_OK, + SPI_FLASH_RESULT_ERR, + SPI_FLASH_RESULT_TIMEOUT +} SpiFlashOpResult; typedef void (*spi_flash_func_t)(void); -typedef esp_rom_spiflash_result_t (*spi_flash_op_t)(void); -typedef esp_rom_spiflash_result_t (*spi_flash_erase_t)(uint32_t); -typedef esp_rom_spiflash_result_t (*spi_flash_rd_t)(uint32_t, void*, int); -typedef esp_rom_spiflash_result_t (*spi_flash_wr_t)(uint32_t, const uint32_t*, int); -typedef esp_rom_spiflash_result_t (*spi_flash_ewr_t)(uint32_t, const void*, uint32_t); -typedef esp_rom_spiflash_result_t (*spi_flash_wren_t)(void*); -typedef esp_rom_spiflash_result_t (* spi_flash_erase_area_t)(uint32_t, uint32_t); +typedef SpiFlashOpResult (*spi_flash_op_t)(void); +typedef SpiFlashOpResult (*spi_flash_erase_t)(uint32_t); +typedef SpiFlashOpResult (*spi_flash_rd_t)(uint32_t, void*, int); +typedef SpiFlashOpResult (*spi_flash_wr_t)(uint32_t, const uint32_t*, int); +typedef SpiFlashOpResult (*spi_flash_ewr_t)(uint32_t, const void*, uint32_t); +typedef SpiFlashOpResult (*spi_flash_wren_t)(void*); +typedef SpiFlashOpResult (* spi_flash_erase_area_t)(uint32_t, uint32_t); typedef struct { uint8_t pp_addr_bit_len; @@ -180,417 +133,6 @@ typedef struct { spi_flash_erase_area_t erase_area; } spiflash_legacy_funcs_t; - -/** - * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. - * Please do not call this function in SDK. - * - * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). - * - * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. - * - * @return None - */ -void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); - -/** - * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); - -/** - * @brief Set SPI Flash pad drivers. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid - * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. - * Values usually read from falsh by rom code, function usually callde by rom code. - * if value with bit(3) set, the value is valid, bit[2:0] is the real value. - * - * @return None - */ -void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); - -/** - * @brief Select SPI Flash function for pads. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); - -/** - * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t legacy: In legacy mode, more SPI command is used in line. - * - * @return None - */ -void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); - -/** - * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t *status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); - -/** - * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t *status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); - -/** - * @brief Write status to Falsh status register. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t status_value : Value to . - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); - -/** - * @brief Use a command to Read Flash status register. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t*status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); - -/** - * @brief Config SPI Flash read mode when init. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. - * - * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : config error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); - -/** - * @brief Config SPI Flash clock divisor. - * Please do not call this function in SDK. - * - * @param uint8_t freqdiv: clock divisor. - * - * @param uint8_t spi: 0 for SPI0, 1 for SPI1. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : config error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); - -/** - * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. - * - * @return uint16_t 0 : do not send command any more. - * 1 : go to the next command. - * n > 1 : skip (n - 1) commands. - */ -uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); - -/** - * @brief Unlock SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); - -/** - * @brief SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Lock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Lock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Lock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_lock(void); - -/** - * @brief Update SPI Flash parameter. - * Please do not call this function in SDK. - * - * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. - * - * @param uint32_t chip_size : The Flash size. - * - * @param uint32_t block_size : The Flash block size. - * - * @param uint32_t sector_size : The Flash sector size. - * - * @param uint32_t page_size : The Flash page size. - * - * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, - uint32_t sector_size, uint32_t page_size, uint32_t status_mask); - -/** - * @brief Erase whole flash chip. - * Please do not call this function in SDK. - * - * @param None - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); - -/** - * @brief Erase a 64KB block of flash - * Uses SPI flash command D8H. - * Please do not call this function in SDK. - * - * @param uint32_t block_num : Which block to erase. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); - -/** - * @brief Erase a sector of flash. - * Uses SPI flash command 20H. - * Please do not call this function in SDK. - * - * @param uint32_t sector_num : Which sector to erase. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); - -/** - * @brief Erase some sectors. - * Please do not call this function in SDK. - * - * @param uint32_t start_addr : Start addr to erase, should be sector aligned. - * - * @param uint32_t area_len : Length to erase, should be sector aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); - -/** - * @brief Write Data to Flash, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. - * - * @param const uint32_t *src : The pointer to data which is to write. - * - * @param uint32_t len : Length to write, should be 4 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); - -/** - * @brief Read Data from Flash, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. - * - * @param uint32_t *dest : The buf to read the data. - * - * @param uint32_t len : Length to read, should be 4 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); - -/** - * @brief SPI1 go into encrypto mode. - * Please do not call this function in SDK. - * - * @param None - * - * @return None - */ -void esp_rom_spiflash_write_encrypted_enable(void); - -/** - * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. - * - * @param uint32_t *data : The pointer to data which is to write. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); - -/** - * @brief SPI1 go out of encrypto mode. - * Please do not call this function in SDK. - * - * @param None - * - * @return None - */ -void esp_rom_spiflash_write_encrypted_disable(void); - -/** - * @brief Write data to flash with transparent encryption. - * @note Sectors to be written should already be erased. - * - * @note Please do not call this function in SDK. - * - * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. - * - * @param uint32_t *data : The pointer to data to write. Note, this pointer must - * be 32 bit aligned and the content of the data will be - * modified by the encryption function. - * - * @param uint32_t len : Length to write, should be 32 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. - * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); - - -/* TODO: figure out how to map these to their new names */ -typedef enum { - SPI_ENCRYPT_DESTINATION_FLASH, - SPI_ENCRYPT_DESTINATION_PSRAM, -} SpiEncryptDest; - -typedef esp_rom_spiflash_result_t SpiFlashOpResult; - -SpiFlashOpResult SPI_Encrypt_Write(uint32_t flash_addr, const void *data, uint32_t len); -SpiFlashOpResult SPI_Encrypt_Write_Dest(SpiEncryptDest dest, uint32_t flash_addr, const void *data, uint32_t len); -void SPI_Write_Encrypt_Enable(void); -void SPI_Write_Encrypt_Disable(void); - -/** @brief Wait until SPI flash write operation is complete - * - * @note Please do not call this function in SDK. - * - * Reads the Write In Progress bit of the SPI flash status register, - * repeats until this bit is zero (indicating write complete). - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete - * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); - - -/** @brief Enable Quad I/O pin functions - * - * @note Please do not call this function in SDK. - * - * Sets the HD & WP pin functions for Quad I/O modes, based on the - * efuse SPI pin configuration. - * - * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. - * - * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). - * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. - * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. - * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used - * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). - * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. - */ -void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); - -/** - * @brief Clear WEL bit unconditionally. - * - * @return always ESP_ROM_SPIFLASH_RESULT_OK - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); -typedef struct { - esp_rom_spiflash_chip_t chip; - uint8_t dummy_len_plus[3]; - uint8_t sig_matrix; -} spiflash_legacy_data_t; - -extern spiflash_legacy_data_t *rom_spiflash_legacy_data; - -#define g_rom_flashchip (rom_spiflash_legacy_data->chip) -#define g_rom_spiflash_dummy_len_plus (rom_spiflash_legacy_data->dummy_len_plus) - #ifdef __cplusplus } #endif diff --git a/components/esp_rom/include/esp_rom_spiflash.h b/components/esp_rom/include/esp_rom_spiflash.h new file mode 100644 index 0000000000..c4646a7472 --- /dev/null +++ b/components/esp_rom/include/esp_rom_spiflash.h @@ -0,0 +1,468 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +/** \defgroup spi_flash_apis, spi flash operation related apis + * @brief spi_flash apis + */ + +/** @addtogroup spi_flash_apis + * @{ + */ + +typedef enum { + ESP_ROM_SPIFLASH_QIO_MODE = 0, + ESP_ROM_SPIFLASH_QOUT_MODE, + ESP_ROM_SPIFLASH_DIO_MODE, + ESP_ROM_SPIFLASH_DOUT_MODE, + ESP_ROM_SPIFLASH_FASTRD_MODE, + ESP_ROM_SPIFLASH_SLOWRD_MODE, + ESP_ROM_SPIFLASH_OPI_STR_MODE, + ESP_ROM_SPIFLASH_OPI_DTR_MODE, + ESP_ROM_SPIFLASH_OOUT_MODE, + ESP_ROM_SPIFLASH_OIO_STR_MODE, + ESP_ROM_SPIFLASH_OIO_DTR_MODE, +} esp_rom_spiflash_read_mode_t; + +typedef enum { + ESP_ROM_SPIFLASH_RESULT_OK, + ESP_ROM_SPIFLASH_RESULT_ERR, + ESP_ROM_SPIFLASH_RESULT_TIMEOUT +} esp_rom_spiflash_result_t; + +typedef struct { + uint32_t device_id; + uint32_t chip_size; // chip size in bytes + uint32_t block_size; + uint32_t sector_size; + uint32_t page_size; + uint32_t status_mask; +} esp_rom_spiflash_chip_t; + +typedef struct { + uint8_t data_length; + uint8_t read_cmd0; + uint8_t read_cmd1; + uint8_t write_cmd; + uint16_t data_mask; + uint16_t data; +} esp_rom_spiflash_common_cmd_t; + +/** + * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. + * Please do not call this function in SDK. + * + * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). + * + * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. + * + * @return None + */ +void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); + +/** + * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None + */ +void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); + +/** + * @brief Set SPI Flash pad drivers. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid + * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. + * Values usually read from falsh by rom code, function usually callde by rom code. + * if value with bit(3) set, the value is valid, bit[2:0] is the real value. + * + * @return None + */ +void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); + +/** + * @brief Select SPI Flash function for pads. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None + */ +void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); + +/** + * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t legacy: In legacy mode, more SPI command is used in line. + * + * @return None + */ +void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); + +/** + * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief Write status to Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t status_value : Value to . + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); + +/** + * @brief Use a command to Read Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t*status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); + +/** + * @brief Config SPI Flash read mode when init. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. + * + * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); + +/** + * @brief Config SPI Flash clock divisor. + * Please do not call this function in SDK. + * + * @param uint8_t freqdiv: clock divisor. + * + * @param uint8_t spi: 0 for SPI0, 1 for SPI1. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); + +/** + * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. + * + * @return uint16_t 0 : do not send command any more. + * 1 : go to the next command. + * n > 1 : skip (n - 1) commands. + */ +uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); + +/** + * @brief Unlock SPI write protect. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); + +/** + * @brief SPI write protect. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Lock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Lock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Lock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_lock(void); + +/** + * @brief Update SPI Flash parameter. + * Please do not call this function in SDK. + * + * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. + * + * @param uint32_t chip_size : The Flash size. + * + * @param uint32_t block_size : The Flash block size. + * + * @param uint32_t sector_size : The Flash sector size. + * + * @param uint32_t page_size : The Flash page size. + * + * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, + uint32_t sector_size, uint32_t page_size, uint32_t status_mask); + +/** + * @brief Erase whole flash chip. + * Please do not call this function in SDK. + * + * @param None + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); + +/** + * @brief Erase a 64KB block of flash + * Uses SPI flash command D8H. + * Please do not call this function in SDK. + * + * @param uint32_t block_num : Which block to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); + +/** + * @brief Erase a sector of flash. + * Uses SPI flash command 20H. + * Please do not call this function in SDK. + * + * @param uint32_t sector_num : Which sector to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); + +/** + * @brief Erase some sectors. + * Please do not call this function in SDK. + * + * @param uint32_t start_addr : Start addr to erase, should be sector aligned. + * + * @param uint32_t area_len : Length to erase, should be sector aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); + +/** + * @brief Write Data to Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. + * + * @param const uint32_t *src : The pointer to data which is to write. + * + * @param uint32_t len : Length to write, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); + +/** + * @brief Read Data from Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. + * + * @param uint32_t *dest : The buf to read the data. + * + * @param uint32_t len : Length to read, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); + +/** + * @brief SPI1 go into encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_enable(void); + +/** + * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. + * + * @param uint32_t *data : The pointer to data which is to write. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); + +/** + * @brief SPI1 go out of encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_disable(void); + +/** + * @brief Write data to flash with transparent encryption. + * @note Sectors to be written should already be erased. + * + * @note Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. + * + * @param uint32_t *data : The pointer to data to write. Note, this pointer must + * be 32 bit aligned and the content of the data will be + * modified by the encryption function. + * + * @param uint32_t len : Length to write, should be 32 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. + * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); + + +/** @brief Wait until SPI flash write operation is complete + * + * @note Please do not call this function in SDK. + * + * Reads the Write In Progress bit of the SPI flash status register, + * repeats until this bit is zero (indicating write complete). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete + * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); + + +/** @brief Enable Quad I/O pin functions + * + * @note Please do not call this function in SDK. + * + * Sets the HD & WP pin functions for Quad I/O modes, based on the + * efuse SPI pin configuration. + * + * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. + * + * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). + * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. + * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. + * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used + * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). + * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. + */ +void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); + +/** + * @brief Clear WEL bit unconditionally. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); + +typedef struct { + esp_rom_spiflash_chip_t chip; + uint8_t dummy_len_plus[3]; + uint8_t sig_matrix; +} spiflash_legacy_data_t; + + +/* Flash data defined in ROM*/ +#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 +extern esp_rom_spiflash_chip_t g_rom_flashchip; +#else +extern spiflash_legacy_data_t *rom_spiflash_legacy_data; +#define g_rom_flashchip (rom_spiflash_legacy_data->chip) +#define g_rom_spiflash_dummy_len_plus (rom_spiflash_legacy_data->dummy_len_plus) +#endif + +#ifdef __cplusplus +} +#endif diff --git a/components/esp_system/port/cpu_start.c b/components/esp_system/port/cpu_start.c index c58fee6570..eae13483b4 100644 --- a/components/esp_system/port/cpu_start.c +++ b/components/esp_system/port/cpu_start.c @@ -85,19 +85,9 @@ #include "bootloader_mem.h" #if CONFIG_APP_BUILD_TYPE_ELF_RAM -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" +#include "esp_rom_spiflash.h" #elif CONFIG_IDF_TARGET_ESP8684 #include "esp8684/rom/spi_flash.h" -#endif #endif // CONFIG_APP_BUILD_TYPE_ELF_RAM // Set efuse ROM_LOG_MODE on first boot diff --git a/components/spi_flash/cache_utils.c b/components/spi_flash/cache_utils.c index db31f6ac41..13b9543695 100644 --- a/components/spi_flash/cache_utils.c +++ b/components/spi_flash/cache_utils.c @@ -14,25 +14,20 @@ #include #if CONFIG_IDF_TARGET_ESP32 #include "soc/dport_reg.h" -#include #include #elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" #include "esp32s2/rom/cache.h" #include "soc/extmem_reg.h" #include "soc/cache_memory.h" #elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/cache.h" #include "soc/extmem_reg.h" #include "soc/cache_memory.h" #elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" #include "esp32c3/rom/cache.h" #include "soc/extmem_reg.h" #include "soc/cache_memory.h" #elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" #include "esp32h2/rom/cache.h" #include "soc/extmem_reg.h" #include "soc/cache_memory.h" @@ -42,6 +37,7 @@ #include "soc/extmem_reg.h" #include "soc/cache_memory.h" #endif +#include "esp_rom_spiflash.h" #include #include "sdkconfig.h" #ifndef CONFIG_FREERTOS_UNICORE diff --git a/components/spi_flash/esp32/flash_ops_esp32.c b/components/spi_flash/esp32/flash_ops_esp32.c index da4a1622a4..2ae5b8fe54 100644 --- a/components/spi_flash/esp32/flash_ops_esp32.c +++ b/components/spi_flash/esp32/flash_ops_esp32.c @@ -1,20 +1,13 @@ -// Copyright 2018 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. +/* + * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include "esp_spi_flash.h" #include "esp32/rom/spi_flash.h" #include "esp32/rom/cache.h" +#include "esp_rom_spiflash.h" static inline void IRAM_ATTR spi_flash_guard_start(void) { diff --git a/components/spi_flash/esp32/spi_flash_rom_patch.c b/components/spi_flash/esp32/spi_flash_rom_patch.c index d25150d852..69738a505e 100644 --- a/components/spi_flash/esp32/spi_flash_rom_patch.c +++ b/components/spi_flash/esp32/spi_flash_rom_patch.c @@ -1,20 +1,13 @@ -// Copyright 2015-2016 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. +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include "sdkconfig.h" #include "esp32/rom/spi_flash.h" #include "soc/spi_periph.h" #include "spi_flash_defs.h" +#include "esp_rom_spiflash.h" #define SPI_IDX 1 diff --git a/components/spi_flash/esp32c3/flash_ops_esp32c3.c b/components/spi_flash/esp32c3/flash_ops_esp32c3.c index 039b2ce246..8765cfc6a9 100644 --- a/components/spi_flash/esp32c3/flash_ops_esp32c3.c +++ b/components/spi_flash/esp32c3/flash_ops_esp32c3.c @@ -1,16 +1,8 @@ -// Copyright 2020 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. +/* + * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include @@ -24,6 +16,7 @@ #include "esp_flash.h" #include "esp_log.h" #include "esp_attr.h" +#include "esp_rom_spiflash.h" static const char *TAG = "spiflash_c3"; diff --git a/components/spi_flash/esp32c3/spi_flash_rom_patch.c b/components/spi_flash/esp32c3/spi_flash_rom_patch.c index 7180f3883f..c272b3fb62 100644 --- a/components/spi_flash/esp32c3/spi_flash_rom_patch.c +++ b/components/spi_flash/esp32c3/spi_flash_rom_patch.c @@ -1,20 +1,13 @@ -// Copyright 2015-2020 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. +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include "sdkconfig.h" #include "esp32c3/rom/spi_flash.h" #include "soc/spi_periph.h" #include "spi_flash_defs.h" +#include "esp_rom_spiflash.h" #define SPI_IDX 1 diff --git a/components/spi_flash/esp32h2/flash_ops_esp32h2.c b/components/spi_flash/esp32h2/flash_ops_esp32h2.c index 10231bdfdd..e69733898e 100644 --- a/components/spi_flash/esp32h2/flash_ops_esp32h2.c +++ b/components/spi_flash/esp32h2/flash_ops_esp32h2.c @@ -1,16 +1,8 @@ -// Copyright 2020 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. +/* + * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include @@ -24,6 +16,7 @@ #include "esp_flash.h" #include "esp_log.h" #include "esp_attr.h" +#include "esp_rom_spiflash.h" static const char *TAG = "spiflash_h2"; diff --git a/components/spi_flash/esp32h2/spi_flash_rom_patch.c b/components/spi_flash/esp32h2/spi_flash_rom_patch.c index a133de307c..cc2dcd5a64 100644 --- a/components/spi_flash/esp32h2/spi_flash_rom_patch.c +++ b/components/spi_flash/esp32h2/spi_flash_rom_patch.c @@ -1,20 +1,13 @@ -// Copyright 2015-2020 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. +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include "sdkconfig.h" #include "esp32h2/rom/spi_flash.h" #include "soc/spi_periph.h" #include "spi_flash_defs.h" +#include "esp_rom_spiflash.h" #define SPI_IDX 1 diff --git a/components/spi_flash/esp32s2/flash_ops_esp32s2.c b/components/spi_flash/esp32s2/flash_ops_esp32s2.c index ccdf0636c5..cf80475194 100644 --- a/components/spi_flash/esp32s2/flash_ops_esp32s2.c +++ b/components/spi_flash/esp32s2/flash_ops_esp32s2.c @@ -1,16 +1,8 @@ -// Copyright 2018 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. +/* + * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include @@ -24,6 +16,7 @@ #include "hal/spi_flash_hal.h" #include "esp_flash.h" #include "esp_log.h" +#include "esp_rom_spiflash.h" static const char *TAG = "spiflash_s2"; @@ -71,7 +64,7 @@ esp_rom_spiflash_result_t IRAM_ATTR spi_flash_write_encrypted_chip(size_t dest_a ops->start(); } flash_rom_init(); - rc = SPI_Encrypt_Write(dest_addr, src, size); + rc = esp_rom_spiflash_write_encrypted(dest_addr, (uint32_t*)src, size); if (ops && ops->end) { ops->end(); } diff --git a/components/spi_flash/esp32s2/spi_flash_rom_patch.c b/components/spi_flash/esp32s2/spi_flash_rom_patch.c index 139a56bd2b..23ee08e56c 100644 --- a/components/spi_flash/esp32s2/spi_flash_rom_patch.c +++ b/components/spi_flash/esp32s2/spi_flash_rom_patch.c @@ -1,20 +1,13 @@ -// Copyright 2015-2020 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. +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include "sdkconfig.h" #include "esp32s2/rom/spi_flash.h" #include "soc/spi_periph.h" #include "spi_flash_defs.h" +#include "esp_rom_spiflash.h" #define SPI_IDX 1 diff --git a/components/spi_flash/esp32s3/flash_ops_esp32s3.c b/components/spi_flash/esp32s3/flash_ops_esp32s3.c index e8e8ed7e2c..f4891fbdbe 100644 --- a/components/spi_flash/esp32s3/flash_ops_esp32s3.c +++ b/components/spi_flash/esp32s3/flash_ops_esp32s3.c @@ -1,16 +1,8 @@ -// Copyright 2020 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. +/* + * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include @@ -24,6 +16,7 @@ #include "hal/spi_flash_hal.h" #include "esp_flash.h" #include "esp_log.h" +#include "esp_rom_spiflash.h" static const char *TAG = "spiflash_s3"; diff --git a/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c b/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c index a725528513..41682cd431 100644 --- a/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c +++ b/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c @@ -10,6 +10,7 @@ #include "esp_rom_gpio.h" #include "esp32s3/rom/gpio.h" #include "esp32s3/rom/spi_flash.h" +#include "esp_rom_spiflash.h" #include "esp32s3/rom/opi_flash.h" #include "esp_private/spi_flash_os.h" #include "opi_flash_private.h" diff --git a/components/spi_flash/esp32s3/spi_timing_config.h b/components/spi_flash/esp32s3/spi_timing_config.h index 06af783c9f..a8d064d491 100644 --- a/components/spi_flash/esp32s3/spi_timing_config.h +++ b/components/spi_flash/esp32s3/spi_timing_config.h @@ -8,6 +8,7 @@ #include "esp_flash_partitions.h" #include "esp32s3/rom/spi_flash.h" +#include "esp_rom_spiflash.h" #include "esp32s3/rom/opi_flash.h" #include "mspi_timing_tuning_configs.h" diff --git a/components/spi_flash/esp_flash_api.c b/components/spi_flash/esp_flash_api.c index 7b0719c277..ba13b7f132 100644 --- a/components/spi_flash/esp_flash_api.c +++ b/components/spi_flash/esp_flash_api.c @@ -16,22 +16,12 @@ #include "esp_flash_internal.h" #include "spi_flash_defs.h" #include "esp_rom_caps.h" +#include "esp_rom_spiflash.h" #if CONFIG_IDF_TARGET_ESP32S2 #include "esp_crypto_lock.h" // for locking flash encryption peripheral #endif //CONFIG_IDF_TARGET_ESP32S2 -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" #elif CONFIG_IDF_TARGET_ESP8684 #include "esp8684/rom/spi_flash.h" -#endif static const char TAG[] = "spi_flash"; diff --git a/components/spi_flash/esp_flash_spi_init.c b/components/spi_flash/esp_flash_spi_init.c index 36441c148e..22cd9fa04c 100644 --- a/components/spi_flash/esp_flash_spi_init.c +++ b/components/spi_flash/esp_flash_spi_init.c @@ -20,21 +20,8 @@ #include "esp_flash_internal.h" #include "esp_rom_gpio.h" #include "esp_private/spi_flash_os.h" -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32C3 +#include "esp_rom_spiflash.h" #include "esp32c3/rom/efuse.h" -#include "esp32c3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP8684 -#include "esp8684/rom/efuse.h" -#include "esp8684/rom/spi_flash.h" -#endif __attribute__((unused)) static const char TAG[] = "spi_flash"; diff --git a/components/spi_flash/flash_mmap.c b/components/spi_flash/flash_mmap.c index 4de236cc56..2315d5f91c 100644 --- a/components/spi_flash/flash_mmap.c +++ b/components/spi_flash/flash_mmap.c @@ -19,26 +19,24 @@ #include "esp_attr.h" #include "esp_spi_flash.h" #include "esp_flash_encrypt.h" +#include "esp_rom_spiflash.h" #include "esp_log.h" #include "cache_utils.h" #if CONFIG_IDF_TARGET_ESP32 #include "soc/dport_reg.h" #include "esp32/rom/cache.h" -#include "esp32/rom/spi_flash.h" #include "esp32/spiram.h" #include "soc/mmu.h" // TODO: IDF-3821 #define INVALID_PHY_PAGE 0xffff #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/rom/cache.h" -#include "esp32s2/rom/spi_flash.h" #include "esp32s2/spiram.h" #include "soc/extmem_reg.h" #include "soc/cache_memory.h" #include "soc/mmu.h" #elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/cache.h" #include "esp32s3/spiram.h" #include "soc/extmem_reg.h" @@ -46,12 +44,10 @@ #include "soc/mmu.h" #elif CONFIG_IDF_TARGET_ESP32C3 #include "esp32c3/rom/cache.h" -#include "esp32c3/rom/spi_flash.h" #include "soc/cache_memory.h" #include "soc/mmu.h" #elif CONFIG_IDF_TARGET_ESP32H2 #include "esp32h2/rom/cache.h" -#include "esp32h2/rom/spi_flash.h" #include "soc/cache_memory.h" #include "soc/mmu.h" #elif CONFIG_IDF_TARGET_ESP8684 diff --git a/components/spi_flash/flash_ops.c b/components/spi_flash/flash_ops.c index df77fb1b04..d6381a2054 100644 --- a/components/spi_flash/flash_ops.c +++ b/components/spi_flash/flash_ops.c @@ -27,23 +27,20 @@ #include "esp32/rom/spi_flash.h" #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/rom/cache.h" -#include "esp32s2/rom/spi_flash.h" #elif CONFIG_IDF_TARGET_ESP32S3 #include "soc/spi_mem_reg.h" -#include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/opi_flash.h" #include "esp32s3/rom/cache.h" #include "esp32s3/opi_flash_private.h" #elif CONFIG_IDF_TARGET_ESP32C3 #include "esp32c3/rom/cache.h" -#include "esp32c3/rom/spi_flash.h" #elif CONFIG_IDF_TARGET_ESP32H2 #include "esp32h2/rom/cache.h" -#include "esp32h2/rom/spi_flash.h" #elif CONFIG_IDF_TARGET_ESP8684 #include "esp8684/rom/cache.h" #include "esp8684/rom/spi_flash.h" #endif +#include "esp_rom_spiflash.h" #include "esp_flash_partitions.h" #include "cache_utils.h" #include "esp_flash.h" diff --git a/components/spi_flash/include/esp_private/spi_flash_os.h b/components/spi_flash/include/esp_private/spi_flash_os.h index e73511bc11..952a6ed544 100644 --- a/components/spi_flash/include/esp_private/spi_flash_os.h +++ b/components/spi_flash/include/esp_private/spi_flash_os.h @@ -19,19 +19,11 @@ */ #pragma once +#include "esp_rom_spiflash.h" #include #include #include "sdkconfig.h" #include "esp_err.h" -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#endif #include "esp_flash.h" #include "hal/spi_flash_hal.h" diff --git a/components/spi_flash/sim/SpiFlash.h b/components/spi_flash/sim/SpiFlash.h index 070d4621bf..89a3048001 100644 --- a/components/spi_flash/sim/SpiFlash.h +++ b/components/spi_flash/sim/SpiFlash.h @@ -1,16 +1,8 @@ -// Copyright 2015-2017 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. +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef _SpiFlash_H_ #define _SpiFlash_H_ @@ -19,6 +11,7 @@ #include "esp_err.h" #include "esp32/rom/spi_flash.h" +#include "esp_rom_spiflash.h" /** * @brief This class is used to emulate flash devices. diff --git a/components/spi_flash/sim/flash_mock.cpp b/components/spi_flash/sim/flash_mock.cpp index 03ddeceefc..90c19f3d0a 100644 --- a/components/spi_flash/sim/flash_mock.cpp +++ b/components/spi_flash/sim/flash_mock.cpp @@ -8,6 +8,7 @@ #include "esp_err.h" #include "esp32/rom/spi_flash.h" +#include "esp_rom_spiflash.h" SpiFlash spiflash = SpiFlash(); diff --git a/components/spi_flash/sim/flash_mock_util.c b/components/spi_flash/sim/flash_mock_util.c index 2db51c58ae..62c0146272 100644 --- a/components/spi_flash/sim/flash_mock_util.c +++ b/components/spi_flash/sim/flash_mock_util.c @@ -3,6 +3,7 @@ #include "esp_err.h" #include "esp32/rom/spi_flash.h" +#include "esp_rom_spiflash.h" bool spi_flash_check_and_flush_cache(size_t start_addr, size_t length) { diff --git a/components/spi_flash/test/test_large_flash_writes.c b/components/spi_flash/test/test_large_flash_writes.c index 11824cd11b..30701cd02d 100644 --- a/components/spi_flash/test/test_large_flash_writes.c +++ b/components/spi_flash/test/test_large_flash_writes.c @@ -1,16 +1,8 @@ -// Copyright 2010-2016 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. +/* + * SPDX-FileCopyrightText: 2010-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ // Test for spi_flash_write() with large buffers (in RAM or on flash) @@ -25,7 +17,7 @@ #include #include #include -#include +#include "esp_rom_spiflash.h" #include "../cache_utils.h" #include "soc/timer_periph.h" diff --git a/components/spi_flash/test/test_read_write.c b/components/spi_flash/test/test_read_write.c index 9608c7b56b..76f4201c1f 100644 --- a/components/spi_flash/test/test_read_write.c +++ b/components/spi_flash/test/test_read_write.c @@ -21,15 +21,8 @@ #if CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" #endif +#include "esp_rom_spiflash.h" #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP8684) // TODO: SPI_FLASH IDF-4025 diff --git a/components/spi_flash/test/test_spi_flash.c b/components/spi_flash/test/test_spi_flash.c index e9bf6c859b..036270e7e1 100644 --- a/components/spi_flash/test/test_spi_flash.c +++ b/components/spi_flash/test/test_spi_flash.c @@ -13,20 +13,12 @@ #include "ccomp_timer.h" #include "esp_log.h" #include "esp_rom_sys.h" +#include "esp_rom_spiflash.h" #include "esp_timer.h" #include "bootloader_flash.h" //for bootloader_flash_xmc_startup #include "sdkconfig.h" -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" -#endif #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP8684) diff --git a/components/spiffs/esp_spiffs.c b/components/spiffs/esp_spiffs.c index 32b831954c..6315f161e4 100644 --- a/components/spiffs/esp_spiffs.c +++ b/components/spiffs/esp_spiffs.c @@ -21,19 +21,9 @@ #include #include "esp_vfs.h" #include "esp_err.h" -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" +#include "esp_rom_spiflash.h" #elif CONFIG_IDF_TARGET_ESP8684 #include "esp8684/rom/spi_flash.h" -#endif #include "spiffs_api.h" diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 894b23cf4f..dddc010e84 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -623,7 +623,6 @@ components/esp_rom/include/esp32/rom/miniz.h components/esp_rom/include/esp32/rom/rsa_pss.h components/esp_rom/include/esp32/rom/rtc.h components/esp_rom/include/esp32/rom/sha.h -components/esp_rom/include/esp32/rom/spi_flash.h components/esp_rom/include/esp32/rom/tbconsole.h components/esp_rom/include/esp32/rom/tjpgd.h components/esp_rom/include/esp32/rom/uart.h @@ -646,7 +645,6 @@ components/esp_rom/include/esp32c3/rom/rom_layout.h components/esp_rom/include/esp32c3/rom/rsa_pss.h components/esp_rom/include/esp32c3/rom/rtc.h components/esp_rom/include/esp32c3/rom/sha.h -components/esp_rom/include/esp32c3/rom/spi_flash.h components/esp_rom/include/esp32c3/rom/tjpgd.h components/esp_rom/include/esp32c3/rom/uart.h components/esp_rom/include/esp32h2/rom/aes.h @@ -668,7 +666,6 @@ components/esp_rom/include/esp32h2/rom/rom_layout.h components/esp_rom/include/esp32h2/rom/rsa_pss.h components/esp_rom/include/esp32h2/rom/rtc.h components/esp_rom/include/esp32h2/rom/sha.h -components/esp_rom/include/esp32h2/rom/spi_flash.h components/esp_rom/include/esp32h2/rom/tjpgd.h components/esp_rom/include/esp32h2/rom/uart.h components/esp_rom/include/esp32s2/rom/aes.h @@ -688,7 +685,6 @@ components/esp_rom/include/esp32s2/rom/opi_flash.h components/esp_rom/include/esp32s2/rom/rsa_pss.h components/esp_rom/include/esp32s2/rom/rtc.h components/esp_rom/include/esp32s2/rom/sha.h -components/esp_rom/include/esp32s2/rom/spi_flash.h components/esp_rom/include/esp32s2/rom/uart.h components/esp_rom/include/esp32s2/rom/usb/cdc_acm.h components/esp_rom/include/esp32s2/rom/usb/chip_usb_dw_wrapper.h @@ -718,7 +714,6 @@ components/esp_rom/include/esp32s3/rom/opi_flash.h components/esp_rom/include/esp32s3/rom/rom_layout.h components/esp_rom/include/esp32s3/rom/rsa_pss.h components/esp_rom/include/esp32s3/rom/sha.h -components/esp_rom/include/esp32s3/rom/spi_flash.h components/esp_rom/include/esp32s3/rom/tjpgd.h components/esp_rom/include/esp32s3/rom/uart.h components/esp_rom/include/esp32s3/rom/usb/cdc_acm.h @@ -2047,15 +2042,6 @@ components/soc/include/soc/usb_periph.h components/soc/lldesc.c components/soc/soc_include_legacy_warn.c components/spi_flash/cache_utils.h -components/spi_flash/esp32/flash_ops_esp32.c -components/spi_flash/esp32/spi_flash_rom_patch.c -components/spi_flash/esp32c3/flash_ops_esp32c3.c -components/spi_flash/esp32c3/spi_flash_rom_patch.c -components/spi_flash/esp32h2/flash_ops_esp32h2.c -components/spi_flash/esp32h2/spi_flash_rom_patch.c -components/spi_flash/esp32s2/flash_ops_esp32s2.c -components/spi_flash/esp32s2/spi_flash_rom_patch.c -components/spi_flash/esp32s3/flash_ops_esp32s3.c components/spi_flash/include/esp_flash.h components/spi_flash/include/esp_flash_internal.h components/spi_flash/include/esp_flash_spi_init.h @@ -2073,7 +2059,6 @@ components/spi_flash/include/spi_flash_chip_mxic.h components/spi_flash/include/spi_flash_chip_winbond.h components/spi_flash/memspi_host_driver.c components/spi_flash/sim/SpiFlash.cpp -components/spi_flash/sim/SpiFlash.h components/spi_flash/sim/flash_mock.cpp components/spi_flash/sim/flash_mock_util.c components/spi_flash/sim/sdkconfig/sdkconfig.h @@ -2086,7 +2071,6 @@ components/spi_flash/spi_flash_chip_mxic_opi.c components/spi_flash/spi_flash_chip_winbond.c components/spi_flash/test/test_esp_flash.c components/spi_flash/test/test_flash_encryption.c -components/spi_flash/test/test_large_flash_writes.c components/spi_flash/test/test_mmap.c components/spi_flash/test/test_out_of_bounds_write.c components/spi_flash/test/test_partition_ext.c From 3a4db97cecfe43bc83659d53f23cc26d4706b57e Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Tue, 28 Sep 2021 19:35:36 +0800 Subject: [PATCH 2/5] spi_flash: move patch files to common rom patch folder --- .../subproject/main/ld/esp32/bootloader.ld | 1 + .../bootloader_support/src/bootloader_flash.c | 6 - .../src/bootloader_flash_config_esp32.c | 1 - .../src/bootloader_flash_config_esp32c3.c | 1 - .../src/bootloader_flash_config_esp32h2.c | 1 - .../src/bootloader_flash_config_esp32s2.c | 1 - .../src/bootloader_flash_config_esp32s3.c | 1 - .../src/bootloader_flash_config_esp8684.c | 2 +- .../src/bootloader_utility.c | 1 - .../src/esp8684/bootloader_esp8684.c | 3 +- .../esp_hw_support/port/esp32/spiram_psram.c | 1 - .../port/esp32s2/spiram_psram.c | 1 - .../port/esp32s3/opiram_psram.c | 1 - .../port/esp32s3/spiram_psram.c | 1 - components/esp_rom/CMakeLists.txt | 4 +- .../esp_rom/include/esp32/rom/spi_flash.h | 6 + .../esp_rom/include/esp32c3/rom/spi_flash.h | 20 +- .../esp_rom/include/esp32h2/rom/spi_flash.h | 20 +- .../esp_rom/include/esp32s2/rom/opi_flash.h | 1 - .../esp_rom/include/esp32s2/rom/spi_flash.h | 8 +- .../esp_rom/include/esp32s3/rom/opi_flash.h | 1 - .../esp_rom/include/esp32s3/rom/spi_flash.h | 22 +- .../esp_rom/include/esp8684/rom/spi_flash.h | 473 +----------------- components/esp_rom/include/esp_rom_spiflash.h | 21 +- components/esp_rom/linker.lf | 4 + .../patches/esp_rom_spiflash.c} | 72 +-- components/esp_system/port/cpu_start.c | 2 - components/spi_flash/CMakeLists.txt | 2 - components/spi_flash/cache_utils.c | 1 - components/spi_flash/esp32/flash_ops_esp32.c | 1 - .../spi_flash/esp32c3/flash_ops_esp32c3.c | 1 - .../spi_flash/esp32c3/spi_flash_rom_patch.c | 19 - .../spi_flash/esp32h2/flash_ops_esp32h2.c | 1 - .../spi_flash/esp32h2/spi_flash_rom_patch.c | 19 - .../spi_flash/esp32s2/flash_ops_esp32s2.c | 1 - .../spi_flash/esp32s2/spi_flash_rom_patch.c | 21 - .../spi_flash/esp32s3/flash_ops_esp32s3.c | 1 - .../esp32s3/spi_flash_oct_flash_init.c | 1 - .../spi_flash/esp32s3/spi_flash_rom_patch.c | 6 - .../spi_flash/esp32s3/spi_timing_config.h | 1 - .../spi_flash/esp8684/flash_ops_esp8684.c | 4 +- .../spi_flash/esp8684/spi_flash_rom_patch.c | 18 - components/spi_flash/esp_flash_api.c | 2 - components/spi_flash/esp_flash_spi_init.c | 1 - components/spi_flash/flash_mmap.c | 1 - components/spi_flash/flash_ops.c | 2 - components/spi_flash/linker.lf | 1 - components/spi_flash/sim/flash_mock_util.c | 1 - components/spi_flash/test/test_read_write.c | 4 - components/spiffs/esp_spiffs.c | 2 - 50 files changed, 116 insertions(+), 671 deletions(-) create mode 100644 components/esp_rom/linker.lf rename components/{spi_flash/esp32/spi_flash_rom_patch.c => esp_rom/patches/esp_rom_spiflash.c} (96%) delete mode 100644 components/spi_flash/esp32c3/spi_flash_rom_patch.c delete mode 100644 components/spi_flash/esp32h2/spi_flash_rom_patch.c delete mode 100644 components/spi_flash/esp32s2/spi_flash_rom_patch.c delete mode 100644 components/spi_flash/esp32s3/spi_flash_rom_patch.c delete mode 100644 components/spi_flash/esp8684/spi_flash_rom_patch.c diff --git a/components/bootloader/subproject/main/ld/esp32/bootloader.ld b/components/bootloader/subproject/main/ld/esp32/bootloader.ld index 30e5a79c45..6af138c9d9 100644 --- a/components/bootloader/subproject/main/ld/esp32/bootloader.ld +++ b/components/bootloader/subproject/main/ld/esp32/bootloader.ld @@ -63,6 +63,7 @@ SECTIONS *libesp_hw_support.a:rtc_clk.*(.literal .text .literal.* .text.*) *libesp_hw_support.a:rtc_time.*(.literal .text .literal.* .text.*) *libefuse.a:*.*(.literal .text .literal.* .text.*) + *libesp_rom.a:*.*(.literal .text .literal.* .text.*) *(.fini.literal) *(.fini) *(.gnu.version) diff --git a/components/bootloader_support/src/bootloader_flash.c b/components/bootloader_support/src/bootloader_flash.c index d038fc525d..10d010f2c9 100644 --- a/components/bootloader_support/src/bootloader_flash.c +++ b/components/bootloader_support/src/bootloader_flash.c @@ -115,26 +115,20 @@ esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size) #else //BOOTLOADER_BUILD /* Bootloader version, uses ROM functions only */ #if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" #include "esp32/rom/cache.h" #elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" #include "esp32s2/rom/cache.h" #include "soc/cache_memory.h" #elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/cache.h" #include "soc/cache_memory.h" #elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" #include "esp32c3/rom/cache.h" #include "soc/cache_memory.h" #elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" #include "esp32h2/rom/cache.h" #include "soc/cache_memory.h" #elif CONFIG_IDF_TARGET_ESP8684 -#include "esp8684/rom/spi_flash.h" #include "esp8684/rom/cache.h" #include "soc/cache_memory.h" #endif diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32.c b/components/bootloader_support/src/bootloader_flash_config_esp32.c index e7d12178fc..6b1546e9a2 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32.c +++ b/components/bootloader_support/src/bootloader_flash_config_esp32.c @@ -11,7 +11,6 @@ #include "esp_log.h" #include "esp_rom_gpio.h" #include "esp_rom_efuse.h" -#include "esp32/rom/spi_flash.h" #include "soc/gpio_periph.h" #include "soc/efuse_reg.h" #include "soc/spi_reg.h" diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32c3.c b/components/bootloader_support/src/bootloader_flash_config_esp32c3.c index afea291240..e1e753578c 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32c3.c +++ b/components/bootloader_support/src/bootloader_flash_config_esp32c3.c @@ -10,7 +10,6 @@ #include "esp_err.h" #include "esp_log.h" #include "esp32c3/rom/gpio.h" -#include "esp32c3/rom/spi_flash.h" #include "esp32c3/rom/efuse.h" #include "soc/gpio_periph.h" #include "soc/efuse_reg.h" diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32h2.c b/components/bootloader_support/src/bootloader_flash_config_esp32h2.c index 9fa9364b89..3851fe9bf7 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32h2.c +++ b/components/bootloader_support/src/bootloader_flash_config_esp32h2.c @@ -10,7 +10,6 @@ #include "esp_err.h" #include "esp_log.h" #include "esp32h2/rom/gpio.h" -#include "esp32h2/rom/spi_flash.h" #include "esp32h2/rom/efuse.h" #include "soc/gpio_periph.h" #include "soc/efuse_reg.h" diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32s2.c b/components/bootloader_support/src/bootloader_flash_config_esp32s2.c index 73bccd895f..35847db9a8 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32s2.c +++ b/components/bootloader_support/src/bootloader_flash_config_esp32s2.c @@ -9,7 +9,6 @@ #include "sdkconfig.h" #include "esp_err.h" #include "esp_log.h" -#include "esp32s2/rom/spi_flash.h" #include "soc/efuse_reg.h" #include "soc/spi_reg.h" #include "soc/spi_mem_reg.h" diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32s3.c b/components/bootloader_support/src/bootloader_flash_config_esp32s3.c index b8f37b0f82..4a36521540 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32s3.c +++ b/components/bootloader_support/src/bootloader_flash_config_esp32s3.c @@ -9,7 +9,6 @@ #include "sdkconfig.h" #include "esp_err.h" #include "esp_log.h" -#include "esp32s3/rom/spi_flash.h" #include "soc/efuse_reg.h" #include "soc/spi_reg.h" #include "soc/spi_mem_reg.h" diff --git a/components/bootloader_support/src/bootloader_flash_config_esp8684.c b/components/bootloader_support/src/bootloader_flash_config_esp8684.c index 27fbf9d5ee..bf4603b038 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp8684.c +++ b/components/bootloader_support/src/bootloader_flash_config_esp8684.c @@ -10,8 +10,8 @@ #include "esp_err.h" #include "esp_log.h" #include "esp8684/rom/gpio.h" -#include "esp8684/rom/spi_flash.h" #include "esp8684/rom/efuse.h" +#include "esp_rom_spiflash.h" #include "soc/gpio_periph.h" #include "soc/efuse_reg.h" #include "soc/spi_reg.h" diff --git a/components/bootloader_support/src/bootloader_utility.c b/components/bootloader_support/src/bootloader_utility.c index 5399756b58..f28cea76f7 100644 --- a/components/bootloader_support/src/bootloader_utility.c +++ b/components/bootloader_support/src/bootloader_utility.c @@ -52,7 +52,6 @@ #include "esp8684/rom/cache.h" #include "esp8684/rom/efuse.h" #include "esp8684/rom/ets_sys.h" -#include "esp8684/rom/spi_flash.h" #include "esp8684/rom/crc.h" #include "esp8684/rom/rtc.h" #include "esp8684/rom/uart.h" diff --git a/components/bootloader_support/src/esp8684/bootloader_esp8684.c b/components/bootloader_support/src/esp8684/bootloader_esp8684.c index de005872b0..ca7a38d901 100644 --- a/components/bootloader_support/src/esp8684/bootloader_esp8684.c +++ b/components/bootloader_support/src/esp8684/bootloader_esp8684.c @@ -13,6 +13,7 @@ #include "esp_rom_efuse.h" #include "esp_rom_uart.h" #include "esp_rom_sys.h" +#include "esp_rom_spiflash.h" #include "soc/efuse_reg.h" #include "soc/gpio_sig_map.h" #include "soc/io_mux_reg.h" @@ -24,10 +25,8 @@ #include "soc/io_mux_reg.h" #include "soc/system_reg.h" #include "esp8684/rom/efuse.h" -#include "esp8684/rom/spi_flash.h" #include "esp8684/rom/cache.h" #include "esp8684/rom/ets_sys.h" -#include "esp8684/rom/spi_flash.h" #include "esp8684/rom/rtc.h" #include "bootloader_common.h" #include "bootloader_init.h" diff --git a/components/esp_hw_support/port/esp32/spiram_psram.c b/components/esp_hw_support/port/esp32/spiram_psram.c index 817f7fba3d..8865ea693c 100644 --- a/components/esp_hw_support/port/esp32/spiram_psram.c +++ b/components/esp_hw_support/port/esp32/spiram_psram.c @@ -17,7 +17,6 @@ #include "esp_log.h" #include "esp_efuse.h" #include "spiram_psram.h" -#include "esp32/rom/spi_flash.h" #include "esp32/rom/cache.h" #include "esp32/rom/efuse.h" #include "esp_rom_efuse.h" diff --git a/components/esp_hw_support/port/esp32s2/spiram_psram.c b/components/esp_hw_support/port/esp32s2/spiram_psram.c index 2c0acebc85..2a59d2e844 100644 --- a/components/esp_hw_support/port/esp32s2/spiram_psram.c +++ b/components/esp_hw_support/port/esp32s2/spiram_psram.c @@ -16,7 +16,6 @@ #include "esp_types.h" #include "esp_log.h" #include "spiram_psram.h" -#include "esp32s2/rom/spi_flash.h" #include "esp32s2/rom/opi_flash.h" #include "esp32s2/rom/cache.h" #include "esp32s2/rom/efuse.h" diff --git a/components/esp_hw_support/port/esp32s3/opiram_psram.c b/components/esp_hw_support/port/esp32s3/opiram_psram.c index d29d564a36..6d75d6be8d 100644 --- a/components/esp_hw_support/port/esp32s3/opiram_psram.c +++ b/components/esp_hw_support/port/esp32s3/opiram_psram.c @@ -12,7 +12,6 @@ #include "esp_log.h" #include "spiram_psram.h" #include "esp32s3/rom/ets_sys.h" -#include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/opi_flash.h" #include "esp32s3/rom/gpio.h" #include "esp32s3/rom/cache.h" diff --git a/components/esp_hw_support/port/esp32s3/spiram_psram.c b/components/esp_hw_support/port/esp32s3/spiram_psram.c index 22129d8940..2ad72b7e81 100644 --- a/components/esp_hw_support/port/esp32s3/spiram_psram.c +++ b/components/esp_hw_support/port/esp32s3/spiram_psram.c @@ -16,7 +16,6 @@ #include "esp_types.h" #include "esp_log.h" #include "spiram_psram.h" -#include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/opi_flash.h" #include "esp32s3/rom/cache.h" #include "esp32s3/rom/efuse.h" diff --git a/components/esp_rom/CMakeLists.txt b/components/esp_rom/CMakeLists.txt index 3b57d02faa..2af8d81cac 100644 --- a/components/esp_rom/CMakeLists.txt +++ b/components/esp_rom/CMakeLists.txt @@ -16,6 +16,7 @@ else() list(APPEND sources "patches/esp_rom_crc.c" "patches/esp_rom_sys.c" "patches/esp_rom_uart.c" + "patches/esp_rom_spiflash.c" "patches/esp_rom_tjpgd.c") list(APPEND private_required_comp soc hal) endif() @@ -26,7 +27,8 @@ endif() idf_component_register(SRCS ${sources} INCLUDE_DIRS ${include_dirs} - PRIV_REQUIRES ${private_required_comp}) + PRIV_REQUIRES ${private_required_comp} + LDFRAGMENTS linker.lf) if(target STREQUAL "esp32h2") if(CONFIG_IDF_TARGET_ESP32H2_BETA_VERSION_1) diff --git a/components/esp_rom/include/esp32/rom/spi_flash.h b/components/esp_rom/include/esp32/rom/spi_flash.h index db70b1ba90..cfc2976f64 100644 --- a/components/esp_rom/include/esp32/rom/spi_flash.h +++ b/components/esp_rom/include/esp32/rom/spi_flash.h @@ -115,6 +115,12 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 +typedef enum { + ESP_ROM_SPIFLASH_RESULT_OK, + ESP_ROM_SPIFLASH_RESULT_ERR, + ESP_ROM_SPIFLASH_RESULT_TIMEOUT +} esp_rom_spiflash_result_t; + extern uint8_t g_rom_spiflash_dummy_len_plus[]; /** diff --git a/components/esp_rom/include/esp32c3/rom/spi_flash.h b/components/esp_rom/include/esp32c3/rom/spi_flash.h index 83f330680d..291f581097 100644 --- a/components/esp_rom/include/esp32c3/rom/spi_flash.h +++ b/components/esp_rom/include/esp32c3/rom/spi_flash.h @@ -71,18 +71,18 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 typedef enum { - SPI_FLASH_RESULT_OK, - SPI_FLASH_RESULT_ERR, - SPI_FLASH_RESULT_TIMEOUT -} SpiFlashOpResult; + ESP_ROM_SPIFLASH_RESULT_OK, + ESP_ROM_SPIFLASH_RESULT_ERR, + ESP_ROM_SPIFLASH_RESULT_TIMEOUT +} esp_rom_spiflash_result_t; typedef void (* spi_flash_func_t)(void); -typedef SpiFlashOpResult (* spi_flash_op_t)(void); -typedef SpiFlashOpResult (* spi_flash_erase_t)(uint32_t); -typedef SpiFlashOpResult (* spi_flash_rd_t)(uint32_t, uint32_t*, int); -typedef SpiFlashOpResult (* spi_flash_wr_t)(uint32_t, const uint32_t*, int); -typedef SpiFlashOpResult (* spi_flash_ewr_t)(uint32_t, const void*, uint32_t); -typedef SpiFlashOpResult (* spi_flash_wren_t)(void*); +typedef esp_rom_spiflash_result_t (* spi_flash_op_t)(void); +typedef esp_rom_spiflash_result_t (* spi_flash_erase_t)(uint32_t); +typedef esp_rom_spiflash_result_t (* spi_flash_rd_t)(uint32_t, uint32_t*, int); +typedef esp_rom_spiflash_result_t (* spi_flash_wr_t)(uint32_t, const uint32_t*, int); +typedef esp_rom_spiflash_result_t (* spi_flash_ewr_t)(uint32_t, const void*, uint32_t); +typedef esp_rom_spiflash_result_t (* spi_flash_wren_t)(void*); typedef struct { uint32_t read_sub_len; diff --git a/components/esp_rom/include/esp32h2/rom/spi_flash.h b/components/esp_rom/include/esp32h2/rom/spi_flash.h index 9f3a1dab8c..fb1faabe98 100644 --- a/components/esp_rom/include/esp32h2/rom/spi_flash.h +++ b/components/esp_rom/include/esp32h2/rom/spi_flash.h @@ -71,18 +71,18 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 typedef enum { - SPI_FLASH_RESULT_OK, - SPI_FLASH_RESULT_ERR, - SPI_FLASH_RESULT_TIMEOUT -} SpiFlashOpResult; + ESP_ROM_SPIFLASH_RESULT_OK, + ESP_ROM_SPIFLASH_RESULT_ERR, + ESP_ROM_SPIFLASH_RESULT_TIMEOUT +} esp_rom_spiflash_result_t; typedef void (* spi_flash_func_t)(void); -typedef SpiFlashOpResult (* spi_flash_op_t)(void); -typedef SpiFlashOpResult (* spi_flash_erase_t)(uint32_t); -typedef SpiFlashOpResult (* spi_flash_rd_t)(uint32_t, uint32_t*, int); -typedef SpiFlashOpResult (* spi_flash_wr_t)(uint32_t, const uint32_t*, int); -typedef SpiFlashOpResult (* spi_flash_ewr_t)(uint32_t, const void*, uint32_t); -typedef SpiFlashOpResult (* spi_flash_wren_t)(void*); +typedef esp_rom_spiflash_result_t (* spi_flash_op_t)(void); +typedef esp_rom_spiflash_result_t (* spi_flash_erase_t)(uint32_t); +typedef esp_rom_spiflash_result_t (* spi_flash_rd_t)(uint32_t, uint32_t*, int); +typedef esp_rom_spiflash_result_t (* spi_flash_wr_t)(uint32_t, const uint32_t*, int); +typedef esp_rom_spiflash_result_t (* spi_flash_ewr_t)(uint32_t, const void*, uint32_t); +typedef esp_rom_spiflash_result_t (* spi_flash_wren_t)(void*); typedef struct { uint32_t read_sub_len; diff --git a/components/esp_rom/include/esp32s2/rom/opi_flash.h b/components/esp_rom/include/esp32s2/rom/opi_flash.h index 9aa0b97c23..1c2392e354 100644 --- a/components/esp_rom/include/esp32s2/rom/opi_flash.h +++ b/components/esp_rom/include/esp32s2/rom/opi_flash.h @@ -8,7 +8,6 @@ #include #include #include -#include "spi_flash.h" #include "esp_rom_spiflash.h" #ifdef __cplusplus diff --git a/components/esp_rom/include/esp32s2/rom/spi_flash.h b/components/esp_rom/include/esp32s2/rom/spi_flash.h index c34e95cb90..45ee6bb704 100644 --- a/components/esp_rom/include/esp32s2/rom/spi_flash.h +++ b/components/esp_rom/include/esp32s2/rom/spi_flash.h @@ -107,10 +107,10 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 typedef enum { - SPI_FLASH_RESULT_OK, - SPI_FLASH_RESULT_ERR, - SPI_FLASH_RESULT_TIMEOUT -} SpiFlashOpResult; + ESP_ROM_SPIFLASH_RESULT_OK, + ESP_ROM_SPIFLASH_RESULT_ERR, + ESP_ROM_SPIFLASH_RESULT_TIMEOUT +} esp_rom_spiflash_result_t; extern uint8_t g_rom_spiflash_dummy_len_plus[]; diff --git a/components/esp_rom/include/esp32s3/rom/opi_flash.h b/components/esp_rom/include/esp32s3/rom/opi_flash.h index 539314eb03..67feabb600 100644 --- a/components/esp_rom/include/esp32s3/rom/opi_flash.h +++ b/components/esp_rom/include/esp32s3/rom/opi_flash.h @@ -9,7 +9,6 @@ #include #include #include -#include "spi_flash.h" #include "esp_rom_spiflash.h" #ifdef __cplusplus diff --git a/components/esp_rom/include/esp32s3/rom/spi_flash.h b/components/esp_rom/include/esp32s3/rom/spi_flash.h index a070f450c1..832ad2496e 100644 --- a/components/esp_rom/include/esp32s3/rom/spi_flash.h +++ b/components/esp_rom/include/esp32s3/rom/spi_flash.h @@ -100,19 +100,19 @@ extern "C" { typedef enum { - SPI_FLASH_RESULT_OK, - SPI_FLASH_RESULT_ERR, - SPI_FLASH_RESULT_TIMEOUT -} SpiFlashOpResult; + ESP_ROM_SPIFLASH_RESULT_OK, + ESP_ROM_SPIFLASH_RESULT_ERR, + ESP_ROM_SPIFLASH_RESULT_TIMEOUT +} esp_rom_spiflash_result_t; typedef void (*spi_flash_func_t)(void); -typedef SpiFlashOpResult (*spi_flash_op_t)(void); -typedef SpiFlashOpResult (*spi_flash_erase_t)(uint32_t); -typedef SpiFlashOpResult (*spi_flash_rd_t)(uint32_t, void*, int); -typedef SpiFlashOpResult (*spi_flash_wr_t)(uint32_t, const uint32_t*, int); -typedef SpiFlashOpResult (*spi_flash_ewr_t)(uint32_t, const void*, uint32_t); -typedef SpiFlashOpResult (*spi_flash_wren_t)(void*); -typedef SpiFlashOpResult (* spi_flash_erase_area_t)(uint32_t, uint32_t); +typedef esp_rom_spiflash_result_t (*spi_flash_op_t)(void); +typedef esp_rom_spiflash_result_t (*spi_flash_erase_t)(uint32_t); +typedef esp_rom_spiflash_result_t (*spi_flash_rd_t)(uint32_t, void*, int); +typedef esp_rom_spiflash_result_t (*spi_flash_wr_t)(uint32_t, const uint32_t*, int); +typedef esp_rom_spiflash_result_t (*spi_flash_ewr_t)(uint32_t, const void*, uint32_t); +typedef esp_rom_spiflash_result_t (*spi_flash_wren_t)(void*); +typedef esp_rom_spiflash_result_t (* spi_flash_erase_area_t)(uint32_t, uint32_t); typedef struct { uint8_t pp_addr_bit_len; diff --git a/components/esp_rom/include/esp8684/rom/spi_flash.h b/components/esp_rom/include/esp8684/rom/spi_flash.h index ce536e4ff2..291f581097 100644 --- a/components/esp_rom/include/esp8684/rom/spi_flash.h +++ b/components/esp_rom/include/esp8684/rom/spi_flash.h @@ -16,14 +16,6 @@ extern "C" { #endif -/** \defgroup spi_flash_apis, spi flash operation related apis - * @brief spi_flash apis - */ - -/** @addtogroup spi_flash_apis - * @{ - */ - #define PERIPHS_SPI_FLASH_CMD SPI_MEM_CMD_REG(1) #define PERIPHS_SPI_FLASH_ADDR SPI_MEM_ADDR_REG(1) #define PERIPHS_SPI_FLASH_CTRL SPI_MEM_CTRL_REG(1) @@ -78,445 +70,21 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 -typedef enum { - ESP_ROM_SPIFLASH_QIO_MODE = 0, - ESP_ROM_SPIFLASH_QOUT_MODE, - ESP_ROM_SPIFLASH_DIO_MODE, - ESP_ROM_SPIFLASH_DOUT_MODE, - ESP_ROM_SPIFLASH_FASTRD_MODE, - ESP_ROM_SPIFLASH_SLOWRD_MODE -} esp_rom_spiflash_read_mode_t; - typedef enum { ESP_ROM_SPIFLASH_RESULT_OK, ESP_ROM_SPIFLASH_RESULT_ERR, ESP_ROM_SPIFLASH_RESULT_TIMEOUT } esp_rom_spiflash_result_t; -typedef struct { - uint32_t device_id; - uint32_t chip_size; // chip size in bytes - uint32_t block_size; - uint32_t sector_size; - uint32_t page_size; - uint32_t status_mask; -} esp_rom_spiflash_chip_t; - -typedef struct { - uint8_t data_length; - uint8_t read_cmd0; - uint8_t read_cmd1; - uint8_t write_cmd; - uint16_t data_mask; - uint16_t data; -} esp_rom_spiflash_common_cmd_t; - -/** - * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. - * Please do not call this function in SDK. - * - * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). - * - * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. - * - * @return None - */ -void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); - -/** - * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); - -/** - * @brief Set SPI Flash pad drivers. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid - * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. - * Values usually read from falsh by rom code, function usually callde by rom code. - * if value with bit(3) set, the value is valid, bit[2:0] is the real value. - * - * @return None - */ -void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); - -/** - * @brief Select SPI Flash function for pads. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); - -/** - * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t legacy: In legacy mode, more SPI command is used in line. - * - * @return None - */ -void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); - -/** - * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t *status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); - -/** - * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t *status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); - -/** - * @brief Write status to Falsh status register. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t status_value : Value to . - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); - -/** - * @brief Use a command to Read Flash status register. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. - * - * @param uint32_t*status : The pointer to which to return the Flash status value. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); - -/** - * @brief Config SPI Flash read mode when init. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. - * - * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : config error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); - -/** - * @brief Config SPI Flash clock divisor. - * Please do not call this function in SDK. - * - * @param uint8_t freqdiv: clock divisor. - * - * @param uint8_t spi: 0 for SPI0, 1 for SPI1. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : config error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); - -/** - * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. - * - * @return uint16_t 0 : do not send command any more. - * 1 : go to the next command. - * n > 1 : skip (n - 1) commands. - */ -uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); - -/** - * @brief Unlock SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); - -/** - * @brief SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Lock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Lock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Lock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_lock(void); - -/** - * @brief Update SPI Flash parameter. - * Please do not call this function in SDK. - * - * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. - * - * @param uint32_t chip_size : The Flash size. - * - * @param uint32_t block_size : The Flash block size. - * - * @param uint32_t sector_size : The Flash sector size. - * - * @param uint32_t page_size : The Flash page size. - * - * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, - uint32_t sector_size, uint32_t page_size, uint32_t status_mask); - -/** - * @brief Erase whole flash chip. - * Please do not call this function in SDK. - * - * @param None - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); - -/** - * @brief Erase a 64KB block of flash - * Uses SPI flash command D8H. - * Please do not call this function in SDK. - * - * @param uint32_t block_num : Which block to erase. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); - -/** - * @brief Erase a sector of flash. - * Uses SPI flash command 20H. - * Please do not call this function in SDK. - * - * @param uint32_t sector_num : Which sector to erase. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); - -/** - * @brief Erase some sectors. - * Please do not call this function in SDK. - * - * @param uint32_t start_addr : Start addr to erase, should be sector aligned. - * - * @param uint32_t area_len : Length to erase, should be sector aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); - -/** - * @brief Write Data to Flash, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. - * - * @param const uint32_t *src : The pointer to data which is to write. - * - * @param uint32_t len : Length to write, should be 4 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); - -/** - * @brief Read Data from Flash, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. - * - * @param uint32_t *dest : The buf to read the data. - * - * @param uint32_t len : Length to read, should be 4 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); - -/** - * @brief SPI1 go into encrypto mode. - * Please do not call this function in SDK. - * - * @param None - * - * @return None - */ -void esp_rom_spiflash_write_encrypted_enable(void); - -/** - * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. - * Please do not call this function in SDK. - * - * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. - * - * @param uint32_t *data : The pointer to data which is to write. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); - -/** - * @brief SPI1 go out of encrypto mode. - * Please do not call this function in SDK. - * - * @param None - * - * @return None - */ -void esp_rom_spiflash_write_encrypted_disable(void); - -/** - * @brief Write data to flash with transparent encryption. - * @note Sectors to be written should already be erased. - * - * @note Please do not call this function in SDK. - * - * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. - * - * @param uint32_t *data : The pointer to data to write. Note, this pointer must - * be 32 bit aligned and the content of the data will be - * modified by the encryption function. - * - * @param uint32_t len : Length to write, should be 32 bytes aligned. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. - * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); - - -/* TODO: figure out how to map these to their new names */ -typedef enum { - SPI_ENCRYPT_DESTINATION_FLASH, -} SpiEncryptDest; - -typedef esp_rom_spiflash_result_t SpiFlashOpResult; - -SpiFlashOpResult SPI_Encrypt_Write(uint32_t flash_addr, const void *data, uint32_t len); -SpiFlashOpResult SPI_Encrypt_Write_Dest(SpiEncryptDest dest, uint32_t flash_addr, const void *data, uint32_t len); -void SPI_Write_Encrypt_Enable(void); -void SPI_Write_Encrypt_Disable(void); - -/** @brief Wait until SPI flash write operation is complete - * - * @note Please do not call this function in SDK. - * - * Reads the Write In Progress bit of the SPI flash status register, - * repeats until this bit is zero (indicating write complete). - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete - * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); - - -/** @brief Enable Quad I/O pin functions - * - * @note Please do not call this function in SDK. - * - * Sets the HD & WP pin functions for Quad I/O modes, based on the - * efuse SPI pin configuration. - * - * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. - * - * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). - * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. - * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. - * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used - * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). - * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. - */ -void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); - - typedef void (* spi_flash_func_t)(void); -typedef SpiFlashOpResult (* spi_flash_op_t)(void); -typedef SpiFlashOpResult (* spi_flash_erase_t)(uint32_t); -typedef SpiFlashOpResult (* spi_flash_rd_t)(uint32_t, uint32_t*, int); -typedef SpiFlashOpResult (* spi_flash_wr_t)(uint32_t, const uint32_t*, int); -typedef SpiFlashOpResult (* spi_flash_ewr_t)(uint32_t, const void*, uint32_t); -typedef SpiFlashOpResult (* spi_flash_wren_t)(void*); +typedef esp_rom_spiflash_result_t (* spi_flash_op_t)(void); +typedef esp_rom_spiflash_result_t (* spi_flash_erase_t)(uint32_t); +typedef esp_rom_spiflash_result_t (* spi_flash_rd_t)(uint32_t, uint32_t*, int); +typedef esp_rom_spiflash_result_t (* spi_flash_wr_t)(uint32_t, const uint32_t*, int); +typedef esp_rom_spiflash_result_t (* spi_flash_ewr_t)(uint32_t, const void*, uint32_t); +typedef esp_rom_spiflash_result_t (* spi_flash_wren_t)(void*); typedef struct { - uint8_t pp_addr_bit_len; - uint8_t se_addr_bit_len; - uint8_t be_addr_bit_len; - uint8_t rd_addr_bit_len; uint32_t read_sub_len; uint32_t write_sub_len; spi_flash_op_t unlock; @@ -530,35 +98,6 @@ typedef struct { spi_flash_op_t wait_idle; } spiflash_legacy_funcs_t; - -extern const spiflash_legacy_funcs_t *rom_spiflash_legacy_funcs; - -/** @brief Global ROM spiflash data, as used by legacy - SPI flash functions -*/ -typedef struct { - esp_rom_spiflash_chip_t chip; - uint8_t dummy_len_plus[3]; - uint8_t sig_matrix; -} spiflash_legacy_data_t; - -extern spiflash_legacy_data_t *rom_spiflash_legacy_data; - -/* Defines to make the C3 ROM legacvy data access compatible with previous chips */ -#define g_rom_flashchip (rom_spiflash_legacy_data->chip) -#define g_rom_spiflash_dummy_len_plus (rom_spiflash_legacy_data->dummy_len_plus) - -/** - * @brief Clear WEL bit unconditionally. - * - * @return always ESP_ROM_SPIFLASH_RESULT_OK - */ -esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); - -/** - * @} - */ - #ifdef __cplusplus } #endif diff --git a/components/esp_rom/include/esp_rom_spiflash.h b/components/esp_rom/include/esp_rom_spiflash.h index c4646a7472..aa5aaf05ce 100644 --- a/components/esp_rom/include/esp_rom_spiflash.h +++ b/components/esp_rom/include/esp_rom_spiflash.h @@ -11,9 +11,24 @@ extern "C" { #endif +#include "sdkconfig.h" #include #include +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32C3 +#include "esp32c3/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32H2 +#include "esp32h2/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP8684 +#include "esp32h2/rom/spi_flash.h" +#endif + /** \defgroup spi_flash_apis, spi flash operation related apis * @brief spi_flash apis */ @@ -36,12 +51,6 @@ typedef enum { ESP_ROM_SPIFLASH_OIO_DTR_MODE, } esp_rom_spiflash_read_mode_t; -typedef enum { - ESP_ROM_SPIFLASH_RESULT_OK, - ESP_ROM_SPIFLASH_RESULT_ERR, - ESP_ROM_SPIFLASH_RESULT_TIMEOUT -} esp_rom_spiflash_result_t; - typedef struct { uint32_t device_id; uint32_t chip_size; // chip size in bytes diff --git a/components/esp_rom/linker.lf b/components/esp_rom/linker.lf new file mode 100644 index 0000000000..4bf5582eb2 --- /dev/null +++ b/components/esp_rom/linker.lf @@ -0,0 +1,4 @@ +[mapping:esp_rom] +archive: libesp_rom.a +entries: + esp_rom_spiflash (noflash) diff --git a/components/spi_flash/esp32/spi_flash_rom_patch.c b/components/esp_rom/patches/esp_rom_spiflash.c similarity index 96% rename from components/spi_flash/esp32/spi_flash_rom_patch.c rename to components/esp_rom/patches/esp_rom_spiflash.c index 69738a505e..d9e6cf4822 100644 --- a/components/spi_flash/esp32/spi_flash_rom_patch.c +++ b/components/esp_rom/patches/esp_rom_spiflash.c @@ -3,19 +3,20 @@ * * SPDX-License-Identifier: Apache-2.0 */ + #include "sdkconfig.h" -#include "esp32/rom/spi_flash.h" +#include "esp_rom_spiflash.h" #include "soc/spi_periph.h" -#include "spi_flash_defs.h" #include "esp_rom_spiflash.h" - #define SPI_IDX 1 -#define OTH_IDX 0 - extern esp_rom_spiflash_chip_t g_rom_spiflash_chip; +#if CONFIG_SPI_FLASH_ROM_DRIVER_PATCH + +#if CONFIG_IDF_TARGET_ESP32 + static inline bool is_issi_chip(const esp_rom_spiflash_chip_t* chip) { return (((chip->device_id >> 16)&0xff) == 0x9D); @@ -24,18 +25,11 @@ static inline bool is_issi_chip(const esp_rom_spiflash_chip_t* chip) esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi) { uint32_t status; -#if CONFIG_IDF_TARGET_ESP32 //wait for spi control ready while ((REG_READ(SPI_EXT2_REG(1)) & SPI_ST)) { } while ((REG_READ(SPI_EXT2_REG(0)) & SPI_ST)) { } -#elif CONFIG_IDF_TARGET_ESP32S2 - while ((REG_READ(SPI_MEM_FSM_REG(1)) & SPI_MEM_ST)) { - } - while ((REG_READ(SPI_MEM_FSM_REG(0)) & SPI_MEM_ST)) { - } -#endif //wait for flash status ready if ( ESP_ROM_SPIFLASH_RESULT_OK != esp_rom_spiflash_read_status(spi, &status)) { return ESP_ROM_SPIFLASH_RESULT_ERR; @@ -43,7 +37,6 @@ esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *sp return ESP_ROM_SPIFLASH_RESULT_OK; } - /* Modified version of esp_rom_spiflash_unlock() that replaces version in ROM. This works around a bug where esp_rom_spiflash_unlock sometimes reads the wrong @@ -104,10 +97,6 @@ __attribute__((__unused__)) esp_rom_spiflash_result_t esp_rom_spiflash_unlock(vo return ret; } -#if CONFIG_SPI_FLASH_ROM_DRIVER_PATCH - -extern uint8_t g_rom_spiflash_dummy_len_plus[]; - static esp_rom_spiflash_result_t esp_rom_spiflash_enable_write(esp_rom_spiflash_chip_t *spi); @@ -118,7 +107,8 @@ static esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip_internal(esp_rom_sp // Chip erase. WRITE_PERI_REG(PERIPHS_SPI_FLASH_CMD, SPI_FLASH_CE); - while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0); + while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0) { + } // check erase is finished. esp_rom_spiflash_wait_idle(spi); @@ -139,7 +129,8 @@ static esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector_internal(esp_rom_ // sector erase 4Kbytes erase is sector erase. WRITE_PERI_REG(PERIPHS_SPI_FLASH_ADDR, addr & 0xffffff); WRITE_PERI_REG(PERIPHS_SPI_FLASH_CMD, SPI_FLASH_SE); - while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0); + while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0) { + } esp_rom_spiflash_wait_idle(spi); @@ -154,7 +145,8 @@ static esp_rom_spiflash_result_t esp_rom_spiflash_erase_block_internal(esp_rom_s // sector erase 4Kbytes erase is sector erase. WRITE_PERI_REG(PERIPHS_SPI_FLASH_ADDR, addr & 0xffffff); WRITE_PERI_REG(PERIPHS_SPI_FLASH_CMD, SPI_FLASH_BE); - while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0); + while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0) { + } esp_rom_spiflash_wait_idle(spi); @@ -208,7 +200,8 @@ static esp_rom_spiflash_result_t esp_rom_spiflash_program_page_internal(esp_rom_ temp_bl = 0; } WRITE_PERI_REG(PERIPHS_SPI_FLASH_CMD, SPI_FLASH_PP); - while ( READ_PERI_REG(PERIPHS_SPI_FLASH_CMD ) != 0 ); + while ( READ_PERI_REG(PERIPHS_SPI_FLASH_CMD ) != 0 ) { + } esp_rom_spiflash_wait_idle(spi); } @@ -224,7 +217,8 @@ esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t * while (ESP_ROM_SPIFLASH_BUSY_FLAG == (status_value & ESP_ROM_SPIFLASH_BUSY_FLAG)) { WRITE_PERI_REG(PERIPHS_SPI_FLASH_STATUS, 0); // clear regisrter WRITE_PERI_REG(PERIPHS_SPI_FLASH_CMD, SPI_FLASH_RDSR); - while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0); + while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0) { + } status_value = READ_PERI_REG(PERIPHS_SPI_FLASH_STATUS) & (spi->status_mask); } @@ -254,7 +248,8 @@ esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t // update status value by status_value WRITE_PERI_REG(PERIPHS_SPI_FLASH_STATUS, status_value); // write status regisrter WRITE_PERI_REG(PERIPHS_SPI_FLASH_CMD, SPI_FLASH_WRSR); - while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0); + while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0) { + } esp_rom_spiflash_wait_idle(spi); return ESP_ROM_SPIFLASH_RESULT_OK; @@ -285,7 +280,8 @@ static esp_rom_spiflash_result_t esp_rom_spiflash_read_data(esp_rom_spiflash_chi REG_WRITE(SPI_MISO_DLEN_REG(1), ((ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM << 3) - 1) << SPI_USR_MISO_DBITLEN_S); WRITE_PERI_REG(PERIPHS_SPI_FLASH_ADDR, temp_addr << 8); REG_WRITE(PERIPHS_SPI_FLASH_CMD, SPI_USR); - while (REG_READ(PERIPHS_SPI_FLASH_CMD) != 0); + while (REG_READ(PERIPHS_SPI_FLASH_CMD) != 0) { + } for (i = 0; i < (ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM >> 2); i++) { *addr_dest++ = READ_PERI_REG(PERIPHS_SPI_FLASH_C0 + i * 4); @@ -297,7 +293,8 @@ static esp_rom_spiflash_result_t esp_rom_spiflash_read_data(esp_rom_spiflash_chi WRITE_PERI_REG(PERIPHS_SPI_FLASH_ADDR, temp_addr << 8); REG_WRITE(SPI_MISO_DLEN_REG(1), ((ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM << 3) - 1) << SPI_USR_MISO_DBITLEN_S); REG_WRITE(PERIPHS_SPI_FLASH_CMD, SPI_USR); - while (REG_READ(PERIPHS_SPI_FLASH_CMD) != 0); + while (REG_READ(PERIPHS_SPI_FLASH_CMD) != 0) { + }; remain_word_num = (0 == (temp_length & 0x3)) ? (temp_length >> 2) : (temp_length >> 2) + 1; for (i = 0; i < remain_word_num; i++) { @@ -318,7 +315,8 @@ static esp_rom_spiflash_result_t esp_rom_spiflash_enable_write(esp_rom_spiflash_ //enable write WRITE_PERI_REG(PERIPHS_SPI_FLASH_CMD, SPI_FLASH_WREN); // enable write operation - while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0); + while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0) { + } // make sure the flash is ready for writing while (ESP_ROM_SPIFLASH_WRENABLE_FLAG != (flash_status & ESP_ROM_SPIFLASH_WRENABLE_FLAG)) { @@ -476,8 +474,9 @@ esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num) esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t target, const uint32_t *src_addr, int32_t len) { uint32_t page_size; - uint32_t pgm_len, pgm_num; - uint8_t i; + uint32_t pgm_len; + uint32_t pgm_num; + uint32_t i; // flash write is always 1 line currently REG_CLR_BIT(PERIPHS_SPI_FLASH_USRREG, SPI_USR_DUMMY); @@ -679,8 +678,21 @@ esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint3 esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void) { REG_WRITE(SPI_CMD_REG(SPI_IDX), SPI_FLASH_WRDI); - while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0); + while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0) { + } return ESP_ROM_SPIFLASH_RESULT_OK; } -#endif +#elif CONFIG_IDF_TARGET_ESP32S2 + +esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void) +{ + REG_WRITE(SPI_MEM_CMD_REG(SPI_IDX), SPI_MEM_FLASH_WRDI); + while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0) { + } + return ESP_ROM_SPIFLASH_RESULT_OK; +} + +#endif // IDF_TARGET + +#endif // CONFIG_SPI_FLASH_ROM_DRIVER_PATCH diff --git a/components/esp_system/port/cpu_start.c b/components/esp_system/port/cpu_start.c index eae13483b4..d921aeaa82 100644 --- a/components/esp_system/port/cpu_start.c +++ b/components/esp_system/port/cpu_start.c @@ -86,8 +86,6 @@ #if CONFIG_APP_BUILD_TYPE_ELF_RAM #include "esp_rom_spiflash.h" -#elif CONFIG_IDF_TARGET_ESP8684 -#include "esp8684/rom/spi_flash.h" #endif // CONFIG_APP_BUILD_TYPE_ELF_RAM // Set efuse ROM_LOG_MODE on first boot diff --git a/components/spi_flash/CMakeLists.txt b/components/spi_flash/CMakeLists.txt index 51406d2e72..364c35f610 100644 --- a/components/spi_flash/CMakeLists.txt +++ b/components/spi_flash/CMakeLists.txt @@ -1,6 +1,5 @@ idf_build_get_property(target IDF_TARGET) if(BOOTLOADER_BUILD) - set(srcs "${target}/spi_flash_rom_patch.c") set(cache_srcs "") set(priv_requires bootloader_support soc) else() @@ -12,7 +11,6 @@ else() ) set(srcs "partition.c" - "${target}/spi_flash_rom_patch.c" ) if(CONFIG_ESPTOOLPY_OCT_FLASH) diff --git a/components/spi_flash/cache_utils.c b/components/spi_flash/cache_utils.c index 13b9543695..f3e4c349f8 100644 --- a/components/spi_flash/cache_utils.c +++ b/components/spi_flash/cache_utils.c @@ -32,7 +32,6 @@ #include "soc/extmem_reg.h" #include "soc/cache_memory.h" #elif CONFIG_IDF_TARGET_ESP8684 -#include "esp8684/rom/spi_flash.h" #include "esp8684/rom/cache.h" #include "soc/extmem_reg.h" #include "soc/cache_memory.h" diff --git a/components/spi_flash/esp32/flash_ops_esp32.c b/components/spi_flash/esp32/flash_ops_esp32.c index 2ae5b8fe54..e9ede64ded 100644 --- a/components/spi_flash/esp32/flash_ops_esp32.c +++ b/components/spi_flash/esp32/flash_ops_esp32.c @@ -5,7 +5,6 @@ */ #include #include "esp_spi_flash.h" -#include "esp32/rom/spi_flash.h" #include "esp32/rom/cache.h" #include "esp_rom_spiflash.h" diff --git a/components/spi_flash/esp32c3/flash_ops_esp32c3.c b/components/spi_flash/esp32c3/flash_ops_esp32c3.c index 8765cfc6a9..bdaa223ae5 100644 --- a/components/spi_flash/esp32c3/flash_ops_esp32c3.c +++ b/components/spi_flash/esp32c3/flash_ops_esp32c3.c @@ -10,7 +10,6 @@ #include "esp_spi_flash.h" #include "soc/system_reg.h" #include "soc/soc_memory_layout.h" -#include "esp32c3/rom/spi_flash.h" #include "esp32c3/rom/cache.h" #include "hal/spi_flash_hal.h" #include "esp_flash.h" diff --git a/components/spi_flash/esp32c3/spi_flash_rom_patch.c b/components/spi_flash/esp32c3/spi_flash_rom_patch.c deleted file mode 100644 index c272b3fb62..0000000000 --- a/components/spi_flash/esp32c3/spi_flash_rom_patch.c +++ /dev/null @@ -1,19 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "sdkconfig.h" -#include "esp32c3/rom/spi_flash.h" -#include "soc/spi_periph.h" -#include "spi_flash_defs.h" -#include "esp_rom_spiflash.h" - -#define SPI_IDX 1 - -esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void) -{ - REG_WRITE(SPI_MEM_CMD_REG(SPI_IDX), SPI_MEM_FLASH_WRDI); - while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0); - return ESP_ROM_SPIFLASH_RESULT_OK; -} diff --git a/components/spi_flash/esp32h2/flash_ops_esp32h2.c b/components/spi_flash/esp32h2/flash_ops_esp32h2.c index e69733898e..ef1b5581a0 100644 --- a/components/spi_flash/esp32h2/flash_ops_esp32h2.c +++ b/components/spi_flash/esp32h2/flash_ops_esp32h2.c @@ -10,7 +10,6 @@ #include "esp_spi_flash.h" #include "soc/system_reg.h" #include "soc/soc_memory_layout.h" -#include "esp32h2/rom/spi_flash.h" #include "esp32h2/rom/cache.h" #include "hal/spi_flash_hal.h" #include "esp_flash.h" diff --git a/components/spi_flash/esp32h2/spi_flash_rom_patch.c b/components/spi_flash/esp32h2/spi_flash_rom_patch.c deleted file mode 100644 index cc2dcd5a64..0000000000 --- a/components/spi_flash/esp32h2/spi_flash_rom_patch.c +++ /dev/null @@ -1,19 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "sdkconfig.h" -#include "esp32h2/rom/spi_flash.h" -#include "soc/spi_periph.h" -#include "spi_flash_defs.h" -#include "esp_rom_spiflash.h" - -#define SPI_IDX 1 - -esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void) -{ - REG_WRITE(SPI_MEM_CMD_REG(SPI_IDX), SPI_MEM_FLASH_WRDI); - while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0); - return ESP_ROM_SPIFLASH_RESULT_OK; -} diff --git a/components/spi_flash/esp32s2/flash_ops_esp32s2.c b/components/spi_flash/esp32s2/flash_ops_esp32s2.c index cf80475194..639637ca19 100644 --- a/components/spi_flash/esp32s2/flash_ops_esp32s2.c +++ b/components/spi_flash/esp32s2/flash_ops_esp32s2.c @@ -10,7 +10,6 @@ #include "esp_spi_flash.h" #include "soc/system_reg.h" #include "soc/soc_memory_layout.h" -#include "esp32s2/rom/spi_flash.h" #include "esp32s2/rom/cache.h" #include "bootloader_flash.h" #include "hal/spi_flash_hal.h" diff --git a/components/spi_flash/esp32s2/spi_flash_rom_patch.c b/components/spi_flash/esp32s2/spi_flash_rom_patch.c deleted file mode 100644 index 23ee08e56c..0000000000 --- a/components/spi_flash/esp32s2/spi_flash_rom_patch.c +++ /dev/null @@ -1,21 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "sdkconfig.h" -#include "esp32s2/rom/spi_flash.h" -#include "soc/spi_periph.h" -#include "spi_flash_defs.h" -#include "esp_rom_spiflash.h" - - -#define SPI_IDX 1 -extern esp_rom_spiflash_chip_t g_rom_spiflash_chip; - -esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void) -{ - REG_WRITE(SPI_MEM_CMD_REG(SPI_IDX), SPI_MEM_FLASH_WRDI); - while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0); - return ESP_ROM_SPIFLASH_RESULT_OK; -} diff --git a/components/spi_flash/esp32s3/flash_ops_esp32s3.c b/components/spi_flash/esp32s3/flash_ops_esp32s3.c index f4891fbdbe..a2c0709266 100644 --- a/components/spi_flash/esp32s3/flash_ops_esp32s3.c +++ b/components/spi_flash/esp32s3/flash_ops_esp32s3.c @@ -10,7 +10,6 @@ #include "esp_spi_flash.h" #include "soc/system_reg.h" #include "soc/soc_memory_layout.h" -#include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/cache.h" #include "bootloader_flash.h" #include "hal/spi_flash_hal.h" diff --git a/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c b/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c index 41682cd431..8f25c714b2 100644 --- a/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c +++ b/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c @@ -9,7 +9,6 @@ #include "esp_err.h" #include "esp_rom_gpio.h" #include "esp32s3/rom/gpio.h" -#include "esp32s3/rom/spi_flash.h" #include "esp_rom_spiflash.h" #include "esp32s3/rom/opi_flash.h" #include "esp_private/spi_flash_os.h" diff --git a/components/spi_flash/esp32s3/spi_flash_rom_patch.c b/components/spi_flash/esp32s3/spi_flash_rom_patch.c deleted file mode 100644 index a3a6079529..0000000000 --- a/components/spi_flash/esp32s3/spi_flash_rom_patch.c +++ /dev/null @@ -1,6 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -// We keep this file here only for future use diff --git a/components/spi_flash/esp32s3/spi_timing_config.h b/components/spi_flash/esp32s3/spi_timing_config.h index a8d064d491..ed6f103308 100644 --- a/components/spi_flash/esp32s3/spi_timing_config.h +++ b/components/spi_flash/esp32s3/spi_timing_config.h @@ -7,7 +7,6 @@ #pragma once #include "esp_flash_partitions.h" -#include "esp32s3/rom/spi_flash.h" #include "esp_rom_spiflash.h" #include "esp32s3/rom/opi_flash.h" #include "mspi_timing_tuning_configs.h" diff --git a/components/spi_flash/esp8684/flash_ops_esp8684.c b/components/spi_flash/esp8684/flash_ops_esp8684.c index b90f90215e..ad62448c62 100644 --- a/components/spi_flash/esp8684/flash_ops_esp8684.c +++ b/components/spi_flash/esp8684/flash_ops_esp8684.c @@ -10,14 +10,14 @@ #include "esp_spi_flash.h" #include "soc/system_reg.h" #include "soc/soc_memory_layout.h" -#include "esp8684/rom/spi_flash.h" #include "esp8684/rom/cache.h" #include "hal/spi_flash_hal.h" #include "esp_flash.h" #include "esp_log.h" #include "esp_attr.h" +#include "esp_rom_spiflash.h" -static const char *TAG = "spiflash_c3"; +static const char *TAG = "spiflash_8684"; #define SPICACHE SPIMEM0 #define SPIFLASH SPIMEM1 diff --git a/components/spi_flash/esp8684/spi_flash_rom_patch.c b/components/spi_flash/esp8684/spi_flash_rom_patch.c deleted file mode 100644 index a48d22222b..0000000000 --- a/components/spi_flash/esp8684/spi_flash_rom_patch.c +++ /dev/null @@ -1,18 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "sdkconfig.h" -#include "esp8684/rom/spi_flash.h" -#include "soc/spi_periph.h" -#include "spi_flash_defs.h" - -#define SPI_IDX 1 - -esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void) -{ - REG_WRITE(SPI_MEM_CMD_REG(SPI_IDX), SPI_MEM_FLASH_WRDI); - while (READ_PERI_REG(PERIPHS_SPI_FLASH_CMD) != 0); - return ESP_ROM_SPIFLASH_RESULT_OK; -} diff --git a/components/spi_flash/esp_flash_api.c b/components/spi_flash/esp_flash_api.c index ba13b7f132..a432e1267a 100644 --- a/components/spi_flash/esp_flash_api.c +++ b/components/spi_flash/esp_flash_api.c @@ -20,8 +20,6 @@ #if CONFIG_IDF_TARGET_ESP32S2 #include "esp_crypto_lock.h" // for locking flash encryption peripheral #endif //CONFIG_IDF_TARGET_ESP32S2 -#elif CONFIG_IDF_TARGET_ESP8684 -#include "esp8684/rom/spi_flash.h" static const char TAG[] = "spi_flash"; diff --git a/components/spi_flash/esp_flash_spi_init.c b/components/spi_flash/esp_flash_spi_init.c index 22cd9fa04c..58ecbc68c6 100644 --- a/components/spi_flash/esp_flash_spi_init.c +++ b/components/spi_flash/esp_flash_spi_init.c @@ -21,7 +21,6 @@ #include "esp_rom_gpio.h" #include "esp_private/spi_flash_os.h" #include "esp_rom_spiflash.h" -#include "esp32c3/rom/efuse.h" __attribute__((unused)) static const char TAG[] = "spi_flash"; diff --git a/components/spi_flash/flash_mmap.c b/components/spi_flash/flash_mmap.c index 2315d5f91c..9364399fdb 100644 --- a/components/spi_flash/flash_mmap.c +++ b/components/spi_flash/flash_mmap.c @@ -52,7 +52,6 @@ #include "soc/mmu.h" #elif CONFIG_IDF_TARGET_ESP8684 #include "esp8684/rom/cache.h" -#include "esp8684/rom/spi_flash.h" #include "soc/cache_memory.h" #include "soc/mmu.h" #endif diff --git a/components/spi_flash/flash_ops.c b/components/spi_flash/flash_ops.c index d6381a2054..db39129f9f 100644 --- a/components/spi_flash/flash_ops.c +++ b/components/spi_flash/flash_ops.c @@ -24,7 +24,6 @@ #include "esp_private/esp_clk.h" #if CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/cache.h" -#include "esp32/rom/spi_flash.h" #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/rom/cache.h" #elif CONFIG_IDF_TARGET_ESP32S3 @@ -38,7 +37,6 @@ #include "esp32h2/rom/cache.h" #elif CONFIG_IDF_TARGET_ESP8684 #include "esp8684/rom/cache.h" -#include "esp8684/rom/spi_flash.h" #endif #include "esp_rom_spiflash.h" #include "esp_flash_partitions.h" diff --git a/components/spi_flash/linker.lf b/components/spi_flash/linker.lf index fdba7df429..5062c9bd97 100644 --- a/components/spi_flash/linker.lf +++ b/components/spi_flash/linker.lf @@ -1,7 +1,6 @@ [mapping:spi_flash] archive: libspi_flash.a entries: - spi_flash_rom_patch (noflash) spi_flash_chip_generic (noflash) spi_flash_chip_issi (noflash) spi_flash_chip_mxic (noflash) diff --git a/components/spi_flash/sim/flash_mock_util.c b/components/spi_flash/sim/flash_mock_util.c index 62c0146272..a6c1a0dd60 100644 --- a/components/spi_flash/sim/flash_mock_util.c +++ b/components/spi_flash/sim/flash_mock_util.c @@ -2,7 +2,6 @@ #include "esp_partition.h" #include "esp_err.h" -#include "esp32/rom/spi_flash.h" #include "esp_rom_spiflash.h" bool spi_flash_check_and_flush_cache(size_t start_addr, size_t length) diff --git a/components/spi_flash/test/test_read_write.c b/components/spi_flash/test/test_read_write.c index 76f4201c1f..e31c94ff36 100644 --- a/components/spi_flash/test/test_read_write.c +++ b/components/spi_flash/test/test_read_write.c @@ -18,10 +18,6 @@ #include "../cache_utils.h" #include "soc/timer_periph.h" #include "esp_heap_caps.h" - -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#endif #include "esp_rom_spiflash.h" #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP8684) diff --git a/components/spiffs/esp_spiffs.c b/components/spiffs/esp_spiffs.c index 6315f161e4..223d09d75e 100644 --- a/components/spiffs/esp_spiffs.c +++ b/components/spiffs/esp_spiffs.c @@ -22,8 +22,6 @@ #include "esp_vfs.h" #include "esp_err.h" #include "esp_rom_spiflash.h" -#elif CONFIG_IDF_TARGET_ESP8684 -#include "esp8684/rom/spi_flash.h" #include "spiffs_api.h" From b0decda1e32e22eeffdff6f4158851943d1f159a Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Mon, 8 Nov 2021 15:10:13 +0800 Subject: [PATCH 3/5] bootloader: move bootloader flash support to isolate folders --- components/app_update/test/test_switch_ota.c | 2 +- components/bootloader_support/CMakeLists.txt | 13 ++- .../include/bootloader_flash.h | 0 .../include/bootloader_flash_config.h | 0 .../bootloader_flash_priv.h | 0 .../flash_qio_mode.h | 0 .../{ => flash_support}/bootloader_flash.c | 4 - .../bootloader_flash_config_esp32.c | 4 +- .../bootloader_flash_config_esp32c3.c | 2 +- .../bootloader_flash_config_esp32h2.c | 2 +- .../bootloader_flash_config_esp32s2.c | 2 +- .../bootloader_flash_config_esp32s3.c | 2 +- .../bootloader_flash_config_esp8684.c | 2 +- .../src/{ => flash_support}/flash_qio_mode.c | 0 components/efuse/src/esp_efuse_utility.c | 2 +- .../esp_hw_support/port/esp32/spiram_psram.c | 2 +- .../port/esp32s2/spiram_psram.c | 2 +- .../port/esp32s3/opiram_psram.c | 2 +- .../port/esp32s3/spiram_psram.c | 2 +- .../esp_rom/include/esp32/rom/spi_flash.h | 78 ++++++++++--- .../esp_rom/include/esp32c3/rom/spi_flash.h | 79 ++++++++++++-- .../esp_rom/include/esp32h2/rom/spi_flash.h | 85 ++++++++++++--- .../esp_rom/include/esp32s2/rom/opi_flash.h | 2 +- .../esp_rom/include/esp32s2/rom/spi_flash.h | 77 ++++++++++--- .../esp_rom/include/esp32s3/rom/opi_flash.h | 2 +- .../esp_rom/include/esp32s3/rom/spi_flash.h | 74 +++++++++++-- .../esp_rom/include/esp8684/rom/spi_flash.h | 85 +++++++++++++-- components/esp_rom/include/esp_rom_spiflash.h | 103 ++---------------- components/esp_rom/patches/esp_rom_spiflash.c | 11 +- .../fatfs/test_fatfs_host/Makefile.files | 1 + .../esp32s3/spi_flash_oct_flash_init.c | 2 +- .../spi_flash/esp32s3/spi_timing_config.h | 2 +- components/spi_flash/flash_ops.c | 1 + .../include/esp_private/spi_flash_os.h | 2 +- components/spi_flash/sim/Makefile.files | 1 + components/spi_flash/sim/stubs/Makefile.files | 1 + components/spi_flash/test/test_read_write.c | 14 ++- .../spiffs/test_spiffs_host/Makefile.files | 1 + .../test_wl_host/Makefile.files | 1 + docs/en/migration-guides/peripherals.rst | 5 + 40 files changed, 474 insertions(+), 196 deletions(-) rename components/bootloader_support/{ => bootloader_flash}/include/bootloader_flash.h (100%) rename components/bootloader_support/{ => bootloader_flash}/include/bootloader_flash_config.h (100%) rename components/bootloader_support/include_bootloader/{ => bootloader_flash_private}/bootloader_flash_priv.h (100%) rename components/bootloader_support/include_bootloader/{ => bootloader_flash_private}/flash_qio_mode.h (100%) rename components/bootloader_support/src/{ => flash_support}/bootloader_flash.c (99%) rename components/bootloader_support/src/{ => flash_support}/bootloader_flash_config_esp32.c (98%) rename components/bootloader_support/src/{ => flash_support}/bootloader_flash_config_esp32c3.c (98%) rename components/bootloader_support/src/{ => flash_support}/bootloader_flash_config_esp32h2.c (98%) rename components/bootloader_support/src/{ => flash_support}/bootloader_flash_config_esp32s2.c (98%) rename components/bootloader_support/src/{ => flash_support}/bootloader_flash_config_esp32s3.c (98%) rename components/bootloader_support/src/{ => flash_support}/bootloader_flash_config_esp8684.c (98%) rename components/bootloader_support/src/{ => flash_support}/flash_qio_mode.c (100%) diff --git a/components/app_update/test/test_switch_ota.c b/components/app_update/test/test_switch_ota.c index 7ccdfc78f5..e5bb8ab599 100644 --- a/components/app_update/test/test_switch_ota.c +++ b/components/app_update/test/test_switch_ota.c @@ -21,7 +21,7 @@ #include "unity.h" #include "bootloader_common.h" -#include "../include_bootloader/bootloader_flash_priv.h" +#include "../include_bootloader/bootloader_flash_private/bootloader_flash_priv.h" #include "esp_log.h" #include "esp_ota_ops.h" diff --git a/components/bootloader_support/CMakeLists.txt b/components/bootloader_support/CMakeLists.txt index 664a59817d..88f26b8762 100644 --- a/components/bootloader_support/CMakeLists.txt +++ b/components/bootloader_support/CMakeLists.txt @@ -2,7 +2,7 @@ set(srcs "src/bootloader_common.c" "src/bootloader_common_loader.c" "src/bootloader_clock_init.c" - "src/bootloader_flash.c" + "src/flash_support/bootloader_flash.c" "src/bootloader_mem.c" "src/bootloader_random.c" "src/bootloader_random_${IDF_TARGET}.c" @@ -11,13 +11,14 @@ set(srcs "src/flash_encrypt.c" "src/secure_boot.c" "src/flash_partitions.c" - "src/flash_qio_mode.c" - "src/bootloader_flash_config_${IDF_TARGET}.c" + "src/flash_support/flash_qio_mode.c" + "src/flash_support/bootloader_flash_config_${IDF_TARGET}.c" "src/bootloader_efuse_${IDF_TARGET}.c" ) if(BOOTLOADER_BUILD) - set(include_dirs "include" "include_bootloader") + set(include_dirs "include" "bootloader_flash/include" + "include_bootloader" "include_bootloader/bootloader_flash_private") set(priv_requires micro-ecc spi_flash efuse) list(APPEND srcs "src/bootloader_init.c" @@ -33,8 +34,8 @@ if(BOOTLOADER_BUILD) else() list(APPEND srcs "src/idf/bootloader_sha.c") - set(include_dirs "include") - set(priv_include_dirs "include_bootloader") + set(include_dirs "include" "bootloader_flash/include") + set(priv_include_dirs "include_bootloader" "include_bootloader/bootloader_flash_private") # heap is required for `heap_memory_layout.h` header set(priv_requires spi_flash mbedtls efuse app_update heap) endif() diff --git a/components/bootloader_support/include/bootloader_flash.h b/components/bootloader_support/bootloader_flash/include/bootloader_flash.h similarity index 100% rename from components/bootloader_support/include/bootloader_flash.h rename to components/bootloader_support/bootloader_flash/include/bootloader_flash.h diff --git a/components/bootloader_support/include/bootloader_flash_config.h b/components/bootloader_support/bootloader_flash/include/bootloader_flash_config.h similarity index 100% rename from components/bootloader_support/include/bootloader_flash_config.h rename to components/bootloader_support/bootloader_flash/include/bootloader_flash_config.h diff --git a/components/bootloader_support/include_bootloader/bootloader_flash_priv.h b/components/bootloader_support/include_bootloader/bootloader_flash_private/bootloader_flash_priv.h similarity index 100% rename from components/bootloader_support/include_bootloader/bootloader_flash_priv.h rename to components/bootloader_support/include_bootloader/bootloader_flash_private/bootloader_flash_priv.h diff --git a/components/bootloader_support/include_bootloader/flash_qio_mode.h b/components/bootloader_support/include_bootloader/bootloader_flash_private/flash_qio_mode.h similarity index 100% rename from components/bootloader_support/include_bootloader/flash_qio_mode.h rename to components/bootloader_support/include_bootloader/bootloader_flash_private/flash_qio_mode.h diff --git a/components/bootloader_support/src/bootloader_flash.c b/components/bootloader_support/src/flash_support/bootloader_flash.c similarity index 99% rename from components/bootloader_support/src/bootloader_flash.c rename to components/bootloader_support/src/flash_support/bootloader_flash.c index 10d010f2c9..f17514e853 100644 --- a/components/bootloader_support/src/bootloader_flash.c +++ b/components/bootloader_support/src/flash_support/bootloader_flash.c @@ -509,10 +509,6 @@ esp_err_t IRAM_ATTR __attribute__((weak)) bootloader_flash_unlock(void) return err; } -/* dummy_len_plus values defined in ROM for SPI flash configuration */ -#ifndef g_rom_spiflash_dummy_len_plus // ESP32-C3 uses a macro to access ROM data here -extern uint8_t g_rom_spiflash_dummy_len_plus[]; -#endif IRAM_ATTR static uint32_t bootloader_flash_execute_command_common( uint8_t command, uint32_t addr_len, uint32_t address, diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32.c b/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32.c similarity index 98% rename from components/bootloader_support/src/bootloader_flash_config_esp32.c rename to components/bootloader_support/src/flash_support/bootloader_flash_config_esp32.c index 6b1546e9a2..3adb7ae9c1 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32.c +++ b/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32.c @@ -11,6 +11,7 @@ #include "esp_log.h" #include "esp_rom_gpio.h" #include "esp_rom_efuse.h" +#include "esp32/rom/spi_flash.h" #include "soc/gpio_periph.h" #include "soc/efuse_reg.h" #include "soc/spi_reg.h" @@ -20,7 +21,7 @@ #include "flash_qio_mode.h" #include "bootloader_common.h" #include "bootloader_flash_config.h" -#include "esp_rom_spiflash.h" + void bootloader_flash_update_id(void) { @@ -134,7 +135,6 @@ void IRAM_ATTR bootloader_flash_dummy_config(const esp_image_header_t* pfhdr) } } - extern uint8_t g_rom_spiflash_dummy_len_plus[]; switch (pfhdr->spi_speed) { case ESP_IMAGE_SPI_SPEED_80M: g_rom_spiflash_dummy_len_plus[0] = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_80M; diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32c3.c b/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32c3.c similarity index 98% rename from components/bootloader_support/src/bootloader_flash_config_esp32c3.c rename to components/bootloader_support/src/flash_support/bootloader_flash_config_esp32c3.c index e1e753578c..61ec2de7cc 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32c3.c +++ b/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32c3.c @@ -10,6 +10,7 @@ #include "esp_err.h" #include "esp_log.h" #include "esp32c3/rom/gpio.h" +#include "esp32c3/rom/spi_flash.h" #include "esp32c3/rom/efuse.h" #include "soc/gpio_periph.h" #include "soc/efuse_reg.h" @@ -19,7 +20,6 @@ #include "flash_qio_mode.h" #include "bootloader_flash_config.h" #include "bootloader_common.h" -#include "esp_rom_spiflash.h" #define FLASH_IO_MATRIX_DUMMY_40M 0 #define FLASH_IO_MATRIX_DUMMY_80M 0 diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32h2.c b/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32h2.c similarity index 98% rename from components/bootloader_support/src/bootloader_flash_config_esp32h2.c rename to components/bootloader_support/src/flash_support/bootloader_flash_config_esp32h2.c index 3851fe9bf7..547e86767f 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32h2.c +++ b/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32h2.c @@ -10,6 +10,7 @@ #include "esp_err.h" #include "esp_log.h" #include "esp32h2/rom/gpio.h" +#include "esp32h2/rom/spi_flash.h" #include "esp32h2/rom/efuse.h" #include "soc/gpio_periph.h" #include "soc/efuse_reg.h" @@ -19,7 +20,6 @@ #include "flash_qio_mode.h" #include "bootloader_flash_config.h" #include "bootloader_common.h" -#include "esp_rom_spiflash.h" #define FLASH_IO_MATRIX_DUMMY_40M 0 #define FLASH_IO_MATRIX_DUMMY_80M 0 diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32s2.c b/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32s2.c similarity index 98% rename from components/bootloader_support/src/bootloader_flash_config_esp32s2.c rename to components/bootloader_support/src/flash_support/bootloader_flash_config_esp32s2.c index 35847db9a8..1959b1b38e 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32s2.c +++ b/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32s2.c @@ -9,6 +9,7 @@ #include "sdkconfig.h" #include "esp_err.h" #include "esp_log.h" +#include "esp32s2/rom/spi_flash.h" #include "soc/efuse_reg.h" #include "soc/spi_reg.h" #include "soc/spi_mem_reg.h" @@ -16,7 +17,6 @@ #include "flash_qio_mode.h" #include "bootloader_flash_config.h" #include "bootloader_common.h" -#include "esp_rom_spiflash.h" #define FLASH_IO_MATRIX_DUMMY_40M 0 #define FLASH_IO_MATRIX_DUMMY_80M 0 diff --git a/components/bootloader_support/src/bootloader_flash_config_esp32s3.c b/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32s3.c similarity index 98% rename from components/bootloader_support/src/bootloader_flash_config_esp32s3.c rename to components/bootloader_support/src/flash_support/bootloader_flash_config_esp32s3.c index 4a36521540..357de775a5 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp32s3.c +++ b/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32s3.c @@ -9,6 +9,7 @@ #include "sdkconfig.h" #include "esp_err.h" #include "esp_log.h" +#include "esp32s3/rom/spi_flash.h" #include "soc/efuse_reg.h" #include "soc/spi_reg.h" #include "soc/spi_mem_reg.h" @@ -16,7 +17,6 @@ #include "flash_qio_mode.h" #include "bootloader_flash_config.h" #include "bootloader_common.h" -#include "esp_rom_spiflash.h" #define FLASH_IO_MATRIX_DUMMY_40M 0 #define FLASH_IO_MATRIX_DUMMY_80M 0 diff --git a/components/bootloader_support/src/bootloader_flash_config_esp8684.c b/components/bootloader_support/src/flash_support/bootloader_flash_config_esp8684.c similarity index 98% rename from components/bootloader_support/src/bootloader_flash_config_esp8684.c rename to components/bootloader_support/src/flash_support/bootloader_flash_config_esp8684.c index bf4603b038..27fbf9d5ee 100644 --- a/components/bootloader_support/src/bootloader_flash_config_esp8684.c +++ b/components/bootloader_support/src/flash_support/bootloader_flash_config_esp8684.c @@ -10,8 +10,8 @@ #include "esp_err.h" #include "esp_log.h" #include "esp8684/rom/gpio.h" +#include "esp8684/rom/spi_flash.h" #include "esp8684/rom/efuse.h" -#include "esp_rom_spiflash.h" #include "soc/gpio_periph.h" #include "soc/efuse_reg.h" #include "soc/spi_reg.h" diff --git a/components/bootloader_support/src/flash_qio_mode.c b/components/bootloader_support/src/flash_support/flash_qio_mode.c similarity index 100% rename from components/bootloader_support/src/flash_qio_mode.c rename to components/bootloader_support/src/flash_support/flash_qio_mode.c diff --git a/components/efuse/src/esp_efuse_utility.c b/components/efuse/src/esp_efuse_utility.c index 319bebaa7b..3803960ea5 100644 --- a/components/efuse/src/esp_efuse_utility.c +++ b/components/efuse/src/esp_efuse_utility.c @@ -404,7 +404,7 @@ void esp_efuse_init_virtual_mode_in_ram(void) #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH -#include "../include_bootloader/bootloader_flash_priv.h" +#include "../include_bootloader/bootloader_flash_private/bootloader_flash_priv.h" static uint32_t esp_efuse_flash_offset = 0; static uint32_t esp_efuse_flash_size = 0; diff --git a/components/esp_hw_support/port/esp32/spiram_psram.c b/components/esp_hw_support/port/esp32/spiram_psram.c index 8865ea693c..35ca3b90e7 100644 --- a/components/esp_hw_support/port/esp32/spiram_psram.c +++ b/components/esp_hw_support/port/esp32/spiram_psram.c @@ -17,6 +17,7 @@ #include "esp_log.h" #include "esp_efuse.h" #include "spiram_psram.h" +#include "esp32/rom/spi_flash.h" #include "esp32/rom/cache.h" #include "esp32/rom/efuse.h" #include "esp_rom_efuse.h" @@ -30,7 +31,6 @@ #include "bootloader_common.h" #include "esp_rom_gpio.h" #include "bootloader_flash_config.h" -#include "esp_rom_spiflash.h" #if CONFIG_SPIRAM #include "soc/rtc.h" diff --git a/components/esp_hw_support/port/esp32s2/spiram_psram.c b/components/esp_hw_support/port/esp32s2/spiram_psram.c index 2a59d2e844..886e08b045 100644 --- a/components/esp_hw_support/port/esp32s2/spiram_psram.c +++ b/components/esp_hw_support/port/esp32s2/spiram_psram.c @@ -16,6 +16,7 @@ #include "esp_types.h" #include "esp_log.h" #include "spiram_psram.h" +#include "esp32s2/rom/spi_flash.h" #include "esp32s2/rom/opi_flash.h" #include "esp32s2/rom/cache.h" #include "esp32s2/rom/efuse.h" @@ -32,7 +33,6 @@ #include "driver/spi_common.h" #include "esp_private/periph_ctrl.h" #include "bootloader_common.h" -#include "esp_rom_spiflash.h" #if CONFIG_SPIRAM #include "soc/rtc.h" diff --git a/components/esp_hw_support/port/esp32s3/opiram_psram.c b/components/esp_hw_support/port/esp32s3/opiram_psram.c index 6d75d6be8d..efcecdbc6b 100644 --- a/components/esp_hw_support/port/esp32s3/opiram_psram.c +++ b/components/esp_hw_support/port/esp32s3/opiram_psram.c @@ -12,6 +12,7 @@ #include "esp_log.h" #include "spiram_psram.h" #include "esp32s3/rom/ets_sys.h" +#include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/opi_flash.h" #include "esp32s3/rom/gpio.h" #include "esp32s3/rom/cache.h" @@ -23,7 +24,6 @@ #include "driver/gpio.h" #include "driver/spi_common.h" #include "esp_private/periph_ctrl.h" -#include "esp_rom_spiflash.h" #if CONFIG_SPIRAM_MODE_OCT #include "soc/rtc.h" diff --git a/components/esp_hw_support/port/esp32s3/spiram_psram.c b/components/esp_hw_support/port/esp32s3/spiram_psram.c index 2ad72b7e81..9ca43271ec 100644 --- a/components/esp_hw_support/port/esp32s3/spiram_psram.c +++ b/components/esp_hw_support/port/esp32s3/spiram_psram.c @@ -16,6 +16,7 @@ #include "esp_types.h" #include "esp_log.h" #include "spiram_psram.h" +#include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/opi_flash.h" #include "esp32s3/rom/cache.h" #include "esp32s3/rom/efuse.h" @@ -34,7 +35,6 @@ #include "driver/spi_common.h" #include "esp_private/periph_ctrl.h" #include "bootloader_common.h" -#include "esp_rom_spiflash.h" #if CONFIG_SPIRAM_MODE_QUAD #include "soc/rtc.h" diff --git a/components/esp_rom/include/esp32/rom/spi_flash.h b/components/esp_rom/include/esp32/rom/spi_flash.h index cfc2976f64..8a2e17ef36 100644 --- a/components/esp_rom/include/esp32/rom/spi_flash.h +++ b/components/esp_rom/include/esp32/rom/spi_flash.h @@ -4,15 +4,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -#ifndef _ROM_SPI_FLASH_H_ -#define _ROM_SPI_FLASH_H_ +#pragma once #include #include - #include "esp_attr.h" - #include "sdkconfig.h" +#include "esp_rom_spiflash.h" #ifdef CONFIG_LEGACY_INCLUDE_COMMON_HEADERS #include "soc/spi_reg.h" @@ -115,20 +113,72 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 -typedef enum { - ESP_ROM_SPIFLASH_RESULT_OK, - ESP_ROM_SPIFLASH_RESULT_ERR, - ESP_ROM_SPIFLASH_RESULT_TIMEOUT -} esp_rom_spiflash_result_t; - -extern uint8_t g_rom_spiflash_dummy_len_plus[]; +/** + * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. + * Please do not call this function in SDK. + * + * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). + * + * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. + * + * @return None + */ +void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); /** - * @} + * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None */ +void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); + +/** + * @brief Set SPI Flash pad drivers. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid + * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. + * Values usually read from falsh by rom code, function usually callde by rom code. + * if value with bit(3) set, the value is valid, bit[2:0] is the real value. + * + * @return None + */ +void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); + +/** + * @brief Select SPI Flash function for pads. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None + */ +void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); + +/** + * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. + * + * @return uint16_t 0 : do not send command any more. + * 1 : go to the next command. + * n > 1 : skip (n - 1) commands. + */ +uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); #ifdef __cplusplus } #endif - -#endif /* _ROM_SPI_FLASH_H_ */ diff --git a/components/esp_rom/include/esp32c3/rom/spi_flash.h b/components/esp_rom/include/esp32c3/rom/spi_flash.h index 291f581097..76c67e5025 100644 --- a/components/esp_rom/include/esp32c3/rom/spi_flash.h +++ b/components/esp_rom/include/esp32c3/rom/spi_flash.h @@ -4,13 +4,12 @@ * SPDX-License-Identifier: Apache-2.0 */ -#ifndef _ROM_SPI_FLASH_H_ -#define _ROM_SPI_FLASH_H_ +#pragma once #include #include - #include "esp_attr.h" +#include "esp_rom_spiflash.h" #ifdef __cplusplus extern "C" { @@ -70,12 +69,6 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 -typedef enum { - ESP_ROM_SPIFLASH_RESULT_OK, - ESP_ROM_SPIFLASH_RESULT_ERR, - ESP_ROM_SPIFLASH_RESULT_TIMEOUT -} esp_rom_spiflash_result_t; - typedef void (* spi_flash_func_t)(void); typedef esp_rom_spiflash_result_t (* spi_flash_op_t)(void); typedef esp_rom_spiflash_result_t (* spi_flash_erase_t)(uint32_t); @@ -98,8 +91,72 @@ typedef struct { spi_flash_op_t wait_idle; } spiflash_legacy_funcs_t; +/** + * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. + * Please do not call this function in SDK. + * + * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). + * + * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. + * + * @return None + */ +void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); + +/** + * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None + */ +void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); + +/** + * @brief Set SPI Flash pad drivers. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid + * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. + * Values usually read from falsh by rom code, function usually callde by rom code. + * if value with bit(3) set, the value is valid, bit[2:0] is the real value. + * + * @return None + */ +void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); + +/** + * @brief Select SPI Flash function for pads. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None + */ +void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); + +/** + * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. + * + * @return uint16_t 0 : do not send command any more. + * 1 : go to the next command. + * n > 1 : skip (n - 1) commands. + */ +uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); + #ifdef __cplusplus } #endif - -#endif /* _ROM_SPI_FLASH_H_ */ diff --git a/components/esp_rom/include/esp32h2/rom/spi_flash.h b/components/esp_rom/include/esp32h2/rom/spi_flash.h index fb1faabe98..fcf4308fbc 100644 --- a/components/esp_rom/include/esp32h2/rom/spi_flash.h +++ b/components/esp_rom/include/esp32h2/rom/spi_flash.h @@ -4,13 +4,12 @@ * SPDX-License-Identifier: Apache-2.0 */ -#ifndef _ROM_SPI_FLASH_H_ -#define _ROM_SPI_FLASH_H_ +#pragma once #include #include - #include "esp_attr.h" +#include "esp_rom_spiflash.h" #ifdef __cplusplus extern "C" { @@ -70,12 +69,6 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 -typedef enum { - ESP_ROM_SPIFLASH_RESULT_OK, - ESP_ROM_SPIFLASH_RESULT_ERR, - ESP_ROM_SPIFLASH_RESULT_TIMEOUT -} esp_rom_spiflash_result_t; - typedef void (* spi_flash_func_t)(void); typedef esp_rom_spiflash_result_t (* spi_flash_op_t)(void); typedef esp_rom_spiflash_result_t (* spi_flash_erase_t)(uint32_t); @@ -83,8 +76,13 @@ typedef esp_rom_spiflash_result_t (* spi_flash_rd_t)(uint32_t, uint32_t*, int); typedef esp_rom_spiflash_result_t (* spi_flash_wr_t)(uint32_t, const uint32_t*, int); typedef esp_rom_spiflash_result_t (* spi_flash_ewr_t)(uint32_t, const void*, uint32_t); typedef esp_rom_spiflash_result_t (* spi_flash_wren_t)(void*); +typedef esp_rom_spiflash_result_t (* spi_flash_erase_area_t)(uint32_t, uint32_t); typedef struct { + uint8_t pp_addr_bit_len; + uint8_t se_addr_bit_len; + uint8_t be_addr_bit_len; + uint8_t rd_addr_bit_len; uint32_t read_sub_len; uint32_t write_sub_len; spi_flash_op_t unlock; @@ -96,18 +94,75 @@ typedef struct { spi_flash_func_t check_sus; spi_flash_wren_t wren; spi_flash_op_t wait_idle; + spi_flash_erase_area_t erase_area; } spiflash_legacy_funcs_t; - -extern const spiflash_legacy_funcs_t *rom_spiflash_legacy_funcs; - +/** + * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. + * Please do not call this function in SDK. + * + * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). + * + * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. + * + * @return None + */ +void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); /** - * @} + * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None */ +void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); + +/** + * @brief Set SPI Flash pad drivers. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid + * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. + * Values usually read from falsh by rom code, function usually callde by rom code. + * if value with bit(3) set, the value is valid, bit[2:0] is the real value. + * + * @return None + */ +void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); + +/** + * @brief Select SPI Flash function for pads. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None + */ +void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); + +/** + * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. + * + * @return uint16_t 0 : do not send command any more. + * 1 : go to the next command. + * n > 1 : skip (n - 1) commands. + */ +uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); #ifdef __cplusplus } #endif - -#endif /* _ROM_SPI_FLASH_H_ */ diff --git a/components/esp_rom/include/esp32s2/rom/opi_flash.h b/components/esp_rom/include/esp32s2/rom/opi_flash.h index 1c2392e354..bb209f67f0 100644 --- a/components/esp_rom/include/esp32s2/rom/opi_flash.h +++ b/components/esp_rom/include/esp32s2/rom/opi_flash.h @@ -8,7 +8,7 @@ #include #include #include -#include "esp_rom_spiflash.h" +#include "spi_flash.h" #ifdef __cplusplus extern "C" { diff --git a/components/esp_rom/include/esp32s2/rom/spi_flash.h b/components/esp_rom/include/esp32s2/rom/spi_flash.h index 45ee6bb704..2b65be5e12 100644 --- a/components/esp_rom/include/esp32s2/rom/spi_flash.h +++ b/components/esp_rom/include/esp32s2/rom/spi_flash.h @@ -4,8 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#ifndef _ROM_SPI_FLASH_H_ -#define _ROM_SPI_FLASH_H_ +#pragma once #ifndef CONFIG_IDF_TARGET_ESP32S2 #error This file should only be included for ESP32-S2 target @@ -13,9 +12,9 @@ #include #include - #include "esp_attr.h" #include "soc/spi_mem_reg.h" +#include "esp_rom_spiflash.h" #ifdef __cplusplus extern "C" { @@ -106,20 +105,72 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 -typedef enum { - ESP_ROM_SPIFLASH_RESULT_OK, - ESP_ROM_SPIFLASH_RESULT_ERR, - ESP_ROM_SPIFLASH_RESULT_TIMEOUT -} esp_rom_spiflash_result_t; - -extern uint8_t g_rom_spiflash_dummy_len_plus[]; +/** + * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. + * Please do not call this function in SDK. + * + * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). + * + * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. + * + * @return None + */ +void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); /** - * @} + * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None */ +void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); + +/** + * @brief Set SPI Flash pad drivers. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid + * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. + * Values usually read from falsh by rom code, function usually callde by rom code. + * if value with bit(3) set, the value is valid, bit[2:0] is the real value. + * + * @return None + */ +void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); + +/** + * @brief Select SPI Flash function for pads. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None + */ +void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); + +/** + * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. + * + * @return uint16_t 0 : do not send command any more. + * 1 : go to the next command. + * n > 1 : skip (n - 1) commands. + */ +uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); #ifdef __cplusplus } #endif - -#endif /* _ROM_SPI_FLASH_H_ */ diff --git a/components/esp_rom/include/esp32s3/rom/opi_flash.h b/components/esp_rom/include/esp32s3/rom/opi_flash.h index 67feabb600..8976e7b53e 100644 --- a/components/esp_rom/include/esp32s3/rom/opi_flash.h +++ b/components/esp_rom/include/esp32s3/rom/opi_flash.h @@ -9,7 +9,7 @@ #include #include #include -#include "esp_rom_spiflash.h" +#include "spi_flash.h" #ifdef __cplusplus extern "C" { diff --git a/components/esp_rom/include/esp32s3/rom/spi_flash.h b/components/esp_rom/include/esp32s3/rom/spi_flash.h index 832ad2496e..4f16106cee 100644 --- a/components/esp_rom/include/esp32s3/rom/spi_flash.h +++ b/components/esp_rom/include/esp32s3/rom/spi_flash.h @@ -8,6 +8,7 @@ #include #include #include "esp_attr.h" +#include "esp_rom_spiflash.h" #ifdef __cplusplus extern "C" { @@ -98,13 +99,6 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 - -typedef enum { - ESP_ROM_SPIFLASH_RESULT_OK, - ESP_ROM_SPIFLASH_RESULT_ERR, - ESP_ROM_SPIFLASH_RESULT_TIMEOUT -} esp_rom_spiflash_result_t; - typedef void (*spi_flash_func_t)(void); typedef esp_rom_spiflash_result_t (*spi_flash_op_t)(void); typedef esp_rom_spiflash_result_t (*spi_flash_erase_t)(uint32_t); @@ -133,6 +127,72 @@ typedef struct { spi_flash_erase_area_t erase_area; } spiflash_legacy_funcs_t; +/** + * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. + * Please do not call this function in SDK. + * + * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). + * + * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. + * + * @return None + */ +void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); + +/** + * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None + */ +void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); + +/** + * @brief Set SPI Flash pad drivers. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid + * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. + * Values usually read from falsh by rom code, function usually callde by rom code. + * if value with bit(3) set, the value is valid, bit[2:0] is the real value. + * + * @return None + */ +void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); + +/** + * @brief Select SPI Flash function for pads. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None + */ +void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); + +/** + * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. + * + * @return uint16_t 0 : do not send command any more. + * 1 : go to the next command. + * n > 1 : skip (n - 1) commands. + */ +uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); + #ifdef __cplusplus } #endif diff --git a/components/esp_rom/include/esp8684/rom/spi_flash.h b/components/esp_rom/include/esp8684/rom/spi_flash.h index 291f581097..fcf4308fbc 100644 --- a/components/esp_rom/include/esp8684/rom/spi_flash.h +++ b/components/esp_rom/include/esp8684/rom/spi_flash.h @@ -4,13 +4,12 @@ * SPDX-License-Identifier: Apache-2.0 */ -#ifndef _ROM_SPI_FLASH_H_ -#define _ROM_SPI_FLASH_H_ +#pragma once #include #include - #include "esp_attr.h" +#include "esp_rom_spiflash.h" #ifdef __cplusplus extern "C" { @@ -70,12 +69,6 @@ extern "C" { #define FLASH_ID_GD25LQ32C 0xC86016 -typedef enum { - ESP_ROM_SPIFLASH_RESULT_OK, - ESP_ROM_SPIFLASH_RESULT_ERR, - ESP_ROM_SPIFLASH_RESULT_TIMEOUT -} esp_rom_spiflash_result_t; - typedef void (* spi_flash_func_t)(void); typedef esp_rom_spiflash_result_t (* spi_flash_op_t)(void); typedef esp_rom_spiflash_result_t (* spi_flash_erase_t)(uint32_t); @@ -83,8 +76,13 @@ typedef esp_rom_spiflash_result_t (* spi_flash_rd_t)(uint32_t, uint32_t*, int); typedef esp_rom_spiflash_result_t (* spi_flash_wr_t)(uint32_t, const uint32_t*, int); typedef esp_rom_spiflash_result_t (* spi_flash_ewr_t)(uint32_t, const void*, uint32_t); typedef esp_rom_spiflash_result_t (* spi_flash_wren_t)(void*); +typedef esp_rom_spiflash_result_t (* spi_flash_erase_area_t)(uint32_t, uint32_t); typedef struct { + uint8_t pp_addr_bit_len; + uint8_t se_addr_bit_len; + uint8_t be_addr_bit_len; + uint8_t rd_addr_bit_len; uint32_t read_sub_len; uint32_t write_sub_len; spi_flash_op_t unlock; @@ -96,10 +94,75 @@ typedef struct { spi_flash_func_t check_sus; spi_flash_wren_t wren; spi_flash_op_t wait_idle; + spi_flash_erase_area_t erase_area; } spiflash_legacy_funcs_t; +/** + * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. + * Please do not call this function in SDK. + * + * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). + * + * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. + * + * @return None + */ +void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); + +/** + * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None + */ +void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); + +/** + * @brief Set SPI Flash pad drivers. + * Please do not call this function in SDK. + * + * @param uint8_t wp_gpio_num: WP gpio number. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid + * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. + * Values usually read from falsh by rom code, function usually callde by rom code. + * if value with bit(3) set, the value is valid, bit[2:0] is the real value. + * + * @return None + */ +void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); + +/** + * @brief Select SPI Flash function for pads. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @return None + */ +void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); + +/** + * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. + * + * @return uint16_t 0 : do not send command any more. + * 1 : go to the next command. + * n > 1 : skip (n - 1) commands. + */ +uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); + #ifdef __cplusplus } #endif - -#endif /* _ROM_SPI_FLASH_H_ */ diff --git a/components/esp_rom/include/esp_rom_spiflash.h b/components/esp_rom/include/esp_rom_spiflash.h index aa5aaf05ce..4f5c40f847 100644 --- a/components/esp_rom/include/esp_rom_spiflash.h +++ b/components/esp_rom/include/esp_rom_spiflash.h @@ -7,36 +7,14 @@ #pragma once -#ifdef __cplusplus -extern "C" { -#endif - #include "sdkconfig.h" #include #include -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP8684 -#include "esp32h2/rom/spi_flash.h" +#ifdef __cplusplus +extern "C" { #endif -/** \defgroup spi_flash_apis, spi flash operation related apis - * @brief spi_flash apis - */ - -/** @addtogroup spi_flash_apis - * @{ - */ - typedef enum { ESP_ROM_SPIFLASH_QIO_MODE = 0, ESP_ROM_SPIFLASH_QOUT_MODE, @@ -69,59 +47,11 @@ typedef struct { uint16_t data; } esp_rom_spiflash_common_cmd_t; -/** - * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. - * Please do not call this function in SDK. - * - * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write). - * - * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M. - * - * @return None - */ -void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv); - -/** - * @brief Select SPI Flash to QIO mode when WP pad is read from Flash. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi); - -/** - * @brief Set SPI Flash pad drivers. - * Please do not call this function in SDK. - * - * @param uint8_t wp_gpio_num: WP gpio number. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid - * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp. - * Values usually read from falsh by rom code, function usually callde by rom code. - * if value with bit(3) set, the value is valid, bit[2:0] is the real value. - * - * @return None - */ -void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs); - -/** - * @brief Select SPI Flash function for pads. - * Please do not call this function in SDK. - * - * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping - * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd - * - * @return None - */ -void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); +typedef enum { + ESP_ROM_SPIFLASH_RESULT_OK, + ESP_ROM_SPIFLASH_RESULT_ERR, + ESP_ROM_SPIFLASH_RESULT_TIMEOUT +} esp_rom_spiflash_result_t; /** * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. @@ -130,7 +60,7 @@ void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd * - * @param uint8_t legacy: In legacy mode, more SPI command is used in line. + * @param uint8_t legacy: always keeping false. * * @return None */ @@ -220,18 +150,6 @@ esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read */ esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); -/** - * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD. - * Please do not call this function in SDK. - * - * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command. - * - * @return uint16_t 0 : do not send command any more. - * 1 : go to the next command. - * n > 1 : skip (n - 1) commands. - */ -uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); - /** * @brief Unlock SPI write protect. * Please do not call this function in SDK. @@ -460,14 +378,15 @@ typedef struct { esp_rom_spiflash_chip_t chip; uint8_t dummy_len_plus[3]; uint8_t sig_matrix; -} spiflash_legacy_data_t; +} esp_rom_spiflash_legacy_data_t; /* Flash data defined in ROM*/ #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 extern esp_rom_spiflash_chip_t g_rom_flashchip; +extern uint8_t g_rom_spiflash_dummy_len_plus[]; #else -extern spiflash_legacy_data_t *rom_spiflash_legacy_data; +extern esp_rom_spiflash_legacy_data_t *rom_spiflash_legacy_data; #define g_rom_flashchip (rom_spiflash_legacy_data->chip) #define g_rom_spiflash_dummy_len_plus (rom_spiflash_legacy_data->dummy_len_plus) #endif diff --git a/components/esp_rom/patches/esp_rom_spiflash.c b/components/esp_rom/patches/esp_rom_spiflash.c index d9e6cf4822..a4fd608670 100644 --- a/components/esp_rom/patches/esp_rom_spiflash.c +++ b/components/esp_rom/patches/esp_rom_spiflash.c @@ -5,18 +5,21 @@ */ #include "sdkconfig.h" -#include "esp_rom_spiflash.h" #include "soc/spi_periph.h" -#include "esp_rom_spiflash.h" +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rom/spi_flash.h" +#endif #define SPI_IDX 1 -extern esp_rom_spiflash_chip_t g_rom_spiflash_chip; - #if CONFIG_SPI_FLASH_ROM_DRIVER_PATCH #if CONFIG_IDF_TARGET_ESP32 +extern esp_rom_spiflash_chip_t g_rom_spiflash_chip; + static inline bool is_issi_chip(const esp_rom_spiflash_chip_t* chip) { return (((chip->device_id >> 16)&0xff) == 0x9D); diff --git a/components/fatfs/test_fatfs_host/Makefile.files b/components/fatfs/test_fatfs_host/Makefile.files index 7e7da5bd95..baa9597b70 100644 --- a/components/fatfs/test_fatfs_host/Makefile.files +++ b/components/fatfs/test_fatfs_host/Makefile.files @@ -35,6 +35,7 @@ INCLUDE_DIRS := \ esp32/include \ esp_common/include \ bootloader_support/include \ + bootloader_support/bootloader_flash/include \ app_update/include \ hal/include \ spi_flash/include \ diff --git a/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c b/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c index 8f25c714b2..a725528513 100644 --- a/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c +++ b/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c @@ -9,7 +9,7 @@ #include "esp_err.h" #include "esp_rom_gpio.h" #include "esp32s3/rom/gpio.h" -#include "esp_rom_spiflash.h" +#include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/opi_flash.h" #include "esp_private/spi_flash_os.h" #include "opi_flash_private.h" diff --git a/components/spi_flash/esp32s3/spi_timing_config.h b/components/spi_flash/esp32s3/spi_timing_config.h index ed6f103308..06af783c9f 100644 --- a/components/spi_flash/esp32s3/spi_timing_config.h +++ b/components/spi_flash/esp32s3/spi_timing_config.h @@ -7,7 +7,7 @@ #pragma once #include "esp_flash_partitions.h" -#include "esp_rom_spiflash.h" +#include "esp32s3/rom/spi_flash.h" #include "esp32s3/rom/opi_flash.h" #include "mspi_timing_tuning_configs.h" diff --git a/components/spi_flash/flash_ops.c b/components/spi_flash/flash_ops.c index db39129f9f..90bcd93fe4 100644 --- a/components/spi_flash/flash_ops.c +++ b/components/spi_flash/flash_ops.c @@ -24,6 +24,7 @@ #include "esp_private/esp_clk.h" #if CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/cache.h" +#include "esp32/rom/spi_flash.h" #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/rom/cache.h" #elif CONFIG_IDF_TARGET_ESP32S3 diff --git a/components/spi_flash/include/esp_private/spi_flash_os.h b/components/spi_flash/include/esp_private/spi_flash_os.h index 952a6ed544..9332f001bd 100644 --- a/components/spi_flash/include/esp_private/spi_flash_os.h +++ b/components/spi_flash/include/esp_private/spi_flash_os.h @@ -19,10 +19,10 @@ */ #pragma once -#include "esp_rom_spiflash.h" #include #include #include "sdkconfig.h" +#include "esp_rom_spiflash.h" #include "esp_err.h" #include "esp_flash.h" #include "hal/spi_flash_hal.h" diff --git a/components/spi_flash/sim/Makefile.files b/components/spi_flash/sim/Makefile.files index 336e63895e..d9c7ae62e2 100644 --- a/components/spi_flash/sim/Makefile.files +++ b/components/spi_flash/sim/Makefile.files @@ -37,6 +37,7 @@ INCLUDE_DIRS := \ esp32/include \ esp_timer/include \ bootloader_support/include \ + bootloader_support/bootloader_flash/include \ app_update/include \ hal/include \ hal/esp32/include \ diff --git a/components/spi_flash/sim/stubs/Makefile.files b/components/spi_flash/sim/stubs/Makefile.files index 82ff75c1e2..a241826811 100644 --- a/components/spi_flash/sim/stubs/Makefile.files +++ b/components/spi_flash/sim/stubs/Makefile.files @@ -31,6 +31,7 @@ INCLUDE_DIRS := \ esp32/include \ esp_timer/include \ bootloader_support/include \ + bootloader_support/bootloader_flash/include \ app_update/include \ hal/include \ spi_flash/include \ diff --git a/components/spi_flash/test/test_read_write.c b/components/spi_flash/test/test_read_write.c index e31c94ff36..4ead359e71 100644 --- a/components/spi_flash/test/test_read_write.c +++ b/components/spi_flash/test/test_read_write.c @@ -18,7 +18,19 @@ #include "../cache_utils.h" #include "soc/timer_periph.h" #include "esp_heap_caps.h" -#include "esp_rom_spiflash.h" +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32C3 +#include "esp32c3/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32H2 +#include "esp32h2/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP8684 +#include "esp8684/rom/spi_flash.h" +#endif #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP8684) // TODO: SPI_FLASH IDF-4025 diff --git a/components/spiffs/test_spiffs_host/Makefile.files b/components/spiffs/test_spiffs_host/Makefile.files index 8104f58312..b8269837c5 100644 --- a/components/spiffs/test_spiffs_host/Makefile.files +++ b/components/spiffs/test_spiffs_host/Makefile.files @@ -35,6 +35,7 @@ INCLUDE_DIRS := \ soc/include \ esp32/include \ bootloader_support/include \ + bootloader_support/bootloader_flash/include \ app_update/include \ spi_flash/include \ hal/include \ diff --git a/components/wear_levelling/test_wl_host/Makefile.files b/components/wear_levelling/test_wl_host/Makefile.files index d31308b41a..3b010634c3 100644 --- a/components/wear_levelling/test_wl_host/Makefile.files +++ b/components/wear_levelling/test_wl_host/Makefile.files @@ -35,6 +35,7 @@ INCLUDE_DIRS := \ soc/include \ esp32/include \ bootloader_support/include \ + bootloader_support/bootloader_flash/include \ app_update/include \ hal/include \ spi_flash/include \ diff --git a/docs/en/migration-guides/peripherals.rst b/docs/en/migration-guides/peripherals.rst index 357df1d5b6..7ef2fc990b 100644 --- a/docs/en/migration-guides/peripherals.rst +++ b/docs/en/migration-guides/peripherals.rst @@ -7,3 +7,8 @@ Peripheral Clock Gating As usual, peripheral clock gating is still handled by driver itself, users don't need to take care of the peripheral module clock gating. However, for advanced users who implement their own drivers based on ``hal`` and ``soc`` components, the previous clock gating include path has been changed from ``driver/periph_ctrl.h`` to ``esp_private/periph_ctrl.h``. + +SPI Flash Interface +------------------- + +Version before v5.0, spi flash functions in rom can be included by ``esp32**/rom/spi_flash.h``. However, it duplicates so much and not clean. Therefore, it has been moved to ``esp_rom_spiflash.h``. If you want to use the functions with prefix ``esp_rom_spiflash`` , please include ``esp_rom_spiflash.h`` From 8e220f7bbe55349c7ed7bea4ab43c477f02919cb Mon Sep 17 00:00:00 2001 From: morris Date: Thu, 25 Nov 2021 13:58:03 +0800 Subject: [PATCH 4/5] esp_rom: add README to describe the component layout --- components/esp_rom/README.md | 78 ++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 components/esp_rom/README.md diff --git a/components/esp_rom/README.md b/components/esp_rom/README.md new file mode 100644 index 0000000000..74fe101978 --- /dev/null +++ b/components/esp_rom/README.md @@ -0,0 +1,78 @@ +# `esp_rom` Component + +## Function Description + +`esp_rom` component contains each chip's ROM functions, which are used in the ROM bootloader, 2nd bootloader, esp_tool flash stub and some driver code (e.g. GPIO matrix). ROM functions as not treated as public APIs, attentions are required when you use ROM functions: + +1. ROM functions are **not** thread-safe in RTOS, extra locks are needed to be around the ROM functions. +2. There're risks for ROM functions that their names/signatures/behaviors being changed in different chips. +3. ROM functions are not guaranteed to be exist across all chips. + +When using ROM functions in esp-idf, the including convention is `/rom/.h`. This can prevent you from using a nonexistent ROM function for specific ``. Thus ROM functions are recommended to be used in a target-specific source file. For example, `bootloader_esp32.c` can include `esp32/rom/.h` without any violations. However, this is not the case when it comes to a common source file who also want to use some of ROM functions. The including list would be tortuous like: + +```c +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rom/uart.h" +#elif CONFIG_IDF_TARGET_ESP32C3 +#include "esp32c3/rom/uart.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/uart.h" +#elif CONFIG_IDF_TARGET_ESP32H2 +#include "esp32h2/rom/uart.h" +... +``` + +So, we added a wrapper for those commonly used ROM functions. They're declared in `esp_rom/include/esp_rom_xxx.h`. Unlike the original ROM functions, these extracted ones are expected to be exist across all chips. If some of them are missed in the new chips, we will implement them again in `esp_rom/patches`. These ROM apis are always prefixed with the name `esp_rom` (e.g. `esp_rom_printf`), so that it's obvious to know whether a function is linked to ROM. + +Most of the time, the ROM wrapper APIs are just alias to the original ROM functions by linker script `esp_rom//ld/.rom.api.ld`. For example, `esp_rom_printf` is alias to `ets_printf` in the following way: + +``` +PROVIDE ( esp_rom_printf = ets_printf ); +``` + +If some original ROM functions have changed the behavior or have bugs, we should override it in the wrapper layer. A common example is the `esp_rom_install_uart_printf()`, on ESP32 and ESP32S2, it's just alias to `ets_install_uart_printf`, but on other chips, it's re-implemented in the `esp_rom/patches/esp_rom_uart.c`. To some extent, the ROM wrapper layer works like an anti-corrosion layer between esp-rom project and esp-idf project. + +As ROM functions are unique to each target, features are as well. For example, ESP32 has built the `tjpgd` library into the ROM, but ESP32S2 doesn't. We have a header file declaring the features: `esp_rom//esp_rom_caps.h` that supported by each target. Based on the macros defined there, we can decide whether a function should be patched or whether a feature should be re-implemented. + +## Directory Structure + +``` +. +├── CMakeLists.txt +├── +│   ├── esp_rom_caps.h +│   └── ld +│   ├── .rom.api.ld +│   ├── .rom.ld +│   ├── .rom.libgcc.ld +│   ├── .rom.newlib.ld +│   ├── .rom.newlib-nano.ld +│   ├── .rom.version.ld +│   └── ... // other ROM linker scripts, added when bring up new chip +├── include +│   ├── +│   │   └── rom +│   │   ├── cache.h +│   │   ├── efuse.h +│   │   ├── esp_flash.h +│   │   ├── ets_sys.h +│   │   ├── gpio.h +│   │   ├── uart.h +│   │   └── ... // other original ROM header files, added when bring up new chip +│   ├── esp_rom_gpio.h +│   ├── esp_rom_md5.h +│   ├── esp_rom_sys.h +│   ├── esp_rom_uart.h +│   └── ... // other ROM wrapper api files +├── Kconfig.projbuild +├── linker.lf +├── patches +│   ├── esp_rom_sys.c +│   ├── esp_rom_uart.c +│   └── ... // other patched source files +├── README.md +└── test + ├── CMakeLists.txt + ├── test_miniz.c + └── ... // other ROM function unit tests +``` \ No newline at end of file From d397464fc4cb51f7928364111a3b4f306290540a Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Fri, 26 Nov 2021 16:04:49 +0800 Subject: [PATCH 5/5] spi_flash: refactor spi_flash.h to esp_rom_spiflash.h but keep the content in spi_flash.h --- components/app_update/test/test_switch_ota.c | 2 +- components/bootloader_support/CMakeLists.txt | 10 +- .../include/bootloader_flash_override.h | 0 .../include}/bootloader_flash_priv.h | 8 + .../include}/flash_qio_mode.h | 2 + .../src}/bootloader_flash.c | 0 .../src}/bootloader_flash_config_esp32.c | 1 - .../src}/bootloader_flash_config_esp32c3.c | 0 .../src}/bootloader_flash_config_esp32h2.c | 0 .../src}/bootloader_flash_config_esp32s2.c | 0 .../src}/bootloader_flash_config_esp32s3.c | 0 .../src}/bootloader_flash_config_esp8684.c | 0 .../src}/flash_qio_mode.c | 0 .../src/esp8684/bootloader_esp8684.c | 2 +- components/efuse/src/esp_efuse_utility.c | 2 +- components/esp_rom/README.md | 12 +- components/esp_rom/esp32/ld/esp32.rom.api.ld | 3 + components/esp_rom/esp32/ld/esp32.rom.ld | 1 + .../esp_rom/esp32c3/ld/esp32c3.rom.api.ld | 3 + .../esp32h2/ld/rev1/esp32h2.rom.api.ld | 3 + .../esp32h2/ld/rev2/esp32h2.rom.api.ld | 3 + .../esp_rom/esp32s2/ld/esp32s2.rom.api.ld | 3 + .../esp_rom/esp32s3/ld/esp32s3.rom.api.ld | 2 + .../esp_rom/esp8684/ld/esp8684.rom.api.ld | 2 + .../esp_rom/include/esp32/rom/spi_flash.h | 361 +++++++++++++++++- .../esp_rom/include/esp32c3/rom/spi_flash.h | 352 ++++++++++++++++- .../esp_rom/include/esp32h2/rom/spi_flash.h | 352 ++++++++++++++++- .../esp_rom/include/esp32s2/rom/spi_flash.h | 346 ++++++++++++++++- .../esp_rom/include/esp32s3/rom/spi_flash.h | 351 ++++++++++++++++- .../esp_rom/include/esp8684/rom/spi_flash.h | 352 ++++++++++++++++- components/esp_rom/include/esp_rom_spiflash.h | 55 +-- .../esp_rom/include/esp_rom_spiflash_defs.h | 31 ++ components/esp_rom/patches/esp_rom_spiflash.c | 37 +- .../esp32s3/spi_flash_oct_flash_init.c | 5 +- components/spi_flash/sim/SpiFlash.h | 1 - components/spi_flash/sim/flash_mock.cpp | 1 - components/spi_flash/test/test_read_write.c | 12 +- components/spi_flash/test/test_spi_flash.c | 2 +- docs/en/migration-guides/peripherals.rst | 6 +- 39 files changed, 2171 insertions(+), 152 deletions(-) rename components/bootloader_support/{ => bootloader_flash}/include/bootloader_flash_override.h (100%) rename components/bootloader_support/{include_bootloader/bootloader_flash_private => bootloader_flash/include}/bootloader_flash_priv.h (98%) rename components/bootloader_support/{include_bootloader/bootloader_flash_private => bootloader_flash/include}/flash_qio_mode.h (96%) rename components/bootloader_support/{src/flash_support => bootloader_flash/src}/bootloader_flash.c (100%) rename components/bootloader_support/{src/flash_support => bootloader_flash/src}/bootloader_flash_config_esp32.c (99%) rename components/bootloader_support/{src/flash_support => bootloader_flash/src}/bootloader_flash_config_esp32c3.c (100%) rename components/bootloader_support/{src/flash_support => bootloader_flash/src}/bootloader_flash_config_esp32h2.c (100%) rename components/bootloader_support/{src/flash_support => bootloader_flash/src}/bootloader_flash_config_esp32s2.c (100%) rename components/bootloader_support/{src/flash_support => bootloader_flash/src}/bootloader_flash_config_esp32s3.c (100%) rename components/bootloader_support/{src/flash_support => bootloader_flash/src}/bootloader_flash_config_esp8684.c (100%) rename components/bootloader_support/{src/flash_support => bootloader_flash/src}/flash_qio_mode.c (100%) create mode 100644 components/esp_rom/include/esp_rom_spiflash_defs.h diff --git a/components/app_update/test/test_switch_ota.c b/components/app_update/test/test_switch_ota.c index e5bb8ab599..3ba3cf575a 100644 --- a/components/app_update/test/test_switch_ota.c +++ b/components/app_update/test/test_switch_ota.c @@ -21,7 +21,7 @@ #include "unity.h" #include "bootloader_common.h" -#include "../include_bootloader/bootloader_flash_private/bootloader_flash_priv.h" +#include "../bootloader_flash/include/bootloader_flash_priv.h" #include "esp_log.h" #include "esp_ota_ops.h" diff --git a/components/bootloader_support/CMakeLists.txt b/components/bootloader_support/CMakeLists.txt index 88f26b8762..585106991c 100644 --- a/components/bootloader_support/CMakeLists.txt +++ b/components/bootloader_support/CMakeLists.txt @@ -2,7 +2,7 @@ set(srcs "src/bootloader_common.c" "src/bootloader_common_loader.c" "src/bootloader_clock_init.c" - "src/flash_support/bootloader_flash.c" + "bootloader_flash/src/bootloader_flash.c" "src/bootloader_mem.c" "src/bootloader_random.c" "src/bootloader_random_${IDF_TARGET}.c" @@ -11,14 +11,14 @@ set(srcs "src/flash_encrypt.c" "src/secure_boot.c" "src/flash_partitions.c" - "src/flash_support/flash_qio_mode.c" - "src/flash_support/bootloader_flash_config_${IDF_TARGET}.c" + "bootloader_flash/src/flash_qio_mode.c" + "bootloader_flash/src/bootloader_flash_config_${IDF_TARGET}.c" "src/bootloader_efuse_${IDF_TARGET}.c" ) if(BOOTLOADER_BUILD) set(include_dirs "include" "bootloader_flash/include" - "include_bootloader" "include_bootloader/bootloader_flash_private") + "include_bootloader") set(priv_requires micro-ecc spi_flash efuse) list(APPEND srcs "src/bootloader_init.c" @@ -35,7 +35,7 @@ else() list(APPEND srcs "src/idf/bootloader_sha.c") set(include_dirs "include" "bootloader_flash/include") - set(priv_include_dirs "include_bootloader" "include_bootloader/bootloader_flash_private") + set(priv_include_dirs "include_bootloader") # heap is required for `heap_memory_layout.h` header set(priv_requires spi_flash mbedtls efuse app_update heap) endif() diff --git a/components/bootloader_support/include/bootloader_flash_override.h b/components/bootloader_support/bootloader_flash/include/bootloader_flash_override.h similarity index 100% rename from components/bootloader_support/include/bootloader_flash_override.h rename to components/bootloader_support/bootloader_flash/include/bootloader_flash_override.h diff --git a/components/bootloader_support/include_bootloader/bootloader_flash_private/bootloader_flash_priv.h b/components/bootloader_support/bootloader_flash/include/bootloader_flash_priv.h similarity index 98% rename from components/bootloader_support/include_bootloader/bootloader_flash_private/bootloader_flash_priv.h rename to components/bootloader_support/bootloader_flash/include/bootloader_flash_priv.h index ee1bfbea24..eeb20fdf07 100644 --- a/components/bootloader_support/include_bootloader/bootloader_flash_private/bootloader_flash_priv.h +++ b/components/bootloader_support/bootloader_flash/include/bootloader_flash_priv.h @@ -14,6 +14,10 @@ #include "sdkconfig.h" #include "bootloader_flash.h" +#ifdef __cplusplus +extern "C" { +#endif + #define FLASH_SECTOR_SIZE 0x1000 #define FLASH_BLOCK_SIZE 0x10000 #define MMAP_ALIGNED_MASK 0x0000FFFF @@ -171,4 +175,8 @@ uint32_t bootloader_flash_read_sfdp(uint32_t sfdp_addr, unsigned int miso_byte_n */ void bootloader_enable_wp(void); +#ifdef __cplusplus +} +#endif + #endif diff --git a/components/bootloader_support/include_bootloader/bootloader_flash_private/flash_qio_mode.h b/components/bootloader_support/bootloader_flash/include/flash_qio_mode.h similarity index 96% rename from components/bootloader_support/include_bootloader/bootloader_flash_private/flash_qio_mode.h rename to components/bootloader_support/bootloader_flash/include/flash_qio_mode.h index 05954b25de..311bbb0d04 100644 --- a/components/bootloader_support/include_bootloader/bootloader_flash_private/flash_qio_mode.h +++ b/components/bootloader_support/bootloader_flash/include/flash_qio_mode.h @@ -5,6 +5,8 @@ */ #pragma once +#include + #ifdef __cplusplus extern "C" { #endif diff --git a/components/bootloader_support/src/flash_support/bootloader_flash.c b/components/bootloader_support/bootloader_flash/src/bootloader_flash.c similarity index 100% rename from components/bootloader_support/src/flash_support/bootloader_flash.c rename to components/bootloader_support/bootloader_flash/src/bootloader_flash.c diff --git a/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32.c b/components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp32.c similarity index 99% rename from components/bootloader_support/src/flash_support/bootloader_flash_config_esp32.c rename to components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp32.c index 3adb7ae9c1..64b24600bb 100644 --- a/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32.c +++ b/components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp32.c @@ -22,7 +22,6 @@ #include "bootloader_common.h" #include "bootloader_flash_config.h" - void bootloader_flash_update_id(void) { g_rom_flashchip.device_id = bootloader_read_flash_id(); diff --git a/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32c3.c b/components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp32c3.c similarity index 100% rename from components/bootloader_support/src/flash_support/bootloader_flash_config_esp32c3.c rename to components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp32c3.c diff --git a/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32h2.c b/components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp32h2.c similarity index 100% rename from components/bootloader_support/src/flash_support/bootloader_flash_config_esp32h2.c rename to components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp32h2.c diff --git a/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32s2.c b/components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp32s2.c similarity index 100% rename from components/bootloader_support/src/flash_support/bootloader_flash_config_esp32s2.c rename to components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp32s2.c diff --git a/components/bootloader_support/src/flash_support/bootloader_flash_config_esp32s3.c b/components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp32s3.c similarity index 100% rename from components/bootloader_support/src/flash_support/bootloader_flash_config_esp32s3.c rename to components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp32s3.c diff --git a/components/bootloader_support/src/flash_support/bootloader_flash_config_esp8684.c b/components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp8684.c similarity index 100% rename from components/bootloader_support/src/flash_support/bootloader_flash_config_esp8684.c rename to components/bootloader_support/bootloader_flash/src/bootloader_flash_config_esp8684.c diff --git a/components/bootloader_support/src/flash_support/flash_qio_mode.c b/components/bootloader_support/bootloader_flash/src/flash_qio_mode.c similarity index 100% rename from components/bootloader_support/src/flash_support/flash_qio_mode.c rename to components/bootloader_support/bootloader_flash/src/flash_qio_mode.c diff --git a/components/bootloader_support/src/esp8684/bootloader_esp8684.c b/components/bootloader_support/src/esp8684/bootloader_esp8684.c index ca7a38d901..2eb9bcd27a 100644 --- a/components/bootloader_support/src/esp8684/bootloader_esp8684.c +++ b/components/bootloader_support/src/esp8684/bootloader_esp8684.c @@ -202,7 +202,7 @@ static esp_err_t bootloader_init_spi_flash(void) #endif bootloader_spi_flash_resume(); - esp_rom_spiflash_unlock(); + bootloader_flash_unlock(); #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT bootloader_enable_qio_mode(); diff --git a/components/efuse/src/esp_efuse_utility.c b/components/efuse/src/esp_efuse_utility.c index 3803960ea5..168dc821fb 100644 --- a/components/efuse/src/esp_efuse_utility.c +++ b/components/efuse/src/esp_efuse_utility.c @@ -404,7 +404,7 @@ void esp_efuse_init_virtual_mode_in_ram(void) #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH -#include "../include_bootloader/bootloader_flash_private/bootloader_flash_priv.h" +#include "../bootloader_flash/include/bootloader_flash_priv.h" static uint32_t esp_efuse_flash_offset = 0; static uint32_t esp_efuse_flash_size = 0; diff --git a/components/esp_rom/README.md b/components/esp_rom/README.md index 74fe101978..bfef38f0e2 100644 --- a/components/esp_rom/README.md +++ b/components/esp_rom/README.md @@ -5,10 +5,10 @@ `esp_rom` component contains each chip's ROM functions, which are used in the ROM bootloader, 2nd bootloader, esp_tool flash stub and some driver code (e.g. GPIO matrix). ROM functions as not treated as public APIs, attentions are required when you use ROM functions: 1. ROM functions are **not** thread-safe in RTOS, extra locks are needed to be around the ROM functions. -2. There're risks for ROM functions that their names/signatures/behaviors being changed in different chips. -3. ROM functions are not guaranteed to be exist across all chips. +2. Names/signatures/behaviors of ROM function may be different between chips. +3. ROM functions are not guaranteed to exist across all chips. -When using ROM functions in esp-idf, the including convention is `/rom/.h`. This can prevent you from using a nonexistent ROM function for specific ``. Thus ROM functions are recommended to be used in a target-specific source file. For example, `bootloader_esp32.c` can include `esp32/rom/.h` without any violations. However, this is not the case when it comes to a common source file who also want to use some of ROM functions. The including list would be tortuous like: +When using ROM functions in esp-idf, the including convention is `/rom/.h`. This can prevent you from using a nonexistent ROM function for a specific ``. Thus ROM functions are recommended for use in a target-specific source file. For example, `bootloader_esp32.c` can include `esp32/rom/.h` without any violations. However, this is not the case when it comes to a common source file that also wants to use some of the ROM functions. The include list would be quite extensive: ```c #if CONFIG_IDF_TARGET_ESP32 @@ -22,7 +22,7 @@ When using ROM functions in esp-idf, the including convention is `/rom/< ... ``` -So, we added a wrapper for those commonly used ROM functions. They're declared in `esp_rom/include/esp_rom_xxx.h`. Unlike the original ROM functions, these extracted ones are expected to be exist across all chips. If some of them are missed in the new chips, we will implement them again in `esp_rom/patches`. These ROM apis are always prefixed with the name `esp_rom` (e.g. `esp_rom_printf`), so that it's obvious to know whether a function is linked to ROM. +So, we added a wrapper for those commonly used ROM functions. They're declared in `esp_rom/include/esp_rom_xxx.h`. Unlike the original ROM functions, these extracted ones are expected to exist across all chips. If some of them are missed in the new chips, we will implement them again in `esp_rom/patches`. These ROM APIs are always prefixed with the name `esp_rom` (e.g. `esp_rom_printf`), so that it's obvious to know whether a function is linked to ROM. Most of the time, the ROM wrapper APIs are just alias to the original ROM functions by linker script `esp_rom//ld/.rom.api.ld`. For example, `esp_rom_printf` is alias to `ets_printf` in the following way: @@ -30,9 +30,9 @@ Most of the time, the ROM wrapper APIs are just alias to the original ROM functi PROVIDE ( esp_rom_printf = ets_printf ); ``` -If some original ROM functions have changed the behavior or have bugs, we should override it in the wrapper layer. A common example is the `esp_rom_install_uart_printf()`, on ESP32 and ESP32S2, it's just alias to `ets_install_uart_printf`, but on other chips, it's re-implemented in the `esp_rom/patches/esp_rom_uart.c`. To some extent, the ROM wrapper layer works like an anti-corrosion layer between esp-rom project and esp-idf project. +If some original ROM functions have changed the behavior or have bugs, we should override them in the wrapper layer. A common example is the `esp_rom_install_uart_printf()`, on ESP32 and ESP32S2, it's just alias to `ets_install_uart_printf`, but on other chips, it's re-implemented in the `esp_rom/patches/esp_rom_uart.c`. To some extent, the ROM wrapper layer works like an anti-corrosion layer between esp-rom project and esp-idf project. -As ROM functions are unique to each target, features are as well. For example, ESP32 has built the `tjpgd` library into the ROM, but ESP32S2 doesn't. We have a header file declaring the features: `esp_rom//esp_rom_caps.h` that supported by each target. Based on the macros defined there, we can decide whether a function should be patched or whether a feature should be re-implemented. +As ROM functions are unique to each target, features are as well. For example, ESP32 has the `tjpgd` library built into the ROM, but ESP32S2 hasn't. We have a header file `esp_rom//esp_rom_caps.h` declaring the features that are supported by each target. Based on the macros defined there, we can decide whether a function should be patched or whether a feature should be re-implemented. ## Directory Structure diff --git a/components/esp_rom/esp32/ld/esp32.rom.api.ld b/components/esp_rom/esp32/ld/esp32.rom.api.ld index afd3cc04eb..493c8a1b03 100644 --- a/components/esp_rom/esp32/ld/esp32.rom.api.ld +++ b/components/esp_rom/esp32/ld/esp32.rom.api.ld @@ -43,3 +43,6 @@ PROVIDE ( esp_rom_printf = ets_printf ); PROVIDE ( esp_rom_delay_us = ets_delay_us ); PROVIDE ( esp_rom_install_uart_printf = ets_install_uart_printf ); PROVIDE ( esp_rom_get_reset_reason = rtc_get_reset_reason ); + +PROVIDE ( esp_rom_spiflash_set_bp = esp_rom_spiflash_lock ); +PROVIDE ( esp_rom_spiflash_write_enable = SPI_write_enable); diff --git a/components/esp_rom/esp32/ld/esp32.rom.ld b/components/esp_rom/esp32/ld/esp32.rom.ld index d433cc5a54..18ee3a4aa2 100644 --- a/components/esp_rom/esp32/ld/esp32.rom.ld +++ b/components/esp_rom/esp32/ld/esp32.rom.ld @@ -1365,6 +1365,7 @@ PROVIDE ( esp_rom_spiflash_select_qio_pins = 0x40061ddc ); PROVIDE ( esp_rom_spiflash_attach = 0x40062a6c ); PROVIDE ( esp_rom_spiflash_config_clk = 0x40062bc8 ); PROVIDE ( g_rom_spiflash_chip = 0x3ffae270 ); +PROVIDE ( SPI_write_enable = 0x40062320 ); PROVIDE ( hci_le_rd_rem_used_feats_cmd_handler = 0x400417b4 ); PROVIDE ( llcp_length_req_handler = 0x40043808 ); PROVIDE ( llcp_unknown_rsp_handler = 0x40043ba8 ); diff --git a/components/esp_rom/esp32c3/ld/esp32c3.rom.api.ld b/components/esp_rom/esp32c3/ld/esp32c3.rom.api.ld index e05d9b8aa4..c453216367 100644 --- a/components/esp_rom/esp32c3/ld/esp32c3.rom.api.ld +++ b/components/esp_rom/esp32c3/ld/esp32c3.rom.api.ld @@ -40,3 +40,6 @@ PROVIDE ( esp_rom_md5_final = MD5Final ); PROVIDE ( esp_rom_printf = ets_printf ); PROVIDE ( esp_rom_delay_us = ets_delay_us ); PROVIDE ( esp_rom_get_reset_reason = rtc_get_reset_reason ); + +PROVIDE ( esp_rom_spiflash_clear_bp = esp_rom_spiflash_unlock ); +PROVIDE ( esp_rom_spiflash_write_enable = SPI_write_enable); diff --git a/components/esp_rom/esp32h2/ld/rev1/esp32h2.rom.api.ld b/components/esp_rom/esp32h2/ld/rev1/esp32h2.rom.api.ld index 6c27a2dd01..f401c7adfb 100644 --- a/components/esp_rom/esp32h2/ld/rev1/esp32h2.rom.api.ld +++ b/components/esp_rom/esp32h2/ld/rev1/esp32h2.rom.api.ld @@ -43,3 +43,6 @@ PROVIDE ( esp_rom_md5_final = MD5Final ); PROVIDE ( esp_rom_printf = ets_printf ); PROVIDE ( esp_rom_delay_us = ets_delay_us ); PROVIDE ( esp_rom_get_reset_reason = rtc_get_reset_reason ); + +PROVIDE ( esp_rom_spiflash_clear_bp = esp_rom_spiflash_unlock ); +PROVIDE ( esp_rom_spiflash_write_enable = SPI_write_enable); diff --git a/components/esp_rom/esp32h2/ld/rev2/esp32h2.rom.api.ld b/components/esp_rom/esp32h2/ld/rev2/esp32h2.rom.api.ld index 6c27a2dd01..f401c7adfb 100644 --- a/components/esp_rom/esp32h2/ld/rev2/esp32h2.rom.api.ld +++ b/components/esp_rom/esp32h2/ld/rev2/esp32h2.rom.api.ld @@ -43,3 +43,6 @@ PROVIDE ( esp_rom_md5_final = MD5Final ); PROVIDE ( esp_rom_printf = ets_printf ); PROVIDE ( esp_rom_delay_us = ets_delay_us ); PROVIDE ( esp_rom_get_reset_reason = rtc_get_reset_reason ); + +PROVIDE ( esp_rom_spiflash_clear_bp = esp_rom_spiflash_unlock ); +PROVIDE ( esp_rom_spiflash_write_enable = SPI_write_enable); diff --git a/components/esp_rom/esp32s2/ld/esp32s2.rom.api.ld b/components/esp_rom/esp32s2/ld/esp32s2.rom.api.ld index afb0849271..d26866c0b4 100644 --- a/components/esp_rom/esp32s2/ld/esp32s2.rom.api.ld +++ b/components/esp_rom/esp32s2/ld/esp32s2.rom.api.ld @@ -37,3 +37,6 @@ PROVIDE ( esp_rom_printf = ets_printf ); PROVIDE ( esp_rom_delay_us = ets_delay_us ); PROVIDE ( esp_rom_install_uart_printf = ets_install_uart_printf ); PROVIDE ( esp_rom_get_reset_reason = rtc_get_reset_reason ); + +PROVIDE ( esp_rom_spiflash_clear_bp = esp_rom_spiflash_unlock ); +PROVIDE ( esp_rom_spiflash_write_enable = SPI_write_enable); diff --git a/components/esp_rom/esp32s3/ld/esp32s3.rom.api.ld b/components/esp_rom/esp32s3/ld/esp32s3.rom.api.ld index 4d45188393..0f2073ac0b 100644 --- a/components/esp_rom/esp32s3/ld/esp32s3.rom.api.ld +++ b/components/esp_rom/esp32s3/ld/esp32s3.rom.api.ld @@ -45,3 +45,5 @@ PROVIDE ( esp_rom_install_uart_printf = ets_install_uart_printf ); PROVIDE ( esp_rom_get_reset_reason = rtc_get_reset_reason ); PROVIDE( esp_rom_spiflash_attach = spi_flash_attach ); +PROVIDE ( esp_rom_spiflash_clear_bp = esp_rom_spiflash_unlock ); +PROVIDE ( esp_rom_spiflash_write_enable = SPI_write_enable); diff --git a/components/esp_rom/esp8684/ld/esp8684.rom.api.ld b/components/esp_rom/esp8684/ld/esp8684.rom.api.ld index 4351eb7c49..4938724231 100644 --- a/components/esp_rom/esp8684/ld/esp8684.rom.api.ld +++ b/components/esp_rom/esp8684/ld/esp8684.rom.api.ld @@ -39,3 +39,5 @@ PROVIDE ( esp_rom_delay_us = ets_delay_us ); PROVIDE ( esp_rom_get_reset_reason = rtc_get_reset_reason ); PROVIDE( esp_rom_spiflash_attach = spi_flash_attach ); +PROVIDE ( esp_rom_spiflash_clear_bp = esp_rom_spiflash_unlock ); +PROVIDE ( esp_rom_spiflash_write_enable = SPI_write_enable); diff --git a/components/esp_rom/include/esp32/rom/spi_flash.h b/components/esp_rom/include/esp32/rom/spi_flash.h index 8a2e17ef36..2192ca2417 100644 --- a/components/esp_rom/include/esp32/rom/spi_flash.h +++ b/components/esp_rom/include/esp32/rom/spi_flash.h @@ -95,23 +95,362 @@ extern "C" { #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM 64 #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_BITS 0x3f -//SPI status register -#define ESP_ROM_SPIFLASH_BUSY_FLAG BIT0 -#define ESP_ROM_SPIFLASH_WRENABLE_FLAG BIT1 -#define ESP_ROM_SPIFLASH_BP0 BIT2 -#define ESP_ROM_SPIFLASH_BP1 BIT3 -#define ESP_ROM_SPIFLASH_BP2 BIT4 -#define ESP_ROM_SPIFLASH_WR_PROTECT (ESP_ROM_SPIFLASH_BP0|ESP_ROM_SPIFLASH_BP1|ESP_ROM_SPIFLASH_BP2) -#define ESP_ROM_SPIFLASH_QE BIT9 -#define ESP_ROM_SPIFLASH_BP_MASK_ISSI (BIT7 | BIT5 | BIT4 | BIT3 | BIT2) - //Extra dummy for flash read #define ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_20M 0 #define ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_26M 0 #define ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_40M 1 #define ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_80M 2 -#define FLASH_ID_GD25LQ32C 0xC86016 +typedef struct { + uint8_t data_length; + uint8_t read_cmd0; + uint8_t read_cmd1; + uint8_t write_cmd; + uint16_t data_mask; + uint16_t data; +} esp_rom_spiflash_common_cmd_t; + +/** + * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t legacy: always keeping false. + * + * @return None + */ +void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); + +/** + * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief Write status to Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t status_value : Value to . + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); + +/** + * @brief Use a command to Read Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t*status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); + +/** + * @brief Config SPI Flash read mode when init. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. + * + * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); + +/** + * @brief Config SPI Flash clock divisor. + * Please do not call this function in SDK. + * + * @param uint8_t freqdiv: clock divisor. + * + * @param uint8_t spi: 0 for SPI0, 1 for SPI1. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); + +/** + * @brief Clear all SR bits except QE bit. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_clear_bp(void); + +/** + * @brief Clear all SR bits except QE bit. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); + +/** + * @brief SPI flash set BP0 to BP2.(Only valid when WRSR+2Bytes) + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Lock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Lock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Lock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_lock(void); + +/** + * @brief Update SPI Flash parameter. + * Please do not call this function in SDK. + * + * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. + * + * @param uint32_t chip_size : The Flash size. + * + * @param uint32_t block_size : The Flash block size. + * + * @param uint32_t sector_size : The Flash sector size. + * + * @param uint32_t page_size : The Flash page size. + * + * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, + uint32_t sector_size, uint32_t page_size, uint32_t status_mask); + +/** + * @brief Erase whole flash chip. + * Please do not call this function in SDK. + * + * @param None + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); + +/** + * @brief Erase a 64KB block of flash + * Uses SPI flash command D8H. + * Please do not call this function in SDK. + * + * @param uint32_t block_num : Which block to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); + +/** + * @brief Erase a sector of flash. + * Uses SPI flash command 20H. + * Please do not call this function in SDK. + * + * @param uint32_t sector_num : Which sector to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); + +/** + * @brief Erase some sectors. + * Please do not call this function in SDK. + * + * @param uint32_t start_addr : Start addr to erase, should be sector aligned. + * + * @param uint32_t area_len : Length to erase, should be sector aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); + +/** + * @brief Write Data to Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. + * + * @param const uint32_t *src : The pointer to data which is to write. + * + * @param uint32_t len : Length to write, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); + +/** + * @brief Read Data from Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. + * + * @param uint32_t *dest : The buf to read the data. + * + * @param uint32_t len : Length to read, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); + +/** + * @brief SPI1 go into encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_enable(void); + +/** + * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. + * + * @param uint32_t *data : The pointer to data which is to write. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); + +/** + * @brief SPI1 go out of encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_disable(void); + +/** + * @brief Write data to flash with transparent encryption. + * @note Sectors to be written should already be erased. + * + * @note Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. + * + * @param uint32_t *data : The pointer to data to write. Note, this pointer must + * be 32 bit aligned and the content of the data will be + * modified by the encryption function. + * + * @param uint32_t len : Length to write, should be 32 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. + * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); + + +/** @brief Wait until SPI flash write operation is complete + * + * @note Please do not call this function in SDK. + * + * Reads the Write In Progress bit of the SPI flash status register, + * repeats until this bit is zero (indicating write complete). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete + * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); + + +/** @brief Enable Quad I/O pin functions + * + * @note Please do not call this function in SDK. + * + * Sets the HD & WP pin functions for Quad I/O modes, based on the + * efuse SPI pin configuration. + * + * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. + * + * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). + * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. + * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. + * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used + * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). + * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. + */ +void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); + +/** + * @brief Clear WEL bit unconditionally. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); + +/** + * @brief Set WREN bit. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_enable(esp_rom_spiflash_chip_t *spi); /** * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. diff --git a/components/esp_rom/include/esp32c3/rom/spi_flash.h b/components/esp_rom/include/esp32c3/rom/spi_flash.h index 76c67e5025..08ec39bdc3 100644 --- a/components/esp_rom/include/esp32c3/rom/spi_flash.h +++ b/components/esp_rom/include/esp32c3/rom/spi_flash.h @@ -58,17 +58,6 @@ extern "C" { #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM 16 #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_BITS 0xf -//SPI status register -#define ESP_ROM_SPIFLASH_BUSY_FLAG BIT0 -#define ESP_ROM_SPIFLASH_WRENABLE_FLAG BIT1 -#define ESP_ROM_SPIFLASH_BP0 BIT2 -#define ESP_ROM_SPIFLASH_BP1 BIT3 -#define ESP_ROM_SPIFLASH_BP2 BIT4 -#define ESP_ROM_SPIFLASH_WR_PROTECT (ESP_ROM_SPIFLASH_BP0|ESP_ROM_SPIFLASH_BP1|ESP_ROM_SPIFLASH_BP2) -#define ESP_ROM_SPIFLASH_QE BIT9 - -#define FLASH_ID_GD25LQ32C 0xC86016 - typedef void (* spi_flash_func_t)(void); typedef esp_rom_spiflash_result_t (* spi_flash_op_t)(void); typedef esp_rom_spiflash_result_t (* spi_flash_erase_t)(uint32_t); @@ -91,6 +80,345 @@ typedef struct { spi_flash_op_t wait_idle; } spiflash_legacy_funcs_t; +typedef struct { + uint8_t data_length; + uint8_t read_cmd0; + uint8_t read_cmd1; + uint8_t write_cmd; + uint16_t data_mask; + uint16_t data; +} esp_rom_spiflash_common_cmd_t; + +/** + * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t legacy: always keeping false. + * + * @return None + */ +void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); + +/** + * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief Write status to Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t status_value : Value to . + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); + +/** + * @brief Use a command to Read Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t*status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); + +/** + * @brief Config SPI Flash read mode when init. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. + * + * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); + +/** + * @brief Config SPI Flash clock divisor. + * Please do not call this function in SDK. + * + * @param uint8_t freqdiv: clock divisor. + * + * @param uint8_t spi: 0 for SPI0, 1 for SPI1. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); + +/** + * @brief Clear all SR bits except QE bit. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_clear_bp(void); + +/** + * @brief Clear all SR bits except QE bit. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); + +/** + * @brief Update SPI Flash parameter. + * Please do not call this function in SDK. + * + * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. + * + * @param uint32_t chip_size : The Flash size. + * + * @param uint32_t block_size : The Flash block size. + * + * @param uint32_t sector_size : The Flash sector size. + * + * @param uint32_t page_size : The Flash page size. + * + * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, + uint32_t sector_size, uint32_t page_size, uint32_t status_mask); + +/** + * @brief Erase whole flash chip. + * Please do not call this function in SDK. + * + * @param None + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); + +/** + * @brief Erase a 64KB block of flash + * Uses SPI flash command D8H. + * Please do not call this function in SDK. + * + * @param uint32_t block_num : Which block to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); + +/** + * @brief Erase a sector of flash. + * Uses SPI flash command 20H. + * Please do not call this function in SDK. + * + * @param uint32_t sector_num : Which sector to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); + +/** + * @brief Erase some sectors. + * Please do not call this function in SDK. + * + * @param uint32_t start_addr : Start addr to erase, should be sector aligned. + * + * @param uint32_t area_len : Length to erase, should be sector aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); + +/** + * @brief Write Data to Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. + * + * @param const uint32_t *src : The pointer to data which is to write. + * + * @param uint32_t len : Length to write, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); + +/** + * @brief Read Data from Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. + * + * @param uint32_t *dest : The buf to read the data. + * + * @param uint32_t len : Length to read, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); + +/** + * @brief SPI1 go into encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_enable(void); + +/** + * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. + * + * @param uint32_t *data : The pointer to data which is to write. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); + +/** + * @brief SPI1 go out of encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_disable(void); + +/** + * @brief Write data to flash with transparent encryption. + * @note Sectors to be written should already be erased. + * + * @note Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. + * + * @param uint32_t *data : The pointer to data to write. Note, this pointer must + * be 32 bit aligned and the content of the data will be + * modified by the encryption function. + * + * @param uint32_t len : Length to write, should be 32 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. + * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); + + +/** @brief Wait until SPI flash write operation is complete + * + * @note Please do not call this function in SDK. + * + * Reads the Write In Progress bit of the SPI flash status register, + * repeats until this bit is zero (indicating write complete). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete + * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); + + +/** @brief Enable Quad I/O pin functions + * + * @note Please do not call this function in SDK. + * + * Sets the HD & WP pin functions for Quad I/O modes, based on the + * efuse SPI pin configuration. + * + * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. + * + * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). + * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. + * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. + * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used + * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). + * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. + */ +void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); + +/** + * @brief Clear WEL bit unconditionally. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); + +/** + * @brief Set WREN bit. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_enable(esp_rom_spiflash_chip_t *spi); + /** * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. * Please do not call this function in SDK. @@ -157,6 +485,8 @@ void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); */ uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); +extern const spiflash_legacy_funcs_t *rom_spiflash_legacy_funcs; + #ifdef __cplusplus } #endif diff --git a/components/esp_rom/include/esp32h2/rom/spi_flash.h b/components/esp_rom/include/esp32h2/rom/spi_flash.h index fcf4308fbc..e93a3f8f11 100644 --- a/components/esp_rom/include/esp32h2/rom/spi_flash.h +++ b/components/esp_rom/include/esp32h2/rom/spi_flash.h @@ -58,17 +58,6 @@ extern "C" { #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM 16 #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_BITS 0xf -//SPI status register -#define ESP_ROM_SPIFLASH_BUSY_FLAG BIT0 -#define ESP_ROM_SPIFLASH_WRENABLE_FLAG BIT1 -#define ESP_ROM_SPIFLASH_BP0 BIT2 -#define ESP_ROM_SPIFLASH_BP1 BIT3 -#define ESP_ROM_SPIFLASH_BP2 BIT4 -#define ESP_ROM_SPIFLASH_WR_PROTECT (ESP_ROM_SPIFLASH_BP0|ESP_ROM_SPIFLASH_BP1|ESP_ROM_SPIFLASH_BP2) -#define ESP_ROM_SPIFLASH_QE BIT9 - -#define FLASH_ID_GD25LQ32C 0xC86016 - typedef void (* spi_flash_func_t)(void); typedef esp_rom_spiflash_result_t (* spi_flash_op_t)(void); typedef esp_rom_spiflash_result_t (* spi_flash_erase_t)(uint32_t); @@ -97,6 +86,345 @@ typedef struct { spi_flash_erase_area_t erase_area; } spiflash_legacy_funcs_t; +typedef struct { + uint8_t data_length; + uint8_t read_cmd0; + uint8_t read_cmd1; + uint8_t write_cmd; + uint16_t data_mask; + uint16_t data; +} esp_rom_spiflash_common_cmd_t; + +/** + * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t legacy: always keeping false. + * + * @return None + */ +void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); + +/** + * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief Write status to Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t status_value : Value to . + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); + +/** + * @brief Use a command to Read Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t*status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); + +/** + * @brief Config SPI Flash read mode when init. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. + * + * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); + +/** + * @brief Config SPI Flash clock divisor. + * Please do not call this function in SDK. + * + * @param uint8_t freqdiv: clock divisor. + * + * @param uint8_t spi: 0 for SPI0, 1 for SPI1. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); + +/** + * @brief Clear all SR bits except QE bit. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_clear_bp(void); + +/** + * @brief Clear all SR bits except QE bit. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); + +/** + * @brief Update SPI Flash parameter. + * Please do not call this function in SDK. + * + * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. + * + * @param uint32_t chip_size : The Flash size. + * + * @param uint32_t block_size : The Flash block size. + * + * @param uint32_t sector_size : The Flash sector size. + * + * @param uint32_t page_size : The Flash page size. + * + * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, + uint32_t sector_size, uint32_t page_size, uint32_t status_mask); + +/** + * @brief Erase whole flash chip. + * Please do not call this function in SDK. + * + * @param None + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); + +/** + * @brief Erase a 64KB block of flash + * Uses SPI flash command D8H. + * Please do not call this function in SDK. + * + * @param uint32_t block_num : Which block to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); + +/** + * @brief Erase a sector of flash. + * Uses SPI flash command 20H. + * Please do not call this function in SDK. + * + * @param uint32_t sector_num : Which sector to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); + +/** + * @brief Erase some sectors. + * Please do not call this function in SDK. + * + * @param uint32_t start_addr : Start addr to erase, should be sector aligned. + * + * @param uint32_t area_len : Length to erase, should be sector aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); + +/** + * @brief Write Data to Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. + * + * @param const uint32_t *src : The pointer to data which is to write. + * + * @param uint32_t len : Length to write, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); + +/** + * @brief Read Data from Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. + * + * @param uint32_t *dest : The buf to read the data. + * + * @param uint32_t len : Length to read, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); + +/** + * @brief SPI1 go into encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_enable(void); + +/** + * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. + * + * @param uint32_t *data : The pointer to data which is to write. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); + +/** + * @brief SPI1 go out of encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_disable(void); + +/** + * @brief Write data to flash with transparent encryption. + * @note Sectors to be written should already be erased. + * + * @note Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. + * + * @param uint32_t *data : The pointer to data to write. Note, this pointer must + * be 32 bit aligned and the content of the data will be + * modified by the encryption function. + * + * @param uint32_t len : Length to write, should be 32 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. + * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); + + +/** @brief Wait until SPI flash write operation is complete + * + * @note Please do not call this function in SDK. + * + * Reads the Write In Progress bit of the SPI flash status register, + * repeats until this bit is zero (indicating write complete). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete + * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); + + +/** @brief Enable Quad I/O pin functions + * + * @note Please do not call this function in SDK. + * + * Sets the HD & WP pin functions for Quad I/O modes, based on the + * efuse SPI pin configuration. + * + * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. + * + * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). + * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. + * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. + * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used + * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). + * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. + */ +void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); + +/** + * @brief Clear WEL bit unconditionally. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); + +/** + * @brief Set WREN bit. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_enable(esp_rom_spiflash_chip_t *spi); + /** * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. * Please do not call this function in SDK. @@ -163,6 +491,8 @@ void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); */ uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); +extern const spiflash_legacy_funcs_t *rom_spiflash_legacy_funcs; + #ifdef __cplusplus } #endif diff --git a/components/esp_rom/include/esp32s2/rom/spi_flash.h b/components/esp_rom/include/esp32s2/rom/spi_flash.h index 2b65be5e12..3f7bf10ea4 100644 --- a/components/esp_rom/include/esp32s2/rom/spi_flash.h +++ b/components/esp_rom/include/esp32s2/rom/spi_flash.h @@ -94,16 +94,344 @@ extern "C" { #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM 16 #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_BITS 0xf -//SPI status register -#define ESP_ROM_SPIFLASH_BUSY_FLAG BIT0 -#define ESP_ROM_SPIFLASH_WRENABLE_FLAG BIT1 -#define ESP_ROM_SPIFLASH_BP0 BIT2 -#define ESP_ROM_SPIFLASH_BP1 BIT3 -#define ESP_ROM_SPIFLASH_BP2 BIT4 -#define ESP_ROM_SPIFLASH_WR_PROTECT (ESP_ROM_SPIFLASH_BP0|ESP_ROM_SPIFLASH_BP1|ESP_ROM_SPIFLASH_BP2) -#define ESP_ROM_SPIFLASH_QE BIT9 +typedef struct { + uint8_t data_length; + uint8_t read_cmd0; + uint8_t read_cmd1; + uint8_t write_cmd; + uint16_t data_mask; + uint16_t data; +} esp_rom_spiflash_common_cmd_t; -#define FLASH_ID_GD25LQ32C 0xC86016 +/** + * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t legacy: always keeping false. + * + * @return None + */ +void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); + +/** + * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief Write status to Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t status_value : Value to . + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); + +/** + * @brief Use a command to Read Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t*status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); + +/** + * @brief Config SPI Flash read mode when init. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. + * + * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); + +/** + * @brief Config SPI Flash clock divisor. + * Please do not call this function in SDK. + * + * @param uint8_t freqdiv: clock divisor. + * + * @param uint8_t spi: 0 for SPI0, 1 for SPI1. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); + +/** + * @brief Clear all SR bits except QE bit. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_clear_bp(void); + +/** + * @brief Clear all SR bits except QE bit. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); + +/** + * @brief Update SPI Flash parameter. + * Please do not call this function in SDK. + * + * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. + * + * @param uint32_t chip_size : The Flash size. + * + * @param uint32_t block_size : The Flash block size. + * + * @param uint32_t sector_size : The Flash sector size. + * + * @param uint32_t page_size : The Flash page size. + * + * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, + uint32_t sector_size, uint32_t page_size, uint32_t status_mask); + +/** + * @brief Erase whole flash chip. + * Please do not call this function in SDK. + * + * @param None + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); + +/** + * @brief Erase a 64KB block of flash + * Uses SPI flash command D8H. + * Please do not call this function in SDK. + * + * @param uint32_t block_num : Which block to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); + +/** + * @brief Erase a sector of flash. + * Uses SPI flash command 20H. + * Please do not call this function in SDK. + * + * @param uint32_t sector_num : Which sector to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); + +/** + * @brief Erase some sectors. + * Please do not call this function in SDK. + * + * @param uint32_t start_addr : Start addr to erase, should be sector aligned. + * + * @param uint32_t area_len : Length to erase, should be sector aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); + +/** + * @brief Write Data to Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. + * + * @param const uint32_t *src : The pointer to data which is to write. + * + * @param uint32_t len : Length to write, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); + +/** + * @brief Read Data from Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. + * + * @param uint32_t *dest : The buf to read the data. + * + * @param uint32_t len : Length to read, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); + +/** + * @brief SPI1 go into encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_enable(void); + +/** + * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. + * + * @param uint32_t *data : The pointer to data which is to write. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); + +/** + * @brief SPI1 go out of encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_disable(void); + +/** + * @brief Write data to flash with transparent encryption. + * @note Sectors to be written should already be erased. + * + * @note Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. + * + * @param uint32_t *data : The pointer to data to write. Note, this pointer must + * be 32 bit aligned and the content of the data will be + * modified by the encryption function. + * + * @param uint32_t len : Length to write, should be 32 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. + * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); + + +/** @brief Wait until SPI flash write operation is complete + * + * @note Please do not call this function in SDK. + * + * Reads the Write In Progress bit of the SPI flash status register, + * repeats until this bit is zero (indicating write complete). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete + * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); + + +/** @brief Enable Quad I/O pin functions + * + * @note Please do not call this function in SDK. + * + * Sets the HD & WP pin functions for Quad I/O modes, based on the + * efuse SPI pin configuration. + * + * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. + * + * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). + * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. + * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. + * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used + * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). + * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. + */ +void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); + +/** + * @brief Clear WEL bit unconditionally. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); + +/** + * @brief Set WREN bit. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_enable(esp_rom_spiflash_chip_t *spi); /** * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. diff --git a/components/esp_rom/include/esp32s3/rom/spi_flash.h b/components/esp_rom/include/esp32s3/rom/spi_flash.h index 4f16106cee..e9b75fafb0 100644 --- a/components/esp_rom/include/esp32s3/rom/spi_flash.h +++ b/components/esp_rom/include/esp32s3/rom/spi_flash.h @@ -88,16 +88,6 @@ extern "C" { #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM 16 #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_BITS 0xf -//SPI status register -#define ESP_ROM_SPIFLASH_BUSY_FLAG BIT0 -#define ESP_ROM_SPIFLASH_WRENABLE_FLAG BIT1 -#define ESP_ROM_SPIFLASH_BP0 BIT2 -#define ESP_ROM_SPIFLASH_BP1 BIT3 -#define ESP_ROM_SPIFLASH_BP2 BIT4 -#define ESP_ROM_SPIFLASH_WR_PROTECT (ESP_ROM_SPIFLASH_BP0|ESP_ROM_SPIFLASH_BP1|ESP_ROM_SPIFLASH_BP2) -#define ESP_ROM_SPIFLASH_QE BIT9 - -#define FLASH_ID_GD25LQ32C 0xC86016 typedef void (*spi_flash_func_t)(void); typedef esp_rom_spiflash_result_t (*spi_flash_op_t)(void); @@ -127,6 +117,345 @@ typedef struct { spi_flash_erase_area_t erase_area; } spiflash_legacy_funcs_t; +typedef struct { + uint8_t data_length; + uint8_t read_cmd0; + uint8_t read_cmd1; + uint8_t write_cmd; + uint16_t data_mask; + uint16_t data; +} esp_rom_spiflash_common_cmd_t; + +/** + * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t legacy: always keeping false. + * + * @return None + */ +void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); + +/** + * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief Write status to Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t status_value : Value to . + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); + +/** + * @brief Use a command to Read Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t*status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); + +/** + * @brief Config SPI Flash read mode when init. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. + * + * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); + +/** + * @brief Config SPI Flash clock divisor. + * Please do not call this function in SDK. + * + * @param uint8_t freqdiv: clock divisor. + * + * @param uint8_t spi: 0 for SPI0, 1 for SPI1. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); + +/** + * @brief Clear all SR bits except QE bit. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_clear_bp(void); + +/** + * @brief Clear all SR bits except QE bit. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); + +/** + * @brief Update SPI Flash parameter. + * Please do not call this function in SDK. + * + * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. + * + * @param uint32_t chip_size : The Flash size. + * + * @param uint32_t block_size : The Flash block size. + * + * @param uint32_t sector_size : The Flash sector size. + * + * @param uint32_t page_size : The Flash page size. + * + * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, + uint32_t sector_size, uint32_t page_size, uint32_t status_mask); + +/** + * @brief Erase whole flash chip. + * Please do not call this function in SDK. + * + * @param None + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); + +/** + * @brief Erase a 64KB block of flash + * Uses SPI flash command D8H. + * Please do not call this function in SDK. + * + * @param uint32_t block_num : Which block to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); + +/** + * @brief Erase a sector of flash. + * Uses SPI flash command 20H. + * Please do not call this function in SDK. + * + * @param uint32_t sector_num : Which sector to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); + +/** + * @brief Erase some sectors. + * Please do not call this function in SDK. + * + * @param uint32_t start_addr : Start addr to erase, should be sector aligned. + * + * @param uint32_t area_len : Length to erase, should be sector aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); + +/** + * @brief Write Data to Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. + * + * @param const uint32_t *src : The pointer to data which is to write. + * + * @param uint32_t len : Length to write, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); + +/** + * @brief Read Data from Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. + * + * @param uint32_t *dest : The buf to read the data. + * + * @param uint32_t len : Length to read, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); + +/** + * @brief SPI1 go into encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_enable(void); + +/** + * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. + * + * @param uint32_t *data : The pointer to data which is to write. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); + +/** + * @brief SPI1 go out of encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_disable(void); + +/** + * @brief Write data to flash with transparent encryption. + * @note Sectors to be written should already be erased. + * + * @note Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. + * + * @param uint32_t *data : The pointer to data to write. Note, this pointer must + * be 32 bit aligned and the content of the data will be + * modified by the encryption function. + * + * @param uint32_t len : Length to write, should be 32 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. + * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); + + +/** @brief Wait until SPI flash write operation is complete + * + * @note Please do not call this function in SDK. + * + * Reads the Write In Progress bit of the SPI flash status register, + * repeats until this bit is zero (indicating write complete). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete + * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); + + +/** @brief Enable Quad I/O pin functions + * + * @note Please do not call this function in SDK. + * + * Sets the HD & WP pin functions for Quad I/O modes, based on the + * efuse SPI pin configuration. + * + * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. + * + * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). + * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. + * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. + * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used + * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). + * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. + */ +void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); + +/** + * @brief Clear WEL bit unconditionally. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); + +/** + * @brief Set WREN bit. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_enable(esp_rom_spiflash_chip_t *spi); + /** * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. * Please do not call this function in SDK. @@ -193,6 +522,8 @@ void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); */ uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); +extern const spiflash_legacy_funcs_t *rom_spiflash_legacy_funcs; + #ifdef __cplusplus } #endif diff --git a/components/esp_rom/include/esp8684/rom/spi_flash.h b/components/esp_rom/include/esp8684/rom/spi_flash.h index fcf4308fbc..e93a3f8f11 100644 --- a/components/esp_rom/include/esp8684/rom/spi_flash.h +++ b/components/esp_rom/include/esp8684/rom/spi_flash.h @@ -58,17 +58,6 @@ extern "C" { #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM 16 #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_BITS 0xf -//SPI status register -#define ESP_ROM_SPIFLASH_BUSY_FLAG BIT0 -#define ESP_ROM_SPIFLASH_WRENABLE_FLAG BIT1 -#define ESP_ROM_SPIFLASH_BP0 BIT2 -#define ESP_ROM_SPIFLASH_BP1 BIT3 -#define ESP_ROM_SPIFLASH_BP2 BIT4 -#define ESP_ROM_SPIFLASH_WR_PROTECT (ESP_ROM_SPIFLASH_BP0|ESP_ROM_SPIFLASH_BP1|ESP_ROM_SPIFLASH_BP2) -#define ESP_ROM_SPIFLASH_QE BIT9 - -#define FLASH_ID_GD25LQ32C 0xC86016 - typedef void (* spi_flash_func_t)(void); typedef esp_rom_spiflash_result_t (* spi_flash_op_t)(void); typedef esp_rom_spiflash_result_t (* spi_flash_erase_t)(uint32_t); @@ -97,6 +86,345 @@ typedef struct { spi_flash_erase_area_t erase_area; } spiflash_legacy_funcs_t; +typedef struct { + uint8_t data_length; + uint8_t read_cmd0; + uint8_t read_cmd1; + uint8_t write_cmd; + uint16_t data_mask; + uint16_t data; +} esp_rom_spiflash_common_cmd_t; + +/** + * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode. + * Please do not call this function in SDK. + * + * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping + * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd + * + * @param uint8_t legacy: always keeping false. + * + * @return None + */ +void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy); + +/** + * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2). + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t *status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status); + +/** + * @brief Write status to Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t status_value : Value to . + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value); + +/** + * @brief Use a command to Read Flash status register. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @param uint32_t*status : The pointer to which to return the Flash status value. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd); + +/** + * @brief Config SPI Flash read mode when init. + * Please do not call this function in SDK. + * + * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD. + * + * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode); + +/** + * @brief Config SPI Flash clock divisor. + * Please do not call this function in SDK. + * + * @param uint8_t freqdiv: clock divisor. + * + * @param uint8_t spi: 0 for SPI0, 1 for SPI1. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : config error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); + +/** + * @brief Clear all SR bits except QE bit. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_clear_bp(void); + +/** + * @brief Clear all SR bits except QE bit. + * Please do not call this function in SDK. + * + * @param None. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); + +/** + * @brief Update SPI Flash parameter. + * Please do not call this function in SDK. + * + * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit. + * + * @param uint32_t chip_size : The Flash size. + * + * @param uint32_t block_size : The Flash block size. + * + * @param uint32_t sector_size : The Flash sector size. + * + * @param uint32_t page_size : The Flash page size. + * + * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Update error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size, + uint32_t sector_size, uint32_t page_size, uint32_t status_mask); + +/** + * @brief Erase whole flash chip. + * Please do not call this function in SDK. + * + * @param None + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void); + +/** + * @brief Erase a 64KB block of flash + * Uses SPI flash command D8H. + * Please do not call this function in SDK. + * + * @param uint32_t block_num : Which block to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num); + +/** + * @brief Erase a sector of flash. + * Uses SPI flash command 20H. + * Please do not call this function in SDK. + * + * @param uint32_t sector_num : Which sector to erase. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num); + +/** + * @brief Erase some sectors. + * Please do not call this function in SDK. + * + * @param uint32_t start_addr : Start addr to erase, should be sector aligned. + * + * @param uint32_t area_len : Length to erase, should be sector aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len); + +/** + * @brief Write Data to Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned. + * + * @param const uint32_t *src : The pointer to data which is to write. + * + * @param uint32_t len : Length to write, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len); + +/** + * @brief Read Data from Flash, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t src_addr : Address to read, should be 4 bytes aligned. + * + * @param uint32_t *dest : The buf to read the data. + * + * @param uint32_t len : Length to read, should be 4 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Read error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len); + +/** + * @brief SPI1 go into encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_enable(void); + +/** + * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need. + * Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned. + * + * @param uint32_t *data : The pointer to data which is to write. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK. + * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data); + +/** + * @brief SPI1 go out of encrypto mode. + * Please do not call this function in SDK. + * + * @param None + * + * @return None + */ +void esp_rom_spiflash_write_encrypted_disable(void); + +/** + * @brief Write data to flash with transparent encryption. + * @note Sectors to be written should already be erased. + * + * @note Please do not call this function in SDK. + * + * @param uint32_t flash_addr : Address to write, should be 32 byte aligned. + * + * @param uint32_t *data : The pointer to data to write. Note, this pointer must + * be 32 bit aligned and the content of the data will be + * modified by the encryption function. + * + * @param uint32_t len : Length to write, should be 32 bytes aligned. + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully. + * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error. + * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len); + + +/** @brief Wait until SPI flash write operation is complete + * + * @note Please do not call this function in SDK. + * + * Reads the Write In Progress bit of the SPI flash status register, + * repeats until this bit is zero (indicating write complete). + * + * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete + * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status. + */ +esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi); + + +/** @brief Enable Quad I/O pin functions + * + * @note Please do not call this function in SDK. + * + * Sets the HD & WP pin functions for Quad I/O modes, based on the + * efuse SPI pin configuration. + * + * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O. + * + * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig(). + * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored. + * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored. + * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used + * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI). + * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral. + */ +void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); + +/** + * @brief Clear WEL bit unconditionally. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); + +/** + * @brief Set WREN bit. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_enable(esp_rom_spiflash_chip_t *spi); + /** * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed. * Please do not call this function in SDK. @@ -163,6 +491,8 @@ void esp_rom_spiflash_select_padsfunc(uint32_t ishspi); */ uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd); +extern const spiflash_legacy_funcs_t *rom_spiflash_legacy_funcs; + #ifdef __cplusplus } #endif diff --git a/components/esp_rom/include/esp_rom_spiflash.h b/components/esp_rom/include/esp_rom_spiflash.h index 4f5c40f847..216b90fb8e 100644 --- a/components/esp_rom/include/esp_rom_spiflash.h +++ b/components/esp_rom/include/esp_rom_spiflash.h @@ -10,6 +10,7 @@ #include "sdkconfig.h" #include #include +#include "esp_rom_spiflash_defs.h" #ifdef __cplusplus extern "C" { @@ -27,6 +28,7 @@ typedef enum { ESP_ROM_SPIFLASH_OOUT_MODE, ESP_ROM_SPIFLASH_OIO_STR_MODE, ESP_ROM_SPIFLASH_OIO_DTR_MODE, + ESP_ROM_SPIFLASH_QPI_MODE, } esp_rom_spiflash_read_mode_t; typedef struct { @@ -38,15 +40,6 @@ typedef struct { uint32_t status_mask; } esp_rom_spiflash_chip_t; -typedef struct { - uint8_t data_length; - uint8_t read_cmd0; - uint8_t read_cmd1; - uint8_t write_cmd; - uint16_t data_mask; - uint16_t data; -} esp_rom_spiflash_common_cmd_t; - typedef enum { ESP_ROM_SPIFLASH_RESULT_OK, ESP_ROM_SPIFLASH_RESULT_ERR, @@ -150,30 +143,6 @@ esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read */ esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi); -/** - * @brief Unlock SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void); - -/** - * @brief SPI write protect. - * Please do not call this function in SDK. - * - * @param None. - * - * @return ESP_ROM_SPIFLASH_RESULT_OK : Lock OK. - * ESP_ROM_SPIFLASH_RESULT_ERR : Lock error. - * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Lock timeout. - */ -esp_rom_spiflash_result_t esp_rom_spiflash_lock(void); - /** * @brief Update SPI Flash parameter. * Please do not call this function in SDK. @@ -374,18 +343,26 @@ void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig); */ esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void); -typedef struct { - esp_rom_spiflash_chip_t chip; - uint8_t dummy_len_plus[3]; - uint8_t sig_matrix; -} esp_rom_spiflash_legacy_data_t; - +/** + * @brief Set WREN bit. + * + * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file. + * + * @return always ESP_ROM_SPIFLASH_RESULT_OK + */ +esp_rom_spiflash_result_t esp_rom_spiflash_write_enable(esp_rom_spiflash_chip_t *spi); /* Flash data defined in ROM*/ #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 extern esp_rom_spiflash_chip_t g_rom_flashchip; extern uint8_t g_rom_spiflash_dummy_len_plus[]; #else +typedef struct { + esp_rom_spiflash_chip_t chip; + uint8_t dummy_len_plus[3]; + uint8_t sig_matrix; +} esp_rom_spiflash_legacy_data_t; + extern esp_rom_spiflash_legacy_data_t *rom_spiflash_legacy_data; #define g_rom_flashchip (rom_spiflash_legacy_data->chip) #define g_rom_spiflash_dummy_len_plus (rom_spiflash_legacy_data->dummy_len_plus) diff --git a/components/esp_rom/include/esp_rom_spiflash_defs.h b/components/esp_rom/include/esp_rom_spiflash_defs.h new file mode 100644 index 0000000000..6bc131b0e9 --- /dev/null +++ b/components/esp_rom/include/esp_rom_spiflash_defs.h @@ -0,0 +1,31 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +#pragma once + +/********************************************************** + * Public definations for ROM + *********************************************************/ + +#ifdef __cplusplus +extern "C" { +#endif + +#define ESP_ROM_SPIFLASH_BUSY_FLAG BIT0 +#define ESP_ROM_SPIFLASH_WRENABLE_FLAG BIT1 +#define ESP_ROM_SPIFLASH_BP0 BIT2 +#define ESP_ROM_SPIFLASH_BP1 BIT3 +#define ESP_ROM_SPIFLASH_BP2 BIT4 +#define ESP_ROM_SPIFLASH_WR_PROTECT (ESP_ROM_SPIFLASH_BP0|ESP_ROM_SPIFLASH_BP1|ESP_ROM_SPIFLASH_BP2) +#define ESP_ROM_SPIFLASH_QE BIT9 +#define ESP_ROM_SPIFLASH_BP_MASK_ISSI (BIT7 | BIT5 | BIT4 | BIT3 | BIT2) + +#define FLASH_ID_GD25LQ32C 0xC86016 + +#ifdef __cplusplus +} +#endif diff --git a/components/esp_rom/patches/esp_rom_spiflash.c b/components/esp_rom/patches/esp_rom_spiflash.c index a4fd608670..4c87e73bc2 100644 --- a/components/esp_rom/patches/esp_rom_spiflash.c +++ b/components/esp_rom/patches/esp_rom_spiflash.c @@ -6,6 +6,7 @@ #include "sdkconfig.h" #include "soc/spi_periph.h" +#include "esp_rom_spiflash.h" #if CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/spi_flash.h" #elif CONFIG_IDF_TARGET_ESP32S2 @@ -40,9 +41,9 @@ esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *sp return ESP_ROM_SPIFLASH_RESULT_OK; } -/* Modified version of esp_rom_spiflash_unlock() that replaces version in ROM. +/* Modified version of esp_rom_spiflash_clear_bp() that replaces version in ROM. - This works around a bug where esp_rom_spiflash_unlock sometimes reads the wrong + This works around a bug where esp_rom_spiflash_clear_bp sometimes reads the wrong high status byte (RDSR2 result) and then copies it back to the flash status, which can cause the CMP bit or Status Register Protect bit to become set. @@ -52,7 +53,7 @@ esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *sp about interrupts, CPU coordination, flash mapping. However some of the functions in esp_spi_flash.c call it. */ -__attribute__((__unused__)) esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void) +__attribute__((__unused__)) esp_rom_spiflash_result_t esp_rom_spiflash_clear_bp(void) { uint32_t status; uint32_t new_status; @@ -67,7 +68,7 @@ __attribute__((__unused__)) esp_rom_spiflash_result_t esp_rom_spiflash_unlock(vo } /* Clear all bits in the mask. - (This is different from ROM esp_rom_spiflash_unlock, which keeps all bits as-is.) + (This is different from ROM esp_rom_spiflash_clear_bp, which keeps all bits as-is.) */ new_status = status & (~ESP_ROM_SPIFLASH_BP_MASK_ISSI); // Skip if nothing needs to be cleared. Otherwise will waste time waiting for the flash to clear nothing. @@ -80,7 +81,7 @@ __attribute__((__unused__)) esp_rom_spiflash_result_t esp_rom_spiflash_unlock(vo } /* Clear all bits except QE, if it is set. - (This is different from ROM esp_rom_spiflash_unlock, which keeps all bits as-is.) + (This is different from ROM esp_rom_spiflash_clear_bp, which keeps all bits as-is.) */ new_status = status & ESP_ROM_SPIFLASH_QE; SET_PERI_REG_MASK(SPI_CTRL_REG(SPI_IDX), SPI_WRSR_2B); @@ -99,7 +100,7 @@ __attribute__((__unused__)) esp_rom_spiflash_result_t esp_rom_spiflash_unlock(vo } return ret; } - +esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void) __attribute__((alias("esp_rom_spiflash_clear_bp"))); static esp_rom_spiflash_result_t esp_rom_spiflash_enable_write(esp_rom_spiflash_chip_t *spi); @@ -369,7 +370,7 @@ static void spi_cache_mode_switch(uint32_t modebit) } } -esp_rom_spiflash_result_t esp_rom_spiflash_lock(void) +esp_rom_spiflash_result_t esp_rom_spiflash_set_bp(void) { uint32_t status; @@ -390,7 +391,7 @@ esp_rom_spiflash_result_t esp_rom_spiflash_lock(void) return ESP_ROM_SPIFLASH_RESULT_OK; } - +esp_rom_spiflash_result_t esp_rom_spiflash_lock(void) __attribute__((alias("esp_rom_spiflash_set_bp"))); esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode) { @@ -474,7 +475,7 @@ esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num) return ESP_ROM_SPIFLASH_RESULT_OK; } -esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t target, const uint32_t *src_addr, int32_t len) +esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len) { uint32_t page_size; uint32_t pgm_len; @@ -485,20 +486,20 @@ esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t target, const uint32_t REG_CLR_BIT(PERIPHS_SPI_FLASH_USRREG, SPI_USR_DUMMY); REG_SET_FIELD(PERIPHS_SPI_FLASH_USRREG1, SPI_USR_ADDR_BITLEN, ESP_ROM_SPIFLASH_W_SIO_ADDR_BITSLEN); //check program size - if ( (target + len) > (g_rom_spiflash_chip.chip_size)) { + if ( (dest_addr + len) > (g_rom_spiflash_chip.chip_size)) { return ESP_ROM_SPIFLASH_RESULT_ERR; } page_size = g_rom_spiflash_chip.page_size; - pgm_len = page_size - (target % page_size); + pgm_len = page_size - (dest_addr % page_size); if (len < pgm_len) { if (ESP_ROM_SPIFLASH_RESULT_OK != esp_rom_spiflash_program_page_internal(&g_rom_spiflash_chip, - target, (uint32_t *)src_addr, len)) { + dest_addr, (uint32_t *)src, len)) { return ESP_ROM_SPIFLASH_RESULT_ERR; } } else { if (ESP_ROM_SPIFLASH_RESULT_OK != esp_rom_spiflash_program_page_internal(&g_rom_spiflash_chip, - target, (uint32_t *)src_addr, pgm_len)) { + dest_addr, (uint32_t *)src, pgm_len)) { return ESP_ROM_SPIFLASH_RESULT_ERR; } @@ -506,7 +507,7 @@ esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t target, const uint32_t pgm_num = (len - pgm_len) / page_size; for (i = 0; i < pgm_num; i++) { if (ESP_ROM_SPIFLASH_RESULT_OK != esp_rom_spiflash_program_page_internal(&g_rom_spiflash_chip, - target + pgm_len, (uint32_t *)src_addr + (pgm_len >> 2), page_size)) { + dest_addr + pgm_len, (uint32_t *)src + (pgm_len >> 2), page_size)) { return ESP_ROM_SPIFLASH_RESULT_ERR; } pgm_len += page_size; @@ -514,7 +515,7 @@ esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t target, const uint32_t //remain parts to program if (ESP_ROM_SPIFLASH_RESULT_OK != esp_rom_spiflash_program_page_internal(&g_rom_spiflash_chip, - target + pgm_len, (uint32_t *)src_addr + (pgm_len >> 2), len - pgm_len)) { + dest_addr + pgm_len, (uint32_t *)src + (pgm_len >> 2), len - pgm_len)) { return ESP_ROM_SPIFLASH_RESULT_ERR; } } @@ -547,7 +548,7 @@ esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, return ret; } -esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t target, uint32_t *dest_addr, int32_t len) +esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len) { // QIO or SIO, non-QIO regard as SIO uint32_t modebit; @@ -602,7 +603,7 @@ esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t target, uint32_t *dest_ REG_WRITE(PERIPHS_SPI_FLASH_USRREG2, (0x7 << SPI_USR_COMMAND_BITLEN_S) | 0x03); } - if ( ESP_ROM_SPIFLASH_RESULT_OK != esp_rom_spiflash_read_data(&g_rom_spiflash_chip, target, dest_addr, len)) { + if ( ESP_ROM_SPIFLASH_RESULT_OK != esp_rom_spiflash_read_data(&g_rom_spiflash_chip, src_addr, dest, len)) { return ESP_ROM_SPIFLASH_RESULT_ERR; } return ESP_ROM_SPIFLASH_RESULT_OK; @@ -632,7 +633,7 @@ esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint3 } //Unlock flash to enable erase - if (ESP_ROM_SPIFLASH_RESULT_OK != esp_rom_spiflash_unlock(/*&g_rom_spiflash_chip*/)) { + if (ESP_ROM_SPIFLASH_RESULT_OK != esp_rom_spiflash_clear_bp(/*&g_rom_spiflash_chip*/)) { return ESP_ROM_SPIFLASH_RESULT_ERR; } diff --git a/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c b/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c index a725528513..f644415d8f 100644 --- a/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c +++ b/components/spi_flash/esp32s3/spi_flash_oct_flash_init.c @@ -30,7 +30,6 @@ const static char *TAG = "Octal Flash"; // default value is rom_default_spiflash_legacy_flash_func extern const spiflash_legacy_funcs_t *rom_spiflash_legacy_funcs; -extern int SPI_write_enable(void *spi); static uint32_t s_chip_id; @@ -78,7 +77,7 @@ static void s_set_flash_dtr_str_opi_mode(int spi_num, uint8_t val) int dummy = 0; int data_bit_len = 8; - SPI_write_enable(&g_rom_flashchip); + esp_rom_spiflash_write_enable(&g_rom_flashchip); //SPI command, WRCR2 esp_rom_opiflash_exec_cmd(spi_num, ESP_ROM_SPIFLASH_FASTRD_MODE, SPI_FLASH_SPI_CMD_WRCR2, cmd_len, @@ -129,7 +128,7 @@ static void s_set_flash_ouput_driver_strength(int spi_num, uint8_t strength) //Write //SPI command, WRSR/WRCR data_bit_len = 16; - SPI_write_enable(&g_rom_flashchip); + esp_rom_spiflash_write_enable(&g_rom_flashchip); esp_rom_opiflash_exec_cmd(spi_num, ESP_ROM_SPIFLASH_FASTRD_MODE, SPI_FLASH_SPI_CMD_WRSRCR, cmd_len, addr, addr_bit_len, diff --git a/components/spi_flash/sim/SpiFlash.h b/components/spi_flash/sim/SpiFlash.h index 89a3048001..1071201a4f 100644 --- a/components/spi_flash/sim/SpiFlash.h +++ b/components/spi_flash/sim/SpiFlash.h @@ -10,7 +10,6 @@ #include #include "esp_err.h" -#include "esp32/rom/spi_flash.h" #include "esp_rom_spiflash.h" /** diff --git a/components/spi_flash/sim/flash_mock.cpp b/components/spi_flash/sim/flash_mock.cpp index 90c19f3d0a..6e757d3aca 100644 --- a/components/spi_flash/sim/flash_mock.cpp +++ b/components/spi_flash/sim/flash_mock.cpp @@ -7,7 +7,6 @@ #include "esp_partition.h" #include "esp_err.h" -#include "esp32/rom/spi_flash.h" #include "esp_rom_spiflash.h" SpiFlash spiflash = SpiFlash(); diff --git a/components/spi_flash/test/test_read_write.c b/components/spi_flash/test/test_read_write.c index 4ead359e71..12bd471af6 100644 --- a/components/spi_flash/test/test_read_write.c +++ b/components/spi_flash/test/test_read_write.c @@ -18,18 +18,10 @@ #include "../cache_utils.h" #include "soc/timer_periph.h" #include "esp_heap_caps.h" +#include "esp_rom_spiflash.h" #if CONFIG_IDF_TARGET_ESP32 +// Used for rom_fix function #include "esp32/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32C3 -#include "esp32c3/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP32H2 -#include "esp32h2/rom/spi_flash.h" -#elif CONFIG_IDF_TARGET_ESP8684 -#include "esp8684/rom/spi_flash.h" #endif #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP8684) diff --git a/components/spi_flash/test/test_spi_flash.c b/components/spi_flash/test/test_spi_flash.c index 036270e7e1..dd5a5f69a2 100644 --- a/components/spi_flash/test/test_spi_flash.c +++ b/components/spi_flash/test/test_spi_flash.c @@ -417,7 +417,7 @@ TEST_CASE("rom unlock will not erase QE bit", "[spi_flash]") if (((legacy_chip->device_id >> 16) & 0xff) != 0x9D) { TEST_IGNORE_MESSAGE("This test is only for ISSI chips. Ignore."); } - esp_rom_spiflash_unlock(); + bootloader_flash_unlock(); esp_rom_spiflash_read_status(legacy_chip, &status); printf("status: %08x\n", status); diff --git a/docs/en/migration-guides/peripherals.rst b/docs/en/migration-guides/peripherals.rst index 7ef2fc990b..6581925207 100644 --- a/docs/en/migration-guides/peripherals.rst +++ b/docs/en/migration-guides/peripherals.rst @@ -11,4 +11,8 @@ However, for advanced users who implement their own drivers based on ``hal`` and SPI Flash Interface ------------------- -Version before v5.0, spi flash functions in rom can be included by ``esp32**/rom/spi_flash.h``. However, it duplicates so much and not clean. Therefore, it has been moved to ``esp_rom_spiflash.h``. If you want to use the functions with prefix ``esp_rom_spiflash`` , please include ``esp_rom_spiflash.h`` +Version before v5.0, spi flash functions in rom can be included by ``esp32**/rom/spi_flash.h``. However, your code written for different chips may be filled with ROM headers of different versions. At the meantime not all the APIs can be used on all chips. + +Therefore, the common APIs are extracted to ``esp_rom_spiflash.h``. Although it's not a breaking change, it is strongly recommended to only use the functions with prefix ``esp_rom_spiflash`` included by ``esp_rom_spiflash.h`` for better cross-compatibility. + +To make the API clearer, we renamed the function ``esp_rom_spiflash_lock`` to ``esp_rom_spiflash_set_bp``. We renamed ``esp_rom_spiflash_unlock`` to ``esp_rom_spiflash_clear_bp``.