ethernet_demo.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*============================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =============================================================================*/
  5. /*===========================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ---------- ------------ ----------------------------------------------------
  11. =============================================================================*/
  12. /*===========================================================================
  13. * include files
  14. ===========================================================================*/
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include "ql_api_osi.h"
  19. #include "ql_log.h"
  20. #include "ql_gpio.h"
  21. #include "ql_api_spi.h"
  22. #include "ethernet_demo_macro.h"
  23. #include "ql_api_ethernet.h"
  24. #include "ql_api_datacall.h"
  25. #include "ql_api_nw.h"
  26. #include "ql_power.h"
  27. #include "sockets.h"
  28. #include "lwip/ip_addr.h"
  29. #include "lwip/ip6_addr.h"
  30. #include "lwip/inet.h"
  31. #include "lwip/tcp.h"
  32. #ifdef ETHERNET_PHY_CH395
  33. #include "CH395.H"
  34. #endif
  35. /*========================================================================
  36. * Macro Definition
  37. *========================================================================*/
  38. #define QL_ETHERNET_LOG_LEVEL QL_LOG_LEVEL_INFO
  39. #define QL_ETHERNET_DEMO_LOG(msg, ...) QL_LOG(QL_ETHERNET_LOG_LEVEL, "phy_demo", msg, ##__VA_ARGS__)
  40. ql_task_t ethernet_phy_task = NULL;
  41. #ifdef ETHERNET_PHY_CH395
  42. // SPI
  43. #define QL_CUR_SPI_PORT QL_SPI_PORT1
  44. #define QL_CUR_SPI_CS_PIN QL_CUR_SPI1_CS_PIN
  45. #define QL_CUR_SPI_CS_FUNC QL_CUR_SPI1_CS_FUNC
  46. #define QL_CUR_SPI_CLK_PIN QL_CUR_SPI1_CLK_PIN
  47. #define QL_CUR_SPI_CLK_FUNC QL_CUR_SPI1_CLK_FUNC
  48. #define QL_CUR_SPI_DO_PIN QL_CUR_SPI1_DO_PIN
  49. #define QL_CUR_SPI_DO_FUNC QL_CUR_SPI1_DO_FUNC
  50. #define QL_CUR_SPI_DI_PIN QL_CUR_SPI1_DI_PIN
  51. #define QL_CUR_SPI_DI_FUNC QL_CUR_SPI1_DI_FUNC
  52. // GPIO
  53. #define QL_CUR_INT_PIN 59
  54. #define QL_CUR_INT_PIN_GPIO_FUNC 0
  55. #define QL_CUR_INT_GPIO_NUM GPIO_2
  56. #define QL_ETHERNET_HOT_PLUG_PIN 121
  57. #define QL_ETHERNET_HOT_PLUG_PIN_GPIO_FUNC 0
  58. #define QL_ETHERNET_HOT_PLUG_GPIO_NUM GPIO_23
  59. #define QL_ETHERNET_HOT_PLUG_DEBOUNCE_TIME 100
  60. // TCP
  61. #define QL_ETHERNET_PHY_TCP_IP4_SIZE 4
  62. #define QL_ETHERNET_PHY_TCP_CLIENTS_CNT 1
  63. #endif
  64. /*========================================================================
  65. * Enumeration Definition
  66. *========================================================================*/
  67. /*========================================================================
  68. * Type Definition
  69. *=========================================================================*/
  70. #ifdef ETHERNET_PHY_CH395
  71. typedef struct
  72. {
  73. int id;
  74. int fd;
  75. int close_flag;
  76. uint8_t srcip[QL_ETHERNET_PHY_TCP_IP4_SIZE];
  77. uint16_t srcport;
  78. uint8_t destip[QL_ETHERNET_PHY_TCP_IP4_SIZE];
  79. uint16_t destport;
  80. ql_task_t task;
  81. struct sockaddr_in addr_in;
  82. } ethernet_phy_tcp_ctx_s;
  83. typedef struct
  84. {
  85. ql_spi_port_e port;
  86. ql_LvlMode lvl;
  87. }ethernet_phy_spi_cs_s;
  88. typedef struct
  89. {
  90. ql_task_t task;
  91. #ifdef ETHERNET_PHY_CH395
  92. ql_mutex_t mutex;
  93. // TCP
  94. ethernet_phy_tcp_ctx_s client_ctx[QL_ETHERNET_PHY_TCP_CLIENTS_CNT];
  95. int client_num;
  96. // Debounce
  97. ql_timer_t timer;
  98. ql_GpioNum gpio_num;
  99. #endif
  100. } ethernet_phy_manager_s;
  101. #endif
  102. /*========================================================================
  103. * Global Variable
  104. *========================================================================*/
  105. ethernet_phy_manager_s ethernet_phy_manager = {0};
  106. #ifdef ETHERNET_PHY_CH395
  107. static char send_buf[128]={0};
  108. static int send_len = 0;
  109. static char recv_buf[128]={0};
  110. static int recv_len = 0;
  111. #endif
  112. /*========================================================================
  113. * function Definition
  114. *========================================================================*/
  115. #ifdef ETHERNET_PHY_CH395
  116. void ethernet_phy_create_mutex(ql_mutex_t *lockaddr)
  117. {
  118. if (!lockaddr)
  119. {
  120. return;
  121. }
  122. if (NULL == *lockaddr)
  123. {
  124. ql_rtos_mutex_create(lockaddr);
  125. }
  126. }
  127. void ethernet_phy_delete_mutex(ql_mutex_t lock)
  128. {
  129. if (NULL == lock)
  130. {
  131. return;
  132. }
  133. ql_rtos_mutex_delete(lock);
  134. }
  135. void ethernet_phy_try_lock(ql_mutex_t lock)
  136. {
  137. if (NULL == lock)
  138. {
  139. return;
  140. }
  141. ql_rtos_mutex_lock(lock, 0xffffffffUL);
  142. }
  143. void ethernet_phy_unlock(ql_mutex_t lock)
  144. {
  145. if (NULL == lock)
  146. {
  147. return;
  148. }
  149. ql_rtos_mutex_unlock(lock);
  150. }
  151. void ethernet_phy_send_event(uint32_t id, uint32_t param1, uint32_t param2, uint32_t param3)
  152. {
  153. ethernet_phy_manager_s *manager = &ethernet_phy_manager;
  154. ql_event_t event;
  155. event.id = id;
  156. event.param1 = param1;
  157. event.param2 = param2;
  158. event.param3 = param3;
  159. ethernet_phy_try_lock(manager->mutex);
  160. ql_rtos_event_send(manager->task, &event);
  161. ethernet_phy_unlock(manager->mutex);
  162. }
  163. void ethernet_phy_ch395_app_reset_cb(void *ctx)
  164. {
  165. // Do hardware reset here.
  166. QL_ETHERNET_DEMO_LOG("ch395 reset cb");
  167. }
  168. void ethernet_phy_ch395_app_notify_cb(void *ctx)
  169. {
  170. ch395_app_net_status_e status = *((ch395_app_net_status_e *)ctx);
  171. QL_ETHERNET_DEMO_LOG("get phy status: %d", status);
  172. if (status == CH395_APP_NET_CONNECTED)
  173. {
  174. ethernet_phy_send_event(QUEC_ETHERNET_APP_CONNECTED, 0, 0, 0);
  175. }
  176. else if (status == CH395_APP_NET_DISCONNECTED)
  177. {
  178. ethernet_phy_send_event(QUEC_ETHERNET_APP_DISCONNECTED, 0, 0, 0);
  179. }
  180. }
  181. void ethernet_phy_hot_plug_timer_cb(void *ctx)
  182. {
  183. ql_GpioNum gpio_num = *((ql_GpioNum*)ctx);
  184. ql_LvlMode lvl = LVL_LOW;
  185. ql_gpio_get_level(QL_ETHERNET_HOT_PLUG_GPIO_NUM, &lvl);
  186. if(lvl == LVL_LOW)
  187. {
  188. QL_ETHERNET_DEMO_LOG("eth plug in");
  189. ch395_app_reset();
  190. }
  191. else
  192. {
  193. QL_ETHERNET_DEMO_LOG("eth plug out");
  194. }
  195. if(QL_GPIO_SUCCESS !=ql_int_enable(gpio_num))
  196. {
  197. return;
  198. }
  199. }
  200. void ethernet_phy_hot_plug_cb(void *ctx)
  201. {
  202. ql_GpioNum gpio_num = *((ql_GpioNum*)ctx);
  203. if(QL_GPIO_SUCCESS !=ql_int_disable(gpio_num))
  204. {
  205. return;
  206. }
  207. ethernet_phy_manager_s *manager = &ethernet_phy_manager;
  208. if(manager->timer == NULL || ql_rtos_timer_is_running(manager->timer))
  209. {
  210. return;
  211. }
  212. QL_ETHERNET_DEMO_LOG("hot plug debounce");
  213. ql_rtos_timer_start(manager->timer, QL_ETHERNET_HOT_PLUG_DEBOUNCE_TIME, 0);
  214. }
  215. int ethernet_phy_int_gpio(void* cb,void *ctx)
  216. {
  217. int err = -1;
  218. if(QL_GPIO_SUCCESS != ql_pin_set_func(QL_CUR_INT_PIN, QL_CUR_INT_PIN_GPIO_FUNC))
  219. {
  220. goto exit;
  221. }
  222. if(QL_GPIO_SUCCESS != ql_int_register(QL_CUR_INT_GPIO_NUM,EDGE_TRIGGER,DEBOUNCE_DIS,EDGE_FALLING, PULL_UP,cb,ctx))
  223. {
  224. goto exit;
  225. }
  226. if(QL_GPIO_SUCCESS != ql_int_enable(QL_CUR_INT_GPIO_NUM))
  227. {
  228. goto exit;
  229. }
  230. // ch395 hot plug
  231. /*
  232. ethernet_phy_manager_s *manager = &ethernet_phy_manager;
  233. manager->gpio_num = QL_ETHERNET_HOT_PLUG_GPIO_NUM;
  234. if (QL_GPIO_SUCCESS != ql_pin_set_func(QL_ETHERNET_HOT_PLUG_PIN, QL_ETHERNET_HOT_PLUG_PIN_GPIO_FUNC))
  235. {
  236. goto exit;
  237. }
  238. if (QL_GPIO_SUCCESS != ql_int_register(QL_ETHERNET_HOT_PLUG_GPIO_NUM, EDGE_TRIGGER, DEBOUNCE_EN, EDGE_BOTH, PULL_UP, ethernet_phy_hot_plug_cb, &(manager->gpio_num)))
  239. {
  240. goto exit;
  241. }
  242. if (QL_GPIO_SUCCESS != ql_int_enable(QL_ETHERNET_HOT_PLUG_GPIO_NUM))
  243. {
  244. goto exit;
  245. }
  246. */
  247. err = 0;
  248. exit:
  249. return err;
  250. }
  251. void ethernet_phy_set_cs(void *ctx)
  252. {
  253. if(!ctx)
  254. {
  255. return;
  256. }
  257. ql_LvlMode lvl = (ql_LvlMode)(*((uint8_t*)ctx));
  258. if(lvl == LVL_HIGH)
  259. {
  260. ql_spi_cs_high(QL_CUR_SPI_PORT);
  261. }
  262. else
  263. {
  264. ql_spi_cs_low(QL_CUR_SPI_PORT);
  265. }
  266. }
  267. static void ethernet_phy_client_thread(void *argv)
  268. {
  269. ethernet_phy_manager_s *manager = &ethernet_phy_manager;
  270. ethernet_phy_tcp_ctx_s *ctx = (ethernet_phy_tcp_ctx_s *)argv;
  271. ctx->close_flag = 0;
  272. int i = 0;
  273. int ret = 0;
  274. int socket_fd = -1;
  275. int flags = 0;
  276. int connected = 0;
  277. fd_set read_fds;
  278. fd_set write_fds;
  279. fd_set exp_fds;
  280. int fd_changed = 0;
  281. int closing = false;
  282. struct sockaddr_in local4, server_ipv4;
  283. ip4_addr_t int_srcip;
  284. ip4_addr_t int_destip;
  285. IP4_ADDR(&int_srcip, ctx->srcip[0],ctx->srcip[1],ctx->srcip[2],ctx->srcip[3]);
  286. IP4_ADDR(&int_destip, ctx->destip[0],ctx->destip[1],ctx->destip[2],ctx->destip[3]);
  287. FD_ZERO(&read_fds);
  288. FD_ZERO(&write_fds);
  289. FD_ZERO(&exp_fds);
  290. memset(&local4, 0x00, sizeof(struct sockaddr_in));
  291. local4.sin_family = AF_INET;
  292. local4.sin_port = htons(ctx->srcport);;
  293. memcpy(&(local4.sin_addr),&int_srcip,sizeof(int_srcip));
  294. QL_ETHERNET_DEMO_LOG("socket start!");
  295. socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  296. if(socket_fd < 0)
  297. {
  298. goto exit;
  299. }
  300. ret = bind(socket_fd,(struct sockaddr *)&local4,sizeof(struct sockaddr));
  301. if(ret < 0)
  302. {
  303. close(socket_fd);
  304. socket_fd = -1;
  305. goto exit;
  306. }
  307. flags |= O_NONBLOCK;
  308. fcntl(socket_fd, F_SETFL,flags);
  309. memset(&server_ipv4, 0x00, sizeof(struct sockaddr_in));
  310. server_ipv4.sin_family = AF_INET;
  311. server_ipv4.sin_port = htons(ctx->destport);
  312. memcpy(&(server_ipv4.sin_addr),&int_destip,sizeof(int_destip));
  313. ret = connect(socket_fd, (struct sockaddr *)&server_ipv4, sizeof(server_ipv4));
  314. if(ret == 0)
  315. {
  316. connected = 1;
  317. }
  318. else
  319. {
  320. if(lwip_get_error(socket_fd) != EINPROGRESS)
  321. {
  322. close(socket_fd);
  323. socket_fd = -1;
  324. goto exit;
  325. }
  326. }
  327. if(connected == 1)
  328. {
  329. ethernet_phy_try_lock(manager->mutex);
  330. ctx->fd = socket_fd;
  331. ethernet_phy_unlock(manager->mutex);
  332. FD_SET(socket_fd, &read_fds);
  333. QL_ETHERNET_DEMO_LOG("=====connect to \"tcp\" success=====");
  334. memset(send_buf, 0x00, 128);
  335. send_len = snprintf(send_buf, 128,"%d%s%d\r\n",i,"startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend",i);
  336. if(write(socket_fd, send_buf, send_len) != send_len){
  337. FD_SET(socket_fd, &write_fds);
  338. }else{
  339. i++;
  340. }
  341. }
  342. else
  343. {
  344. FD_SET(socket_fd, &write_fds);
  345. }
  346. FD_SET(socket_fd, &exp_fds);
  347. while(1)
  348. {
  349. if(ctx->close_flag)
  350. {
  351. QL_ETHERNET_DEMO_LOG("client close flag");
  352. goto exit;
  353. }
  354. if(connected == 1)
  355. {
  356. FD_SET(socket_fd, &read_fds);
  357. }
  358. else
  359. {
  360. FD_SET(socket_fd, &write_fds);
  361. }
  362. FD_SET(socket_fd, &exp_fds);
  363. struct timeval timeout = {3,0};
  364. fd_changed = select(socket_fd+1, &read_fds, &write_fds, &exp_fds, &timeout);
  365. if(fd_changed > 0)
  366. {
  367. if(FD_ISSET(socket_fd, &write_fds))
  368. {
  369. FD_CLR(socket_fd, &write_fds);
  370. if(connected== 0)
  371. {
  372. int value = 0;
  373. int len = 0;
  374. len = sizeof(value);
  375. getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &value, &len);
  376. if(value == 0 || value == EISCONN )
  377. {
  378. ethernet_phy_try_lock(manager->mutex);
  379. ctx->fd = socket_fd;
  380. ethernet_phy_unlock(manager->mutex);
  381. QL_ETHERNET_DEMO_LOG("=====connect to \"192.168.1.200:8252\" success=====");
  382. connected = 1;
  383. FD_SET(socket_fd, &read_fds);
  384. memset(send_buf, 0x00, 128);
  385. send_len = snprintf(send_buf, 128,"%d%s%d\r\n",i,"startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend",i);
  386. write(socket_fd, send_buf, send_len);
  387. i++;
  388. }
  389. else
  390. {
  391. QL_ETHERNET_DEMO_LOG("=====connect to \"192.168.1.200:8252\" failed=====");
  392. close(socket_fd);
  393. socket_fd = -1;
  394. break;
  395. }
  396. }
  397. else
  398. {
  399. memset(send_buf, 0x00, 128);
  400. send_len = snprintf(send_buf, 128,"%d%s%d\r\n",i,"startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend",i);
  401. write(socket_fd, send_buf, send_len);
  402. i++;
  403. }
  404. }
  405. else if(FD_ISSET(socket_fd, &read_fds))
  406. {
  407. FD_CLR(socket_fd, &read_fds);
  408. memset(recv_buf,0x00, 128);
  409. recv_len = read(socket_fd, recv_buf, 128);
  410. if(recv_len > 0)
  411. {
  412. QL_ETHERNET_DEMO_LOG(">>>>Recv: %s", recv_buf);
  413. memset(send_buf, 0x00, 128);
  414. send_len = snprintf(send_buf, 128,"%d%s%d\r\n",i,"startAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAend",i);
  415. write(socket_fd, send_buf, send_len);
  416. i++;
  417. FD_SET(socket_fd, &read_fds);
  418. }
  419. else if(recv_len == 0)
  420. {
  421. if(closing == false)
  422. {
  423. QL_ETHERNET_DEMO_LOG("===passive close!!!!");
  424. shutdown(socket_fd, SHUT_WR);
  425. closing = true;
  426. FD_SET(socket_fd, &read_fds);
  427. }
  428. else
  429. {
  430. close(socket_fd);
  431. socket_fd = -1;
  432. break;
  433. }
  434. }
  435. else
  436. {
  437. if(lwip_get_error(socket_fd) == EAGAIN)
  438. {
  439. FD_SET(socket_fd, &read_fds);
  440. }
  441. else
  442. {
  443. close(socket_fd);
  444. socket_fd = -1;
  445. break;
  446. }
  447. }
  448. }
  449. else if(FD_ISSET(socket_fd, &exp_fds))
  450. {
  451. FD_CLR(socket_fd, &exp_fds);
  452. close(socket_fd);
  453. socket_fd = -1;
  454. break;
  455. }
  456. }
  457. }
  458. exit:
  459. QL_ETHERNET_DEMO_LOG("client end");
  460. close(socket_fd);
  461. ethernet_phy_try_lock(manager->mutex);
  462. memset(ctx, 0, sizeof(ethernet_phy_tcp_ctx_s));
  463. ctx->id = -1;
  464. ctx->fd = -1;
  465. manager->client_num--;
  466. ethernet_phy_unlock(manager->mutex);
  467. ql_rtos_task_delete(NULL);
  468. }
  469. int ethernet_phy_tcp_client_create(void *argv)
  470. {
  471. if (!argv)
  472. {
  473. QL_ETHERNET_DEMO_LOG("client param err");
  474. return -1;
  475. }
  476. ethernet_phy_tcp_ctx_s *ctx = (ethernet_phy_tcp_ctx_s *)argv;
  477. QlOSStatus err = QL_OSI_SUCCESS;
  478. err = ql_rtos_task_create(&(ctx->task), 4 * 1024, APP_PRIORITY_NORMAL, "phy_client", ethernet_phy_client_thread, argv, 10);
  479. if (err != QL_OSI_SUCCESS)
  480. {
  481. QL_ETHERNET_DEMO_LOG("task created failed");
  482. return -1;
  483. }
  484. return 0;
  485. }
  486. #endif
  487. static void ethernet_phy_thread(void * argv)
  488. {
  489. ethernet_phy_manager_s *manager = &ethernet_phy_manager;
  490. #ifdef ETHERNET_PHY_CH395
  491. uint8_t srcip[] = {192,168,1,100};
  492. uint16_t srcport = 0; //Zero for random port.
  493. uint8_t destip[] = {192,168,1,200};
  494. uint16_t destport = 8252;
  495. uint8_t gw[] = {192,168,1,1};
  496. uint8_t netmask[] = {255,255,255,0};
  497. ethernet_phy_create_mutex(&(manager->mutex));
  498. /*
  499. ch395q init
  500. */
  501. ql_ethernet_phy_s spi_ctx =
  502. {
  503. .mode = QL_ETHERNET_PHY_HW_SPI_MODE,
  504. .hw_spi.mosi_pin_num = QL_CUR_SPI_DO_PIN,
  505. .hw_spi.mosi_func_sel = QL_CUR_SPI_DO_FUNC,
  506. .hw_spi.miso_pin_num = QL_CUR_SPI_DI_PIN,
  507. .hw_spi.miso_func_sel = QL_CUR_SPI_DO_FUNC,
  508. .hw_spi.clk_pin_num = QL_CUR_SPI_CLK_PIN,
  509. .hw_spi.clk_func_sel = QL_CUR_SPI_CLK_FUNC,
  510. .hw_spi.cs_pin_num = QL_CUR_SPI_CS_PIN,
  511. .hw_spi.cs_func_sel = QL_CUR_SPI_CS_FUNC,
  512. /*********************************************/
  513. .hw_spi.config.input_mode = QL_SPI_INPUT_TRUE,
  514. .hw_spi.config.port = QL_CUR_SPI_PORT,
  515. .hw_spi.config.spiclk = QL_SPI_CLK_1_5625MHZ,
  516. #if QL_SPI_16BIT_DMA
  517. .hw_spi.config.framesize = 16,
  518. #else
  519. .hw_spi.config.framesize = 8,
  520. #endif
  521. .hw_spi.config.cs_polarity0 = QL_SPI_CS_ACTIVE_LOW,
  522. .hw_spi.config.cs_polarity1 = QL_SPI_CS_ACTIVE_LOW,
  523. .hw_spi.config.cpol = QL_SPI_CPOL_LOW,
  524. .hw_spi.config.cpha = QL_SPI_CPHA_1Edge,
  525. .hw_spi.config.input_sel = QL_SPI_DI_1,
  526. .hw_spi.config.transmode = QL_SPI_DMA_IRQ,
  527. .hw_spi.config.cs = QL_SPI_CS0,
  528. .hw_spi.config.clk_delay = QL_SPI_CLK_DELAY_0,
  529. };
  530. if(QL_ETHERNET_SUCCESS != ql_ethernet_phy_init(&spi_ctx))
  531. {
  532. goto exit;
  533. }
  534. if(QL_OSI_SUCCESS != ql_rtos_timer_create(&(manager->timer),manager->task,ethernet_phy_hot_plug_timer_cb,&(manager->gpio_num)))
  535. {
  536. QL_ETHERNET_DEMO_LOG("timer create fail!");
  537. goto exit;
  538. }
  539. if (!ch395_app_cb_register(CH395_APP_CB_TYPE_RESET, ethernet_phy_ch395_app_reset_cb))
  540. {
  541. QL_ETHERNET_DEMO_LOG("ch395 register fail!");
  542. goto exit;
  543. }
  544. if (!ch395_app_cb_register(CH395_APP_CB_TYPE_NOTIFY, ethernet_phy_ch395_app_notify_cb))
  545. {
  546. QL_ETHERNET_DEMO_LOG("ch395 register fail!");
  547. goto exit;
  548. }
  549. if(0 != ethernet_phy_int_gpio(ch395_app_get_gpio_cb(),NULL))
  550. {
  551. goto exit;
  552. }
  553. /*
  554. QUECTEL------------------CH395Q--------------------PC
  555. IP_LAN|192.168.1.100(port) 192.168.1.100(port) 192.168.1.200(8252)
  556. GW |192.168.1.1 192.168.1.1 Any except for 192.168.1.100
  557. */
  558. ql_ethernet_ctx_s ctx =
  559. {
  560. .ip4 = srcip,
  561. .gw = gw,
  562. .netmask = netmask,
  563. .mode = QL_ETHERNET_MODE_NONE,
  564. };
  565. if(!ch395_app_init(&ctx))
  566. {
  567. QL_ETHERNET_DEMO_LOG("ch395 init fail!");
  568. goto exit;
  569. }
  570. while(1)
  571. {
  572. ql_event_t event;
  573. if(ql_event_try_wait(&event) != 0)
  574. {
  575. continue;
  576. }
  577. if (event.id == 0)
  578. {
  579. continue;
  580. }
  581. QL_ETHERNET_DEMO_LOG("ch395 event:%x",event.id);
  582. switch(event.id)
  583. {
  584. case QUEC_ETHERNET_APP_CONNECTED:
  585. {
  586. ethernet_phy_try_lock(manager->mutex);
  587. //Find NULL task
  588. ethernet_phy_tcp_ctx_s *ctx = NULL;
  589. int i = 0;
  590. for (i = 0; i < QL_ETHERNET_PHY_TCP_CLIENTS_CNT; i++)
  591. {
  592. ctx = &(manager->client_ctx[i]);
  593. if(NULL == ctx->task)
  594. {
  595. break;
  596. }
  597. }
  598. if(i < QL_ETHERNET_PHY_TCP_CLIENTS_CNT)
  599. {
  600. memcpy(ctx->srcip, srcip, QL_ETHERNET_PHY_TCP_IP4_SIZE);
  601. ctx->srcport = srcport;
  602. memcpy(ctx->destip, destip, QL_ETHERNET_PHY_TCP_IP4_SIZE);
  603. ctx->destport = destport;
  604. if (0 == ethernet_phy_tcp_client_create((void *)ctx))
  605. {
  606. manager->client_num++;
  607. QL_ETHERNET_DEMO_LOG("client %d", manager->client_num);
  608. }
  609. else
  610. {
  611. QL_ETHERNET_DEMO_LOG("client create failed %d/%d",manager->client_num,QL_ETHERNET_PHY_TCP_CLIENTS_CNT);
  612. }
  613. }
  614. else
  615. {
  616. QL_ETHERNET_DEMO_LOG("client max %d/%d,end", manager->client_num,QL_ETHERNET_PHY_TCP_CLIENTS_CNT);
  617. }
  618. ethernet_phy_unlock(manager->mutex);
  619. break;
  620. }
  621. case QUEC_ETHERNET_APP_DISCONNECTED: {
  622. QL_ETHERNET_DEMO_LOG("phy discon");
  623. ethernet_phy_try_lock(manager->mutex);
  624. ethernet_phy_tcp_ctx_s *ctx = NULL;
  625. int i = 0;
  626. for (i = 0; i < QL_ETHERNET_PHY_TCP_CLIENTS_CNT; i++)
  627. {
  628. ctx = &(manager->client_ctx[i]);
  629. if(ctx->task)
  630. {
  631. ctx->close_flag = 1;
  632. }
  633. }
  634. ethernet_phy_unlock(manager->mutex);
  635. break;
  636. }
  637. default:
  638. {
  639. break;
  640. }
  641. }
  642. }
  643. exit:
  644. QL_ETHERNET_DEMO_LOG("ethernet demo end");
  645. if(manager->timer)
  646. {
  647. ql_rtos_timer_stop(manager->timer);
  648. ql_rtos_timer_delete(manager->timer);
  649. manager->timer = NULL;
  650. }
  651. if (manager->mutex)
  652. {
  653. ql_rtos_mutex_delete(manager->mutex);
  654. manager->mutex = NULL;
  655. }
  656. #endif
  657. if (manager->task)
  658. {
  659. manager->task = NULL;
  660. ql_rtos_task_delete(NULL);
  661. }
  662. }
  663. void ql_ethernet_demo_init(void)
  664. {
  665. ethernet_phy_manager_s *manager = &ethernet_phy_manager;
  666. QlOSStatus err = QL_OSI_SUCCESS;
  667. err = ql_rtos_task_create(&(manager->task), 4*1024, APP_PRIORITY_NORMAL, "q_phy_demo", ethernet_phy_thread, NULL, 10);
  668. if(err != QL_OSI_SUCCESS)
  669. {
  670. QL_ETHERNET_DEMO_LOG("task created failed");
  671. return;
  672. }
  673. }