luat_rtt_base.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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(void) {}
  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. #ifdef RT_USING_HWCRYPTO
  79. {"crypto", luaopen_crypto}, // 加密和hash库
  80. #endif
  81. {NULL, NULL}
  82. };
  83. // 按不同的rtconfig加载不同的库函数
  84. void luat_openlibs(lua_State *L) {
  85. // 初始化队列服务
  86. luat_msgbus_init();
  87. print_list_mem("done>luat_msgbus_init");
  88. // 加载系统库
  89. const luaL_Reg *lib;
  90. /* "require" functions from 'loadedlibs' and set results to global table */
  91. for (lib = loadedlibs; lib->func; lib++) {
  92. luaL_requiref(L, lib->name, lib->func, 1);
  93. lua_pop(L, 1); /* remove lib */
  94. //extern void print_list_mem(const char* name);
  95. //print_list_mem(lib->name);
  96. }
  97. }
  98. void luat_os_reboot(int code) {
  99. rt_hw_cpu_reset();
  100. }
  101. void sys_start_standby(int ms);
  102. void luat_os_standy(int timeout) {
  103. #ifdef BSP_USING_WM_LIBRARIES
  104. #ifdef BSP_USING_STANDBY
  105. sys_start_standby(timeout);
  106. #endif
  107. #endif
  108. }
  109. const char* luat_os_bsp(void) {
  110. #ifdef BSP_USING_WM_LIBRARIES
  111. return "w60x";
  112. #else
  113. #ifdef STM32L1
  114. return "stm32";
  115. #else
  116. return "_";
  117. #endif
  118. #endif
  119. }
  120. RT_WEAK void rt_hw_us_delay(rt_uint32_t us)
  121. {
  122. ; // nop
  123. }
  124. RT_WEAK void rt_hw_cpu_reset() {
  125. ; // nop
  126. }
  127. // watchdog
  128. #ifdef BSP_USING_WDT
  129. #include <rtdevice.h>
  130. static rt_uint32_t wdg_timeout = 15; /* 溢出时间,单位:秒*/
  131. static rt_device_t wdg_dev; /* 看门狗设备句柄 */
  132. static int wdt_chk(void) {
  133. wdg_dev = rt_device_find("wdt");
  134. if (wdg_dev == RT_NULL) {
  135. wdg_dev = rt_device_find("wdg");
  136. if (wdg_dev == RT_NULL) {
  137. LOG_I("watchdog is miss");
  138. return RT_EOK;
  139. }
  140. }
  141. LOG_I("watchdog found, enable it");
  142. rt_device_init(wdg_dev);
  143. rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, (void *)wdg_timeout);
  144. rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_START, (void *)wdg_timeout);
  145. //rt_thread_idle_sethook(idle_hook);
  146. return RT_EOK;
  147. }
  148. INIT_DEVICE_EXPORT(wdt_chk);
  149. #define THREAD_PRIORITY 25
  150. #define THREAD_TIMESLICE 5
  151. ALIGN(RT_ALIGN_SIZE)
  152. static char rtt_wdt_stack[256];
  153. static struct rt_thread rtt_wdt;
  154. static int rtt_wdt_feed(void* args) {
  155. while (1) {
  156. if (wdg_dev)
  157. rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
  158. rt_thread_mdelay(5000);
  159. }
  160. return 0;
  161. }
  162. static int rtt_wdt_thread_start() {
  163. rt_thread_init(&rtt_wdt,
  164. "rtt_wdt",
  165. rtt_wdt_feed,
  166. RT_NULL,
  167. &rtt_wdt_stack[0],
  168. sizeof(rtt_wdt_stack),
  169. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  170. rt_thread_startup(&rtt_wdt);
  171. }
  172. INIT_COMPONENT_EXPORT(rtt_wdt_thread_start);
  173. #endif