luat_lib_fatfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. 取消挂载fatfs
  179. @api fatfs.unmount(mount_point)
  180. @string 虚拟文件系统的挂载点, 默认是 fatfs,必须与fatfs.mount一致
  181. @return int 成功返回0, 否则返回失败码
  182. @usage
  183. fatfs.mount("SD")
  184. */
  185. static int fatfs_unmount(lua_State *L) {
  186. const char *mount_point = luaL_optstring(L, 1, "/fatfs");
  187. #ifdef LUAT_USE_FS_VFS
  188. luat_fs_conf_t conf = {
  189. .busname = (char*)fs,
  190. .type = "fatfs",
  191. .filesystem = "fatfs",
  192. .mount_point = mount_point,
  193. };
  194. luat_fs_umount(&conf);
  195. #endif
  196. FRESULT re = f_mount(NULL, "/", 0);
  197. lua_pushinteger(L, re);
  198. return 1;
  199. }
  200. /*
  201. static int fatfs_mkfs(lua_State *L) {
  202. const char *mount_point = luaL_optstring(L, 1, "");
  203. // BYTE sfd = luaL_optinteger(L, 2, 0);
  204. // DWORD au = luaL_optinteger(L, 3, 0);
  205. BYTE work[FF_MAX_SS] = {0};
  206. if (FATFS_DEBUG)
  207. LLOGI("mkfs GO %d");
  208. MKFS_PARM parm = {
  209. .fmt = FM_ANY, // 暂时应付一下ramdisk
  210. .au_size = 0,
  211. .align = 0,
  212. .n_fat = 0,
  213. .n_root = 0,
  214. };
  215. if (!strcmp("ramdisk", mount_point) || !strcmp("ram", mount_point)) {
  216. parm.fmt = FM_ANY | FM_SFD;
  217. }
  218. FRESULT re = f_mkfs(mount_point, &parm, work, FF_MAX_SS);
  219. lua_pushinteger(L, re);
  220. if (FATFS_DEBUG)
  221. LLOGI("mkfs ret %d", re);
  222. return 1;
  223. }
  224. */
  225. /**
  226. 获取可用空间信息
  227. @api fatfs.getfree(mount_point)
  228. @string 挂载点, 需要跟fatfs.mount传入的值一致
  229. @return table 若成功会返回table,否则返回nil
  230. @return int 导致失败的底层返回值
  231. @usage
  232. -- table包含的内容有
  233. -- total_sectors 总扇区数量
  234. -- free_sectors 空闲扇区数量
  235. -- total_kb 总字节数,单位kb
  236. -- free_kb 空闲字节数, 单位kb
  237. -- 注意,当前扇区大小固定在512字节
  238. local data, err = fatfs.getfree("SD")
  239. if data then
  240. log.info("fatfs", "getfree", json.encode(data))
  241. else
  242. log.info("fatfs", "err", err)
  243. end
  244. */
  245. static int fatfs_getfree(lua_State *L)
  246. {
  247. DWORD fre_clust, fre_sect, tot_sect;
  248. // 挂载点
  249. const char *mount_point = luaL_optstring(L, 1, "");
  250. FATFS *fs2;
  251. FRESULT re2 = f_getfree(mount_point, &fre_clust, &fs2);
  252. if (re2) {
  253. lua_pushnil(L);
  254. lua_pushinteger(L, re2);
  255. return 2;
  256. }
  257. /* Get total sectors and free sectors */
  258. tot_sect = (fs2->n_fatent - 2) * fs2->csize;
  259. fre_sect = fre_clust * fs2->csize;
  260. lua_newtable(L);
  261. lua_pushstring(L, "total_sectors");
  262. lua_pushinteger(L, tot_sect);
  263. lua_settable(L, -3);
  264. lua_pushstring(L, "free_sectors");
  265. lua_pushinteger(L, fre_sect);
  266. lua_settable(L, -3);
  267. lua_pushstring(L, "total_kb");
  268. lua_pushinteger(L, tot_sect / 2);
  269. lua_settable(L, -3);
  270. lua_pushstring(L, "free_kb");
  271. lua_pushinteger(L, fre_sect / 2);
  272. lua_settable(L, -3);
  273. return 1;
  274. }
  275. /**
  276. 设置调试模式
  277. @api fatfs.debug(value)
  278. @bool 是否进入调试模式,true代表进入调试模式,增加调试日志
  279. @return nil 无返回值
  280. */
  281. static int fatfs_debug_mode(lua_State *L) {
  282. FATFS_DEBUG = luaL_optinteger(L, 1, 1);
  283. return 0;
  284. }
  285. #if 0
  286. // ------------------------------------------------
  287. // ------------------------------------------------
  288. static int fatfs_mkdir(lua_State *L) {
  289. int luaType = lua_type( L, 1);
  290. if(luaType != LUA_TSTRING) {
  291. lua_pushinteger(L, -1);
  292. lua_pushstring(L, "file path must string");
  293. return 2;
  294. }
  295. FRESULT re = f_mkdir(lua_tostring(L, 1));
  296. lua_pushinteger(L, re);
  297. return 1;
  298. }
  299. static int fatfs_lsdir(lua_State *L)
  300. {
  301. //FIL Fil; /* File object needed for each open file */
  302. DIR dir;
  303. FILINFO fileinfo;
  304. int luaType = lua_type( L, 1);
  305. if(luaType != LUA_TSTRING) {
  306. lua_pushinteger(L, -1);
  307. lua_pushstring(L, "dir must string");
  308. return 2;
  309. }
  310. //u8 *buf;
  311. size_t len;
  312. const char *buf = lua_tolstring( L, 1, &len );
  313. char dirname[len+1];
  314. memcpy(dirname, buf, len);
  315. dirname[len] = 0x00;
  316. FRESULT re = f_opendir(&dir, dirname);
  317. if (re != FR_OK) {
  318. lua_pushinteger(L, re);
  319. return 1;
  320. }
  321. lua_pushinteger(L, 0);
  322. lua_newtable(L);
  323. while(f_readdir(&dir, &fileinfo) == FR_OK) {
  324. if(!fileinfo.fname[0]) break;
  325. lua_pushlstring(L, fileinfo.fname, strlen(fileinfo.fname));
  326. lua_newtable(L);
  327. lua_pushstring(L, "size");
  328. lua_pushinteger(L, fileinfo.fsize);
  329. lua_settable(L, -3);
  330. lua_pushstring(L, "date");
  331. lua_pushinteger(L, fileinfo.fdate);
  332. lua_settable(L, -3);
  333. lua_pushstring(L, "time");
  334. lua_pushinteger(L, fileinfo.ftime);
  335. lua_settable(L, -3);
  336. lua_pushstring(L, "attrib");
  337. lua_pushinteger(L, fileinfo.fattrib);
  338. lua_settable(L, -3);
  339. lua_pushstring(L, "isdir");
  340. lua_pushinteger(L, fileinfo.fattrib & AM_DIR);
  341. lua_settable(L, -3);
  342. lua_settable(L, -3);
  343. }
  344. f_closedir(&dir);
  345. //LLOGD("[FatFS] lua_gettop=%d", lua_gettop(L));
  346. return 2;
  347. }
  348. //-------------------------------------------------------------
  349. static int fatfs_stat(lua_State *L) {
  350. int luaType = lua_type(L, 1);
  351. if(luaType != LUA_TSTRING) {
  352. lua_pushinteger(L, -1);
  353. lua_pushstring(L, "file path must string");
  354. return 2;
  355. }
  356. FILINFO fileinfo;
  357. const char *path = lua_tostring(L, 1);
  358. FRESULT re = f_stat(path, &fileinfo);
  359. lua_pushinteger(L, re);
  360. if (re == FR_OK) {
  361. lua_newtable(L);
  362. lua_pushstring(L, "size");
  363. lua_pushinteger(L, fileinfo.fsize);
  364. lua_rawset(L, -3);
  365. lua_pushstring(L, "date");
  366. lua_pushinteger(L, fileinfo.fdate);
  367. lua_rawset(L, -3);
  368. lua_pushstring(L, "time");
  369. lua_pushinteger(L, fileinfo.ftime);
  370. lua_rawset(L, -3);
  371. lua_pushstring(L, "attrib");
  372. lua_pushinteger(L, fileinfo.fattrib);
  373. lua_rawset(L, -3);
  374. lua_pushstring(L, "isdir");
  375. lua_pushinteger(L, fileinfo.fattrib & AM_DIR);
  376. lua_rawset(L, -3);
  377. }
  378. else {
  379. lua_pushnil(L);
  380. }
  381. return 2;
  382. }
  383. /**
  384. * fatfs.open("adc.txt")
  385. * fatfs.open("adc.txt", 2)
  386. */
  387. static int fatfs_open(lua_State *L) {
  388. int luaType = lua_type( L, 1);
  389. if(luaType != LUA_TSTRING) {
  390. lua_pushnil(L);
  391. lua_pushinteger(L, -1);
  392. lua_pushstring(L, "file path must string");
  393. return 3;
  394. }
  395. const char *path = lua_tostring(L, 1);
  396. int flag = luaL_optinteger(L, 2, 1); // 第二个参数
  397. flag |= luaL_optinteger(L, 3, 0); // 第三个参数
  398. flag |= luaL_optinteger(L, 4, 0); // 第四个参数
  399. if (FATFS_DEBUG)
  400. LLOGD("[FatFS]open %s %0X", path, flag);
  401. FIL* fil = (FIL*)lua_newuserdata(L, sizeof(FIL));
  402. FRESULT re = f_open(fil, path, (BYTE)flag);
  403. if (re != FR_OK) {
  404. lua_remove(L, -1);
  405. lua_pushnil(L);
  406. lua_pushinteger(L, re);
  407. return 2;
  408. }
  409. return 1;
  410. }
  411. static int fatfs_close(lua_State *L) {
  412. int luaType = lua_type(L, 1);
  413. if(luaType != LUA_TUSERDATA) {
  414. lua_pushinteger(L, -1);
  415. lua_pushstring(L, "must be FIL*");
  416. return 2;
  417. }
  418. FIL* fil = (FIL*)lua_touserdata(L, 1);
  419. FRESULT re = f_close(fil);
  420. //free(fil);
  421. lua_pushinteger(L, re);
  422. return 1;
  423. }
  424. static int fatfs_seek(lua_State *L) {
  425. int luaType = lua_type( L, 1);
  426. if(luaType != LUA_TUSERDATA) {
  427. lua_pushinteger(L, -1);
  428. lua_pushstring(L, "must be FIL*");
  429. return 2;
  430. }
  431. UINT seek = luaL_optinteger(L, 2, 0);
  432. FRESULT re = f_lseek((FIL*)lua_touserdata(L, 1), seek);
  433. lua_pushinteger(L, re);
  434. return 1;
  435. }
  436. static int fatfs_truncate(lua_State *L) {
  437. int luaType = lua_type( L, 1);
  438. if(luaType != LUA_TUSERDATA) {
  439. lua_pushinteger(L, -1);
  440. lua_pushstring(L, "must be FIL*");
  441. return 2;
  442. }
  443. FRESULT re = f_truncate((FIL*)lua_touserdata(L, 1));
  444. lua_pushinteger(L, re);
  445. return 1;
  446. }
  447. static int fatfs_read(lua_State *L) {
  448. int luaType = lua_type( L, 1);
  449. if(luaType != LUA_TUSERDATA) {
  450. lua_pushinteger(L, -1);
  451. lua_pushstring(L, "must be FIL*");
  452. return 2;
  453. }
  454. UINT limit = luaL_optinteger(L, 2, 512);
  455. BYTE buf[limit];
  456. UINT len;
  457. if (FATFS_DEBUG)
  458. LLOGD("[FatFS]readfile limit=%d", limit);
  459. FRESULT re = f_read((FIL*)lua_touserdata(L, 1), buf, limit, &len);
  460. lua_pushinteger(L, re);
  461. if (re != FR_OK) {
  462. return 1;
  463. }
  464. lua_pushlstring(L, (const char*)buf, len);
  465. return 2;
  466. }
  467. static int fatfs_write(lua_State *L) {
  468. int luaType = lua_type( L, 1);
  469. if(luaType != LUA_TUSERDATA) {
  470. lua_pushinteger(L, -1);
  471. lua_pushstring(L, "must be FIL*");
  472. return 2;
  473. }
  474. FIL* fil = (FIL*)lua_touserdata(L, 1);
  475. luaType = lua_type( L, 2 );
  476. size_t len;
  477. char* buf;
  478. FRESULT re = FR_OK;
  479. if(luaType == LUA_TSTRING )
  480. {
  481. buf = (char*)lua_tolstring( L, 2, &len );
  482. re = f_write(fil, buf, len, &len);
  483. }
  484. else if(luaType == LUA_TLIGHTUSERDATA)
  485. {
  486. buf = lua_touserdata(L, 2);
  487. len = lua_tointeger( L, 3);
  488. re = f_write(fil, buf, len, &len);
  489. }
  490. if (FATFS_DEBUG)
  491. LLOGD("[FatFS]write re=%d len=%d", re, len);
  492. lua_pushinteger(L, re);
  493. lua_pushinteger(L, len);
  494. return 2;
  495. }
  496. static int fatfs_remove(lua_State *L) {
  497. int luaType = lua_type(L, 1);
  498. if(luaType != LUA_TSTRING) {
  499. lua_pushinteger(L, -1);
  500. lua_pushstring(L, "file path must string");
  501. return 2;
  502. }
  503. FRESULT re = f_unlink(lua_tostring(L, 1));
  504. lua_pushinteger(L, re);
  505. return 1;
  506. }
  507. static int fatfs_rename(lua_State *L) {
  508. int luaType = lua_type(L, 1);
  509. if(luaType != LUA_TSTRING) {
  510. lua_pushinteger(L, -1);
  511. lua_pushstring(L, "source file path must string");
  512. return 2;
  513. }
  514. luaType = lua_type(L, 2);
  515. if(luaType != LUA_TSTRING) {
  516. lua_pushinteger(L, -1);
  517. lua_pushstring(L, "dest file path must string");
  518. return 2;
  519. }
  520. FRESULT re = f_rename(lua_tostring(L, 1), lua_tostring(L, 2));
  521. lua_pushinteger(L, re);
  522. return 1;
  523. }
  524. /**
  525. * fatfs.readfile("adc.txt")
  526. * fatfs.readfile("adc.txt", 512, 0) 默认只读取512字节,从0字节开始读
  527. */
  528. static int fatfs_readfile(lua_State *L) {
  529. int luaType = lua_type( L, 1);
  530. if(luaType != LUA_TSTRING) {
  531. lua_pushinteger(L, -1);
  532. lua_pushstring(L, "file path must string");
  533. return 2;
  534. }
  535. FIL fil;
  536. FRESULT re = f_open(&fil, lua_tostring(L, 1), FA_READ);
  537. if (re != FR_OK) {
  538. lua_pushinteger(L, re);
  539. return 1;
  540. }
  541. DWORD limit = luaL_optinteger(L, 2, 512);
  542. DWORD seek = luaL_optinteger(L, 3, 0);
  543. if (seek > 0) {
  544. f_lseek(&fil, seek);
  545. }
  546. BYTE buf[limit];
  547. size_t len;
  548. if (FATFS_DEBUG)
  549. LLOGD("[FatFS]readfile seek=%d limit=%d", seek, limit);
  550. FRESULT fr = f_read(&fil, buf, limit, &len);
  551. if (fr != FR_OK) {
  552. lua_pushinteger(L, -3);
  553. lua_pushinteger(L, fr);
  554. return 2;
  555. }
  556. f_close(&fil);
  557. lua_pushinteger(L, 0);
  558. lua_pushlstring(L, (const char*)buf, len);
  559. if (FATFS_DEBUG)
  560. LLOGD("[FatFS]readfile seek=%d limit=%d len=%d", seek, limit, len);
  561. return 2;
  562. }
  563. #endif
  564. // Module function map
  565. #include "rotable2.h"
  566. static const rotable_Reg_t reg_fatfs[] =
  567. {
  568. { "init", ROREG_FUNC(fatfs_mount)}, //初始化,挂载, 别名方法
  569. { "mount", ROREG_FUNC(fatfs_mount)}, //初始化,挂载
  570. { "getfree", ROREG_FUNC(fatfs_getfree)}, // 获取文件系统大小,剩余空间
  571. { "debug", ROREG_FUNC(fatfs_debug_mode)}, // 调试模式,打印更多日志
  572. { "unmount", ROREG_FUNC(fatfs_unmount)}, // 取消挂载
  573. #if 0
  574. { "mkfs", ROREG_FUNC(fatfs_mkfs)}, // 格式化!!!
  575. //{ "test", fatfs_test)},
  576. { "lsdir", ROREG_FUNC(fatfs_lsdir)}, // 列举目录下的文件,名称,大小,日期,属性
  577. { "mkdir", ROREG_FUNC(fatfs_mkdir)}, // 列举目录下的文件,名称,大小,日期,属性
  578. { "stat", ROREG_FUNC(fatfs_stat)}, // 查询文件信息
  579. { "open", ROREG_FUNC(fatfs_open)}, // 打开一个文件句柄
  580. { "close", ROREG_FUNC(fatfs_close)}, // 关闭一个文件句柄
  581. { "seek", ROREG_FUNC(fatfs_seek)}, // 移动句柄的当前位置
  582. { "truncate", ROREG_FUNC(fatfs_truncate)}, // 缩减文件尺寸到当前seek位置
  583. { "read", ROREG_FUNC(fatfs_read)}, // 读取数据
  584. { "write", ROREG_FUNC(fatfs_write)}, // 写入数据
  585. { "remove", ROREG_FUNC(fatfs_remove)}, // 删除文件,别名方法
  586. { "unlink", ROREG_FUNC(fatfs_remove)}, // 删除文件
  587. { "rename", ROREG_FUNC(fatfs_rename)}, // 文件改名
  588. { "readfile", ROREG_FUNC(fatfs_readfile)}, // 读取文件的简易方法
  589. #endif
  590. { "SPI", ROREG_INT(DISK_SPI)},
  591. { "SDIO", ROREG_INT(DISK_SDIO)},
  592. { "RAM", ROREG_INT(DISK_RAM)},
  593. // { "USB", ROREG_INT(DISK_USB)},
  594. { NULL, ROREG_INT(0)}
  595. };
  596. int luaopen_fatfs( lua_State *L )
  597. {
  598. luat_newlib2(L, reg_fatfs);
  599. #ifdef LUAT_USE_FS_VFS
  600. luat_vfs_reg(&vfs_fs_fatfs);
  601. #endif
  602. return 1;
  603. }