luat_lib_sfud.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. @module sfud
  3. @summary SPI FLASH sfud软件包
  4. @version 1.0
  5. @date 2021.09.23
  6. */
  7. #include "luat_base.h"
  8. #include "luat_spi.h"
  9. #include "sfud.h"
  10. #define LUAT_LOG_TAG "luat.sfud"
  11. #include "luat_log.h"
  12. luat_sfud_flash_t luat_sfud;
  13. /*
  14. 初始化sfud
  15. @api sfud.init(spi_id, spi_cs, spi_bandrate)/sfud.init(spi_device)
  16. @int spi_id SPI的ID/userdata spi_device
  17. @int spi_cs SPI的片选
  18. @int spi_bandrate SPI的频率
  19. @return bool 成功返回true,否则返回false
  20. @usage
  21. --spi
  22. log.info("sfud.init",sfud.init(0,20,20 * 1000 * 1000))
  23. --spi_device
  24. local spi_device = spi.deviceSetup(0,17,0,0,8,2000000,spi.MSB,1,1)
  25. log.info("sfud.init",sfud.init(spi_device))
  26. */
  27. static int luat_sfud_init(lua_State *L){
  28. static luat_spi_t sfud_spi_flash;
  29. static luat_spi_device_t* sfud_spi_device_flash = NULL;
  30. if (lua_type(L, 1) == LUA_TNUMBER){
  31. luat_sfud.luat_spi = LUAT_TYPE_SPI;
  32. sfud_spi_flash.id = luaL_checkinteger(L, 1);
  33. sfud_spi_flash.cs = luaL_checkinteger(L, 2);
  34. sfud_spi_flash.bandrate = luaL_checkinteger(L, 3);
  35. sfud_spi_flash.CPHA = 1; // CPHA0
  36. sfud_spi_flash.CPOL = 1; // CPOL0
  37. sfud_spi_flash.dataw = 8; // 8bit
  38. sfud_spi_flash.bit_dict = 1; // MSB=1, LSB=0
  39. sfud_spi_flash.master = 1; // master=1,slave=0
  40. sfud_spi_flash.mode = 1; // FULL=1, half=0
  41. luat_spi_setup(&sfud_spi_flash);
  42. luat_sfud.user_data = &sfud_spi_flash;
  43. }else if (lua_type(L, 1) == LUA_TUSERDATA){
  44. luat_sfud.luat_spi = LUAT_TYPE_SPI_DEVICE;
  45. sfud_spi_device_flash = (luat_spi_device_t*)lua_touserdata(L, 1);
  46. luat_sfud.user_data = sfud_spi_device_flash;
  47. }
  48. int re = sfud_init();
  49. lua_pushboolean(L, re == 0 ? 1 : 0);
  50. return 1;
  51. }
  52. /*
  53. 获取flash设备信息表中的设备总数
  54. @api sfud.getDeviceNum()
  55. @return int 返回设备总数
  56. @usage
  57. log.info("sfud.getDeviceNum",sfud.getDeviceNum())
  58. */
  59. static int luat_sfud_get_device_num(lua_State *L){
  60. int re = sfud_get_device_num();
  61. lua_pushinteger(L, re);
  62. return 1;
  63. }
  64. /*
  65. 通过flash信息表中的索引获取flash设备
  66. @api sfud.getDevice(index)
  67. @int index flash信息表中的索引
  68. @return userdata 成功返回一个数据结构,否则返回nil
  69. @usage
  70. local sfud_device = sfud.getDevice(1)
  71. */
  72. static int luat_sfud_get_device(lua_State *L){
  73. sfud_flash *flash = sfud_get_device(luaL_checkinteger(L, 1));
  74. lua_pushlightuserdata(L, flash);
  75. return 1;
  76. }
  77. /*
  78. 获取flash设备信息表
  79. @api sfud.getDeviceTable()
  80. @return userdata 成功返回一个数据结构,否则返回nil
  81. @usage
  82. local sfud_device = sfud.getDeviceTable()
  83. */
  84. static int luat_sfud_get_device_table(lua_State *L){
  85. sfud_flash *flash = sfud_get_device_table();
  86. lua_pushlightuserdata(L, flash);
  87. return 1;
  88. }
  89. /*
  90. 擦除 Flash 全部数据
  91. @api sfud.chipErase(flash)
  92. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  93. @return int 成功返回0
  94. @usage
  95. sfud.chipErase(flash)
  96. */
  97. static int luat_sfud_chip_erase(lua_State *L){
  98. const sfud_flash *flash = lua_touserdata(L, 1);
  99. sfud_err re = sfud_chip_erase(flash);
  100. lua_pushinteger(L, re);
  101. return 1;
  102. }
  103. /*
  104. 擦除 Flash 全部数据
  105. @api sfud.chip_erase(flash)
  106. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  107. @return int 成功返回0
  108. @usage
  109. sfud.chip_erase(flash)
  110. */
  111. static int luat_sfud_erase(lua_State *L){
  112. const sfud_flash *flash = lua_touserdata(L, 1);
  113. uint32_t addr = luaL_checkinteger(L, 2);
  114. size_t size = luaL_checkinteger(L, 3);
  115. sfud_err re = sfud_erase(flash,addr,size);
  116. lua_pushinteger(L, re);
  117. return 1;
  118. }
  119. /*
  120. 读取 Flash 数据
  121. @api sfud.read(flash, addr, size)
  122. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  123. @int addr 起始地址
  124. @int size 从起始地址开始读取数据的总大小
  125. @return string data 读取到的数据
  126. @usage
  127. log.info("sfud.read",sfud.read(sfud_device,1024,4))
  128. */
  129. static int luat_sfud_read(lua_State *L){
  130. const sfud_flash *flash = lua_touserdata(L, 1);
  131. uint32_t addr = luaL_checkinteger(L, 2);
  132. size_t size = luaL_checkinteger(L, 3);
  133. uint8_t* data = (uint8_t*)luat_heap_malloc(size);
  134. sfud_err re = sfud_read(flash, addr, size,data);
  135. if(re != SFUD_SUCCESS){
  136. size = 0;
  137. LLOGD("sfud_read re %d", re);
  138. }
  139. lua_pushlstring(L, data, size);
  140. luat_heap_free(data);
  141. return 1;
  142. }
  143. /*
  144. 向 Flash 写数据
  145. @api sfud.write(flash, addr, size,data)
  146. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  147. @int addr 起始地址
  148. @int size 从起始地址开始读取数据的总大小
  149. @string data 待写入的数据
  150. @return int 成功返回0
  151. @usage
  152. log.info("sfud.write",sfud.write(sfud_device,1024,"sfud"))
  153. */
  154. static int luat_sfud_write(lua_State *L){
  155. const sfud_flash *flash = lua_touserdata(L, 1);
  156. uint32_t addr = luaL_checkinteger(L, 2);
  157. size_t size = 0;
  158. const char* data = luaL_checklstring(L, 3, &size);
  159. sfud_err re = sfud_write(flash, addr, size,data);
  160. lua_pushinteger(L, re);
  161. return 1;
  162. }
  163. /*
  164. 先擦除再往 Flash 写数据
  165. @api sfud.eraseWrite(flash, addr, size,data)
  166. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  167. @int addr 起始地址
  168. @int size 从起始地址开始读取数据的总大小
  169. @string data 待写入的数据
  170. @return int 成功返回0
  171. @usage
  172. log.info("sfud.eraseWrite",sfud.eraseWrite(sfud_device,1024,"sfud"))
  173. */
  174. static int luat_sfud_erase_write(lua_State *L){
  175. const sfud_flash *flash = lua_touserdata(L, 1);
  176. uint32_t addr = luaL_checkinteger(L, 2);
  177. size_t size = 0;
  178. const char* data = luaL_checklstring(L, 3, &size);
  179. sfud_err re = sfud_erase_write(flash, addr, size,data);
  180. lua_pushinteger(L, re);
  181. return 1;
  182. }
  183. #ifdef LUAT_USE_FS_VFS
  184. #include "luat_fs.h"
  185. #include "lfs.h"
  186. extern lfs_t* flash_lfs_sfud(sfud_flash* flash);
  187. /*
  188. 挂载sfud lfs文件系统
  189. @api sfud.mount(flash, mount_point)
  190. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  191. @string mount_point 挂载目录名
  192. @return bool 成功返回true
  193. @usage
  194. log.info("sfud.mount",sfud.mount(sfud_device,"/sfud"))
  195. log.info("fsstat", fs.fsstat("/"))
  196. log.info("fsstat", fs.fsstat("/sfud"))
  197. */
  198. static int luat_sfud_mount(lua_State *L) {
  199. const sfud_flash *flash = lua_touserdata(L, 1);
  200. const char* mount_point = luaL_checkstring(L, 2);
  201. lfs_t* lfs = flash_lfs_sfud(flash);
  202. if (lfs) {
  203. luat_fs_conf_t conf = {
  204. .busname = (char*)lfs,
  205. .type = "lfs2",
  206. .filesystem = "lfs2",
  207. .mount_point = mount_point,
  208. };
  209. luat_fs_mount(&conf);
  210. lua_pushboolean(L, 1);
  211. }
  212. else {
  213. lua_pushboolean(L, 0);
  214. }
  215. return 1;
  216. }
  217. #endif
  218. #include "rotable.h"
  219. static const rotable_Reg reg_sfud[] =
  220. {
  221. { "init", luat_sfud_init, 0},
  222. { "getDeviceNum", luat_sfud_get_device_num, 0},
  223. { "getDevice", luat_sfud_get_device, 0},
  224. { "getDeviceTable", luat_sfud_get_device_table, 0},
  225. { "erase", luat_sfud_erase, 0},
  226. { "chipErase", luat_sfud_chip_erase, 0},
  227. { "read", luat_sfud_read, 0},
  228. { "write", luat_sfud_write, 0},
  229. { "eraseWrite", luat_sfud_erase_write, 0},
  230. #ifdef LUAT_USE_FS_VFS
  231. { "mount", luat_sfud_mount, 0},
  232. #endif
  233. { NULL, NULL, 0}
  234. };
  235. LUAMOD_API int luaopen_sfud( lua_State *L ) {
  236. luat_newlib(L, reg_sfud);
  237. return 1;
  238. }