luat_base_air101.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #if defined(LUA_COMPAT_BITLIB)
  38. {LUA_BITLIBNAME, luaopen_bit32}, // 不太可能启用
  39. #endif
  40. // 往下是LuatOS定制的库, 如需精简请仔细测试
  41. //----------------------------------------------------------------------
  42. // 核心支撑库, 不可禁用!!
  43. {"rtos", luaopen_rtos}, // rtos底层库, 核心功能是队列和定时器
  44. {"log", luaopen_log}, // 日志库
  45. {"timer", luaopen_timer}, // 延时库
  46. //-----------------------------------------------------------------------
  47. // 设备驱动类, 可按实际情况删减. 即使最精简的固件, 也强烈建议保留uart库
  48. #ifdef LUAT_USE_UART
  49. {"uart", luaopen_uart}, // 串口操作
  50. #endif
  51. #ifdef LUAT_USE_GPIO
  52. {"gpio", luaopen_gpio}, // GPIO脚的操作
  53. #endif
  54. #ifdef LUAT_USE_I2C
  55. {"i2c", luaopen_i2c}, // I2C操作
  56. #endif
  57. #ifdef LUAT_USE_SPI
  58. {"spi", luaopen_spi}, // SPI操作
  59. #endif
  60. #ifdef LUAT_USE_ADC
  61. {"adc", luaopen_adc}, // ADC模块
  62. #endif
  63. #ifdef LUAT_USE_SDIO
  64. {"sdio", luaopen_sdio}, // SDIO模块
  65. #endif
  66. #ifdef LUAT_USE_PWM
  67. {"pwm", luaopen_pwm}, // PWM模块
  68. #endif
  69. #ifdef LUAT_USE_WDT
  70. {"wdt", luaopen_wdt}, // watchdog模块
  71. #endif
  72. #ifdef LUAT_USE_PM
  73. {"pm", luaopen_pm}, // 电源管理模块
  74. #endif
  75. #ifdef LUAT_USE_MCU
  76. {"mcu", luaopen_mcu}, // MCU特有的一些操作
  77. #endif
  78. #ifdef LUAT_USE_HWTIMER
  79. {"hwtimer", luaopen_hwtimer}, // 硬件定时器
  80. #endif
  81. #ifdef LUAT_USE_RTC
  82. {"rtc", luaopen_rtc}, // 实时时钟
  83. #endif
  84. //-----------------------------------------------------------------------
  85. // 工具库, 按需选用
  86. #ifdef LUAT_USE_CRYPTO
  87. {"crypto",luaopen_crypto}, // 加密和hash模块
  88. #endif
  89. #ifdef LUAT_USE_CJSON
  90. {"json", luaopen_cjson}, // json的序列化和反序列化
  91. #endif
  92. #ifdef LUAT_USE_ZBUFF
  93. {"zbuff", luaopen_zbuff}, // 像C语言语言操作内存块
  94. #endif
  95. #ifdef LUAT_USE_PACK
  96. {"pack", luaopen_pack}, // pack.pack/pack.unpack
  97. #endif
  98. // {"mqttcore",luaopen_mqttcore}, // MQTT 协议封装
  99. // {"libcoap", luaopen_libcoap}, // 处理COAP消息
  100. #ifdef LUAT_USE_GNSS
  101. {"libgnss", luaopen_libgnss}, // 处理GNSS定位数据
  102. #endif
  103. #ifdef LUAT_USE_FS
  104. {"fs", luaopen_fs}, // 文件系统库,在io库之外再提供一些方法
  105. #endif
  106. #ifdef LUAT_USE_SENSOR
  107. {"sensor", luaopen_sensor}, // 传感器库,支持DS18B20
  108. #endif
  109. #ifdef LUAT_USE_SFUD
  110. {"sfud", luaopen_sfud}, // sfud
  111. #endif
  112. #ifdef LUAT_USE_DISP
  113. {"disp", luaopen_disp}, // OLED显示模块,支持SSD1306
  114. #endif
  115. #ifdef LUAT_USE_U8G2
  116. {"u8g2", luaopen_u8g2}, // u8g2
  117. #endif
  118. #ifdef LUAT_USE_EINK
  119. {"eink", luaopen_eink}, // 电子墨水屏,试验阶段
  120. #endif
  121. #ifdef LUAT_USE_LVGL
  122. #ifndef LUAT_USE_LCD
  123. #define LUAT_USE_LCD
  124. #endif
  125. {"lvgl", luaopen_lvgl},
  126. #endif
  127. #ifdef LUAT_USE_LCD
  128. {"lcd", luaopen_lcd},
  129. #endif
  130. #ifdef LUAT_USE_STATEM
  131. {"statem", luaopen_statem},
  132. #endif
  133. #ifdef LUAT_USE_GTFONT
  134. {"gtfont", luaopen_gtfont},
  135. #endif
  136. #ifdef LUAT_USE_NIMBLE
  137. {"nimble", luaopen_nimble},
  138. #endif
  139. {NULL, NULL}
  140. };
  141. // 按不同的rtconfig加载不同的库函数
  142. void luat_openlibs(lua_State *L) {
  143. // 初始化队列服务
  144. luat_msgbus_init();
  145. // print_list_mem("done>luat_msgbus_init");
  146. // 加载系统库
  147. const luaL_Reg *lib;
  148. /* "require" functions from 'loadedlibs' and set results to global table */
  149. for (lib = loadedlibs; lib->func; lib++) {
  150. luaL_requiref(L, lib->name, lib->func, 1);
  151. lua_pop(L, 1); /* remove lib */
  152. //extern void print_list_mem(const char* name);
  153. //print_list_mem(lib->name);
  154. }
  155. }
  156. #include "FreeRTOS.h"
  157. #include "task.h"
  158. #if configUSE_HEAP3
  159. extern size_t xTotalHeapSize;
  160. extern size_t xFreeBytesRemaining;
  161. extern size_t xFreeBytesMin;
  162. #endif
  163. void luat_meminfo_sys(size_t* total, size_t* used, size_t* max_used)
  164. {
  165. #if configUSE_HEAP3
  166. *used = xTotalHeapSize - xFreeBytesRemaining;
  167. *max_used = xTotalHeapSize - xFreeBytesMin;
  168. *total = xTotalHeapSize;
  169. #else
  170. *used = configTOTAL_HEAP_SIZE - xPortGetFreeHeapSize();
  171. *max_used = *used;
  172. *total = configTOTAL_HEAP_SIZE;
  173. #endif
  174. }
  175. const char* luat_os_bsp(void)
  176. {
  177. #ifdef AIR103
  178. return "air103";
  179. #else
  180. return "air101";
  181. #endif
  182. }
  183. void luat_os_reboot(int code)
  184. {
  185. tls_sys_reset();
  186. }
  187. static void pmu_timer0_irq(u8 *arg){}
  188. void luat_os_standy(int timeout)
  189. {
  190. tls_pmu_timer0_isr_register((tls_pmu_irq_callback)pmu_timer0_irq, NULL);
  191. tls_pmu_timer0_start(timeout);
  192. tls_pmu_standby_start();
  193. return;
  194. }
  195. //void delay_1us(unsigned int time);
  196. void luat_timer_us_delay(size_t time)
  197. {
  198. if(time<=0)
  199. return;
  200. volatile unsigned int value=1;
  201. clk_div_reg clk_div;
  202. clk_div.w = tls_reg_read32(HR_CLK_DIV_CTL);
  203. switch (clk_div.b.CPU)
  204. {
  205. case CPU_CLK_240M:
  206. value = 341*time/10 - 30;
  207. break;
  208. case CPU_CLK_160M:
  209. value = 227*time/10 - 22;
  210. break;
  211. case CPU_CLK_80M:
  212. value = 113*time/10;
  213. if(value>=22) value -= 22;
  214. break;
  215. case CPU_CLK_40M:
  216. value = 562*time/100;
  217. if(value>=31) value -= 31;
  218. break;
  219. default:
  220. value = time/2;
  221. break;
  222. }
  223. if(value<=1)
  224. return;
  225. while(value--)
  226. {
  227. __NOP();
  228. }
  229. }
  230. u32 cpu_sr = 0;
  231. void luat_os_entry_cri(void) {
  232. cpu_sr = tls_os_set_critical();
  233. }
  234. void luat_os_exit_cri(void) {
  235. tls_os_release_critical(cpu_sr);
  236. }
  237. #ifdef LUAT_USE_LVGL
  238. #include "lvgl.h"
  239. #endif
  240. void vApplicationTickHook( void ) {
  241. #ifdef LUAT_USE_LVGL
  242. lv_tick_inc(1000 / configTICK_RATE_HZ);
  243. #endif
  244. }
  245. //-------------- cjson 需要这个函数
  246. int strncasecmp ( const char* s1, const char* s2, size_t len )
  247. {
  248. register unsigned int x2;
  249. register unsigned int x1;
  250. register const char* end = s1 + len;
  251. while (1)
  252. {
  253. if ((s1 >= end) )
  254. return 0;
  255. x2 = *s2 - 'A'; if ((x2 < 26u)) x2 += 32;
  256. x1 = *s1 - 'A'; if ((x1 < 26u)) x1 += 32;
  257. s1++; s2++;
  258. if (x2 != x1)
  259. break;
  260. if (x1 == (unsigned int)-'A')
  261. break;
  262. }
  263. return x1 - x2;
  264. }
  265. //--------------