main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. uint32_t rtc_ctrl1 = tls_reg_read32(HR_PMU_RTC_CTRL1);
  109. // 如果RTC计数少于1, 那肯定是第一次开机, 启动RTC并设置到1970年.
  110. // uint32_t rtc_ctrl2 = tls_reg_read32(HR_PMU_RTC_CTRL2);
  111. if (0x2 > rtc_ctrl1) {
  112. tblock.tm_mday = 1;
  113. tblock.tm_mon = 0;
  114. tblock.tm_year = 70;
  115. tls_set_rtc(&tblock);
  116. }
  117. else {
  118. // 只需要确保RTC启用
  119. int ctrl2 = tls_reg_read32(HR_PMU_RTC_CTRL2); /* enable */
  120. ctrl2 |= (1 << 16);
  121. tls_reg_write32(HR_PMU_RTC_CTRL2, ctrl2);
  122. }
  123. // 完全禁用jtag
  124. //u32 value = tls_reg_read32(HR_CLK_SEL_CTL);
  125. // printf("HR_CLK_SEL_CTL %08X\n", value);
  126. //value = value & 0x7FFF;
  127. // value = value & 0x7F00;
  128. //tls_reg_write32(HR_CLK_SEL_CTL, value);
  129. // tls_reg_read32(HR_CLK_SEL_CTL);
  130. // printf("HR_CLK_SEL_CTL %08X\n", value);
  131. // 读取开机原因
  132. // rst_sta = tls_reg_read32(HR_CLK_RST_STA);
  133. // tls_reg_write32(HR_CLK_RST_STA, 0xFF);
  134. #ifdef LUAT_USE_SHELL
  135. luat_shell_poweron(0);
  136. #endif
  137. #ifdef __LUATOS__
  138. extern void luat_mcu_tick64_init(void);
  139. luat_mcu_tick64_init();
  140. tls_fls_read_unique_id(unique_id);
  141. if (unique_id[1] == 0x10){
  142. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  143. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  144. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  145. unique_id[10], unique_id[11], unique_id[12], unique_id[13], unique_id[14],unique_id[15],
  146. luat_os_bsp());
  147. }else{
  148. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  149. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  150. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  151. luat_os_bsp());
  152. }
  153. #endif
  154. #ifdef AIR103
  155. TLS_FLASH_PARAM_DEFAULT = (0x80FB000UL);
  156. TLS_FLASH_PARAM1_ADDR = (0x80FC000UL);
  157. TLS_FLASH_PARAM2_ADDR = (0x80FD000UL);
  158. TLS_FLASH_PARAM_RESTORE_ADDR = (0x80FE000UL);
  159. TLS_FLASH_OTA_FLAG_ADDR = (0x80FF000UL);
  160. TLS_FLASH_END_ADDR = (0x80FFFFFUL);
  161. #endif
  162. #ifdef LUAT_USE_NIMBLE
  163. // TODO 注意, 除了启用LUAT_USE_NIMBTE外
  164. // 1. 修改FreeRTOSConfig.h的configTICK_RATE_HZ为500, 并重新make lib
  165. // 2. 若修改libblehost.a相关代码,需要手工复制bin目录下的文件,拷贝到lib目录.
  166. tls_ft_param_init();
  167. tls_param_load_factory_default();
  168. tls_param_init(); /*add param to init sysparam_lock sem*/
  169. // 读蓝牙mac, 如果是默认值,就根据unique_id读取
  170. uint8_t bt_mac[6];
  171. // 缺省mac C0:25:08:09:01:10
  172. uint8_t bt_default_mac[] = {0xC0,0x25,0x08,0x09,0x01,0x10};
  173. tls_get_bt_mac_addr(bt_mac);
  174. if (!memcmp(bt_mac, bt_default_mac, 6)) { // 看来是默认MAC, 那就改一下吧
  175. if (unique_id[1] == 0x10){
  176. memcpy(bt_mac, unique_id + 10, 6);
  177. }
  178. else {
  179. memcpy(bt_mac, unique_id + 2, 6);
  180. }
  181. tls_set_bt_mac_addr(bt_mac);
  182. }
  183. 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]);
  184. #endif
  185. // 如要使用psram,启用以下代码,并重新编译sdk
  186. #ifdef LUAT_USE_PSRAM
  187. // 首先, 初始化psram相关引脚
  188. #ifndef LUAT_USE_PSRAM_PORT
  189. #ifdef AIR101
  190. // air101只能是0, 与SPI和UART3冲突, PB0~PB5
  191. #define LUAT_USE_PSRAM_PORT 0
  192. #else
  193. // air103可以是0或1
  194. // 1的话, PB2~PB5, PA15, PB27, 依然占用SPI0,但改用SPI1
  195. #define LUAT_USE_PSRAM_PORT 1
  196. #endif
  197. #endif
  198. printf("psram init\n");
  199. wm_psram_config(LUAT_USE_PSRAM_PORT);
  200. // 然后初始化psram的寄存器
  201. psram_init(PSRAM_QPI);
  202. //uint8_t* psram_ptr = (uint8_t*)(PSRAM_ADDR_START);
  203. #endif
  204. #ifdef __LUATOS__
  205. #ifdef LUAT_USE_LVGL
  206. lv_init();
  207. static tls_os_timer_t *os_timer = NULL;
  208. tls_os_timer_create(&os_timer, lvgl_timer_cb, NULL, 10/(1000 / configTICK_RATE_HZ), 1, NULL);
  209. tls_os_timer_start(os_timer);
  210. #endif
  211. tls_os_task_create(NULL, NULL,
  212. luat_start,
  213. NULL,
  214. (void *)TaskStartStk, /* task's stack start address */
  215. TASK_START_STK_SIZE * sizeof(u32), /* task's stack size, unit:byte */
  216. 31,
  217. 0);
  218. #else
  219. printf("hello word\n");
  220. while (1);
  221. #endif
  222. }
  223. #ifndef __LUATOS__
  224. // void vApplicationTickHook( void ) {}
  225. void bpool(void *buffer, long len) {}
  226. #endif