mirror of
https://github.com/cesanta/mongoose.git
synced 2026-12-03 21:48: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
@@ -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
|
||||
Reference in new issue
Block a user