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:
Tue Oct 21 20:39:15 2014 +0000
Parent:
17:88ef7a078095
Child:
19:05934ee9ee67
Commit message:
Added flags for tcp server and client but not in use.; Updated debug strings.

Changed in this revision

W5500Interface.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
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
my_eeprom_funcs.lib Show annotated file Show diff for this revision Revisions of this file
protocol.txt Show annotated file Show diff for this revision Revisions of this file
readme.txt Show annotated file Show diff for this revision Revisions of this file
--- a/W5500Interface.lib	Sat Oct 04 20:25:45 2014 +0000
+++ b/W5500Interface.lib	Tue Oct 21 20:39:15 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/EthernetInterfaceW5500-makers/code/W5500Interface/#da52bf0e0e5d
+http://mbed.org/teams/EthernetInterfaceW5500-makers/code/W5500Interface/#713b6d2aaefb
--- a/main.cpp	Sat Oct 04 20:25:45 2014 +0000
+++ b/main.cpp	Tue Oct 21 20:39:15 2014 +0000
@@ -93,7 +93,7 @@
                                      // this section is for selecting protocol
                                      0xA212, // 0xA5A5 = enable TCP server
                                      0xA313, // 0xA5A5 = eanble TCP client
-                                     0xA414  // 0xA5A5 = enable UDP client
+                                     0xA414  // 0xA5A5 = enable UDP server
                                      };
 
 /*
@@ -141,6 +141,7 @@
 uint8_t u8server_ip_addr[4]; // server ip address in 8-bits
 char str_server_ip_addr[16];// for printf, converted from 16-bits u16server_ip_addr
 uint16_t u16tcp_server_port; // directly loaded from eeprom
+uint16_t u16enable_tcp_client, u16enable_tcp_server;// flags for enabling TCP client or TCP server
 
 char tcp_client_buffer[256]; // socket buffer
 char udp_server_buffer[256];
@@ -235,13 +236,12 @@
 
 // Timer thread for auto update
 void auto_update_timer_thread(void const* args) {
-    bool update_flag;
+    bool update_flag = true;
     
     Thread::wait(500);
     while(true) {
-        update_flag = true;
         auto_update_queue.put(&update_flag);
-        Thread::wait(1000*transmit_time_period);
+        Thread::wait(1000*transmit_time_period); // Thread::wait() in ms
     }
 }
 
@@ -250,21 +250,21 @@
 * Ethernet init
 */
 int ethernet_init(void) {
-    DBG("Start initialising ethernet\n");
+    DBG("Start initialising ethernet");
     int ret = eth.init(u8mac, str_ip_addr, str_ip_subnet, str_ip_gateway); // static
 
     if (!ret) {
-        DBG("Initialized, MAC: %s\n", eth.getMACAddress());
+        DBG("Initialized, MAC: %s", eth.getMACAddress());
     } else {
-        ERR("Error eth.init() - ret = %d\n", ret);
+        ERR("Error eth.init() - ret = %d", ret);
         return -1;
     }
 
     ret = eth.connect();
     if (!ret) {
-        DBG("IP: %s, MASK: %s, GW: %s\n", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
+        DBG("IP: %s, MASK: %s, GW: %s", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
     } else {
-        ERR("Error eth.connect() - ret = %d\n", ret);
+        ERR("Error eth.connect() - ret = %d", ret);
         return -1;
     }
     
@@ -286,20 +286,13 @@
     * Configure
     */
     uart.baud(115200);
-    DBG("\r\nStarting...\r\n");
+    DBG("\r\nStarting...");
   
     /*
     * UI threads
     */
     Thread t1(uart_thread);
-    Thread t2(auto_update_timer_thread);
-    
-    //// send to uart
-    //buffer[n] = '\0';
-    //message.len = n;
-    //message.msg = buffer;
-    //uart_queue.put(&message);
-    
+    Thread t2(auto_update_timer_thread);    
     
     /*
     * FLASH
@@ -312,7 +305,7 @@
     */
     ret = ethernet_init();
     if (ret) {
-        ERR("Ethernet initialisation failed. App halted\r\n");
+        ERR("Ethernet initialisation failed. App halted.");
         while (true) {};
     }
     
@@ -323,17 +316,17 @@
 #ifdef TCP_SERVER
     tcp_server.bind(tcp_server_local_port);
     tcp_server.listen();
-    DBG("TCP server started...\r\n");
+    DBG("TCP server started...");
     tcp_server.set_blocking(false, TCP_SERVER_WAIT_CLIENT_TIMEOUT);
 #endif
 
 #ifdef TCP_CLIENT
-    //RtosTimer tcp_client_auto_update_timer(tcp_client_auto_update, osTimerPeriodic, NULL);
+
 #endif
     
 #ifdef UDP_SERVER
     ret = udp_server.bind(udp_server_local_port);
-    DBG("UDP started (sock.bind = %d)\r\n", ret);
+    DBG("UDP started (sock.bind = %d)", ret);
     udp_server.set_blocking(false, UDP_SERVER_RECEIVE_TIMEOUT);
 #endif
 
@@ -343,43 +336,44 @@
     */
     while (true) {
 #ifdef TCP_CLIENT
-    // FOR AUTO TRANSMIT DEVICE STATUS
-    if (auto_transmit_flag == 0xA5A5) {
-        // connect to TCP server if required
-        if (!tcp_sock.is_connected()) {
-            ret = tcp_sock.connect(str_server_ip_addr, u16tcp_server_port);
-            if (ret > -1) {
-                DBG("Successfully connected to %s on port %d\r\n", str_server_ip_addr, u16tcp_server_port);
-            }
-            else {
-                ERR("Unable to connect to %s on port %d\r\n", str_server_ip_addr, u16tcp_server_port);
+        // FOR AUTO TRANSMIT DEVICE STATUS
+        //if ((u16enable_tcp_client == 0xA5A5) && (auto_transmit_flag == 0xA5A5)) {
+        if (auto_transmit_flag == 0xA5A5) {
+            // connect to TCP server if required
+            if (!tcp_sock.is_connected()) {
+                ret = tcp_sock.connect(str_server_ip_addr, u16tcp_server_port);
+                if (ret > -1) {
+                    DBG("Successfully connected to %s on port %d", str_server_ip_addr, u16tcp_server_port);
+                }
+                else {
+                    ERR("Unable to connect to %s on port %d", str_server_ip_addr, u16tcp_server_port);
+                }
             }
-        }
-        
-        // transmit data if connected
-        if (tcp_sock.is_connected()) {
-            osEvent evt = auto_update_queue.get(1); // timeout after 1ms
-            if (evt.status == osEventMessage) {
-                DBG("Updating...\r\n");
-                update_sending_frame(tcp_client_buffer);
-                tcp_sock.send_all(tcp_client_buffer, SENDING_PROTOCOL_LENGTH);
+                
+            // transmit data if connected
+            if (tcp_sock.is_connected()) {
+                osEvent evt = auto_update_queue.get(1); // timeout after 1ms
+                if (evt.status == osEventMessage) {
+                    DBG("Updating...");
+                    update_sending_frame(tcp_client_buffer);
+                    tcp_sock.send_all(tcp_client_buffer, SENDING_PROTOCOL_LENGTH);
+                }
             }
-        }
-    } // auto transmit
+        } // if tcp client enabled && auto transmit
 #endif
 
 
 // FOR INTERFACING
 #ifdef TCP_SERVER
         // no tcp client connected
-        if (!tcp_client.is_connected())
-        {
+        //if ((u16enable_tcp_server == 0xA5A5) && (!tcp_client.is_connected())) {
+        if (!tcp_client.is_connected()) {
             // wait for client within timeout
             ret = tcp_server.accept(tcp_client);
             
             // tcp client connected
             if (ret > -1) {
-                DBG("Connection from: %s\r\n", tcp_client.get_address());
+                DBG("Connection from: %s", tcp_client.get_address());
                 
                 // loop waiting and receiving data within timeout
                 tcp_client.set_blocking(false, TCP_SERVER_RECEIVE_TIMEOUT); // Timeout after x seconds
@@ -388,7 +382,7 @@
                     if (n <= 0) break;
                     
                     // got some data, test it
-                    DBG("TCP server received: %s\r\n", tcp_server_buffer);
+                    DBG("TCP server received: %s", tcp_server_buffer);
                     // process received data
                     switch (n) {
                         // length 58-bytes, Receiving protocol
@@ -400,12 +394,12 @@
                                 break;
                             else if ((id - tcp_server_buffer) > 0)
                                 break;
-                            DBG("Correct.\r\n");
+                            DBG("Correct.");
                             
                             // firstly, update outputs if required
                             // digital outputs
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_DO_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
-                                DBG("Update digital outputs\r\n");
+                                DBG("Update digital outputs");
                                 char str_dout[9];
                                 memcpy(str_dout, &tcp_server_buffer[RECEIVING_PROTOCOL_DO_POS], 8);
                                 str_dout[8] = '\0';
@@ -413,11 +407,11 @@
                             }
                             // analog output 0
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_A0O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
-                                DBG("Update analog output 0\r\n");
+                                DBG("Update analog output 0");
                             }
                             // analog output 1
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_A1O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
-                                DBG("Update analog output 1\r\n");
+                                DBG("Update analog output 1");
                             }
                             // UART
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_EN_UART_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) {
@@ -430,7 +424,7 @@
                             
                             // then, check query status command and sending protocol if required
                             if (tcp_server_buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) {
-                                DBG("Sent device status through TCP\r\n");
+                                DBG("Sent device status through TCP");
                                 // sending protocol
                                 update_sending_frame(tcp_server_buffer);
                                 tcp_client.send_all(tcp_server_buffer, SENDING_PROTOCOL_LENGTH);
@@ -444,19 +438,19 @@
                 } // end loop if no data received within timeout
                 tcp_client.close();
             } // if client connected
-        } // if no client connected
+        } // if tcp server enabled && no client connected
 #endif
     
     
     
-// ONLY FOR CONFIGRATION
+// ALWAYS ENABLED, ONLY FOR CONFIGRATION
 #ifdef UDP_SERVER
         // wait for udp packet within timeout
         n = udp_server.receiveFrom(ep_udp_client, udp_server_buffer, sizeof(udp_server_buffer));
         if (n <= 0) continue;
 
         // got some data, test it
-        DBG("UDP received (%s) from (%s) and port (%d)\r\n", udp_server_buffer, ep_udp_client.get_address(), ep_udp_client.get_port());
+        DBG("UDP received (%s) from (%s) and port (%d)", udp_server_buffer, ep_udp_client.get_address(), ep_udp_client.get_port());
         // process received data
         switch (n) {
             // length = 6, a QUERY command (discovery command, TCP port, or UDP port)
@@ -496,23 +490,23 @@
 #ifdef NTP
                     char str_time[50];
                     
-                    DBG("Trying to update time...\r\n");
+                    DBG("Trying to update time...");
                     if (ntp.setTime("0.pool.ntp.org") == 0) {
-                        DBG("Set time successfully\r\n");
+                        DBG("Set time successfully");
                         time_t ctTime;
                         ctTime = time(NULL);
                         
-                        DBG("Time is set to (UTC): %s\r\n", ctime(&ctTime));
+                        DBG("Time is set to (UTC): %s", ctime(&ctTime));
                         sprintf(str_time, "%s", ctime(&ctTime));
                         udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
                     }
                     else {
-                        WARN("Error\r\n");
+                        WARN("Error");
                         sprintf(str_time, "ERR");
                         udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
                     }
 #elif
-                    WARN("NTP disabled\r\n");
+                    WARN("NTP disabled");
                     sprintf(str_time, "DIS");
                     udp_server.sendTo(ep_udp_client, str_time, strlen(str_time));
 #endif
@@ -530,7 +524,7 @@
                 else if ((id - udp_server_buffer) > 0)
                     break;
 
-                DBG("Received user configuration\r\n");
+                DBG("Received user configuration");
                 write_eeprom_network(&udp_server_buffer[strlen(DEVICE_ID)]); // parameters from 5th char, 15-bytes
                 break;
             }
@@ -545,7 +539,7 @@
                 else if ((id - udp_server_buffer) > 0)
                     break;
                 
-                DBG("Received TCP server configuration\r\n");
+                DBG("Received TCP server configuration");
                 write_eeprom_tcpserver(&udp_server_buffer[strlen(DEVICE_ID)]); // parameters from 5th char
                 break;
             }
@@ -560,7 +554,7 @@
 * Update digital outputs following receiving frame from TCP client
 */
 void update_digital_outputs(char* buf) {
-    DBG("Digital outputs: %s\n", buf);
+    DBG("Digital outputs: %s", buf);
     
     dout0 = (buf[0] == DIGITAL_HIGH)? 1 : 0;
     dout1 = (buf[1] == DIGITAL_HIGH)? 1 : 0;
--- a/mbed-rtos.lib	Sat Oct 04 20:25:45 2014 +0000
+++ b/mbed-rtos.lib	Tue Oct 21 20:39:15 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/olympux/code/mbed-rtos/#56a3d044541d
+http://mbed.org/users/olympux/code/mbed-rtos/#fd9580752759
--- a/my_eeprom_funcs.lib	Sat Oct 04 20:25:45 2014 +0000
+++ b/my_eeprom_funcs.lib	Tue Oct 21 20:39:15 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/olympux/code/my_eeprom_funcs/#944a4646b825
+http://mbed.org/users/olympux/code/my_eeprom_funcs/#eea03d0eb95a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/protocol.txt	Tue Oct 21 20:39:15 2014 +0000
@@ -0,0 +1,92 @@
+CONFIGURATION SECTION (UDP)
+1. DISCOVERY Command
+    + UDP broadcast: 192.168.0.255 to port 11000
+    + Send: NNIODS
+    + Receive: NNIO IP-address
+1a. Query Command: IP, subnet, gateway, mac
+    + + Send: NNIOIP, NNIOSN, NNIOGW, NNIOMC
+
+2. TCP SERVER PORT Command
+    + Send: NNIOTP
+    + Receive: 10000
+
+3. UDP SERVER PORT Command
+    + Send: NNIOUP
+    + Receive: 11000
+
+4. Set time using NTP Command    
+    + Send NNIOTM
+    + Receive:
+      DIS: if NTP is disabled
+      ERR: if cannot update time
+      Successful: Fri Sep 26 20:28:01 2014 {0A}
+      
+5. Set new network configuration
+    + Send: 19 bytes in total, NNIO + 15-byte
+      NNIO         4-byte IP address    4-byte subnet    4-byte gateway   3-byte MAC
+      4E 4E 49 4F    C0 A8 00 78         FF FF FF 00       C0 A8 00 01     00 00 01
+
+6. Set TCP server info (only when the device is as a TCP client)
+    + Send: 12 bytes in total, NNIO + 8 bytes
+      NNIO         1-byte auto flag        1-byte time period (s)      4-byte IP       2-byte port (LSB MSB)
+      4E 4E 49 4F  'Y' or others            05 (5s)                   C0 A8 00 09       E0 2E (0x2EE0 = 12000)
+
+NOTE:
+1. Both TCP client and server are working. Can be enabled/disabled using u16enable_tcp_client/server flags in eeprom (not in use now).
+2. UDP server is always working.
+
+
+INTERFACING SECTION (TCP)
+4. Receiving Protocol: 58-bytes in total
+Ex in hex
+ID: 4E 4E 49 4F
+OP: 4F 4F 4F 4F 51
+IP: C0 A8 00 78
+DO: 48 48 48 48 48 48 48 48
+AO: 80 00 80 00 (no DAC)
+UART: 32-byte
+CR: 0D
+    
+    + Field ID (4-bytes) = NNIO
+    + Field OP (5-bytes): output control enable flag
+      Byte 1: Digital output
+      Byte 2: Analog output 0
+      Byte 3: Analog output 1
+      Byte 4: UART output
+      Byte 5: Command (Q: query status)
+      'O': enable controlling output
+      Others: disable controlling output
+      If Command is 'Q', device will update its outputs if needed and send its status including new outputs
+    + Field IP (4-bytes): device IP address
+    + Field DO[n] (8-bytes): digital output values
+      'H': output set to high
+      'L': output set to low
+    + Field AO_0 (2-bytes): 16-bit analog output value, channel 0
+      no DAC
+    + Field AO_1 (2-bytes): 16-bit analog output value, channel 1
+      no DAC
+    + UART output (32-bytes): max 32 bytes, stop string by NULL
+    + End char: CR
+    
+5. Sending Protocol: 39-bytes in total
+    + Field ID (4-bytes) = NNIO
+    + Field MAC (6-bytes)
+    + Field IP (4-bytes)
+    + Field DI[n]: 8-bit digital input values
+      'H' if input is HIGH
+      'L' if input is LOW
+    + Field DO[n]: 8-bit digital output values
+      'H' if output is HIGH
+      'L' if output is LOW
+    + Field AI_0 (2-bytes): analog 16-bit input value, normalised, channel 0
+      1st byte = LSB, 2nd byte = MSB
+    + Field AI_1 (2-bytes): analog 16-bit input value, normalised, channel 1
+      1st byte = LSB, 2nd byte = MSB
+    + Field AO_0 (2-bytes): 16-bit analog output value, channel 0
+      no DAC, fixed
+    + Field AO_1 (2-bytes): 16-bit analog output value, channel 1
+      no DAC, fixed
+    + End char: CR
+    
+6 Commands:
+    + QUERY STATUS ('Q')
\ No newline at end of file
--- a/readme.txt	Sat Oct 04 20:25:45 2014 +0000
+++ b/readme.txt	Tue Oct 21 20:39:15 2014 +0000
@@ -1,88 +1,4 @@
-CONFIGURATION SECTION (UDP)
-1. DISCOVERY Command
-    + UDP broadcast: 192.168.0.255 to port 11000
-    + Send: NNIODS
-    + Receive: NNIO IP-address
-1a. Query Command: IP, subnet, gateway, mac
-    + + Send: NNIOIP, NNIOSN, NNIOGW, NNIOMC
-
-2. TCP SERVER PORT Command
-    + Send: NNIOTP
-    + Receive: 10000
-
-3. UDP SERVER PORT Command
-    + Send: NNIOUP
-    + Receive: 11000
-
-4. Set time using NTP Command    
-    + Send NNIOTM
-    + Receive:
-      DIS: if NTP is disabled
-      ERR: if cannot update time
-      Successful: Fri Sep 26 20:28:01 2014 {0A}
-      
-5. Set new network configuration
-    + Send: 19 bytes in total, NNIO + 15-byte
-      NNIO         4-byte IP address    4-byte subnet    4-byte gateway   3-byte MAC
-      4E 4E 49 4F    C0 A8 00 78         FF FF FF 00       C0 A8 00 01     00 00 01
-
-6. Set TCP server info (only when the device is as a TCP client)
-    + Send: 12 bytes in total, NNIO + 8 bytes
-      NNIO         1-byte auto flag        1-byte time period (s)      4-byte IP       2-byte port (LSB MSB)
-      4E 4E 49 4F  'Y' or others            05 (5s)                   C0 A8 00 09       E0 2E (0x2EE0 = 12000)
-
-
-INTERFACING SECTION (TCP)
-4. Receiving Protocol: 58-bytes in total
-Ex in hex
-ID: 4E 4E 49 4F
-OP: 4F 4F 4F 4F 51
-IP: C0 A8 00 78
-DO: 48 48 48 48 48 48 48 48
-AO: 80 00 80 00 (no DAC)
-UART: 32-byte
-CR: 0D
-    
-    + Field ID (4-bytes) = NNIO
-    + Field OP (5-bytes): output control enable flag
-      Byte 1: Digital output
-      Byte 2: Analog output 0
-      Byte 3: Analog output 1
-      Byte 4: UART output
-      Byte 5: Command (Q: query status)
-      'O': enable controlling output
-      Others: disable controlling output
-      If Command is 'Q', device will update its outputs if needed and send its status including new outputs
-    + Field IP (4-bytes): device IP address
-    + Field DO[n] (8-bytes): digital output values
-      'H': output set to high
-      'L': output set to low
-    + Field AO_0 (2-bytes): 16-bit analog output value, channel 0
-      no DAC
-    + Field AO_1 (2-bytes): 16-bit analog output value, channel 1
-      no DAC
-    + UART output (32-bytes): max 32 bytes, stop string by NULL
-    + End char: CR
-    
-5. Sending Protocol: 39-bytes in total
-    + Field ID (4-bytes) = NNIO
-    + Field MAC (6-bytes)
-    + Field IP (4-bytes)
-    + Field DI[n]: 8-bit digital input values
-      'H' if input is HIGH
-      'L' if input is LOW
-    + Field DO[n]: 8-bit digital output values
-      'H' if output is HIGH
-      'L' if output is LOW
-    + Field AI_0 (2-bytes): analog 16-bit input value, normalised, channel 0
-      1st byte = LSB, 2nd byte = MSB
-    + Field AI_1 (2-bytes): analog 16-bit input value, normalised, channel 1
-      1st byte = LSB, 2nd byte = MSB
-    + Field AO_0 (2-bytes): 16-bit analog output value, channel 0
-      no DAC, fixed
-    + Field AO_1 (2-bytes): 16-bit analog output value, channel 1
-      no DAC, fixed
-    + End char: CR
-    
-6 Commands:
-    + QUERY STATUS ('Q')
\ No newline at end of file
+0. Wait for 2s after resetting for ethernet to work.
+1. When auto update enabled and no TCP server is running, module will try to connect to server which results in a timeout every 15s.
+   This makes UDP packets take more time to be received and processed by the module. The result is slow UDP commands, such as Discovery.
+   To make it faster, start a TCP server on Hercules. The module will connect to it to transmit updates.
\ No newline at end of file