refactor(spi_flash): Add spi_flash driver list linked check

This commit is contained in:
C.S.M
2025-04-10 15:31:06 +08:00
parent 95465323c6
commit 15fa7843ae
11 changed files with 144 additions and 76 deletions

View File

@@ -17,6 +17,7 @@
#include "esp_private/spi_share_hw_ctrl.h"
#include "esp_ldo_regulator.h"
#include "hal/spi_flash_hal.h"
#include "spi_flash_chip_driver.h"
#include "hal/gpio_hal.h"
#include "esp_flash_internal.h"
#include "esp_rom_gpio.h"
@@ -370,6 +371,9 @@ esp_err_t esp_flash_init_default_chip(void)
s_esp_flash_choose_correct_mode(&cfg);
#endif
#if !CONFIG_SPI_FLASH_OVERRIDE_CHIP_DRIVER_LIST
spi_flash_chip_list_check(&default_chip, legacy_chip->device_id);
#endif
// For chips need time tuning, get value directly from system here.
#if SOC_SPI_MEM_SUPPORT_TIMING_TUNING

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -80,3 +80,13 @@
#define SPI_FLASH_OPIDTR_DUMMY_BITLEN 40
#define SPI_FLASH_QIO_HPM_DUMMY_BITLEN 10
#define SPI_FLASH_DIO_HPM_DUMMY_BITLEN 8
// Flash vendors.
#define SPI_FLASH_GD 0xC8
#define SPI_FLASH_ISSI 0x9D
#define SPI_FLASH_MXIC 0xC2
#define SPI_FLASH_XMC_1 0x20
#define SPI_FLASH_XMC_2 0x46
#define SPI_FLASH_WINBOND 0xEF
#define SPI_FLASH_TH 0xCD
#define SPI_FLASH_BY 0x68

View File

@@ -1,21 +1,18 @@
// Copyright 2015-2021 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-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include "esp_flash.h"
#include "spi_flash_chip_driver.h"
#include "sdkconfig.h"
#ifdef CONFIG_SPI_FLASH_SUPPORT_BOYA_CHIP
extern const spi_flash_chip_t esp_flash_chip_boya;
#else
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_boya;
#endif

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -212,3 +212,15 @@ struct spi_flash_chip_t {
This pointer can be overwritten with a pointer to a new array, to update the list of known flash chips.
*/
extern const spi_flash_chip_t **esp_flash_registered_chips;
/**
* @brief Helper function to check if a specific flash chip driver is linked.
*
* Logs a warning if the driver for the detected chip is not linked.
*
* @param chip Pointer to an `esp_flash_t` structure representing the flash chip instance.
* This structure typically contains information about the chip's characteristics and state.
* @param device_id A 24-bit value representing the unique identifier (ID) of the flash chip.
* This ID is used to match the chip against known drivers.
*/
void spi_flash_chip_list_check(esp_flash_t *chip, uint32_t device_id);

View File

@@ -1,22 +1,15 @@
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include "esp_flash.h"
#include "spi_flash_chip_driver.h"
#include "sdkconfig.h"
/**
@@ -33,4 +26,8 @@ esp_err_t spi_flash_chip_gd_probe(esp_flash_t *chip, uint32_t flash_id);
esp_err_t spi_flash_chip_gd_set_io_mode(esp_flash_t *chip);
esp_err_t spi_flash_chip_gd_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode);
#ifdef CONFIG_SPI_FLASH_SUPPORT_GD_CHIP
extern const spi_flash_chip_t esp_flash_chip_gd;
#else
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_gd;
#endif

View File

@@ -1,22 +1,15 @@
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include "esp_flash.h"
#include "spi_flash_chip_driver.h"
#include "sdkconfig.h"
/**
@@ -28,4 +21,8 @@ esp_err_t spi_flash_chip_issi_probe(esp_flash_t *chip, uint32_t flash_id);
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);
#ifdef CONFIG_SPI_FLASH_SUPPORT_ISSI_CHIP
extern const spi_flash_chip_t esp_flash_chip_issi;
#else
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_issi;
#endif

View File

@@ -1,34 +1,34 @@
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include "esp_flash.h"
#include "spi_flash_chip_driver.h"
#include "sdkconfig.h"
/**
* MXIC SPI flash chip_drv, uses all the above functions for its operations. In
* default autodetection, this is used as a catchall if a more specific chip_drv
* is not found.
*/
#ifdef CONFIG_SPI_FLASH_SUPPORT_MXIC_CHIP
extern const spi_flash_chip_t esp_flash_chip_mxic;
#else
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_mxic;
#endif
/**
* MXIC OPI flash chip_drv, uses all the above functions for its operations. In
* default autodetection, this is used as a catchall if a more specific chip_drv
* is not found.
*/
#ifdef CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP
extern const spi_flash_chip_t esp_flash_chip_mxic_opi;
#else
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_mxic_opi;
#endif

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -9,5 +9,10 @@
#include <stdint.h>
#include "esp_flash.h"
#include "spi_flash_chip_driver.h"
#include "sdkconfig.h"
#ifdef CONFIG_SPI_FLASH_SUPPORT_TH_CHIP
extern const spi_flash_chip_t esp_flash_chip_th;
#else
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_th;
#endif

View File

@@ -1,27 +1,24 @@
// 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-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include "esp_flash.h"
#include "spi_flash_chip_driver.h"
#include "sdkconfig.h"
/**
* Winbond SPI flash chip_drv, uses all the above functions for its operations. In
* default autodetection, this is used as a catchall if a more specific chip_drv
* is not found.
*/
#ifdef CONFIG_SPI_FLASH_SUPPORT_WINBOND_CHIP
extern const spi_flash_chip_t esp_flash_chip_winbond;
#else
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_winbond;
#endif

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -13,7 +13,11 @@
#include "spi_flash_chip_winbond.h"
#include "spi_flash_chip_boya.h"
#include "spi_flash_chip_th.h"
#include "spi_flash_defs.h"
#include "sdkconfig.h"
#include "esp_log.h"
#define TAG "spi_flash"
#if !CONFIG_SPI_FLASH_OVERRIDE_CHIP_DRIVER_LIST
/*
@@ -52,6 +56,56 @@ static const spi_flash_chip_t *default_registered_chips[] = {
&esp_flash_chip_generic,
NULL,
};
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Waddress"
void spi_flash_chip_list_check(esp_flash_t *chip, uint32_t device_id) {
uint8_t vendor_id = device_id >> 16;
switch (vendor_id)
{
case SPI_FLASH_GD:
if (&esp_flash_chip_gd == NULL) {
ESP_EARLY_LOGW(TAG, "GigaDevice detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_GD_CHIP`");
}
break;
case SPI_FLASH_ISSI:
if (&esp_flash_chip_issi == NULL) {
ESP_EARLY_LOGW(TAG, "ISSI detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_ISSI_CHIP`");
}
break;
case SPI_FLASH_TH:
if (&esp_flash_chip_th == NULL) {
ESP_EARLY_LOGW(TAG, "TH detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_TH_CHIP`");
}
break;
case SPI_FLASH_WINBOND:
if (&esp_flash_chip_winbond == NULL) {
ESP_EARLY_LOGW(TAG, "winbond detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_WINBOND_CHIP`");
}
break;
case SPI_FLASH_MXIC:
// Need to tell the difference between octal and quad flash.
if (chip->read_mode < SPI_FLASH_OPI_FLAG) {
if (&esp_flash_chip_mxic == NULL) {
ESP_EARLY_LOGW(TAG, "MXIC detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_MXIC_CHIP`");
}
} else {
if (&esp_flash_chip_mxic_opi == NULL) {
ESP_EARLY_LOGW(TAG, "MXIC detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_MXIC_OPI_CHIP`");
}
}
break;
case SPI_FLASH_BY:
if (&esp_flash_chip_boya == NULL) {
ESP_EARLY_LOGW(TAG, "boya detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_BOYA_CHIP`");
}
break;
default:
break;
}
}
#pragma GCC diagnostic pop
#else
//When the config option is enabled, user should provide this struct themselves.
extern const spi_flash_chip_t *default_registered_chips[];

View File

@@ -515,12 +515,7 @@ components/soc/esp32s2/include/soc/nrx_reg.h
components/soc/esp32s2/include/soc/soc_ulp.h
components/soc/esp32s2/include/soc/touch_sensor_channel.h
components/soc/esp32s2/include/soc/touch_sensor_pins.h
components/spi_flash/include/spi_flash_chip_boya.h
components/spi_flash/include/spi_flash_chip_gd.h
components/spi_flash/include/spi_flash_chip_generic.h
components/spi_flash/include/spi_flash_chip_issi.h
components/spi_flash/include/spi_flash_chip_mxic.h
components/spi_flash/include/spi_flash_chip_winbond.h
components/spi_flash/spi_flash_chip_boya.c
components/spi_flash/spi_flash_chip_issi.c
components/tcp_transport/include/esp_transport_ws.h