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
@@ -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
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user