Webserver only w/o any other functions, single thread. Running on STM32F013+W5500

Dependencies:   NTPClient W5500Interface Watchdog device_configuration eeprom_flash mbed-rpc-nucleo mbed-rtos mbed

Fork of F103-Serial-to-Ethernet by Chau Vo

Committer:
olympux
Date:
Sat Sep 20 20:42:49 2014 +0000
Revision:
0:c2eac797face
Child:
2:18f10e7209f4
Initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
olympux 0:c2eac797face 1 /*
olympux 0:c2eac797face 2 *
olympux 0:c2eac797face 3 * Test program for W5500 mbed Library
olympux 0:c2eac797face 4
olympux 0:c2eac797face 5 */
olympux 0:c2eac797face 6 #include "mbed.h"
olympux 0:c2eac797face 7 #include "EthernetInterface.h"
olympux 0:c2eac797face 8 #include "rtos.h"
olympux 0:c2eac797face 9
olympux 0:c2eac797face 10 #define ECHO_SERVER_PORT 5000
olympux 0:c2eac797face 11
olympux 0:c2eac797face 12 /**
olympux 0:c2eac797face 13 * Setting DHCP or static
olympux 0:c2eac797face 14 */
olympux 0:c2eac797face 15 //#define USE_DHCP
olympux 0:c2eac797face 16
olympux 0:c2eac797face 17 /**
olympux 0:c2eac797face 18 * Setting the platform to test
olympux 0:c2eac797face 19 */
olympux 0:c2eac797face 20 //#define LPC
olympux 0:c2eac797face 21 #define ST_NUCLEO
olympux 0:c2eac797face 22 //#define FRDM_KL25Z
olympux 0:c2eac797face 23 //#define Seeeduino_Arch
olympux 0:c2eac797face 24
olympux 0:c2eac797face 25 #ifdef LPC
olympux 0:c2eac797face 26 // LPC1768 & LPC11U24
olympux 0:c2eac797face 27 SPI spi(p5, p6, p7); // mosi, miso, sclk
olympux 0:c2eac797face 28 WIZnetInterface eth(&spi, p8, p9); // spi, cs, reset
olympux 0:c2eac797face 29 #endif
olympux 0:c2eac797face 30
olympux 0:c2eac797face 31 #ifdef ST_NUCLEO
olympux 0:c2eac797face 32 // ST Nucleo
olympux 0:c2eac797face 33 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
olympux 0:c2eac797face 34 EthernetInterface eth(&spi, PC_8, PC_9); // spi, cs, reset
olympux 0:c2eac797face 35 #endif
olympux 0:c2eac797face 36
olympux 0:c2eac797face 37 #ifdef FRDM_KL25Z
olympux 0:c2eac797face 38 // Freescale FRDM KL25Z
olympux 0:c2eac797face 39 SPI spi(PTD2, PTD3, PTD1); // mosi, miso, sclk
olympux 0:c2eac797face 40 WIZnetInterface eth(&spi, PTD0, PTA20); // spi, cs, reset
olympux 0:c2eac797face 41 #endif
olympux 0:c2eac797face 42
olympux 0:c2eac797face 43 #ifdef Seeeduino_Arch
olympux 0:c2eac797face 44 // Seeedstudio Arch
olympux 0:c2eac797face 45 SPI spi(P1_22, P1_21, P1_20); // mosi, miso, sclk
olympux 0:c2eac797face 46 WIZnetInterface eth(&spi, P0_2, P0_0); // spi, cs, reset
olympux 0:c2eac797face 47 Serial pc(P1_13, P1_14); // tx, rx
olympux 0:c2eac797face 48 #else
olympux 0:c2eac797face 49 Serial pc(USBTX,USBRX);
olympux 0:c2eac797face 50 #endif
olympux 0:c2eac797face 51
olympux 0:c2eac797face 52 #ifndef USE_DHCP
olympux 0:c2eac797face 53 // for static IP setting
olympux 0:c2eac797face 54 const char * IP_Addr = "192.168.0.120";
olympux 0:c2eac797face 55 const char * IP_Subnet = "255.255.255.0";
olympux 0:c2eac797face 56 const char * IP_Gateway = "192.168.0.1";
olympux 0:c2eac797face 57 #endif
olympux 0:c2eac797face 58
olympux 0:c2eac797face 59
olympux 0:c2eac797face 60 void uart_thread(void const *args) {
olympux 0:c2eac797face 61 int i = 0;
olympux 0:c2eac797face 62
olympux 0:c2eac797face 63 while (true) {
olympux 0:c2eac797face 64 printf("%d", i++);
olympux 0:c2eac797face 65 Thread::wait(2000);
olympux 0:c2eac797face 66 }
olympux 0:c2eac797face 67 }
olympux 0:c2eac797face 68
olympux 0:c2eac797face 69
olympux 0:c2eac797face 70 int main()
olympux 0:c2eac797face 71 {
olympux 0:c2eac797face 72 uint8_t mac[6];
olympux 0:c2eac797face 73
olympux 0:c2eac797face 74 Thread thread(uart_thread);
olympux 0:c2eac797face 75
olympux 0:c2eac797face 76 mbed_mac_address((char *)mac); // using the MAC address in LPC11U24 or LPC1178
olympux 0:c2eac797face 77 mac[0] = 0x00; mac[1] = 0x08; mac[2] = 0xDC; mac[3] = 0x00; mac[4] = 0x00; mac[5] = 0x00;
olympux 0:c2eac797face 78 // you can alo use WIZ550io's MAC address by enabling "USE_WIZ550IO_MAC" in wiznet.h
olympux 0:c2eac797face 79
olympux 0:c2eac797face 80 pc.printf("Start\n");
olympux 0:c2eac797face 81 #ifdef USE_DHCP
olympux 0:c2eac797face 82 int ret = eth.init(mac); //Use DHCP
olympux 0:c2eac797face 83 #else
olympux 0:c2eac797face 84 int ret = eth.init(mac, IP_Addr, IP_Subnet, IP_Gateway); // static
olympux 0:c2eac797face 85 #endif
olympux 0:c2eac797face 86
olympux 0:c2eac797face 87 if (!ret) {
olympux 0:c2eac797face 88 pc.printf("Initialized, MAC: %s\n", eth.getMACAddress());
olympux 0:c2eac797face 89 } else {
olympux 0:c2eac797face 90 pc.printf("Error eth.init() - ret = %d\n", ret);
olympux 0:c2eac797face 91 return -1;
olympux 0:c2eac797face 92 }
olympux 0:c2eac797face 93
olympux 0:c2eac797face 94 ret = eth.connect();
olympux 0:c2eac797face 95 if (!ret) {
olympux 0:c2eac797face 96 pc.printf("IP: %s, MASK: %s, GW: %s\n",
olympux 0:c2eac797face 97 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
olympux 0:c2eac797face 98 } else {
olympux 0:c2eac797face 99 pc.printf("Error eth.connect() - ret = %d\n", ret);
olympux 0:c2eac797face 100 return -1;
olympux 0:c2eac797face 101 }
olympux 0:c2eac797face 102
olympux 0:c2eac797face 103 TCPSocketServer server;
olympux 0:c2eac797face 104 server.bind(ECHO_SERVER_PORT);
olympux 0:c2eac797face 105 server.listen();
olympux 0:c2eac797face 106
olympux 0:c2eac797face 107 while (true) {
olympux 0:c2eac797face 108 pc.printf("\nWait for new connection...\n");
olympux 0:c2eac797face 109 TCPSocketConnection client;
olympux 0:c2eac797face 110 server.accept(client);
olympux 0:c2eac797face 111 client.set_blocking(false, 10000); // Timeout after (10)s
olympux 0:c2eac797face 112
olympux 0:c2eac797face 113 pc.printf("Connection from: %s\n", client.get_address());
olympux 0:c2eac797face 114 char buffer[256];
olympux 0:c2eac797face 115 while (true) {
olympux 0:c2eac797face 116 int n = client.receive(buffer, sizeof(buffer));
olympux 0:c2eac797face 117 if (n <= 0) break;
olympux 0:c2eac797face 118
olympux 0:c2eac797face 119 client.send_all(buffer, n);
olympux 0:c2eac797face 120 if (n <= 0) break;
olympux 0:c2eac797face 121 }
olympux 0:c2eac797face 122
olympux 0:c2eac797face 123 client.close();
olympux 0:c2eac797face 124 }
olympux 0:c2eac797face 125 }