luat_rtt_base.c 5.9 KB

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