main.c 5.1 KB

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