From cdd6681f406e8a34a3e91f9840e817c604502f30 Mon Sep 17 00:00:00 2001 From: robert Date: Thu, 3 Dec 2026 16:55:51 +0200 Subject: [PATCH] Add initial LFS support for IMXRT --- mongoose.c | 37 +++++++++++++++++++ src/ota_imxrt.c | 37 +++++++++++++++++++ tutorials/nxp/frdm-imxrt1186/minimal/Makefile | 22 +++++++---- tutorials/nxp/frdm-imxrt1186/minimal/main.c | 20 ++++++++++ .../frdm-imxrt1186/minimal/mongoose_config.h | 6 +++ 5 files changed, 114 insertions(+), 8 deletions(-) diff --git a/mongoose.c b/mongoose.c index 59fdf6c0..7f7aca3f 100644 --- a/mongoose.c +++ b/mongoose.c @@ -12900,9 +12900,40 @@ MG_IRAM static bool mg_imxrt_erase(void *addr) { } #endif +#if MG_OTA == MG_OTA_RT1180 +MG_IRAM static void flash_sync(void *addr, size_t len) { + // Include any sectors erased while programming their first page. + size_t ss = s_mg_flash_imxrt.secsz; + uintptr_t start = MG_ROUND_DOWN((uintptr_t) addr, ss); + uintptr_t end = MG_ROUND_UP((uintptr_t) addr + len, ss); + volatile uint32_t *cache = (volatile uint32_t *) 0x44400000; // XCACHE_PC + +#if defined(__GNUC__) && defined(__arm__) + __asm__ __volatile__("dsb 0xf\nisb 0xf" : : : "memory"); +#endif + // Reset the FlexSPI2 AHB read buffers and IP FIFOs, with the bus idle. + while ((MG_REG(MG_FLEXSPI_BASE + 0xe0) & 3U) != 3U) (void) 0; // STS0 + MG_REG(MG_FLEXSPI_BASE) |= MG_BIT(0); // MCR0 SWRESET + while (MG_REG(MG_FLEXSPI_BASE) & MG_BIT(0)) (void) 0; + + // CLCR: invalidate by physical address. CM33 cache lines are 16 bytes. + MG_SET_BITS(cache[1], 3U << 24, MG_BIT(24) | MG_BIT(26)); + for (; start < end; start += 16) { + cache[2] = (uint32_t) start | MG_BIT(0); // CSAR: address | LGO + while (cache[2] & MG_BIT(0)) (void) 0; + } +#if defined(__GNUC__) && defined(__arm__) + __asm__ __volatile__("dsb 0xf\nisb 0xf" : : : "memory"); +#endif +} +#endif + MG_IRAM static bool mg_imxrt_write(void *addr, const void *buf, size_t len) { struct mg_flexspi_nor_config config, *config_ptr = &config; bool ok = false; +#if MG_OTA == MG_OTA_RT1180 + bool modified = false; +#endif // Interrupts must be disabled before calls to ROM API in RT1020 and 1060 MG_ARM_DISABLE_IRQ(); if (flexspi_nor_get_config(&config_ptr) != 0) goto fwxit; @@ -12923,6 +12954,9 @@ MG_IRAM static bool mg_imxrt_write(void *addr, const void *buf, size_t len) { ok = true; while (ok && src < end) { +#if MG_OTA == MG_OTA_RT1180 + modified = true; // Erase/program may modify flash even on failure +#endif if (flash_page_start(dst) && flash_erase(config_ptr, dst) == false) { ok = false; break; @@ -12964,6 +12998,9 @@ MG_IRAM static bool mg_imxrt_write(void *addr, const void *buf, size_t len) { } MG_DEBUG(("Flash write %lu bytes @ %p: %s.", len, dst, ok ? "ok" : "fail")); fwxit: +#if MG_OTA == MG_OTA_RT1180 + if (modified) flash_sync(addr, len); +#endif if (!s_flash_irq_disabled) MG_ARM_ENABLE_IRQ(); return ok; } diff --git a/src/ota_imxrt.c b/src/ota_imxrt.c index 81af1b22..f58d1bfc 100644 --- a/src/ota_imxrt.c +++ b/src/ota_imxrt.c @@ -576,9 +576,40 @@ MG_IRAM static bool mg_imxrt_erase(void *addr) { } #endif +#if MG_OTA == MG_OTA_RT1180 +MG_IRAM static void flash_sync(void *addr, size_t len) { + // Include any sectors erased while programming their first page. + size_t ss = s_mg_flash_imxrt.secsz; + uintptr_t start = MG_ROUND_DOWN((uintptr_t) addr, ss); + uintptr_t end = MG_ROUND_UP((uintptr_t) addr + len, ss); + volatile uint32_t *cache = (volatile uint32_t *) 0x44400000; // XCACHE_PC + +#if defined(__GNUC__) && defined(__arm__) + __asm__ __volatile__("dsb 0xf\nisb 0xf" : : : "memory"); +#endif + // Reset the FlexSPI2 AHB read buffers and IP FIFOs, with the bus idle. + while ((MG_REG(MG_FLEXSPI_BASE + 0xe0) & 3U) != 3U) (void) 0; // STS0 + MG_REG(MG_FLEXSPI_BASE) |= MG_BIT(0); // MCR0 SWRESET + while (MG_REG(MG_FLEXSPI_BASE) & MG_BIT(0)) (void) 0; + + // CLCR: invalidate by physical address. CM33 cache lines are 16 bytes. + MG_SET_BITS(cache[1], 3U << 24, MG_BIT(24) | MG_BIT(26)); + for (; start < end; start += 16) { + cache[2] = (uint32_t) start | MG_BIT(0); // CSAR: address | LGO + while (cache[2] & MG_BIT(0)) (void) 0; + } +#if defined(__GNUC__) && defined(__arm__) + __asm__ __volatile__("dsb 0xf\nisb 0xf" : : : "memory"); +#endif +} +#endif + MG_IRAM static bool mg_imxrt_write(void *addr, const void *buf, size_t len) { struct mg_flexspi_nor_config config, *config_ptr = &config; bool ok = false; +#if MG_OTA == MG_OTA_RT1180 + bool modified = false; +#endif // Interrupts must be disabled before calls to ROM API in RT1020 and 1060 MG_ARM_DISABLE_IRQ(); if (flexspi_nor_get_config(&config_ptr) != 0) goto fwxit; @@ -599,6 +630,9 @@ MG_IRAM static bool mg_imxrt_write(void *addr, const void *buf, size_t len) { ok = true; while (ok && src < end) { +#if MG_OTA == MG_OTA_RT1180 + modified = true; // Erase/program may modify flash even on failure +#endif if (flash_page_start(dst) && flash_erase(config_ptr, dst) == false) { ok = false; break; @@ -640,6 +674,9 @@ MG_IRAM static bool mg_imxrt_write(void *addr, const void *buf, size_t len) { } MG_DEBUG(("Flash write %lu bytes @ %p: %s.", len, dst, ok ? "ok" : "fail")); fwxit: +#if MG_OTA == MG_OTA_RT1180 + if (modified) flash_sync(addr, len); +#endif if (!s_flash_irq_disabled) MG_ARM_ENABLE_IRQ(); return ok; } diff --git a/tutorials/nxp/frdm-imxrt1186/minimal/Makefile b/tutorials/nxp/frdm-imxrt1186/minimal/Makefile index 897619e9..927b892b 100644 --- a/tutorials/nxp/frdm-imxrt1186/minimal/Makefile +++ b/tutorials/nxp/frdm-imxrt1186/minimal/Makefile @@ -1,18 +1,21 @@ # Environment setup: https://mongoose.ws/documentation/tutorials/tools/ +MGDIR = ../../../.. + CFLAGS = -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion CFLAGS += -Wformat-truncation -fno-common -Wno-conversion -Wno-sign-conversion CFLAGS += -g3 -Os -ffunction-sections -fdata-sections CFLAGS += -Wno-cast-function-type -Wno-old-style-declaration CFLAGS += -Wno-unused-parameter -Wno-unused-function -CFLAGS += -I. -Imongoose -Icmsis_core/CMSIS/Core/Include/ +CFLAGS += -I. -I$(MGDIR) -Ilittlefs -Icmsis_core/CMSIS/Core/Include/ CFLAGS += -Icmsis_rt1186/devices/MIMXRT1186/ -Icmsis_rt1186/devices/MIMXRT1186/periph/ -Icmsis_rt1186/devices/MIMXRT1186/drivers/ CFLAGS += -DCPU_MIMXRT1186CVJ8C_cm33 -DCPU_MIMXRT1186CVJ8C -DMIMXRT1186_cm33_SERIES CFLAGS += -DXIP_EXTERNAL_FLASH=1 -DXIP_BOOT_HEADER_ENABLE=1 CFLAGS += -mcpu=cortex-m33 -mthumb -mfpu=fpv5-sp-d16 -mfloat-abi=hard $(CFLAGS_EXTRA) LDFLAGS ?= -Tlink.ld -nostdlib -nostartfiles --specs=nano.specs -lc -lgcc -Wl,--gc-sections -SOURCES = main.c hal.c flash_image.c mongoose/mongoose.c +SOURCES = main.c hal.c flash_image.c $(MGDIR)/mongoose.c +SOURCES += littlefs/lfs.c littlefs/lfs_util.c SOURCES += cmsis_rt1186/devices/MIMXRT1186/gcc/startup_MIMXRT1186_cm33.S SOURCES += cmsis_rt1186/devices/MIMXRT1186/drivers/fsl_clock.c SOURCES += cmsis_rt1186/devices/MIMXRT1186/drivers/fsl_pmu.c @@ -23,17 +26,16 @@ SOURCES += cmsis_rt1186/devices/MIMXRT1186/drivers/fsl_trdc.c CFLAGS += -D__ATOLLIC__ -D__STARTUP_CLEAR_BSS -DMG_HAL_DISABLE_RANDOM -DMG_HAL_ENABLE_PHY_STRAP -all build: firmware.bin +# Disabled for littlefs/lfs.c +CFLAGS += -Wno-shadow -mongoose/mongoose.c mongoose/mongoose.h: - mkdir -p mongoose - cp ../../../../mongoose.[ch] mongoose/ +all build: firmware.bin firmware.bin: firmware.elf arm-none-eabi-objcopy -O binary $< $@ arm-none-eabi-size --format=berkeley $< -firmware.elf: cmsis_core cmsis_rt1186 $(SOURCES) $(wildcard *.h) link.ld Makefile +firmware.elf: cmsis_core cmsis_rt1186 littlefs sys/dirent.h $(SOURCES) $(MGDIR)/mongoose.h $(wildcard *.h) link.ld Makefile arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@ cmsis_core: @@ -44,9 +46,13 @@ cmsis_rt1186: unzip -q NXP.MIMXRT1186_DFP.26.06.00.pack -d $@ rm NXP.MIMXRT1186_DFP.26.06.00.pack +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 $@ + # Show all symbols sorted by size show_symbols_sorted_by_size: firmware.bin arm-none-eabi-readelf -sW firmware.elf | sed 1,3d | sed 's,^.*: ,,' | tr -s ' ' | cut -d ' ' -f 2,7 | sort -nk1 | grep -v '^0 ' clean: - rm -rf firmware.* cmsis_* mongoose/mongoose.* + rm -rf firmware.* cmsis_* mongoose/mongoose.* littlefs diff --git a/tutorials/nxp/frdm-imxrt1186/minimal/main.c b/tutorials/nxp/frdm-imxrt1186/minimal/main.c index 7e03bc0a..3669eea5 100644 --- a/tutorials/nxp/frdm-imxrt1186/minimal/main.c +++ b/tutorials/nxp/frdm-imxrt1186/minimal/main.c @@ -74,6 +74,14 @@ static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_match(hm->uri, mg_str("/api/tick"), NULL)) { mg_http_reply(c, 200, "", "{%m:%llu}\n", MG_ESC("tick"), hal_get_tick()); + } else if (mg_match(hm->uri, mg_str("/files/#"), NULL)) { + struct mg_http_serve_opts opts = { + .root_dir = "/files/=/fs/", + .enable_delete = true, + .enable_upload = true, + .enable_dir_listing = true, + }; + mg_http_serve_dir(c, hm, &opts); } else if (mg_match(hm->uri, mg_str("/api/crash"), NULL)) { __asm volatile("udf #0"); // Trigger HardFault } else { @@ -108,6 +116,18 @@ int main(void) { MG_INFO(("RAM used: %zu, free: %zu", hal_ram_used(), hal_ram_free())); MG_INFO(("Crash record: %M", mg_print_crash_record)); + // Create directory for Web file manager. Will be mapped to /files/ URL + if (!mg_lfs_init(MG_LFS_SIZE)) { + MG_ERROR(("LittleFS initialisation failed")); + } else { + mkdir("/fs", 0755); + MG_INFO(("File manager operations:")); + MG_INFO(("Upload: curl IP/files/NAME --data-binary @FILE")); + MG_INFO(("Delete: curl IP/files/NAME -X DELETE")); + MG_INFO(("Show: curl IP/files/NAME")); + MG_INFO(("List: curl IP/files/")); + } + for (;;) { mg_mgr_poll(&mgr, 0); blink_task(); diff --git a/tutorials/nxp/frdm-imxrt1186/minimal/mongoose_config.h b/tutorials/nxp/frdm-imxrt1186/minimal/mongoose_config.h index a4d61163..e83a1d37 100644 --- a/tutorials/nxp/frdm-imxrt1186/minimal/mongoose_config.h +++ b/tutorials/nxp/frdm-imxrt1186/minimal/mongoose_config.h @@ -3,6 +3,12 @@ #define MG_ENABLE_TCPIP 1 #define MG_ENABLE_DRIVER_NETC 1 #define MG_OTA MG_OTA_RT1180 + +#define MG_ENABLE_LFS 1 +#define MG_LFS_SIZE (128 * 1024) // Multiple of the 4 KiB flash sector size +#define MG_LFS_BUF_SIZE 256 // Match the RT1180 flash write alignment +// LittleFS shares the OTA staging area; files may be lost after an update. + // Keep the Mongoose PHY address aligned with NETC switch link binding #include "board_config.h" #define MG_TCPIP_PHY_ADDR BOARD_ETH_PHY_ADDR