luat_lib_sfud.c 8.8 KB

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