From b078569cffb4b32585806b42ef7a0501bc4d0bca Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 2 Apr 2025 12:47:48 +1100 Subject: [PATCH] drivers/memory/spiflash: Allow a board/port to detect SPI flash. This commit allows the user of this driver to intercept the SPI flash initialisation routine and possibly take some action based on the JEDEC id, for example change the `mp_spiflash_t::chip_params` element. To do this, enable `MICROPY_HW_SPIFLASH_DETECT_DEVICE` and define a function called `mp_spiflash_detect()`. Signed-off-by: Damien George --- drivers/memory/spiflash.c | 12 +++++++++--- drivers/memory/spiflash.h | 10 ++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/drivers/memory/spiflash.c b/drivers/memory/spiflash.c index 09ab7157b8..b4133fe2f0 100644 --- a/drivers/memory/spiflash.c +++ b/drivers/memory/spiflash.c @@ -31,6 +31,10 @@ #include "py/mphal.h" #include "drivers/memory/spiflash.h" +#if defined(CHECK_DEVID) +#error "CHECK_DEVID no longer supported, use MICROPY_HW_SPIFLASH_DETECT_DEVICE instead" +#endif + #define QSPI_QE_MASK (0x02) #define USE_WR_DELAY (1) @@ -198,11 +202,13 @@ void mp_spiflash_init(mp_spiflash_t *self) { mp_hal_delay_ms(1); #endif - #if defined(CHECK_DEVID) - // Validate device id + #if MICROPY_HW_SPIFLASH_DETECT_DEVICE + // Attempt to detect SPI flash based on its JEDEC id. uint32_t devid; int ret = mp_spiflash_read_cmd(self, CMD_RD_DEVID, 3, &devid); - if (ret != 0 || devid != CHECK_DEVID) { + ret = mp_spiflash_detect(self, ret, devid); + if (ret != 0) { + // Could not read device id. mp_spiflash_release_bus(self); return; } diff --git a/drivers/memory/spiflash.h b/drivers/memory/spiflash.h index a22742fc6f..d98047c89f 100644 --- a/drivers/memory/spiflash.h +++ b/drivers/memory/spiflash.h @@ -34,6 +34,11 @@ #define MICROPY_HW_SPIFLASH_CHIP_PARAMS (0) #endif +// Whether to enable detection of SPI flash during initialisation. +#ifndef MICROPY_HW_SPIFLASH_DETECT_DEVICE +#define MICROPY_HW_SPIFLASH_DETECT_DEVICE (0) +#endif + #define MP_SPIFLASH_ERASE_BLOCK_SIZE (4096) // must be a power of 2 enum { @@ -91,6 +96,11 @@ typedef struct _mp_spiflash_t { void mp_spiflash_init(mp_spiflash_t *self); void mp_spiflash_deepsleep(mp_spiflash_t *self, int value); +#if MICROPY_HW_SPIFLASH_DETECT_DEVICE +// A board/port should define this function to perform actions based on the JEDEC id. +int mp_spiflash_detect(mp_spiflash_t *spiflash, int ret, uint32_t devid); +#endif + // These functions go direct to the SPI flash device int mp_spiflash_erase_block(mp_spiflash_t *self, uint32_t addr); int mp_spiflash_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest);