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

+103
View File
@@ -1,7 +1,110 @@
#include "flash.h"
#include "log.h"
#include "util.h"
// Read function for memory-mapped (XIP) flash, usable as mg_flash read_fn
bool mg_flash_read_xip(void *addr, void *buf, size_t len) {
memmove(buf, addr, len);
return true;
}
// JEDEC SPI NOR commands
#define MG_SPI_FLASH_WREN 0x06 // Write enable
#define MG_SPI_FLASH_RDSR 0x05 // Read status register: bit 0 busy, 1 WEL
#define MG_SPI_FLASH_READ 0x03 // Read data
#define MG_SPI_FLASH_PP 0x02 // Page program
#define MG_SPI_FLASH_SE 0x20 // 4k sector erase
#define MG_SPI_FLASH_RDID 0x9f // Read JEDEC ID: vendor, type, log2(size)
#define MG_SPI_FLASH_RDP 0xab // Release from deep power-down
#define MG_SPI_FLASH_SECSZ 4096
#define MG_SPI_FLASH_PAGESZ 256
// Sends cmd, then a 3-byte address if use_addr is true, then writes wbuf or
// reads into rbuf, len bytes, in one chip-select cycle
static void spi_flash_cmd(struct mg_spi *spi, uint8_t cmd, bool use_addr,
size_t addr, const void *wbuf, void *rbuf,
size_t len) {
uint8_t hdr[4] = {cmd, (uint8_t) (addr >> 16), (uint8_t) (addr >> 8),
(uint8_t) addr};
spi->begin(spi->spi);
spi->txn(spi->spi, hdr, NULL, use_addr ? 4 : 1);
if (len > 0) spi->txn(spi->spi, (uint8_t *) wbuf, (uint8_t *) rbuf, len);
spi->end(spi->spi);
}
// Returns the status register
static uint8_t spi_flash_status(struct mg_spi *spi) {
uint8_t sr = 0xff;
spi_flash_cmd(spi, MG_SPI_FLASH_RDSR, false, 0, NULL, &sr, 1);
return sr;
}
// Waits up to ms milliseconds for a program or erase to finish
static bool spi_flash_wait(struct mg_spi *spi, uint64_t ms) {
uint64_t deadline = mg_millis() + ms + 1;
while (spi_flash_status(spi) & 1) {
if (mg_millis() > deadline) {
MG_ERROR(("SPI flash timeout"));
return false;
}
}
return true;
}
// Sends write enable. Returns false if the chip did not set WEL, e.g. there
// is no chip, or it ignores writes
static bool spi_flash_wren(struct mg_spi *spi) {
spi_flash_cmd(spi, MG_SPI_FLASH_WREN, false, 0, NULL, NULL, 0);
if ((spi_flash_status(spi) & 3) == 2) return true;
MG_ERROR(("SPI flash write enable failed"));
return false;
}
// Wakes the chip and reads its ID. Sets size from the ID, max 16 MB
bool mg_spi_flash_init(struct mg_spi *spi, size_t *size) {
uint8_t id[3] = {0, 0, 0};
uint64_t t;
spi_flash_cmd(spi, MG_SPI_FLASH_RDP, false, 0, NULL, NULL, 0);
t = mg_millis() + 2; // Wake-up takes microseconds
while (mg_millis() < t) (void) 0;
spi_flash_cmd(spi, MG_SPI_FLASH_RDID, false, 0, NULL, id, sizeof(id));
if (id[0] == 0 || id[0] == 0xff || id[2] < 0x10 || id[2] > 0x20) {
MG_ERROR(("No SPI flash, ID %02x %02x %02x", id[0], id[1], id[2]));
return false;
}
if (id[2] > 0x18) {
MG_INFO(("SPI flash: 3-byte addresses, using the first 16 MB"));
id[2] = 0x18;
}
*size = (size_t) 1 << id[2];
MG_INFO(("SPI flash %02x %02x, %lu bytes", id[0], id[1],
(unsigned long) *size));
return true;
}
// Reads len bytes at flash offset ofs into buf
bool mg_spi_flash_read(struct mg_spi *spi, size_t ofs, void *buf, size_t len) {
spi_flash_cmd(spi, MG_SPI_FLASH_READ, true, ofs, NULL, buf, len);
return true;
}
// Writes len bytes at flash offset ofs, a page at most per program command.
// Erases each 4k sector when the write reaches its start
bool mg_spi_flash_write(struct mg_spi *spi, size_t ofs, const void *buf,
size_t len) {
const uint8_t *p = (const uint8_t *) buf;
while (len > 0) {
size_t n = MG_SPI_FLASH_PAGESZ - ofs % MG_SPI_FLASH_PAGESZ;
if (n > len) n = len;
if (ofs % MG_SPI_FLASH_SECSZ == 0) {
if (!spi_flash_wren(spi)) return false;
spi_flash_cmd(spi, MG_SPI_FLASH_SE, true, ofs, NULL, NULL, 0);
if (!spi_flash_wait(spi, 1000)) return false; // Chips differ, 20-400 ms
}
if (!spi_flash_wren(spi)) return false;
spi_flash_cmd(spi, MG_SPI_FLASH_PP, true, ofs, p, NULL, n);
if (!spi_flash_wait(spi, 50)) return false; // Usually 3 ms max
ofs += n, p += n, len -= n;
}
return true;
}
+28
View File
@@ -1,6 +1,7 @@
#pragma once
#include "arch.h"
#include "periph.h"
struct mg_flash {
void *start; // Address at which flash starts
@@ -17,3 +18,30 @@ 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}
+4 -2
View File
@@ -2,8 +2,10 @@
#include "arch.h"
// 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