luat_lib_sfud.c 7.5 KB

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