From 4c66d7e6df1c916a30f0246fd96e3e465eb7b752 Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Tue, 21 Mar 2023 12:10:38 +0800 Subject: [PATCH] esp_flash: correct veriable 'size' description --- components/spi_flash/esp_flash_spi_init.c | 2 +- components/spi_flash/include/esp_flash.h | 6 +-- components/spi_flash/spi_flash_chip_generic.c | 2 +- components/spi_flash/spi_flash_chip_mxic.c | 40 ++++++++++++------- .../sdkconfig.ci.release_f8r8 | 1 + tools/ci/check_copyright_ignore.txt | 1 - 6 files changed, 32 insertions(+), 20 deletions(-) diff --git a/components/spi_flash/esp_flash_spi_init.c b/components/spi_flash/esp_flash_spi_init.c index efdb76eadd..d4ac362e2b 100644 --- a/components/spi_flash/esp_flash_spi_init.c +++ b/components/spi_flash/esp_flash_spi_init.c @@ -372,7 +372,7 @@ esp_err_t esp_flash_init_default_chip(void) if (default_chip.size > legacy_chip->chip_size) { ESP_EARLY_LOGW(TAG, "Detected size(%dk) larger than the size in the binary image header(%dk). Using the size in the binary image header.", default_chip.size/1024, legacy_chip->chip_size/1024); } - // Set chip->size equal to ROM flash size(also equal to menuconfig flash size), which means the available size that can be used + // Set chip->size equal to ROM flash size(also equal to the size in binary image header), which means the available size that can be used default_chip.size = legacy_chip->chip_size; esp_flash_default_chip = &default_chip; diff --git a/components/spi_flash/include/esp_flash.h b/components/spi_flash/include/esp_flash.h index 5933f30678..405b2f95f0 100644 --- a/components/spi_flash/include/esp_flash.h +++ b/components/spi_flash/include/esp_flash.h @@ -97,7 +97,7 @@ struct esp_flash_t { void *os_func_data; ///< Pointer to argument for os-specific hooks. Left NULL and will be initialized with ``os_func``. esp_flash_io_mode_t read_mode; ///< Configured SPI flash read mode. Set before ``esp_flash_init`` is called. - uint32_t size; ///< Size of SPI flash in bytes. If 0, size will be detected during initialisation. Note: Only stands for the available size (`CONFIG_ESPTOOLPY_FLASHSIZE`), If you want to get the flash physical size, please call `esp_flash_get_physical_size`. + uint32_t size; ///< Size of SPI flash in bytes. If 0, size will be detected during initialisation. Note: this stands for the size in the binary image header. If you want to get the flash physical size, please call `esp_flash_get_physical_size`. uint32_t chip_id; ///< Detected chip id. uint32_t busy :1; ///< This flag is used to verify chip's status. uint32_t hpm_dummy_ena :1; ///< This flag is used to verify whether flash works under HPM status. @@ -145,11 +145,11 @@ esp_err_t esp_flash_read_id(esp_flash_t *chip, uint32_t *out_id); /** @brief Detect flash size based on flash ID. * * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init() - * @param[out] out_size Detected size in bytes, standing for the available size (`CONFIG_ESPTOOLPY_FLASHSIZE`). + * @param[out] out_size Detected size in bytes, standing for the size in the binary image header. * * @note 1. Most flash chips use a common format for flash ID, where the lower 4 bits specify the size as a power of 2. If * the manufacturer doesn't follow this convention, the size may be incorrectly detected. - * 2. The out_size returned only stands for the size selected in menuconfig. + * 2. The out_size returned only stands for The out_size stands for the size in the binary image header. * If you want to get the real size of the chip, please call `esp_flash_get_physical_size` instead. * * @return ESP_OK on success, or a flash error code if operation failed. diff --git a/components/spi_flash/spi_flash_chip_generic.c b/components/spi_flash/spi_flash_chip_generic.c index d63bb73afc..7efd5cbff2 100644 --- a/components/spi_flash/spi_flash_chip_generic.c +++ b/components/spi_flash/spi_flash_chip_generic.c @@ -101,7 +101,7 @@ esp_err_t spi_flash_chip_generic_detect_size(esp_flash_t *chip, uint32_t *size) However, some other flash vendors also have their own rule, we will add them in chip specific files. */ uint32_t mem_density = (id & 0xFF); - if (mem_density > SPI_FLASH_LINEAR_DENSITY_LAST_VALUE ) { + if (mem_density > SPI_FLASH_LINEAR_DENSITY_LAST_VALUE) { mem_density -= SPI_FLASH_HEX_A_F_RANGE; } diff --git a/components/spi_flash/spi_flash_chip_mxic.c b/components/spi_flash/spi_flash_chip_mxic.c index fef8f99f19..0126f33cd6 100644 --- a/components/spi_flash/spi_flash_chip_mxic.c +++ b/components/spi_flash/spi_flash_chip_mxic.c @@ -1,16 +1,8 @@ -// 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-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include "spi_flash_chip_generic.h" @@ -35,6 +27,26 @@ esp_err_t spi_flash_chip_mxic_probe(esp_flash_t *chip, uint32_t flash_id) return ESP_OK; } +esp_err_t spi_flash_chip_mxic_detect_size(esp_flash_t *chip, uint32_t *size) +{ + uint32_t id = chip->chip_id; + *size = 0; + + /* Can't detect size unless the high byte of the product ID matches the same convention, which is usually 0x40 or + * 0xC0 or similar. */ + if (((id & 0xFFFF) == 0x0000) || ((id & 0xFFFF) == 0xFFFF)) { + return ESP_ERR_FLASH_UNSUPPORTED_CHIP; + } + + uint32_t mem_density = (id & 0xFF); + if (mem_density > 0x30) { // For OPI chips + mem_density -= 0x20; + } + + *size = 1 << mem_density; + return ESP_OK; +} + esp_err_t spi_flash_chip_issi_set_io_mode(esp_flash_t *chip); esp_err_t spi_flash_chip_issi_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode); @@ -61,7 +73,7 @@ const spi_flash_chip_t esp_flash_chip_mxic = { .timeout = &spi_flash_chip_generic_timeout, .probe = spi_flash_chip_mxic_probe, .reset = spi_flash_chip_generic_reset, - .detect_size = spi_flash_chip_generic_detect_size, + .detect_size = spi_flash_chip_mxic_detect_size, .erase_chip = spi_flash_chip_generic_erase_chip, .erase_sector = spi_flash_chip_generic_erase_sector, .erase_block = spi_flash_chip_generic_erase_block, diff --git a/components/spi_flash/test_apps/flash_encryption/sdkconfig.ci.release_f8r8 b/components/spi_flash/test_apps/flash_encryption/sdkconfig.ci.release_f8r8 index 5acf0f9f59..0d3585b0cb 100644 --- a/components/spi_flash/test_apps/flash_encryption/sdkconfig.ci.release_f8r8 +++ b/components/spi_flash/test_apps/flash_encryption/sdkconfig.ci.release_f8r8 @@ -15,4 +15,5 @@ CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE=y CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED=y CONFIG_SPIRAM_MODE_OCT=y CONFIG_SPIRAM_TYPE_AUTO=y +CONFIG_ESPTOOLPY_OCT_FLASH=y CONFIG_ESPTOOLPY_FLASHMODE_OPI=y diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 85ce100691..dd91d8fcb0 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -1238,7 +1238,6 @@ components/spi_flash/sim/sdkconfig/sdkconfig.h components/spi_flash/sim/stubs/bsd/strlcpy.c components/spi_flash/spi_flash_chip_boya.c components/spi_flash/spi_flash_chip_issi.c -components/spi_flash/spi_flash_chip_mxic.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