luat_lib_fatfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. @module fatfs
  3. @summary 读写fatfs格式
  4. @version 1.0
  5. @date 2020.07.03
  6. @demo fatfs
  7. @tag LUAT_USE_FATFS
  8. @usage
  9. -- 通常只使用fatfs.mount挂载tf/sd卡,其他操作走io库就可以了
  10. */
  11. #include "luat_base.h"
  12. #include "luat_spi.h"
  13. #include "luat_sdio.h"
  14. #include "luat_timer.h"
  15. #include "luat_gpio.h"
  16. #include "luat_mem.h"
  17. #include "luat_fs.h"
  18. #include "ff.h" /* Obtains integer types */
  19. #include "diskio.h" /* Declarations of disk functions */
  20. #define LUAT_LOG_TAG "fatfs"
  21. #include "luat_log.h"
  22. static FATFS *fs = NULL; /* FatFs work area needed for each volume */
  23. extern BYTE FATFS_DEBUG; // debug log, 0 -- disable , 1 -- enable
  24. extern BYTE FATFS_POWER_PIN;
  25. extern uint16_t FATFS_POWER_DELAY;
  26. extern uint8_t FATFS_NO_CRC_CHECK;
  27. extern uint16_t FATFS_WRITE_TO;
  28. DRESULT diskio_open_ramdisk(BYTE pdrv, size_t len);
  29. DRESULT diskio_open_spitf(BYTE pdrv, void* userdata);
  30. DRESULT diskio_open_sdio(BYTE pdrv, void* userdata);
  31. #ifdef LUAT_USE_FS_VFS
  32. extern const struct luat_vfs_filesystem vfs_fs_fatfs;
  33. #endif
  34. /*
  35. 挂载fatfs
  36. @api fatfs.mount(mode,mount_point, spiid_or_spidevice, spi_cs, spi_speed, power_pin, power_on_delay, auto_format)
  37. @int fatfs模式,可选fatfs.SPI,fatfs.SDIO,fatfs.RAM,fatfs.USB
  38. @string 虚拟文件系统的挂载点, 默认是 /fatfs
  39. @int 传入spi device指针,或者spi的id,或者sdio的id
  40. @int 片选脚的GPIO 号, spi模式有效,若前一个参数传的是spi device,这个参数就不需要传
  41. @int SPI最高速度,默认10M, 若前2个参数传的是spi device,这个参数就不需要传
  42. @int TF卡电源控制脚,TF卡初始前先拉低复位再拉高,如果没有,或者是内置电源控制方式,这个参数就不需要传
  43. @int TF卡电源复位过程时间,单位ms,默认值是1
  44. @bool 挂载失败是否尝试格式化,默认是true,即自动格式化. 本参数在2023.8.16添加
  45. @return bool 成功返回true, 否则返回nil或者false
  46. @return string 失败的原因
  47. @usage
  48. -- 方法1, 使用SPI模式
  49. local spiId = 2
  50. local result = spi.setup(
  51. spiId,--spi id
  52. 255, -- 不使用默认CS脚
  53. 0,--CPHA
  54. 0,--CPOL
  55. 8,--数据宽度
  56. 400*1000 -- 初始化时使用较低的频率
  57. )
  58. local TF_CS = 8
  59. gpio.setup(TF_CS, 1)
  60. --fatfs.debug(1) -- 若挂载失败,可以尝试打开调试信息,查找原因
  61. -- 提醒, 若TF/SD模块带电平转换, 通常不支持10M以上的波特率!!
  62. fatfs.mount(fatfs.SPI,"SD", spiId, TF_CS, 24000000)
  63. local data, err = fatfs.getfree("SD")
  64. if data then
  65. log.info("fatfs", "getfree", json.encode(data))
  66. else
  67. log.info("fatfs", "err", err)
  68. end
  69. -- 往下的操作, 使用 io.open("/sd/xxx", "w+") 等io库的API就可以了
  70. */
  71. static int fatfs_mount(lua_State *L)
  72. {
  73. if (FATFS_DEBUG)
  74. LLOGD("fatfs_init>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
  75. if (fs == NULL) {
  76. fs = luat_heap_malloc(sizeof(FATFS));
  77. if (fs == NULL) {
  78. lua_pushboolean(L, 0);
  79. LLOGD("out of memory when malloc FATFS");
  80. lua_pushstring(L, "out of memory when malloc FATFS");
  81. return 2;
  82. }
  83. }
  84. // 挂载点
  85. const char *mount_point = luaL_optstring(L, 2, "/fatfs");
  86. int fatfs_mode = luaL_checkinteger(L, 1);
  87. FATFS_POWER_PIN = luaL_optinteger(L, 6, 0xff);
  88. FATFS_POWER_DELAY = luaL_optinteger(L, 7, 1);
  89. if (fatfs_mode == DISK_SPI){
  90. luat_fatfs_spi_t *spit = luat_heap_malloc(sizeof(luat_fatfs_spi_t));
  91. if (spit == NULL) {
  92. lua_pushboolean(L, 0);
  93. LLOGD("out of memory when malloc luat_fatfs_spi_t");
  94. lua_pushstring(L, "out of memory when malloc luat_fatfs_spi_t");
  95. return 2;
  96. }
  97. memset(spit, 0, sizeof(luat_fatfs_spi_t));
  98. if (lua_type(L, 3) == LUA_TUSERDATA){
  99. spit->spi_device = (luat_spi_device_t*)lua_touserdata(L, 3);
  100. spit->fast_speed = luaL_optinteger(L, 4, 10000000);
  101. spit->type = 1;
  102. diskio_open_spitf(0, (void*)spit);
  103. } else {
  104. spit->type = 0;
  105. spit->spi_id = luaL_optinteger(L, 3, 0); // SPI_1
  106. spit->spi_cs = luaL_optinteger(L, 4, 3); // GPIO_3
  107. spit->fast_speed = luaL_optinteger(L, 5, 10000000);
  108. LLOGD("init sdcard at spi=%d cs=%d", spit->spi_id, spit->spi_cs);
  109. diskio_open_spitf(0, (void*)spit);
  110. }
  111. }else if(fatfs_mode == DISK_SDIO){
  112. luat_fatfs_sdio_t *fatfs_sdio = luat_heap_malloc(sizeof(luat_fatfs_sdio_t));
  113. if (fatfs_sdio == NULL) {
  114. lua_pushboolean(L, 0);
  115. LLOGD("out of memory when malloc luat_fatfs_sdio_t");
  116. lua_pushstring(L, "out of memory when malloc luat_fatfs_sdio_t");
  117. return 2;
  118. }
  119. memset(fatfs_sdio, 0, sizeof(luat_fatfs_sdio_t));
  120. fatfs_sdio->id = luaL_optinteger(L, 3, 0); // SDIO_ID
  121. LLOGD("init FatFS at sdio");
  122. diskio_open_sdio(0, (void*)fatfs_sdio);
  123. }else if(fatfs_mode == DISK_RAM){
  124. LLOGD("init ramdisk at FatFS");
  125. diskio_open_ramdisk(0, luaL_optinteger(L, 3, 64*1024));
  126. }else if(fatfs_mode == DISK_USB){
  127. }else{
  128. LLOGD("fatfs_mode error");
  129. lua_pushboolean(L, 0);
  130. lua_pushstring(L, "fatfs_mode error");
  131. return 2;
  132. }
  133. FRESULT re = f_mount(fs, mount_point, 1);
  134. if (re != FR_OK) {
  135. if (lua_isboolean(L, 8) && lua_toboolean(L, 8) == 0) {
  136. LLOGI("sd/tf mount failed %d but auto-format is disabled", re);
  137. }
  138. else {
  139. LLOGD("mount failed, try auto format");
  140. MKFS_PARM parm = {
  141. .fmt = FM_ANY, // 暂时应付一下ramdisk
  142. .au_size = 0,
  143. .align = 0,
  144. .n_fat = 0,
  145. .n_root = 0,
  146. };
  147. BYTE work[FF_MAX_SS] = {0};
  148. re = f_mkfs(mount_point, &parm, work, FF_MAX_SS);
  149. LLOGD("auto format ret %d", re);
  150. if (re == FR_OK) {
  151. re = f_mount(fs, mount_point, 1);
  152. LLOGD("remount again %d", re);
  153. }
  154. }
  155. }
  156. lua_pushboolean(L, re == FR_OK);
  157. lua_pushinteger(L, re);
  158. if (re == FR_OK) {
  159. if (FATFS_DEBUG)
  160. LLOGD("[FatFS]fatfs_init success");
  161. #ifdef LUAT_USE_FS_VFS
  162. luat_fs_conf_t conf2 = {
  163. .busname = (char*)fs,
  164. .type = "fatfs",
  165. .filesystem = "fatfs",
  166. .mount_point = mount_point,
  167. };
  168. luat_fs_mount(&conf2);
  169. #endif
  170. }
  171. else {
  172. if (FATFS_DEBUG)
  173. LLOGD("[FatFS]fatfs_init FAIL!! re=%d", re);
  174. }
  175. if (FATFS_DEBUG)
  176. LLOGD("fatfs_init<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
  177. return 2;
  178. }
  179. /*
  180. 取消挂载fatfs
  181. @api fatfs.unmount(mount_point)
  182. @string 虚拟文件系统的挂载点, 默认是 fatfs,必须与fatfs.mount一致
  183. @return int 成功返回0, 否则返回失败码
  184. @usage
  185. fatfs.mount("SD")
  186. */
  187. static int fatfs_unmount(lua_State *L) {
  188. const char *mount_point = luaL_optstring(L, 1, "/fatfs");
  189. #ifdef LUAT_USE_FS_VFS
  190. luat_fs_conf_t conf = {
  191. .busname = (char*)fs,
  192. .type = "fatfs",
  193. .filesystem = "fatfs",
  194. .mount_point = mount_point,
  195. };
  196. luat_fs_umount(&conf);
  197. #endif
  198. FRESULT re = f_mount(NULL, "/", 0);
  199. lua_pushinteger(L, re);
  200. return 1;
  201. }
  202. /*
  203. static int fatfs_mkfs(lua_State *L) {
  204. const char *mount_point = luaL_optstring(L, 1, "");
  205. // BYTE sfd = luaL_optinteger(L, 2, 0);
  206. // DWORD au = luaL_optinteger(L, 3, 0);
  207. BYTE work[FF_MAX_SS] = {0};
  208. if (FATFS_DEBUG)
  209. LLOGI("mkfs GO %d");
  210. MKFS_PARM parm = {
  211. .fmt = FM_ANY, // 暂时应付一下ramdisk
  212. .au_size = 0,
  213. .align = 0,
  214. .n_fat = 0,
  215. .n_root = 0,
  216. };
  217. if (!strcmp("ramdisk", mount_point) || !strcmp("ram", mount_point)) {
  218. parm.fmt = FM_ANY | FM_SFD;
  219. }
  220. FRESULT re = f_mkfs(mount_point, &parm, work, FF_MAX_SS);
  221. lua_pushinteger(L, re);
  222. if (FATFS_DEBUG)
  223. LLOGI("mkfs ret %d", re);
  224. return 1;
  225. }
  226. */
  227. /**
  228. 获取可用空间信息
  229. @api fatfs.getfree(mount_point)
  230. @string 挂载点, 需要跟fatfs.mount传入的值一致
  231. @return table 若成功会返回table,否则返回nil
  232. @return int 导致失败的底层返回值
  233. @usage
  234. -- table包含的内容有
  235. -- total_sectors 总扇区数量
  236. -- free_sectors 空闲扇区数量
  237. -- total_kb 总字节数,单位kb
  238. -- free_kb 空闲字节数, 单位kb
  239. -- 注意,当前扇区大小固定在512字节
  240. local data, err = fatfs.getfree("SD")
  241. if data then
  242. log.info("fatfs", "getfree", json.encode(data))
  243. else
  244. log.info("fatfs", "err", err)
  245. end
  246. */
  247. static int fatfs_getfree(lua_State *L)
  248. {
  249. DWORD fre_clust, fre_sect, tot_sect;
  250. // 挂载点
  251. const char *mount_point = luaL_optstring(L, 1, "/fatfs");
  252. FATFS *fs2;
  253. FRESULT re2 = f_getfree(mount_point, &fre_clust, &fs2);
  254. if (re2) {
  255. lua_pushnil(L);
  256. lua_pushinteger(L, re2);
  257. return 2;
  258. }
  259. /* Get total sectors and free sectors */
  260. tot_sect = (fs2->n_fatent - 2) * fs2->csize;
  261. fre_sect = fre_clust * fs2->csize;
  262. lua_newtable(L);
  263. lua_pushstring(L, "total_sectors");
  264. lua_pushinteger(L, tot_sect);
  265. lua_settable(L, -3);
  266. lua_pushstring(L, "free_sectors");
  267. lua_pushinteger(L, fre_sect);
  268. lua_settable(L, -3);
  269. lua_pushstring(L, "total_kb");
  270. lua_pushinteger(L, tot_sect / 2);
  271. lua_settable(L, -3);
  272. lua_pushstring(L, "free_kb");
  273. lua_pushinteger(L, fre_sect / 2);
  274. lua_settable(L, -3);
  275. return 1;
  276. }
  277. /**
  278. 设置调试模式
  279. @api fatfs.debug(value)
  280. @int 是否进入调试模式,1代表进入调试模式,增加调试日志
  281. @return nil 无返回值
  282. */
  283. static int fatfs_debug_mode(lua_State *L) {
  284. FATFS_DEBUG = luaL_optinteger(L, 1, 1);
  285. return 0;
  286. }
  287. /**
  288. 设置fatfs一些特殊参数,大部分卡无需配置,部分不能正常读写的卡,经过配置后可能能读写成功
  289. @api fatfs.config(crc_check, write_to)
  290. @int 读取时是否跳过CRC检查,1跳过不检查CRC,0不跳过检查CRC,默认不跳过,除非TF卡不支持CRC校验,否则不应该跳过!
  291. @int 单次写入超时时间,单位ms,默认100ms。
  292. @return nil 无返回值
  293. */
  294. static int fatfs_config(lua_State *L) {
  295. FATFS_NO_CRC_CHECK = luaL_optinteger(L, 1, 0);
  296. FATFS_WRITE_TO = luaL_optinteger(L, 2, 100);
  297. return 0;
  298. }
  299. #if 0
  300. // ------------------------------------------------
  301. // ------------------------------------------------
  302. static int fatfs_mkdir(lua_State *L) {
  303. int luaType = lua_type( L, 1);
  304. if(luaType != LUA_TSTRING) {
  305. lua_pushinteger(L, -1);
  306. lua_pushstring(L, "file path must string");
  307. return 2;
  308. }
  309. FRESULT re = f_mkdir(lua_tostring(L, 1));
  310. lua_pushinteger(L, re);
  311. return 1;
  312. }
  313. static int fatfs_lsdir(lua_State *L)
  314. {
  315. //FIL Fil; /* File object needed for each open file */
  316. DIR dir;
  317. FILINFO fileinfo;
  318. int luaType = lua_type( L, 1);
  319. if(luaType != LUA_TSTRING) {
  320. lua_pushinteger(L, -1);
  321. lua_pushstring(L, "dir must string");
  322. return 2;
  323. }
  324. //u8 *buf;
  325. size_t len;
  326. const char *buf = lua_tolstring( L, 1, &len );
  327. char dirname[len+1];
  328. memcpy(dirname, buf, len);
  329. dirname[len] = 0x00;
  330. FRESULT re = f_opendir(&dir, dirname);
  331. if (re != FR_OK) {
  332. lua_pushinteger(L, re);
  333. return 1;
  334. }
  335. lua_pushinteger(L, 0);
  336. lua_newtable(L);
  337. while(f_readdir(&dir, &fileinfo) == FR_OK) {
  338. if(!fileinfo.fname[0]) break;
  339. lua_pushlstring(L, fileinfo.fname, strlen(fileinfo.fname));
  340. lua_newtable(L);
  341. lua_pushstring(L, "size");
  342. lua_pushinteger(L, fileinfo.fsize);
  343. lua_settable(L, -3);
  344. lua_pushstring(L, "date");
  345. lua_pushinteger(L, fileinfo.fdate);
  346. lua_settable(L, -3);
  347. lua_pushstring(L, "time");
  348. lua_pushinteger(L, fileinfo.ftime);
  349. lua_settable(L, -3);
  350. lua_pushstring(L, "attrib");
  351. lua_pushinteger(L, fileinfo.fattrib);
  352. lua_settable(L, -3);
  353. lua_pushstring(L, "isdir");
  354. lua_pushinteger(L, fileinfo.fattrib & AM_DIR);
  355. lua_settable(L, -3);
  356. lua_settable(L, -3);
  357. }
  358. f_closedir(&dir);
  359. //LLOGD("[FatFS] lua_gettop=%d", lua_gettop(L));
  360. return 2;
  361. }
  362. //-------------------------------------------------------------
  363. static int fatfs_stat(lua_State *L) {
  364. int luaType = lua_type(L, 1);
  365. if(luaType != LUA_TSTRING) {
  366. lua_pushinteger(L, -1);
  367. lua_pushstring(L, "file path must string");
  368. return 2;
  369. }
  370. FILINFO fileinfo;
  371. const char *path = lua_tostring(L, 1);
  372. FRESULT re = f_stat(path, &fileinfo);
  373. lua_pushinteger(L, re);
  374. if (re == FR_OK) {
  375. lua_newtable(L);
  376. lua_pushstring(L, "size");
  377. lua_pushinteger(L, fileinfo.fsize);
  378. lua_rawset(L, -3);
  379. lua_pushstring(L, "date");
  380. lua_pushinteger(L, fileinfo.fdate);
  381. lua_rawset(L, -3);
  382. lua_pushstring(L, "time");
  383. lua_pushinteger(L, fileinfo.ftime);
  384. lua_rawset(L, -3);
  385. lua_pushstring(L, "attrib");
  386. lua_pushinteger(L, fileinfo.fattrib);
  387. lua_rawset(L, -3);
  388. lua_pushstring(L, "isdir");
  389. lua_pushinteger(L, fileinfo.fattrib & AM_DIR);
  390. lua_rawset(L, -3);
  391. }
  392. else {
  393. lua_pushnil(L);
  394. }
  395. return 2;
  396. }
  397. /**
  398. * fatfs.open("adc.txt")
  399. * fatfs.open("adc.txt", 2)
  400. */
  401. static int fatfs_open(lua_State *L) {
  402. int luaType = lua_type( L, 1);
  403. if(luaType != LUA_TSTRING) {
  404. lua_pushnil(L);
  405. lua_pushinteger(L, -1);
  406. lua_pushstring(L, "file path must string");
  407. return 3;
  408. }
  409. const char *path = lua_tostring(L, 1);
  410. int flag = luaL_optinteger(L, 2, 1); // 第二个参数
  411. flag |= luaL_optinteger(L, 3, 0); // 第三个参数
  412. flag |= luaL_optinteger(L, 4, 0); // 第四个参数
  413. if (FATFS_DEBUG)
  414. LLOGD("[FatFS]open %s %0X", path, flag);
  415. FIL* fil = (FIL*)lua_newuserdata(L, sizeof(FIL));
  416. FRESULT re = f_open(fil, path, (BYTE)flag);
  417. if (re != FR_OK) {
  418. lua_remove(L, -1);
  419. lua_pushnil(L);
  420. lua_pushinteger(L, re);
  421. return 2;
  422. }
  423. return 1;
  424. }
  425. static int fatfs_close(lua_State *L) {
  426. int luaType = lua_type(L, 1);
  427. if(luaType != LUA_TUSERDATA) {
  428. lua_pushinteger(L, -1);
  429. lua_pushstring(L, "must be FIL*");
  430. return 2;
  431. }
  432. FIL* fil = (FIL*)lua_touserdata(L, 1);
  433. FRESULT re = f_close(fil);
  434. //free(fil);
  435. lua_pushinteger(L, re);
  436. return 1;
  437. }
  438. static int fatfs_seek(lua_State *L) {
  439. int luaType = lua_type( L, 1);
  440. if(luaType != LUA_TUSERDATA) {
  441. lua_pushinteger(L, -1);
  442. lua_pushstring(L, "must be FIL*");
  443. return 2;
  444. }
  445. UINT seek = luaL_optinteger(L, 2, 0);
  446. FRESULT re = f_lseek((FIL*)lua_touserdata(L, 1), seek);
  447. lua_pushinteger(L, re);
  448. return 1;
  449. }
  450. static int fatfs_truncate(lua_State *L) {
  451. int luaType = lua_type( L, 1);
  452. if(luaType != LUA_TUSERDATA) {
  453. lua_pushinteger(L, -1);
  454. lua_pushstring(L, "must be FIL*");
  455. return 2;
  456. }
  457. FRESULT re = f_truncate((FIL*)lua_touserdata(L, 1));
  458. lua_pushinteger(L, re);
  459. return 1;
  460. }
  461. static int fatfs_read(lua_State *L) {
  462. int luaType = lua_type( L, 1);
  463. if(luaType != LUA_TUSERDATA) {
  464. lua_pushinteger(L, -1);
  465. lua_pushstring(L, "must be FIL*");
  466. return 2;
  467. }
  468. UINT limit = luaL_optinteger(L, 2, 512);
  469. BYTE buf[limit];
  470. UINT len;
  471. if (FATFS_DEBUG)
  472. LLOGD("[FatFS]readfile limit=%d", limit);
  473. FRESULT re = f_read((FIL*)lua_touserdata(L, 1), buf, limit, &len);
  474. lua_pushinteger(L, re);
  475. if (re != FR_OK) {
  476. return 1;
  477. }
  478. lua_pushlstring(L, (const char*)buf, len);
  479. return 2;
  480. }
  481. static int fatfs_write(lua_State *L) {
  482. int luaType = lua_type( L, 1);
  483. if(luaType != LUA_TUSERDATA) {
  484. lua_pushinteger(L, -1);
  485. lua_pushstring(L, "must be FIL*");
  486. return 2;
  487. }
  488. FIL* fil = (FIL*)lua_touserdata(L, 1);
  489. luaType = lua_type( L, 2 );
  490. size_t len;
  491. char* buf;
  492. FRESULT re = FR_OK;
  493. if(luaType == LUA_TSTRING )
  494. {
  495. buf = (char*)lua_tolstring( L, 2, &len );
  496. re = f_write(fil, buf, len, &len);
  497. }
  498. else if(luaType == LUA_TLIGHTUSERDATA)
  499. {
  500. buf = lua_touserdata(L, 2);
  501. len = lua_tointeger( L, 3);
  502. re = f_write(fil, buf, len, &len);
  503. }
  504. if (FATFS_DEBUG)
  505. LLOGD("[FatFS]write re=%d len=%d", re, len);
  506. lua_pushinteger(L, re);
  507. lua_pushinteger(L, len);
  508. return 2;
  509. }
  510. static int fatfs_remove(lua_State *L) {
  511. int luaType = lua_type(L, 1);
  512. if(luaType != LUA_TSTRING) {
  513. lua_pushinteger(L, -1);
  514. lua_pushstring(L, "file path must string");
  515. return 2;
  516. }
  517. FRESULT re = f_unlink(lua_tostring(L, 1));
  518. lua_pushinteger(L, re);
  519. return 1;
  520. }
  521. static int fatfs_rename(lua_State *L) {
  522. int luaType = lua_type(L, 1);
  523. if(luaType != LUA_TSTRING) {
  524. lua_pushinteger(L, -1);
  525. lua_pushstring(L, "source file path must string");
  526. return 2;
  527. }
  528. luaType = lua_type(L, 2);
  529. if(luaType != LUA_TSTRING) {
  530. lua_pushinteger(L, -1);
  531. lua_pushstring(L, "dest file path must string");
  532. return 2;
  533. }
  534. FRESULT re = f_rename(lua_tostring(L, 1), lua_tostring(L, 2));
  535. lua_pushinteger(L, re);
  536. return 1;
  537. }
  538. /**
  539. * fatfs.readfile("adc.txt")
  540. * fatfs.readfile("adc.txt", 512, 0) 默认只读取512字节,从0字节开始读
  541. */
  542. static int fatfs_readfile(lua_State *L) {
  543. int luaType = lua_type( L, 1);
  544. if(luaType != LUA_TSTRING) {
  545. lua_pushinteger(L, -1);
  546. lua_pushstring(L, "file path must string");
  547. return 2;
  548. }
  549. FIL fil;
  550. FRESULT re = f_open(&fil, lua_tostring(L, 1), FA_READ);
  551. if (re != FR_OK) {
  552. lua_pushinteger(L, re);
  553. return 1;
  554. }
  555. DWORD limit = luaL_optinteger(L, 2, 512);
  556. DWORD seek = luaL_optinteger(L, 3, 0);
  557. if (seek > 0) {
  558. f_lseek(&fil, seek);
  559. }
  560. BYTE buf[limit];
  561. size_t len;
  562. if (FATFS_DEBUG)
  563. LLOGD("[FatFS]readfile seek=%d limit=%d", seek, limit);
  564. FRESULT fr = f_read(&fil, buf, limit, &len);
  565. if (fr != FR_OK) {
  566. lua_pushinteger(L, -3);
  567. lua_pushinteger(L, fr);
  568. return 2;
  569. }
  570. f_close(&fil);
  571. lua_pushinteger(L, 0);
  572. lua_pushlstring(L, (const char*)buf, len);
  573. if (FATFS_DEBUG)
  574. LLOGD("[FatFS]readfile seek=%d limit=%d len=%d", seek, limit, len);
  575. return 2;
  576. }
  577. #endif
  578. // Module function map
  579. #include "rotable2.h"
  580. static const rotable_Reg_t reg_fatfs[] =
  581. {
  582. { "init", ROREG_FUNC(fatfs_mount)}, //初始化,挂载, 别名方法
  583. { "mount", ROREG_FUNC(fatfs_mount)}, //初始化,挂载
  584. { "getfree", ROREG_FUNC(fatfs_getfree)}, // 获取文件系统大小,剩余空间
  585. { "debug", ROREG_FUNC(fatfs_debug_mode)}, // 调试模式,打印更多日志
  586. { "config", ROREG_FUNC(fatfs_config)}, //初始化,挂载, 别名方法
  587. { "unmount", ROREG_FUNC(fatfs_unmount)}, // 取消挂载
  588. #if 0
  589. { "mkfs", ROREG_FUNC(fatfs_mkfs)}, // 格式化!!!
  590. //{ "test", fatfs_test)},
  591. { "lsdir", ROREG_FUNC(fatfs_lsdir)}, // 列举目录下的文件,名称,大小,日期,属性
  592. { "mkdir", ROREG_FUNC(fatfs_mkdir)}, // 列举目录下的文件,名称,大小,日期,属性
  593. { "stat", ROREG_FUNC(fatfs_stat)}, // 查询文件信息
  594. { "open", ROREG_FUNC(fatfs_open)}, // 打开一个文件句柄
  595. { "close", ROREG_FUNC(fatfs_close)}, // 关闭一个文件句柄
  596. { "seek", ROREG_FUNC(fatfs_seek)}, // 移动句柄的当前位置
  597. { "truncate", ROREG_FUNC(fatfs_truncate)}, // 缩减文件尺寸到当前seek位置
  598. { "read", ROREG_FUNC(fatfs_read)}, // 读取数据
  599. { "write", ROREG_FUNC(fatfs_write)}, // 写入数据
  600. { "remove", ROREG_FUNC(fatfs_remove)}, // 删除文件,别名方法
  601. { "unlink", ROREG_FUNC(fatfs_remove)}, // 删除文件
  602. { "rename", ROREG_FUNC(fatfs_rename)}, // 文件改名
  603. { "readfile", ROREG_FUNC(fatfs_readfile)}, // 读取文件的简易方法
  604. #endif
  605. { "SPI", ROREG_INT(DISK_SPI)},
  606. { "SDIO", ROREG_INT(DISK_SDIO)},
  607. { "RAM", ROREG_INT(DISK_RAM)},
  608. // { "USB", ROREG_INT(DISK_USB)},
  609. { NULL, ROREG_INT(0)}
  610. };
  611. int luaopen_fatfs( lua_State *L )
  612. {
  613. luat_newlib2(L, reg_fatfs);
  614. #ifdef LUAT_USE_FS_VFS
  615. luat_vfs_reg(&vfs_fs_fatfs);
  616. #endif
  617. return 1;
  618. }