luat_base_air105.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "luat_base.h"
  22. #include "luat_msgbus.h"
  23. #include "luat_fs.h"
  24. #include "luat_timer.h"
  25. #include "luat_lcd.h"
  26. #include "time.h"
  27. #include <stdlib.h>
  28. #include "app_interface.h"
  29. #define LUAT_LOG_TAG "base"
  30. #include "luat_log.h"
  31. #ifdef LUAT_USE_LVGL
  32. #include "lvgl.h"
  33. void luat_lv_fs_init(void);
  34. void lv_bmp_init(void);
  35. void lv_png_init(void);
  36. void lv_split_jpeg_init(void);
  37. #endif
  38. LUAMOD_API int luaopen_usbapp( lua_State *L );
  39. static const luaL_Reg loadedlibs[] = {
  40. {"_G", luaopen_base}, // _G
  41. {LUA_LOADLIBNAME, luaopen_package}, // require
  42. {LUA_COLIBNAME, luaopen_coroutine}, // coroutine协程库
  43. {LUA_TABLIBNAME, luaopen_table}, // table库,操作table类型的数据结构
  44. {LUA_IOLIBNAME, luaopen_io}, // io库,操作文件
  45. {LUA_OSLIBNAME, luaopen_os}, // os库,已精简
  46. {LUA_STRLIBNAME, luaopen_string}, // string库,字符串操作
  47. {LUA_MATHLIBNAME, luaopen_math}, // math 数值计算
  48. {LUA_UTF8LIBNAME, luaopen_utf8},
  49. {LUA_DBLIBNAME, luaopen_debug}, // debug库,已精简
  50. #ifdef LUAT_USE_DBG
  51. #ifndef LUAT_USE_SHELL
  52. #define LUAT_USE_SHELL
  53. #endif
  54. {"dbg", luaopen_dbg}, // 调试库
  55. #endif
  56. #if defined(LUA_COMPAT_BITLIB)
  57. {LUA_BITLIBNAME, luaopen_bit32}, // 不太可能启用
  58. #endif
  59. // 往下是LuatOS定制的库, 如需精简请仔细测试
  60. //----------------------------------------------------------------------
  61. // 核心支撑库, 不可禁用!!
  62. {"rtos", luaopen_rtos}, // rtos底层库, 核心功能是队列和定时器
  63. {"log", luaopen_log}, // 日志库
  64. {"timer", luaopen_timer}, // 延时库
  65. //-----------------------------------------------------------------------
  66. // 设备驱动类, 可按实际情况删减. 即使最精简的固件, 也强烈建议保留uart库
  67. #ifdef LUAT_USE_UART
  68. {"uart", luaopen_uart}, // 串口操作
  69. #endif
  70. #ifdef LUAT_USE_GPIO
  71. {"gpio", luaopen_gpio}, // GPIO脚的操作
  72. #endif
  73. #ifdef LUAT_USE_I2C
  74. {"i2c", luaopen_i2c}, // I2C操作
  75. #endif
  76. #ifdef LUAT_USE_SPI
  77. {"spi", luaopen_spi}, // SPI操作
  78. #endif
  79. #ifdef LUAT_USE_ADC
  80. {"adc", luaopen_adc}, // ADC模块
  81. #endif
  82. // #ifdef LUAT_USE_SDIO
  83. // {"sdio", luaopen_sdio}, // SDIO模块
  84. // #endif
  85. #ifdef LUAT_USE_PWM
  86. {"pwm", luaopen_pwm}, // PWM模块
  87. #endif
  88. #ifdef LUAT_USE_WDT
  89. {"wdt", luaopen_wdt}, // watchdog模块
  90. #endif
  91. #ifdef LUAT_USE_PM
  92. {"pm", luaopen_pm}, // 电源管理模块
  93. #endif
  94. #ifdef LUAT_USE_MCU
  95. {"mcu", luaopen_mcu}, // MCU特有的一些操作
  96. #endif
  97. // #ifdef LUAT_USE_HWTIMER
  98. // {"hwtimer", luaopen_hwtimer}, // 硬件定时器
  99. // #endif
  100. #ifdef LUAT_USE_RTC
  101. {"rtc", luaopen_rtc}, // 实时时钟
  102. #endif
  103. {"pin", luaopen_pin}, // pin
  104. #ifdef LUAT_USE_DAC
  105. {"dac", luaopen_dac},
  106. #endif
  107. #ifdef LUAT_USE_KEYBOARD
  108. {"keyboard", luaopen_keyboard},
  109. #endif
  110. #ifdef LUAT_USE_OTP
  111. {"otp", luaopen_otp},
  112. #endif
  113. #ifdef LUAT_USE_CAMERA
  114. {"camera", luaopen_camera},
  115. #endif
  116. //-----------------------------------------------------------------------
  117. // 工具库, 按需选用
  118. #ifdef LUAT_USE_CRYPTO
  119. {"crypto",luaopen_crypto}, // 加密和hash模块
  120. #endif
  121. #ifdef LUAT_USE_CJSON
  122. {"json", luaopen_cjson}, // json的序列化和反序列化
  123. #endif
  124. #ifdef LUAT_USE_ZBUFF
  125. {"zbuff", luaopen_zbuff}, // 像C语言语言操作内存块
  126. #endif
  127. #ifdef LUAT_USE_PACK
  128. {"pack", luaopen_pack}, // pack.pack/pack.unpack
  129. #endif
  130. // {"mqttcore",luaopen_mqttcore}, // MQTT 协议封装
  131. // {"libcoap", luaopen_libcoap}, // 处理COAP消息
  132. #ifdef LUAT_USE_FATFS
  133. {"fatfs", luaopen_fatfs},
  134. #endif
  135. #ifdef LUAT_USE_LIBGNSS
  136. {"libgnss", luaopen_libgnss}, // 处理GNSS定位数据
  137. #endif
  138. #ifdef LUAT_USE_FS
  139. {"fs", luaopen_fs}, // 文件系统库,在io库之外再提供一些方法
  140. #endif
  141. #ifdef LUAT_USE_SENSOR
  142. {"sensor", luaopen_sensor}, // 传感器库,支持DS18B20
  143. #endif
  144. #ifdef LUAT_USE_SFUD
  145. {"sfud", luaopen_sfud}, // sfud
  146. #endif
  147. #ifdef LUAT_USE_SFD
  148. {"sfd", luaopen_sfd},
  149. #endif
  150. #ifdef LUAT_USE_DISP
  151. {"disp", luaopen_disp}, // OLED显示模块,支持SSD1306
  152. #endif
  153. #ifdef LUAT_USE_U8G2
  154. {"u8g2", luaopen_u8g2}, // u8g2
  155. #endif
  156. #ifdef LUAT_USE_EINK
  157. {"eink", luaopen_eink}, // 电子墨水屏,试验阶段
  158. #endif
  159. #ifdef LUAT_USE_LVGL
  160. #ifndef LUAT_USE_LCD
  161. #define LUAT_USE_LCD
  162. #endif
  163. {"lvgl", luaopen_lvgl},
  164. #endif
  165. #ifdef LUAT_USE_LCD
  166. {"lcd", luaopen_lcd},
  167. #endif
  168. #ifdef LUAT_USE_STATEM
  169. {"statem", luaopen_statem},
  170. #endif
  171. #ifdef LUAT_USE_GTFONT
  172. {"gtfont", luaopen_gtfont},
  173. #endif
  174. #ifdef LUAT_USE_COREMARK
  175. {"coremark", luaopen_coremark},
  176. #endif
  177. #ifdef LUAT_USE_FDB
  178. {"fdb", luaopen_fdb},
  179. #endif
  180. #ifdef LUAT_USE_ZLIB
  181. {"zlib", luaopen_zlib},
  182. #endif
  183. #ifdef LUAT_USE_VMX
  184. {"vmx", luaopen_vmx},
  185. #endif
  186. #ifdef LUAT_USE_NES
  187. {"nes", luaopen_nes},
  188. #endif
  189. #ifdef LUAT_USE_SOFTKB
  190. {"softkb", luaopen_softkb},
  191. #endif
  192. #ifdef LUAT_USE_YMODEM
  193. {"ymodem", luaopen_ymodem},
  194. #endif
  195. #ifdef LUAT_USE_W5500
  196. {"w5500", luaopen_w5500},
  197. {"socket", luaopen_socket_adapter},
  198. {"mqtt", luaopen_mqtt},
  199. {"http", luaopen_http},
  200. #endif
  201. // #ifdef LUAT_USE_FOTA
  202. {"fota", luaopen_fota},
  203. // #endif
  204. #ifdef LUAT_USE_LORA
  205. {"lora", luaopen_lora},
  206. #endif
  207. #ifdef LUAT_USE_FONTS
  208. {"fonts", luaopen_fonts},
  209. #endif
  210. #ifdef LUAT_USE_MLX90640
  211. {"mlx90640", luaopen_mlx90640},
  212. #endif
  213. #ifdef LUAT_USE_MINIZ
  214. {"miniz", luaopen_miniz},
  215. #endif
  216. #ifdef LUAT_USE_IOTAUTH
  217. {"iotauth", luaopen_iotauth},
  218. #endif
  219. #ifdef LUAT_USE_PROTOBUF
  220. {"protobuf", luaopen_protobuf},
  221. #endif
  222. #ifdef LUAT_USE_RSA
  223. {"rsa", luaopen_rsa},
  224. #endif
  225. #ifdef LUAT_USE_USB
  226. {"usbapp", luaopen_usbapp},
  227. #endif
  228. #ifdef LUAT_USE_MEDIA
  229. {"audio", luaopen_multimedia_audio},
  230. {"codec", luaopen_multimedia_codec},
  231. #endif
  232. #ifdef LUAT_USE_IO_QUEUE
  233. {"ioqueue", luaopen_io_queue},
  234. #endif
  235. {NULL, NULL}
  236. };
  237. void luat_lvgl_tick_sleep(uint8_t OnOff);
  238. // 按不同的rtconfig加载不同的库函数
  239. void luat_openlibs(lua_State *L) {
  240. // 初始化队列服务
  241. luat_msgbus_init();
  242. //print_list_mem("done>luat_msgbus_init");
  243. // 加载系统库
  244. const luaL_Reg *lib;
  245. /* "require" functions from 'loadedlibs' and set results to global table */
  246. for (lib = loadedlibs; lib->func; lib++) {
  247. luaL_requiref(L, lib->name, lib->func, 1);
  248. lua_pop(L, 1); /* remove lib */
  249. }
  250. }
  251. void luat_os_reboot(int code) {
  252. SystemReset();
  253. }
  254. uint32_t luat_poweron_reason(void){
  255. return 0;
  256. }
  257. const char* luat_os_bsp(void) {
  258. return "AIR105";
  259. }
  260. void vConfigureTimerForRunTimeStats( void ) {}
  261. /** 设备进入待机模式 */
  262. void luat_os_standy(int timeout) {
  263. return; // nop
  264. }
  265. void luat_ota_reboot(int timeout_ms) {
  266. luat_lvgl_tick_sleep(1);
  267. if (timeout_ms > 0)
  268. luat_timer_mdelay(timeout_ms);
  269. SystemReset();
  270. }
  271. #ifdef LUAT_USE_LVGL
  272. #include "app_interface.h"
  273. #define LVGL_TICK_PERIOD 10
  274. unsigned int gLVFlashTime;
  275. static timer_t *lv_timer;
  276. static uint32_t lvgl_tick_cnt;
  277. static int luat_lvg_handler(lua_State* L, void* ptr) {
  278. // DBG("%u", lv_tick_get());
  279. if (lvgl_tick_cnt) lvgl_tick_cnt--;
  280. lv_task_handler();
  281. return 0;
  282. }
  283. static int32_t _lvgl_handler(void *pData, void *pParam) {
  284. if (lvgl_tick_cnt < 10)
  285. {
  286. lvgl_tick_cnt++;
  287. rtos_msg_t msg = {0};
  288. msg.handler = luat_lvg_handler;
  289. luat_msgbus_put(&msg, 0);
  290. }
  291. return 0;
  292. }
  293. void luat_lvgl_tick_sleep(uint8_t OnOff)
  294. {
  295. if (!OnOff)
  296. {
  297. Timer_StartMS(lv_timer, LVGL_TICK_PERIOD, 1);
  298. }
  299. else
  300. {
  301. Timer_Stop(lv_timer);
  302. }
  303. }
  304. #else
  305. void luat_lvgl_tick_sleep(uint8_t OnOff)
  306. {
  307. }
  308. #endif
  309. void luat_shell_poweron(int _drv);
  310. void luat_base_init(void)
  311. {
  312. luat_vm_pool_init();
  313. #ifdef LUAT_USE_SHELL
  314. luat_shell_poweron(0);
  315. #endif
  316. #ifdef LUAT_USE_LVGL
  317. gLVFlashTime = 33;
  318. lv_init();
  319. lv_timer = Timer_Create(_lvgl_handler, NULL, NULL);
  320. #ifdef __LVGL_SLEEP_ENABLE__
  321. luat_lvgl_tick_sleep(1);
  322. #else
  323. Timer_StartMS(lv_timer, LVGL_TICK_PERIOD, 1);
  324. #endif
  325. #endif
  326. }
  327. time_t luat_time(time_t *_Time) {
  328. if (_Time != NULL) {
  329. *_Time = RTC_GetUTC();
  330. }
  331. return RTC_GetUTC();
  332. }
  333. static uint32_t cri;
  334. uint8_t cri_flag;
  335. static uint8_t disable_all; //如果设置成1,将关闭总中断,非常危险,谨慎使用
  336. void luat_os_set_cri_all(uint8_t onoff)
  337. {
  338. disable_all = onoff;
  339. }
  340. //进入临界保护,不可重入
  341. void luat_os_entry_cri(void) {
  342. if (disable_all) {
  343. __disable_irq();
  344. return;
  345. }
  346. if (!cri_flag) {
  347. cri = OS_EnterCritical();
  348. cri_flag = 1;
  349. }
  350. }
  351. //退出临界保护,不可重入
  352. void luat_os_exit_cri(void) {
  353. if (disable_all) {
  354. __enable_irq();
  355. return;
  356. }
  357. if (cri_flag) {
  358. cri_flag = 0;
  359. OS_ExitCritical(cri);
  360. }
  361. }