ch390h_task.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #include "luat_base.h"
  2. #include "luat_netdrv.h"
  3. #include "luat_network_adapter.h"
  4. #include "luat_netdrv_ch390h.h"
  5. #include "luat_netdrv_napt.h"
  6. #include "luat_ch390h.h"
  7. #include "luat_malloc.h"
  8. // #include "luat_spi.h"
  9. #include "luat_gpio.h"
  10. #include "net_lwip2.h"
  11. #include "luat_ulwip.h"
  12. #include "lwip/tcp.h"
  13. #include "lwip/sys.h"
  14. #include "lwip/tcpip.h"
  15. #include "lwip/pbuf.h"
  16. #include "luat_mem.h"
  17. #include "luat_mcu.h"
  18. #include "luat_wdt.h"
  19. #include "luat_rtos.h"
  20. #include "luat_netdrv_event.h"
  21. #define LUAT_LOG_TAG "netdrv.ch390x"
  22. #include "luat_log.h"
  23. typedef struct pkg_evt
  24. {
  25. uint8_t id;
  26. luat_ch390h_cstring_t* cs;
  27. ch390h_t *ch;
  28. }pkg_evt_t;
  29. #ifdef LUAT_USE_NETDRV_LWIP_ARP
  30. extern err_t luat_netdrv_netif_input_main(struct pbuf *p, struct netif *inp);
  31. extern err_t luat_netdrv_etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr);
  32. #else
  33. #define luat_netdrv_netif_input_main netif_input
  34. #define luat_netdrv_etharp_output ulwip_etharp_output
  35. #endif
  36. extern ch390h_t* ch390h_drvs[MAX_CH390H_NUM];
  37. static luat_rtos_task_handle ch390h_task_handle;
  38. static luat_rtos_queue_t qt;
  39. static uint64_t warn_vid_pid_tm;
  40. static uint64_t warn_msg_tm;
  41. static uint32_t s_ch390h_mode; // 0 -- PULL 模式, 1 == IRQ 模式
  42. static int ch390h_irq_cb(void *data, void *args) {
  43. uint32_t len = 0;
  44. luat_rtos_queue_get_cnt(qt, &len);
  45. if (len > 4) {
  46. return 0;
  47. }
  48. pkg_evt_t evt = {
  49. .id = 2
  50. };
  51. luat_rtos_queue_send(qt, &evt, sizeof(pkg_evt_t), 0);
  52. return 0;
  53. }
  54. static int ch390h_bootup(ch390h_t* ch) {
  55. if (ch->init_done) {
  56. return 0;
  57. }
  58. // 初始化SPI设备, 由外部代码初始化, 因为不同bsp的速度不一样, 就不走固定值了
  59. luat_gpio_cfg_t gpio_cfg = {0};
  60. // 初始化CS脚
  61. luat_gpio_t gpio = {0};
  62. gpio.pin = ch->cspin;
  63. gpio.mode = LUAT_GPIO_OUTPUT;
  64. gpio.pull = LUAT_GPIO_PULLUP;
  65. gpio.irq = 1;
  66. luat_gpio_setup(&gpio);
  67. // 初始化INT脚
  68. if (ch->intpin != 0xff) {
  69. luat_gpio_set_default_cfg(&gpio_cfg);
  70. gpio_cfg.pin = ch->intpin;
  71. gpio_cfg.mode = LUAT_GPIO_IRQ;
  72. gpio_cfg.irq_type = LUAT_GPIO_RISING_IRQ;
  73. gpio_cfg.pull = 0;
  74. gpio_cfg.irq_cb = ch390h_irq_cb;
  75. luat_gpio_open(&gpio_cfg);
  76. LLOGI("enable irq mode in pin %d", ch->intpin);
  77. s_ch390h_mode = 1;
  78. }
  79. else {
  80. // LLOGI("enable pull mode, use pool mode");
  81. }
  82. ch->init_done = 1;
  83. return 0;
  84. }
  85. static luat_ch390h_cstring_t* new_cstring(ch390h_t* ch, uint16_t len) {
  86. size_t total = 0;
  87. size_t used = 0;
  88. size_t max_used = 0;
  89. luat_meminfo_opt_sys(ch->pkg_mem_type, &total, &used, &max_used);
  90. if (total > 0 && total - used > 32*1024) { // 最少留32k给系统用
  91. luat_ch390h_cstring_t* cs = luat_heap_opt_malloc(ch->pkg_mem_type, sizeof(luat_ch390h_cstring_t) + len - 4);
  92. if (cs == NULL) {
  93. LLOGE("有剩余内存不多但分配失败! total %d used %d max_used %d len %d", total, used, max_used, len);
  94. }
  95. return cs;
  96. }
  97. LLOGE("剩余内存不多了,抛弃数据包 total %d used %d max_used %d len %d", total, used, max_used, len);
  98. return NULL;
  99. }
  100. static void send_msg_cs(ch390h_t* ch, luat_ch390h_cstring_t* cs) {
  101. uint32_t len = 0;
  102. luat_rtos_queue_get_cnt(qt, &len);
  103. uint64_t tm;
  104. if (len >= 1000) {
  105. tm = luat_mcu_tick64_ms();
  106. if (tm - warn_msg_tm > 1000) {
  107. warn_msg_tm = tm;
  108. LLOGW("太多待处理消息了!!! %d", len);
  109. }
  110. luat_heap_opt_free(ch->pkg_mem_type, cs);
  111. return;
  112. }
  113. if (len > 512) {
  114. tm = luat_mcu_tick64_ms();
  115. if (tm - warn_msg_tm > 1000) {
  116. warn_msg_tm = tm;
  117. LLOGD("当前消息数量 %d", len);
  118. }
  119. }
  120. pkg_evt_t evt = {
  121. .id = 1,
  122. .cs = cs,
  123. .ch = ch
  124. };
  125. int ret = luat_rtos_queue_send(qt, &evt, sizeof(pkg_evt_t), 0);
  126. if (ret) {
  127. LLOGE("消息发送失败 %d", ret);
  128. luat_heap_opt_free(ch->pkg_mem_type, cs);
  129. }
  130. }
  131. static void ch390h_dataout(luat_netdrv_t* drv, void* userdata, uint8_t* buff, uint16_t len) {
  132. ch390h_t* ch = (ch390h_t*)userdata;
  133. luat_ch390h_cstring_t* cs = new_cstring(ch, len);
  134. if (cs == NULL) {
  135. return;
  136. }
  137. cs->len = len;
  138. memcpy(cs->buff, buff, len);
  139. send_msg_cs(ch, cs);
  140. }
  141. static void ch390h_dataout_pbuf(ch390h_t* ch, struct pbuf* p) {
  142. luat_ch390h_cstring_t* cs = new_cstring(ch, p->tot_len);
  143. if (cs == NULL) {
  144. return;
  145. }
  146. cs->len = p->tot_len;
  147. pbuf_copy_partial(p, cs->buff, p->tot_len, 0);
  148. send_msg_cs(ch, cs);
  149. }
  150. err_t ch390_netif_output(struct netif *netif, struct pbuf *p) {
  151. // LLOGD("lwip待发送数据 %p %d", p, p->tot_len);
  152. ch390h_t* ch = NULL;
  153. for (size_t i = 0; i < MAX_CH390H_NUM; i++)
  154. {
  155. ch = ch390h_drvs[i];
  156. if (ch == NULL) {
  157. continue;
  158. }
  159. if (ch->netdrv->netif != netif) {
  160. continue;
  161. }
  162. ch390h_dataout_pbuf(ch, p);
  163. break;
  164. }
  165. return 0;
  166. }
  167. static void netdrv_netif_input(void* args) {
  168. netdrv_pkg_msg_t* ptr = (netdrv_pkg_msg_t*)args;
  169. struct pbuf* p = pbuf_alloc(PBUF_TRANSPORT, ptr->len, PBUF_RAM);
  170. if (p == NULL) {
  171. LLOGD("分配pbuf失败!!! %d", ptr->len);
  172. luat_heap_free(ptr);
  173. return;
  174. }
  175. pbuf_take(p, ptr->buff, ptr->len);
  176. // LLOGD("数据注入到netif " MACFMT, MAC_ARG(p->payload));
  177. int ret = ptr->netif->input(p, ptr->netif);
  178. if (ret) {
  179. pbuf_free(p);
  180. }
  181. luat_heap_free(ptr);
  182. }
  183. static int check_vid_pid(ch390h_t* ch) {
  184. uint8_t buff[6] = {0};
  185. luat_ch390h_read_vid_pid(ch, buff);
  186. if (0 == memcmp(buff, "\x00\x1C\x51\x91", 4)) {
  187. return 0; // 第一次就读取成功, 那就马上返回
  188. }
  189. // 再读一次
  190. luat_ch390h_read_vid_pid(ch, buff);
  191. if (0 != memcmp(buff, "\x00\x1C\x51\x91", 4)) {
  192. uint64_t tnow = luat_mcu_tick64_ms();
  193. if (tnow - warn_vid_pid_tm > 2000) {
  194. LLOGE("读取vid/pid失败!请检查接线!! %d %d %02X%02X%02X%02X", ch->spiid, ch->cspin, buff[0], buff[1], buff[2], buff[3]);
  195. warn_vid_pid_tm = tnow;
  196. }
  197. return -1;
  198. }
  199. // LLOGE("读取vid/pid成功!!! %d %d %02X%02X%02X%02X", ch->spiid, ch->cspin, buff[0], buff[1], buff[2], buff[3]);
  200. return 0;
  201. }
  202. static int task_loop_one(ch390h_t* ch, luat_ch390h_cstring_t* cs) {
  203. uint8_t buff[32] = {0};
  204. int ret = 0;
  205. uint16_t len = 0;
  206. // LLOGD("状态 spi %d cs %d stat %d", ch->spiid, ch->cspin, ch->status);
  207. // 首先, 判断设备状态
  208. if (ch->status == 0) {
  209. // 状态0, 代表刚加入, 还没成功通信过!!
  210. ch390h_bootup(ch);
  211. luat_ch390h_software_reset(ch);
  212. if (check_vid_pid(ch)) {
  213. return 0;
  214. }
  215. luat_rtos_task_sleep(10);
  216. // 读取MAC地址, 开始初始化
  217. luat_ch390h_read_mac(ch, buff);
  218. size_t tmpc = 0;
  219. for (size_t i = 0; i < 6; i++)
  220. {
  221. if (buff[i] == 0) {
  222. tmpc ++;
  223. if (tmpc == 2) {
  224. LLOGD("非法MAC地址 %02X%02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  225. return 0;
  226. }
  227. }
  228. }
  229. luat_ch390h_read_mac(ch, buff + 6);
  230. luat_ch390h_read_mac(ch, buff + 12);
  231. if (memcmp(buff, buff+6, 6) || memcmp(buff, buff+12, 6)) {
  232. LLOGE("读取3次mac地址不匹配!!! %02X%02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  233. return 0;
  234. }
  235. LLOGD("初始化MAC %02X%02X%02X%02X%02X%02X", buff[0], buff[1], buff[2], buff[3], buff[4], buff[5]);
  236. // TODO 判断mac是否合法
  237. memcpy(ch->netdrv->netif->hwaddr, buff, 6);
  238. ch->status = 2;
  239. ch->netdrv->dataout = ch390h_dataout;
  240. luat_ch390h_basic_config(ch);
  241. luat_ch390h_set_phy(ch, 1);
  242. luat_ch390h_set_rx(ch, 1);
  243. if (ch->intpin != 255) {
  244. luat_ch390h_write_reg(ch, 0x7F, 1); // 开启接收中断
  245. }
  246. return 0; // 等待下一个周期
  247. }
  248. if (check_vid_pid(ch)) {
  249. // TODO 是不是应该恢复到状态0
  250. return 0;
  251. }
  252. if (ch->status == 3) {
  253. LLOGD("request ch390 reset spi%d cs%d", ch->spiid, ch->cspin);
  254. luat_ch390h_software_reset(ch);
  255. ch->status = 2;
  256. luat_rtos_task_sleep(10);
  257. return 0;
  258. }
  259. if (ch->status != 2) {
  260. // 处于中间状态, 暂不管它
  261. LLOGI("wait for netif init %d %d", ch->spiid, ch->cspin);
  262. return 0;
  263. }
  264. // 然后判断link的状态
  265. luat_ch390h_read(ch, 0x01, 1, buff);
  266. uint8_t NSR = buff[0];
  267. // LLOGD("网络状态寄存器 %02X %d", buff[0], (NSR & (1 << 6)) != 0);
  268. if (0 == (NSR & (1 << 6))) {
  269. // 网线没插, 或者phy没有上电
  270. // 首先, 确保phy上电
  271. // luat_ch390h_read(ch, 0x1F, 1, buff);
  272. // LLOGD("PHY状态 %02X", buff[0]);
  273. luat_ch390h_set_phy(ch, 1);
  274. luat_ch390h_set_rx(ch, 1);
  275. if (netif_is_link_up(ch->netdrv->netif)) {
  276. LLOGI("link is down %d %d %p", ch->spiid, ch->cspin, ch->netdrv->netif);
  277. luat_netdrv_set_link_updown(ch->netdrv, 0);
  278. }
  279. return 0; // 网络断了, 没那么快恢复的, 等吧
  280. }
  281. if (!netif_is_link_up(ch->netdrv->netif)) {
  282. LLOGI("link is up %d %d %s", ch->spiid, ch->cspin, (NSR & (1<<7)) ? "10M" : "100M");
  283. luat_netdrv_set_link_updown(ch->netdrv, 1);
  284. }
  285. if (cs) {
  286. // LLOGD("数据写入 %p %d", cs->buff, cs->len);
  287. luat_ch390h_write_pkg(ch, cs->buff, cs->len);
  288. }
  289. // 有没有数据待读取
  290. if (NSR & 0x01) {
  291. ret = luat_ch390h_read_pkg(ch, ch->rxbuff, &len);
  292. if (ret) {
  293. LLOGE("读数据包报错,立即复位模组 ret %d spi %d cs %d", ret, ch->spiid, ch->cspin);
  294. luat_ch390h_write_reg(ch, 0x05, 0);
  295. luat_ch390h_write_reg(ch, 0x55, 1);
  296. luat_ch390h_write_reg(ch, 0x75, 0);
  297. luat_rtos_task_sleep(1); // 是否真的需要呢??
  298. luat_ch390h_basic_config(ch);
  299. luat_ch390h_set_phy(ch, 1);
  300. luat_ch390h_set_rx(ch, 1);
  301. if (ch->intpin != 255) {
  302. luat_ch390h_write_reg(ch, 0x7F, 1); // 开启接收中断
  303. }
  304. return 0;
  305. }
  306. if (len > 0) {
  307. NETDRV_STAT_IN(ch->netdrv, len);
  308. // 收到数据, 开始后续处理
  309. //print_erp_pkg(ch->rxbuff, len);
  310. // 先经过netdrv过滤器
  311. // LLOGD("ETH数据包 " MACFMT " " MACFMT " %02X%02X", MAC_ARG(ch->rxbuff), MAC_ARG(ch->rxbuff + 6), ((uint16_t)ch->rxbuff[6]) + (((uint16_t)ch->rxbuff[7])));
  312. ret = luat_netdrv_napt_pkg_input(ch->adapter_id, ch->rxbuff, len - 4);
  313. // LLOGD("napt ret %d", ret);
  314. if (ret != 0) {
  315. // 不需要输入到LWIP了
  316. // LLOGD("napt说不需要注入lwip了");
  317. }
  318. else {
  319. // 如果返回值是0, 那就是继续处理, 输入到netif
  320. ret = luat_netdrv_netif_input_proxy(ch->netdrv->netif, ch->rxbuff, len - 4);
  321. if (ret) {
  322. LLOGE("luat_netdrv_netif_input_proxy 返回错误!!! ret %d", ret);
  323. return 1;
  324. }
  325. }
  326. }
  327. // 很好, RX数据处理完成了
  328. }
  329. else {
  330. // LLOGD("没有数据待读取");
  331. }
  332. if (ch->intpin != 255) {
  333. luat_ch390h_write_reg(ch, 0x7E, 0x3F); // 清除中断
  334. }
  335. // 这一轮处理完成了
  336. // 如果rx有数据, 那就不要等待, 立即开始下一轮
  337. if (NSR & 0x01 || cs) {
  338. return 1;
  339. }
  340. return 0;
  341. }
  342. static int task_loop(ch390h_t *ch, luat_ch390h_cstring_t* cs) {
  343. int ret = 0;
  344. for (size_t i = 0; i < MAX_CH390H_NUM; i++)
  345. {
  346. if (ch390h_drvs[i] != NULL && ch390h_drvs[i]->init_step) {
  347. ret += task_loop_one(ch390h_drvs[i], ch == ch390h_drvs[i] ? cs : NULL);
  348. }
  349. }
  350. if (ret) {
  351. pkg_evt_t evt = {0};
  352. size_t t = 0;
  353. luat_rtos_queue_get_cnt(qt, &t);
  354. if (t < 4) {
  355. luat_rtos_queue_send(qt, &evt, sizeof(pkg_evt_t), 0);
  356. }
  357. }
  358. return ret;
  359. }
  360. static int task_wait_msg(uint32_t timeout) {
  361. luat_ch390h_cstring_t* cs = NULL;
  362. ch390h_t *ch = NULL;
  363. pkg_evt_t evt = {0};
  364. int ret = luat_rtos_queue_recv(qt, &evt, sizeof(pkg_evt_t), timeout);
  365. // LLOGD("evt id %d ret %d timeout %d", evt.id, ret, timeout);
  366. if (ret == 0 && evt.id == 1) {
  367. // 收到消息了
  368. ch = (ch390h_t *)evt.ch;
  369. cs = (luat_ch390h_cstring_t*)evt.cs;
  370. // LLOGD("收到消息 %p %p", ch, cs);
  371. ret = task_loop(ch, cs);
  372. if (cs) {
  373. // remain_tx_size -= cs->len;
  374. luat_heap_opt_free(ch->pkg_mem_type, cs);
  375. cs = NULL;
  376. }
  377. return 1; // 拿到消息, 那队列里可能还有消息, 马上执行下一轮操作
  378. }
  379. else {
  380. // if (evt.id == 2) {
  381. // LLOGD("CH390中断触发");
  382. // }
  383. ret = task_loop(NULL, NULL);
  384. }
  385. return ret;
  386. }
  387. static void ch390_task_main(void* args) {
  388. (void)args;
  389. int ret = 0;
  390. uint32_t count = 0;
  391. // 延时30ms,等待CH390H芯片完全启动,避免直接初始化导致异常
  392. luat_rtos_task_sleep(30);
  393. while (1) {
  394. count ++;
  395. if (count % 10 == 0) {
  396. luat_wdt_feed();
  397. }
  398. if (count > 1024) {
  399. // 每隔1024次循环, 休眠10ms, 不然CP会被饿死
  400. if (ret) {
  401. // LLOGD("强制休眠20ms");
  402. // luat_rtos_task_sleep(10);
  403. }
  404. count = 0;
  405. }
  406. if (s_ch390h_mode == 0) {
  407. ret = task_wait_msg(5);
  408. }
  409. else {
  410. ret = task_wait_msg(1000);
  411. }
  412. }
  413. }
  414. void luat_ch390h_task_start(void) {
  415. int ret = 0;
  416. if (ch390h_task_handle == NULL) {
  417. // 为所有CH390H设备初始化pkg_mem_type
  418. size_t total = 0;
  419. size_t used = 0;
  420. size_t max_used = 0;
  421. luat_meminfo_opt_sys(LUAT_HEAP_PSRAM, &total, &used, &max_used);
  422. int default_mem_type = (total > 1024 * 512) ? LUAT_HEAP_PSRAM : LUAT_HEAP_AUTO;
  423. for (size_t i = 0; i < MAX_CH390H_NUM; i++) {
  424. if (ch390h_drvs[i] != NULL) {
  425. ch390h_drvs[i]->pkg_mem_type = default_mem_type;
  426. }
  427. }
  428. ret = luat_rtos_queue_create(&qt, 1024, sizeof(pkg_evt_t));
  429. if (ret) {
  430. LLOGE("queue create fail %d", ret);
  431. return;
  432. }
  433. ret = luat_rtos_task_create(&ch390h_task_handle, 8*1024, 50, "ch390h", ch390_task_main, NULL, 0);
  434. if (ret) {
  435. LLOGE("task create fail %d", ret);
  436. return;
  437. }
  438. LLOGD("task started");
  439. }
  440. }
  441. // 辅助函数
  442. #if 0
  443. static void print_erp_pkg(uint8_t* buff, uint16_t len) {
  444. // LLOGD("pkg len %d head " MACFMT " " MACFMT, len, MAC_ARG(buff), MAC_ARG(buff+6));
  445. if (len < 24 || len > 1600) {
  446. LLOGW("非法的pkg长度 %d", len);
  447. return;
  448. }
  449. struct eth_hdr* eth = (struct eth_hdr*)buff;
  450. struct ip_hdr* iphdr = (struct ip_hdr*)(buff + SIZEOF_ETH_HDR);
  451. struct etharp_hdr* arp = (struct etharp_hdr*)(buff + SIZEOF_ETH_HDR);
  452. // LLOGD("eth " MACFMT " -> " MACFMT " tp %02X", MAC_ARG(eth->src.addr), MAC_ARG(eth->dest.addr), (u16_t)lwip_htons(eth->type));
  453. switch (eth->type) {
  454. case PP_HTONS(ETHTYPE_IP):
  455. // LLOGD(" ipv%d %d len %d", (u16_t)IPH_V(iphdr), (u16_t)IPH_PROTO(iphdr),(u16_t)IPH_LEN(iphdr));
  456. break;
  457. case PP_HTONS(ETHTYPE_ARP):
  458. // LLOGD(" arp proto %d", arp->proto);
  459. break;
  460. }
  461. }
  462. #endif