luat_base_win32.c 7.0 KB

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