luat_base_win32.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. {"sfd", luaopen_sfd},
  39. {"lfs2", luaopen_lfs2},
  40. {"gpio", luaopen_gpio},
  41. {NULL, NULL}
  42. };
  43. // 按不同的rtconfig加载不同的库函数
  44. void luat_openlibs(lua_State *L) {
  45. // 初始化队列服务
  46. luat_msgbus_init();
  47. //print_list_mem("done>luat_msgbus_init");
  48. // 加载系统库
  49. const luaL_Reg *lib;
  50. /* "require" functions from 'loadedlibs' and set results to global table */
  51. for (lib = loadedlibs; lib->func; lib++) {
  52. luaL_requiref(L, lib->name, lib->func, 1);
  53. lua_pop(L, 1); /* remove lib */
  54. //extern void print_list_mem(const char* name);
  55. //print_list_mem(lib->name);
  56. }
  57. }
  58. void luat_os_reboot(int code) {
  59. exit(code);
  60. }
  61. const char* luat_os_bsp(void) {
  62. #ifdef LUA_USE_WINDOWS
  63. return "win32";
  64. #else
  65. return "posix";
  66. #endif
  67. }
  68. extern const struct luat_vfs_filesystem vfs_fs_posix;
  69. extern const struct luat_vfs_filesystem vfs_fs_luadb;
  70. #ifdef LUAT_USE_VFS_INLINE_LIB
  71. extern const char luadb_inline[];
  72. #endif
  73. int luat_fs_init(void) {
  74. #ifdef LUAT_USE_FS_VFS
  75. // vfs进行必要的初始化
  76. luat_vfs_init(NULL);
  77. // 注册vfs for posix 实现
  78. luat_vfs_reg(&vfs_fs_posix);
  79. luat_vfs_reg(&vfs_fs_luadb);
  80. luat_fs_conf_t conf = {
  81. .busname = "",
  82. .type = "posix",
  83. .filesystem = "posix",
  84. .mount_point = "", // window环境下, 需要支持任意路径的读取,不能强制要求必须是/
  85. };
  86. luat_fs_mount(&conf);
  87. #ifdef LUAT_USE_VFS_INLINE_LIB
  88. luat_fs_conf_t conf2 = {
  89. .busname = (char*)luadb_inline,
  90. .type = "luadb",
  91. .filesystem = "luadb",
  92. .mount_point = "/luadb/",
  93. };
  94. luat_fs_mount(&conf2);
  95. #endif
  96. return 0;
  97. #else
  98. return 0;
  99. #endif
  100. }
  101. void vConfigureTimerForRunTimeStats( void ) {}
  102. /** 设备进入待机模式 */
  103. void luat_os_standy(int timeout) {
  104. return; // nop
  105. }
  106. void luat_ota_reboot(int timeout_ms) {
  107. if (timeout_ms > 0)
  108. luat_timer_mdelay(timeout_ms);
  109. exit(0);
  110. }
  111. //--------------------------------------------------------------------------------
  112. // for freertos
  113. #include "FreeRTOS.h"
  114. #include "task.h"
  115. /* When configSUPPORT_STATIC_ALLOCATION is set to 1 the application writer can
  116. use a callback function to optionally provide the memory required by the idle
  117. and timer tasks. This is the stack that will be used by the timer task. It is
  118. declared here, as a global, so it can be checked by a test that is implemented
  119. in a different file. */
  120. StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
  121. /* Notes if the trace is running or not. */
  122. static BaseType_t xTraceRunning = pdTRUE;
  123. void vApplicationIdleHook( void )
  124. {
  125. #ifdef LUA_USE_WINDOWS
  126. Sleep(1);
  127. #else
  128. usleep(1000);
  129. #endif
  130. }
  131. void vAssertCalled( unsigned long ulLine, const char * const pcFileName ) {
  132. printf( "ASSERT! Line %d, file %s\r\n", ulLine, pcFileName );
  133. exit(255);
  134. }
  135. void vApplicationDaemonTaskStartupHook( void )
  136. {
  137. /* This function will be called once only, when the daemon task starts to
  138. execute (sometimes called the timer task). This is useful if the
  139. application includes initialisation code that would benefit from executing
  140. after the scheduler has been started. */
  141. }
  142. void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
  143. {
  144. ( void ) pcTaskName;
  145. ( void ) pxTask;
  146. /* Run time stack overflow checking is performed if
  147. configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
  148. function is called if a stack overflow is detected. This function is
  149. provided as an example only as stack overflow checking does not function
  150. when running the FreeRTOS Windows port. */
  151. vAssertCalled( __LINE__, __FILE__ );
  152. }
  153. /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
  154. implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
  155. used by the Idle task. */
  156. void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
  157. {
  158. /* If the buffers to be provided to the Idle task are declared inside this
  159. function then they must be declared static - otherwise they will be allocated on
  160. the stack and so not exists after this function exits. */
  161. static StaticTask_t xIdleTaskTCB;
  162. static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
  163. /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
  164. state will be stored. */
  165. *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
  166. /* Pass out the array that will be used as the Idle task's stack. */
  167. *ppxIdleTaskStackBuffer = uxIdleTaskStack;
  168. /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
  169. Note that, as the array is necessarily of type StackType_t,
  170. configMINIMAL_STACK_SIZE is specified in words, not bytes. */
  171. *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  172. }
  173. /*-----------------------------------------------------------*/
  174. /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
  175. application must provide an implementation of vApplicationGetTimerTaskMemory()
  176. to provide the memory that is used by the Timer service task. */
  177. void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
  178. {
  179. /* If the buffers to be provided to the Timer task are declared inside this
  180. function then they must be declared static - otherwise they will be allocated on
  181. the stack and so not exists after this function exits. */
  182. static StaticTask_t xTimerTaskTCB;
  183. /* Pass out a pointer to the StaticTask_t structure in which the Timer
  184. task's state will be stored. */
  185. *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
  186. /* Pass out the array that will be used as the Timer task's stack. */
  187. *ppxTimerTaskStackBuffer = uxTimerTaskStack;
  188. /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
  189. Note that, as the array is necessarily of type StackType_t,
  190. configMINIMAL_STACK_SIZE is specified in words, not bytes. */
  191. *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
  192. }
  193. /*-----------------------------------------------------------*/
  194. void vApplicationMallocFailedHook( void )
  195. {
  196. /* vApplicationMallocFailedHook() will only be called if
  197. configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
  198. function that will get called if a call to pvPortMalloc() fails.
  199. pvPortMalloc() is called internally by the kernel whenever a task, queue,
  200. timer or semaphore is created. It is also called by various parts of the
  201. demo application. If heap_1.c, heap_2.c or heap_4.c is being used, then the
  202. size of the heap available to pvPortMalloc() is defined by
  203. configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()
  204. API function can be used to query the size of free heap space that remains
  205. (although it does not provide information on how the remaining heap might be
  206. fragmented). See http://www.freertos.org/a00111.html for more
  207. information. */
  208. vAssertCalled( __LINE__, __FILE__ );
  209. }
  210. /*-----------------------------------------------------------*/