Program for test the tcp/ip communication between mbed and Android phone

Dependencies:   EthernetInterface mbed-rtos mbed

Notebook page HERE

Files at this revision

API Documentation at this revision

Comitter:
edodm85
Date:
Wed Jul 20 20:24:15 2016 +0000
Commit message:
First commit

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
eth_status.h 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
main.h 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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 149b394928f3 EthernetInterface.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Wed Jul 20 20:24:15 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/EthernetInterface/#183490eb1b4a
diff -r 000000000000 -r 149b394928f3 eth_status.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eth_status.h	Wed Jul 20 20:24:15 2016 +0000
@@ -0,0 +1,54 @@
+#include "lpc_phy.h"
+
+/** \brief DP83848 PHY status definitions */
+#define DP8_REMOTEFAULT    (1 << 6)   /**< Remote fault */
+#define DP8_FULLDUPLEX     (1 << 2)   /**< 1=full duplex */
+#define DP8_SPEED10MBPS    (1 << 1)   /**< 1=10MBps speed */
+#define DP8_VALID_LINK     (1 << 0)   /**< 1=Link active */
+
+
+    // This function returns the current status of connection.
+static bool get_link_status()
+{
+    u32_t tmp = lpc_mii_read_data();        
+    return (tmp & DP8_VALID_LINK) ? true : false;
+}
+
+    // This function returns the status of transmission.
+static char* get_transmission_status()
+{
+    u32_t tmp = lpc_mii_read_data();
+    if(tmp & DP8_FULLDUPLEX)
+    { 
+        return "FULL DUPLEX"; 
+    }else
+    {        
+        return "HALF DUPLEX";
+    }
+}
+
+    // This function returns the speed of the connection.
+static int get_connection_speed()
+{
+    u32_t tmp = lpc_mii_read_data();
+    return (tmp & DP8_SPEED10MBPS) ? 10 : 100;
+}
+
+    // This function returns the current value in the MII data register.
+static u32_t mii_read_data()
+{
+    return lpc_mii_read_data();  // 16-bit MRDD - address 0x2008 4030 
+    // 16661 = 100000100010101 (full duplex - 100Mbps)   // 16659 = 100000100010011 (half duplex - 10Mbps)                           
+}
+
+
+/*
+// Starts a read operation via the MII link (non-blocking) 
+u32_t lpc_mii_read_data(void)
+{
+    u32_t data = LPC_EMAC->MRDD;
+    LPC_EMAC->MCMD = 0;
+
+    return data;
+}
+*/
diff -r 000000000000 -r 149b394928f3 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jul 20 20:24:15 2016 +0000
@@ -0,0 +1,109 @@
+/*
+ * Author: Edoardo De Marchi
+ * Date: 20/07/16
+ * Notes: Test ethernet server (for Android connection)
+*/
+
+#include "main.h"
+
+
+
+int Init()
+{
+    led1 = 0;               
+    led2 = 0;              
+    led3 = 0;              
+    led4 = 0;               
+    
+       
+    // ETHERNET
+    eth.init(ip, mask, gateway);
+    eth.connect();
+    server.bind(ECHO_SERVER_PORT);
+    server.listen(1);
+    pc.printf("IP Address is %s\r\n", eth.getIPAddress());
+    
+    pc.printf("%s  - speed: %d Mbps\n", get_transmission_status(), get_connection_speed());
+   
+    // THREAD 
+    osThreadCreate(osThread(net_thread), NULL);
+            
+    return 0;
+}
+
+
+
+
+int main (void) 
+{
+    bool eth_status = false;
+    bool eth_status_temp = true;
+ 
+    Init();
+    
+    while (true)
+    {        
+        if(!get_link_status())
+        {
+            eth_status = true; 
+        }else
+        {
+            eth_status = false;
+            eth_status_temp = true;
+            led1 = 0;                           
+        }
+        
+        if(eth_status == eth_status_temp)
+        {
+            eth_status_temp = !eth_status;
+            pc.printf("Check cable connection\r\n");
+            led1 = 1;
+            eth_status = false;
+            if(checketh)
+            {
+                client.close();
+                pc.printf("Connection close.\r\n");
+                checketh = false;
+            }
+        }
+        led2 = !led2;          
+        osDelay(500);
+    }     
+}
+
+
+void net_thread(void const *argument)
+{
+    while (true) 
+    {
+        led3 = 1;
+        server.accept(client);
+        checketh = true;
+        pc.printf("Connection from: %s\r\n", client.get_address());
+        while (true) 
+        {
+            led3 = 0;
+            int n = client.receive(bufferRX, sizeof(bufferRX));  // ritorna il numero di byte spediti
+            if (n <= 0) break;
+            
+            bufferRX[n]=0; // make terminater
+            parse_cmd(n);
+        }
+        checketh = false;
+        client.close();
+        pc.printf("Connection close.\r\n");  
+    } 
+} 
+
+
+void parse_cmd(int sizeCMD)
+{
+            new_send = false;
+            printf("cmd << %s\r\n", bufferRX);
+            
+            sprintf(bufferTX, "CMD Received\r\n");
+            client.send_all(bufferTX, sizeof(bufferTX));
+            
+            memset(bufferTX, 0, sizeof(bufferTX));                                             
+            memset(bufferRX, 0, sizeof(bufferRX));           
+}
\ No newline at end of file
diff -r 000000000000 -r 149b394928f3 main.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Wed Jul 20 20:24:15 2016 +0000
@@ -0,0 +1,48 @@
+#pragma once 
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "eth_status.h"
+
+
+#define ECHO_SERVER_PORT   2000
+
+
+bool new_send = false;
+bool checketh = false;
+
+DigitalOut led1(LED1);          // broken cable
+DigitalOut led2(LED2);          // ARM is alive?
+DigitalOut led3(LED3);          // 0 = established connection
+DigitalOut led4(LED4);          // 
+
+
+Serial pc(USBTX, USBRX);
+
+
+//ETHERNET
+char* ip = "192.168.153.153";             // ip
+char* mask = "255.255.255.0";           // mask
+char* gateway = "192.168.153.254";          // gateway
+EthernetInterface eth;
+TCPSocketConnection client;
+TCPSocketServer server; 
+
+
+//ETHERNET BUFFER
+char bufferRX[20];
+char bufferTX[4800];    
+
+//RESET
+extern "C" void mbed_reset();
+
+
+
+//THREAD
+void net_thread(void const *argument);
+osThreadId tencid;
+osThreadDef(net_thread, osPriorityNormal, DEFAULT_STACK_SIZE); 
+
+
+
+//FUNCTION
+void parse_cmd(int sizeCMD);
diff -r 000000000000 -r 149b394928f3 mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Jul 20 20:24:15 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#4c105b8d7cae
diff -r 000000000000 -r 149b394928f3 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Jul 20 20:24:15 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6c34061e7c34
\ No newline at end of file