diff --git a/mongoose.c b/mongoose.c index accd4104..382202b8 100644 --- a/mongoose.c +++ b/mongoose.c @@ -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 diff --git a/mongoose.h b/mongoose.h index b4fc559a..f647bbb3 100644 --- a/mongoose.h +++ b/mongoose.h @@ -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} + diff --git a/src/flash.c b/src/flash.c index 010cd513..2117461e 100644 --- a/src/flash.c +++ b/src/flash.c @@ -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; +} diff --git a/src/flash.h b/src/flash.h index d2a84038..4ea0cee5 100644 --- a/src/flash.h +++ b/src/flash.h @@ -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} diff --git a/src/periph.h b/src/periph.h index cdbff2e7..2040455e 100644 --- a/src/periph.h +++ b/src/periph.h @@ -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 diff --git a/tutorials/device-dashboard/full/dashboard.c b/tutorials/device-dashboard/full/dashboard.c index 47bc8594..123653e4 100644 --- a/tutorials/device-dashboard/full/dashboard.c +++ b/tutorials/device-dashboard/full/dashboard.c @@ -287,7 +287,7 @@ void mg_dash_init(struct mg_mgr *mgr) { MG_DASH_ADD_FIELD_SET(&s_dash, &set_graph); MG_DASH_ADD_FIELD_SET(&s_dash, &set_actions); -#if MG_ARCH == MG_ARCH_UNIX +#if MG_ARCH == MG_ARCH_UNIX || MG_ARCH == MG_ARCH_WIN32 || MG_ENABLE_LFS // Create demo files in the upload directory mkdir("/tmp", 0755); mkdir("/tmp/dashboard", 0755); diff --git a/tutorials/nordic/xiao-nrf52840/usb/Makefile b/tutorials/nordic/xiao-nrf52840/usb/Makefile index 22578475..5db6a772 100644 --- a/tutorials/nordic/xiao-nrf52840/usb/Makefile +++ b/tutorials/nordic/xiao-nrf52840/usb/Makefile @@ -12,7 +12,7 @@ NRFUTIL ?= adafruit-nrfutil CFLAGS = -W -Wall -Wextra -Werror -Wshadow -Wdouble-promotion CFLAGS += -Wformat-truncation -fno-common -Wno-conversion -Wno-sign-conversion CFLAGS += -g3 -Os -ffunction-sections -fdata-sections -CFLAGS += -I. -I$(MGDIR) -Icmsis_core/CMSIS/Core/Include -Inrfx/mdk +CFLAGS += -I. -I$(MGDIR) -Icmsis_core/CMSIS/Core/Include -Inrfx/mdk -Ilittlefs CFLAGS += -DNRF52840_XXAA -D__STARTUP_CLEAR_BSS -D__START=main -D__HEAP_SIZE=0 CFLAGS += -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 CFLAGS += $(CFLAGS_EXTRA) @@ -22,13 +22,15 @@ LDFLAGS ?= -Tlink.ld -nostdlib -nostartfiles --specs=nano.specs -lc -lgcc LDFLAGS += -Wl,--gc-sections -Wl,-Map=$@.map SOURCES = hal.c main.c $(MGDIR)/mongoose.c +SOURCES += littlefs/lfs.c littlefs/lfs_util.c +CFLAGS += -Wno-shadow # Disabled for littlefs/lfs.c SOURCES += $(MGDIR)/tutorials/device-dashboard/full/dashboard.c SOURCES += $(MGDIR)/tutorials/device-dashboard/full/file_data.c SOURCES += nrfx/mdk/system_nrf52840.c nrfx/mdk/gcc_startup_nrf52840.S all build: firmware.hex firmware.bin -firmware.elf: cmsis_core nrfx $(SOURCES) $(wildcard *.h) link.ld Makefile +firmware.elf: cmsis_core nrfx littlefs $(SOURCES) $(wildcard *.h) link.ld Makefile arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@ arm-none-eabi-size --format=berkeley $@ @@ -52,10 +54,14 @@ cmsis_core: nrfx: git clone -q -c advice.detachedHead=false --depth 1 -b v2.11.0 https://github.com/NordicSemiconductor/nrfx $@ +littlefs/lfs.c littlefs/lfs_util.c: littlefs +littlefs: + git clone -q -c advice.detachedHead=false --depth 1 -b v2.11.3 https://github.com/littlefs-project/littlefs $@ + $(MGDIR)/tutorials/device-dashboard/full/file_data.c: $(MGDIR)/tutorials/device-dashboard/full/dashboard.html cd $(dir $@) && $(MAKE) $(notdir $@) clean: - rm -rf firmware.* cmsis_core nrfx + rm -rf firmware.* cmsis_core nrfx littlefs .PHONY: all build flash clean diff --git a/tutorials/nordic/xiao-nrf52840/usb/hal.c b/tutorials/nordic/xiao-nrf52840/usb/hal.c index e4f3c316..5527c821 100644 --- a/tutorials/nordic/xiao-nrf52840/usb/hal.c +++ b/tutorials/nordic/xiao-nrf52840/usb/hal.c @@ -40,19 +40,19 @@ void *_sbrk(int incr) { return prev_heap; } -// Newlib stubs +// Newlib stubs. Weak ones are replaced by littlefs, see MG_ENABLE_LFS struct stat; -int _fstat(int fd, struct stat *st) { +__attribute__((weak)) int _fstat(int fd, struct stat *st) { (void) fd, (void) st; return -1; } -int _open(const char *path) { +__attribute__((weak)) int _open(const char *path) { (void) path; return -1; } -int _close(int fd) { +__attribute__((weak)) int _close(int fd) { (void) fd; return -1; } @@ -62,7 +62,7 @@ int _isatty(int fd) { return 1; } -int _lseek(int fd, int ptr, int dir) { +__attribute__((weak)) int _lseek(int fd, int ptr, int dir) { (void) fd, (void) ptr, (void) dir; return 0; } @@ -80,12 +80,12 @@ int _getpid(void) { return -1; } -int _write(int fd, char *ptr, int len) { +__attribute__((weak)) int _write(int fd, char *ptr, int len) { (void) fd, (void) ptr, (void) len; return -1; } -int _read(int fd, char *ptr, int len) { +__attribute__((weak)) int _read(int fd, char *ptr, int len) { (void) fd, (void) ptr, (void) len; return -1; } diff --git a/tutorials/nordic/xiao-nrf52840/usb/hal.h b/tutorials/nordic/xiao-nrf52840/usb/hal.h index 497be5f1..c7efa267 100644 --- a/tutorials/nordic/xiao-nrf52840/usb/hal.h +++ b/tutorials/nordic/xiao-nrf52840/usb/hal.h @@ -15,6 +15,12 @@ #define IMU_PWR PIN(1, 8) // LSM6DS3TR-C IMU power, active high (Sense only) #define IMU_SCL PIN(0, 27) // IMU I2C bus #define IMU_SDA PIN(0, 7) +#define FLASH_CS PIN(0, 25) // P25Q16H 2 MB flash, used over plain SPI +#define FLASH_SCK PIN(0, 21) // The board wires it for QSPI: IO0..IO3 +#define FLASH_MOSI PIN(0, 20) // IO0 +#define FLASH_MISO PIN(0, 24) // IO1 +#define FLASH_WP PIN(0, 22) // IO2, active low +#define FLASH_HOLD PIN(0, 23) // IO3, active low uint64_t hal_get_tick(void); size_t hal_ram_used(void); @@ -49,6 +55,61 @@ static inline void hal_gpio_output(uint16_t pin) { (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos); } +// SPIM2 to the on-board flash, 8 MHz, mode 0. SPIM0 is not used because it +// shares its registers with TWIM0, the IMU's I2C +static inline void hal_spi_init(void) { + hal_gpio_write(FLASH_CS, true); + hal_gpio_write(FLASH_WP, true); + hal_gpio_write(FLASH_HOLD, true); + hal_gpio_write(FLASH_SCK, false); + hal_gpio_output(FLASH_CS); + hal_gpio_output(FLASH_WP); + hal_gpio_output(FLASH_HOLD); + hal_gpio_output(FLASH_SCK); // Its input buffer must stay connected + hal_gpio_output(FLASH_MOSI); + hal_gpio_bank(FLASH_MISO)->PIN_CNF[PINNO(FLASH_MISO)] = + (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | + (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos); + NRF_SPIM2->PSEL.SCK = FLASH_SCK; // PIN() matches the PSEL port/pin format + NRF_SPIM2->PSEL.MOSI = FLASH_MOSI; + NRF_SPIM2->PSEL.MISO = FLASH_MISO; + NRF_SPIM2->FREQUENCY = SPIM_FREQUENCY_FREQUENCY_M8; + NRF_SPIM2->CONFIG = 0; // MSB first, mode 0 + NRF_SPIM2->ORC = 0xff; // Sent when there is nothing to write + NRF_SPIM2->ENABLE = SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos; +} + +// struct mg_spi callbacks. spi is unused, there is one bus +static inline void hal_spi_begin(void *spi) { + (void) spi; + hal_gpio_write(FLASH_CS, false); +} + +static inline void hal_spi_end(void *spi) { + (void) spi; + hal_gpio_write(FLASH_CS, true); +} + +// Sends write and receives into read, len bytes. Either can be NULL. Buffers +// must be in RAM, for EasyDMA +static inline void hal_spi_txn(void *spi, uint8_t *write, uint8_t *read, + size_t len) { + (void) spi; + while (len > 0) { + size_t n = len > 0xffff ? 0xffff : len; // MAXCNT is 16 bits + NRF_SPIM2->TXD.PTR = (uint32_t) write; + NRF_SPIM2->TXD.MAXCNT = write == NULL ? 0 : n; + NRF_SPIM2->RXD.PTR = (uint32_t) read; + NRF_SPIM2->RXD.MAXCNT = read == NULL ? 0 : n; + NRF_SPIM2->EVENTS_END = 0; + NRF_SPIM2->TASKS_START = 1; + while (!NRF_SPIM2->EVENTS_END) (void) 0; + if (write != NULL) write += n; + if (read != NULL) read += n; + len -= n; + } +} + // Vectors above the bootloader, 64 MHz crystal clock, 1 ms SysTick static inline void hal_system_init(void) { extern uint32_t __isr_vector[]; diff --git a/tutorials/nordic/xiao-nrf52840/usb/main.c b/tutorials/nordic/xiao-nrf52840/usb/main.c index e9426aec..6a04f13e 100644 --- a/tutorials/nordic/xiao-nrf52840/usb/main.c +++ b/tutorials/nordic/xiao-nrf52840/usb/main.c @@ -25,6 +25,13 @@ bool mg_random(void *buf, size_t len) { return true; } +// The on-board P25Q16H 2 MB flash for littlefs, see MG_LFS_FLASH in +// mongoose_config.h. Mongoose's SPI flash API drives it over hal.h's SPI. +// Early boards (V1.1) have the chip not fitted. Then init fails and the +// filesystem is not available +static struct mg_spi s_spi = {NULL, hal_spi_begin, hal_spi_end, hal_spi_txn}; +MG_SPI_FLASH(mg_flash_spi, &s_spi); + int main(void) { hal_system_init(); hal_rng_init(); @@ -35,6 +42,7 @@ int main(void) { hal_gpio_output(LED_GREEN); hal_gpio_output(LED_BLUE); int imu = hal_imu_init(); // For the dashboard's real-time graph + hal_spi_init(); // On-board flash, see MG_SPI_FLASH() above struct mg_mgr mgr; mg_mgr_init(&mgr); // Also starts the USB driver, see mongoose_config.h diff --git a/tutorials/nordic/xiao-nrf52840/usb/mongoose_config.h b/tutorials/nordic/xiao-nrf52840/usb/mongoose_config.h index 92fbf811..f71036b8 100644 --- a/tutorials/nordic/xiao-nrf52840/usb/mongoose_config.h +++ b/tutorials/nordic/xiao-nrf52840/usb/mongoose_config.h @@ -10,6 +10,12 @@ #define MG_ENABLE_USB_ETHERNET 1 // USB NCM Ethernet interface #define MG_ENABLE_USB_SERIAL 1 // USB serial log console +// Filesystem on the on-board 2 MB flash, see main.c +#define MG_ENABLE_LFS 1 +#define MG_LFS_SIZE (2 * 1024 * 1024) // The whole chip +#define MG_LFS_FLASH (&mg_flash_spi) +extern struct mg_flash mg_flash_spi; + // Static device address; the driver serves DHCP to the USB host. Remove // these to get an address over DHCP instead, e.g. from Internet Sharing #define MG_TCPIP_IP MG_IPV4(192, 168, 7, 1) diff --git a/tutorials/nordic/xiao-nrf52840/usb/sys/dirent.h b/tutorials/nordic/xiao-nrf52840/usb/sys/dirent.h new file mode 100644 index 00000000..23460671 --- /dev/null +++ b/tutorials/nordic/xiao-nrf52840/usb/sys/dirent.h @@ -0,0 +1,3 @@ +// Replaces newlib's sys/dirent.h, which is an #error. With MG_ENABLE_LFS, +// struct dirent and DIR come from mongoose.h +#pragma once diff --git a/tutorials/stm32/portenta-h7/minimal-usb/Makefile b/tutorials/stm32/portenta-h7/minimal-usb/Makefile index 726ef025..fa139c0b 100644 --- a/tutorials/stm32/portenta-h7/minimal-usb/Makefile +++ b/tutorials/stm32/portenta-h7/minimal-usb/Makefile @@ -9,11 +9,14 @@ CFLAGS = -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion CFLAGS += -Wformat-truncation -fno-common -Wconversion -Wno-sign-conversion CFLAGS += -g3 -Os -ffunction-sections -fdata-sections CFLAGS += -I. -I$(MGDIR) -Icmsis_core/CMSIS/Core/Include -Icmsis_h7/Include +CFLAGS += -Ilittlefs CFLAGS += -mcpu=cortex-m7 -mthumb -mfloat-abi=hard -mfpu=fpv5-d16 -DCORE_CM7 CFLAGS += $(CFLAGS_EXTRA) LDFLAGS ?= -Tlink.ld -nostdlib -nostartfiles --specs nano.specs -lc -lgcc -Wl,--gc-sections SOURCES = main.c hal.c $(MGDIR)/mongoose.c +SOURCES += littlefs/lfs.c littlefs/lfs_util.c +CFLAGS += -Wno-conversion -Wno-shadow # Disabled for littlefs/lfs.c SOURCES += $(MGDIR)/tutorials/device-dashboard/full/dashboard.c SOURCES += $(MGDIR)/tutorials/device-dashboard/full/file_data.c SOURCES += cmsis_h7/Source/Templates/gcc/startup_stm32h747xx.s @@ -24,7 +27,7 @@ firmware.bin: firmware.elf arm-none-eabi-objcopy -O binary $< $@ arm-none-eabi-size --format=berkeley $< -firmware.elf: cmsis_core cmsis_h7 $(SOURCES) $(wildcard *.h) $(MGDIR)/mongoose.h link.ld Makefile +firmware.elf: cmsis_core cmsis_h7 littlefs $(SOURCES) $(wildcard *.h) $(MGDIR)/mongoose.h link.ld Makefile arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@ flash: firmware.bin @@ -36,8 +39,12 @@ cmsis_core: cmsis_h7: git clone -q -c advice.detachedHead=false --depth 1 -b v1.10.4 https://github.com/STMicroelectronics/cmsis_device_h7 $@ +littlefs/lfs.c littlefs/lfs_util.c: littlefs +littlefs: + git clone -q -c advice.detachedHead=false --depth 1 -b v2.11.3 https://github.com/littlefs-project/littlefs $@ + $(MGDIR)/tutorials/device-dashboard/full/file_data.c: cd $(dir $@) && $(MAKE) $(notdir $@) clean: - rm -rf firmware.* cmsis_* + rm -rf firmware.* cmsis_* littlefs diff --git a/tutorials/stm32/portenta-h7/minimal-usb/hal.h b/tutorials/stm32/portenta-h7/minimal-usb/hal.h index 55ac1c2f..18df561c 100644 --- a/tutorials/stm32/portenta-h7/minimal-usb/hal.h +++ b/tutorials/stm32/portenta-h7/minimal-usb/hal.h @@ -13,6 +13,15 @@ #define LED_3 PIN('K', 7) // On-board LED pin (blue) #define LED LED_2 // Use green LED for blinking +// On-board 16 MB QSPI flash, used over plain SPI. Pins from Arduino's mbed-os, +// TARGET_PORTENTA_H7 PinNames.h: QSPI_FLASH1_* +#define FLASH_CS PIN('G', 6) +#define FLASH_SCK PIN('F', 10) +#define FLASH_MOSI PIN('D', 11) // IO0 +#define FLASH_MISO PIN('D', 12) // IO1 +#define FLASH_WP PIN('F', 7) // IO2, active low +#define FLASH_HOLD PIN('D', 13) // IO3, active low + #include #include @@ -103,6 +112,50 @@ static inline void hal_gpio_output(uint16_t pin) { HAL_GPIO_SPEED_HIGH, HAL_GPIO_PULL_NONE, 0); } +// Bit-banged SPI to the on-board flash, mode 0. No SPI peripheral reaches the +// flash pins, they are wired for QUADSPI +static inline void hal_spi_init(void) { + uint16_t outs[] = {FLASH_CS, FLASH_WP, FLASH_HOLD, FLASH_SCK, FLASH_MOSI}; + for (size_t i = 0; i < sizeof(outs) / sizeof(outs[0]); i++) { + hal_gpio_write(outs[i], outs[i] != FLASH_SCK); // SCK low, the rest high + hal_gpio_output(outs[i]); + } + hal_gpio_init(FLASH_MISO, HAL_GPIO_MODE_INPUT, HAL_GPIO_OTYPE_PUSH_PULL, + HAL_GPIO_SPEED_HIGH, HAL_GPIO_PULL_NONE, 0); +} + +// struct mg_spi callbacks. spi is unused, there is one bus +static inline void hal_spi_begin(void *spi) { + (void) spi; + hal_gpio_write(FLASH_CS, false); +} + +static inline void hal_spi_end(void *spi) { + (void) spi; + hal_gpio_write(FLASH_CS, true); +} + +// Sends write and receives into read, len bytes. Either can be NULL, then +// 0xff is sent, or the input is dropped +static inline void hal_spi_txn(void *spi, uint8_t *write, uint8_t *read, + size_t len) { + GPIO_TypeDef *sck = hal_gpio_bank(FLASH_SCK), *mosi = hal_gpio_bank(FLASH_MOSI), + *miso = hal_gpio_bank(FLASH_MISO); + uint32_t sck_bit = BIT(PINNO(FLASH_SCK)), mosi_bit = BIT(PINNO(FLASH_MOSI)), + miso_bit = BIT(PINNO(FLASH_MISO)); + (void) spi; + for (size_t i = 0; i < len; i++) { + uint32_t out = write == NULL ? 0xff : write[i], in = 0; + for (int b = 7; b >= 0; b--) { + mosi->BSRR = mosi_bit << ((out >> b) & 1 ? 0 : 16); + sck->BSRR = sck_bit; // Rising edge: the flash samples MOSI + in = (in << 1) | ((miso->IDR & miso_bit) ? 1 : 0); + sck->BSRR = sck_bit << 16; // Falling edge: the flash shifts out + } + if (read != NULL) read[i] = (uint8_t) in; + } +} + static inline void hal_rng_init(void) { RCC->D2CCIP2R |= RCC_D2CCIP2R_RNGSEL_0; // RNG clock source pll1_q_ck RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN; // Enable RNG clock diff --git a/tutorials/stm32/portenta-h7/minimal-usb/main.c b/tutorials/stm32/portenta-h7/minimal-usb/main.c index a54f5b4f..083266c2 100644 --- a/tutorials/stm32/portenta-h7/minimal-usb/main.c +++ b/tutorials/stm32/portenta-h7/minimal-usb/main.c @@ -24,11 +24,17 @@ bool mg_random(void *buf, size_t len) { return true; } +// The on-board 16 MB flash for littlefs, see MG_LFS_FLASH in +// mongoose_config.h. Mongoose's SPI flash API drives it over hal.h's SPI +static struct mg_spi s_spi = {NULL, hal_spi_begin, hal_spi_end, hal_spi_txn}; +MG_SPI_FLASH(mg_flash_spi, &s_spi); + int main(void) { hal_rng_init(); hal_gpio_output(LED); hal_gpio_write(LED, true); // Off, the LEDs are active low hal_usb_init(); + hal_spi_init(); // On-board flash, see MG_SPI_FLASH() above struct mg_mgr mgr; mg_mgr_init(&mgr); // Also starts the USB driver, see mongoose_config.h diff --git a/tutorials/stm32/portenta-h7/minimal-usb/mongoose_config.h b/tutorials/stm32/portenta-h7/minimal-usb/mongoose_config.h index b3969a16..05310eaa 100644 --- a/tutorials/stm32/portenta-h7/minimal-usb/mongoose_config.h +++ b/tutorials/stm32/portenta-h7/minimal-usb/mongoose_config.h @@ -14,6 +14,12 @@ #define MG_USB_DWC2_BASE USB1_OTG_HS_PERIPH_BASE // OTG_HS #define MG_USB_DWC2_ULPI 1 // External ULPI PHY +// Filesystem on the on-board 16 MB flash, see main.c +#define MG_ENABLE_LFS 1 +#define MG_LFS_SIZE (16 * 1024 * 1024) // The whole chip +#define MG_LFS_FLASH (&mg_flash_spi) +extern struct mg_flash mg_flash_spi; + // Static device address; the driver serves DHCP to the USB host. Remove // these to get an address over DHCP instead, e.g. from Internet Sharing #define MG_TCPIP_IP MG_IPV4(192, 168, 7, 1) diff --git a/tutorials/stm32/portenta-h7/minimal-usb/sys/dirent.h b/tutorials/stm32/portenta-h7/minimal-usb/sys/dirent.h new file mode 100644 index 00000000..23460671 --- /dev/null +++ b/tutorials/stm32/portenta-h7/minimal-usb/sys/dirent.h @@ -0,0 +1,3 @@ +// Replaces newlib's sys/dirent.h, which is an #error. With MG_ENABLE_LFS, +// struct dirent and DIR come from mongoose.h +#pragma once