Quellcode durchsuchen

add: 为air640w添加SD/TF卡模块的支持,并附带demo. 需要额外启用tf卡编译才能使用.

Wendal Chen vor 5 Jahren
Ursprung
Commit
6a164c383d

+ 45 - 0
bsp/air640w/demo/30.fs_sdcard/main.lua

@@ -0,0 +1,45 @@
+
+
+--[[
+
+SD 模块接线说明
+
+TF模块      --  Air640W
+CS          --  PB9(对应为gpio29)
+SCK         --  SPI_SCK,  PB16, gpio21
+MISO        --  SPI_MISO, PB17, gpio22
+MOSI        --  SPI_MOSI, PB18, gpio23
+
+淘宝链接: https://item.taobao.com/item.htm?id=609983719613 小卡版本
+]]
+
+local sys = require "sys"
+
+log.info("main", "sdcard demo")
+
+sys.timerLoopStart(function()
+    print("rdy")
+end, 3000)
+
+sys.taskInit(function()
+    sys.wait(1000)
+    -- 挂载sd卡
+    fs.mkdir("/sd")
+    spi.setup(0 * 10 + 1, 29) -- 生成的设备为 spi01
+    fs.mount("spi01", "elm", "/sd", "sd")
+
+    sys.wait(100)
+
+    -- 写入文件
+    local f = io.open("/sd/lua_ver.txt", "wb")
+    f:write(_VERSION)
+    f:close()
+
+    sys.wait(100)
+
+    f = io.open("/sd/lua_ver.txt", "rb")
+    log.info("sdcard", f:read())
+    f:close()
+end)
+
+sys.run()

+ 8 - 19
bsp/air640w/rtt/applications/luat_fs_w60x.c

@@ -16,23 +16,20 @@
 #ifdef BSP_USING_WM_LIBRARIES
 #include "drv_flash.h"
 #include "lfs.h"
+rt_err_t wm_spi_bus_attach_device(const char *bus_name, const char *device_name, rt_uint32_t pin);
 
 int luat_fs_init(void) {
-    //luat_lfs_init();
-    //#ifdef RT_USING_SFUD
-    //dfs_mount("W25QXX", "/", "elm", 0, 0);
-    //#endif
     int re;
-    re = dfs_mount("spi01", "/", "lfs2", 0, 0);
+    re = dfs_mount("onflash", "/", "lfs2", 0, 0);
     if (re) {
       LOG_W("w600 onchiip filesystem damage");
-      re = dfs_mkfs("lfs2", "spi01");
+      re = dfs_mkfs("lfs2", "onflash");
       if (re) {
         LOG_E("mkfs FAIL!!!! re=%d", re);
       }
       else {
         LOG_I("mkfs complete");
-        re = dfs_mount("spi01", "/", "lfs2", 0, 0);
+        re = dfs_mount("onflash", "/", "lfs2", 0, 0);
         if (re) {
           LOG_E("mount FAIL!!!! re=%d", re);
         }
@@ -47,17 +44,10 @@ int luat_fs_init(void) {
     return 0;
 }
 
-rt_err_t wm_spi_bus_attach_device(const char *bus_name, const char *device_name, rt_uint32_t pin);
+
 static int rt_hw_spi_flash_init(void)
 {
-  wm_spi_bus_attach_device(WM_SPI_BUS_NAME, "spi01", 20);
-
-#ifdef RT_USING_SFUD
-  if (RT_NULL == rt_sfud_flash_probe("W25QXX", "spi01"))
-  {
-    //return -RT_ERROR;
-  }
-#endif
+  wm_spi_bus_attach_device(WM_SPI_BUS_NAME, "onflash", 20); -- 占用PB_15了,怎么解决呢
   return RT_EOK;
 }
 INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);
@@ -75,9 +65,9 @@ static void reinit(void* params) {
     // 抹除整个分区
     //wm_flash_erase(USER_ADDR_START, USER_ADDR_END - USER_ADDR_START);
     // 重新格式化
-    dfs_mkfs("lfs2", "spi01");
+    dfs_mkfs("lfs2", "onflash");
     // 挂载
-    dfs_mount("spi01", "/", "lfs2", 0, 0);
+    dfs_mount("onflash", "/", "lfs2", 0, 0);
     t_end = rt_tick_get();
     LOG_I("time use %dms", t_end - t_start);
   }
@@ -85,4 +75,3 @@ static void reinit(void* params) {
 }
 MSH_CMD_EXPORT(reinit, clean all user data);
 #endif
-

+ 7 - 3
luat/include/luat_fs.h

@@ -13,9 +13,10 @@
 #endif
 
 typedef struct luat_fs_conf {
-    char name[8];
-    char filesystem[8];
-    char mount_point[32];
+    char* busname;
+    char* type;
+    char* filesystem;
+    char* mount_point;
 } luat_fs_conf_t;
 
 typedef struct luat_fs_info
@@ -30,6 +31,7 @@ typedef struct luat_fs_info
 
 int luat_fs_init(void);
 
+int luat_fs_mkfs(luat_fs_conf_t *conf);
 int luat_fs_mount(luat_fs_conf_t *conf);
 int luat_fs_umount(luat_fs_conf_t *conf);
 int luat_fs_info(const char* path, luat_fs_info_t *conf);
@@ -51,5 +53,7 @@ int luat_fs_fexist(const char *filename);
 // TODO 文件夹相关的API
 //int luat_fs_diropen(char const* _FileName);
 
+int luat_fs_mkdir(char const* _DirName);
+int luat_fs_rmdir(char const* _DirName);
 
 #endif

+ 23 - 1
luat/modules/luat_fs_weak.c

@@ -64,4 +64,26 @@ LUAT_WEAK size_t luat_fs_fsize(const char *filename) {
         luat_fs_fclose(fd);
     }
     return size;
-}
+}
+
+LUAT_WEAK int luat_fs_mkfs(luat_fs_conf_t *conf) {
+    LLOGE("not support yet : mkfs");
+    return -1;
+}
+LUAT_WEAK int luat_fs_mount(luat_fs_conf_t *conf) {
+    LLOGE("not support yet : mount");
+    return -1;
+}
+LUAT_WEAK int luat_fs_umount(luat_fs_conf_t *conf) {
+    LLOGE("not support yet : umount");
+    return -1;
+}
+
+LUAT_WEAK int luat_fs_mkdir(char const* _DirName) {
+    LLOGE("not support yet : mkdir");
+    return -1;
+}
+LUAT_WEAK int luat_fs_rmdir(char const* _DirName) {
+    LLOGE("not support yet : rmdir");
+    return -1;
+}

+ 43 - 0
luat/modules/luat_lib_fs.c

@@ -50,11 +50,54 @@ static int l_fs_fsize(lua_State *L) {
     return 1;
 }
 
+
+static int l_fs_mkdir(lua_State *L) {
+    const char* path = luaL_checkstring(L, 1);
+    lua_pushinteger(L, luat_fs_mkdir(path));
+    return 1;
+}
+
+static int l_fs_rmdir(lua_State *L) {
+    const char* path = luaL_checkstring(L, 1);
+    lua_pushinteger(L, luat_fs_rmdir(path));
+    return 1;
+}
+
+static int l_fs_mkfs(lua_State *L) {
+    luat_fs_conf_t conf = {0};
+    conf.busname = luaL_checkstring(L, 1);
+    conf.filesystem = luaL_checkstring(L, 2);
+    lua_pushinteger(L, luat_fs_mkfs(&conf));
+    return 1;
+}
+
+static int l_fs_mount(lua_State *L) {
+    luat_fs_conf_t conf = {0};
+    conf.busname = luaL_checkstring(L, 1);
+    conf.filesystem = luaL_checkstring(L, 2);
+    conf.mount_point = luaL_checkstring(L, 3);
+    conf.type = luaL_checkstring(L, 4);
+    lua_pushinteger(L, luat_fs_mount(&conf));
+    return 1;
+}
+
+static int l_fs_umount(lua_State *L) {
+    luat_fs_conf_t conf = {0};
+    conf.mount_point = luaL_checkstring(L, 1);
+    lua_pushinteger(L, luat_fs_umount(&conf));
+    return 1;
+}
+
 #include "rotable.h"
 static const rotable_Reg reg_fs[] =
 {
     { "fsstat",      l_fs_fsstat,   0},
     { "fsize",       l_fs_fsize,    0},
+    { "mkdir",       l_fs_mkdir,    0},
+    { "rmdir",       l_fs_rmdir,    0},
+    { "mkfs",        l_fs_mkfs,     0},
+    { "mount",       l_fs_mount,    0},
+    { "umount",      l_fs_umount,   0},
 	{ NULL,                 NULL,   0}
 };
 

+ 53 - 0
luat/rtt/luat_fs_rtt.c

@@ -25,3 +25,56 @@ int luat_fs_info(const char* path, luat_fs_info_t *conf) {
     }
     return -1;
 }
+
+int luat_fs_mkdir(char const* _DirName) {
+    return mkdir(_DirName, 0);
+}
+int luat_fs_rmdir(char const* _DirName) {
+    return rmdir(_DirName);
+}
+
+int luat_fs_mount(luat_fs_conf_t *conf) {
+    // SPI Flash, 需要SFUD支持
+    #ifdef RT_USING_SFUD
+    if (!rt_strcmp(conf->type, "flash")) {
+        if (rt_sfud_flash_probe("w25q", conf->busname) == RT_NULL) {
+            if (!dfs_mount("w25q", conf->mount_point, conf->filesystem, 0, 0)) {
+                LLOGD("flash mount success");
+                return 0;
+            }
+            else {
+                LLOGD("flash mount fail");
+                return -1;
+            }
+        }
+        else {
+            LLOGD("flash probe fail");
+            return -1;
+        }
+    }
+    #endif
+
+    // SDCard系列
+    #if defined(RT_USING_SPI_MSD)
+    if (!rt_strcmp(conf->type, "sd")) {
+        if (msd_init("sd0", conf->busname) == RT_NULL) {
+            if (!dfs_mount("sd0", conf->mount_point, conf->filesystem, 0, 0)) {
+                LLOGD("sdcard mount success");
+                return 0;
+            }
+            else {
+                LLOGD("sdcard mount fail");
+                return -1;
+            }
+        }
+        else {
+            LLOGD("sdcard init fail");
+            return -1;
+        }
+    }
+    #endif
+
+    LLOGD("not support type: %s", conf->type);
+    return -1;
+}
+