SPI flash support, with xiao nrf52 and portenta-h7

This commit is contained in:
cpq committed 2026-12-03 11:56:57 +00:00
1 parent 57e30ab2e9
commit f02ea9859e
17 files changed
+442 -17

No files matched your search

+32 -2
View File
@@ -4647,8 +4647,10 @@ void mg_mdash_free(struct mg_mgr *);
// SPI bus abstraction for SPI-attached chips, e.g. W5500.
// Network drivers get it via the driver_data field
// SPI bus abstraction for SPI-attached chips, e.g. W5500 or SPI flash, see
// mg_spi_flash_init(). Network drivers get it via the driver_data field.
// Callbacks get the spi field. txn() with NULL write sends filler bytes, with
// NULL read discards what it receives
struct mg_spi {
void *spi; // Opaque SPI bus handle passed to all callbacks
void (*begin)(void *); // Assert chip-select, start of transaction
@@ -4663,6 +4665,7 @@ extern struct mg_spi mg_spi;
struct mg_flash {
void *start; // Address at which flash starts
size_t size; // Flash size
@@ -4679,6 +4682,33 @@ extern struct mg_flash *mg_flash;
// Reads memory-mapped (XIP) flash. Use as read_fn for such flash
bool mg_flash_read_xip(void *addr, void *buf, size_t len);
// JEDEC SPI NOR flash: W25Q, P25Q, MX25 and similar, up to 16 MB, 3-byte
// addresses, single-line SPI. MG_SPI_FLASH() wraps these into a struct mg_flash
bool mg_spi_flash_init(struct mg_spi *spi, size_t *size);
bool mg_spi_flash_read(struct mg_spi *spi, size_t ofs, void *buf, size_t len);
bool mg_spi_flash_write(struct mg_spi *spi, size_t ofs, const void *buf,
size_t len);
// Defines struct mg_flash name for a JEDEC SPI NOR chip on the struct mg_spi
// pointer spi. Start is 0, so addresses are flash offsets. The size comes
// from the chip on first use, and stays 0 if there is no chip. For example:
// static struct mg_spi s_spi = {NULL, my_begin, my_end, my_txn};
// MG_SPI_FLASH(my_flash, &s_spi);
// #define MG_LFS_FLASH (&my_flash) // In mongoose_config.h
#define MG_SPI_FLASH(name, spi) \
static bool name##_init(struct mg_flash *f) { \
if (f->size == 0) mg_spi_flash_init(spi, &f->size); \
return f->size > 0; \
} \
static bool name##_read(void *a, void *b, size_t n) { \
return mg_spi_flash_read(spi, (size_t) a, b, n); \
} \
static bool name##_write(void *a, const void *b, size_t n) { \
return mg_spi_flash_write(spi, (size_t) a, b, n); \
} \
struct mg_flash name = {NULL, 0, 4096, 1, name##_read, \
name##_write, NULL, name##_init}