Skip to content

Commit b078569

Browse files
committed
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 <[email protected]>
1 parent e7edf07 commit b078569

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

drivers/memory/spiflash.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
#include "py/mphal.h"
3232
#include "drivers/memory/spiflash.h"
3333

34+
#if defined(CHECK_DEVID)
35+
#error "CHECK_DEVID no longer supported, use MICROPY_HW_SPIFLASH_DETECT_DEVICE instead"
36+
#endif
37+
3438
#define QSPI_QE_MASK (0x02)
3539
#define USE_WR_DELAY (1)
3640

@@ -198,11 +202,13 @@ void mp_spiflash_init(mp_spiflash_t *self) {
198202
mp_hal_delay_ms(1);
199203
#endif
200204

201-
#if defined(CHECK_DEVID)
202-
// Validate device id
205+
#if MICROPY_HW_SPIFLASH_DETECT_DEVICE
206+
// Attempt to detect SPI flash based on its JEDEC id.
203207
uint32_t devid;
204208
int ret = mp_spiflash_read_cmd(self, CMD_RD_DEVID, 3, &devid);
205-
if (ret != 0 || devid != CHECK_DEVID) {
209+
ret = mp_spiflash_detect(self, ret, devid);
210+
if (ret != 0) {
211+
// Could not read device id.
206212
mp_spiflash_release_bus(self);
207213
return;
208214
}

drivers/memory/spiflash.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
#define MICROPY_HW_SPIFLASH_CHIP_PARAMS (0)
3535
#endif
3636

37+
// Whether to enable detection of SPI flash during initialisation.
38+
#ifndef MICROPY_HW_SPIFLASH_DETECT_DEVICE
39+
#define MICROPY_HW_SPIFLASH_DETECT_DEVICE (0)
40+
#endif
41+
3742
#define MP_SPIFLASH_ERASE_BLOCK_SIZE (4096) // must be a power of 2
3843

3944
enum {
@@ -91,6 +96,11 @@ typedef struct _mp_spiflash_t {
9196
void mp_spiflash_init(mp_spiflash_t *self);
9297
void mp_spiflash_deepsleep(mp_spiflash_t *self, int value);
9398

99+
#if MICROPY_HW_SPIFLASH_DETECT_DEVICE
100+
// A board/port should define this function to perform actions based on the JEDEC id.
101+
int mp_spiflash_detect(mp_spiflash_t *spiflash, int ret, uint32_t devid);
102+
#endif
103+
94104
// These functions go direct to the SPI flash device
95105
int mp_spiflash_erase_block(mp_spiflash_t *self, uint32_t addr);
96106
int mp_spiflash_read(mp_spiflash_t *self, uint32_t addr, size_t len, uint8_t *dest);

0 commit comments

Comments
 (0)