main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #include "wm_osal.h"
  7. #include "wm_watchdog.h"
  8. #include "FreeRTOS.h"
  9. #include "task.h"
  10. #ifdef __LUATOS__
  11. #include "string.h"
  12. #include "luat_fs.h"
  13. #include "bget.h"
  14. #include "luat_base.h"
  15. #include "luat_msgbus.h"
  16. #include "luat_pm.h"
  17. #include "luat_rtc.h"
  18. #include "luat_pcap.h"
  19. #include "luat_uart.h"
  20. #include "luat_malloc.h"
  21. #include <string.h>
  22. #include "wm_irq.h"
  23. #include "tls_sys.h"
  24. #include "wm_ram_config.h"
  25. #include "wm_internal_flash.h"
  26. #include "wm_psram.h"
  27. #include "wm_efuse.h"
  28. #include "wm_regs.h"
  29. #include "wm_wifi.h"
  30. #include "FreeRTOS.h"
  31. #define LUAT_LOG_TAG "main"
  32. #include "luat_log.h"
  33. #define LUAT_PCAP_UART_ID 2
  34. void luat_heap_init(void);
  35. #if defined(LUAT_USE_PCAP)
  36. static void pcap_uart_write(void *ptr, const void* buf, size_t len) {
  37. luat_uart_write(LUAT_PCAP_UART_ID, buf, len);
  38. }
  39. #endif
  40. static void luat_start(void *sdata){
  41. (void)sdata;
  42. luat_main();
  43. }
  44. #ifdef LUAT_USE_LVGL
  45. #include "lvgl.h"
  46. // static uint8_t lvgl_called = 0;
  47. static uint32_t lvgl_tick_cnt;
  48. static int luat_lvgl_cb(lua_State *L, void* ptr) {
  49. if (lvgl_tick_cnt) lvgl_tick_cnt--;
  50. lv_task_handler();
  51. // lvgl_called = 0;
  52. return 0;
  53. }
  54. static void lvgl_timer_cb(void *ptmr, void *parg) {
  55. // if (lvgl_called)
  56. // return;
  57. if (lvgl_tick_cnt < 10)
  58. {
  59. lvgl_tick_cnt++;
  60. rtos_msg_t msg = {0};
  61. msg.handler = luat_lvgl_cb;
  62. luat_msgbus_put(&msg, 0);
  63. }
  64. // lvgl_called = 1;
  65. }
  66. #endif
  67. #endif
  68. void check_stack(void* ptr) {
  69. (void)ptr;
  70. while (1) {
  71. vTaskDelay(1000);
  72. tls_os_disp_task_stat_info();
  73. }
  74. }
  75. static const const char* reason[] = {
  76. "power or reset",
  77. "by charge", // 不可能
  78. "wakeup by rtc",
  79. "reset by software",
  80. "unkown",
  81. "reset by key",
  82. "reboot by exception",
  83. "reboot by tool",
  84. "reset by watchdog",
  85. "reset by pad",
  86. "by charge" // 不可能
  87. };
  88. extern int luat_pm_get_poweron_reason(void);
  89. extern int power_bk_reg;
  90. void luat_fs_update_addr(void);
  91. #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
  92. void UserMain(void){
  93. unsigned char unique_id [20] = {0};
  94. tls_uart_options_t opt = {0};
  95. opt.baudrate = UART_BAUDRATE_B921600;
  96. opt.charlength = TLS_UART_CHSIZE_8BIT;
  97. opt.flow_ctrl = TLS_UART_FLOW_CTRL_NONE;
  98. opt.paritytype = TLS_UART_PMODE_DISABLED;
  99. opt.stopbits = TLS_UART_ONE_STOPBITS;
  100. tls_uart_port_init(0, &opt, 0);
  101. LLOGD("poweron: %s", reason[luat_pm_get_poweron_reason()]);
  102. luat_fs_update_addr();
  103. // printf("Bit 8 -- %d\n", CHECK_BIT(power_bk_reg, 8));
  104. // printf("Bit 5 -- %d\n", CHECK_BIT(power_bk_reg, 5));
  105. // printf("Bit 2 -- %d\n", CHECK_BIT(power_bk_reg, 2));
  106. //printf("bsp reboot_reason %d\n", tls_sys_get_reboot_reason());
  107. struct tm tt = {0};
  108. luat_rtc_get(&tt);
  109. // printf("main get %d-%d-%d %d:%d:%d\n", tt.tm_year + 1900, tt.tm_mon + 1, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec);
  110. // uint32_t rtc_ctrl1 = tls_reg_read32(HR_PMU_RTC_CTRL1);
  111. // printf("rtc_ctrl1 %ld\n", rtc_ctrl1);
  112. // 如果RTC计数少于1, 那肯定是第一次开机, 启动RTC并设置到1970年.
  113. // uint32_t rtc_ctrl2 = tls_reg_read32(HR_PMU_RTC_CTRL2);
  114. if (tt.tm_year == 0) {
  115. tt.tm_mday = 1;
  116. tt.tm_mon = 0;
  117. tt.tm_year = 71;
  118. luat_rtc_set(&tt);
  119. }
  120. else {
  121. // 只需要确保RTC启用
  122. int ctrl2 = tls_reg_read32(HR_PMU_RTC_CTRL2); /* enable */
  123. ctrl2 |= (1 << 16);
  124. tls_reg_write32(HR_PMU_RTC_CTRL2, ctrl2);
  125. }
  126. // 完全禁用jtag
  127. //u32 value = tls_reg_read32(HR_CLK_SEL_CTL);
  128. // printf("HR_CLK_SEL_CTL %08X\n", value);
  129. //value = value & 0x7FFF;
  130. // value = value & 0x7F00;
  131. //tls_reg_write32(HR_CLK_SEL_CTL, value);
  132. // tls_reg_read32(HR_CLK_SEL_CTL);
  133. // printf("HR_CLK_SEL_CTL %08X\n", value);
  134. // 读取开机原因
  135. // rst_sta = tls_reg_read32(HR_CLK_RST_STA);
  136. // tls_reg_write32(HR_CLK_RST_STA, 0xFF);
  137. #if defined(LUAT_USE_SHELL) || defined(LUAT_USE_REPL)
  138. luat_shell_poweron(0);
  139. #endif
  140. #ifdef __LUATOS__
  141. extern void luat_mcu_tick64_init(void);
  142. luat_mcu_tick64_init();
  143. tls_fls_read_unique_id(unique_id);
  144. if (unique_id[1] == 0x10) {
  145. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  146. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  147. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  148. unique_id[10], unique_id[11], unique_id[12], unique_id[13], unique_id[14],
  149. unique_id[15],unique_id[16],unique_id[17],
  150. luat_os_bsp());
  151. }else{
  152. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  153. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  154. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  155. luat_os_bsp());
  156. }
  157. #endif
  158. #ifdef LUAT_USE_WLAN
  159. u8 tmpmac[8] = {0};
  160. tls_ft_param_get(CMD_WIFI_MACAP, tmpmac, 6);
  161. LLOGD("AP MAC %02X:%02X:%02X:%02X:%02X:%02X", tmpmac[0], tmpmac[1], tmpmac[2], tmpmac[3], tmpmac[4], tmpmac[5]);
  162. tls_ft_param_get(CMD_WIFI_MAC, tmpmac, 6);
  163. LLOGD("STA MAC %02X:%02X:%02X:%02X:%02X:%02X", tmpmac[0], tmpmac[1], tmpmac[2], tmpmac[3], tmpmac[4], tmpmac[5]);
  164. #endif
  165. // 如要使用psram,启用以下代码,并重新编译sdk
  166. #ifdef LUAT_USE_PSRAM
  167. // 首先, 初始化psram相关引脚
  168. #ifndef LUAT_USE_PSRAM_PORT
  169. #ifdef AIR101
  170. // air101只能是0, 与SPI和UART3冲突, PB0~PB5
  171. #define LUAT_USE_PSRAM_PORT 0
  172. #else
  173. // air103可以是0或1
  174. // 1的话, PB2~PB5, PA15, PB27, 依然占用SPI0,但改用SPI1
  175. #define LUAT_USE_PSRAM_PORT 1
  176. #endif
  177. #endif
  178. printf("psram init\n");
  179. wm_psram_config(LUAT_USE_PSRAM_PORT);
  180. // 然后初始化psram的寄存器
  181. psram_init(PSRAM_QPI);
  182. //uint8_t* psram_ptr = (uint8_t*)(PSRAM_ADDR_START);
  183. #endif
  184. #if defined(LUAT_USE_FOTA)
  185. extern void luat_fota_boot_check(void);
  186. luat_fota_boot_check();
  187. #endif
  188. #ifdef __LUATOS__
  189. luat_heap_init();
  190. // PCAP抓包
  191. #ifdef LUAT_USE_PCAP
  192. // 初始化pcap
  193. luat_uart_t uart2 = {
  194. .id = LUAT_PCAP_UART_ID,
  195. .baud_rate = 115200,
  196. .data_bits = 8,
  197. .stop_bits = 1,
  198. .parity = 0
  199. };
  200. luat_uart_setup(&uart2);
  201. luat_pcap_init(pcap_uart_write, NULL);
  202. luat_pcap_write_head();
  203. #endif
  204. #ifdef LUAT_USE_LVGL
  205. lv_init();
  206. static tls_os_timer_t *os_timer = NULL;
  207. tls_os_timer_create(&os_timer, lvgl_timer_cb, NULL, 10/(1000 / configTICK_RATE_HZ), 1, NULL);
  208. tls_os_timer_start(os_timer);
  209. #endif
  210. #define VM_SIZE (12*1024)
  211. #if defined(LUAT_USE_WLAN) && defined(LUAT_USE_NIMBLE) && defined(LUAT_USE_TLS)
  212. char *vm_task_stack = luat_heap_alloc(NULL, NULL, 0, VM_SIZE);
  213. // LLOGD("VM heap start %p", vm_task_stack);
  214. #else
  215. char *vm_task_stack = luat_heap_malloc(VM_SIZE);
  216. #endif
  217. tls_os_task_create(NULL, "luatos",
  218. luat_start,
  219. NULL,
  220. (void *)vm_task_stack, /* task's stack start address */
  221. VM_SIZE, /* task's stack size, unit:byte */
  222. 21,
  223. 0);
  224. // tls_os_task_create(NULL, "cstack", check_stack, NULL, NULL, 2048, 10, 0);
  225. #else
  226. printf("hello word\n");
  227. while (1);
  228. #endif
  229. }
  230. #ifndef __LUATOS__
  231. // void vApplicationTickHook( void ) {}
  232. void bpool(void *buffer, long len) {}
  233. #endif