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 <damien@micropython.org>
This commit is contained in:
Damien George
2025-04-02 12:47:48 +11:00
parent e7edf0783e
commit b078569cff
2 changed files with 19 additions and 3 deletions

View File

@@ -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;
}

View File

@@ -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);