luat_base_idf5.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include "luat_base.h"
  2. #include "luat_msgbus.h"
  3. #include "luat_fs.h"
  4. #include "luat_timer.h"
  5. #include <stdlib.h>
  6. #ifdef LUAT_USE_LVGL
  7. #include "lvgl.h"
  8. void luat_lv_fs_init(void);
  9. void lv_bmp_init(void);
  10. void lv_png_init(void);
  11. void lv_split_jpeg_init(void);
  12. #endif
  13. // C2需要强制关闭全部UI组件,否则2M的flash放不下
  14. #if defined(CONFIG_IDF_TARGET_ESP32C2)
  15. #undef LUAT_USE_LCD
  16. #undef LUAT_USE_LVGL
  17. #undef LUAT_USE_EINK
  18. #undef LUAT_USE_U8G2
  19. #undef LUAT_USE_DISP
  20. #undef LUAT_USE_FONTS
  21. #undef LUAT_USE_GTFONT
  22. #undef LUAT_USE_RSA
  23. #endif
  24. LUAMOD_API int luaopen_nimble( lua_State *L );
  25. static const luaL_Reg loadedlibs[] = {
  26. {"_G", luaopen_base}, // _G
  27. {LUA_LOADLIBNAME, luaopen_package}, // require
  28. {LUA_COLIBNAME, luaopen_coroutine}, // coroutine协程库
  29. {LUA_TABLIBNAME, luaopen_table}, // table库,操作table类型的数据结构
  30. {LUA_IOLIBNAME, luaopen_io}, // io库,操作文件
  31. {LUA_OSLIBNAME, luaopen_os}, // os库,已精简
  32. {LUA_STRLIBNAME, luaopen_string}, // string库,字符串操作
  33. {LUA_MATHLIBNAME, luaopen_math}, // math 数值计算
  34. {LUA_UTF8LIBNAME, luaopen_utf8},
  35. {LUA_DBLIBNAME, luaopen_debug}, // debug库,已精简
  36. #ifdef LUAT_USE_DBG
  37. #ifndef LUAT_USE_SHELL
  38. #define LUAT_USE_SHELL
  39. #endif
  40. {"dbg", luaopen_dbg}, // 调试库
  41. #endif
  42. #if defined(LUA_COMPAT_BITLIB)
  43. {LUA_BITLIBNAME, luaopen_bit32}, // 不太可能启用
  44. #endif
  45. // 往下是LuatOS定制的库, 如需精简请仔细测试
  46. //----------------------------------------------------------------------
  47. // 核心支撑库, 不可禁用!!
  48. {"rtos", luaopen_rtos}, // rtos底层库, 核心功能是队列和定时器
  49. {"log", luaopen_log}, // 日志库
  50. {"timer", luaopen_timer}, // 延时库
  51. //-----------------------------------------------------------------------
  52. // 设备驱动类, 可按实际情况删减. 即使最精简的固件, 也强烈建议保留uart库
  53. #ifdef LUAT_USE_UART
  54. {"uart", luaopen_uart}, // 串口操作
  55. #endif
  56. #ifdef LUAT_USE_GPIO
  57. {"gpio", luaopen_gpio}, // GPIO脚的操作
  58. #endif
  59. #ifdef LUAT_USE_I2C
  60. {"i2c", luaopen_i2c}, // I2C操作
  61. #endif
  62. #ifdef LUAT_USE_SPI
  63. {"spi", luaopen_spi}, // SPI操作
  64. #endif
  65. #ifdef LUAT_USE_ADC
  66. {"adc", luaopen_adc}, // ADC模块
  67. #endif
  68. #ifdef LUAT_USE_SDIO
  69. {"sdio", luaopen_sdio}, // SDIO模块
  70. #endif
  71. #ifdef LUAT_USE_PWM
  72. {"pwm", luaopen_pwm}, // PWM模块
  73. #endif
  74. #ifdef LUAT_USE_WDT
  75. {"wdt", luaopen_wdt}, // watchdog模块
  76. #endif
  77. #ifdef LUAT_USE_PM
  78. {"pm", luaopen_pm}, // 电源管理模块
  79. #endif
  80. #ifdef LUAT_USE_MCU
  81. {"mcu", luaopen_mcu}, // MCU特有的一些操作
  82. #endif
  83. #ifdef LUAT_USE_HWTIMER
  84. {"hwtimer", luaopen_hwtimer}, // 硬件定时器
  85. #endif
  86. #ifdef LUAT_USE_RTC
  87. {"rtc", luaopen_rtc}, // 实时时钟
  88. #endif
  89. #ifdef LUAT_USE_OTP
  90. // {"otp", luaopen_otp}, // OTP
  91. #endif
  92. #ifdef LUAT_USE_TOUCHKEY
  93. {"touchkey", luaopen_touchkey}, // OTP
  94. #endif
  95. // {"pin", luaopen_pin}, // pin
  96. //-----------------------------------------------------------------------
  97. // 工具库, 按需选用
  98. #ifdef LUAT_USE_CRYPTO
  99. {"crypto",luaopen_crypto}, // 加密和hash模块
  100. #endif
  101. #ifdef LUAT_USE_CJSON
  102. {"json", luaopen_cjson}, // json的序列化和反序列化
  103. #endif
  104. #ifdef LUAT_USE_ZBUFF
  105. {"zbuff", luaopen_zbuff}, // 像C语言语言操作内存块
  106. #endif
  107. #ifdef LUAT_USE_PACK
  108. {"pack", luaopen_pack}, // pack.pack/pack.unpack
  109. #endif
  110. // {"mqttcore",luaopen_mqttcore}, // MQTT 协议封装
  111. // {"libcoap", luaopen_libcoap}, // 处理COAP消息
  112. #ifdef LUAT_USE_LIBGNSS
  113. {"libgnss", luaopen_libgnss}, // 处理GNSS定位数据
  114. #endif
  115. #ifdef LUAT_USE_FS
  116. {"fs", luaopen_fs}, // 文件系统库,在io库之外再提供一些方法
  117. #endif
  118. #ifdef LUAT_USE_SENSOR
  119. {"sensor", luaopen_sensor}, // 传感器库,支持DS18B20
  120. #endif
  121. #ifdef LUAT_USE_SFUD
  122. {"sfud", luaopen_sfud}, // sfud
  123. #endif
  124. #ifdef LUAT_USE_SFD
  125. {"sfd", luaopen_sfd}, // sfud
  126. #endif
  127. #ifdef LUAT_USE_DISP
  128. {"disp", luaopen_disp}, // OLED显示模块,支持SSD1306
  129. #endif
  130. #ifdef LUAT_USE_U8G2
  131. {"u8g2", luaopen_u8g2}, // u8g2
  132. #endif
  133. #ifdef LUAT_USE_EINK
  134. {"eink", luaopen_eink}, // 电子墨水屏,试验阶段
  135. #endif
  136. #ifdef LUAT_USE_LVGL
  137. // #ifndef LUAT_USE_LCD
  138. // #define LUAT_USE_LCD
  139. // #endif
  140. {"lvgl", luaopen_lvgl},
  141. #endif
  142. #ifdef LUAT_USE_LCD
  143. {"lcd", luaopen_lcd},
  144. #endif
  145. #ifdef LUAT_USE_STATEM
  146. {"statem", luaopen_statem},
  147. #endif
  148. #ifdef LUAT_USE_GTFONT
  149. {"gtfont", luaopen_gtfont},
  150. #endif
  151. #ifdef LUAT_USE_FSKV
  152. {"fskv", luaopen_fskv},
  153. // 启用FSKV的时候,自动禁用FDB
  154. #ifdef LUAT_USE_FDB
  155. #undef LUAT_USE_FDB
  156. #endif
  157. #endif
  158. #ifdef LUAT_USE_FDB
  159. {"fdb", luaopen_fdb},
  160. #endif
  161. #ifdef LUAT_USE_VMX
  162. {"vmx", luaopen_vmx},
  163. #endif
  164. #ifdef LUAT_USE_COREMARK
  165. {"coremark", luaopen_coremark},
  166. #endif
  167. #ifdef LUAT_USE_FONTS
  168. {"fonts", luaopen_fonts},
  169. #endif
  170. #ifdef LUAT_USE_ZLIB
  171. {"zlib", luaopen_zlib},
  172. #endif
  173. #ifdef LUAT_USE_MLX90640
  174. {"mlx90640", luaopen_mlx90640},
  175. #endif
  176. #ifdef LUAT_USE_IR
  177. {"ir", luaopen_ir},
  178. #endif
  179. #ifdef LUAT_USE_YMODEM
  180. {"ymodem", luaopen_ymodem},
  181. #endif
  182. #ifdef LUAT_USE_I2S
  183. {"i2s", luaopen_i2s},
  184. #endif
  185. #ifdef LUAT_USE_LORA
  186. {"lora", luaopen_lora},
  187. #endif
  188. #ifdef LUAT_USE_LORA2
  189. {"lora2", luaopen_lora2},
  190. #endif
  191. #ifdef LUAT_USE_MINIZ
  192. {"miniz", luaopen_miniz},
  193. #endif
  194. #ifdef LUAT_USE_PROTOBUF
  195. {"protobuf", luaopen_protobuf},
  196. #endif
  197. #ifdef LUAT_USE_IOTAUTH
  198. {"iotauth", luaopen_iotauth},
  199. #endif
  200. #ifdef LUAT_USE_WLAN
  201. {"wlan", luaopen_wlan},
  202. #endif
  203. #ifdef LUAT_USE_NETWORK
  204. {"http", luaopen_http},
  205. {"mqtt", luaopen_mqtt},
  206. {"socket", luaopen_socket_adapter},
  207. {"websocket", luaopen_websocket},
  208. #ifdef LUAT_USE_FTP
  209. {"ftp", luaopen_ftp},
  210. #endif
  211. #ifdef LUAT_USE_W5500
  212. {"w5500", luaopen_w5500},
  213. #endif
  214. #endif
  215. #ifdef LUAT_USE_HTTPSRV
  216. {"httpsrv", luaopen_httpsrv},
  217. #endif
  218. #ifdef LUAT_USE_NIMBLE
  219. #if CONFIG_BT_ENABLED
  220. {"nimble", luaopen_nimble},
  221. #endif
  222. #endif
  223. #ifdef LUAT_USE_RSA
  224. {"rsa", luaopen_rsa},
  225. #endif
  226. #ifdef LUAT_USE_FATFS
  227. {"fatfs", luaopen_fatfs},
  228. #endif
  229. #ifdef LUAT_USE_MAX30102
  230. {"max30102", luaopen_max30102},
  231. #endif
  232. #ifdef LUAT_USE_ICONV
  233. {"iconv", luaopen_iconv},
  234. #endif
  235. #ifdef LUAT_USE_BIT64
  236. {"bit64", luaopen_bit64},
  237. #endif
  238. #ifdef LUAT_USE_GMSSL
  239. {"gmssl", luaopen_gmssl},
  240. #endif
  241. #ifdef LUAT_USE_REPL
  242. {"repl", luaopen_repl},
  243. #endif
  244. #ifdef LUAT_USE_XXTEA
  245. {"xxtea", luaopen_xxtea},
  246. #endif
  247. #ifdef LUAT_USE_ULWIP
  248. {"ulwip", luaopen_ulwip},
  249. #endif
  250. {NULL, NULL}
  251. };
  252. // 按不同的rtconfig加载不同的库函数
  253. void luat_openlibs(lua_State *L) {
  254. // 初始化队列服务
  255. luat_msgbus_init();
  256. //print_list_mem("done>luat_msgbus_init");
  257. // 加载系统库
  258. const luaL_Reg *lib;
  259. /* "require" functions from 'loadedlibs' and set results to global table */
  260. for (lib = loadedlibs; lib->func; lib++) {
  261. luaL_requiref(L, lib->name, lib->func, 1);
  262. lua_pop(L, 1); /* remove lib */
  263. //extern void print_list_mem(const char* name);
  264. //print_list_mem(lib->name);
  265. }
  266. }
  267. #include "esp_system.h"
  268. void luat_os_reboot(int code){
  269. esp_restart();
  270. }
  271. const char* luat_os_bsp(void) {
  272. #if defined(CONFIG_IDF_TARGET_ESP32)
  273. return "ESP32";
  274. #elif defined(CONFIG_IDF_TARGET_ESP32C2)
  275. return "ESP32C2";
  276. #elif defined(CONFIG_IDF_TARGET_ESP32C3)
  277. return "ESP32C3";
  278. #elif defined(CONFIG_IDF_TARGET_ESP32S2)
  279. return "ESP32S2";
  280. #elif defined(CONFIG_IDF_TARGET_ESP32S3)
  281. return "ESP32S3";
  282. #else
  283. return "ESP32-UNKOWN";
  284. #endif
  285. }
  286. /** 设备进入待机模式 */
  287. void luat_os_standy(int timeout) {
  288. return; // nop
  289. }
  290. void luat_ota_reboot(int timeout_ms) {
  291. if (timeout_ms > 0)
  292. luat_timer_mdelay(timeout_ms);
  293. esp_restart();
  294. }
  295. #include "freertos/FreeRTOS.h"
  296. #include "freertos/task.h"
  297. #include "esp_rom_sys.h"
  298. static uint8_t cri = 0;
  299. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S3)
  300. portMUX_TYPE lock;
  301. #endif
  302. void luat_os_entry_cri(void) {
  303. if (cri == 0) {
  304. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S3)
  305. vPortExitCritical(&lock);
  306. #else
  307. vPortExitCritical();
  308. #endif
  309. cri = 1;
  310. }
  311. }
  312. void luat_os_exit_cri(void) {
  313. if (cri == 1) {
  314. cri = 0;
  315. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S3)
  316. vPortExitCritical(&lock);
  317. #else
  318. vPortExitCritical();
  319. #endif
  320. }
  321. }
  322. void luat_timer_us_delay(size_t us) {
  323. esp_rom_delay_us(us);
  324. }