Skip to content

Commit 15fa784

Browse files
committed
refactor(spi_flash): Add spi_flash driver list linked check
1 parent 9546532 commit 15fa784

File tree

11 files changed

+144
-76
lines changed

11 files changed

+144
-76
lines changed

components/spi_flash/esp_flash_spi_init.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "esp_private/spi_share_hw_ctrl.h"
1818
#include "esp_ldo_regulator.h"
1919
#include "hal/spi_flash_hal.h"
20+
#include "spi_flash_chip_driver.h"
2021
#include "hal/gpio_hal.h"
2122
#include "esp_flash_internal.h"
2223
#include "esp_rom_gpio.h"
@@ -370,6 +371,9 @@ esp_err_t esp_flash_init_default_chip(void)
370371
s_esp_flash_choose_correct_mode(&cfg);
371372
#endif
372373

374+
#if !CONFIG_SPI_FLASH_OVERRIDE_CHIP_DRIVER_LIST
375+
spi_flash_chip_list_check(&default_chip, legacy_chip->device_id);
376+
#endif
373377

374378
// For chips need time tuning, get value directly from system here.
375379
#if SOC_SPI_MEM_SUPPORT_TIMING_TUNING

components/spi_flash/include/spi_flash/spi_flash_defs.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -80,3 +80,13 @@
8080
#define SPI_FLASH_OPIDTR_DUMMY_BITLEN 40
8181
#define SPI_FLASH_QIO_HPM_DUMMY_BITLEN 10
8282
#define SPI_FLASH_DIO_HPM_DUMMY_BITLEN 8
83+
84+
// Flash vendors.
85+
#define SPI_FLASH_GD 0xC8
86+
#define SPI_FLASH_ISSI 0x9D
87+
#define SPI_FLASH_MXIC 0xC2
88+
#define SPI_FLASH_XMC_1 0x20
89+
#define SPI_FLASH_XMC_2 0x46
90+
#define SPI_FLASH_WINBOND 0xEF
91+
#define SPI_FLASH_TH 0xCD
92+
#define SPI_FLASH_BY 0x68
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
// Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
1+
/*
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
146

157
#pragma once
168

179
#include <stdint.h>
1810
#include "esp_flash.h"
1911
#include "spi_flash_chip_driver.h"
12+
#include "sdkconfig.h"
2013

14+
#ifdef CONFIG_SPI_FLASH_SUPPORT_BOYA_CHIP
2115
extern const spi_flash_chip_t esp_flash_chip_boya;
16+
#else
17+
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_boya;
18+
#endif

components/spi_flash/include/spi_flash_chip_driver.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -212,3 +212,15 @@ struct spi_flash_chip_t {
212212
This pointer can be overwritten with a pointer to a new array, to update the list of known flash chips.
213213
*/
214214
extern const spi_flash_chip_t **esp_flash_registered_chips;
215+
216+
/**
217+
* @brief Helper function to check if a specific flash chip driver is linked.
218+
*
219+
* Logs a warning if the driver for the detected chip is not linked.
220+
*
221+
* @param chip Pointer to an `esp_flash_t` structure representing the flash chip instance.
222+
* This structure typically contains information about the chip's characteristics and state.
223+
* @param device_id A 24-bit value representing the unique identifier (ID) of the flash chip.
224+
* This ID is used to match the chip against known drivers.
225+
*/
226+
void spi_flash_chip_list_check(esp_flash_t *chip, uint32_t device_id);
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
1+
/*
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
146

157
#pragma once
168

179
#include <stdint.h>
1810
#include "esp_flash.h"
1911
#include "spi_flash_chip_driver.h"
12+
#include "sdkconfig.h"
2013

2114

2215
/**
@@ -33,4 +26,8 @@ esp_err_t spi_flash_chip_gd_probe(esp_flash_t *chip, uint32_t flash_id);
3326
esp_err_t spi_flash_chip_gd_set_io_mode(esp_flash_t *chip);
3427
esp_err_t spi_flash_chip_gd_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode);
3528

29+
#ifdef CONFIG_SPI_FLASH_SUPPORT_GD_CHIP
3630
extern const spi_flash_chip_t esp_flash_chip_gd;
31+
#else
32+
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_gd;
33+
#endif
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
1+
/*
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
146

157
#pragma once
168

179
#include <stdint.h>
1810
#include "esp_flash.h"
1911
#include "spi_flash_chip_driver.h"
12+
#include "sdkconfig.h"
2013

2114

2215
/**
@@ -28,4 +21,8 @@ esp_err_t spi_flash_chip_issi_probe(esp_flash_t *chip, uint32_t flash_id);
2821
esp_err_t spi_flash_chip_issi_set_io_mode(esp_flash_t *chip);
2922
esp_err_t spi_flash_chip_issi_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode);
3023

24+
#ifdef CONFIG_SPI_FLASH_SUPPORT_ISSI_CHIP
3125
extern const spi_flash_chip_t esp_flash_chip_issi;
26+
#else
27+
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_issi;
28+
#endif
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
1+
/*
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
146

157
#pragma once
168

179
#include <stdint.h>
1810
#include "esp_flash.h"
1911
#include "spi_flash_chip_driver.h"
20-
12+
#include "sdkconfig.h"
2113

2214
/**
2315
* MXIC SPI flash chip_drv, uses all the above functions for its operations. In
2416
* default autodetection, this is used as a catchall if a more specific chip_drv
2517
* is not found.
2618
*/
19+
#ifdef CONFIG_SPI_FLASH_SUPPORT_MXIC_CHIP
2720
extern const spi_flash_chip_t esp_flash_chip_mxic;
21+
#else
22+
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_mxic;
23+
#endif
2824

2925
/**
3026
* MXIC OPI flash chip_drv, uses all the above functions for its operations. In
3127
* default autodetection, this is used as a catchall if a more specific chip_drv
3228
* is not found.
3329
*/
30+
#ifdef CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP
3431
extern const spi_flash_chip_t esp_flash_chip_mxic_opi;
32+
#else
33+
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_mxic_opi;
34+
#endif

components/spi_flash/include/spi_flash_chip_th.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -9,5 +9,10 @@
99
#include <stdint.h>
1010
#include "esp_flash.h"
1111
#include "spi_flash_chip_driver.h"
12+
#include "sdkconfig.h"
1213

14+
#ifdef CONFIG_SPI_FLASH_SUPPORT_TH_CHIP
1315
extern const spi_flash_chip_t esp_flash_chip_th;
16+
#else
17+
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_th;
18+
#endif
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
// Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
1+
/*
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
146

157
#pragma once
168

179
#include <stdint.h>
1810
#include "esp_flash.h"
1911
#include "spi_flash_chip_driver.h"
20-
12+
#include "sdkconfig.h"
2113

2214
/**
2315
* Winbond SPI flash chip_drv, uses all the above functions for its operations. In
2416
* default autodetection, this is used as a catchall if a more specific chip_drv
2517
* is not found.
2618
*/
19+
20+
#ifdef CONFIG_SPI_FLASH_SUPPORT_WINBOND_CHIP
2721
extern const spi_flash_chip_t esp_flash_chip_winbond;
22+
#else
23+
extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_winbond;
24+
#endif

components/spi_flash/spi_flash_chip_drivers.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -13,7 +13,11 @@
1313
#include "spi_flash_chip_winbond.h"
1414
#include "spi_flash_chip_boya.h"
1515
#include "spi_flash_chip_th.h"
16+
#include "spi_flash_defs.h"
1617
#include "sdkconfig.h"
18+
#include "esp_log.h"
19+
20+
#define TAG "spi_flash"
1721

1822
#if !CONFIG_SPI_FLASH_OVERRIDE_CHIP_DRIVER_LIST
1923
/*
@@ -52,6 +56,56 @@ static const spi_flash_chip_t *default_registered_chips[] = {
5256
&esp_flash_chip_generic,
5357
NULL,
5458
};
59+
60+
#pragma GCC diagnostic push
61+
#pragma GCC diagnostic ignored "-Waddress"
62+
void spi_flash_chip_list_check(esp_flash_t *chip, uint32_t device_id) {
63+
uint8_t vendor_id = device_id >> 16;
64+
switch (vendor_id)
65+
{
66+
case SPI_FLASH_GD:
67+
if (&esp_flash_chip_gd == NULL) {
68+
ESP_EARLY_LOGW(TAG, "GigaDevice detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_GD_CHIP`");
69+
}
70+
break;
71+
case SPI_FLASH_ISSI:
72+
if (&esp_flash_chip_issi == NULL) {
73+
ESP_EARLY_LOGW(TAG, "ISSI detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_ISSI_CHIP`");
74+
}
75+
break;
76+
case SPI_FLASH_TH:
77+
if (&esp_flash_chip_th == NULL) {
78+
ESP_EARLY_LOGW(TAG, "TH detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_TH_CHIP`");
79+
}
80+
break;
81+
case SPI_FLASH_WINBOND:
82+
if (&esp_flash_chip_winbond == NULL) {
83+
ESP_EARLY_LOGW(TAG, "winbond detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_WINBOND_CHIP`");
84+
}
85+
break;
86+
case SPI_FLASH_MXIC:
87+
// Need to tell the difference between octal and quad flash.
88+
if (chip->read_mode < SPI_FLASH_OPI_FLAG) {
89+
if (&esp_flash_chip_mxic == NULL) {
90+
ESP_EARLY_LOGW(TAG, "MXIC detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_MXIC_CHIP`");
91+
}
92+
} else {
93+
if (&esp_flash_chip_mxic_opi == NULL) {
94+
ESP_EARLY_LOGW(TAG, "MXIC detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_MXIC_OPI_CHIP`");
95+
}
96+
}
97+
break;
98+
case SPI_FLASH_BY:
99+
if (&esp_flash_chip_boya == NULL) {
100+
ESP_EARLY_LOGW(TAG, "boya detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_BOYA_CHIP`");
101+
}
102+
break;
103+
default:
104+
break;
105+
}
106+
}
107+
#pragma GCC diagnostic pop
108+
55109
#else
56110
//When the config option is enabled, user should provide this struct themselves.
57111
extern const spi_flash_chip_t *default_registered_chips[];

0 commit comments

Comments
 (0)