luat_base_win32.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "luat_base.h"
  2. #include "luat_msgbus.h"
  3. #include "luat_fs.h"
  4. #include <stdlib.h>
  5. LUAMOD_API int luaopen_win32( lua_State *L );
  6. int luaopen_lfs(lua_State * L);
  7. int luaopen_rs232_core(lua_State * L);
  8. static const luaL_Reg loadedlibs[] = {
  9. {"_G", luaopen_base}, // _G
  10. {LUA_LOADLIBNAME, luaopen_package}, // require
  11. {LUA_COLIBNAME, luaopen_coroutine}, // coroutine协程库
  12. {LUA_TABLIBNAME, luaopen_table}, // table库,操作table类型的数据结构
  13. {LUA_IOLIBNAME, luaopen_io}, // io库,操作文件
  14. {LUA_OSLIBNAME, luaopen_os}, // os库,已精简
  15. {LUA_STRLIBNAME, luaopen_string}, // string库,字符串操作
  16. {LUA_MATHLIBNAME, luaopen_math}, // math 数值计算
  17. // {LUA_UTF8LIBNAME, luaopen_utf8},
  18. {LUA_DBLIBNAME, luaopen_debug}, // debug库,已精简
  19. #if defined(LUA_COMPAT_BITLIB)
  20. {LUA_BITLIBNAME, luaopen_bit32}, // 不太可能启用
  21. #endif
  22. {"rtos", luaopen_rtos}, // rtos底层库, 核心功能是队列和定时器
  23. {"log", luaopen_log}, // 日志库
  24. {"timer", luaopen_timer}, // 延时库
  25. {"pack", luaopen_pack}, // pack.pack/pack.unpack
  26. {"json", luaopen_cjson}, // json
  27. {"win32", luaopen_win32}, // windows 32 tools
  28. {"zbuff", luaopen_zbuff}, //
  29. {"mqttcore", luaopen_mqttcore}, //
  30. {"libcoap", luaopen_libcoap}, //
  31. #ifdef LUA_USE_WINDOWS
  32. {"lfs", luaopen_lfs}, //
  33. {"rs232.core", luaopen_rs232_core},
  34. #endif
  35. {"crypto", luaopen_crypto},
  36. {NULL, NULL}
  37. };
  38. // 按不同的rtconfig加载不同的库函数
  39. void luat_openlibs(lua_State *L) {
  40. // 初始化队列服务
  41. luat_msgbus_init();
  42. //print_list_mem("done>luat_msgbus_init");
  43. // 加载系统库
  44. const luaL_Reg *lib;
  45. /* "require" functions from 'loadedlibs' and set results to global table */
  46. for (lib = loadedlibs; lib->func; lib++) {
  47. luaL_requiref(L, lib->name, lib->func, 1);
  48. lua_pop(L, 1); /* remove lib */
  49. //extern void print_list_mem(const char* name);
  50. //print_list_mem(lib->name);
  51. }
  52. }
  53. void luat_os_reboot(int code) {
  54. exit(code);
  55. }
  56. const char* luat_os_bsp(void) {
  57. #ifdef LUA_USE_WINDOWS
  58. return "win32";
  59. #else
  60. return "posix";
  61. #endif
  62. }
  63. extern const struct luat_vfs_filesystem vfs_fs_posix;
  64. extern const struct luat_vfs_filesystem vfs_fs_luadb;
  65. #ifdef LUAT_USE_VFS_INLINE_LIB
  66. extern const char luadb_inline[];
  67. #endif
  68. int luat_fs_init(void) {
  69. #ifdef LUAT_USE_FS_VFS
  70. // vfs进行必要的初始化
  71. luat_vfs_init(NULL);
  72. // 注册vfs for posix 实现
  73. luat_vfs_reg(&vfs_fs_posix);
  74. luat_vfs_reg(&vfs_fs_luadb);
  75. luat_fs_conf_t conf = {
  76. .busname = "",
  77. .type = "posix",
  78. .filesystem = "posix",
  79. .mount_point = "", // window环境下, 需要支持任意路径的读取,不能强制要求必须是/
  80. };
  81. luat_fs_mount(&conf);
  82. #ifdef LUAT_USE_VFS_INLINE_LIB
  83. luat_fs_conf_t conf2 = {
  84. .busname = (char*)luadb_inline,
  85. .type = "luadb",
  86. .filesystem = "luadb",
  87. .mount_point = "/luadb/",
  88. };
  89. luat_fs_mount(&conf2);
  90. #endif
  91. return 0;
  92. #else
  93. return 0;
  94. #endif
  95. }
  96. void vConfigureTimerForRunTimeStats( void ) {}
  97. /** 设备进入待机模式 */
  98. void luat_os_standy(int timeout) {
  99. return; // nop
  100. }
  101. //--------------------------------------------------------------------------------
  102. // for freertos
  103. #include "FreeRTOS.h"
  104. #include "task.h"
  105. /* When configSUPPORT_STATIC_ALLOCATION is set to 1 the application writer can
  106. use a callback function to optionally provide the memory required by the idle
  107. and timer tasks. This is the stack that will be used by the timer task. It is
  108. declared here, as a global, so it can be checked by a test that is implemented
  109. in a different file. */
  110. StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
  111. /* Notes if the trace is running or not. */
  112. static BaseType_t xTraceRunning = pdTRUE;
  113. void vApplicationIdleHook( void )
  114. {
  115. #ifdef LUA_USE_WINDOWS
  116. Sleep(1);
  117. #else
  118. usleep(1000);
  119. #endif
  120. }
  121. void vAssertCalled( unsigned long ulLine, const char * const pcFileName ) {
  122. printf( "ASSERT! Line %d, file %s\r\n", ulLine, pcFileName );
  123. exit(255);
  124. }
  125. void vApplicationDaemonTaskStartupHook( void )
  126. {
  127. /* This function will be called once only, when the daemon task starts to
  128. execute (sometimes called the timer task). This is useful if the
  129. application includes initialisation code that would benefit from executing
  130. after the scheduler has been started. */
  131. }
  132. void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
  133. {
  134. ( void ) pcTaskName;
  135. ( void ) pxTask;
  136. /* Run time stack overflow checking is performed if
  137. configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
  138. function is called if a stack overflow is detected. This function is
  139. provided as an example only as stack overflow checking does not function
  140. when running the FreeRTOS Windows port. */
  141. vAssertCalled( __LINE__, __FILE__ );
  142. }
  143. /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
  144. implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
  145. used by the Idle task. */
  146. void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
  147. {
  148. /* If the buffers to be provided to the Idle task are declared inside this
  149. function then they must be declared static - otherwise they will be allocated on
  150. the stack and so not exists after this function exits. */
  151. static StaticTask_t xIdleTaskTCB;
  152. static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
  153. /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
  154. state will be stored. */
  155. *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
  156. /* Pass out the array that will be used as the Idle task's stack. */
  157. *ppxIdleTaskStackBuffer = uxIdleTaskStack;
  158. /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
  159. Note that, as the array is necessarily of type StackType_t,
  160. configMINIMAL_STACK_SIZE is specified in words, not bytes. */
  161. *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  162. }
  163. /*-----------------------------------------------------------*/
  164. /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
  165. application must provide an implementation of vApplicationGetTimerTaskMemory()
  166. to provide the memory that is used by the Timer service task. */
  167. void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
  168. {
  169. /* If the buffers to be provided to the Timer task are declared inside this
  170. function then they must be declared static - otherwise they will be allocated on
  171. the stack and so not exists after this function exits. */
  172. static StaticTask_t xTimerTaskTCB;
  173. /* Pass out a pointer to the StaticTask_t structure in which the Timer
  174. task's state will be stored. */
  175. *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
  176. /* Pass out the array that will be used as the Timer task's stack. */
  177. *ppxTimerTaskStackBuffer = uxTimerTaskStack;
  178. /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
  179. Note that, as the array is necessarily of type StackType_t,
  180. configMINIMAL_STACK_SIZE is specified in words, not bytes. */
  181. *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
  182. }
  183. /*-----------------------------------------------------------*/
  184. void vApplicationMallocFailedHook( void )
  185. {
  186. /* vApplicationMallocFailedHook() will only be called if
  187. configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
  188. function that will get called if a call to pvPortMalloc() fails.
  189. pvPortMalloc() is called internally by the kernel whenever a task, queue,
  190. timer or semaphore is created. It is also called by various parts of the
  191. demo application. If heap_1.c, heap_2.c or heap_4.c is being used, then the
  192. size of the heap available to pvPortMalloc() is defined by
  193. configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()
  194. API function can be used to query the size of free heap space that remains
  195. (although it does not provide information on how the remaining heap might be
  196. fragmented). See http://www.freertos.org/a00111.html for more
  197. information. */
  198. vAssertCalled( __LINE__, __FILE__ );
  199. }
  200. /*-----------------------------------------------------------*/