mirror of
https://github.com/cesanta/mongoose.git
synced 2026-12-04 21:58:22 +00:00
SPI flash support, with xiao nrf52 and portenta-h7
This commit is contained in:
1 parent
57e30ab2e9
commit
f02ea9859e
17 files changed
+442
-17
No files matched your search
+103
@@ -2958,12 +2958,115 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) {
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
#ifdef MG_ENABLE_LINES
|
||||
#line 1 "src/fmt.c"
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user