main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "wm_include.h"
  2. #include "wm_gpio_afsel.h"
  3. #include "wm_psram.h"
  4. #include "wm_internal_flash.h"
  5. #ifdef __LUATOS__
  6. #include "string.h"
  7. #include "luat_fs.h"
  8. #include "bget.h"
  9. #include "luat_base.h"
  10. #include "luat_msgbus.h"
  11. #include "luat_pm.h"
  12. #include <string.h>
  13. #include "wm_irq.h"
  14. #include "tls_sys.h"
  15. #include "wm_ram_config.h"
  16. #include "wm_internal_flash.h"
  17. #include "wm_psram.h"
  18. #include "FreeRTOS.h"
  19. #define LUAT_LOG_TAG "main"
  20. #include "luat_log.h"
  21. #ifndef LUAT_HEAP_SIZE
  22. #ifdef LUAT_USE_LVGL
  23. #define LUAT_HEAP_SIZE (128+48)*1024
  24. #else
  25. /*非LVGL项目并不需要太多的系统内存*/
  26. #define LUAT_HEAP_SIZE (128+80)*1024
  27. #endif
  28. #endif
  29. #if (LUAT_HEAP_SIZE > 128*1024)
  30. static uint8_t __attribute__((aligned(4))) heap_ext[LUAT_HEAP_SIZE - 128*1024] = {0};
  31. #endif
  32. static void luat_start(void *sdata){
  33. #ifdef LUAT_USE_PSRAM
  34. bpool((void*)0x30000000, 4*1024*1024);
  35. #else
  36. bpool((void*)0x20028000, 128*1024);
  37. #if (LUAT_HEAP_SIZE > 128*1024)
  38. bpool((void*)heap_ext, LUAT_HEAP_SIZE - 128*1024);
  39. #endif
  40. #endif
  41. luat_main();
  42. }
  43. #ifdef LUAT_USE_LVGL
  44. #include "lvgl.h"
  45. // static uint8_t lvgl_called = 0;
  46. static int luat_lvgl_cb(lua_State *L, void* ptr) {
  47. lv_task_handler();
  48. // lvgl_called = 0;
  49. return 0;
  50. }
  51. static void lvgl_timer_cb(void *ptmr, void *parg) {
  52. // if (lvgl_called)
  53. // return;
  54. rtos_msg_t msg = {0};
  55. msg.handler = luat_lvgl_cb;
  56. luat_msgbus_put(&msg, 0);
  57. // lvgl_called = 1;
  58. }
  59. // #define LVGL_TASK_SIZE 512
  60. // static OS_STK __attribute__((aligned(4))) LVGLTaskStk[LVGL_TASK_SIZE] = {0};
  61. #endif
  62. #define TASK_START_STK_SIZE 4096
  63. static OS_STK __attribute__((aligned(4))) TaskStartStk[TASK_START_STK_SIZE] = {0};
  64. #endif
  65. uint32_t rst_sta = 0;
  66. #ifdef __LUATOS__
  67. extern unsigned int TLS_FLASH_PARAM_DEFAULT ;
  68. extern unsigned int TLS_FLASH_PARAM1_ADDR ;
  69. extern unsigned int TLS_FLASH_PARAM2_ADDR ;
  70. extern unsigned int TLS_FLASH_PARAM_RESTORE_ADDR ;
  71. extern unsigned int TLS_FLASH_OTA_FLAG_ADDR ;
  72. extern unsigned int TLS_FLASH_END_ADDR ;
  73. #endif
  74. void UserMain(void){
  75. char unique_id [18] = {0};
  76. tls_uart_options_t opt;
  77. opt.baudrate = UART_BAUDRATE_B921600;
  78. opt.charlength = TLS_UART_CHSIZE_8BIT;
  79. opt.flow_ctrl = TLS_UART_FLOW_CTRL_NONE;
  80. opt.paritytype = TLS_UART_PMODE_DISABLED;
  81. opt.stopbits = TLS_UART_ONE_STOPBITS;
  82. tls_uart_port_init(0, &opt, 0);
  83. // 读取开机原因
  84. rst_sta = tls_reg_read32(HR_CLK_RST_STA);
  85. tls_reg_write32(HR_CLK_RST_STA, 0xFF);
  86. #ifdef LUAT_USE_SHELL
  87. luat_shell_poweron(0);
  88. #endif
  89. #ifdef __LUATOS__
  90. tls_fls_read_unique_id(unique_id);
  91. if (unique_id[1] == 0x10){
  92. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  93. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  94. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  95. unique_id[10], unique_id[11], unique_id[12], unique_id[13], unique_id[14],unique_id[15],
  96. luat_os_bsp());
  97. }else{
  98. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  99. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  100. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  101. luat_os_bsp());
  102. }
  103. #endif
  104. #ifdef AIR103
  105. TLS_FLASH_PARAM_DEFAULT = (0x80FB000UL);
  106. TLS_FLASH_PARAM1_ADDR = (0x80FC000UL);
  107. TLS_FLASH_PARAM2_ADDR = (0x80FD000UL);
  108. TLS_FLASH_PARAM_RESTORE_ADDR = (0x80FE000UL);
  109. TLS_FLASH_OTA_FLAG_ADDR = (0x80FF000UL);
  110. TLS_FLASH_END_ADDR = (0x80FFFFFUL);
  111. #endif
  112. #ifdef LUAT_USE_NIMBLE
  113. // TODO 注意, 除了启用LUAT_USE_NIMBTE外
  114. // 1. 修改FreeRTOSConfig.h的configTICK_RATE_HZ为500, 并重新make lib
  115. // 2. 若修改libblehost.a相关代码,需要手工复制bin目录下的文件,拷贝到lib目录.
  116. tls_ft_param_init();
  117. tls_param_load_factory_default();
  118. tls_param_init(); /*add param to init sysparam_lock sem*/
  119. #endif
  120. // 如要使用psram,启用以下代码,并重新编译sdk
  121. #ifdef LUAT_USE_PSRAM
  122. // 首先, 初始化psram相关引脚
  123. wm_psram_config(1);
  124. // 然后初始化psram的寄存器
  125. psram_init(PSRAM_QPI);
  126. //uint8_t* psram_ptr = (uint8_t*)(PSRAM_ADDR_START);
  127. #endif
  128. #ifdef __LUATOS__
  129. #ifdef LUAT_USE_LVGL
  130. lv_init();
  131. static tls_os_timer_t *os_timer = NULL;
  132. tls_os_timer_create(&os_timer, lvgl_timer_cb, NULL, 10/(1000 / configTICK_RATE_HZ), 1, NULL);
  133. tls_os_timer_start(os_timer);
  134. #endif
  135. tls_os_task_create(NULL, NULL,
  136. luat_start,
  137. NULL,
  138. (void *)TaskStartStk, /* task's stack start address */
  139. TASK_START_STK_SIZE * sizeof(u32), /* task's stack size, unit:byte */
  140. 31,
  141. 0);
  142. #else
  143. printf("hello word\n");
  144. while (1);
  145. #endif
  146. }
  147. #ifndef __LUATOS__
  148. // void vApplicationTickHook( void ) {}
  149. void bpool(void *buffer, long len) {}
  150. #endif