luat_base_air101.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #ifdef LUAT_USE_MQTTCORE
  100. {"mqttcore",luaopen_mqttcore}, // MQTT 协议封装
  101. #endif
  102. #ifdef LUAT_USE_LIBCOAP
  103. {"libcoap", luaopen_libcoap}, // 处理COAP消息
  104. #endif
  105. #ifdef LUAT_USE_ERCOAP
  106. {"ercoap", luaopen_ercoap}, // 处理COAP消息
  107. #endif
  108. #ifdef LUAT_USE_LIBGNSS
  109. {"libgnss", luaopen_libgnss}, // 处理GNSS定位数据
  110. #endif
  111. #ifdef LUAT_USE_FS
  112. {"fs", luaopen_fs}, // 文件系统库,在io库之外再提供一些方法
  113. #endif
  114. #ifdef LUAT_USE_SENSOR
  115. {"sensor", luaopen_sensor}, // 传感器库,支持DS18B20
  116. #endif
  117. #ifdef LUAT_USE_SFUD
  118. {"sfud", luaopen_sfud}, // sfud
  119. #endif
  120. #ifdef LUAT_USE_DISP
  121. {"disp", luaopen_disp}, // OLED显示模块,支持SSD1306
  122. #endif
  123. #ifdef LUAT_USE_U8G2
  124. {"u8g2", luaopen_u8g2}, // u8g2
  125. #endif
  126. #ifdef LUAT_USE_EINK
  127. {"eink", luaopen_eink}, // 电子墨水屏,试验阶段
  128. #endif
  129. #ifdef LUAT_USE_LVGL
  130. // #ifndef LUAT_USE_LCD
  131. // #define LUAT_USE_LCD
  132. // #endif
  133. {"lvgl", luaopen_lvgl},
  134. #endif
  135. #ifdef LUAT_USE_LCD
  136. {"lcd", luaopen_lcd},
  137. #endif
  138. #ifdef LUAT_USE_STATEM
  139. {"statem", luaopen_statem},
  140. #endif
  141. #ifdef LUAT_USE_GTFONT
  142. {"gtfont", luaopen_gtfont},
  143. #endif
  144. #ifdef LUAT_USE_NIMBLE
  145. {"nimble", luaopen_nimble},
  146. #endif
  147. #ifdef LUAT_USE_FSKV
  148. {"fskv", luaopen_fskv},
  149. // 启用FSKV的时候,自动禁用FDB
  150. #ifdef LUAT_USE_FDB
  151. #undef LUAT_USE_FDB
  152. #endif
  153. #endif
  154. #ifdef LUAT_USE_FDB
  155. {"fdb", luaopen_fdb},
  156. #endif
  157. #ifdef LUAT_USE_LCDSEG
  158. {"lcdseg", luaopen_lcdseg},
  159. #endif
  160. #ifdef LUAT_USE_VMX
  161. {"vmx", luaopen_vmx},
  162. #endif
  163. #ifdef LUAT_USE_COREMARK
  164. {"coremark", luaopen_coremark},
  165. #endif
  166. #ifdef LUAT_USE_FONTS
  167. {"fonts", luaopen_fonts},
  168. #endif
  169. #ifdef LUAT_USE_MLX90640
  170. {"mlx90640", luaopen_mlx90640},
  171. #endif
  172. #ifdef LUAT_USE_IR
  173. {"ir", luaopen_ir},
  174. #endif
  175. #ifdef LUAT_USE_YMODEM
  176. {"ymodem", luaopen_ymodem},
  177. #endif
  178. #ifdef LUAT_USE_I2S
  179. {"i2s", luaopen_i2s},
  180. #endif
  181. #ifdef LUAT_USE_NETWORK
  182. {"socket", luaopen_socket_adapter},
  183. #ifdef LUAT_USE_HTTP
  184. {"http", luaopen_http},
  185. #endif
  186. #ifdef LUAT_USE_MQTT
  187. {"mqtt", luaopen_mqtt},
  188. #endif
  189. #ifdef LUAT_USE_WEBSOCKET
  190. {"websocket", luaopen_websocket},
  191. #endif
  192. #ifdef LUAT_USE_W5500
  193. {"w5500", luaopen_w5500},
  194. #endif
  195. #ifdef LUAT_USE_HTTPSRV
  196. // {"httpsrv", luaopen_httpsrv},
  197. #endif
  198. #ifdef LUAT_USE_FTP
  199. {"ftp", luaopen_ftp},
  200. #endif
  201. #ifdef LUAT_USE_ULWIP
  202. {"ulwip", luaopen_ulwip},
  203. #endif
  204. #ifdef LUAT_USE_ERRDUMP
  205. {"errDump", luaopen_errdump},
  206. #endif
  207. #endif
  208. #ifdef LUAT_USE_LORA
  209. {"lora", luaopen_lora},
  210. #endif
  211. #ifdef LUAT_USE_MINIZ
  212. {"miniz", luaopen_miniz},
  213. #endif
  214. #ifdef LUAT_USE_PROTOBUF
  215. {"protobuf", luaopen_protobuf},
  216. #endif
  217. #ifdef LUAT_USE_IOTAUTH
  218. {"iotauth", luaopen_iotauth},
  219. #endif
  220. #ifdef LUAT_USE_RSA
  221. {"rsa", luaopen_rsa},
  222. #endif
  223. #ifdef LUAT_USE_FATFS
  224. {"fatfs", luaopen_fatfs}, // SD卡/tf卡
  225. #endif
  226. #ifdef LUAT_USE_GMSSL
  227. {"gmssl", luaopen_gmssl}, // 国密算法
  228. #endif
  229. #ifdef LUAT_USE_NES
  230. {"nes", luaopen_nes},
  231. #endif
  232. #ifdef LUAT_USE_MAX30102
  233. {"max30102", luaopen_max30102},
  234. #endif
  235. #ifdef LUAT_USE_WLAN
  236. {"wlan", luaopen_wlan},
  237. #ifdef LUAT_USE_WLAN_RAW
  238. {"wlanraw", luaopen_wlan_raw},
  239. #endif
  240. #endif
  241. #ifdef LUAT_USE_PROFILER
  242. {"profiler", luaopen_profiler},
  243. #endif
  244. #ifdef LUAT_USE_REPL
  245. {"repl", luaopen_repl},
  246. #endif
  247. #ifdef LUAT_USE_ICONV
  248. {"iconv", luaopen_iconv},
  249. #endif
  250. #ifdef LUAT_USE_FASTLZ
  251. {"fastlz", luaopen_fastlz},
  252. #endif
  253. #ifdef LUAT_USE_I2S
  254. {"i2s", luaopen_i2s},
  255. #endif
  256. #ifdef LUAT_USE_MEDIA
  257. {"audio", luaopen_multimedia_audio},
  258. {"codec", luaopen_multimedia_codec},
  259. #endif
  260. #ifdef LUAT_USE_USERNET
  261. {"usernet", luaopen_usernet},
  262. #endif
  263. #ifdef LUAT_USE_SQLITE3
  264. {"sqlite3", luaopen_sqlite3},
  265. #endif
  266. #ifdef LUAT_USE_WS2812
  267. {"ws2812", luaopen_ws2812},
  268. #endif
  269. #ifdef LUAT_USE_ONEWIRE
  270. {"onewire", luaopen_onewire},
  271. #endif
  272. #ifdef LUAT_USE_ERCOAP
  273. {"ercoap", luaopen_ercoap},
  274. #endif
  275. #ifdef LUAT_USE_XXTEA
  276. {"xxtea", luaopen_xxtea},
  277. #endif
  278. #ifdef LUAT_USE_FOTA
  279. {"fota", luaopen_fota},
  280. #endif
  281. #ifdef LUAT_USE_HT1621
  282. {"ht1621", luaopen_ht1621},
  283. #endif
  284. #ifdef LUAT_USE_AIRLINK
  285. {"airlink", luaopen_airlink},
  286. #endif
  287. #ifdef LUAT_USE_NETDRV
  288. {"netdrv", luaopen_netdrv},
  289. #endif
  290. // {"opus", luaopen_opus},
  291. {NULL, NULL}
  292. };
  293. // 按不同的rtconfig加载不同的库函数
  294. void luat_openlibs(lua_State *L) {
  295. // 初始化队列服务
  296. luat_msgbus_init();
  297. // print_list_mem("done>luat_msgbus_init");
  298. // 加载系统库
  299. const luaL_Reg *lib;
  300. /* "require" functions from 'loadedlibs' and set results to global table */
  301. for (lib = loadedlibs; lib->func; lib++) {
  302. luaL_requiref(L, lib->name, lib->func, 1);
  303. lua_pop(L, 1); /* remove lib */
  304. //extern void print_list_mem(const char* name);
  305. //print_list_mem(lib->name);
  306. }
  307. }
  308. #include "FreeRTOS.h"
  309. #include "task.h"
  310. const char* luat_os_bsp(void)
  311. {
  312. #if defined(AIR601)
  313. return "AIR601";
  314. #elif defined(AIR690)
  315. return "AIR690";
  316. #elif defined(AIR103)
  317. return "AIR103";
  318. #else
  319. return "AIR101";
  320. #endif
  321. }
  322. #include "wm_watchdog.h"
  323. #include "luat_mcu.h"
  324. void luat_os_reboot(int code)
  325. {
  326. if (luat_mcu_get_clk() < 80) {
  327. luat_mcu_set_clk(80);
  328. }
  329. tls_sys_set_reboot_reason(REBOOT_REASON_ACTIVE);
  330. tls_sys_reset();
  331. }
  332. static void pmu_timer0_irq(u8 *arg){}
  333. void luat_os_standy(int timeout)
  334. {
  335. tls_pmu_timer0_isr_register((tls_pmu_irq_callback)pmu_timer0_irq, NULL);
  336. tls_pmu_timer0_start(timeout);
  337. tls_pmu_standby_start();
  338. return;
  339. }
  340. //void delay_1us(unsigned int time);
  341. void luat_timer_us_delay(size_t time)
  342. {
  343. if(time<=0)
  344. return;
  345. volatile unsigned int value=1;
  346. clk_div_reg clk_div;
  347. clk_div.w = tls_reg_read32(HR_CLK_DIV_CTL);
  348. switch (clk_div.b.CPU)
  349. {
  350. case CPU_CLK_240M:
  351. value = 341*time/10 - 30;
  352. break;
  353. case CPU_CLK_160M:
  354. value = 227*time/10 - 22;
  355. break;
  356. case CPU_CLK_80M:
  357. value = 113*time/10;
  358. if(value>=22) value -= 22;
  359. break;
  360. case CPU_CLK_40M:
  361. value = 562*time/100;
  362. if(value>=31) value -= 31;
  363. break;
  364. default:
  365. value = time/2;
  366. break;
  367. }
  368. if(value<=1)
  369. return;
  370. while(value--)
  371. {
  372. __NOP();
  373. }
  374. }
  375. u32 cpu_sr = 0;
  376. void luat_os_entry_cri(void) {
  377. cpu_sr = tls_os_set_critical();
  378. }
  379. void luat_os_exit_cri(void) {
  380. tls_os_release_critical(cpu_sr);
  381. }
  382. void luat_os_irq_disable(uint8_t IRQ_Type) {
  383. tls_irq_disable(IRQ_Type);
  384. }
  385. void luat_os_irq_enable(uint8_t IRQ_Type) {
  386. tls_irq_enable(IRQ_Type);
  387. }
  388. #ifdef LUAT_USE_LVGL
  389. #include "lvgl.h"
  390. #endif
  391. void vApplicationTickHook( void ) {
  392. #ifdef LUAT_USE_LVGL
  393. lv_tick_inc(1000 / configTICK_RATE_HZ);
  394. #endif
  395. }
  396. //-------------- cjson 需要这个函数
  397. int strncasecmp ( const char* s1, const char* s2, size_t len )
  398. {
  399. register unsigned int x2;
  400. register unsigned int x1;
  401. register const char* end = s1 + len;
  402. while (1)
  403. {
  404. if ((s1 >= end) )
  405. return 0;
  406. x2 = *s2 - 'A'; if ((x2 < 26u)) x2 += 32;
  407. x1 = *s1 - 'A'; if ((x1 < 26u)) x1 += 32;
  408. s1++; s2++;
  409. if (x2 != x1)
  410. break;
  411. if (x1 == (unsigned int)-'A')
  412. break;
  413. }
  414. return x1 - x2;
  415. }
  416. void luat_debug_assert(const char *fun_name, unsigned int line_no, const char *fmt, ...){
  417. printf("luat_debug_assert: %s:%d ", fun_name, line_no);
  418. va_list args;
  419. va_start(args, fmt);
  420. vprintf(fmt, args);
  421. va_end(args);
  422. printf("\n");
  423. while (1){};
  424. }
  425. //--------------