luat_lib_sfud.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. lua_pushlightuserdata(L, flash);
  78. return 1;
  79. }
  80. /*
  81. 获取flash设备信息表
  82. @api sfud.getDeviceTable()
  83. @return userdata 成功返回一个数据结构,否则返回nil
  84. @usage
  85. local sfud_device = sfud.getDeviceTable()
  86. */
  87. static int luat_sfud_get_device_table(lua_State *L){
  88. const sfud_flash *flash = sfud_get_device_table();
  89. lua_pushlightuserdata(L, flash);
  90. return 1;
  91. }
  92. /*
  93. 擦除 Flash 全部数据
  94. @api sfud.chipErase(flash)
  95. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  96. @return int 成功返回0
  97. @usage
  98. sfud.chipErase(flash)
  99. */
  100. static int luat_sfud_chip_erase(lua_State *L){
  101. const sfud_flash *flash = lua_touserdata(L, 1);
  102. sfud_err re = sfud_chip_erase(flash);
  103. lua_pushinteger(L, re);
  104. return 1;
  105. }
  106. /*
  107. 擦除 Flash 指定地址指定大小,按照flash block大小进行擦除
  108. @api sfud.erase(flash,add,size)
  109. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  110. @number add 擦除地址
  111. @number size 擦除大小
  112. @return int 成功返回0
  113. @usage
  114. sfud.erase(flash,add,size)
  115. */
  116. static int luat_sfud_erase(lua_State *L){
  117. const sfud_flash *flash = lua_touserdata(L, 1);
  118. uint32_t addr = luaL_checkinteger(L, 2);
  119. size_t size = luaL_checkinteger(L, 3);
  120. sfud_err re = sfud_erase(flash,addr,size);
  121. lua_pushinteger(L, re);
  122. return 1;
  123. }
  124. /*
  125. 读取 Flash 数据
  126. @api sfud.read(flash, addr, size)
  127. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  128. @int addr 起始地址
  129. @int size 从起始地址开始读取数据的总大小
  130. @return string data 读取到的数据
  131. @usage
  132. log.info("sfud.read",sfud.read(sfud_device,1024,4))
  133. */
  134. static int luat_sfud_read(lua_State *L){
  135. const sfud_flash *flash = lua_touserdata(L, 1);
  136. uint32_t addr = luaL_checkinteger(L, 2);
  137. size_t size = luaL_checkinteger(L, 3);
  138. uint8_t* data = (uint8_t*)luat_heap_malloc(size);
  139. sfud_err re = sfud_read(flash, addr, size, data);
  140. if(re != SFUD_SUCCESS){
  141. size = 0;
  142. LLOGD("sfud_read re %d", re);
  143. }
  144. lua_pushlstring(L, (const char*)data, size);
  145. luat_heap_free(data);
  146. return 1;
  147. }
  148. /*
  149. 向 Flash 写数据
  150. @api sfud.write(flash, addr,data)
  151. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  152. @int addr 起始地址
  153. @string data 待写入的数据
  154. @return int 成功返回0
  155. @usage
  156. log.info("sfud.write",sfud.write(sfud_device,1024,"sfud"))
  157. */
  158. static int luat_sfud_write(lua_State *L){
  159. const sfud_flash *flash = lua_touserdata(L, 1);
  160. uint32_t addr = luaL_checkinteger(L, 2);
  161. size_t size = 0;
  162. const char* data = luaL_checklstring(L, 3, &size);
  163. sfud_err re = sfud_write(flash, addr, size, (const uint8_t*)data);
  164. lua_pushinteger(L, re);
  165. return 1;
  166. }
  167. /*
  168. 先擦除再往 Flash 写数据
  169. @api sfud.eraseWrite(flash, addr,data)
  170. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  171. @int addr 起始地址
  172. @string data 待写入的数据
  173. @return int 成功返回0
  174. @usage
  175. log.info("sfud.eraseWrite",sfud.eraseWrite(sfud_device,1024,"sfud"))
  176. */
  177. static int luat_sfud_erase_write(lua_State *L){
  178. const sfud_flash *flash = lua_touserdata(L, 1);
  179. uint32_t addr = luaL_checkinteger(L, 2);
  180. size_t size = 0;
  181. const char* data = luaL_checklstring(L, 3, &size);
  182. sfud_err re = sfud_erase_write(flash, addr, size, (const uint8_t*)data);
  183. lua_pushinteger(L, re);
  184. return 1;
  185. }
  186. /*
  187. 获取 Flash 容量和page大小
  188. @api sfud.getInfo(flash)
  189. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  190. @return int Flash 容量
  191. @return int page 页大小
  192. @usage
  193. log.info("sfud.getInfo",sfud.getInfo(sfud_device))
  194. */
  195. static int luat_sfud_get_info(lua_State *L){
  196. const sfud_flash *flash = lua_touserdata(L, 1);
  197. uint32_t capacity = 0;
  198. uint32_t page = 0;
  199. capacity = flash->chip.capacity;
  200. page = flash->chip.erase_gran;
  201. lua_pushinteger(L, capacity);
  202. lua_pushinteger(L, page);
  203. return 2;
  204. }
  205. #ifdef LUAT_USE_FS_VFS
  206. #include "luat_fs.h"
  207. #include "lfs.h"
  208. extern lfs_t* flash_lfs_sfud(sfud_flash* flash, size_t offset, size_t maxsize);
  209. /*
  210. 挂载sfud lfs文件系统
  211. @api sfud.mount(flash, mount_point, offset, maxsize)
  212. @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
  213. @string mount_point 挂载目录名
  214. @int 起始偏移量,默认0
  215. @int 总大小, 默认是整个flash
  216. @return bool 成功返回true
  217. @usage
  218. log.info("sfud.mount",sfud.mount(sfud_device,"/sfud"))
  219. log.info("fsstat", fs.fsstat("/"))
  220. log.info("fsstat", fs.fsstat("/sfud"))
  221. */
  222. static int luat_sfud_mount(lua_State *L) {
  223. const sfud_flash *flash = lua_touserdata(L, 1);
  224. const char* mount_point = luaL_checkstring(L, 2);
  225. size_t offset = luaL_optinteger(L, 3, 0);
  226. size_t maxsize = luaL_optinteger(L, 4, 0);
  227. lfs_t* lfs = flash_lfs_sfud(flash, offset, maxsize);
  228. if (lfs) {
  229. luat_fs_conf_t conf = {
  230. .busname = (char*)lfs,
  231. .type = "lfs2",
  232. .filesystem = "lfs2",
  233. .mount_point = mount_point,
  234. };
  235. int ret = luat_fs_mount(&conf);
  236. LLOGD("vfs mount %s ret %d", mount_point, ret);
  237. lua_pushboolean(L, 1);
  238. }
  239. else {
  240. lua_pushboolean(L, 0);
  241. }
  242. return 1;
  243. }
  244. #endif
  245. #include "rotable2.h"
  246. static const rotable_Reg_t reg_sfud[] =
  247. {
  248. { "init", ROREG_FUNC(luat_sfud_init)},
  249. { "getDeviceNum", ROREG_FUNC(luat_sfud_get_device_num)},
  250. { "getDevice", ROREG_FUNC(luat_sfud_get_device)},
  251. { "getDeviceTable", ROREG_FUNC(luat_sfud_get_device_table)},
  252. { "erase", ROREG_FUNC(luat_sfud_erase)},
  253. { "chipErase", ROREG_FUNC(luat_sfud_chip_erase)},
  254. { "read", ROREG_FUNC(luat_sfud_read)},
  255. { "write", ROREG_FUNC(luat_sfud_write)},
  256. { "eraseWrite", ROREG_FUNC(luat_sfud_erase_write)},
  257. { "getInfo", ROREG_FUNC(luat_sfud_get_info)},
  258. #ifdef LUAT_USE_FS_VFS
  259. { "mount", ROREG_FUNC(luat_sfud_mount)},
  260. #endif
  261. { NULL, ROREG_INT(0)}
  262. };
  263. LUAMOD_API int luaopen_sfud( lua_State *L ) {
  264. luat_newlib2(L, reg_sfud);
  265. return 1;
  266. }