luat_lib_fatfs.c 17 KB

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