Basic example using W5500 Ported to LPC800_Max (LPC812)
Fork of W5500_Porting_WIZnetLib by
main.cpp@2:12863af451af, 2014-03-31 (annotated)
- Committer:
- dennyem
- Date:
- Mon Mar 31 04:01:59 2014 +0000
- Revision:
- 2:12863af451af
- Parent:
- 0:496d676b6364
New from W5500_Porting_WIZnetlib
Who changed what in which revision?
User | Revision | Line number | New 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 | |
dennyem | 2:12863af451af | 5 | SPI spi(P0_14, P0_15, P0_12); // mosi, miso, sclk |
dennyem | 2:12863af451af | 6 | DigitalOut cs(P0_13), nRESET(P0_16); // reset pin of Wiz550io |
dennyem | 2:12863af451af | 7 | DigitalIn RDY(P0_17); // ready pin of Wiz550io |
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 |
dennyem | 2:12863af451af | 32 | spi.frequency(1000000); // 1MHz. Do not use more than 1Mhz on the LPC800, errors result |
xeon011 | 0:496d676b6364 | 33 | |
xeon011 | 0:496d676b6364 | 34 | nRESET = 0; |
xeon011 | 0:496d676b6364 | 35 | wait(0.001); // 1ms |
xeon011 | 0:496d676b6364 | 36 | nRESET = 1; |
xeon011 | 0:496d676b6364 | 37 | |
xeon011 | 0:496d676b6364 | 38 | while(RDY!=1); |
dennyem | 2:12863af451af | 39 | |
xeon011 | 0:496d676b6364 | 40 | cs = 1; |
xeon011 | 0:496d676b6364 | 41 | |
xeon011 | 0:496d676b6364 | 42 | #if _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_ |
xeon011 | 0:496d676b6364 | 43 | reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); |
xeon011 | 0:496d676b6364 | 44 | #elif _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_ |
xeon011 | 0:496d676b6364 | 45 | reg_wizchip_cs_cbfunc(wizchip_select, wizchip_select); // CS must be tried with LOW. |
xeon011 | 0:496d676b6364 | 46 | #else |
xeon011 | 0:496d676b6364 | 47 | #if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_ |
xeon011 | 0:496d676b6364 | 48 | #error "Unknown _WIZCHIP_IO_MODE_" |
xeon011 | 0:496d676b6364 | 49 | #else |
xeon011 | 0:496d676b6364 | 50 | reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); |
xeon011 | 0:496d676b6364 | 51 | #endif |
xeon011 | 0:496d676b6364 | 52 | #endif |
xeon011 | 0:496d676b6364 | 53 | //reg_wizchip_bus_cbfunc(uint8_t (*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, uint8_t wb)); |
xeon011 | 0:496d676b6364 | 54 | reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); |
xeon011 | 0:496d676b6364 | 55 | |
xeon011 | 0:496d676b6364 | 56 | /* wizchip initialize*/ |
xeon011 | 0:496d676b6364 | 57 | if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) { |
xeon011 | 0:496d676b6364 | 58 | pc.printf("WIZCHIP Initialized fail.\r\n"); |
xeon011 | 0:496d676b6364 | 59 | while(1); |
xeon011 | 0:496d676b6364 | 60 | } |
dennyem | 2:12863af451af | 61 | |
dennyem | 2:12863af451af | 62 | do { |
xeon011 | 0:496d676b6364 | 63 | if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) |
dennyem | 2:12863af451af | 64 | pc.printf("Unknown PHY Link status.\r\n"); |
dennyem | 2:12863af451af | 65 | } while(tmp == PHY_LINK_OFF); |
dennyem | 2:12863af451af | 66 | |
xeon011 | 0:496d676b6364 | 67 | /* wizchip netconf */ |
dennyem | 2:12863af451af | 68 | ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); |
dennyem | 2:12863af451af | 69 | |
xeon011 | 0:496d676b6364 | 70 | gWIZNETINFO.ip[0] = 192; |
xeon011 | 0:496d676b6364 | 71 | gWIZNETINFO.ip[1] = 168; |
dennyem | 2:12863af451af | 72 | gWIZNETINFO.ip[2] = 43; |
dennyem | 2:12863af451af | 73 | gWIZNETINFO.ip[3] = 116; |
xeon011 | 0:496d676b6364 | 74 | gWIZNETINFO.dhcp = NETINFO_STATIC; |
dennyem | 2:12863af451af | 75 | |
xeon011 | 0:496d676b6364 | 76 | ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO); |
xeon011 | 0:496d676b6364 | 77 | ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); |
dennyem | 2:12863af451af | 78 | |
xeon011 | 0:496d676b6364 | 79 | //DISPLAY NETWORK |
xeon011 | 0:496d676b6364 | 80 | ctlwizchip(CW_GET_ID,(void*)tmpstr); |
dennyem | 2:12863af451af | 81 | |
xeon011 | 0:496d676b6364 | 82 | pc.printf("=== %s NET CONF ===\r\n",(char*)tmpstr); |
xeon011 | 0:496d676b6364 | 83 | pc.printf("MAC:%02X.%02X.%02X.%02X.%02X.%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2], |
xeon011 | 0:496d676b6364 | 84 | gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]); |
xeon011 | 0:496d676b6364 | 85 | pc.printf("GAR:%d.%d.%d.%d\r\n", gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]); |
xeon011 | 0:496d676b6364 | 86 | pc.printf("SUB:%d.%d.%d.%d\r\n", gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]); |
xeon011 | 0:496d676b6364 | 87 | pc.printf("SIP:%d.%d.%d.%d\r\n", gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]); |
xeon011 | 0:496d676b6364 | 88 | |
xeon011 | 0:496d676b6364 | 89 | while(1) |
xeon011 | 0:496d676b6364 | 90 | { |
xeon011 | 0:496d676b6364 | 91 | uint8_t sn = 2; |
dennyem | 2:12863af451af | 92 | |
dennyem | 2:12863af451af | 93 | if((ret = socket(sn, Sn_MR_TCP, 3000, SF_TCP_NODELAY)) != sn) |
xeon011 | 0:496d676b6364 | 94 | { |
dennyem | 2:12863af451af | 95 | pc.printf("%d:socket() error: %ld\r\n", sn,ret); |
xeon011 | 0:496d676b6364 | 96 | close(sn); |
xeon011 | 0:496d676b6364 | 97 | continue; |
xeon011 | 0:496d676b6364 | 98 | } |
dennyem | 2:12863af451af | 99 | pc.printf("%d:socket() ok.\r\n", sn); |
dennyem | 2:12863af451af | 100 | |
dennyem | 2:12863af451af | 101 | if((ret = listen(sn)) != SOCK_OK) { |
dennyem | 2:12863af451af | 102 | pc.printf("%d:listen() error:%ld", sn, ret); |
xeon011 | 0:496d676b6364 | 103 | close(sn); |
xeon011 | 0:496d676b6364 | 104 | return ret; |
xeon011 | 0:496d676b6364 | 105 | } |
dennyem | 2:12863af451af | 106 | pc.printf("%d:listen() ok.\r\n", sn); |
dennyem | 2:12863af451af | 107 | |
dennyem | 2:12863af451af | 108 | if((ret = socket(sn + 1, Sn_MR_UDP, 3000, 0)) != sn + 1) { |
dennyem | 2:12863af451af | 109 | pc.printf("%d:socket() error:%ld\r\n", sn + 1, ret); |
xeon011 | 0:496d676b6364 | 110 | continue; |
dennyem | 2:12863af451af | 111 | } |
dennyem | 2:12863af451af | 112 | |
dennyem | 2:12863af451af | 113 | do { |
dennyem | 2:12863af451af | 114 | getsockopt(sn, SO_STATUS, &tmp); |
dennyem | 2:12863af451af | 115 | } while(tmp != SOCK_CLOSED && tmp != SOCK_ESTABLISHED); |
dennyem | 2:12863af451af | 116 | |
xeon011 | 0:496d676b6364 | 117 | if(tmp == SOCK_CLOSED) continue; |
dennyem | 2:12863af451af | 118 | |
xeon011 | 0:496d676b6364 | 119 | //Accept for client |
dennyem | 2:12863af451af | 120 | pc.printf("%d:connected\r\nLBStarted Blocking mode\r\n", sn); |
dennyem | 2:12863af451af | 121 | |
dennyem | 2:12863af451af | 122 | while(1) { |
dennyem | 2:12863af451af | 123 | if((ret = loopback_tcps(sn, gDATABUF, DATA_BUF_SIZE)) < 0) { |
dennyem | 2:12863af451af | 124 | pc.printf("%d:loopback_tcps error:%ld\r\n", sn, ret); |
xeon011 | 0:496d676b6364 | 125 | break; |
xeon011 | 0:496d676b6364 | 126 | } |
xeon011 | 0:496d676b6364 | 127 | /* |
dennyem | 2:12863af451af | 128 | if((ret=loopback_udps(sn+1,gDATABUF,10)) < 0) { |
dennyem | 2:12863af451af | 129 | pc.printf("%d:loopback_udps error:%ld\r\n", sn + 1, ret); |
xeon011 | 0:496d676b6364 | 130 | break; |
xeon011 | 0:496d676b6364 | 131 | } |
xeon011 | 0:496d676b6364 | 132 | */ |
xeon011 | 0:496d676b6364 | 133 | } |
xeon011 | 0:496d676b6364 | 134 | } |
xeon011 | 0:496d676b6364 | 135 | } |
xeon011 | 0:496d676b6364 | 136 | |
xeon011 | 0:496d676b6364 | 137 | void wizchip_select(void) |
xeon011 | 0:496d676b6364 | 138 | { |
xeon011 | 0:496d676b6364 | 139 | cs = 0; |
xeon011 | 0:496d676b6364 | 140 | } |
xeon011 | 0:496d676b6364 | 141 | |
xeon011 | 0:496d676b6364 | 142 | void wizchip_deselect(void) |
xeon011 | 0:496d676b6364 | 143 | { |
xeon011 | 0:496d676b6364 | 144 | cs = 1; |
xeon011 | 0:496d676b6364 | 145 | } |
xeon011 | 0:496d676b6364 | 146 | |
xeon011 | 0:496d676b6364 | 147 | uint8_t wizchip_read() |
xeon011 | 0:496d676b6364 | 148 | { |
xeon011 | 0:496d676b6364 | 149 | return spi.write(0x00); |
xeon011 | 0:496d676b6364 | 150 | } |
xeon011 | 0:496d676b6364 | 151 | |
xeon011 | 0:496d676b6364 | 152 | void wizchip_write(uint8_t wb) |
xeon011 | 0:496d676b6364 | 153 | { |
xeon011 | 0:496d676b6364 | 154 | spi.write(wb); |
xeon011 | 0:496d676b6364 | 155 | } |
xeon011 | 0:496d676b6364 | 156 | |
xeon011 | 0:496d676b6364 | 157 | int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t size) |
xeon011 | 0:496d676b6364 | 158 | { |
xeon011 | 0:496d676b6364 | 159 | int32_t ret = 0; |
dennyem | 2:12863af451af | 160 | |
dennyem | 2:12863af451af | 161 | ret = recv(sn, buf, size); |
dennyem | 2:12863af451af | 162 | |
dennyem | 2:12863af451af | 163 | if(ret != size) { |
xeon011 | 0:496d676b6364 | 164 | if(ret < 0) |
xeon011 | 0:496d676b6364 | 165 | { |
dennyem | 2:12863af451af | 166 | pc.printf("%d:recv() error:%ld\r\n", sn, ret); |
xeon011 | 0:496d676b6364 | 167 | close(sn); |
xeon011 | 0:496d676b6364 | 168 | return ret; |
xeon011 | 0:496d676b6364 | 169 | } |
xeon011 | 0:496d676b6364 | 170 | } |
dennyem | 2:12863af451af | 171 | |
xeon011 | 0:496d676b6364 | 172 | size = ret; |
dennyem | 2:12863af451af | 173 | ret = send(sn, buf, size); |
dennyem | 2:12863af451af | 174 | |
dennyem | 2:12863af451af | 175 | for(int x = 0; x < size; x++) |
dennyem | 2:12863af451af | 176 | pc.printf("%c", buf[x]); |
dennyem | 2:12863af451af | 177 | |
dennyem | 2:12863af451af | 178 | if(ret != size) { |
dennyem | 2:12863af451af | 179 | if(ret < 0) { |
dennyem | 2:12863af451af | 180 | pc.printf("%d:send() error:%ld\r\n", sn, ret); |
xeon011 | 0:496d676b6364 | 181 | close(sn); |
xeon011 | 0:496d676b6364 | 182 | } |
xeon011 | 0:496d676b6364 | 183 | } |
xeon011 | 0:496d676b6364 | 184 | return ret; |
xeon011 | 0:496d676b6364 | 185 | } |
xeon011 | 0:496d676b6364 | 186 | |
xeon011 | 0:496d676b6364 | 187 | int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t size) |
xeon011 | 0:496d676b6364 | 188 | { |
xeon011 | 0:496d676b6364 | 189 | int32_t ret = 0; |
xeon011 | 0:496d676b6364 | 190 | static uint8_t addr[4] = {0,}; |
xeon011 | 0:496d676b6364 | 191 | static uint16_t port = 0; |
xeon011 | 0:496d676b6364 | 192 | uint8_t packinfo; |
xeon011 | 0:496d676b6364 | 193 | |
xeon011 | 0:496d676b6364 | 194 | if((ret = recvfrom(sn,buf,size, addr,&port,&packinfo)) < 0) |
xeon011 | 0:496d676b6364 | 195 | { |
dennyem | 2:12863af451af | 196 | pc.printf("%d:recvfrom error:%ld\r\n", sn, ret); |
xeon011 | 0:496d676b6364 | 197 | return ret; |
xeon011 | 0:496d676b6364 | 198 | } |
xeon011 | 0:496d676b6364 | 199 | if(packinfo & 0x80) |
xeon011 | 0:496d676b6364 | 200 | { |
dennyem | 2:12863af451af | 201 | pc.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 | 202 | } |
xeon011 | 0:496d676b6364 | 203 | if(packinfo & 0x01) |
xeon011 | 0:496d676b6364 | 204 | { |
dennyem | 2:12863af451af | 205 | pc.printf("%d:recvfrom remained packet.\r\n", sn); |
xeon011 | 0:496d676b6364 | 206 | } |
xeon011 | 0:496d676b6364 | 207 | else |
xeon011 | 0:496d676b6364 | 208 | { |
dennyem | 2:12863af451af | 209 | pc.printf("%d:recvfrom completed.\r\n", sn); |
xeon011 | 0:496d676b6364 | 210 | } |
xeon011 | 0:496d676b6364 | 211 | if( (ret = sendto(sn, buf, ret, addr, port)) < 0) |
xeon011 | 0:496d676b6364 | 212 | { |
dennyem | 2:12863af451af | 213 | pc.printf("%d:sendto error:%ld\r\n", sn, ret); |
xeon011 | 0:496d676b6364 | 214 | return ret; |
xeon011 | 0:496d676b6364 | 215 | } |
dennyem | 2:12863af451af | 216 | pc.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 | 217 | return ret; |
xeon011 | 0:496d676b6364 | 218 | } |