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

Files at this revision

API Documentation at this revision

Comitter:
olympux
Date:
Sun Sep 21 16:22:21 2014 +0000
Parent:
3:972ed747474c
Child:
5:a01dc14ec038
Commit message:
Adding EEPROM emulation

Changed in this revision

eeprom.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eeprom.lib	Sun Sep 21 16:22:21 2014 +0000
@@ -0,0 +1,1 @@
+eeprom#0b8e4689a075
--- a/main.cpp	Sun Sep 21 08:15:57 2014 +0000
+++ b/main.cpp	Sun Sep 21 16:22:21 2014 +0000
@@ -4,6 +4,7 @@
 
 */
 #include "mbed.h"
+#include "eeprom.h"
 #include "EthernetInterface.h"
 #include "rtos.h"
 
@@ -21,6 +22,10 @@
 
 Serial uart(USBTX,USBRX);
 
+/* Virtual address defined by the user: 0xFFFF value is prohibited */
+uint16_t VirtAddVarTab[NumbOfVar] = {0x5555, 0x6666, 0x7777};
+
+
 
 /*
 * Network configuration
@@ -29,19 +34,23 @@
 #define UDP_LOCAL_PORT    11000
 //#define USE_DHCP // DHCP or static
 
+#define TCP_SERVER
+//#define TCP_CLIENT
+#define UDP_SERVER
+//#define UDP_CLIENT
+//#define NTP
+
+#define TCP_SERVER_WAIT_CLIENT_TIMEOUT     200
+#define TCP_SERVER_RECEIVE_TIMEOUT         3000
+#define UDP_SERVER_RECEIVE_TIMEOUT         200
+
 #ifndef USE_DHCP
 // for static IP setting
-const char * IP_Addr    = "192.168.0.120";
+char * IP_Addr    = "192.168.0.120";
 const char * IP_Subnet  = "255.255.255.0";
 const char * IP_Gateway = "192.168.0.1";
 #endif
 
-#define TCP_SERVER
-//#define TCP_CLIENT
-//#define UDP_SERVER
-//#define UDP_CLIENT
-//#define NTP
-
 char buffer[256]; // socket buffer
 
 
@@ -77,32 +86,13 @@
 }
 
 
-int main()
-{
-    message_t message;
-    
-    /*
-    * configure
-    */
-    uart.baud(115200);
-    
-    
-    /*
-    * UI threads
-    */
-    Thread t1(uart_thread);
-    
-    
-    
-    
-    /*
-    * Ethernet
-    */
+int ethernet_init(void) {
     uint8_t mac[6];
         
     mbed_mac_address((char *)mac);     // using the MAC address in LPC11U24 or LPC1178
     mac[0] = 0x00; mac[1] = 0x08; mac[2] = 0xDC; mac[3] = 0x00; mac[4] = 0x00; mac[5] = 0x00; 
     
+    
     printf("Start\n");
     #ifdef USE_DHCP
       int ret = eth.init(mac); //Use DHCP
@@ -125,6 +115,43 @@
         return -1;
     }
     
+    return 0;
+}
+
+
+
+int main()
+{
+    message_t message;
+    
+    /*
+    * configure
+    */
+    uart.baud(115200);
+    
+    /*
+    * FLASH
+    */
+    // Unlock the Flash Program Erase controller */
+    FLASH_Unlock();
+    // EEPROM Init
+    EE_Init();
+  
+    /*
+    * UI threads
+    */
+    Thread t1(uart_thread);
+    
+        
+    /*
+    * Ethernet
+    */
+    int ret = ethernet_init();
+    if (ret) {
+        printf("App halted\n");
+        while (true) {};
+    }
+    
         
 #ifdef TCP_SERVER
     TCPSocketServer tcp_server;
@@ -132,6 +159,8 @@
     
     tcp_server.bind(TCP_SERVER_PORT);
     tcp_server.listen();
+    uart.printf("\nWait for new connection...\n");
+    tcp_server.set_blocking(false, TCP_SERVER_WAIT_CLIENT_TIMEOUT);
 #endif
     
 #ifdef UDP_SERVER
@@ -139,57 +168,53 @@
     Endpoint ep_udp_client;
     
     ret = udp_server.bind(UDP_LOCAL_PORT);
-    //printf("sock.bind = %d\n", ret);
-#endif
-
-#ifdef TCP_CLIENT
-TCPSocketConnection tcp_socket;
-#endif
-
-#ifdef UDP_CLIENT
-    UDPSocket udp_socket;
-    Endpoint ep_udp_server;
+    printf("sock.bind = %d\n", ret);
+    udp_server.set_blocking(false, UDP_SERVER_RECEIVE_TIMEOUT);
 #endif
 
 
-/*
-* TCP server
-*/
-#ifdef TCP_SERVER
+    // Network processor
     while (true) {
-        //uart.printf("\nWait for new connection...\n");
-        tcp_server.accept(tcp_client);
-        tcp_client.set_blocking(false, 10000); // Timeout after (10)s
-
-        //uart.printf("Connection from: %s\n", client.get_address());
-        while (true) {
-            int n = tcp_client.receive(buffer, sizeof(buffer));
-            if (n <= 0) break;
+#ifdef TCP_SERVER
+        // no tcp client connected
+        if (!tcp_client.is_connected())
+        {
+            // wait for client within timeout
+            ret = tcp_server.accept(tcp_client);
             
-            // send to uart
-            buffer[n] = '\0';
-            message.len = n;
-            message.msg = buffer;
-            uart_queue.put(&message);
-            
-            // echo to tcp client
-            tcp_client.send_all(buffer, n);
-            if (n <= 0) break;
-        }
-
-        tcp_client.close();
-    }
+            // tcp client connected
+            if (ret > -1) {
+                uart.printf("Connection from: %s\n", tcp_client.get_address());
+                
+                // loop waiting and receiving data within timeout
+                tcp_client.set_blocking(false, TCP_SERVER_RECEIVE_TIMEOUT); // Timeout after x seconds
+                while (true) {
+                    int n = tcp_client.receive(buffer, sizeof(buffer));
+                    if (n <= 0) break;
+                    
+                    // got data, process it
+                    // send to uart
+                    buffer[n] = '\0';
+                    message.len = n;
+                    message.msg = buffer;
+                    uart_queue.put(&message);
+                    
+                    // echo to tcp client
+                    tcp_client.send_all(buffer, n);
+                    if (n <= 0) break;
+                } // end loop if no data received within timeout
+                tcp_client.close();
+            } // if client connected
+        } // if no client connected
 #endif
     
-/*
-* UDP server
-*/
+
 #ifdef UDP_SERVER
-    while (true) {
-        //printf("\nWait for packet...\n");
+        // wait for udp packet within timeout
         int n = udp_server.receiveFrom(ep_udp_client, buffer, sizeof(buffer));
         if (n < 0) continue;
 
+        // got some data, process it
         // send to uart
         buffer[n] = '\0';
         message.len = n;
@@ -199,94 +224,12 @@
         // echo
         //printf("Received packet from: %s\n", client.get_address());
         udp_server.sendTo(ep_udp_client, buffer, n);
-    }
-#endif  
-    
-    
-/*
-* TCP client
-*/
-#ifdef TCP_CLIENT
-    while (tcp_socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
-        printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
-        wait(1);
-    }
- 
-    char hello[] = "Hello World\n";
-    tcp_socket.send_all(hello, sizeof(hello) - 1);
- 
-    char buf[256];
-    int n = tcp_socket.receive(buf, 256);
-    buf[n] = '\0';
-    printf("%s", buf);
- 
-    tcp_socket.close();
-    eth.disconnect();
- 
-    while(true) {}
+        
+        // Command = set new IP
+        if (strstr(buffer, "SetIP:")) {
+            IP_Addr = &buffer[6];
+            printf("New IP=%s\n", IP_Addr);
+        }
 #endif
-
-
-/*
-* UDP client
-*/
-#ifdef UDP_CLIENT
-    ret = udp_socket.init();
-    udp_socket.bind(0);
-    printf("sock.bind = %d\n", ret);
-    if (ret == -1) printf("Socket creation Fail\n");
- 
-    ep_udp_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
- 
-    printf("\nSend UDP data\n");
- 
-    char out_buffer[] = "Hello World\n";
-    ret = udp_socket.sendTo(ep_udp_server, out_buffer, sizeof(out_buffer));
-    if (ret < 0) printf("UDP Send Error\n");
-    else printf("UDP Send: %d\n", ret);
- 
-    char in_buffer[256];
-    int n = udp_socket.receiveFrom(ep_udp_server, in_buffer, sizeof(in_buffer));
- 
-    in_buffer[n] = '\0';
-    printf("%s\n", in_buffer);
- 
-    udp_socket.close();
- 
-    eth.disconnect();
-    while(1) {}
-#endif
-  
-  
-/*
-* NTP
-*/
-#ifdef NTP
-    time_t ctTime;
-    ctTime = time(NULL);
-    printf("1. Current Time is: %s\r\n", ctime(&ctTime));
- 
-    printf("Trying to update time...\r\n");
-    if (ntp.setTime("0.pool.ntp.org") == 0) {
-        ctTime = time(NULL);
-        printf("2. Current Time is: %s\r\n", ctime(&ctTime));
- 
-        // resetting GMT+9
-        set_time( time(NULL) + 32400 ); // 9x60x60
-        //
-        printf("Set time successfully\r\n");
-        //time_t ctTime;
-        ctTime = time(NULL);
-        printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
-    } else {
-        printf("Error\r\n");
-    }
- 
-    eth.disconnect();
- 
-    while(1) {
-    }
-#endif
-
-    while(true) {}
-}
+    } // network processor
+}
\ No newline at end of file