luat_base_air105.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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_gtfont( lua_State *L );
  39. LUAMOD_API int luaopen_usbapp( lua_State *L );
  40. static const luaL_Reg loadedlibs[] = {
  41. {"_G", luaopen_base}, // _G
  42. {LUA_LOADLIBNAME, luaopen_package}, // require
  43. {LUA_COLIBNAME, luaopen_coroutine}, // coroutine协程库
  44. {LUA_TABLIBNAME, luaopen_table}, // table库,操作table类型的数据结构
  45. {LUA_IOLIBNAME, luaopen_io}, // io库,操作文件
  46. {LUA_OSLIBNAME, luaopen_os}, // os库,已精简
  47. {LUA_STRLIBNAME, luaopen_string}, // string库,字符串操作
  48. {LUA_MATHLIBNAME, luaopen_math}, // math 数值计算
  49. // {LUA_UTF8LIBNAME, luaopen_utf8},
  50. {LUA_DBLIBNAME, luaopen_debug}, // debug库,已精简
  51. #ifdef LUAT_USE_DBG
  52. #ifndef LUAT_USE_SHELL
  53. #define LUAT_USE_SHELL
  54. #endif
  55. {"dbg", luaopen_dbg}, // 调试库
  56. #endif
  57. #if defined(LUA_COMPAT_BITLIB)
  58. {LUA_BITLIBNAME, luaopen_bit32}, // 不太可能启用
  59. #endif
  60. // 往下是LuatOS定制的库, 如需精简请仔细测试
  61. //----------------------------------------------------------------------
  62. // 核心支撑库, 不可禁用!!
  63. {"rtos", luaopen_rtos}, // rtos底层库, 核心功能是队列和定时器
  64. {"log", luaopen_log}, // 日志库
  65. {"timer", luaopen_timer}, // 延时库
  66. //-----------------------------------------------------------------------
  67. // 设备驱动类, 可按实际情况删减. 即使最精简的固件, 也强烈建议保留uart库
  68. #ifdef LUAT_USE_UART
  69. {"uart", luaopen_uart}, // 串口操作
  70. #endif
  71. #ifdef LUAT_USE_GPIO
  72. {"gpio", luaopen_gpio}, // GPIO脚的操作
  73. #endif
  74. #ifdef LUAT_USE_I2C
  75. {"i2c", luaopen_i2c}, // I2C操作
  76. #endif
  77. #ifdef LUAT_USE_SPI
  78. {"spi", luaopen_spi}, // SPI操作
  79. #endif
  80. #ifdef LUAT_USE_ADC
  81. {"adc", luaopen_adc}, // ADC模块
  82. #endif
  83. // #ifdef LUAT_USE_SDIO
  84. // {"sdio", luaopen_sdio}, // SDIO模块
  85. // #endif
  86. #ifdef LUAT_USE_PWM
  87. {"pwm", luaopen_pwm}, // PWM模块
  88. #endif
  89. #ifdef LUAT_USE_WDT
  90. {"wdt", luaopen_wdt}, // watchdog模块
  91. #endif
  92. #ifdef LUAT_USE_PM
  93. {"pm", luaopen_pm}, // 电源管理模块
  94. #endif
  95. #ifdef LUAT_USE_MCU
  96. {"mcu", luaopen_mcu}, // MCU特有的一些操作
  97. #endif
  98. // #ifdef LUAT_USE_HWTIMER
  99. // {"hwtimer", luaopen_hwtimer}, // 硬件定时器
  100. // #endif
  101. #ifdef LUAT_USE_RTC
  102. {"rtc", luaopen_rtc}, // 实时时钟
  103. #endif
  104. {"pin", luaopen_pin}, // pin
  105. #ifdef LUAT_USE_DAC
  106. {"dac", luaopen_dac},
  107. #endif
  108. #ifdef LUAT_USE_KEYBOARD
  109. {"keyboard", luaopen_keyboard},
  110. #endif
  111. #ifdef LUAT_USE_OTP
  112. {"otp", luaopen_otp},
  113. #endif
  114. #ifdef LUAT_USE_CAMERA
  115. {"camera", luaopen_camera},
  116. #endif
  117. //-----------------------------------------------------------------------
  118. // 工具库, 按需选用
  119. #ifdef LUAT_USE_CRYPTO
  120. {"crypto",luaopen_crypto}, // 加密和hash模块
  121. #endif
  122. #ifdef LUAT_USE_CJSON
  123. {"json", luaopen_cjson}, // json的序列化和反序列化
  124. #endif
  125. #ifdef LUAT_USE_ZBUFF
  126. {"zbuff", luaopen_zbuff}, // 像C语言语言操作内存块
  127. #endif
  128. #ifdef LUAT_USE_PACK
  129. {"pack", luaopen_pack}, // pack.pack/pack.unpack
  130. #endif
  131. // {"mqttcore",luaopen_mqttcore}, // MQTT 协议封装
  132. // {"libcoap", luaopen_libcoap}, // 处理COAP消息
  133. #ifdef LUAT_USE_FATFS
  134. {"fatfs", luaopen_fatfs},
  135. #endif
  136. #ifdef LUAT_USE_LIBGNSS
  137. {"libgnss", luaopen_libgnss}, // 处理GNSS定位数据
  138. #endif
  139. #ifdef LUAT_USE_FS
  140. {"fs", luaopen_fs}, // 文件系统库,在io库之外再提供一些方法
  141. #endif
  142. #ifdef LUAT_USE_SENSOR
  143. {"sensor", luaopen_sensor}, // 传感器库,支持DS18B20
  144. #endif
  145. #ifdef LUAT_USE_SFUD
  146. {"sfud", luaopen_sfud}, // sfud
  147. #endif
  148. #ifdef LUAT_USE_SFD
  149. {"sfd", luaopen_sfd},
  150. #endif
  151. #ifdef LUAT_USE_DISP
  152. {"disp", luaopen_disp}, // OLED显示模块,支持SSD1306
  153. #endif
  154. #ifdef LUAT_USE_U8G2
  155. {"u8g2", luaopen_u8g2}, // u8g2
  156. #endif
  157. #ifdef LUAT_USE_EINK
  158. {"eink", luaopen_eink}, // 电子墨水屏,试验阶段
  159. #endif
  160. #ifdef LUAT_USE_LVGL
  161. #ifndef LUAT_USE_LCD
  162. #define LUAT_USE_LCD
  163. #endif
  164. {"lvgl", luaopen_lvgl},
  165. #endif
  166. #ifdef LUAT_USE_LCD
  167. {"lcd", luaopen_lcd},
  168. #endif
  169. #ifdef LUAT_USE_STATEM
  170. {"statem", luaopen_statem},
  171. #endif
  172. #ifdef LUAT_USE_GTFONT
  173. {"gtfont", luaopen_gtfont},
  174. #endif
  175. #ifdef LUAT_USE_COREMARK
  176. {"coremark", luaopen_coremark},
  177. #endif
  178. #ifdef LUAT_USE_FDB
  179. {"fdb", luaopen_fdb},
  180. #endif
  181. #ifdef LUAT_USE_ZLIB
  182. {"zlib", luaopen_zlib},
  183. #endif
  184. #ifdef LUAT_USE_VMX
  185. {"vmx", luaopen_vmx},
  186. #endif
  187. #ifdef LUAT_USE_NES
  188. {"nes", luaopen_nes},
  189. #endif
  190. {"usbapp", luaopen_usbapp},
  191. {NULL, NULL}
  192. };
  193. // 按不同的rtconfig加载不同的库函数
  194. void luat_openlibs(lua_State *L) {
  195. // 初始化队列服务
  196. luat_msgbus_init();
  197. //print_list_mem("done>luat_msgbus_init");
  198. // 加载系统库
  199. const luaL_Reg *lib;
  200. /* "require" functions from 'loadedlibs' and set results to global table */
  201. for (lib = loadedlibs; lib->func; lib++) {
  202. luaL_requiref(L, lib->name, lib->func, 1);
  203. lua_pop(L, 1); /* remove lib */
  204. }
  205. }
  206. void luat_os_reboot(int code) {
  207. SystemReset();
  208. }
  209. const char* luat_os_bsp(void) {
  210. return "air105";
  211. }
  212. void vConfigureTimerForRunTimeStats( void ) {}
  213. /** 设备进入待机模式 */
  214. void luat_os_standy(int timeout) {
  215. return; // nop
  216. }
  217. void luat_ota_reboot(int timeout_ms) {
  218. if (timeout_ms > 0)
  219. luat_timer_mdelay(timeout_ms);
  220. SystemReset();
  221. }
  222. #ifdef LUAT_USE_LVGL
  223. #include "app_interface.h"
  224. #define LVGL_TICK_PERIOD 10
  225. unsigned int gLVFlashTime;
  226. static timer_t *lv_timer;
  227. static int luat_lvg_handler(lua_State* L, void* ptr) {
  228. // DBG("%u", lv_tick_get());
  229. lv_task_handler();
  230. return 0;
  231. }
  232. static int32_t _lvgl_handler(void *pData, void *pParam) {
  233. rtos_msg_t msg = {0};
  234. msg.handler = luat_lvg_handler;
  235. luat_msgbus_put(&msg, 0);
  236. }
  237. void luat_lvgl_tick_sleep(uint8_t OnOff)
  238. {
  239. if (!OnOff)
  240. {
  241. Timer_StartMS(lv_timer, LVGL_TICK_PERIOD, 1);
  242. }
  243. else
  244. {
  245. Timer_Stop(lv_timer);
  246. }
  247. }
  248. #endif
  249. void luat_shell_poweron(int _drv);
  250. void luat_base_init(void)
  251. {
  252. luat_gpio_init();
  253. #ifdef LUAT_USE_SHELL
  254. luat_shell_poweron(0);
  255. #endif
  256. #ifdef LUAT_USE_LVGL
  257. gLVFlashTime = 33;
  258. lv_init();
  259. lv_timer = Timer_Create(_lvgl_handler, NULL, NULL);
  260. #ifdef __LVGL_SLEEP_ENABLE__
  261. luat_lvgl_tick_sleep(1);
  262. #else
  263. Timer_StartMS(lv_timer, LVGL_TICK_PERIOD, 1);
  264. #endif
  265. #endif
  266. }
  267. time_t luat_time(time_t *_Time) {
  268. if (_Time != NULL) {
  269. *_Time = RTC_GetUTC();
  270. }
  271. return RTC_GetUTC();
  272. }
  273. //static uint32_t cri = 0;
  274. void luat_os_entry_cri(void) {
  275. // if (cri == 0)
  276. // cri = OS_EnterCritical();
  277. }
  278. void luat_os_exit_cri(void) {
  279. // if (cri != 0) {
  280. // OS_ExitCritical(cri);
  281. // cri = 0;
  282. // }
  283. }