luat_lib_little_flash.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. @module little_flash
  3. @summary flash驱动 软件包(同时支持驱动nor flash和nand flash设备)
  4. @version 1.0
  5. @date 2024.05.11
  6. @demo little_flash
  7. @tag LUAT_USE_LITTLE_FLASH
  8. */
  9. #include "luat_base.h"
  10. #include "luat_spi.h"
  11. #include "luat_mem.h"
  12. #define LUAT_LOG_TAG "little_flash"
  13. #include "luat_log.h"
  14. #include "little_flash.h"
  15. /*
  16. 初始化 little_flash
  17. @api lf.init(spi_device)
  18. @userdata spi_device
  19. @return userdata 成功返回一个数据结构,否则返回nil
  20. @usage
  21. --spi_device
  22. spi_device = spi.deviceSetup(0,17,0,0,8,2000000,spi.MSB,1,0)
  23. log.info("lf.init",lf.init(spi_device))
  24. */
  25. static int luat_little_flash_init(lua_State *L){
  26. luat_spi_device_t* little_flash_spi_device = NULL;
  27. little_flash_t* lf_flash = NULL;
  28. if (lua_type(L, 1) == LUA_TUSERDATA){
  29. little_flash_spi_device = (luat_spi_device_t*)lua_touserdata(L, 1);
  30. if (little_flash_spi_device->spi_config.mode == 1){
  31. LLOGW("flash need half mode, spi_device mode is full mode, change to half mode");
  32. little_flash_spi_device->spi_config.mode = 0;
  33. }
  34. lf_flash = luat_heap_malloc(sizeof(little_flash_t));
  35. memset(lf_flash, 0, sizeof(little_flash_t));
  36. lf_flash->spi.user_data = little_flash_spi_device;
  37. }else{
  38. LLOGW("little_flash init spi_device is nil");
  39. return 0;
  40. }
  41. // little_flash_init();
  42. lf_err_t re = little_flash_device_init(lf_flash);
  43. if (re == LF_ERR_OK){
  44. lua_pushlightuserdata(L, lf_flash);
  45. return 1;
  46. }
  47. luat_heap_free(lf_flash);
  48. return 0;
  49. }
  50. /*
  51. 擦除 Flash 指定地址指定大小,按照flash block大小进行擦除
  52. @api lf.erase(flash,add,size)
  53. @userdata flash Flash 设备对象 lf.init()返回的数据结构
  54. @number add 擦除地址
  55. @number size 擦除大小
  56. @return bool 成功返回true
  57. @usage
  58. lf.erase(flash,add,size)
  59. */
  60. static int luat_little_flash_erase(lua_State *L){
  61. little_flash_t *flash = lua_touserdata(L, 1);
  62. if (flash == NULL) {
  63. LLOGE("little_flash mount flash is nil");
  64. return 0;
  65. }
  66. uint32_t addr = luaL_checkinteger(L, 2);
  67. size_t size = luaL_checkinteger(L, 3);
  68. lf_err_t ret = little_flash_erase(flash,addr,size);
  69. lua_pushboolean(L, ret ? 0 : 1);
  70. return 1;
  71. }
  72. /*
  73. 擦除 Flash 全部数据
  74. @api lf.chipErase(flash)
  75. @userdata flash Flash 设备对象 lf.init()返回的数据结构
  76. @return bool 成功返回true
  77. @usage
  78. lf.chipErase(flash)
  79. */
  80. static int luat_little_flash_chip_erase(lua_State *L){
  81. little_flash_t *flash = lua_touserdata(L, 1);
  82. if (flash == NULL) {
  83. LLOGE("little_flash mount flash is nil");
  84. return 0;
  85. }
  86. lf_err_t ret = little_flash_chip_erase(flash);
  87. lua_pushboolean(L, ret ? 0 : 1);
  88. return 1;
  89. }
  90. /*
  91. 读取 Flash 数据
  92. @api lf.read(flash, addr, size)
  93. @userdata flash Flash 设备对象 lf.init()返回的数据结构
  94. @int addr 起始地址
  95. @int size 从起始地址开始读取数据的总大小
  96. @return string data 读取到的数据
  97. @usage
  98. log.info("lf.read",lf.read(lf_device,1024,4))
  99. */
  100. static int luat_little_flash_read(lua_State *L){
  101. little_flash_t *flash = lua_touserdata(L, 1);
  102. if (flash == NULL) {
  103. LLOGE("little_flash mount flash is nil");
  104. return 0;
  105. }
  106. uint32_t addr = luaL_checkinteger(L, 2);
  107. size_t size = luaL_checkinteger(L, 3);
  108. uint8_t* data = (uint8_t*)luat_heap_malloc(size);
  109. lf_err_t ret = little_flash_read(flash, addr, data, size);
  110. if(ret != 0){
  111. size = 0;
  112. LLOGD("lf read ret %d", ret);
  113. }
  114. lua_pushlstring(L, (const char*)data, size);
  115. luat_heap_free(data);
  116. return 1;
  117. }
  118. /*
  119. 向 Flash 写数据
  120. @api lf.write(flash, addr,data)
  121. @userdata flash Flash 设备对象 lf.init()返回的数据结构
  122. @int addr 起始地址
  123. @string data 待写入的数据
  124. @return bool 成功返回true
  125. @usage
  126. log.info("lf.write",lf.write(lf_device,1024,"lf"))
  127. */
  128. static int luat_little_flash_write(lua_State *L){
  129. little_flash_t *flash = lua_touserdata(L, 1);
  130. if (flash == NULL) {
  131. LLOGE("little_flash mount flash is nil");
  132. return 0;
  133. }
  134. uint32_t addr = luaL_checkinteger(L, 2);
  135. size_t size = 0;
  136. const char* data = luaL_checklstring(L, 3, &size);
  137. lf_err_t ret = little_flash_write(flash, addr, (const uint8_t*)data, size);
  138. lua_pushboolean(L, ret ? 0 : 1);
  139. return 1;
  140. }
  141. /*
  142. 先擦除再往 Flash 写数据
  143. @api lf.eraseWrite(flash, addr,data)
  144. @userdata flash Flash 设备对象 lf.init()返回的数据结构
  145. @int addr 起始地址
  146. @string data 待写入的数据
  147. @return bool 成功返回true
  148. @usage
  149. log.info("lf.eraseWrite",lf.eraseWrite(lf_device,1024,"lf"))
  150. */
  151. static int luat_little_flash_erase_write(lua_State *L){
  152. little_flash_t *flash = lua_touserdata(L, 1);
  153. if (flash == NULL) {
  154. LLOGE("little_flash mount flash is nil");
  155. return 0;
  156. }
  157. uint32_t addr = luaL_checkinteger(L, 2);
  158. size_t size = 0;
  159. const char* data = luaL_checklstring(L, 3, &size);
  160. lf_err_t ret = little_flash_erase_write(flash, addr, (const uint8_t*)data, size);
  161. lua_pushboolean(L, ret ? 0 : 1);
  162. return 1;
  163. }
  164. /*
  165. 获取 Flash 容量和page大小
  166. @api lf.getInfo(flash)
  167. @userdata flash Flash 设备对象 lf.init()返回的数据结构
  168. @return int Flash 容量
  169. @return int page 页大小
  170. @usage
  171. log.info("lf.getInfo",lf.getInfo(lf_device))
  172. */
  173. static int luat_little_flash_get_info(lua_State *L){
  174. little_flash_t *flash = lua_touserdata(L, 1);
  175. if (flash == NULL) {
  176. LLOGE("little_flash mount flash is nil");
  177. return 0;
  178. }
  179. uint32_t capacity = 0;
  180. uint32_t page = 0;
  181. capacity = flash->chip_info.capacity;
  182. page = flash->chip_info.prog_size;
  183. lua_pushinteger(L, capacity);
  184. lua_pushinteger(L, page);
  185. return 2;
  186. }
  187. #ifdef LUAT_USE_FS_VFS
  188. #include "luat_fs.h"
  189. #include "lfs.h"
  190. extern lfs_t* flash_lfs_lf(little_flash_t* flash, size_t offset, size_t maxsize);
  191. /*
  192. 挂载 little_flash lfs文件系统
  193. @api lf.mount(flash, mount_point, offset, maxsize)
  194. @userdata flash Flash 设备对象 lf.init()返回的数据结构
  195. @string mount_point 挂载目录名
  196. @int 起始偏移量,默认0
  197. @int 总大小, 默认是整个flash
  198. @return bool 成功返回true
  199. @usage
  200. log.info("lf.mount",lf.mount(little_flash_device,"/little_flash"))
  201. */
  202. static int luat_little_flash_mount(lua_State *L) {
  203. little_flash_t *flash = lua_touserdata(L, 1);
  204. if (flash == NULL) {
  205. LLOGE("little_flash mount flash is nil");
  206. return 0;
  207. }
  208. const char* mount_point = luaL_checkstring(L, 2);
  209. size_t offset = luaL_optinteger(L, 3, 0);
  210. size_t maxsize = luaL_optinteger(L, 4, 0);
  211. lfs_t* lfs = flash_lfs_lf(flash, offset, maxsize);
  212. if (lfs) {
  213. luat_fs_conf_t conf = {
  214. .busname = (char*)lfs,
  215. .type = "lfs2",
  216. .filesystem = "lfs2",
  217. .mount_point = mount_point,
  218. };
  219. int ret = luat_fs_mount(&conf);
  220. LLOGD("vfs mount %s ret %d", mount_point, ret);
  221. lua_pushboolean(L, 1);
  222. }
  223. else {
  224. lua_pushboolean(L, 0);
  225. }
  226. return 1;
  227. }
  228. #endif
  229. #include "rotable2.h"
  230. static const rotable_Reg_t reg_little_flash[] =
  231. {
  232. { "init", ROREG_FUNC(luat_little_flash_init)},
  233. { "erase", ROREG_FUNC(luat_little_flash_erase)},
  234. { "chipErase", ROREG_FUNC(luat_little_flash_chip_erase)},
  235. { "read", ROREG_FUNC(luat_little_flash_read)},
  236. { "write", ROREG_FUNC(luat_little_flash_write)},
  237. { "eraseWrite", ROREG_FUNC(luat_little_flash_erase_write)},
  238. { "getInfo", ROREG_FUNC(luat_little_flash_get_info)},
  239. #ifdef LUAT_USE_FS_VFS
  240. { "mount", ROREG_FUNC(luat_little_flash_mount)},
  241. #endif
  242. { NULL, ROREG_INT(0)}
  243. };
  244. LUAMOD_API int luaopen_little_flash( lua_State *L ) {
  245. luat_newlib2(L, reg_little_flash);
  246. return 1;
  247. }