Basic example using W5500 Ported to LPC800_Max (LPC812)
Fork of W5500_Porting_WIZnetLib by
Diff: main.cpp
- Revision:
- 2:12863af451af
- Parent:
- 0:496d676b6364
diff -r 0476e70177e6 -r 12863af451af main.cpp --- a/main.cpp Tue Feb 18 05:42:27 2014 +0000 +++ b/main.cpp Mon Mar 31 04:01:59 2014 +0000 @@ -2,9 +2,9 @@ #include "Ethernet/wizchip_conf.h" #include "Ethernet/socket.h" -SPI spi(p5, p6, p7); // mosi, miso, sclk -DigitalOut cs(p8), nRESET(p9); -DigitalIn RDY(p10); +SPI spi(P0_14, P0_15, P0_12); // mosi, miso, sclk +DigitalOut cs(P0_13), nRESET(P0_16); // reset pin of Wiz550io +DigitalIn RDY(P0_17); // ready pin of Wiz550io Serial pc(USBTX, USBRX); // tx, rx @@ -29,22 +29,15 @@ pc.baud(115200); spi.format(8,0); // 8bit, mode 0 - //spi.frequency(80000000); // 80MHz - //spi.frequency(8000000); // 8MHz - spi.frequency(7000000); // 7MHz - //spi.frequency(6000000); // 6MHz - //spi.frequency(5000000); // 5MHz - //spi.frequency(1000000); // 1MHz - //spi.frequency(100000); // 100KHz - //spi.frequency(10000); // 10KHz + spi.frequency(1000000); // 1MHz. Do not use more than 1Mhz on the LPC800, errors result nRESET = 0; wait(0.001); // 1ms nRESET = 1; while(RDY!=1); + cs = 1; - //wait(0.1); // 100ms #if _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_ reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); @@ -65,47 +58,27 @@ pc.printf("WIZCHIP Initialized fail.\r\n"); while(1); } - do - { + + do { if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) - pc.printf("Unknown PHY Link stauts.\r\n"); - }while(tmp == PHY_LINK_OFF); + pc.printf("Unknown PHY Link status.\r\n"); + } while(tmp == PHY_LINK_OFF); + /* wizchip netconf */ -#if _WIZCHIP_ == 5200 - gWIZNETINFO.mac[0] = 0x00; - gWIZNETINFO.mac[1] = 0x08; - gWIZNETINFO.mac[2] = 0xDC; - gWIZNETINFO.mac[3] = 0x00; - gWIZNETINFO.mac[4] = 0xAB; - gWIZNETINFO.mac[5] = 0xCD; + ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); + gWIZNETINFO.ip[0] = 192; gWIZNETINFO.ip[1] = 168; - gWIZNETINFO.ip[2] = 1; - gWIZNETINFO.ip[3] = 119; - gWIZNETINFO.gw[0] = 192; - gWIZNETINFO.gw[1] = 168; - gWIZNETINFO.gw[2] = 1; - gWIZNETINFO.gw[3] = 1; - gWIZNETINFO.sn[0] = 255; - gWIZNETINFO.sn[1] = 255; - gWIZNETINFO.sn[2] = 255; - gWIZNETINFO.sn[3] = 0; - gWIZNETINFO.dns[0] = 0; - gWIZNETINFO.dns[1] = 0; - gWIZNETINFO.dns[2] = 0; - gWIZNETINFO.dns[3] = 0; + gWIZNETINFO.ip[2] = 43; + gWIZNETINFO.ip[3] = 116; gWIZNETINFO.dhcp = NETINFO_STATIC; -#else - ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); -#endif - gWIZNETINFO.ip[0] = 192; - gWIZNETINFO.ip[1] = 168; - gWIZNETINFO.ip[2] = 1; - gWIZNETINFO.ip[3] = 119; + ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO); ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); + //DISPLAY NETWORK ctlwizchip(CW_GET_ID,(void*)tmpstr); + pc.printf("=== %s NET CONF ===\r\n",(char*)tmpstr); pc.printf("MAC:%02X.%02X.%02X.%02X.%02X.%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2], gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]); @@ -116,43 +89,44 @@ while(1) { uint8_t sn = 2; - if((ret = socket(sn,Sn_MR_TCP,3000,SF_TCP_NODELAY)) != sn) + + if((ret = socket(sn, Sn_MR_TCP, 3000, SF_TCP_NODELAY)) != sn) { - printf("%d:socket() error:%ld\r\n", sn,ret); + pc.printf("%d:socket() error: %ld\r\n", sn,ret); close(sn); continue; } - printf("%d:socket() ok.\r\n",sn); - if((ret = listen(sn)) != SOCK_OK) - { - printf("%d:listen() error:%ld",sn,ret); + pc.printf("%d:socket() ok.\r\n", sn); + + if((ret = listen(sn)) != SOCK_OK) { + pc.printf("%d:listen() error:%ld", sn, ret); close(sn); return ret; } - printf("%d:listen() ok.\r\n",sn); - if((ret = socket(sn+1, Sn_MR_UDP, 3000, 0)) != sn+1) - { - printf("%d:socket() error:%ld\r\n", sn+1,ret); + pc.printf("%d:listen() ok.\r\n", sn); + + if((ret = socket(sn + 1, Sn_MR_UDP, 3000, 0)) != sn + 1) { + pc.printf("%d:socket() error:%ld\r\n", sn + 1, ret); continue; - } - do - { - getsockopt(sn,SO_STATUS, &tmp); - }while(tmp != SOCK_CLOSED && tmp != SOCK_ESTABLISHED); + } + + do { + getsockopt(sn, SO_STATUS, &tmp); + } while(tmp != SOCK_CLOSED && tmp != SOCK_ESTABLISHED); + if(tmp == SOCK_CLOSED) continue; + //Accept for client - printf("%d:connected\r\nLBStarted Blocking mode\r\n",sn); - while(1) - { - if((ret = loopback_tcps(sn, gDATABUF, DATA_BUF_SIZE)) < 0) - { - printf("%d:loopback_tcps error:%ld\r\n",sn,ret); + pc.printf("%d:connected\r\nLBStarted Blocking mode\r\n", sn); + + while(1) { + if((ret = loopback_tcps(sn, gDATABUF, DATA_BUF_SIZE)) < 0) { + pc.printf("%d:loopback_tcps error:%ld\r\n", sn, ret); break; } /* - if((ret=loopback_udps(sn+1,gDATABUF,10)) < 0) - { - printf("%d:loopback_udps error:%ld\r\n",sn+1,ret); + if((ret=loopback_udps(sn+1,gDATABUF,10)) < 0) { + pc.printf("%d:loopback_udps error:%ld\r\n", sn + 1, ret); break; } */ @@ -183,23 +157,27 @@ int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t size) { int32_t ret = 0; - ret = recv(sn,buf,size); - if(ret != size) - { + + ret = recv(sn, buf, size); + + if(ret != size) { if(ret < 0) { - printf("%d:recv() error:%ld\r\n",sn,ret); + pc.printf("%d:recv() error:%ld\r\n", sn, ret); close(sn); return ret; } } + size = ret; - ret = send(sn,buf,size); - if(ret != size) - { - if(ret < 0) - { - printf("%d:send() error:%ld\r\n",sn,ret); + ret = send(sn, buf, size); + + for(int x = 0; x < size; x++) + pc.printf("%c", buf[x]); + + if(ret != size) { + if(ret < 0) { + pc.printf("%d:send() error:%ld\r\n", sn, ret); close(sn); } } @@ -215,26 +193,26 @@ if((ret = recvfrom(sn,buf,size, addr,&port,&packinfo)) < 0) { - printf("%d:recvfrom error:%ld\r\n",sn,ret); + pc.printf("%d:recvfrom error:%ld\r\n", sn, ret); return ret; } if(packinfo & 0x80) { - printf("%d:recvfrom %d.%d.%d.%d(%d), size=%ld.\r\n",sn,addr[0],addr[1],addr[2],addr[3],port, ret); + pc.printf("%d:recvfrom %d.%d.%d.%d(%d), size=%ld.\r\n", sn, addr[0], addr[1], addr[2], addr[3], port, ret); } if(packinfo & 0x01) { - printf("%d:recvfrom remained packet.\r\n",sn); + pc.printf("%d:recvfrom remained packet.\r\n", sn); } else { - printf("%d:recvfrom completed.\r\n",sn); + pc.printf("%d:recvfrom completed.\r\n", sn); } if( (ret = sendto(sn, buf, ret, addr, port)) < 0) { - printf("%d:sendto error:%ld\r\n",sn,ret); + pc.printf("%d:sendto error:%ld\r\n", sn, ret); return ret; } - printf("%d:sendto %d.%d.%d.%d(%d), size=%ld\r\n",sn,addr[0],addr[1],addr[2],addr[3],port, ret); + pc.printf("%d:sendto %d.%d.%d.%d(%d), size=%ld\r\n", sn, addr[0], addr[1], addr[2], addr[3], port, ret); return ret; } \ No newline at end of file