w5500.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  1. #include "luat_base.h"
  2. #ifdef LUAT_USE_W5500
  3. #include "luat_rtos.h"
  4. #include "luat_zbuff.h"
  5. #include "luat_spi.h"
  6. #include "luat_gpio.h"
  7. #include "luat_network_adapter.h"
  8. #include "bsp_common.h"
  9. #include "w5500_def.h"
  10. #include "dhcp_def.h"
  11. #include "dns_def.h"
  12. #include "platform_def.h"
  13. extern void DBG_Printf(const char* format, ...);
  14. extern void DBG_HexPrintf(void *Data, unsigned int len);
  15. #define DBG(x,y...) DBG_Printf("%s %d:"x"\r\n", __FUNCTION__,__LINE__,##y)
  16. #define DBG_ERR(x,y...) DBG_Printf("%s %d:"x"\r\n", __FUNCTION__,__LINE__,##y)
  17. #define W5500_LOCK OS_SuspendTask(NULL)
  18. #define W5500_UNLOCK OS_ResumeTask(NULL)
  19. #define socket_index(n) (n << 5)
  20. #define common_reg (0)
  21. #define socket_reg (1 << 3)
  22. #define socket_tx (2 << 3)
  23. #define socket_rx (3 << 3)
  24. #define is_write (1 << 2)
  25. #define MAX_SOCK_NUM 8
  26. #define MAX_DNS_SERVER 4
  27. #define SYS_SOCK_ID 0
  28. #define MR_RST 0x80 /**< reset */
  29. #define MR_WOL 0x20 /**< Wake on Lan */
  30. #define MR_PB 0x10 /**< ping block */
  31. #define MR_PPPOE 0x08 /**< enable pppoe */
  32. #define MR_UDP_FARP 0x02 /**< enbale FORCE ARP */
  33. #define IR_CONFLICT 0x80 /**< check ip confict */
  34. #define IR_UNREACH 0x40 /**< get the destination unreachable message in UDP sending */
  35. #define IR_PPPoE 0x20 /**< get the PPPoE close message */
  36. #define IR_MAGIC 0x10 /**< get the magic packet interrupt */
  37. #define Sn_MR_CLOSE 0x00 /**< unused socket */
  38. #define Sn_MR_TCP 0x01 /**< TCP */
  39. #define Sn_MR_UDP 0x02 /**< UDP */
  40. #define Sn_MR_IPRAW 0x03 /**< IP LAYER RAW SOCK */
  41. #define Sn_MR_MACRAW 0x04 /**< MAC LAYER RAW SOCK */
  42. #define Sn_MR_PPPOE 0x05 /**< PPPoE */
  43. #define Sn_MR_UCASTB 0x10 /**< Unicast Block in UDP Multicating*/
  44. #define Sn_MR_ND 0x20 /**< No Delayed Ack(TCP) flag */
  45. #define Sn_MR_MC 0x20 /**< Multicast IGMP (UDP) flag */
  46. #define Sn_MR_BCASTB 0x40 /**< Broadcast blcok in UDP Multicating */
  47. #define Sn_MR_MULTI 0x80 /**< support UDP Multicating */
  48. #define Sn_MR_MIP6N 0x10 /**< IPv6 packet Block */
  49. #define Sn_MR_MMB 0x20 /**< IPv4 Multicasting Block */
  50. //#define Sn_MR_BCASTB 0x40 /**< Broadcast blcok */
  51. #define Sn_MR_MFEN 0x80 /**< support MAC filter enable */
  52. #define Sn_CR_OPEN 0x01 /**< initialize or open socket */
  53. #define Sn_CR_LISTEN 0x02 /**< wait connection request in tcp mode(Server mode) */
  54. #define Sn_CR_CONNECT 0x04 /**< send connection request in tcp mode(Client mode) */
  55. #define Sn_CR_DISCON 0x08 /**< send closing reqeuset in tcp mode */
  56. #define Sn_CR_CLOSE 0x10 /**< close socket */
  57. #define Sn_CR_SEND 0x20 /**< update txbuf pointer, send data */
  58. #define Sn_CR_SEND_MAC 0x21 /**< send data with MAC address, so without ARP process */
  59. #define Sn_CR_SEND_KEEP 0x22 /**< send keep alive message */
  60. #define Sn_CR_RECV 0x40 /**< update rxbuf pointer, recv data */
  61. #define Sn_IR_SEND_OK 0x10 /**< complete sending */
  62. #define Sn_IR_TIMEOUT 0x08 /**< assert timeout */
  63. #define Sn_IR_RECV 0x04 /**< receiving data */
  64. #define Sn_IR_DISCON 0x02 /**< closed socket */
  65. #define Sn_IR_CON 0x01 /**< established connection */
  66. #define SOCK_CLOSED 0x00 /**< closed */
  67. #define SOCK_INIT 0x13 /**< init state */
  68. #define SOCK_LISTEN 0x14 /**< listen state */
  69. #define SOCK_SYNSENT 0x15 /**< connection state */
  70. #define SOCK_SYNRECV 0x16 /**< connection state */
  71. #define SOCK_ESTABLISHED 0x17 /**< success to connect */
  72. #define SOCK_FIN_WAIT 0x18 /**< closing state */
  73. #define SOCK_CLOSING 0x1A /**< closing state */
  74. #define SOCK_TIME_WAIT 0x1B /**< closing state */
  75. #define SOCK_CLOSE_WAIT 0x1C /**< closing state */
  76. #define SOCK_LAST_ACK 0x1D /**< closing state */
  77. #define SOCK_UDP 0x22 /**< udp socket */
  78. #define SOCK_IPRAW 0x32 /**< ip raw mode socket */
  79. #define SOCK_MACRAW 0x42 /**< mac raw mode socket */
  80. #define SOCK_PPPOE 0x5F /**< pppoe socket */
  81. #define SOCK_BUF_LEN (20 * 1024)
  82. enum
  83. {
  84. W5500_COMMON_MR,
  85. W5500_COMMON_GAR0,
  86. W5500_COMMON_SUBR0 = 5,
  87. W5500_COMMON_MAC0 = 9,
  88. W5500_COMMON_IP0 = 0x0f,
  89. W5500_COMMON_INTLEVEL0 = 0x13,
  90. W5500_COMMON_IR = 0x15,
  91. W5500_COMMON_IMR,
  92. W5500_COMMON_SOCKET_IR,
  93. W5500_COMMON_SOCKET_IMR,
  94. W5500_COMMON_SOCKET_RTR0,
  95. W5500_COMMON_SOCKET_RCR = 0x1b,
  96. W5500_COMMON_PPP,
  97. W5500_COMMON_UIPR0 = 0x28,
  98. W5500_COMMON_UPORT0 = 0x2c,
  99. W5500_COMMON_PHY = 0x2e,
  100. W5500_COMMON_VERSIONR = 0x39,
  101. W5500_COMMON_QTY,
  102. W5500_SOCKET_MR = 0,
  103. W5500_SOCKET_CR,
  104. W5500_SOCKET_IR,
  105. W5500_SOCKET_SR,
  106. W5500_SOCKET_SOURCE_PORT0,
  107. W5500_SOCKET_DEST_MAC0 = 0x06,
  108. W5500_SOCKET_DEST_IP0 = 0x0C,
  109. W5500_SOCKET_DEST_PORT0 = 0x10,
  110. W5500_SOCKET_SEGMENT0 = 0x12,
  111. W5500_SOCKET_TOS = 0x15,
  112. W5500_SOCKET_TTL,
  113. W5500_SOCKET_RX_MEM_SIZE = 0x1e,
  114. W5500_SOCKET_TX_MEM_SIZE,
  115. W5500_SOCKET_TX_FREE_SIZE0 = 0x20,
  116. W5500_SOCKET_TX_READ_POINT0 = 0x22,
  117. W5500_SOCKET_TX_WRITE_POINT0 = 0x24,
  118. W5500_SOCKET_RX_SIZE0 = 0x26,
  119. W5500_SOCKET_RX_READ_POINT0 = 0x28,
  120. W5500_SOCKET_RX_WRITE_POINT0 = 0x2A,
  121. W5500_SOCKET_IMR = 0x2C,
  122. W5500_SOCKET_IP_HEAD_FRAG_VALUE0,
  123. W5500_SOCKET_KEEP_TIME = 0x2f,
  124. W5500_SOCKET_QTY,
  125. EV_W5500_IRQ = USER_EVENT_ID_START + 1,
  126. EV_W5500_LINK,
  127. //以下event的顺序不能变,新增event请在上方添加
  128. EV_W5500_SOCKET_TX,
  129. EV_W5500_SOCKET_CONNECT,
  130. EV_W5500_SOCKET_CLOSE,
  131. EV_W5500_SOCKET_LISTEN,
  132. EV_W5500_SOCKET_DNS,
  133. EV_W5500_RE_INIT,
  134. EV_W5500_REG_OP,
  135. W5500_SOCKET_OFFLINE = 0,
  136. W5500_SOCKET_CONFIG,
  137. W5500_SOCKET_CONNECT,
  138. W5500_SOCKET_ONLINE,
  139. W5500_SOCKET_CLOSING,
  140. };
  141. typedef struct
  142. {
  143. llist_head node;
  144. uint64_t tag; //考虑到socket复用的问题,必须有tag来做比对
  145. luat_ip_addr_t ip;
  146. uint8_t *data;
  147. uint32_t read_pos;
  148. uint32_t len;
  149. uint16_t port;
  150. uint8_t is_sending;
  151. }socket_data_t;
  152. typedef struct
  153. {
  154. socket_ctrl_t socket[MAX_SOCK_NUM];
  155. dhcp_client_info_t dhcp_client;
  156. uint64_t last_tx_time;
  157. uint64_t tag;
  158. CBFuncEx_t socket_cb;
  159. void *user_data;
  160. void *task_handle;
  161. uint32_t static_ip; //大端格式存放
  162. uint32_t static_submask; //大端格式存放
  163. uint32_t static_gateway; //大端格式存放
  164. uint32_t static_dns_server[MAX_DNS_SERVER];
  165. uint16_t RTR;
  166. uint8_t RCR;
  167. uint8_t force_arp;
  168. uint8_t auto_speed;
  169. uint8_t spi_id;
  170. uint8_t cs_pin;
  171. uint8_t rst_pin;
  172. uint8_t irq_pin;
  173. uint8_t link_pin;
  174. uint8_t speed_status;
  175. uint8_t run_dns;
  176. uint8_t link_ready;
  177. uint8_t ip_ready;
  178. uint8_t network_ready;
  179. uint8_t inter_error;
  180. uint8_t device_on;
  181. uint8_t last_udp_send_ok;
  182. uint8_t rx_buf[2048 + 8];
  183. uint8_t mac[6];
  184. uint8_t next_index;
  185. }w5500_ctrl_t;
  186. static w5500_ctrl_t *prv_w5500_ctrl;
  187. static void w5500_ip_state(w5500_ctrl_t *w5500, uint8_t check_state);
  188. static void w5500_check_dhcp(w5500_ctrl_t *w5500);
  189. static void w5500_init_reg(w5500_ctrl_t *w5500);
  190. static int w5500_del_data_cache(void *p, void *u)
  191. {
  192. socket_data_t *pdata = (socket_data_t *)p;
  193. free(pdata->data);
  194. return LIST_DEL;
  195. }
  196. static int w5500_next_data_cache(void *p, void *u)
  197. {
  198. socket_ctrl_t *socket = (socket_ctrl_t *)u;
  199. socket_data_t *pdata = (socket_data_t *)p;
  200. if (socket->tag != pdata->tag)
  201. {
  202. DBG("tag error");
  203. free(pdata->data);
  204. return LIST_DEL;
  205. }
  206. return LIST_FIND;
  207. }
  208. static int32_t w5500_irq(int pin, void *args)
  209. {
  210. w5500_ctrl_t *w5500 = (w5500_ctrl_t *)args;
  211. if ((pin & 0x00ff) == w5500->irq_pin)
  212. {
  213. platform_send_event(w5500->task_handle, EV_W5500_IRQ, 0, 0, 0);
  214. }
  215. if ((pin & 0x00ff) == w5500->link_pin)
  216. {
  217. platform_send_event(w5500->task_handle, EV_W5500_LINK, 0, 0, 0);
  218. }
  219. }
  220. static void w5500_callback_to_nw_task(w5500_ctrl_t *w5500, uint32_t event_id, uint32_t param1, uint32_t param2, uint32_t param3)
  221. {
  222. luat_network_cb_param_t param = {.tag = 0, .param = w5500->user_data};
  223. OS_EVENT event = { .ID = event_id, .Param1 = param1, .Param2 = param2, .Param3 = param3};
  224. if (event_id > EV_NW_TIMEOUT)
  225. {
  226. event.Param3 = prv_w5500_ctrl->socket[param1].param;
  227. param.tag = prv_w5500_ctrl->socket[param1].tag;
  228. }
  229. w5500->socket_cb(&event, &param);
  230. }
  231. static void w5500_xfer(w5500_ctrl_t *w5500, uint16_t address, uint8_t ctrl, uint8_t *data, uint32_t len)
  232. {
  233. BytesPutBe16(w5500->rx_buf, address);
  234. w5500->rx_buf[2] = ctrl;
  235. if (data && len)
  236. {
  237. memcpy(w5500->rx_buf + 3, data, len);
  238. }
  239. luat_gpio_set(w5500->cs_pin, 0);
  240. luat_spi_transfer(w5500->spi_id, w5500->rx_buf, len + 3, w5500->rx_buf, len + 3);
  241. luat_gpio_set(w5500->cs_pin, 1);
  242. if (data && len)
  243. {
  244. memcpy(data, w5500->rx_buf + 3, len);
  245. }
  246. }
  247. static uint8_t w5500_socket_state(w5500_ctrl_t *w5500, uint8_t socket_id)
  248. {
  249. uint8_t retry = 0;
  250. uint8_t temp[4];
  251. do
  252. {
  253. w5500_xfer(w5500, W5500_SOCKET_MR, socket_index(socket_id)|socket_reg, temp, 4);
  254. if (temp[W5500_SOCKET_MR] == 0xff) //模块读不到了
  255. {
  256. w5500->device_on = 0;
  257. return 0;
  258. }
  259. retry++;
  260. }while(temp[W5500_SOCKET_CR] && (retry < 10));
  261. if (retry >= 10)
  262. {
  263. w5500->inter_error++;
  264. DBG_ERR("check too much times, error %d", w5500->inter_error);
  265. }
  266. return temp[W5500_SOCKET_SR];
  267. }
  268. static void w5500_socket_disconnect(w5500_ctrl_t *w5500, uint8_t socket_id)
  269. {
  270. uint8_t temp;
  271. temp = w5500_socket_state(w5500, socket_id);
  272. if (!w5500->device_on) return;
  273. if (SOCK_CLOSED == temp)
  274. {
  275. w5500->socket[socket_id].state = W5500_SOCKET_OFFLINE;
  276. // DBG("socket %d already closed");
  277. return;
  278. }
  279. // if ((temp >= SOCK_FIN_WAIT) && (temp <= SOCK_LAST_ACK))
  280. // {
  281. // w5500->socket[socket_id].state = W5500_SOCKET_CLOSING;
  282. // DBG("socket %d is closing");
  283. // return;
  284. //
  285. // }
  286. temp = Sn_CR_DISCON;
  287. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, &temp, 1);
  288. w5500->socket[socket_id].state = W5500_SOCKET_CLOSING;
  289. }
  290. static void w5500_socket_close(w5500_ctrl_t *w5500, uint8_t socket_id)
  291. {
  292. uint8_t temp;
  293. temp = w5500_socket_state(w5500, socket_id);
  294. if (!w5500->device_on) return;
  295. //
  296. // if (SOCK_CLOSED == temp)
  297. // {
  298. // w5500->socket[socket_id].state = W5500_SOCKET_OFFLINE;
  299. //// DBG("socket %d already closed");
  300. // return;
  301. // }
  302. temp = Sn_CR_CLOSE;
  303. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, &temp, 1);
  304. w5500->socket[socket_id].state = W5500_SOCKET_OFFLINE;
  305. }
  306. static int w5500_socket_config(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t is_tcp, uint16_t local_port)
  307. {
  308. uint8_t delay_cnt;
  309. uint8_t temp;
  310. temp = w5500_socket_state(w5500, socket_id);
  311. if (!w5500->device_on) return -1;
  312. if (SOCK_CLOSED != temp)
  313. {
  314. DBG_ERR("socket %d not closed state %x", socket_id, temp);
  315. return -1;
  316. }
  317. uint8_t cmd[32];
  318. uint16_t wtemp;
  319. for(temp = 0; temp < 3; temp++)
  320. {
  321. cmd[W5500_SOCKET_MR] = is_tcp?Sn_MR_TCP:Sn_MR_UDP;
  322. cmd[W5500_SOCKET_CR] = 0;
  323. cmd[W5500_SOCKET_IR] = 0xff;
  324. BytesPutBe16(&cmd[W5500_SOCKET_SOURCE_PORT0], local_port);
  325. BytesPutLe32(&cmd[W5500_SOCKET_DEST_IP0], 0xffffffff);
  326. BytesPutBe16(&cmd[W5500_SOCKET_DEST_PORT0], 0);
  327. BytesPutBe16(&cmd[W5500_SOCKET_SEGMENT0], is_tcp?1460:1472);
  328. w5500_xfer(w5500, W5500_SOCKET_MR, socket_index(socket_id)|socket_reg|is_write, cmd, W5500_SOCKET_TOS - 1);
  329. w5500_xfer(w5500, W5500_SOCKET_MR, socket_index(socket_id)|socket_reg, cmd, W5500_SOCKET_TOS - 1);
  330. wtemp = BytesGetBe16(&cmd[W5500_SOCKET_SOURCE_PORT0]);
  331. if (wtemp != local_port)
  332. {
  333. DBG_ERR("error port %u %u", wtemp, local_port);
  334. }
  335. else
  336. {
  337. goto W5500_SOCKET_CONFIG_START;
  338. }
  339. }
  340. return -1;
  341. W5500_SOCKET_CONFIG_START:
  342. cmd[0] = Sn_CR_OPEN;
  343. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, cmd, 1);
  344. do
  345. {
  346. temp = w5500_socket_state(w5500, socket_id);
  347. if (!w5500->device_on) return -1;
  348. delay_cnt++;
  349. }while((temp != SOCK_INIT) && (temp != SOCK_UDP) && (delay_cnt < 100));
  350. if (delay_cnt >= 100)
  351. {
  352. w5500->inter_error++;
  353. DBG_ERR("socket %d config timeout, error %d", socket_id, w5500->inter_error);
  354. return -1;
  355. }
  356. w5500->socket[socket_id].state = is_tcp?W5500_SOCKET_CONFIG:W5500_SOCKET_ONLINE;
  357. return 0;
  358. }
  359. static int w5500_socket_connect(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t is_listen, uint32_t remote_ip, uint16_t remote_port)
  360. {
  361. uint32_t ip;
  362. uint16_t port;
  363. uint8_t delay_cnt;
  364. uint8_t temp;
  365. uint8_t cmd[16];
  366. temp = w5500_socket_state(w5500, socket_id);
  367. if (!w5500->device_on) return -1;
  368. if ((temp != SOCK_INIT) && (temp != SOCK_UDP))
  369. {
  370. DBG("socket %d not config state %x", socket_id, temp);
  371. return -1;
  372. }
  373. // DBG("%08x, %u", remote_ip, remote_port);
  374. if (!is_listen)
  375. {
  376. // w5500_xfer(w5500, W5500_SOCKET_DEST_IP0, socket_index(socket_id)|socket_reg, cmd, 6);
  377. // ip = BytesGetLe32(cmd);
  378. // port = BytesGetBe16(cmd + 4);
  379. // if (ip != remote_ip || port != remote_port)
  380. {
  381. BytesPutLe32(cmd, remote_ip);
  382. BytesPutBe16(&cmd[W5500_SOCKET_DEST_PORT0 - W5500_SOCKET_DEST_IP0], remote_port);
  383. w5500_xfer(w5500, W5500_SOCKET_DEST_IP0, socket_index(socket_id)|socket_reg|is_write, cmd, 6);
  384. }
  385. }
  386. W5500_SOCKET_CONNECT_START:
  387. if (temp != SOCK_UDP)
  388. {
  389. uint8_t temp = is_listen?Sn_CR_LISTEN:Sn_CR_CONNECT;
  390. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, &temp, 1);
  391. w5500->socket[socket_id].state = W5500_SOCKET_CONNECT;
  392. }
  393. return 0;
  394. }
  395. static int w5500_socket_auto_heart(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t time)
  396. {
  397. uint8_t point[6];
  398. point[0] = time;
  399. w5500_xfer(w5500, W5500_SOCKET_KEEP_TIME, socket_index(socket_id)|socket_reg|is_write, point, 1);
  400. return 0;
  401. }
  402. static int w5500_socket_tx(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t *data, uint16_t len)
  403. {
  404. uint8_t delay_cnt;
  405. uint8_t temp;
  406. uint8_t point[6];
  407. uint16_t tx_free, tx_point;
  408. temp = w5500_socket_state(w5500, socket_id);
  409. if (!w5500->device_on) return -1;
  410. if ((temp != SOCK_ESTABLISHED) && (temp != SOCK_UDP))
  411. {
  412. DBG("socket %d not online state %x", socket_id, temp);
  413. return -1;
  414. }
  415. w5500->socket[socket_id].state = W5500_SOCKET_ONLINE;
  416. if (!data || !len)
  417. {
  418. point[0] = Sn_CR_SEND_KEEP;
  419. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, point, 1);
  420. }
  421. w5500_xfer(w5500, W5500_SOCKET_TX_FREE_SIZE0, socket_index(socket_id)|socket_reg, point, 6);
  422. tx_free = BytesGetBe16(point);
  423. tx_point = BytesGetBe16(point + 4);
  424. if (tx_free != 2048)
  425. {
  426. DBG("%d,0x%04x,%u", socket_id, tx_point, len);
  427. }
  428. if (len > tx_free)
  429. {
  430. len = tx_free;
  431. }
  432. w5500->last_tx_time = GetSysTickMS();
  433. w5500_xfer(w5500, tx_point, socket_index(socket_id)|socket_tx|is_write, data, len);
  434. tx_point += len;
  435. BytesPutBe16(point, tx_point);
  436. w5500_xfer(w5500, W5500_SOCKET_TX_WRITE_POINT0, socket_index(socket_id)|socket_reg|is_write, point, 2);
  437. point[0] = Sn_CR_SEND;
  438. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, point, 1);
  439. return len;
  440. }
  441. static int w5500_socket_rx(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t *data, uint16_t len)
  442. {
  443. uint8_t delay_cnt;
  444. uint8_t temp;
  445. uint8_t point[4];
  446. uint16_t rx_size, rx_point;
  447. temp = w5500_socket_state(w5500, socket_id);
  448. if (!w5500->device_on) return -1;
  449. if ((temp != SOCK_ESTABLISHED) && (temp != SOCK_UDP))
  450. {
  451. DBG("socket %d not config state %x", socket_id, temp);
  452. return -1;
  453. }
  454. w5500->socket[socket_id].state = W5500_SOCKET_ONLINE;
  455. w5500_xfer(w5500, W5500_SOCKET_RX_SIZE0, socket_index(socket_id)|socket_reg, point, 4);
  456. rx_size = BytesGetBe16(point);
  457. rx_point = BytesGetBe16(point + 2);
  458. // DBG("%d,0x%04x,%u", socket_id, rx_point, rx_size);
  459. if (!rx_size) return 0;
  460. if (rx_size < len)
  461. {
  462. len = rx_size;
  463. }
  464. w5500_xfer(w5500, rx_point, socket_index(socket_id)|socket_rx, data, len);
  465. rx_point += len;
  466. BytesPutBe16(point, rx_point);
  467. w5500_xfer(w5500, W5500_SOCKET_RX_READ_POINT0, socket_index(socket_id)|socket_reg|is_write, point, 2);
  468. point[0] = Sn_CR_RECV;
  469. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, point, 1);
  470. return len;
  471. }
  472. static void w5500_nw_state(w5500_ctrl_t *w5500)
  473. {
  474. if (w5500->link_ready && w5500->ip_ready)
  475. {
  476. if (!w5500->network_ready)
  477. {
  478. w5500->network_ready = 1;
  479. w5500_callback_to_nw_task(w5500, EV_NW_STATE, 0, 1, 0);
  480. DBG("network ready");
  481. }
  482. }
  483. else
  484. {
  485. if (w5500->network_ready)
  486. {
  487. w5500->network_ready = 0;
  488. w5500_callback_to_nw_task(w5500, EV_NW_STATE, 0, 0, 0);
  489. DBG("network not ready");
  490. }
  491. }
  492. }
  493. static void w5500_check_dhcp(w5500_ctrl_t *w5500)
  494. {
  495. if (w5500->static_ip) return;
  496. if (DHCP_STATE_CHECK == w5500->dhcp_client.state)
  497. {
  498. w5500->dhcp_client.state = DHCP_STATE_WAIT_LEASE_P1;
  499. uint8_t temp[24];
  500. PV_Union uIP;
  501. memset(temp, 0, sizeof(temp));
  502. temp[0] = w5500->force_arp?MR_UDP_FARP:0;
  503. BytesPutLe32(&temp[W5500_COMMON_GAR0], w5500->dhcp_client.gateway);
  504. BytesPutLe32(&temp[W5500_COMMON_SUBR0], w5500->dhcp_client.submask);
  505. BytesPutLe32(&temp[W5500_COMMON_IP0], w5500->dhcp_client.ip);
  506. memcpy(&temp[W5500_COMMON_MAC0], w5500->dhcp_client.mac, 6);
  507. w5500_xfer(w5500, W5500_COMMON_MR, is_write, temp, W5500_COMMON_INTLEVEL0);
  508. w5500->dhcp_client.discover_cnt = 0;
  509. w5500_ip_state(w5500, 1);
  510. uIP.u32 = w5500->dhcp_client.ip;
  511. DBG("动态IP:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  512. uIP.u32 = w5500->dhcp_client.submask;
  513. DBG("子网掩码:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  514. uIP.u32 = w5500->dhcp_client.gateway;
  515. DBG("网关:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  516. DBG("租约时间:%u秒", w5500->dhcp_client.lease_time);
  517. if (w5500->static_dns_server[0])
  518. {
  519. int i;
  520. for(i = 0; i < MAX_DNS_SERVER;i++)
  521. {
  522. uIP.u32 = w5500->static_dns_server[i];
  523. DBG("静态DNS%d:%d.%d.%d.%d",i, uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  524. }
  525. }
  526. else
  527. {
  528. uIP.u32 = w5500->dhcp_client.dns_server[0];
  529. DBG("动态DNS1:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  530. uIP.u32 = w5500->dhcp_client.dns_server[1];
  531. DBG("动态DNS2:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  532. }
  533. }
  534. if ((!w5500->last_udp_send_ok && w5500->dhcp_client.discover_cnt >= 1) || (w5500->last_udp_send_ok && w5500->dhcp_client.discover_cnt >= 3))
  535. {
  536. DBG("dhcp long time not get ip, reboot w5500");
  537. memset(&w5500->dhcp_client, 0, sizeof(w5500->dhcp_client));
  538. platform_send_event(w5500->task_handle, EV_W5500_RE_INIT, 0, 0, 0);
  539. }
  540. }
  541. static void w5500_link_state(w5500_ctrl_t *w5500, uint8_t check_state)
  542. {
  543. Buffer_Struct tx_msg_buf = {0,0,0};
  544. uint32_t remote_ip;
  545. int result;
  546. if (w5500->link_ready != check_state)
  547. {
  548. DBG("link %d -> %d", w5500->link_ready, check_state);
  549. w5500->link_ready = check_state;
  550. if (w5500->link_ready)
  551. {
  552. if (!w5500->static_ip)
  553. {
  554. w5500_ip_state(w5500, 0);
  555. w5500->dhcp_client.state = w5500->dhcp_client.ip?DHCP_STATE_REQUIRE:DHCP_STATE_DISCOVER;
  556. uint8_t temp[1];
  557. temp[0] = MR_UDP_FARP;
  558. w5500_xfer(w5500, W5500_COMMON_MR, is_write, temp, 1);
  559. result = ip4_dhcp_run(&w5500->dhcp_client, NULL, &tx_msg_buf, &remote_ip);
  560. w5500_check_dhcp(w5500);
  561. if (tx_msg_buf.Pos)
  562. {
  563. w5500_socket_connect(w5500, SYS_SOCK_ID, 0, remote_ip, DHCP_SERVER_PORT);
  564. w5500_socket_tx(w5500, SYS_SOCK_ID, tx_msg_buf.Data, tx_msg_buf.Pos);
  565. w5500->last_udp_send_ok = 0;
  566. }
  567. OS_DeInitBuffer(&tx_msg_buf);
  568. }
  569. }
  570. else
  571. {
  572. if (GetSysTickMS() < (w5500->last_tx_time + 1500))
  573. {
  574. w5500->inter_error++;
  575. DBG_ERR("link down too fast, error %u", w5500->inter_error);
  576. }
  577. }
  578. w5500_nw_state(w5500);
  579. }
  580. }
  581. static void w5500_ip_state(w5500_ctrl_t *w5500, uint8_t check_state)
  582. {
  583. if (w5500->ip_ready != check_state)
  584. {
  585. DBG("ip %d -> %d", w5500->ip_ready, check_state);
  586. w5500->ip_ready = check_state;
  587. w5500_nw_state(w5500);
  588. }
  589. }
  590. static void w5500_init_reg(w5500_ctrl_t *w5500)
  591. {
  592. uint8_t temp[64];
  593. luat_gpio_set(w5500->rst_pin, 0);
  594. msleep(5);
  595. luat_gpio_set(w5500->rst_pin, 1);
  596. msleep(10);
  597. w5500->ip_ready = 0;
  598. w5500->network_ready = 0;
  599. if (w5500->static_ip)
  600. {
  601. w5500->dhcp_client.state = DHCP_STATE_NOT_WORK;
  602. }
  603. else
  604. {
  605. if (w5500->auto_speed)
  606. {
  607. w5500->dhcp_client.state = DHCP_STATE_DISCOVER;
  608. }
  609. else
  610. {
  611. if ((w5500->dhcp_client.state == DHCP_STATE_NOT_WORK) || !w5500->dhcp_client.ip)
  612. {
  613. w5500->dhcp_client.state = DHCP_STATE_DISCOVER;
  614. }
  615. else
  616. {
  617. w5500->dhcp_client.state = DHCP_STATE_REQUIRE;
  618. }
  619. }
  620. }
  621. w5500_callback_to_nw_task(w5500, EV_NW_RESET, 0, 0, 0);
  622. luat_gpio_close(w5500->link_pin);
  623. luat_gpio_close(w5500->irq_pin);
  624. w5500_xfer(w5500, W5500_COMMON_MR, 0, temp, W5500_COMMON_QTY);
  625. w5500->device_on = (0x04 == temp[W5500_COMMON_VERSIONR])?1:0;
  626. w5500_link_state(w5500, w5500->device_on?(temp[W5500_COMMON_PHY] & 0x01):0);
  627. temp[W5500_COMMON_MR] = w5500->force_arp?MR_UDP_FARP:0;
  628. if (w5500->static_ip)
  629. {
  630. BytesPutLe32(&temp[W5500_COMMON_GAR0], w5500->static_gateway); //已经是大端格式了不需要转换
  631. BytesPutLe32(&temp[W5500_COMMON_SUBR0], w5500->static_submask); //已经是大端格式了不需要转换
  632. BytesPutLe32(&temp[W5500_COMMON_IP0], w5500->static_ip); //已经是大端格式了不需要转换
  633. }
  634. else if (w5500->dhcp_client.ip)
  635. {
  636. BytesPutLe32(&temp[W5500_COMMON_GAR0], w5500->dhcp_client.gateway);
  637. BytesPutLe32(&temp[W5500_COMMON_SUBR0], w5500->dhcp_client.submask);
  638. BytesPutLe32(&temp[W5500_COMMON_IP0], w5500->dhcp_client.ip);
  639. }
  640. else
  641. {
  642. BytesPutLe32(&temp[W5500_COMMON_GAR0], 0);
  643. BytesPutLe32(&temp[W5500_COMMON_SUBR0], 0);
  644. BytesPutLe32(&temp[W5500_COMMON_IP0], 0);
  645. }
  646. memcpy(&temp[W5500_COMMON_MAC0], w5500->mac, 6);
  647. memcpy(w5500->dhcp_client.mac, &temp[W5500_COMMON_MAC0], 6);
  648. sprintf_(w5500->dhcp_client.name, "airm2m-%02x%02x%02x%02x%02x%02x",
  649. w5500->dhcp_client.mac[0],w5500->dhcp_client.mac[1], w5500->dhcp_client.mac[2],
  650. w5500->dhcp_client.mac[3],w5500->dhcp_client.mac[4], w5500->dhcp_client.mac[5]);
  651. // BytesPutBe16(&temp[W5500_COMMON_SOCKET_RTR0], w5500->RTR);
  652. temp[W5500_COMMON_IMR] = IR_CONFLICT|IR_UNREACH|IR_MAGIC;
  653. temp[W5500_COMMON_SOCKET_IMR] = 0xff;
  654. // temp[W5500_COMMON_SOCKET_RCR] = w5500->RCR;
  655. // DBG_HexPrintf(temp + W5500_COMMON_SOCKET_RTR0, 3);
  656. w5500_xfer(w5500, W5500_COMMON_MR, is_write, temp, W5500_COMMON_QTY);
  657. // memset(temp, 0, sizeof(temp));
  658. // w5500_xfer(w5500, W5500_COMMON_MR, 0, NULL, W5500_COMMON_QTY);
  659. // DBG_HexPrintf(temp, W5500_COMMON_QTY);
  660. w5500_ip_state(w5500, w5500->static_ip?1:0);
  661. luat_gpio_t gpio = {0};
  662. gpio.pin = w5500->irq_pin;
  663. gpio.mode = Luat_GPIO_IRQ;
  664. gpio.pull = Luat_GPIO_PULLUP;
  665. gpio.irq = Luat_GPIO_FALLING;
  666. gpio.irq_cb = w5500_irq;
  667. gpio.irq_args = w5500;
  668. luat_gpio_setup(&gpio);
  669. gpio.pin = w5500->link_pin;
  670. gpio.pull = Luat_GPIO_DEFAULT;
  671. gpio.irq = Luat_GPIO_BOTH;
  672. gpio.irq_cb = w5500_irq;
  673. gpio.irq_args = w5500;
  674. luat_gpio_setup(&gpio);
  675. w5500_socket_auto_heart(w5500, SYS_SOCK_ID, 2);
  676. w5500_socket_config(w5500, SYS_SOCK_ID, 0, DHCP_CLIENT_PORT);
  677. if (w5500->auto_speed)
  678. {
  679. temp[0] = 0x78;
  680. w5500_xfer(w5500, W5500_COMMON_PHY, is_write, temp, 1);
  681. temp[0] = 0x78+ 0x80;
  682. w5500_xfer(w5500, W5500_COMMON_PHY, is_write, temp, 1);
  683. }
  684. w5500->inter_error = 0;
  685. w5500->next_index = 1;
  686. }
  687. static int32_t w5500_dummy_callback(void *pData, void *pParam)
  688. {
  689. return 0;
  690. }
  691. static socket_data_t * w5500_create_data_node(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t *data, uint32_t len, luat_ip_addr_t *remote_ip, uint16_t remote_port)
  692. {
  693. socket_data_t *p = (socket_data_t *)malloc(sizeof(socket_data_t));
  694. if (p)
  695. {
  696. memset(p, 0, sizeof(socket_data_t));
  697. p->len = len;
  698. p->port = remote_port;
  699. if (remote_ip)
  700. {
  701. p->ip = *remote_ip;
  702. }
  703. p->tag = w5500->socket[socket_id].tag;
  704. if (data && len)
  705. {
  706. p->data = malloc(len);
  707. if (p->data)
  708. {
  709. memcpy(p->data, data, len);
  710. }
  711. else
  712. {
  713. free(p);
  714. return NULL;
  715. }
  716. }
  717. }
  718. return p;
  719. }
  720. static void w5500_socket_tx_next_data(w5500_ctrl_t *w5500, uint8_t socket_id)
  721. {
  722. socket_data_t *p = llist_traversal(&w5500->socket[socket_id].tx_head, w5500_next_data_cache, &prv_w5500_ctrl->socket[socket_id]);
  723. if (p)
  724. {
  725. if (!w5500->socket[socket_id].is_tcp)
  726. {
  727. w5500_socket_connect(w5500, socket_id, 0, p->ip.ipv4, p->port);
  728. }
  729. if (p->data && p->len)
  730. {
  731. p->read_pos += w5500_socket_tx(w5500, socket_id, p->data + p->read_pos, p->len - p->read_pos);
  732. p->is_sending = 1;
  733. }
  734. else
  735. {
  736. w5500_socket_tx(w5500, socket_id, NULL, 0);
  737. p->len = 0;
  738. p->read_pos = 0;
  739. p->is_sending = 1;
  740. }
  741. }
  742. }
  743. static void w5500_sys_socket_callback(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t event)
  744. {
  745. Buffer_Struct rx_buf;
  746. Buffer_Struct msg_buf;
  747. Buffer_Struct tx_msg_buf = {0,0,0};
  748. int result, i;
  749. uint32_t ip;
  750. uint16_t port;
  751. uint16_t len;
  752. socket_data_t *p;
  753. luat_ip_addr_t ip_addr;
  754. switch(event)
  755. {
  756. case Sn_IR_SEND_OK:
  757. if (!socket_id)
  758. {
  759. w5500->last_udp_send_ok = 1;
  760. }
  761. if (w5500->network_ready)
  762. {
  763. if (socket_id) W5500_LOCK;
  764. p = llist_traversal(&w5500->socket[socket_id].tx_head, w5500_next_data_cache, &prv_w5500_ctrl->socket[socket_id]);
  765. if (p && !p->is_sending)
  766. {
  767. DBG_ERR("socket %d should sending!", socket_id);
  768. p->read_pos = 0;
  769. }
  770. if (p && (p->read_pos >= p->len))
  771. {
  772. w5500->socket[socket_id].tx_wait_size -= p->len;
  773. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_TX_OK, socket_id, p->len, 0);
  774. llist_del(&p->node);
  775. free(p->data);
  776. free(p);
  777. }
  778. w5500_socket_tx_next_data(w5500, socket_id);
  779. if (llist_empty(&w5500->socket[socket_id].tx_head))
  780. {
  781. w5500->socket[socket_id].tx_wait_size = 0;
  782. }
  783. if (socket_id) W5500_UNLOCK;
  784. }
  785. break;
  786. case Sn_IR_RECV:
  787. if (socket_id && (w5500->socket[socket_id].rx_wait_size >= 32768))
  788. {
  789. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_RX_FULL, socket_id, 0, 0);
  790. break;
  791. }
  792. OS_InitBuffer(&rx_buf, 2048);
  793. result = w5500_socket_rx(w5500, socket_id, rx_buf.Data, rx_buf.MaxLen);
  794. if (result > 0)
  795. {
  796. if (socket_id)
  797. {
  798. if (w5500->socket[socket_id].is_tcp)
  799. {
  800. socket_data_t *p = w5500_create_data_node(w5500, socket_id, rx_buf.Data, result, NULL, 0);
  801. W5500_LOCK;
  802. llist_add_tail(&p->node, &prv_w5500_ctrl->socket[socket_id].rx_head);
  803. prv_w5500_ctrl->socket[socket_id].rx_wait_size += result;
  804. W5500_UNLOCK;
  805. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_RX_NEW, socket_id, result, 0);
  806. }
  807. else
  808. {
  809. W5500_LOCK;
  810. rx_buf.Pos = 0;
  811. while (rx_buf.Pos < result)
  812. {
  813. ip_addr.ipv4 = BytesGetLe32(rx_buf.Data + rx_buf.Pos);
  814. ip_addr.is_ipv6 = 0;
  815. port = BytesGetBe16(rx_buf.Data + rx_buf.Pos + 4);
  816. len = BytesGetBe16(rx_buf.Data + rx_buf.Pos + 6);
  817. msg_buf.Data = rx_buf.Data + rx_buf.Pos + 8;
  818. msg_buf.MaxLen = len;
  819. socket_data_t *p = w5500_create_data_node(w5500, socket_id, rx_buf.Data + rx_buf.Pos + 8, len, &ip_addr, port);
  820. rx_buf.Pos += 8 + len;
  821. llist_add_tail(&p->node, &prv_w5500_ctrl->socket[socket_id].rx_head);
  822. prv_w5500_ctrl->socket[socket_id].rx_wait_size += len;
  823. }
  824. W5500_UNLOCK;
  825. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_RX_NEW, socket_id, result, 0);
  826. }
  827. }
  828. else
  829. {
  830. rx_buf.Pos = 0;
  831. while (rx_buf.Pos < result)
  832. {
  833. ip = BytesGetBe32(rx_buf.Data + rx_buf.Pos);
  834. port = BytesGetBe16(rx_buf.Data + rx_buf.Pos + 4);
  835. len = BytesGetBe16(rx_buf.Data + rx_buf.Pos + 6);
  836. msg_buf.Data = rx_buf.Data + rx_buf.Pos + 8;
  837. msg_buf.MaxLen = len;
  838. msg_buf.Pos = 0;
  839. switch(port)
  840. {
  841. case DHCP_SERVER_PORT:
  842. ip4_dhcp_run(&w5500->dhcp_client, &msg_buf, &tx_msg_buf, &ip);
  843. w5500_check_dhcp(w5500);
  844. if (tx_msg_buf.Pos)
  845. {
  846. w5500_socket_connect(w5500, SYS_SOCK_ID, 0, ip, DHCP_SERVER_PORT);
  847. w5500_socket_tx(w5500, SYS_SOCK_ID, tx_msg_buf.Data, tx_msg_buf.Pos);
  848. w5500->last_udp_send_ok = 0;
  849. }
  850. OS_DeInitBuffer(&tx_msg_buf);
  851. break;
  852. case DNS_SERVER_PORT:
  853. break;
  854. }
  855. rx_buf.Pos += 8 + len;
  856. }
  857. }
  858. }
  859. OS_DeInitBuffer(&rx_buf);
  860. break;
  861. case Sn_IR_TIMEOUT:
  862. // DBG_ERR("socket %d timeout", socket_id);
  863. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  864. break;
  865. case Sn_IR_CON:
  866. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  867. break;
  868. case Sn_IR_DISCON:
  869. // DBG_ERR("socket %d disconnect", socket_id);
  870. if (w5500_socket_state(w5500, socket_id) != SOCK_CLOSED)
  871. {
  872. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_REMOTE_CLOSE, socket_id, 0, 0);
  873. }
  874. else
  875. {
  876. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_CLOSE_OK, socket_id, 0, 0);
  877. }
  878. break;
  879. default:
  880. break;
  881. }
  882. }
  883. static void w5500_read_irq(w5500_ctrl_t *w5500)
  884. {
  885. OS_EVENT socket_event;
  886. uint8_t temp[64];
  887. uint8_t socket_irqs[MAX_SOCK_NUM];
  888. uint8_t socket_irq, common_irq;
  889. Buffer_Struct tx_msg_buf = {0,0,0};
  890. uint32_t remote_ip;
  891. int i, j;
  892. w5500_xfer(w5500, W5500_COMMON_IR, 0, temp, W5500_COMMON_QTY - W5500_COMMON_IR);
  893. common_irq = temp[0] & 0xf0;
  894. socket_irq = temp[W5500_COMMON_SOCKET_IR - W5500_COMMON_IR];
  895. w5500->device_on = (0x04 == temp[W5500_COMMON_VERSIONR - W5500_COMMON_IR])?1:0;
  896. w5500_link_state(w5500, w5500->device_on?(temp[W5500_COMMON_PHY - W5500_COMMON_IR] & 0x01):0);
  897. memset(socket_irqs, 0, MAX_SOCK_NUM);
  898. if (!w5500->device_on)
  899. {
  900. luat_gpio_close(w5500->link_pin);
  901. luat_gpio_close(w5500->irq_pin);
  902. return;
  903. }
  904. if (common_irq)
  905. {
  906. if (common_irq & IR_CONFLICT)
  907. {
  908. memset(temp, 0, 4);
  909. w5500_xfer(w5500, W5500_COMMON_IP0, is_write, temp, 4);
  910. w5500->dhcp_client.state = DHCP_STATE_DECLINE;
  911. ip4_dhcp_run(&w5500->dhcp_client, NULL, &tx_msg_buf, &remote_ip);
  912. if (tx_msg_buf.Pos)
  913. {
  914. w5500_socket_connect(w5500, SYS_SOCK_ID, 0, remote_ip, DHCP_SERVER_PORT);
  915. w5500_socket_tx(w5500, SYS_SOCK_ID, tx_msg_buf.Data, tx_msg_buf.Pos);
  916. w5500->last_udp_send_ok = 0;
  917. }
  918. OS_DeInitBuffer(&tx_msg_buf);
  919. w5500_ip_state(w5500, 0);
  920. }
  921. }
  922. if (socket_irq)
  923. {
  924. for(i = 0; i < MAX_SOCK_NUM; i++)
  925. {
  926. if (socket_irq & (1 << i))
  927. {
  928. w5500_xfer(w5500, W5500_SOCKET_IR, socket_index(i)|socket_reg, &socket_irqs[i], 1);
  929. temp[0] = socket_irqs[i];
  930. w5500_xfer(w5500, W5500_SOCKET_IR, socket_index(i)|socket_reg|is_write, temp, 1);
  931. }
  932. }
  933. }
  934. temp[0] = 0;
  935. temp[W5500_COMMON_IMR - W5500_COMMON_IR] = IR_CONFLICT|IR_UNREACH|IR_MAGIC;
  936. temp[W5500_COMMON_SOCKET_IR - W5500_COMMON_IR] = 0;
  937. w5500_xfer(w5500, W5500_COMMON_IR, is_write, temp, 1);
  938. if (socket_irq)
  939. {
  940. for(i = 0; i < MAX_SOCK_NUM; i++)
  941. {
  942. if (socket_irqs[i])
  943. {
  944. for (j = 0; j < 5; j++)
  945. {
  946. if (socket_irqs[i] & (1 << j))
  947. {
  948. w5500_sys_socket_callback(w5500, i, (1 << j));
  949. }
  950. }
  951. }
  952. }
  953. }
  954. }
  955. static void w5500_task(void *param)
  956. {
  957. w5500_ctrl_t *w5500 = (w5500_ctrl_t *)param;
  958. OS_EVENT event;
  959. int result;
  960. Buffer_Struct tx_msg_buf = {0,0,0};
  961. uint32_t remote_ip, sleep_time;
  962. PV_Union uPV;
  963. socket_data_t *p;
  964. w5500_init_reg(w5500);
  965. while(1)
  966. {
  967. if (w5500->inter_error >= 2)
  968. {
  969. DBG("w5500 error too much, reboot");
  970. w5500_init_reg(w5500);
  971. }
  972. sleep_time = 100;
  973. if (w5500->network_ready)
  974. {
  975. if (!w5500->run_dns && (w5500->link_pin != 0xff))
  976. {
  977. sleep_time = 0;
  978. }
  979. else
  980. {
  981. sleep_time = 1000;
  982. }
  983. }
  984. else if (w5500->link_ready)
  985. {
  986. sleep_time = 500;
  987. }
  988. else if (w5500->link_pin != 0xff)
  989. {
  990. sleep_time = 0;
  991. }
  992. result = platform_wait_event(w5500->task_handle, 0, &event, NULL, sleep_time);
  993. w5500_xfer(w5500, W5500_COMMON_MR, 0, NULL, W5500_COMMON_QTY);
  994. w5500->device_on = (0x04 == w5500->rx_buf[3 + W5500_COMMON_VERSIONR])?1:0;
  995. if (w5500->device_on && w5500->rx_buf[3 + W5500_COMMON_IMR] != (IR_CONFLICT|IR_UNREACH|IR_MAGIC))
  996. {
  997. w5500_init_reg(w5500);
  998. }
  999. if (!w5500->device_on)
  1000. {
  1001. w5500_link_state(w5500, 0);
  1002. luat_gpio_close(w5500->link_pin);
  1003. luat_gpio_close(w5500->irq_pin);
  1004. }
  1005. else
  1006. {
  1007. w5500_link_state(w5500, w5500->rx_buf[3 + W5500_COMMON_PHY] & 0x01);
  1008. w5500->speed_status = w5500->rx_buf[3 + W5500_COMMON_PHY] & (3 << 1);
  1009. }
  1010. if (result && w5500->link_ready)
  1011. {
  1012. result = ip4_dhcp_run(&w5500->dhcp_client, NULL, &tx_msg_buf, &remote_ip);
  1013. w5500_check_dhcp(w5500);
  1014. if (tx_msg_buf.Pos)
  1015. {
  1016. w5500_socket_connect(w5500, SYS_SOCK_ID, 0, remote_ip, DHCP_SERVER_PORT);
  1017. w5500_socket_tx(w5500, SYS_SOCK_ID, tx_msg_buf.Data, tx_msg_buf.Pos);
  1018. w5500->last_udp_send_ok = 0;
  1019. }
  1020. OS_DeInitBuffer(&tx_msg_buf);
  1021. continue;
  1022. }
  1023. switch(event.ID)
  1024. {
  1025. case EV_W5500_IRQ:
  1026. if (w5500->device_on)
  1027. {
  1028. w5500_read_irq(w5500);
  1029. }
  1030. break;
  1031. case EV_W5500_SOCKET_TX:
  1032. if (event.Param1) W5500_LOCK;
  1033. p = llist_traversal(&w5500->socket[event.Param1].tx_head, w5500_next_data_cache, &prv_w5500_ctrl->socket[event.Param1]);
  1034. if (p->is_sending)
  1035. {
  1036. DBG("socket %d is sending, need wait!", event.Param1);
  1037. }
  1038. else
  1039. {
  1040. w5500_socket_tx_next_data(w5500, event.Param1);
  1041. if (!p->is_sending)
  1042. {
  1043. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_ERROR, event.Param1, 0, 0);
  1044. }
  1045. }
  1046. if (event.Param1) W5500_UNLOCK;
  1047. break;
  1048. case EV_W5500_SOCKET_CONNECT:
  1049. uPV.u8[0] = 0;
  1050. while(w5500_socket_state(w5500, event.Param1) != SOCK_CLOSED)
  1051. {
  1052. w5500_socket_close(w5500, event.Param1);
  1053. uPV.u8[0]++;
  1054. if (uPV.u8[0] > 100)
  1055. {
  1056. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_ERROR, event.Param1, 0, 0);
  1057. break;
  1058. }
  1059. }
  1060. uPV.u32 = event.Param3;
  1061. w5500_socket_config(w5500, event.Param1, w5500->socket[event.Param1].is_tcp, uPV.u16[0]);
  1062. w5500_socket_connect(w5500, event.Param1, 0, event.Param2, uPV.u16[1]);
  1063. if (!w5500->socket[event.Param1].is_tcp)
  1064. {
  1065. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_CONNECT_OK, event.Param1, 0, 0);
  1066. }
  1067. break;
  1068. case EV_W5500_SOCKET_CLOSE:
  1069. if ((w5500_socket_state(w5500, event.Param1) != SOCK_CLOSED))
  1070. {
  1071. w5500_socket_disconnect(w5500, event.Param1);
  1072. }
  1073. else
  1074. {
  1075. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_CLOSE_OK, event.Param1, 0, 0);
  1076. }
  1077. break;
  1078. case EV_W5500_SOCKET_LISTEN:
  1079. uPV.u8[0] = 0;
  1080. while(w5500_socket_state(w5500, event.Param1) != SOCK_CLOSED)
  1081. {
  1082. w5500_socket_close(w5500, event.Param1);
  1083. uPV.u8[0]++;
  1084. if (uPV.u8[0] > 100)
  1085. {
  1086. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_ERROR, event.Param1, 0, 0);
  1087. }
  1088. }
  1089. uPV.u32 = event.Param3;
  1090. w5500_socket_config(w5500, event.Param1, w5500->socket[event.Param1].is_tcp, event.Param2);
  1091. w5500_socket_connect(w5500, event.Param1, 1, 0xffffffff, 0xff00);
  1092. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_LISTEN, event.Param1, 0, 0);
  1093. break;
  1094. case EV_W5500_SOCKET_DNS:
  1095. break;
  1096. case EV_W5500_RE_INIT:
  1097. w5500_init_reg(w5500);
  1098. break;
  1099. case EV_W5500_REG_OP:
  1100. uPV.u8[0] = event.Param3;
  1101. w5500_xfer(w5500, event.Param1, socket_index(event.Param2)|socket_reg|is_write, uPV.u8, 1);
  1102. break;
  1103. case EV_W5500_LINK:
  1104. w5500_link_state(w5500, !luat_gpio_get(w5500->link_pin));
  1105. break;
  1106. }
  1107. }
  1108. }
  1109. int w5500_set_static_ip(uint32_t ipv4, uint32_t submask, uint32_t gateway)
  1110. {
  1111. if (prv_w5500_ctrl)
  1112. {
  1113. prv_w5500_ctrl->static_ip = ipv4;
  1114. prv_w5500_ctrl->static_submask = submask;
  1115. prv_w5500_ctrl->static_gateway = gateway;
  1116. }
  1117. }
  1118. void w5500_set_mac(uint8_t mac[6])
  1119. {
  1120. if (prv_w5500_ctrl)
  1121. {
  1122. memcpy(prv_w5500_ctrl->mac, mac, 6);
  1123. }
  1124. }
  1125. void w5500_set_param(uint16_t timeout, uint8_t retry, uint8_t auto_speed, uint8_t force_arp)
  1126. {
  1127. if (prv_w5500_ctrl)
  1128. {
  1129. prv_w5500_ctrl->RTR = timeout;
  1130. prv_w5500_ctrl->RTR = retry;
  1131. prv_w5500_ctrl->auto_speed = auto_speed;
  1132. prv_w5500_ctrl->force_arp = force_arp;
  1133. }
  1134. }
  1135. int w5500_reset(void)
  1136. {
  1137. if (prv_w5500_ctrl && prv_w5500_ctrl->device_on)
  1138. {
  1139. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_RE_INIT, 0, 0, 0);
  1140. return 0;
  1141. }
  1142. else
  1143. {
  1144. return -1;
  1145. }
  1146. }
  1147. void w5500_init(luat_spi_t* spi, uint8_t irq_pin, uint8_t rst_pin, uint8_t link_pin)
  1148. {
  1149. uint8_t *uid;
  1150. size_t t, i;
  1151. if (!prv_w5500_ctrl)
  1152. {
  1153. w5500_ctrl_t *w5500 = malloc(sizeof(w5500_ctrl_t));
  1154. memset(w5500, 0, sizeof(w5500_ctrl_t));
  1155. w5500->socket_cb = w5500_dummy_callback;
  1156. w5500->tag = GetSysTickMS();
  1157. w5500->RCR = 8;
  1158. w5500->RTR = 2000;
  1159. w5500->spi_id = spi->id;
  1160. w5500->cs_pin = spi->cs;
  1161. w5500->irq_pin = irq_pin;
  1162. w5500->rst_pin = rst_pin;
  1163. w5500->link_pin = link_pin;
  1164. spi->cs = 0xff;
  1165. luat_spi_setup(spi);
  1166. luat_spi_config_dma(w5500->spi_id, 0xffff, 0xffff);
  1167. luat_gpio_t gpio = {0};
  1168. gpio.pin = w5500->cs_pin;
  1169. gpio.mode = Luat_GPIO_OUTPUT;
  1170. gpio.pull = Luat_GPIO_DEFAULT;
  1171. luat_gpio_setup(&gpio);
  1172. luat_gpio_set(w5500->cs_pin, 1);
  1173. gpio.pin = w5500->rst_pin;
  1174. luat_gpio_setup(&gpio);
  1175. luat_gpio_set(w5500->rst_pin, 0);
  1176. char rands[4];
  1177. platform_random(rands, 4);
  1178. for(i = 0; i < MAX_SOCK_NUM; i++)
  1179. {
  1180. INIT_LLIST_HEAD(&w5500->socket[i].tx_head);
  1181. INIT_LLIST_HEAD(&w5500->socket[i].rx_head);
  1182. }
  1183. w5500->dhcp_client.xid = BytesGetBe32(rands);
  1184. w5500->dhcp_client.state = DHCP_STATE_NOT_WORK;
  1185. uid = luat_mcu_unique_id(&t);
  1186. memcpy(w5500->mac, &uid[10], 6);
  1187. luat_thread_t thread;
  1188. thread.task_fun = w5500_task;
  1189. thread.name = "w5500";
  1190. thread.stack_size = 2 * 1024;
  1191. thread.priority = 3;
  1192. thread.userdata = w5500;
  1193. platform_create_task(&thread);
  1194. prv_w5500_ctrl = w5500;
  1195. w5500->task_handle = thread.handle;
  1196. prv_w5500_ctrl->device_on = 1;
  1197. }
  1198. }
  1199. uint8_t w5500_device_ready(void)
  1200. {
  1201. if (prv_w5500_ctrl)
  1202. {
  1203. return prv_w5500_ctrl->device_on;
  1204. }
  1205. else
  1206. {
  1207. return 0;
  1208. }
  1209. }
  1210. static int w5500_check_socket(w5500_ctrl_t *w5500, int socket_id, uint64_t tag)
  1211. {
  1212. if (w5500 != prv_w5500_ctrl) return -1;
  1213. if (socket_id < 1 || socket_id >= MAX_SOCK_NUM) return -1;
  1214. if (w5500->socket[socket_id].tag != tag) return -1;
  1215. if (!w5500->socket[socket_id].in_use) return -1;
  1216. return 0;
  1217. }
  1218. static int w5500_socket_check(int socket_id, uint64_t tag, void *user_data)
  1219. {
  1220. return w5500_check_socket(user_data, socket_id, tag);
  1221. }
  1222. static uint8_t w5500_check_ready(void *user_data)
  1223. {
  1224. return ((w5500_ctrl_t *)user_data)->network_ready;
  1225. }
  1226. static int w5500_create_soceket(uint8_t is_tcp, uint64_t *tag, void *param, uint8_t is_ipv6, void *user_data)
  1227. {
  1228. if (user_data != prv_w5500_ctrl) return -1;
  1229. int i, socket_id;
  1230. socket_id = -1;
  1231. W5500_LOCK;
  1232. if (!prv_w5500_ctrl->socket[prv_w5500_ctrl->next_index].in_use)
  1233. {
  1234. socket_id = prv_w5500_ctrl->next_index;
  1235. prv_w5500_ctrl->next_index++;
  1236. }
  1237. else
  1238. {
  1239. for (i = 1; i < MAX_SOCK_NUM; i++)
  1240. {
  1241. if (!prv_w5500_ctrl->socket[i].in_use)
  1242. {
  1243. socket_id = i;
  1244. prv_w5500_ctrl->next_index = i + 1;
  1245. break;
  1246. }
  1247. }
  1248. }
  1249. if (prv_w5500_ctrl->next_index >= MAX_SOCK_NUM)
  1250. {
  1251. prv_w5500_ctrl->next_index = 1;
  1252. }
  1253. if (socket_id > 0)
  1254. {
  1255. prv_w5500_ctrl->tag++;
  1256. *tag = prv_w5500_ctrl->tag;
  1257. prv_w5500_ctrl->socket[socket_id].in_use = 1;
  1258. prv_w5500_ctrl->socket[socket_id].tag = *tag;
  1259. prv_w5500_ctrl->socket[socket_id].rx_wait_size = 0;
  1260. prv_w5500_ctrl->socket[socket_id].tx_wait_size = 0;
  1261. prv_w5500_ctrl->socket[socket_id].param = param;
  1262. prv_w5500_ctrl->socket[socket_id].is_tcp = is_tcp;
  1263. llist_traversal(&prv_w5500_ctrl->socket[socket_id].tx_head, w5500_del_data_cache, NULL);
  1264. llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_del_data_cache, NULL);
  1265. }
  1266. W5500_UNLOCK;
  1267. return socket_id;
  1268. }
  1269. //作为client绑定一个port,并连接remote_ip和remote_port对应的server
  1270. static int w5500_socket_connect_ex(int socket_id, uint64_t tag, uint16_t local_port, luat_ip_addr_t *remote_ip, uint16_t remote_port, void *user_data)
  1271. {
  1272. int result = w5500_check_socket(user_data, socket_id, tag);
  1273. if (result) return result;
  1274. PV_Union uPV;
  1275. uPV.u16[0] = local_port;
  1276. uPV.u16[1] = remote_port;
  1277. W5500_LOCK;
  1278. llist_traversal(&prv_w5500_ctrl->socket[socket_id].tx_head, w5500_del_data_cache, NULL);
  1279. llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_del_data_cache, NULL);
  1280. W5500_UNLOCK;
  1281. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_CONNECT, socket_id, remote_ip->ipv4, uPV.u32);
  1282. return 0;
  1283. }
  1284. //作为server绑定一个port,开始监听
  1285. static int w5500_socket_listen(int socket_id, uint64_t tag, uint16_t local_port, void *user_data)
  1286. {
  1287. int result = w5500_check_socket(user_data, socket_id, tag);
  1288. if (result) return result;
  1289. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_LISTEN, socket_id, local_port, NULL);
  1290. return 0;
  1291. }
  1292. //作为server接受一个client
  1293. static int w5500_socket_accept(int socket_id, uint64_t tag, luat_ip_addr_t *remote_ip, uint16_t *remote_port, void *user_data)
  1294. {
  1295. return -1;
  1296. }
  1297. //主动断开一个tcp连接,需要走完整个tcp流程,用户需要接收到close ok回调才能确认彻底断开
  1298. static int w5500_socket_disconnect_ex(int socket_id, uint64_t tag, void *user_data)
  1299. {
  1300. int result = w5500_check_socket(user_data, socket_id, tag);
  1301. if (result) return result;
  1302. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_CLOSE, socket_id, 0, 0);
  1303. return 0;
  1304. }
  1305. static int w5500_socket_force_close(int socket_id, void *user_data)
  1306. {
  1307. W5500_LOCK;
  1308. w5500_socket_close(prv_w5500_ctrl, socket_id);
  1309. if (prv_w5500_ctrl->socket[socket_id].in_use)
  1310. {
  1311. prv_w5500_ctrl->socket[socket_id].in_use = 0;
  1312. prv_w5500_ctrl->socket[socket_id].tag = 0;
  1313. llist_traversal(&prv_w5500_ctrl->socket[socket_id].tx_head, w5500_del_data_cache, NULL);
  1314. llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_del_data_cache, NULL);
  1315. prv_w5500_ctrl->socket[socket_id].rx_wait_size = 0;
  1316. prv_w5500_ctrl->socket[socket_id].tx_wait_size = 0;
  1317. prv_w5500_ctrl->socket[socket_id].param = NULL;
  1318. }
  1319. W5500_UNLOCK;
  1320. return 0;
  1321. }
  1322. static int w5500_socket_close_ex(int socket_id, uint64_t tag, void *user_data)
  1323. {
  1324. int result = w5500_check_socket(user_data, socket_id, tag);
  1325. if (result) return result;
  1326. return w5500_socket_force_close(socket_id, user_data);
  1327. }
  1328. static uint32_t w5500_socket_read_data(uint8_t *buf, uint32_t *read_len, uint32_t len, socket_data_t *p)
  1329. {
  1330. uint32_t dummy_len;
  1331. dummy_len = ((p->len - p->read_pos) > (len - *read_len))?(len - *read_len):(p->len - p->read_pos);
  1332. memcpy(buf, p->data + p->read_pos, dummy_len);
  1333. p->read_pos += dummy_len;
  1334. if (p->read_pos >= p->len)
  1335. {
  1336. llist_del(&p->node);
  1337. free(p->data);
  1338. free(p);
  1339. }
  1340. *read_len += dummy_len;
  1341. return dummy_len;
  1342. }
  1343. static int w5500_socket_receive(int socket_id, uint64_t tag, uint8_t *buf, uint32_t len, int flags, luat_ip_addr_t *remote_ip, uint16_t *remote_port, void *user_data)
  1344. {
  1345. int result = w5500_check_socket(user_data, socket_id, tag);
  1346. if (result) return result;
  1347. W5500_LOCK;
  1348. uint32_t read_len = 0;
  1349. if (buf)
  1350. {
  1351. socket_data_t *p = (socket_data_t *)llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_next_data_cache, &prv_w5500_ctrl->socket[socket_id]);
  1352. if (prv_w5500_ctrl->socket[socket_id].is_tcp)
  1353. {
  1354. while((read_len < len) && p)
  1355. {
  1356. prv_w5500_ctrl->socket[socket_id].rx_wait_size -= w5500_socket_read_data(buf + read_len, &read_len, len, p);
  1357. p = (socket_data_t *)llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_next_data_cache, &prv_w5500_ctrl->socket[socket_id]);
  1358. }
  1359. }
  1360. else
  1361. {
  1362. prv_w5500_ctrl->socket[socket_id].rx_wait_size -= w5500_socket_read_data(buf + read_len, &read_len, len, p);
  1363. }
  1364. if (llist_empty(&prv_w5500_ctrl->socket[socket_id].rx_head))
  1365. {
  1366. prv_w5500_ctrl->socket[socket_id].rx_wait_size = 0;
  1367. }
  1368. }
  1369. else
  1370. {
  1371. read_len = prv_w5500_ctrl->socket[socket_id].rx_wait_size;
  1372. }
  1373. W5500_UNLOCK;
  1374. return read_len;
  1375. }
  1376. static int w5500_socket_send(int socket_id, uint64_t tag, const uint8_t *buf, uint32_t len, int flags, luat_ip_addr_t *remote_ip, uint16_t remote_port, void *user_data)
  1377. {
  1378. int result = w5500_check_socket(user_data, socket_id, tag);
  1379. if (result) return result;
  1380. if (prv_w5500_ctrl->socket[socket_id].tx_wait_size >= SOCK_BUF_LEN) return 0;
  1381. socket_data_t *p = w5500_create_data_node(prv_w5500_ctrl, socket_id, buf, len, remote_ip, remote_port);
  1382. if (p)
  1383. {
  1384. W5500_LOCK;
  1385. llist_add_tail(&p->node, &prv_w5500_ctrl->socket[socket_id].tx_head);
  1386. prv_w5500_ctrl->socket[socket_id].tx_wait_size += len;
  1387. W5500_UNLOCK;
  1388. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_TX, socket_id, 0, 0);
  1389. result = len;
  1390. }
  1391. else
  1392. {
  1393. result = -1;
  1394. }
  1395. return result;
  1396. }
  1397. void w5500_socket_clean(int *vaild_socket_list, uint32_t num, void *user_data)
  1398. {
  1399. if (user_data != prv_w5500_ctrl) return;
  1400. int socket_list[MAX_SOCK_NUM] = {0,0,0,0,0,0,0,0};
  1401. uint32_t i;
  1402. for(i = 0; i < num; i++)
  1403. {
  1404. if ( (vaild_socket_list[i] > 0) && (vaild_socket_list[i] < MAX_SOCK_NUM) )
  1405. {
  1406. socket_list[vaild_socket_list[i]] = 1;
  1407. }
  1408. DBG("%d,%d",i,vaild_socket_list[i]);
  1409. }
  1410. for(i = 1; i < MAX_SOCK_NUM; i++)
  1411. {
  1412. DBG("%d,%d",i,socket_list[i]);
  1413. if ( !socket_list[i] )
  1414. {
  1415. W5500_LOCK;
  1416. prv_w5500_ctrl->socket[i].in_use = 0;
  1417. prv_w5500_ctrl->socket[i].tag = 0;
  1418. llist_traversal(&prv_w5500_ctrl->socket[i].tx_head, w5500_del_data_cache, NULL);
  1419. llist_traversal(&prv_w5500_ctrl->socket[i].rx_head, w5500_del_data_cache, NULL);
  1420. prv_w5500_ctrl->socket[i].rx_wait_size = 0;
  1421. prv_w5500_ctrl->socket[i].tx_wait_size = 0;
  1422. w5500_socket_close(prv_w5500_ctrl, i);
  1423. W5500_UNLOCK;
  1424. }
  1425. }
  1426. }
  1427. static int w5500_getsockopt(int socket_id, uint64_t tag, int level, int optname, void *optval, uint32_t *optlen, void *user_data)
  1428. {
  1429. return -1;
  1430. }
  1431. static int w5500_setsockopt(int socket_id, uint64_t tag, int level, int optname, const void *optval, uint32_t optlen, void *user_data)
  1432. {
  1433. return -1;
  1434. }
  1435. static int w5500_get_local_ip_info(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, void *user_data)
  1436. {
  1437. if (user_data != prv_w5500_ctrl) return -1;
  1438. if (prv_w5500_ctrl->static_ip)
  1439. {
  1440. ip->ipv4 = prv_w5500_ctrl->static_ip;
  1441. ip->is_ipv6 = 0;
  1442. submask->ipv4 = prv_w5500_ctrl->static_submask;
  1443. submask->is_ipv6 = 0;
  1444. gateway->ipv4 = prv_w5500_ctrl->static_gateway;
  1445. gateway->is_ipv6 = 0;
  1446. return 0;
  1447. }
  1448. else
  1449. {
  1450. if (!prv_w5500_ctrl->ip_ready)
  1451. {
  1452. return -1;
  1453. }
  1454. ip->ipv4 = prv_w5500_ctrl->dhcp_client.ip;
  1455. ip->is_ipv6 = 0;
  1456. submask->ipv4 = prv_w5500_ctrl->dhcp_client.submask;
  1457. submask->is_ipv6 = 0;
  1458. gateway->ipv4 = prv_w5500_ctrl->dhcp_client.gateway;
  1459. gateway->is_ipv6 = 0;
  1460. return 0;
  1461. }
  1462. }
  1463. static int w5500_user_cmd(int socket_id, uint64_t tag, uint32_t cmd, uint32_t value, void *user_data)
  1464. {
  1465. int result = w5500_check_socket(user_data, socket_id, tag);
  1466. if (result) return result;
  1467. switch (cmd)
  1468. {
  1469. case NW_CMD_AUTO_HEART_TIME:
  1470. value = (value + 4) / 5;
  1471. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_REG_OP, W5500_SOCKET_KEEP_TIME, socket_id, value);
  1472. break;
  1473. default:
  1474. return -1;
  1475. }
  1476. return 0;
  1477. }
  1478. static int w5500_dns(const char *url, uint32_t len, void *user_data)
  1479. {
  1480. if (user_data != prv_w5500_ctrl) return -1;
  1481. return -1;
  1482. }
  1483. static int w5500_set_dns_server(uint8_t server_index, luat_ip_addr_t *ip, void *user_data)
  1484. {
  1485. if (user_data != prv_w5500_ctrl) return -1;
  1486. if (server_index >= MAX_DNS_SERVER) return -1;
  1487. prv_w5500_ctrl->static_dns_server[server_index] = ip->ipv4;
  1488. return 0;
  1489. }
  1490. static void w5500_socket_set_callback(CBFuncEx_t cb_fun, void *param, void *user_data)
  1491. {
  1492. if (user_data != prv_w5500_ctrl) return ;
  1493. ((w5500_ctrl_t *)user_data)->socket_cb = cb_fun?cb_fun:w5500_dummy_callback;
  1494. ((w5500_ctrl_t *)user_data)->user_data = param;
  1495. }
  1496. static network_adapter_info prv_w5500_adapter =
  1497. {
  1498. .check_ready = w5500_check_ready,
  1499. .create_soceket = w5500_create_soceket,
  1500. .socket_connect = w5500_socket_connect_ex,
  1501. .socket_listen = w5500_socket_listen,
  1502. .socket_accept = w5500_socket_accept,
  1503. .socket_disconnect = w5500_socket_disconnect_ex,
  1504. .socket_close = w5500_socket_close_ex,
  1505. .socket_force_close = w5500_socket_force_close,
  1506. .socket_receive = w5500_socket_receive,
  1507. .socket_send = w5500_socket_send,
  1508. .socket_check = w5500_socket_check,
  1509. .socket_clean = w5500_socket_clean,
  1510. .getsockopt = w5500_getsockopt,
  1511. .setsockopt = w5500_setsockopt,
  1512. .user_cmd = w5500_user_cmd,
  1513. .dns = w5500_dns,
  1514. .set_dns_server = w5500_set_dns_server,
  1515. .get_local_ip_info = w5500_get_local_ip_info,
  1516. .socket_set_callback = w5500_socket_set_callback,
  1517. .name = "w5500",
  1518. .max_socket_num = MAX_SOCK_NUM - 1,
  1519. .no_accept = 1,
  1520. .is_posix = 0,
  1521. };
  1522. void w5500_register_adapter(int index)
  1523. {
  1524. if (prv_w5500_ctrl)
  1525. {
  1526. network_register_adapter(index, &prv_w5500_adapter, prv_w5500_ctrl);
  1527. }
  1528. }
  1529. #else
  1530. void w5500_init(luat_spi_t* spi, uint8_t irq_pin, uint8_t rst_pin) {;}
  1531. void w5500_set_static_ip(uint32_t ipv4, uint32_t submask, uint32_t gateway) {;}
  1532. void w5500_set_mac(uint8_t mac[6]) {;}
  1533. void w5500_set_param(uint16_t timeout, uint8_t retry, uint8_t auto_speed, uint8_t force_arp) {;}
  1534. int w5500_reset(void) {return -1;}
  1535. uint32_t w5500_string_to_ip(const char *string, uint32_t len) {return 0}
  1536. void w5500_array_to_mac(uint8_t *array, uint32_t *mac1, uint16_t *mac2) {return;}
  1537. uint8_t w5500_device_ready(void) {return 0;}
  1538. void w5500_register_adapter(int index) {;}
  1539. #endif