luat_rtt_base.c 5.4 KB

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