luat_base_win32.c 8.8 KB

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