This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500, W5200 and W5100. One of them can be selected by enabling it in wiznet.h.

Dependencies:   mbed

Fork of WIZnet_Library by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 *
00003 *  Test program for W5500 mbed Library
00004 
00005 */
00006 #include "mbed.h"
00007 #include "WIZnetInterface.h"
00008 
00009 #define ECHO_SERVER_PORT   5000
00010 const char* ECHO_SERVER_ADDRESS = "192.168.1.229";  // Server IP address
00011 
00012 /** 
00013 * Setting DHCP or static
00014 */
00015 //#define USE_DHCP
00016 
00017 /** 
00018 * Setting the platform to test
00019 */
00020 #define LPC
00021 //#define ST_NUCLEO
00022 //#define FRDM_KL25Z
00023 //#define Seeeduino_Arch
00024 
00025 #ifdef LPC
00026 // LPC1768 & LPC11U24
00027 SPI spi(p5, p6, p7); // mosi, miso, sclk
00028 WIZnetInterface eth(&spi, p8, p9); // spi, cs, reset
00029 #endif
00030 
00031 #ifdef ST_NUCLEO
00032 // ST Nucleo
00033 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
00034 WIZnetInterface eth(&spi, PB_6, PA_10); // spi, cs, reset
00035 #endif
00036 
00037 #ifdef FRDM_KL25Z
00038 // Freescale FRDM KL25Z
00039 SPI spi(PTD2, PTD3, PTD1); // mosi, miso, sclk
00040 WIZnetInterface eth(&spi, PTD0, PTA20); // spi, cs, reset
00041 #endif
00042 
00043 #ifdef Seeeduino_Arch
00044 // Seeedstudio Arch
00045 SPI spi(P1_22, P1_21, P1_20); // mosi, miso, sclk
00046 WIZnetInterface eth(&spi, P0_2, P0_0); // spi, cs, reset
00047     Serial pc(P1_13, P1_14); // tx, rx
00048 #else
00049     Serial pc(USBTX,USBRX);
00050 #endif
00051 
00052 #ifndef USE_DHCP
00053 // for static IP setting
00054 const char * IP_Addr    = "192.168.1.120";
00055 const char * IP_Subnet  = "255.255.255.0";
00056 const char * IP_Gateway = "192.168.1.111";
00057 #endif
00058 
00059 
00060 int main()
00061 {
00062     uint8_t mac[6];
00063     
00064     mbed_mac_address((char *)mac);     // using the MAC address in LPC11U24 or LPC1178
00065 //    mac[0] = 0x00; mac[1] = 0x08; mac[2] = 0xDC; mac[3] = 0x00; mac[4] = 0x00; mac[5] = 0x00; 
00066 // you can alo use WIZ550io's MAC address by enabling "USE_WIZ550IO_MAC" in wiznet.h
00067     
00068     pc.printf("Start\n");
00069     #ifdef USE_DHCP
00070       int ret = eth.init(mac); //Use DHCP
00071     #else
00072       int ret = eth.init(mac, IP_Addr, IP_Subnet, IP_Gateway); // static
00073     #endif
00074 
00075     if (!ret) {
00076         pc.printf("Initialized, MAC: %s\n", eth.getMACAddress());
00077     } else {
00078         pc.printf("Error eth.init() - ret = %d\n", ret);
00079         return -1;
00080     }
00081 
00082     ret = eth.connect();
00083     if (!ret) {
00084         pc.printf("IP: %s, MASK: %s, GW: %s\n",
00085                   eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
00086     } else {
00087         pc.printf("Error eth.connect() - ret = %d\n", ret);
00088         return -1;
00089     }
00090 
00091     TCPSocketServer server;
00092     server.bind(ECHO_SERVER_PORT);
00093     server.listen();
00094 
00095     while (true) {
00096         pc.printf("\nWait for new connection...\n");
00097         TCPSocketConnection client;
00098         server.accept(client);
00099         //client.set_blocking(false, 1500); // Timeout after (1.5)s
00100 
00101         pc.printf("Connection from: %s\n", client.get_address());
00102         char buffer[256];
00103         while (true) {
00104             int n = client.receive(buffer, sizeof(buffer));
00105             if (n <= 0) break;
00106 
00107             client.send_all(buffer, n);
00108             if (n <= 0) break;
00109         }
00110 
00111         client.close();
00112     }
00113 }