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

+1 -1
View File
@@ -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);
+9 -3
View File
@@ -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
+7 -7
View File
@@ -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;
}
+61
View File
@@ -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[];
@@ -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
@@ -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)
@@ -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
@@ -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
@@ -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 <stm32h747xx.h>
#include <stdbool.h>
@@ -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
@@ -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
@@ -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)
@@ -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