luat_lib_sfud.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_spi_t sfud_spi_flash;
  13. /*
  14. 初始化sfud
  15. @api sfud.init(spi_id, spi_cs, spi_bandrate)
  16. @int spi_id SPI的ID
  17. @int spi_cs SPI的片选
  18. @int spi_bandrate SPI的频率
  19. @return bool 成功返回true,否则返回false
  20. @usage
  21. log.info("sfud.init",sfud.init(0,20,20 * 1000 * 1000))
  22. */
  23. static int l_sfud_init(lua_State *L){
  24. sfud_spi_flash.id = luaL_checkinteger(L, 1);
  25. sfud_spi_flash.cs = luaL_checkinteger(L, 2);
  26. sfud_spi_flash.bandrate = luaL_checkinteger(L, 3);
  27. // sfud_spi_flash.id = 0;
  28. // sfud_spi_flash.cs = 20; // 默认无
  29. sfud_spi_flash.CPHA = 1; // CPHA0
  30. sfud_spi_flash.CPOL = 1; // CPOL0
  31. sfud_spi_flash.dataw = 8; // 8bit
  32. // sfud_spi_flash.bandrate = 20 * 1000 * 1000; // 2000000U
  33. sfud_spi_flash.bit_dict = 1; // MSB=1, LSB=0
  34. sfud_spi_flash.master = 1; // master=1,slave=0
  35. sfud_spi_flash.mode = 1; // FULL=1, half=0
  36. luat_spi_setup(&sfud_spi_flash);
  37. int re = sfud_init();
  38. lua_pushboolean(L, re == 0 ? 1 : 0);
  39. return 1;
  40. }
  41. /*
  42. 获取flash设备信息表中的设备总数
  43. @api sfud.get_device_num()
  44. @return int 返回设备总数
  45. @usage
  46. log.info("sfud.get_device_num",sfud.get_device_num())
  47. */
  48. static int l_sfud_get_device_num(lua_State *L){
  49. int re = sfud_get_device_num();
  50. lua_pushinteger(L, re);
  51. return 1;
  52. }
  53. /*
  54. 通过flash信息表中的索引获取flash设备
  55. @api sfud.get_device(index)
  56. @int index flash信息表中的索引
  57. @return userdata 成功返回一个数据结构,否则返回nil
  58. @usage
  59. local sfud_device = sfud.get_device(1)
  60. */
  61. static int l_sfud_get_device(lua_State *L){
  62. sfud_flash *flash = sfud_get_device(luaL_checkinteger(L, 1));
  63. lua_pushlightuserdata(L, flash);
  64. return 1;
  65. }
  66. /*
  67. 获取flash设备信息表
  68. @api sfud.get_device_table()
  69. @return userdata 成功返回一个数据结构,否则返回nil
  70. @usage
  71. local sfud_device = sfud.get_device_table()
  72. */
  73. static int l_sfud_get_device_table(lua_State *L){
  74. sfud_flash *flash = sfud_get_device_table();
  75. lua_pushlightuserdata(L, flash);
  76. return 1;
  77. }
  78. /*
  79. 擦除 Flash 全部数据
  80. @api sfud.chip_erase(flash)
  81. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  82. @return int 成功返回0
  83. @usage
  84. sfud.chip_erase(flash)
  85. */
  86. static int l_sfud_chip_erase(lua_State *L){
  87. const sfud_flash *flash = lua_touserdata(L, 1);
  88. sfud_err re = sfud_chip_erase(flash);
  89. lua_pushinteger(L, re);
  90. return 1;
  91. }
  92. /*
  93. 擦除 Flash 全部数据
  94. @api sfud.chip_erase(flash)
  95. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  96. @return int 成功返回0
  97. @usage
  98. sfud.chip_erase(flash)
  99. */
  100. static int l_sfud_erase(lua_State *L){
  101. const sfud_flash *flash = lua_touserdata(L, 1);
  102. uint32_t addr = luaL_checkinteger(L, 2);
  103. size_t size = luaL_checkinteger(L, 3);
  104. sfud_err re = sfud_erase(flash,addr,size);
  105. lua_pushinteger(L, re);
  106. return 1;
  107. }
  108. /*
  109. 读取 Flash 数据
  110. @api sfud.read(flash, addr, size)
  111. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  112. @int addr 起始地址
  113. @int size 从起始地址开始读取数据的总大小
  114. @return string data 读取到的数据
  115. @usage
  116. log.info("sfud.read",sfud.read(sfud_device,1024,4))
  117. */
  118. static int l_sfud_read(lua_State *L){
  119. const sfud_flash *flash = lua_touserdata(L, 1);
  120. uint32_t addr = luaL_checkinteger(L, 2);
  121. size_t size = luaL_checkinteger(L, 3);
  122. uint8_t* data = (uint8_t*)luat_heap_malloc(size);
  123. sfud_err re = sfud_read(flash, addr, size,data);
  124. if(re != SFUD_SUCCESS){
  125. size = 0;
  126. LLOGD("sfud_read re %d", re);
  127. }
  128. lua_pushlstring(L, data, size);
  129. luat_heap_free(data);
  130. return 1;
  131. }
  132. /*
  133. 向 Flash 写数据
  134. @api sfud.write(flash, addr, size,data)
  135. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  136. @int addr 起始地址
  137. @int size 从起始地址开始读取数据的总大小
  138. @string data 待写入的数据
  139. @return int 成功返回0
  140. @usage
  141. log.info("sfud.write",sfud.write(sfud_device,1024,"sfud"))
  142. */
  143. static int l_sfud_write(lua_State *L){
  144. const sfud_flash *flash = lua_touserdata(L, 1);
  145. uint32_t addr = luaL_checkinteger(L, 2);
  146. size_t size = 0;
  147. const char* data = luaL_checklstring(L, 3, &size);
  148. sfud_err re = sfud_write(flash, addr, size,data);
  149. lua_pushinteger(L, re);
  150. return 1;
  151. }
  152. /*
  153. 先擦除再往 Flash 写数据
  154. @api sfud.erase_write(flash, addr, size,data)
  155. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  156. @int addr 起始地址
  157. @int size 从起始地址开始读取数据的总大小
  158. @string data 待写入的数据
  159. @return int 成功返回0
  160. @usage
  161. log.info("sfud.erase_write",sfud.erase_write(sfud_device,1024,"sfud"))
  162. */
  163. static int l_sfud_erase_write(lua_State *L){
  164. const sfud_flash *flash = lua_touserdata(L, 1);
  165. uint32_t addr = luaL_checkinteger(L, 2);
  166. size_t size = 0;
  167. const char* data = luaL_checklstring(L, 3, &size);
  168. sfud_err re = sfud_erase_write(flash, addr, size,data);
  169. lua_pushinteger(L, re);
  170. return 1;
  171. }
  172. #ifdef LUAT_USE_FS_VFS
  173. #include "luat_fs.h"
  174. #include "lfs.h"
  175. extern lfs_t* flash_lfs_sfud(sfud_flash* flash);
  176. static int l_sfud_mount(lua_State *L) {
  177. const sfud_flash *flash = lua_touserdata(L, 1);
  178. const char* mount_point = luaL_checkstring(L, 2);
  179. lfs_t* lfs = flash_lfs_sfud(flash);
  180. if (lfs) {
  181. luat_fs_conf_t conf = {
  182. .busname = (char*)lfs,
  183. .type = "lfs2",
  184. .filesystem = "lfs2",
  185. .mount_point = mount_point,
  186. };
  187. luat_fs_mount(&conf);
  188. lua_pushboolean(L, 1);
  189. }
  190. else {
  191. lua_pushboolean(L, 0);
  192. }
  193. return 1;
  194. }
  195. #endif
  196. #include "rotable.h"
  197. static const rotable_Reg reg_sfud[] =
  198. {
  199. { "init", l_sfud_init, 0},
  200. { "get_device_num", l_sfud_get_device_num, 0},
  201. { "get_device", l_sfud_get_device, 0},
  202. { "get_device_table", l_sfud_get_device_table, 0},
  203. { "erase", l_sfud_erase, 0},
  204. { "chip_erase", l_sfud_chip_erase, 0},
  205. { "read", l_sfud_read, 0},
  206. { "write", l_sfud_write, 0},
  207. { "erase_write", l_sfud_erase_write, 0},
  208. #ifdef LUAT_USE_FS_VFS
  209. { "mount", l_sfud_mount, 0},
  210. #endif
  211. { NULL, NULL, 0}
  212. };
  213. LUAMOD_API int luaopen_sfud( lua_State *L ) {
  214. luat_newlib(L, reg_sfud);
  215. return 1;
  216. }