luat_lib_sfud.c 9.0 KB

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