luat_base_air101.c 8.9 KB

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