main.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 <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. static void pcap_uart_write(void *ptr, const void* buf, size_t len) {
  35. luat_uart_write(LUAT_PCAP_UART_ID, buf, len);
  36. }
  37. static void luat_start(void *sdata){
  38. luat_heap_init();
  39. luat_main();
  40. }
  41. #ifdef LUAT_USE_LVGL
  42. #include "lvgl.h"
  43. // static uint8_t lvgl_called = 0;
  44. static uint32_t lvgl_tick_cnt;
  45. static int luat_lvgl_cb(lua_State *L, void* ptr) {
  46. if (lvgl_tick_cnt) lvgl_tick_cnt--;
  47. lv_task_handler();
  48. // lvgl_called = 0;
  49. return 0;
  50. }
  51. static void lvgl_timer_cb(void *ptmr, void *parg) {
  52. // if (lvgl_called)
  53. // return;
  54. if (lvgl_tick_cnt < 10)
  55. {
  56. lvgl_tick_cnt++;
  57. rtos_msg_t msg = {0};
  58. msg.handler = luat_lvgl_cb;
  59. luat_msgbus_put(&msg, 0);
  60. }
  61. // lvgl_called = 1;
  62. }
  63. // #define LVGL_TASK_SIZE 512
  64. // static OS_STK __attribute__((aligned(4))) LVGLTaskStk[LVGL_TASK_SIZE] = {0};
  65. #endif
  66. #define TASK_START_STK_SIZE (3*1024) // 实际*4, 即12k
  67. static OS_STK __attribute__((aligned(4))) TaskStartStk[TASK_START_STK_SIZE] = {0};
  68. #endif
  69. void check_stack(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. #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
  91. void UserMain(void){
  92. char unique_id [20] = {0};
  93. tls_uart_options_t opt = {0};
  94. opt.baudrate = UART_BAUDRATE_B921600;
  95. opt.charlength = TLS_UART_CHSIZE_8BIT;
  96. opt.flow_ctrl = TLS_UART_FLOW_CTRL_NONE;
  97. opt.paritytype = TLS_UART_PMODE_DISABLED;
  98. opt.stopbits = TLS_UART_ONE_STOPBITS;
  99. tls_uart_port_init(0, &opt, 0);
  100. LLOGD("poweron: %s", reason[luat_pm_get_poweron_reason()]);
  101. // printf("Bit 8 -- %d\n", CHECK_BIT(power_bk_reg, 8));
  102. // printf("Bit 5 -- %d\n", CHECK_BIT(power_bk_reg, 5));
  103. // printf("Bit 2 -- %d\n", CHECK_BIT(power_bk_reg, 2));
  104. //printf("bsp reboot_reason %d\n", tls_sys_get_reboot_reason());
  105. struct tm tt = {0};
  106. luat_rtc_get(&tt);
  107. // 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);
  108. // uint32_t rtc_ctrl1 = tls_reg_read32(HR_PMU_RTC_CTRL1);
  109. // printf("rtc_ctrl1 %ld\n", rtc_ctrl1);
  110. // 如果RTC计数少于1, 那肯定是第一次开机, 启动RTC并设置到1970年.
  111. // uint32_t rtc_ctrl2 = tls_reg_read32(HR_PMU_RTC_CTRL2);
  112. if (tt.tm_year == 0) {
  113. tt.tm_mday = 1;
  114. tt.tm_mon = 0;
  115. tt.tm_year = 71;
  116. luat_rtc_set(&tt);
  117. }
  118. else {
  119. // 只需要确保RTC启用
  120. int ctrl2 = tls_reg_read32(HR_PMU_RTC_CTRL2); /* enable */
  121. ctrl2 |= (1 << 16);
  122. tls_reg_write32(HR_PMU_RTC_CTRL2, ctrl2);
  123. }
  124. // 完全禁用jtag
  125. //u32 value = tls_reg_read32(HR_CLK_SEL_CTL);
  126. // printf("HR_CLK_SEL_CTL %08X\n", value);
  127. //value = value & 0x7FFF;
  128. // value = value & 0x7F00;
  129. //tls_reg_write32(HR_CLK_SEL_CTL, value);
  130. // tls_reg_read32(HR_CLK_SEL_CTL);
  131. // printf("HR_CLK_SEL_CTL %08X\n", value);
  132. // 读取开机原因
  133. // rst_sta = tls_reg_read32(HR_CLK_RST_STA);
  134. // tls_reg_write32(HR_CLK_RST_STA, 0xFF);
  135. #if defined(LUAT_USE_SHELL) || defined(LUAT_USE_REPL)
  136. luat_shell_poweron(0);
  137. #endif
  138. #ifdef __LUATOS__
  139. extern void luat_mcu_tick64_init(void);
  140. luat_mcu_tick64_init();
  141. tls_fls_read_unique_id(unique_id);
  142. if (unique_id[1] == 0x10) {
  143. 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",
  144. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  145. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  146. unique_id[10], unique_id[11], unique_id[12], unique_id[13], unique_id[14],
  147. unique_id[15],unique_id[16],unique_id[17],
  148. luat_os_bsp());
  149. }else{
  150. printf("I/main auth ok %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X %s\n",
  151. unique_id[0], unique_id[1], unique_id[2], unique_id[3], unique_id[4],
  152. unique_id[5], unique_id[6], unique_id[7], unique_id[8], unique_id[9],
  153. luat_os_bsp());
  154. }
  155. #endif
  156. #ifdef LUAT_USE_WLAN
  157. u8 tmpmac[8] = {0};
  158. tls_ft_param_get(CMD_WIFI_MACAP, tmpmac, 6);
  159. extern u8 *hostapd_get_mac(void);
  160. u8* macptr = hostapd_get_mac();
  161. //LLOGD("default AP MAC %02X:%02X:%02X:%02X:%02X:%02X", macptr[0], macptr[1], macptr[2], macptr[3], macptr[4], macptr[5]);
  162. memcpy(macptr, tmpmac, 6);
  163. LLOGD("AP MAC %02X:%02X:%02X:%02X:%02X:%02X", macptr[0], macptr[1], macptr[2], macptr[3], macptr[4], macptr[5]);
  164. tls_ft_param_get(CMD_WIFI_MAC, tmpmac, 6);
  165. LLOGD("STA MAC %02X:%02X:%02X:%02X:%02X:%02X", tmpmac[0], tmpmac[1], tmpmac[2], tmpmac[3], tmpmac[4], tmpmac[5]);
  166. #endif
  167. // 如要使用psram,启用以下代码,并重新编译sdk
  168. #ifdef LUAT_USE_PSRAM
  169. // 首先, 初始化psram相关引脚
  170. #ifndef LUAT_USE_PSRAM_PORT
  171. #ifdef AIR101
  172. // air101只能是0, 与SPI和UART3冲突, PB0~PB5
  173. #define LUAT_USE_PSRAM_PORT 0
  174. #else
  175. // air103可以是0或1
  176. // 1的话, PB2~PB5, PA15, PB27, 依然占用SPI0,但改用SPI1
  177. #define LUAT_USE_PSRAM_PORT 1
  178. #endif
  179. #endif
  180. printf("psram init\n");
  181. wm_psram_config(LUAT_USE_PSRAM_PORT);
  182. // 然后初始化psram的寄存器
  183. psram_init(PSRAM_QPI);
  184. //uint8_t* psram_ptr = (uint8_t*)(PSRAM_ADDR_START);
  185. #endif
  186. #ifdef __LUATOS__
  187. // PCAP抓包
  188. #ifdef LUAT_USE_PCAP
  189. // 初始化pcap
  190. luat_uart_t uart2 = {
  191. .id = LUAT_PCAP_UART_ID,
  192. .baud_rate = 115200,
  193. .data_bits = 8,
  194. .stop_bits = 1,
  195. .parity = 0
  196. };
  197. luat_uart_setup(&uart2);
  198. luat_pcap_init(pcap_uart_write, NULL);
  199. luat_pcap_write_head();
  200. #endif
  201. #ifdef LUAT_USE_LVGL
  202. lv_init();
  203. static tls_os_timer_t *os_timer = NULL;
  204. tls_os_timer_create(&os_timer, lvgl_timer_cb, NULL, 10/(1000 / configTICK_RATE_HZ), 1, NULL);
  205. tls_os_timer_start(os_timer);
  206. #endif
  207. tls_os_task_create(NULL, "luatos",
  208. luat_start,
  209. NULL,
  210. (void *)TaskStartStk, /* task's stack start address */
  211. TASK_START_STK_SIZE * sizeof(u32), /* task's stack size, unit:byte */
  212. 21,
  213. 0);
  214. // tls_os_task_create(NULL, "cstack", check_stack, NULL, NULL, 2048, 10, 0);
  215. #else
  216. printf("hello word\n");
  217. while (1);
  218. #endif
  219. }
  220. #ifndef __LUATOS__
  221. // void vApplicationTickHook( void ) {}
  222. void bpool(void *buffer, long len) {}
  223. #endif
  224. extern const u8 default_mac[];
  225. static int is_mac_ok(u8 mac_addr[6]) {
  226. // 如果是默认值, 那就是不合法
  227. if (!memcmp(mac_addr, default_mac, 6)) {
  228. return 0;
  229. }
  230. // 如果任意一位是0x00或者0xFF,那就是不合法
  231. for (size_t i = 0; i < 6; i++)
  232. {
  233. if (mac_addr[i] == 0 || mac_addr[i] == 0xFF) {
  234. return 0;
  235. }
  236. }
  237. return 1;
  238. }
  239. void sys_mac_init() {
  240. #ifdef LUAT_CONF_LOG_UART1
  241. luat_log_set_uart_port(1);
  242. #endif
  243. u8 tmp_mac[6] = {0};
  244. u8 mac_addr[6] = {0};
  245. u8 ap_mac[6] = {0};
  246. char unique_id [20] = {0};
  247. tls_fls_read_unique_id(unique_id);
  248. int ret = 0;
  249. #ifdef LUAT_USE_NIMBLE
  250. // 读蓝牙mac, 如果是默认值,就根据unique_id读取
  251. uint8_t bt_mac[6];
  252. // 缺省mac C0:25:08:09:01:10
  253. uint8_t bt_default_mac[] = {0xC0,0x25,0x08,0x09,0x01,0x10};
  254. tls_get_bt_mac_addr(bt_mac);
  255. if (!memcmp(bt_mac, bt_default_mac, 6)) { // 看来是默认MAC, 那就改一下吧
  256. if (unique_id[1] == 0x10){
  257. memcpy(bt_mac, unique_id + 10, 6);
  258. }
  259. else {
  260. memcpy(bt_mac, unique_id + 2, 6);
  261. }
  262. tls_set_bt_mac_addr(bt_mac);
  263. }
  264. 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]);
  265. #endif
  266. #ifdef LUAT_USE_WLAN
  267. ret = tls_ft_param_get(CMD_WIFI_MAC, mac_addr, 6);
  268. tls_ft_param_get(CMD_WIFI_MACAP, ap_mac, 6);
  269. //printf("CMD_WIFI_MAC ret %d %02X%02X%02X%02X%02X%02X\n", ret, mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  270. int sta_mac_ok = is_mac_ok(mac_addr);
  271. int ap_mac_ok = is_mac_ok(ap_mac);
  272. // 其中一个合法呢?
  273. if (sta_mac_ok && !ap_mac_ok) {
  274. printf("sta地址合法, ap地址不合法\n");
  275. memcpy(ap_mac, mac_addr, 6);
  276. }
  277. else if (!sta_mac_ok && ap_mac_ok) {
  278. printf("sta地址不合法, ap地址合法\n");
  279. memcpy(mac_addr, ap_mac, 6);
  280. }
  281. else if (!sta_mac_ok && !ap_mac_ok){ // 均不合法, 那就用uniqid
  282. printf("sta地址不合法, ap地址不合法\n");
  283. if (unique_id[1] == 0x10){
  284. memcpy(mac_addr, unique_id + 12, 6);
  285. }
  286. else {
  287. memcpy(mac_addr, unique_id + 4, 6);
  288. }
  289. mac_addr[0] = 0x0C;
  290. for (size_t i = 1; i < 6; i++)
  291. {
  292. if (mac_addr[i] == 0 || mac_addr[i] == 0xFF) {
  293. mac_addr[i] = 0x01;
  294. }
  295. }
  296. memcpy(ap_mac, mac_addr, 6);
  297. }
  298. if (!memcmp(mac_addr, ap_mac, 6)) {
  299. printf("sta与ap地址相同, 调整ap地址\n");
  300. // 完全相同, 那就改一下ap的地址
  301. if (ap_mac[5] == 0x01 || ap_mac[5] == 0xFE) {
  302. ap_mac[5] = 0x02;
  303. }
  304. else {
  305. ap_mac[5] += 1;
  306. }
  307. }
  308. // 全部mac都得到了, 然后重新读一下mac, 不相同的就重新写入
  309. // 先判断sta的mac
  310. tls_ft_param_get(CMD_WIFI_MAC, tmp_mac, 6);
  311. if (memcmp(tmp_mac, mac_addr, 6)) {
  312. // 不一致, 写入
  313. tls_ft_param_set(CMD_WIFI_MAC, mac_addr, 6);
  314. printf("更新STA mac %02X%02X%02X%02X%02X%02X\n", ret, mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  315. }
  316. // 再判断ap的mac
  317. tls_ft_param_get(CMD_WIFI_MACAP, tmp_mac, 6);
  318. if (memcmp(tmp_mac, ap_mac, 6)) {
  319. // 不一致, 写入
  320. tls_ft_param_set(CMD_WIFI_MACAP, ap_mac, 6);
  321. printf("更新AP mac %02X%02X%02X%02X%02X%02X\n", ret, ap_mac[0], ap_mac[1], ap_mac[2], ap_mac[3], ap_mac[4], ap_mac[5]);
  322. }
  323. #endif
  324. }