![](/media/cache/profiles/a9992f6c7611759605d948f6084808ed.50x50_q85.jpg)
Wiznet W5500 driver and TCP/UDP loopback
main.cpp@0:2513c6696bdc, 2013-12-13 (annotated)
- Committer:
- jbkim
- Date:
- Fri Dec 13 07:35:58 2013 +0000
- Revision:
- 0:2513c6696bdc
Wiznet W5500 library and TCP/UDP loopback program for mbed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jbkim | 0:2513c6696bdc | 1 | /* Wiznet W5500 Test Program |
jbkim | 0:2513c6696bdc | 2 | * |
jbkim | 0:2513c6696bdc | 3 | * TCP server(192.168.1.125) is listening with port number 3000 and send back the data to the peer. |
jbkim | 0:2513c6696bdc | 4 | * |
jbkim | 0:2513c6696bdc | 5 | */ |
jbkim | 0:2513c6696bdc | 6 | |
jbkim | 0:2513c6696bdc | 7 | #include "mbed.h" |
jbkim | 0:2513c6696bdc | 8 | #include "Ethernet/wizchip_conf.h" |
jbkim | 0:2513c6696bdc | 9 | #include "Ethernet/socket.h" |
jbkim | 0:2513c6696bdc | 10 | |
jbkim | 0:2513c6696bdc | 11 | SPI spi(p5, p6, p7); // mosi, miso, sclk |
jbkim | 0:2513c6696bdc | 12 | DigitalOut cs(p8), nRESET(p9); // reset pin of Wiz550io |
jbkim | 0:2513c6696bdc | 13 | DigitalIn RDY(p10); // ready pin of Wiz550io |
jbkim | 0:2513c6696bdc | 14 | |
jbkim | 0:2513c6696bdc | 15 | Serial pc(USBTX, USBRX); // tx, rx |
jbkim | 0:2513c6696bdc | 16 | |
jbkim | 0:2513c6696bdc | 17 | void wizchip_select(void); |
jbkim | 0:2513c6696bdc | 18 | void wizchip_deselect(void); |
jbkim | 0:2513c6696bdc | 19 | uint8_t wizchip_read(); |
jbkim | 0:2513c6696bdc | 20 | void wizchip_write(uint8_t wb); |
jbkim | 0:2513c6696bdc | 21 | int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t size); |
jbkim | 0:2513c6696bdc | 22 | int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t size); |
jbkim | 0:2513c6696bdc | 23 | |
jbkim | 0:2513c6696bdc | 24 | #define DATA_BUF_SIZE 2048 |
jbkim | 0:2513c6696bdc | 25 | uint8_t gDATABUF[DATA_BUF_SIZE]; |
jbkim | 0:2513c6696bdc | 26 | wiz_NetInfo gWIZNETINFO; |
jbkim | 0:2513c6696bdc | 27 | |
jbkim | 0:2513c6696bdc | 28 | int main() |
jbkim | 0:2513c6696bdc | 29 | { |
jbkim | 0:2513c6696bdc | 30 | uint8_t tmp; |
jbkim | 0:2513c6696bdc | 31 | uint8_t tmpstr[6] = {0,}; |
jbkim | 0:2513c6696bdc | 32 | int32_t ret = 0; |
jbkim | 0:2513c6696bdc | 33 | uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}}; |
jbkim | 0:2513c6696bdc | 34 | |
jbkim | 0:2513c6696bdc | 35 | pc.baud(115200); |
jbkim | 0:2513c6696bdc | 36 | |
jbkim | 0:2513c6696bdc | 37 | spi.format(8,0); // 8bit, mode 0 |
jbkim | 0:2513c6696bdc | 38 | spi.frequency(7000000); // 7MHz |
jbkim | 0:2513c6696bdc | 39 | |
jbkim | 0:2513c6696bdc | 40 | |
jbkim | 0:2513c6696bdc | 41 | nRESET = 0; |
jbkim | 0:2513c6696bdc | 42 | wait(0.001); // 1ms |
jbkim | 0:2513c6696bdc | 43 | nRESET = 1; |
jbkim | 0:2513c6696bdc | 44 | |
jbkim | 0:2513c6696bdc | 45 | cs = 1; |
jbkim | 0:2513c6696bdc | 46 | wait(0.1); // 100ms |
jbkim | 0:2513c6696bdc | 47 | |
jbkim | 0:2513c6696bdc | 48 | // register the chip select/ deselect fucntion |
jbkim | 0:2513c6696bdc | 49 | reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); |
jbkim | 0:2513c6696bdc | 50 | |
jbkim | 0:2513c6696bdc | 51 | // register the chip read/ write fucntion |
jbkim | 0:2513c6696bdc | 52 | reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); |
jbkim | 0:2513c6696bdc | 53 | |
jbkim | 0:2513c6696bdc | 54 | /* wizchip initialize*/ |
jbkim | 0:2513c6696bdc | 55 | if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) { |
jbkim | 0:2513c6696bdc | 56 | pc.printf("WIZCHIP Initialized fail.\r\n"); |
jbkim | 0:2513c6696bdc | 57 | while(1); |
jbkim | 0:2513c6696bdc | 58 | } |
jbkim | 0:2513c6696bdc | 59 | |
jbkim | 0:2513c6696bdc | 60 | do |
jbkim | 0:2513c6696bdc | 61 | { |
jbkim | 0:2513c6696bdc | 62 | if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) |
jbkim | 0:2513c6696bdc | 63 | pc.printf("Unknown PHY Link stauts.\r\n"); |
jbkim | 0:2513c6696bdc | 64 | }while(tmp == PHY_LINK_OFF); |
jbkim | 0:2513c6696bdc | 65 | |
jbkim | 0:2513c6696bdc | 66 | /* wizchip netconf */ |
jbkim | 0:2513c6696bdc | 67 | ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); |
jbkim | 0:2513c6696bdc | 68 | |
jbkim | 0:2513c6696bdc | 69 | gWIZNETINFO.ip[0] = 192; |
jbkim | 0:2513c6696bdc | 70 | gWIZNETINFO.ip[1] = 168; |
jbkim | 0:2513c6696bdc | 71 | gWIZNETINFO.ip[2] = 1; |
jbkim | 0:2513c6696bdc | 72 | gWIZNETINFO.ip[3] = 125; |
jbkim | 0:2513c6696bdc | 73 | |
jbkim | 0:2513c6696bdc | 74 | // set network configuration |
jbkim | 0:2513c6696bdc | 75 | ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO); |
jbkim | 0:2513c6696bdc | 76 | |
jbkim | 0:2513c6696bdc | 77 | // get network configuration |
jbkim | 0:2513c6696bdc | 78 | ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); |
jbkim | 0:2513c6696bdc | 79 | |
jbkim | 0:2513c6696bdc | 80 | // Display network configuration |
jbkim | 0:2513c6696bdc | 81 | ctlwizchip(CW_GET_ID,(void*)tmpstr); |
jbkim | 0:2513c6696bdc | 82 | pc.printf("=== %s NET CONF ===\r\n",(char*)tmpstr); |
jbkim | 0:2513c6696bdc | 83 | pc.printf("MAC:%02X.%02X.%02X.%02X.%02X.%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2], |
jbkim | 0:2513c6696bdc | 84 | gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]); |
jbkim | 0:2513c6696bdc | 85 | pc.printf("GAR:%d.%d.%d.%d\r\n", gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]); |
jbkim | 0:2513c6696bdc | 86 | pc.printf("SUB:%d.%d.%d.%d\r\n", gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]); |
jbkim | 0:2513c6696bdc | 87 | pc.printf("SIP:%d.%d.%d.%d\r\n", gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]); |
jbkim | 0:2513c6696bdc | 88 | |
jbkim | 0:2513c6696bdc | 89 | while(1) |
jbkim | 0:2513c6696bdc | 90 | { |
jbkim | 0:2513c6696bdc | 91 | uint8_t sn = 2; |
jbkim | 0:2513c6696bdc | 92 | if((ret = socket(sn,Sn_MR_TCP,3000,SF_TCP_NODELAY)) != sn) |
jbkim | 0:2513c6696bdc | 93 | { |
jbkim | 0:2513c6696bdc | 94 | printf("%d:socket() error:%ld\r\n", sn,ret); |
jbkim | 0:2513c6696bdc | 95 | close(sn); |
jbkim | 0:2513c6696bdc | 96 | continue; |
jbkim | 0:2513c6696bdc | 97 | } |
jbkim | 0:2513c6696bdc | 98 | printf("%d:socket() ok.\r\n",sn); |
jbkim | 0:2513c6696bdc | 99 | |
jbkim | 0:2513c6696bdc | 100 | |
jbkim | 0:2513c6696bdc | 101 | if((ret = listen(sn)) != SOCK_OK) |
jbkim | 0:2513c6696bdc | 102 | { |
jbkim | 0:2513c6696bdc | 103 | printf("%d:listen() error:%ld",sn,ret); |
jbkim | 0:2513c6696bdc | 104 | close(sn); |
jbkim | 0:2513c6696bdc | 105 | return ret; |
jbkim | 0:2513c6696bdc | 106 | } |
jbkim | 0:2513c6696bdc | 107 | printf("%d:listen() ok.\r\n",sn); |
jbkim | 0:2513c6696bdc | 108 | |
jbkim | 0:2513c6696bdc | 109 | |
jbkim | 0:2513c6696bdc | 110 | if(((ret = socket(sn+1, Sn_MR_UDP, 3000, 0)) != sn+1)) |
jbkim | 0:2513c6696bdc | 111 | { |
jbkim | 0:2513c6696bdc | 112 | printf("%d:socket() error:%ld\r\n", sn+1,ret); |
jbkim | 0:2513c6696bdc | 113 | continue; |
jbkim | 0:2513c6696bdc | 114 | } |
jbkim | 0:2513c6696bdc | 115 | do |
jbkim | 0:2513c6696bdc | 116 | { |
jbkim | 0:2513c6696bdc | 117 | getsockopt(sn,SO_STATUS, &tmp); |
jbkim | 0:2513c6696bdc | 118 | }while(tmp != SOCK_CLOSED && tmp != SOCK_ESTABLISHED); |
jbkim | 0:2513c6696bdc | 119 | if(tmp == SOCK_CLOSED) continue; |
jbkim | 0:2513c6696bdc | 120 | //Accept for client |
jbkim | 0:2513c6696bdc | 121 | printf("%d:connected\r\nLBStarted Blocking mode\r\n",sn); |
jbkim | 0:2513c6696bdc | 122 | |
jbkim | 0:2513c6696bdc | 123 | while(1) |
jbkim | 0:2513c6696bdc | 124 | { |
jbkim | 0:2513c6696bdc | 125 | if((ret = loopback_tcps(sn, gDATABUF, DATA_BUF_SIZE)) < 0) |
jbkim | 0:2513c6696bdc | 126 | { |
jbkim | 0:2513c6696bdc | 127 | printf("%d:loopback_tcps error:%ld\r\n",sn,ret); |
jbkim | 0:2513c6696bdc | 128 | break; |
jbkim | 0:2513c6696bdc | 129 | } |
jbkim | 0:2513c6696bdc | 130 | /* |
jbkim | 0:2513c6696bdc | 131 | if((ret=loopback_udps(sn+1,gDATABUF,10)) < 0) |
jbkim | 0:2513c6696bdc | 132 | { |
jbkim | 0:2513c6696bdc | 133 | printf("%d:loopback_udps error:%ld\r\n",sn+1,ret); |
jbkim | 0:2513c6696bdc | 134 | break; |
jbkim | 0:2513c6696bdc | 135 | } |
jbkim | 0:2513c6696bdc | 136 | */ |
jbkim | 0:2513c6696bdc | 137 | } |
jbkim | 0:2513c6696bdc | 138 | } |
jbkim | 0:2513c6696bdc | 139 | } |
jbkim | 0:2513c6696bdc | 140 | |
jbkim | 0:2513c6696bdc | 141 | void wizchip_select(void) |
jbkim | 0:2513c6696bdc | 142 | { |
jbkim | 0:2513c6696bdc | 143 | cs = 0; |
jbkim | 0:2513c6696bdc | 144 | } |
jbkim | 0:2513c6696bdc | 145 | |
jbkim | 0:2513c6696bdc | 146 | void wizchip_deselect(void) |
jbkim | 0:2513c6696bdc | 147 | { |
jbkim | 0:2513c6696bdc | 148 | cs = 1; |
jbkim | 0:2513c6696bdc | 149 | } |
jbkim | 0:2513c6696bdc | 150 | |
jbkim | 0:2513c6696bdc | 151 | uint8_t wizchip_read() |
jbkim | 0:2513c6696bdc | 152 | { |
jbkim | 0:2513c6696bdc | 153 | return spi.write(0x00); |
jbkim | 0:2513c6696bdc | 154 | } |
jbkim | 0:2513c6696bdc | 155 | |
jbkim | 0:2513c6696bdc | 156 | void wizchip_write(uint8_t wb) |
jbkim | 0:2513c6696bdc | 157 | { |
jbkim | 0:2513c6696bdc | 158 | spi.write(wb); |
jbkim | 0:2513c6696bdc | 159 | } |
jbkim | 0:2513c6696bdc | 160 | |
jbkim | 0:2513c6696bdc | 161 | int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t size) |
jbkim | 0:2513c6696bdc | 162 | { |
jbkim | 0:2513c6696bdc | 163 | int32_t ret = 0; |
jbkim | 0:2513c6696bdc | 164 | ret = recv(sn,buf,size); |
jbkim | 0:2513c6696bdc | 165 | if(ret != size) |
jbkim | 0:2513c6696bdc | 166 | { |
jbkim | 0:2513c6696bdc | 167 | if(ret < 0) |
jbkim | 0:2513c6696bdc | 168 | { |
jbkim | 0:2513c6696bdc | 169 | printf("%d:recv() error:%ld\r\n",sn,ret); |
jbkim | 0:2513c6696bdc | 170 | close(sn); |
jbkim | 0:2513c6696bdc | 171 | return ret; |
jbkim | 0:2513c6696bdc | 172 | } |
jbkim | 0:2513c6696bdc | 173 | } |
jbkim | 0:2513c6696bdc | 174 | size = ret; |
jbkim | 0:2513c6696bdc | 175 | ret = send(sn,buf,size); |
jbkim | 0:2513c6696bdc | 176 | if(ret != size) |
jbkim | 0:2513c6696bdc | 177 | { |
jbkim | 0:2513c6696bdc | 178 | if(ret < 0) |
jbkim | 0:2513c6696bdc | 179 | { |
jbkim | 0:2513c6696bdc | 180 | printf("%d:send() error:%ld\r\n",sn,ret); |
jbkim | 0:2513c6696bdc | 181 | close(sn); |
jbkim | 0:2513c6696bdc | 182 | } |
jbkim | 0:2513c6696bdc | 183 | } |
jbkim | 0:2513c6696bdc | 184 | return ret; |
jbkim | 0:2513c6696bdc | 185 | } |
jbkim | 0:2513c6696bdc | 186 | |
jbkim | 0:2513c6696bdc | 187 | int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t size) |
jbkim | 0:2513c6696bdc | 188 | { |
jbkim | 0:2513c6696bdc | 189 | int32_t ret = 0; |
jbkim | 0:2513c6696bdc | 190 | static uint8_t addr[4] = {0,}; |
jbkim | 0:2513c6696bdc | 191 | static uint16_t port = 0; |
jbkim | 0:2513c6696bdc | 192 | uint8_t packinfo; |
jbkim | 0:2513c6696bdc | 193 | |
jbkim | 0:2513c6696bdc | 194 | if((ret = recvfrom(sn,buf,size, addr,&port,&packinfo)) < 0) |
jbkim | 0:2513c6696bdc | 195 | { |
jbkim | 0:2513c6696bdc | 196 | printf("%d:recvfrom error:%ld\r\n",sn,ret); |
jbkim | 0:2513c6696bdc | 197 | return ret; |
jbkim | 0:2513c6696bdc | 198 | } |
jbkim | 0:2513c6696bdc | 199 | if(packinfo & 0x80) |
jbkim | 0:2513c6696bdc | 200 | { |
jbkim | 0:2513c6696bdc | 201 | printf("%d:recvfrom %d.%d.%d.%d(%d), size=%ld.\r\n",sn,addr[0],addr[1],addr[2],addr[3],port, ret); |
jbkim | 0:2513c6696bdc | 202 | } |
jbkim | 0:2513c6696bdc | 203 | if(packinfo & 0x01) |
jbkim | 0:2513c6696bdc | 204 | { |
jbkim | 0:2513c6696bdc | 205 | printf("%d:recvfrom remained packet.\r\n",sn); |
jbkim | 0:2513c6696bdc | 206 | } |
jbkim | 0:2513c6696bdc | 207 | else |
jbkim | 0:2513c6696bdc | 208 | { |
jbkim | 0:2513c6696bdc | 209 | printf("%d:recvfrom completed.\r\n",sn); |
jbkim | 0:2513c6696bdc | 210 | } |
jbkim | 0:2513c6696bdc | 211 | if( (ret = sendto(sn, buf, ret, addr, port)) < 0) |
jbkim | 0:2513c6696bdc | 212 | { |
jbkim | 0:2513c6696bdc | 213 | printf("%d:sendto error:%ld\r\n",sn,ret); |
jbkim | 0:2513c6696bdc | 214 | return ret; |
jbkim | 0:2513c6696bdc | 215 | } |
jbkim | 0:2513c6696bdc | 216 | printf("%d:sendto %d.%d.%d.%d(%d), size=%ld\r\n",sn,addr[0],addr[1],addr[2],addr[3],port, ret); |
jbkim | 0:2513c6696bdc | 217 | return ret; |
jbkim | 0:2513c6696bdc | 218 | } |