w5500.c 58 KB

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