main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "wm_efuse.h"
  19. #include "wm_regs.h"
  20. #include "FreeRTOS.h"
  21. #define LUAT_LOG_TAG "main"
  22. #include "luat_log.h"
  23. #ifndef LUAT_HEAP_SIZE
  24. #ifdef LUAT_USE_NIMBLE
  25. #define LUAT_HEAP_SIZE (128+16)*1024
  26. #else
  27. #define LUAT_HEAP_SIZE (128+48)*1024
  28. #endif
  29. #endif
  30. #if (LUAT_HEAP_SIZE > 128*1024)
  31. __attribute__((aligned(8))) static uint64_t heap_ext[(LUAT_HEAP_SIZE - 128*1024) / 8];
  32. #endif
  33. static void luat_start(void *sdata){
  34. // 毕竟sram还是快很多的, 优先sram吧
  35. bpool((void*)0x20028000, 128*1024);
  36. #ifdef LUAT_USE_PSRAM
  37. char test[] = {0xAA, 0xBB, 0xCC, 0xDD};
  38. char* psram_ptr = (void*)0x30010000;
  39. LLOGD("check psram ...");
  40. memcpy(psram_ptr, test, 4);
  41. if (memcmp(psram_ptr, test, 4)) {
  42. LLOGW("psram is enable, but can't access!!");
  43. }
  44. else {
  45. LLOGD("psram is ok");
  46. memset(psram_ptr, 0, 1024);
  47. // 存在psram, 加入到内存次, 就不使用系统额外的内存了.
  48. bpool(psram_ptr, 4*1024*1024); // 如果是8M内存, 改成 8也可以.
  49. luat_main();
  50. return;
  51. }
  52. #else
  53. // 暂时还是放在这里吧, 考虑改到0x20028000之前
  54. #if (LUAT_HEAP_SIZE > 128*1024)
  55. bpool((void*)heap_ext, LUAT_HEAP_SIZE - 128*1024);
  56. #endif
  57. #endif
  58. luat_main();
  59. }
  60. #ifdef LUAT_USE_LVGL
  61. #include "lvgl.h"
  62. // static uint8_t lvgl_called = 0;
  63. static uint32_t lvgl_tick_cnt;
  64. static int luat_lvgl_cb(lua_State *L, void* ptr) {
  65. if (lvgl_tick_cnt) lvgl_tick_cnt--;
  66. lv_task_handler();
  67. // lvgl_called = 0;
  68. return 0;
  69. }
  70. static void lvgl_timer_cb(void *ptmr, void *parg) {
  71. // if (lvgl_called)
  72. // return;
  73. if (lvgl_tick_cnt < 10)
  74. {
  75. lvgl_tick_cnt++;
  76. rtos_msg_t msg = {0};
  77. msg.handler = luat_lvgl_cb;
  78. luat_msgbus_put(&msg, 0);
  79. }
  80. // lvgl_called = 1;
  81. }
  82. // #define LVGL_TASK_SIZE 512
  83. // static OS_STK __attribute__((aligned(4))) LVGLTaskStk[LVGL_TASK_SIZE] = {0};
  84. #endif
  85. #define TASK_START_STK_SIZE 4096
  86. static OS_STK __attribute__((aligned(4))) TaskStartStk[TASK_START_STK_SIZE] = {0};
  87. #endif
  88. uint32_t rst_sta = 0;
  89. #ifdef __LUATOS__
  90. extern unsigned int TLS_FLASH_PARAM_DEFAULT ;
  91. extern unsigned int TLS_FLASH_PARAM1_ADDR ;
  92. extern unsigned int TLS_FLASH_PARAM2_ADDR ;
  93. extern unsigned int TLS_FLASH_PARAM_RESTORE_ADDR ;
  94. extern unsigned int TLS_FLASH_OTA_FLAG_ADDR ;
  95. extern unsigned int TLS_FLASH_END_ADDR ;
  96. #endif
  97. void UserMain(void){
  98. char unique_id [18] = {0};
  99. tls_uart_options_t opt;
  100. opt.baudrate = UART_BAUDRATE_B921600;
  101. opt.charlength = TLS_UART_CHSIZE_8BIT;
  102. opt.flow_ctrl = TLS_UART_FLOW_CTRL_NONE;
  103. opt.paritytype = TLS_UART_PMODE_DISABLED;
  104. opt.stopbits = TLS_UART_ONE_STOPBITS;
  105. tls_uart_port_init(0, &opt, 0);
  106. // 完全禁用jtag
  107. //u32 value = tls_reg_read32(HR_CLK_SEL_CTL);
  108. // printf("HR_CLK_SEL_CTL %08X\n", value);
  109. //value = value & 0x7FFF;
  110. // value = value & 0x7F00;
  111. //tls_reg_write32(HR_CLK_SEL_CTL, value);
  112. // tls_reg_read32(HR_CLK_SEL_CTL);
  113. // printf("HR_CLK_SEL_CTL %08X\n", value);
  114. // 读取开机原因
  115. rst_sta = tls_reg_read32(HR_CLK_RST_STA);
  116. tls_reg_write32(HR_CLK_RST_STA, 0xFF);
  117. #ifdef LUAT_USE_SHELL
  118. luat_shell_poweron(0);
  119. #endif
  120. #ifdef __LUATOS__
  121. tls_fls_read_unique_id(unique_id);
  122. if (unique_id[1] == 0x10){
  123. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  124. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  125. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  126. unique_id[10], unique_id[11], unique_id[12], unique_id[13], unique_id[14],unique_id[15],
  127. luat_os_bsp());
  128. }else{
  129. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  130. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  131. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  132. luat_os_bsp());
  133. }
  134. #endif
  135. #ifdef AIR103
  136. TLS_FLASH_PARAM_DEFAULT = (0x80FB000UL);
  137. TLS_FLASH_PARAM1_ADDR = (0x80FC000UL);
  138. TLS_FLASH_PARAM2_ADDR = (0x80FD000UL);
  139. TLS_FLASH_PARAM_RESTORE_ADDR = (0x80FE000UL);
  140. TLS_FLASH_OTA_FLAG_ADDR = (0x80FF000UL);
  141. TLS_FLASH_END_ADDR = (0x80FFFFFUL);
  142. #endif
  143. #ifdef LUAT_USE_NIMBLE
  144. // TODO 注意, 除了启用LUAT_USE_NIMBTE外
  145. // 1. 修改FreeRTOSConfig.h的configTICK_RATE_HZ为500, 并重新make lib
  146. // 2. 若修改libblehost.a相关代码,需要手工复制bin目录下的文件,拷贝到lib目录.
  147. tls_ft_param_init();
  148. tls_param_load_factory_default();
  149. tls_param_init(); /*add param to init sysparam_lock sem*/
  150. // 读蓝牙mac, 如果是默认值,就根据unique_id读取
  151. uint8_t bt_mac[6];
  152. // 缺省mac C0:25:08:09:01:10
  153. uint8_t bt_default_mac[] = {0xC0,0x25,0x08,0x09,0x01,0x10};
  154. tls_get_bt_mac_addr(bt_mac);
  155. if (!memcmp(bt_mac, bt_default_mac, 6)) { // 看来是默认MAC, 那就改一下吧
  156. if (unique_id[1] == 0x10){
  157. memcpy(bt_mac, unique_id + 10, 6);
  158. }
  159. else {
  160. memcpy(bt_mac, unique_id + 2, 6);
  161. }
  162. tls_set_bt_mac_addr(bt_mac);
  163. }
  164. LLOGD("BLE_4.2 %02X:%02X:%02X:%02X:%02X:%02X", bt_mac[0], bt_mac[1], bt_mac[2], bt_mac[3], bt_mac[4], bt_mac[5]);
  165. #endif
  166. // 如要使用psram,启用以下代码,并重新编译sdk
  167. #ifdef LUAT_USE_PSRAM
  168. // 首先, 初始化psram相关引脚
  169. #ifndef LUAT_USE_PSRAM_PORT
  170. #ifdef AIR101
  171. // air101只能是0, 与SPI和UART3冲突, PB0~PB5
  172. #define LUAT_USE_PSRAM_PORT 0
  173. #else
  174. // air103可以是0或1
  175. // 1的话, PB2~PB5, PA15, PB27, 依然占用SPI0,但改用SPI1
  176. #define LUAT_USE_PSRAM_PORT 1
  177. #endif
  178. #endif
  179. printf("psram init\n");
  180. wm_psram_config(LUAT_USE_PSRAM_PORT);
  181. // 然后初始化psram的寄存器
  182. psram_init(PSRAM_QPI);
  183. //uint8_t* psram_ptr = (uint8_t*)(PSRAM_ADDR_START);
  184. #endif
  185. #ifdef __LUATOS__
  186. #ifdef LUAT_USE_LVGL
  187. lv_init();
  188. static tls_os_timer_t *os_timer = NULL;
  189. tls_os_timer_create(&os_timer, lvgl_timer_cb, NULL, 10/(1000 / configTICK_RATE_HZ), 1, NULL);
  190. tls_os_timer_start(os_timer);
  191. #endif
  192. tls_os_task_create(NULL, NULL,
  193. luat_start,
  194. NULL,
  195. (void *)TaskStartStk, /* task's stack start address */
  196. TASK_START_STK_SIZE * sizeof(u32), /* task's stack size, unit:byte */
  197. 31,
  198. 0);
  199. #else
  200. printf("hello word\n");
  201. while (1);
  202. #endif
  203. }
  204. #ifndef __LUATOS__
  205. // void vApplicationTickHook( void ) {}
  206. void bpool(void *buffer, long len) {}
  207. #endif