main.c 7.5 KB

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