main.c 6.7 KB

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