luat_base_air105.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. extern const uint32_t __isr_start_address;
  39. const uint32_t __attribute__((section (".app_info")))
  40. g_CAppInfo[8] =
  41. {
  42. __APP_START_MAGIC__,
  43. &__isr_start_address,
  44. __BL_VERSION__,
  45. __CORE_VERSION__,
  46. __FLASH_BASE_ADDR__ + __FLASH_MAX_SIZE__ - FLASH_FS_REGION_SIZE * 1024,
  47. __FLASH_BASE_ADDR__ + __FLASH_MAX_SIZE__ - LUAT_FS_SIZE * 1024,
  48. 0,
  49. 0,
  50. };
  51. LUAMOD_API int luaopen_usbapp( lua_State *L );
  52. static const luaL_Reg loadedlibs[] = {
  53. {"_G", luaopen_base}, // _G
  54. {LUA_LOADLIBNAME, luaopen_package}, // require
  55. {LUA_COLIBNAME, luaopen_coroutine}, // coroutine协程库
  56. {LUA_TABLIBNAME, luaopen_table}, // table库,操作table类型的数据结构
  57. {LUA_IOLIBNAME, luaopen_io}, // io库,操作文件
  58. {LUA_OSLIBNAME, luaopen_os}, // os库,已精简
  59. {LUA_STRLIBNAME, luaopen_string}, // string库,字符串操作
  60. {LUA_MATHLIBNAME, luaopen_math}, // math 数值计算
  61. {LUA_UTF8LIBNAME, luaopen_utf8},
  62. {LUA_DBLIBNAME, luaopen_debug}, // debug库,已精简
  63. #ifdef LUAT_USE_DBG
  64. #ifndef LUAT_USE_SHELL
  65. #define LUAT_USE_SHELL
  66. #endif
  67. {"dbg", luaopen_dbg}, // 调试库
  68. #endif
  69. #if defined(LUA_COMPAT_BITLIB)
  70. {LUA_BITLIBNAME, luaopen_bit32}, // 不太可能启用
  71. #endif
  72. // 往下是LuatOS定制的库, 如需精简请仔细测试
  73. //----------------------------------------------------------------------
  74. // 核心支撑库, 不可禁用!!
  75. {"rtos", luaopen_rtos}, // rtos底层库, 核心功能是队列和定时器
  76. {"log", luaopen_log}, // 日志库
  77. {"timer", luaopen_timer}, // 延时库
  78. //-----------------------------------------------------------------------
  79. // 设备驱动类, 可按实际情况删减. 即使最精简的固件, 也强烈建议保留uart库
  80. #ifdef LUAT_USE_UART
  81. {"uart", luaopen_uart}, // 串口操作
  82. #endif
  83. #ifdef LUAT_USE_GPIO
  84. {"gpio", luaopen_gpio}, // GPIO脚的操作
  85. #endif
  86. #ifdef LUAT_USE_I2C
  87. {"i2c", luaopen_i2c}, // I2C操作
  88. #endif
  89. #ifdef LUAT_USE_SPI
  90. {"spi", luaopen_spi}, // SPI操作
  91. #endif
  92. #ifdef LUAT_USE_ADC
  93. {"adc", luaopen_adc}, // ADC模块
  94. #endif
  95. // #ifdef LUAT_USE_SDIO
  96. // {"sdio", luaopen_sdio}, // SDIO模块
  97. // #endif
  98. #ifdef LUAT_USE_PWM
  99. {"pwm", luaopen_pwm}, // PWM模块
  100. #endif
  101. #ifdef LUAT_USE_WDT
  102. {"wdt", luaopen_wdt}, // watchdog模块
  103. #endif
  104. #ifdef LUAT_USE_PM
  105. {"pm", luaopen_pm}, // 电源管理模块
  106. #endif
  107. #ifdef LUAT_USE_MCU
  108. {"mcu", luaopen_mcu}, // MCU特有的一些操作
  109. #endif
  110. // #ifdef LUAT_USE_HWTIMER
  111. // {"hwtimer", luaopen_hwtimer}, // 硬件定时器
  112. // #endif
  113. #ifdef LUAT_USE_RTC
  114. {"rtc", luaopen_rtc}, // 实时时钟
  115. #endif
  116. {"pin", luaopen_pin}, // pin
  117. #ifdef LUAT_USE_DAC
  118. {"dac", luaopen_dac},
  119. #endif
  120. #ifdef LUAT_USE_KEYBOARD
  121. {"keyboard", luaopen_keyboard},
  122. #endif
  123. #ifdef LUAT_USE_OTP
  124. {"otp", luaopen_otp},
  125. #endif
  126. #ifdef LUAT_USE_CAMERA
  127. {"camera", luaopen_camera},
  128. #endif
  129. //-----------------------------------------------------------------------
  130. // 工具库, 按需选用
  131. #ifdef LUAT_USE_CRYPTO
  132. {"crypto",luaopen_crypto}, // 加密和hash模块
  133. #endif
  134. #ifdef LUAT_USE_CJSON
  135. {"json", luaopen_cjson}, // json的序列化和反序列化
  136. #endif
  137. #ifdef LUAT_USE_ZBUFF
  138. {"zbuff", luaopen_zbuff}, // 像C语言语言操作内存块
  139. #endif
  140. #ifdef LUAT_USE_PACK
  141. {"pack", luaopen_pack}, // pack.pack/pack.unpack
  142. #endif
  143. #ifdef LUAT_USE_MQTTCORE
  144. {"mqttcore",luaopen_mqttcore}, // MQTT 协议封装
  145. #endif
  146. #ifdef LUAT_USE_LIBCOAP
  147. {"libcoap", luaopen_libcoap}, // 处理COAP消息
  148. #endif
  149. #ifdef LUAT_USE_FATFS
  150. {"fatfs", luaopen_fatfs},
  151. #endif
  152. #ifdef LUAT_USE_LIBGNSS
  153. {"libgnss", luaopen_libgnss}, // 处理GNSS定位数据
  154. #endif
  155. #ifdef LUAT_USE_FS
  156. {"fs", luaopen_fs}, // 文件系统库,在io库之外再提供一些方法
  157. #endif
  158. #ifdef LUAT_USE_SENSOR
  159. {"sensor", luaopen_sensor}, // 传感器库,支持DS18B20
  160. #endif
  161. #ifdef LUAT_USE_SFUD
  162. {"sfud", luaopen_sfud}, // sfud
  163. #endif
  164. #ifdef LUAT_USE_SFD
  165. {"sfd", luaopen_sfd},
  166. #endif
  167. #ifdef LUAT_USE_DISP
  168. {"disp", luaopen_disp}, // OLED显示模块,支持SSD1306
  169. #endif
  170. #ifdef LUAT_USE_U8G2
  171. {"u8g2", luaopen_u8g2}, // u8g2
  172. #endif
  173. #ifdef LUAT_USE_EINK
  174. {"eink", luaopen_eink}, // 电子墨水屏,试验阶段
  175. #endif
  176. #ifdef LUAT_USE_LVGL
  177. #ifndef LUAT_USE_LCD
  178. #define LUAT_USE_LCD
  179. #endif
  180. {"lvgl", luaopen_lvgl},
  181. #endif
  182. #ifdef LUAT_USE_LCD
  183. {"lcd", luaopen_lcd},
  184. #endif
  185. #ifdef LUAT_USE_STATEM
  186. {"statem", luaopen_statem},
  187. #endif
  188. #ifdef LUAT_USE_GTFONT
  189. {"gtfont", luaopen_gtfont},
  190. #endif
  191. #ifdef LUAT_USE_COREMARK
  192. {"coremark", luaopen_coremark},
  193. #endif
  194. #ifdef LUAT_USE_FSKV
  195. {"fskv", luaopen_fskv},
  196. // 启用FSKV的时候,自动禁用FDB
  197. #ifdef LUAT_USE_FDB
  198. #undef LUAT_USE_FDB
  199. #endif
  200. #endif
  201. #ifdef LUAT_USE_FDB
  202. {"fdb", luaopen_fdb},
  203. #endif
  204. #ifdef LUAT_USE_ZLIB
  205. {"zlib", luaopen_zlib},
  206. #endif
  207. #ifdef LUAT_USE_VMX
  208. {"vmx", luaopen_vmx},
  209. #endif
  210. #ifdef LUAT_USE_NES
  211. {"nes", luaopen_nes},
  212. #endif
  213. #ifdef LUAT_USE_SOFTKB
  214. {"softkb", luaopen_softkb},
  215. #endif
  216. #ifdef LUAT_USE_YMODEM
  217. {"ymodem", luaopen_ymodem},
  218. #endif
  219. #ifdef LUAT_USE_W5500
  220. {"w5500", luaopen_w5500},
  221. {"socket", luaopen_socket_adapter},
  222. {"mqtt", luaopen_mqtt},
  223. {"http", luaopen_http},
  224. {"websocket", luaopen_websocket},
  225. #ifdef LUAT_USE_FTP
  226. {"ftp", luaopen_ftp},
  227. #endif
  228. #endif
  229. // #ifdef LUAT_USE_FOTA
  230. {"fota", luaopen_fota},
  231. // #endif
  232. #ifdef LUAT_USE_LORA
  233. {"lora", luaopen_lora},
  234. #endif
  235. #ifdef LUAT_USE_LORA2
  236. {"lora2", luaopen_lora2},
  237. #endif
  238. #ifdef LUAT_USE_FONTS
  239. {"fonts", luaopen_fonts},
  240. #endif
  241. #ifdef LUAT_USE_MLX90640
  242. {"mlx90640", luaopen_mlx90640},
  243. #endif
  244. #ifdef LUAT_USE_MINIZ
  245. {"miniz", luaopen_miniz},
  246. #endif
  247. #ifdef LUAT_USE_IOTAUTH
  248. {"iotauth", luaopen_iotauth},
  249. #endif
  250. #ifdef LUAT_USE_PROTOBUF
  251. {"protobuf", luaopen_protobuf},
  252. #endif
  253. #ifdef LUAT_USE_RSA
  254. {"rsa", luaopen_rsa},
  255. #endif
  256. #ifdef LUAT_USE_USB
  257. {"usbapp", luaopen_usbapp},
  258. #endif
  259. #ifdef LUAT_USE_MEDIA
  260. {"audio", luaopen_multimedia_audio},
  261. {"codec", luaopen_multimedia_codec},
  262. #endif
  263. #ifdef LUAT_USE_IO_QUEUE
  264. {"ioqueue", luaopen_io_queue},
  265. #endif
  266. #ifdef LUAT_USE_MAX30102
  267. {"max30102", luaopen_max30102},
  268. #endif
  269. #ifdef LUAT_USE_GMSSL
  270. {"gmssl", luaopen_gmssl}, // 国密算法
  271. #endif
  272. #ifdef LUAT_USE_ICONV
  273. {"iconv", luaopen_iconv},
  274. #endif
  275. #ifdef LUAT_USE_BIT64
  276. {"bit64", luaopen_bit64},
  277. #endif
  278. #ifdef LUAT_USE_REPL
  279. {"repl", luaopen_repl},
  280. #endif
  281. {NULL, NULL}
  282. };
  283. void luat_lvgl_tick_sleep(uint8_t OnOff);
  284. // 按不同的rtconfig加载不同的库函数
  285. void luat_openlibs(lua_State *L) {
  286. // 初始化队列服务
  287. luat_msgbus_init();
  288. //print_list_mem("done>luat_msgbus_init");
  289. // 加载系统库
  290. const luaL_Reg *lib;
  291. /* "require" functions from 'loadedlibs' and set results to global table */
  292. for (lib = loadedlibs; lib->func; lib++) {
  293. luaL_requiref(L, lib->name, lib->func, 1);
  294. lua_pop(L, 1); /* remove lib */
  295. }
  296. }
  297. void luat_os_reboot(int code) {
  298. SystemReset();
  299. }
  300. uint32_t luat_poweron_reason(void){
  301. return 0;
  302. }
  303. const char* luat_os_bsp(void) {
  304. return "AIR105";
  305. }
  306. void vConfigureTimerForRunTimeStats( void ) {}
  307. /** 设备进入待机模式 */
  308. void luat_os_standy(int timeout) {
  309. return; // nop
  310. }
  311. void luat_ota_reboot(int timeout_ms) {
  312. luat_lvgl_tick_sleep(1);
  313. if (timeout_ms > 0)
  314. luat_timer_mdelay(timeout_ms);
  315. SystemReset();
  316. }
  317. #ifdef LUAT_USE_LVGL
  318. #include "app_interface.h"
  319. #define LVGL_TICK_PERIOD 10
  320. unsigned int gLVFlashTime;
  321. static timer_t *lv_timer;
  322. static uint32_t lvgl_tick_cnt;
  323. static int luat_lvg_handler(lua_State* L, void* ptr) {
  324. // DBG("%u", lv_tick_get());
  325. if (lvgl_tick_cnt) lvgl_tick_cnt--;
  326. lv_task_handler();
  327. return 0;
  328. }
  329. static int32_t _lvgl_handler(void *pData, void *pParam) {
  330. if (lvgl_tick_cnt < 10)
  331. {
  332. lvgl_tick_cnt++;
  333. rtos_msg_t msg = {0};
  334. msg.handler = luat_lvg_handler;
  335. luat_msgbus_put(&msg, 0);
  336. }
  337. return 0;
  338. }
  339. void luat_lvgl_tick_sleep(uint8_t OnOff)
  340. {
  341. if (!OnOff)
  342. {
  343. Timer_StartMS(lv_timer, LVGL_TICK_PERIOD, 1);
  344. }
  345. else
  346. {
  347. Timer_Stop(lv_timer);
  348. }
  349. }
  350. #else
  351. void luat_lvgl_tick_sleep(uint8_t OnOff)
  352. {
  353. }
  354. #endif
  355. void luat_shell_poweron(int _drv);
  356. void luat_base_init(void)
  357. {
  358. luat_vm_pool_init();
  359. #if defined(LUAT_USE_SHELL) || defined(LUAT_USE_REPL)
  360. luat_shell_poweron(0);
  361. #endif
  362. #ifdef LUAT_USE_LVGL
  363. gLVFlashTime = 33;
  364. lv_init();
  365. lv_timer = Timer_Create(_lvgl_handler, NULL, NULL);
  366. #ifdef __LVGL_SLEEP_ENABLE__
  367. luat_lvgl_tick_sleep(1);
  368. #else
  369. Timer_StartMS(lv_timer, LVGL_TICK_PERIOD, 1);
  370. #endif
  371. #endif
  372. }
  373. time_t luat_time(time_t *_Time) {
  374. if (_Time != NULL) {
  375. *_Time = RTC_GetUTC();
  376. }
  377. return RTC_GetUTC();
  378. }
  379. static uint32_t cri;
  380. uint8_t cri_flag;
  381. static uint8_t disable_all; //如果设置成1,将关闭总中断,非常危险,谨慎使用
  382. void luat_os_set_cri_all(uint8_t onoff)
  383. {
  384. disable_all = onoff;
  385. }
  386. //进入临界保护,不可重入
  387. void luat_os_entry_cri(void) {
  388. if (disable_all) {
  389. __disable_irq();
  390. return;
  391. }
  392. if (!cri_flag) {
  393. cri = OS_EnterCritical();
  394. cri_flag = 1;
  395. }
  396. }
  397. //退出临界保护,不可重入
  398. void luat_os_exit_cri(void) {
  399. if (disable_all) {
  400. __enable_irq();
  401. return;
  402. }
  403. if (cri_flag) {
  404. cri_flag = 0;
  405. OS_ExitCritical(cri);
  406. }
  407. }