luat_base_air101.c 8.9 KB

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