luat_base_win32.c 8.3 KB

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