Skip to content

Commit

Permalink
esp_flash: correct veriable 'size' description
Browse files Browse the repository at this point in the history
  • Loading branch information
mythbuster5 committed Mar 21, 2023
1 parent e94d951 commit 4c66d7e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion components/spi_flash/esp_flash_spi_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions components/spi_flash/include/esp_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion components/spi_flash/spi_flash_chip_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
40 changes: 26 additions & 14 deletions components/spi_flash/spi_flash_chip_mxic.c
Original file line number Diff line number Diff line change
@@ -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 <stdlib.h>
#include "spi_flash_chip_generic.h"
Expand All @@ -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);

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion tools/ci/check_copyright_ignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4c66d7e

Please sign in to comment.