luat_base_win32.c 8.8 KB

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