luat_base_air101.c 8.2 KB

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