From d5ea9f6238605dc428a2f4e3833603518269bac9 Mon Sep 17 00:00:00 2001 From: Lizongdi <1210855344@qq.com> Date: Wed, 16 Sep 2026 16:53:26 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=B8=BB=E7=BA=BF=E5=BE=85=E5=90=88?= =?UTF-8?q?=E3=80=91=E5=A2=9E=E5=8A=A0mount=E5=91=BD=E4=BB=A4=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9cat=E5=AE=9E=E7=8E=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bspMCU/l_stm32.c | 9 +++++++++ library/common/rd_stdio.c | 27 +++++++++++++++++++++++++-- lua/lua.c | 23 +++++++++++++---------- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/bspMCU/l_stm32.c b/bspMCU/l_stm32.c index 5131dbf..3ffb53d 100644 --- a/bspMCU/l_stm32.c +++ b/bspMCU/l_stm32.c @@ -434,6 +434,14 @@ static int l_w25qxx(lua_State *L) lua_pushinteger(L, uId); return 1; } + +int Rd_Lfs_Mount(void); +static int l_mount (lua_State *L) +{ + int err = Rd_Lfs_Mount(); + lua_pushinteger(L, err); + return 1; +} #endif static int l_sendto(lua_State *L) @@ -783,6 +791,7 @@ int luaopen_stm32(lua_State *L) #ifdef USE_W25QXX lua_register_help(L, "w25qxx", l_w25qxx, "Show w25qxx ID"); + lua_register_help(L, "mount", l_mount, "mount littlefs disk"); #endif lua_register_help(L, "sendto", l_sendto, "Send msg: sendto(dst,id,data)"); diff --git a/library/common/rd_stdio.c b/library/common/rd_stdio.c index dabe014..e52c610 100644 --- a/library/common/rd_stdio.c +++ b/library/common/rd_stdio.c @@ -305,7 +305,25 @@ WEAK long Rd_Mtell(void *file) WEAK int Rd_fflush(void *file) { +#ifdef USE_W25QXX +#if LV_USE_FS_LITTLEFS + (void)file; return RD_SUCCESS; +#else + if (file == NULL) return RD_FAILURE; + lfs_file_t *fp = (lfs_file_t *)file; + int err = lfs_file_sync(&g_lfs, fp); + if (err < 0) + { + printf("lfs_file_sync failed: %d\n", err); + return RD_FAILURE; + } + return RD_SUCCESS; +#endif +#else + (void)file; + return RD_FAILURE; +#endif } WEAK int Rd_remove(const char *filename) @@ -335,6 +353,12 @@ WEAK int Rd_rename(const char *oldpath, const char *newpath) #endif } +int Rd_Lfs_Mount(void) +{ + lfs_format(&g_lfs, &g_cfg); + return lfs_mount(&g_lfs, &g_cfg); +} + WEAK void Rd_LittleFSInit(void) { #ifdef USE_W25QXX @@ -345,8 +369,7 @@ WEAK void Rd_LittleFSInit(void) if (err) { printf("mount error format...\n"); - lfs_format(&g_lfs, &g_cfg); - lfs_mount(&g_lfs, &g_cfg); + (void)Rd_Lfs_Mount(); } return; #else diff --git a/lua/lua.c b/lua/lua.c index ff87882..82bbfc3 100644 --- a/lua/lua.c +++ b/lua/lua.c @@ -462,22 +462,25 @@ static int l_echo(lua_State *L) static int l_cat(lua_State *L) { - size_t len; - const char *path = luaL_checklstring(L, 1, &len); - char str[64] = {0}; + const char *path = luaL_checkstring(L, 1); + char buf[128]; /* 小缓冲区,循环读取整个文件 */ void *fp = Rd_Fopen(path, "r"); - if (fp != NULL) + if (fp == NULL) { - Rd_Mread(str, sizeof(str), fp); - Rd_Mclose(fp); - lua_pushstring(L, str); + lua_print("cat: cannot open '%s'\n", path); + return 0; } - else + + ssize_t n; + while ((n = Rd_Mread(buf, sizeof(buf), fp)) > 0) { - lua_pushstring(L, ""); + /* 用精度限定打印,避免依赖缓冲区末尾的 '\0',也支持二进制内容 */ + lua_print("%.*s", (int)n, buf); } - return 1; + + Rd_Mclose(fp); + return 0; /* 直接打印到终端,不再向 Lua 返回字符串 */ } static int l_mv(lua_State *L)