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)