luat_rtt_base.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "luat_base.h"
  2. #include "luat_malloc.h"
  3. #include "rtthread.h"
  4. #include "stdio.h"
  5. #include "luat_msgbus.h"
  6. #include "rthw.h"
  7. #include "vsprintf.h"
  8. #define DBG_TAG "rtt.base"
  9. #define DBG_LVL DBG_INFO
  10. #include <rtdbg.h>
  11. int l_sprintf(char *buf, int32_t size, const char *fmt, ...) {
  12. rt_int32_t n;
  13. va_list args;
  14. va_start(args, fmt);
  15. n = custom_vsprintf(buf, /*size,*/ fmt, args);
  16. va_end(args);
  17. return n;
  18. }
  19. // 打印内存状态
  20. void print_list_mem(const char* name) {
  21. #if (DBG_LVL <= DBG_DEBUG)
  22. LOG_I("check memory status, key=%s", name);
  23. list_mem();
  24. #endif
  25. }
  26. // fix for mled加密库
  27. // rtt的方法名称变了. rt_hwcrypto_dev_dufault --> rt_hwcrypto_dev_default
  28. #ifdef RT_USING_HWCRYPTO
  29. #include <hwcrypto.h>
  30. RT_WEAK struct rt_hwcrypto_device *rt_hwcrypto_dev_dufault(void) {
  31. return rt_hwcrypto_dev_default();
  32. }
  33. #endif
  34. // 文件系统初始化函数, 做个虚拟的
  35. RT_WEAK void luat_fs_init() {}
  36. static const luaL_Reg loadedlibs[] = {
  37. {"_G", luaopen_base}, // _G
  38. {LUA_LOADLIBNAME, luaopen_package}, // require
  39. {LUA_COLIBNAME, luaopen_coroutine}, // coroutine协程库
  40. {LUA_TABLIBNAME, luaopen_table}, // table库,操作table类型的数据结构
  41. {LUA_IOLIBNAME, luaopen_io}, // io库,操作文件
  42. {LUA_OSLIBNAME, luaopen_os}, // os库,已精简
  43. {LUA_STRLIBNAME, luaopen_string}, // string库,字符串操作
  44. {LUA_MATHLIBNAME, luaopen_math}, // math 数值计算
  45. // {LUA_UTF8LIBNAME, luaopen_utf8},
  46. {LUA_DBLIBNAME, luaopen_debug}, // debug库,已精简
  47. #if defined(LUA_COMPAT_BITLIB)
  48. {LUA_BITLIBNAME, luaopen_bit32}, // 不太可能启用
  49. #endif
  50. // 往下是RTT环境下加载的库
  51. {"rtos", luaopen_rtos}, // rtos底层库, 核心功能是队列和定时器
  52. {"log", luaopen_log}, // 日志库
  53. {"timer", luaopen_timer}, // 延时库
  54. {"json", luaopen_cjson}, // json的序列化和反序列化
  55. {"pack", luaopen_pack}, // pack.pack/pack.unpack
  56. {"uart", luaopen_uart}, // 串口操作
  57. {"mqttcore",luaopen_mqttcore}, // MQTT 协议封装
  58. // {"utest", luaopen_utest},
  59. #ifdef RT_USING_PIN
  60. {"gpio", luaopen_gpio}, // GPIO脚的操作
  61. {"sensor", luaopen_sensor}, // 传感器操作
  62. #endif
  63. #ifdef RT_USING_WIFI
  64. {"wlan", luaopen_wlan}, // wlan/wifi联网操作
  65. #endif
  66. #ifdef SAL_USING_POSIX
  67. {"socket", luaopen_socket}, // 套接字操作
  68. #endif
  69. #ifdef RT_USING_I2C
  70. {"i2c", luaopen_i2c}, // I2C操作
  71. #endif
  72. #ifdef RT_USING_SPI
  73. {"spi", luaopen_spi}, // SPI操作
  74. #endif
  75. #ifdef PKG_USING_U8G2
  76. {"disp", luaopen_disp}, // 显示屏
  77. #endif
  78. {NULL, NULL}
  79. };
  80. // 按不同的rtconfig加载不同的库函数
  81. void luat_openlibs(lua_State *L) {
  82. // 初始化队列服务
  83. luat_msgbus_init();
  84. print_list_mem("done>luat_msgbus_init");
  85. // 加载系统库
  86. const luaL_Reg *lib;
  87. /* "require" functions from 'loadedlibs' and set results to global table */
  88. for (lib = loadedlibs; lib->func; lib++) {
  89. luaL_requiref(L, lib->name, lib->func, 1);
  90. lua_pop(L, 1); /* remove lib */
  91. //extern void print_list_mem(const char* name);
  92. //print_list_mem(lib->name);
  93. }
  94. }
  95. void luat_os_reboot(int code) {
  96. rt_hw_cpu_reset();
  97. }
  98. void sys_start_standby(int ms);
  99. void luat_os_standy(int timeout) {
  100. #ifdef BSP_USING_WM_LIBRARIES
  101. #ifdef BSP_USING_STANDBY
  102. sys_start_standby(timeout);
  103. #endif
  104. #endif
  105. }
  106. const char* luat_os_bsp(void) {
  107. #ifdef BSP_USING_WM_LIBRARIES
  108. return "w60x";
  109. #else
  110. #ifdef STM32L1
  111. return "stm32";
  112. #else
  113. return "_";
  114. #endif
  115. #endif
  116. }
  117. RT_WEAK void rt_hw_us_delay(rt_uint32_t us)
  118. {
  119. ; // nop
  120. }
  121. RT_WEAK void rt_hw_cpu_reset() {
  122. ; // nop
  123. }
  124. // watchdog
  125. #ifdef BSP_USING_WDT
  126. #include <rtdevice.h>
  127. static rt_uint32_t wdg_timeout = 15; /* 溢出时间,单位:秒*/
  128. static rt_device_t wdg_dev; /* 看门狗设备句柄 */
  129. static int wdt_chk(void) {
  130. wdg_dev = rt_device_find("wdt");
  131. if (wdg_dev == RT_NULL) {
  132. wdg_dev = rt_device_find("wdg");
  133. if (wdg_dev == RT_NULL) {
  134. LOG_I("watchdog is miss");
  135. return RT_EOK;
  136. }
  137. }
  138. LOG_I("watchdog found, enable it");
  139. rt_device_init(wdg_dev);
  140. rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, (void *)wdg_timeout);
  141. rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_START, (void *)wdg_timeout);
  142. //rt_thread_idle_sethook(idle_hook);
  143. return RT_EOK;
  144. }
  145. INIT_DEVICE_EXPORT(wdt_chk);
  146. #define THREAD_PRIORITY 25
  147. #define THREAD_TIMESLICE 5
  148. ALIGN(RT_ALIGN_SIZE)
  149. static char rtt_wdt_stack[256];
  150. static struct rt_thread rtt_wdt;
  151. static int rtt_wdt_feed(void* args) {
  152. while (1) {
  153. if (wdg_dev)
  154. rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
  155. rt_thread_mdelay(5000);
  156. }
  157. return 0;
  158. }
  159. static int rtt_wdt_thread_start() {
  160. rt_thread_init(&rtt_wdt,
  161. "rtt_wdt",
  162. rtt_wdt_feed,
  163. RT_NULL,
  164. &rtt_wdt_stack[0],
  165. sizeof(rtt_wdt_stack),
  166. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  167. rt_thread_startup(&rtt_wdt);
  168. }
  169. INIT_COMPONENT_EXPORT(rtt_wdt_thread_start);
  170. #endif