Basic example using W5500 Ported to LPC800_Max (LPC812)

Dependencies:   Ethernet mbed

Fork of W5500_Porting_WIZnetLib by WIZnet

Committer:
xeon011
Date:
Tue Feb 18 05:37:15 2014 +0000
Revision:
0:496d676b6364
Child:
2:12863af451af
First release;   - W5500 Driver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xeon011 0:496d676b6364 1 #include "mbed.h"
xeon011 0:496d676b6364 2 #include "Ethernet/wizchip_conf.h"
xeon011 0:496d676b6364 3 #include "Ethernet/socket.h"
xeon011 0:496d676b6364 4
xeon011 0:496d676b6364 5 SPI spi(p5, p6, p7); // mosi, miso, sclk
xeon011 0:496d676b6364 6 DigitalOut cs(p8), nRESET(p9);
xeon011 0:496d676b6364 7 DigitalIn RDY(p10);
xeon011 0:496d676b6364 8
xeon011 0:496d676b6364 9 Serial pc(USBTX, USBRX); // tx, rx
xeon011 0:496d676b6364 10
xeon011 0:496d676b6364 11 void wizchip_select(void);
xeon011 0:496d676b6364 12 void wizchip_deselect(void);
xeon011 0:496d676b6364 13 uint8_t wizchip_read();
xeon011 0:496d676b6364 14 void wizchip_write(uint8_t wb);
xeon011 0:496d676b6364 15 int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t size);
xeon011 0:496d676b6364 16 int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t size);
xeon011 0:496d676b6364 17
xeon011 0:496d676b6364 18 #define DATA_BUF_SIZE 2048
xeon011 0:496d676b6364 19 uint8_t gDATABUF[DATA_BUF_SIZE];
xeon011 0:496d676b6364 20 wiz_NetInfo gWIZNETINFO;
xeon011 0:496d676b6364 21
xeon011 0:496d676b6364 22 int main()
xeon011 0:496d676b6364 23 {
xeon011 0:496d676b6364 24 uint8_t tmp;
xeon011 0:496d676b6364 25 uint8_t tmpstr[6] = {0,};
xeon011 0:496d676b6364 26 int32_t ret = 0;
xeon011 0:496d676b6364 27 uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
xeon011 0:496d676b6364 28
xeon011 0:496d676b6364 29 pc.baud(115200);
xeon011 0:496d676b6364 30
xeon011 0:496d676b6364 31 spi.format(8,0); // 8bit, mode 0
xeon011 0:496d676b6364 32 //spi.frequency(80000000); // 80MHz
xeon011 0:496d676b6364 33 //spi.frequency(8000000); // 8MHz
xeon011 0:496d676b6364 34 spi.frequency(7000000); // 7MHz
xeon011 0:496d676b6364 35 //spi.frequency(6000000); // 6MHz
xeon011 0:496d676b6364 36 //spi.frequency(5000000); // 5MHz
xeon011 0:496d676b6364 37 //spi.frequency(1000000); // 1MHz
xeon011 0:496d676b6364 38 //spi.frequency(100000); // 100KHz
xeon011 0:496d676b6364 39 //spi.frequency(10000); // 10KHz
xeon011 0:496d676b6364 40
xeon011 0:496d676b6364 41 nRESET = 0;
xeon011 0:496d676b6364 42 wait(0.001); // 1ms
xeon011 0:496d676b6364 43 nRESET = 1;
xeon011 0:496d676b6364 44
xeon011 0:496d676b6364 45 while(RDY!=1);
xeon011 0:496d676b6364 46 cs = 1;
xeon011 0:496d676b6364 47 //wait(0.1); // 100ms
xeon011 0:496d676b6364 48
xeon011 0:496d676b6364 49 #if _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_
xeon011 0:496d676b6364 50 reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
xeon011 0:496d676b6364 51 #elif _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_
xeon011 0:496d676b6364 52 reg_wizchip_cs_cbfunc(wizchip_select, wizchip_select); // CS must be tried with LOW.
xeon011 0:496d676b6364 53 #else
xeon011 0:496d676b6364 54 #if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_
xeon011 0:496d676b6364 55 #error "Unknown _WIZCHIP_IO_MODE_"
xeon011 0:496d676b6364 56 #else
xeon011 0:496d676b6364 57 reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
xeon011 0:496d676b6364 58 #endif
xeon011 0:496d676b6364 59 #endif
xeon011 0:496d676b6364 60 //reg_wizchip_bus_cbfunc(uint8_t (*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, uint8_t wb));
xeon011 0:496d676b6364 61 reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write);
xeon011 0:496d676b6364 62
xeon011 0:496d676b6364 63 /* wizchip initialize*/
xeon011 0:496d676b6364 64 if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) {
xeon011 0:496d676b6364 65 pc.printf("WIZCHIP Initialized fail.\r\n");
xeon011 0:496d676b6364 66 while(1);
xeon011 0:496d676b6364 67 }
xeon011 0:496d676b6364 68 do
xeon011 0:496d676b6364 69 {
xeon011 0:496d676b6364 70 if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1)
xeon011 0:496d676b6364 71 pc.printf("Unknown PHY Link stauts.\r\n");
xeon011 0:496d676b6364 72 }while(tmp == PHY_LINK_OFF);
xeon011 0:496d676b6364 73 /* wizchip netconf */
xeon011 0:496d676b6364 74 #if _WIZCHIP_ == 5200
xeon011 0:496d676b6364 75 gWIZNETINFO.mac[0] = 0x00;
xeon011 0:496d676b6364 76 gWIZNETINFO.mac[1] = 0x08;
xeon011 0:496d676b6364 77 gWIZNETINFO.mac[2] = 0xDC;
xeon011 0:496d676b6364 78 gWIZNETINFO.mac[3] = 0x00;
xeon011 0:496d676b6364 79 gWIZNETINFO.mac[4] = 0xAB;
xeon011 0:496d676b6364 80 gWIZNETINFO.mac[5] = 0xCD;
xeon011 0:496d676b6364 81 gWIZNETINFO.ip[0] = 192;
xeon011 0:496d676b6364 82 gWIZNETINFO.ip[1] = 168;
xeon011 0:496d676b6364 83 gWIZNETINFO.ip[2] = 1;
xeon011 0:496d676b6364 84 gWIZNETINFO.ip[3] = 119;
xeon011 0:496d676b6364 85 gWIZNETINFO.gw[0] = 192;
xeon011 0:496d676b6364 86 gWIZNETINFO.gw[1] = 168;
xeon011 0:496d676b6364 87 gWIZNETINFO.gw[2] = 1;
xeon011 0:496d676b6364 88 gWIZNETINFO.gw[3] = 1;
xeon011 0:496d676b6364 89 gWIZNETINFO.sn[0] = 255;
xeon011 0:496d676b6364 90 gWIZNETINFO.sn[1] = 255;
xeon011 0:496d676b6364 91 gWIZNETINFO.sn[2] = 255;
xeon011 0:496d676b6364 92 gWIZNETINFO.sn[3] = 0;
xeon011 0:496d676b6364 93 gWIZNETINFO.dns[0] = 0;
xeon011 0:496d676b6364 94 gWIZNETINFO.dns[1] = 0;
xeon011 0:496d676b6364 95 gWIZNETINFO.dns[2] = 0;
xeon011 0:496d676b6364 96 gWIZNETINFO.dns[3] = 0;
xeon011 0:496d676b6364 97 gWIZNETINFO.dhcp = NETINFO_STATIC;
xeon011 0:496d676b6364 98 #else
xeon011 0:496d676b6364 99 ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
xeon011 0:496d676b6364 100 #endif
xeon011 0:496d676b6364 101 gWIZNETINFO.ip[0] = 192;
xeon011 0:496d676b6364 102 gWIZNETINFO.ip[1] = 168;
xeon011 0:496d676b6364 103 gWIZNETINFO.ip[2] = 1;
xeon011 0:496d676b6364 104 gWIZNETINFO.ip[3] = 119;
xeon011 0:496d676b6364 105 ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
xeon011 0:496d676b6364 106 ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
xeon011 0:496d676b6364 107 //DISPLAY NETWORK
xeon011 0:496d676b6364 108 ctlwizchip(CW_GET_ID,(void*)tmpstr);
xeon011 0:496d676b6364 109 pc.printf("=== %s NET CONF ===\r\n",(char*)tmpstr);
xeon011 0:496d676b6364 110 pc.printf("MAC:%02X.%02X.%02X.%02X.%02X.%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2],
xeon011 0:496d676b6364 111 gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]);
xeon011 0:496d676b6364 112 pc.printf("GAR:%d.%d.%d.%d\r\n", gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);
xeon011 0:496d676b6364 113 pc.printf("SUB:%d.%d.%d.%d\r\n", gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
xeon011 0:496d676b6364 114 pc.printf("SIP:%d.%d.%d.%d\r\n", gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
xeon011 0:496d676b6364 115
xeon011 0:496d676b6364 116 while(1)
xeon011 0:496d676b6364 117 {
xeon011 0:496d676b6364 118 uint8_t sn = 2;
xeon011 0:496d676b6364 119 if((ret = socket(sn,Sn_MR_TCP,3000,SF_TCP_NODELAY)) != sn)
xeon011 0:496d676b6364 120 {
xeon011 0:496d676b6364 121 printf("%d:socket() error:%ld\r\n", sn,ret);
xeon011 0:496d676b6364 122 close(sn);
xeon011 0:496d676b6364 123 continue;
xeon011 0:496d676b6364 124 }
xeon011 0:496d676b6364 125 printf("%d:socket() ok.\r\n",sn);
xeon011 0:496d676b6364 126 if((ret = listen(sn)) != SOCK_OK)
xeon011 0:496d676b6364 127 {
xeon011 0:496d676b6364 128 printf("%d:listen() error:%ld",sn,ret);
xeon011 0:496d676b6364 129 close(sn);
xeon011 0:496d676b6364 130 return ret;
xeon011 0:496d676b6364 131 }
xeon011 0:496d676b6364 132 printf("%d:listen() ok.\r\n",sn);
xeon011 0:496d676b6364 133 if((ret = socket(sn+1, Sn_MR_UDP, 3000, 0)) != sn+1)
xeon011 0:496d676b6364 134 {
xeon011 0:496d676b6364 135 printf("%d:socket() error:%ld\r\n", sn+1,ret);
xeon011 0:496d676b6364 136 continue;
xeon011 0:496d676b6364 137 }
xeon011 0:496d676b6364 138 do
xeon011 0:496d676b6364 139 {
xeon011 0:496d676b6364 140 getsockopt(sn,SO_STATUS, &tmp);
xeon011 0:496d676b6364 141 }while(tmp != SOCK_CLOSED && tmp != SOCK_ESTABLISHED);
xeon011 0:496d676b6364 142 if(tmp == SOCK_CLOSED) continue;
xeon011 0:496d676b6364 143 //Accept for client
xeon011 0:496d676b6364 144 printf("%d:connected\r\nLBStarted Blocking mode\r\n",sn);
xeon011 0:496d676b6364 145 while(1)
xeon011 0:496d676b6364 146 {
xeon011 0:496d676b6364 147 if((ret = loopback_tcps(sn, gDATABUF, DATA_BUF_SIZE)) < 0)
xeon011 0:496d676b6364 148 {
xeon011 0:496d676b6364 149 printf("%d:loopback_tcps error:%ld\r\n",sn,ret);
xeon011 0:496d676b6364 150 break;
xeon011 0:496d676b6364 151 }
xeon011 0:496d676b6364 152 /*
xeon011 0:496d676b6364 153 if((ret=loopback_udps(sn+1,gDATABUF,10)) < 0)
xeon011 0:496d676b6364 154 {
xeon011 0:496d676b6364 155 printf("%d:loopback_udps error:%ld\r\n",sn+1,ret);
xeon011 0:496d676b6364 156 break;
xeon011 0:496d676b6364 157 }
xeon011 0:496d676b6364 158 */
xeon011 0:496d676b6364 159 }
xeon011 0:496d676b6364 160 }
xeon011 0:496d676b6364 161 }
xeon011 0:496d676b6364 162
xeon011 0:496d676b6364 163 void wizchip_select(void)
xeon011 0:496d676b6364 164 {
xeon011 0:496d676b6364 165 cs = 0;
xeon011 0:496d676b6364 166 }
xeon011 0:496d676b6364 167
xeon011 0:496d676b6364 168 void wizchip_deselect(void)
xeon011 0:496d676b6364 169 {
xeon011 0:496d676b6364 170 cs = 1;
xeon011 0:496d676b6364 171 }
xeon011 0:496d676b6364 172
xeon011 0:496d676b6364 173 uint8_t wizchip_read()
xeon011 0:496d676b6364 174 {
xeon011 0:496d676b6364 175 return spi.write(0x00);
xeon011 0:496d676b6364 176 }
xeon011 0:496d676b6364 177
xeon011 0:496d676b6364 178 void wizchip_write(uint8_t wb)
xeon011 0:496d676b6364 179 {
xeon011 0:496d676b6364 180 spi.write(wb);
xeon011 0:496d676b6364 181 }
xeon011 0:496d676b6364 182
xeon011 0:496d676b6364 183 int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t size)
xeon011 0:496d676b6364 184 {
xeon011 0:496d676b6364 185 int32_t ret = 0;
xeon011 0:496d676b6364 186 ret = recv(sn,buf,size);
xeon011 0:496d676b6364 187 if(ret != size)
xeon011 0:496d676b6364 188 {
xeon011 0:496d676b6364 189 if(ret < 0)
xeon011 0:496d676b6364 190 {
xeon011 0:496d676b6364 191 printf("%d:recv() error:%ld\r\n",sn,ret);
xeon011 0:496d676b6364 192 close(sn);
xeon011 0:496d676b6364 193 return ret;
xeon011 0:496d676b6364 194 }
xeon011 0:496d676b6364 195 }
xeon011 0:496d676b6364 196 size = ret;
xeon011 0:496d676b6364 197 ret = send(sn,buf,size);
xeon011 0:496d676b6364 198 if(ret != size)
xeon011 0:496d676b6364 199 {
xeon011 0:496d676b6364 200 if(ret < 0)
xeon011 0:496d676b6364 201 {
xeon011 0:496d676b6364 202 printf("%d:send() error:%ld\r\n",sn,ret);
xeon011 0:496d676b6364 203 close(sn);
xeon011 0:496d676b6364 204 }
xeon011 0:496d676b6364 205 }
xeon011 0:496d676b6364 206 return ret;
xeon011 0:496d676b6364 207 }
xeon011 0:496d676b6364 208
xeon011 0:496d676b6364 209 int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t size)
xeon011 0:496d676b6364 210 {
xeon011 0:496d676b6364 211 int32_t ret = 0;
xeon011 0:496d676b6364 212 static uint8_t addr[4] = {0,};
xeon011 0:496d676b6364 213 static uint16_t port = 0;
xeon011 0:496d676b6364 214 uint8_t packinfo;
xeon011 0:496d676b6364 215
xeon011 0:496d676b6364 216 if((ret = recvfrom(sn,buf,size, addr,&port,&packinfo)) < 0)
xeon011 0:496d676b6364 217 {
xeon011 0:496d676b6364 218 printf("%d:recvfrom error:%ld\r\n",sn,ret);
xeon011 0:496d676b6364 219 return ret;
xeon011 0:496d676b6364 220 }
xeon011 0:496d676b6364 221 if(packinfo & 0x80)
xeon011 0:496d676b6364 222 {
xeon011 0:496d676b6364 223 printf("%d:recvfrom %d.%d.%d.%d(%d), size=%ld.\r\n",sn,addr[0],addr[1],addr[2],addr[3],port, ret);
xeon011 0:496d676b6364 224 }
xeon011 0:496d676b6364 225 if(packinfo & 0x01)
xeon011 0:496d676b6364 226 {
xeon011 0:496d676b6364 227 printf("%d:recvfrom remained packet.\r\n",sn);
xeon011 0:496d676b6364 228 }
xeon011 0:496d676b6364 229 else
xeon011 0:496d676b6364 230 {
xeon011 0:496d676b6364 231 printf("%d:recvfrom completed.\r\n",sn);
xeon011 0:496d676b6364 232 }
xeon011 0:496d676b6364 233 if( (ret = sendto(sn, buf, ret, addr, port)) < 0)
xeon011 0:496d676b6364 234 {
xeon011 0:496d676b6364 235 printf("%d:sendto error:%ld\r\n",sn,ret);
xeon011 0:496d676b6364 236 return ret;
xeon011 0:496d676b6364 237 }
xeon011 0:496d676b6364 238 printf("%d:sendto %d.%d.%d.%d(%d), size=%ld\r\n",sn,addr[0],addr[1],addr[2],addr[3],port, ret);
xeon011 0:496d676b6364 239 return ret;
xeon011 0:496d676b6364 240 }