main.c 6.7 KB

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