Просмотр исходного кода

Merge branch 'master' of https://gitee.com/openLuat/LuatOS

alienwalker 2 лет назад
Родитель
Сommit
94c4ba7ed7

+ 30 - 6
components/fatfs/luat_lib_fatfs.c

@@ -38,14 +38,15 @@ extern const struct luat_vfs_filesystem vfs_fs_fatfs;
 
 /*
 挂载fatfs
-@api fatfs.mount(mode,mount_point, spiid_or_spidevice, spi_cs, spi_speed, power_pin, power_on_delay)
+@api fatfs.mount(mode,mount_point, spiid_or_spidevice, spi_cs, spi_speed, power_pin, power_on_delay, auto_format)
 @int fatfs模式,可选fatfs.SPI,fatfs.SDIO,fatfs.RAM,fatfs.USB
-@string fatfs挂载点, 通常填""或者"SD", 底层会映射到vfs的 /sd 路径
+@string 虚拟文件系统的挂载点, 默认是 /fatfs
 @int 传入spi device指针,或者spi的id,或者sdio的id
 @int 片选脚的GPIO 号, spi模式有效,若前一个参数传的是spi device,这个参数就不需要传
 @int SPI最高速度,默认10M, 若前2个参数传的是spi device,这个参数就不需要传
 @int TF卡电源控制脚,TF卡初始前先拉低复位再拉高,如果没有,或者是内置电源控制方式,这个参数就不需要传
 @int TF卡电源复位过程时间,单位ms,默认值是1
+@bool 挂载失败是否尝试格式化,默认是true,即自动格式化. 本参数在2023.8.16添加
 @return bool 成功返回true, 否则返回nil或者false
 @return string 失败的原因
 @usage
@@ -143,8 +144,30 @@ static int fatfs_mount(lua_State *L)
 	}
 	
 	FRESULT re = f_mount(fs, "/", 0);
+	if (re != FR_OK) {
+		if (lua_isboolean(L, 8) && lua_toboolean(L, 8) == 0) {
+			LLOGI("sd/tf mount failed %d but auto-format is disabled", re);
+		}
+		else {
+			LLOGD("mount failed, try auto format");
+			MKFS_PARM parm = {
+				.fmt = FM_ANY, // 暂时应付一下ramdisk
+				.au_size = 0,
+				.align = 0,
+				.n_fat = 0,
+				.n_root = 0,
+			};
+			BYTE work[FF_MAX_SS] = {0};
+			re = f_mkfs("/", &parm, work, FF_MAX_SS);
+			LLOGD("auto format ret %d", re);
+			if (re == FR_OK) {
+				re = f_mount(fs, "/", 0);
+				LLOGD("remount again %d", re);
+			}
+		}
+	}
 	
-	lua_pushboolean(L, re == 0);
+	lua_pushboolean(L, re == FR_OK);
 	lua_pushinteger(L, re);
 	if (re == FR_OK) {
 		if (FATFS_DEBUG)
@@ -582,10 +605,11 @@ static const rotable_Reg_t reg_fatfs[] =
 { 
   { "init",		ROREG_FUNC(fatfs_mount)}, //初始化,挂载, 别名方法
   { "mount",	ROREG_FUNC(fatfs_mount)}, //初始化,挂载
+  { "getfree",	ROREG_FUNC(fatfs_getfree)}, // 获取文件系统大小,剩余空间
+#if 0
   { "unmount",	ROREG_FUNC(fatfs_unmount)}, // 取消挂载
   { "mkfs",		ROREG_FUNC(fatfs_mkfs)}, // 格式化!!!
   //{ "test",  fatfs_test)},
-  { "getfree",	ROREG_FUNC(fatfs_getfree)}, // 获取文件系统大小,剩余空间
   { "debug",	ROREG_FUNC(fatfs_debug_mode)}, // 调试模式,打印更多日志
 
   { "lsdir",	ROREG_FUNC(fatfs_lsdir)}, // 列举目录下的文件,名称,大小,日期,属性
@@ -603,11 +627,11 @@ static const rotable_Reg_t reg_fatfs[] =
   { "rename",	ROREG_FUNC(fatfs_rename)}, // 文件改名
 
   { "readfile",	ROREG_FUNC(fatfs_readfile)}, // 读取文件的简易方法
-
+#endif
   { "SPI",         ROREG_INT(DISK_SPI)},
   { "SDIO",        ROREG_INT(DISK_SDIO)},
   { "RAM",         ROREG_INT(DISK_RAM)},
-  { "USB",         ROREG_INT(DISK_USB)},
+//  { "USB",         ROREG_INT(DISK_USB)},
 
   { NULL,		ROREG_INT(0)}
 };

+ 10 - 1
lua/src/liolib.c

@@ -1082,7 +1082,16 @@ LUAMOD_API int luaopen_io (lua_State *L) {
   return 1;
 }
 
-// dir method
+/*
+格式化文件系统,需指定挂载点
+@api io.mkfs(path)
+@string 挂载点
+@return bool 成功与否
+@return int 底层返回值
+@usage
+local ret, errio = io.mkfs("/sd")
+log.info("fs", "mkfs", ret, errio)
+*/
 static int io_mkfs (lua_State *L) {
   luat_fs_conf_t conf = {0};
   conf.mount_point = (char*)luaL_checkstring(L, 1);

+ 12 - 2
luat/freertos/luat_timer_freertos.c

@@ -20,6 +20,16 @@
 #define FREERTOS_TIMER_COUNT 32
 static luat_timer_t* timers[FREERTOS_TIMER_COUNT] = {0};
 
+static int inline MS2T(int ms) {
+    if (ms <= 0)
+        return 0;
+    if ((configTICK_RATE_HZ) == 1000)
+        return ms;
+    if ((configTICK_RATE_HZ) == 500)
+        return (ms + 1) / 2;
+    return ms;
+}
+
 static void luat_timer_callback(TimerHandle_t xTimer) {
     //LLOGD("timer callback");
     rtos_msg_t msg;
@@ -55,7 +65,7 @@ int luat_timer_start(luat_timer_t* timer) {
         LLOGE("too many timers");
         return 1; // too many timer!!
     }
-    os_timer = xTimerCreate("luat_timer", timer->timeout, timer->repeat, (void*)(timer->id), luat_timer_callback);
+    os_timer = xTimerCreate("luat_timer", MS2T(timer->timeout), timer->repeat, (void*)(timer->id), luat_timer_callback);
     //LLOGD("timer id=%ld, osTimerNew=%p", timerIndex, os_timer);
     if (!os_timer) {
         LLOGE("xTimerCreate FAIL");
@@ -103,7 +113,7 @@ luat_timer_t* luat_timer_get(size_t timer_id) {
 
 int luat_timer_mdelay(size_t ms) {
     if (ms > 0) {
-        vTaskDelay(ms);
+        vTaskDelay(MS2T(ms));
     }
     return 0;
 }

+ 1 - 1
luat/include/luat_base.h

@@ -7,7 +7,7 @@
 #ifndef LUAT_BASE_H
 #define LUAT_BASE_H
 /**LuatOS版本号*/
-#define LUAT_VERSION "22.12"
+#define LUAT_VERSION "23.08"
 #define LUAT_VERSION_BETA 0
 // 调试开关, 预留
 #define LUAT_DEBUG 0

+ 7 - 8
luat/modules/luat_lib_crypto.c

@@ -672,15 +672,14 @@ static int l_crypto_md(lua_State *L) {
 @return userdata 成功返回一个数据结构,否则返回nil
 @usage
 -- 无hmac的hash stream
-log.info("md5", crypto.hash_init("MD5"))
-log.info("sha1", crypto.hash_init("SHA1"))
-log.info("sha256", crypto.hash_init("SHA256"))
+local md5_stream = crypto.hash_init("MD5")
+local sha1_stream = crypto.hash_init("SHA1")
+local sha256_stream = crypto.hash_init("SHA256")
 
 -- 带hmac的hash stream
-log.info("hmac_md5", crypto.hash_init("MD5", "123456"))
-log.info("hmac_sha1", crypto.hash_init("SHA1", "123456"))
-log.info("hmac_sha256", crypto.hash_init("SHA256", "123456"))
-local stream = crypto.hash_init()
+local md5_stream = crypto.hash_init("MD5", "123456")
+local sha1_stream = crypto.hash_init("SHA1", "123456")
+local sha256_stream = crypto.hash_init("SHA256", "123456")
 */
 static int l_crypt_hash_init(lua_State *L) {
     luat_crypt_stream_t *stream = (luat_crypt_stream_t *)lua_newuserdata(L, sizeof(luat_crypt_stream_t));
@@ -727,7 +726,7 @@ static int l_crypt_hash_update(lua_State *L) {
 @userdata crypto.hash_init()创建的stream,必选
 @return string 成功返回计算得出的流式hash值的hex字符串,失败无返回
 @usage
-local hashResult = crypto.hash_finish("MD5", stream)
+local hashResult = crypto.hash_finish(stream)
 */
 static int l_crypt_hash_finish(lua_State *L) {
     luat_crypt_stream_t *stream = (luat_crypt_stream_t *)luaL_checkudata(L, 1, LUAT_CRYPTO_TYPE);

+ 9 - 51
luat/modules/luat_lib_sdio.c

@@ -7,6 +7,12 @@
 @tag LUAT_USE_SDIO
 @usage
 -- 本sdio库挂载tf卡到文件系统功能已经被fatfs的sdio模式取代
+-- 本sdio库仅保留直接读写TF卡的函数
+-- 例如 使用 sdio 0 挂载TF卡
+fatfs.mount(fatfs.SDIO, "/sd", 0)
+
+-- 挂载完成后, 使用 io 库的相关函数来操作就行
+local f = io.open("/sd/abc.txt")
 */
 #include "luat_base.h"
 #include "luat_sdio.h"
@@ -92,63 +98,15 @@ static int l_sdio_write(lua_State *L) {
     return 1;
 }
 
-/*
-挂载SD卡, 使用FATFS文件系统
-@api sdio.sd_mount(id, path, auto_format)
-@int sdio总线id
-@string 挂载路径, 默认"/sd", 不允许以"/"结尾
-@bool 是否自动格式化,默认是true
-@return bool 挂载成功返回true,否则返回false
-@return int 底层返回的具体结果码,用于调试
-*/
-static int l_sdio_sd_mount(lua_State *L) {
-    int id = luaL_optinteger(L, 1, 0);
-    const char* path = luaL_optstring(L, 2, "/sd");
-    int auto_format = luaL_optinteger(L, 3, 1);
-    int ret = luat_sdio_sd_mount(id, &sdio_t[id].rca, (char*)path, auto_format);
-    lua_pushboolean(L, ret == 0 ? 1: 0);
-    lua_pushinteger(L, ret);
-    return 2;
-}
-
-/*
-卸载SD卡(视硬件情况, 不一定支持)
-@api sdio.sd_umount(id, path)
-@int sdio总线id
-@string 挂载路径, 默认"/sd", 不允许以"/"结尾
-@return bool 挂载成功返回true,否则返回false
-*/
-static int l_sdio_sd_umount(lua_State *L) {
-    int id = luaL_checkinteger(L, 1);
-    //int auto_format = luaL_checkinteger(L, 2);
-    int ret = luat_sdio_sd_unmount(id, sdio_t[id].rca);
-    lua_pushboolean(L, ret == 0 ? 1: 0);
-    return 1;
-}
-
-/*
-格式化SD卡
-@api sdio.sd_format(id)
-@int sdio总线id
-@return bool 挂载成功返回true,否则返回false
-*/
-static int l_sdio_sd_format(lua_State *L) {
-    int id = luaL_checkinteger(L, 1);
-    //int auto_format = luaL_checkinteger(L, 2);
-    int ret = luat_sdio_sd_format(id, sdio_t[id].rca);
-    lua_pushboolean(L, ret == 0 ? 1: 0);
-    return 1;
-}
-
 #include "rotable2.h"
 static const rotable_Reg_t reg_sdio[] =
 {
     { "init" ,          ROREG_FUNC(l_sdio_init )},
     { "sd_read" ,       ROREG_FUNC(l_sdio_read )},
     { "sd_write" ,      ROREG_FUNC(l_sdio_write)},
-    { "sd_mount" ,      ROREG_FUNC(l_sdio_sd_mount)},
-    { "sd_umount" ,     ROREG_FUNC(l_sdio_sd_umount)},
-    { "sd_format" ,     ROREG_FUNC(l_sdio_sd_format)},
+    // { "sd_mount" ,      ROREG_FUNC(l_sdio_sd_mount)},
+    // { "sd_umount" ,     ROREG_FUNC(l_sdio_sd_umount)},
+    // { "sd_format" ,     ROREG_FUNC(l_sdio_sd_format)},
 	{ NULL,             ROREG_INT(0) }
 };