yuki makura / Mbed 2 deprecated mbed_driver_ver_0_3_0

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of mbed_driver_ver_0_2_0 by yuki makura

Files at this revision

API Documentation at this revision

Comitter:
yukisega
Date:
Mon Oct 22 15:47:42 2018 +0000
Parent:
0:c490b05d753e
Child:
2:233951f9354d
Commit message:
ethernet???????; ???????????????????????????

Changed in this revision

EthernetInterface.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-os.lib 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Mon Oct 22 15:47:42 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#183490eb1b4a
--- a/main.cpp	Fri Sep 28 00:04:25 2018 +0000
+++ b/main.cpp	Mon Oct 22 15:47:42 2018 +0000
@@ -14,7 +14,7 @@
     @author Wada Suisei   Copyright (C) 2018 - All rights reserved          
     @author modified by yukimakura
 
-    @date LastModified:2018 July 2 
+    @date LastModified:2018 Oct 23 
 */
 
 /*** Including Libraries ***/
@@ -33,23 +33,21 @@
  * @brief 接続のタイムアウト(ミリ秒)
  * 
  */
-#define TIMEOUT_MS 3000 
+#define TIMEOUT_MS 1000 
 
 
 /*** Network Information ***/
 ///myIpAddrこのモータドライバのIPアドレスを入力
-const char myIpAddr[] = "192.168.39.10";   
+#define myIpAddr "10.42.0.98" 
 /// myPortこのプログラムで使用するポートを入力  
-const uint16_t myPort = 50000;
+#define myPort 50000
 /// NetMask このモータドライバ用のネットマスクを入力 
-const char NetMask[] = "255.255.255.0"; 
+#define NetMask "255.255.255.0"
 /// Gateway このモータドライバ用のゲートウェイを入力 
-const char Gateway[] = "192.168.39.1";
+#define Gateway "10.42.0.1"
 
 
 /*** Instantiation ***/
-//Interruption
-Timeout linkdown_catcher;
 Timer Timeout_timer;
 
 //! PWM信号の差動出力ピンを指定
@@ -57,8 +55,11 @@
 //! PWM信号の差動出力ピンを指定
 PwmOut motor_N(p25);
 
+Thread *(eth_pri);
+
 double pipe_pwmduty;
 
+Mutex g_mtx;
 
 //Network
 DigitalOut EthRecv_LED(LED1);
@@ -82,11 +83,12 @@
  * 
  * 
  */
-void INT_TIMER_LINKDOWN_CATCHER(void){
+void timeout(void){
+    g_mtx.lock();
     pipe_pwmduty = 0.0;
+    g_mtx.unlock();
     motor_P = 0.5;
     motor_N = 0.5;
-    exit(-1);
 }
 
 
@@ -103,94 +105,52 @@
  * \endhtmlonly
  */
 void ethernet_thread(void){
-    //Declaration of Local Variables
-    char in_buf[8];
-    char out_buf[10];
+    char in_buf[64];
     
-    //Initializing Network
-    SocketAddress SockAddr_Receive;
-    eth.set_network(myIpAddr, NetMask, Gateway);
-    if(0 != eth.connect()){
-        exit(-1);
-    }
-    TCPServer srv;
-    TCPSocket sock;
-    srv.open(&eth);
-    srv.bind(eth.get_ip_address(), myPort);
+    EthernetInterface eth;
+    //eth.init();
+    eth.init(myIpAddr,NetMask,Gateway);
+    eth.connect();
+    printf("\nServer IP Address is %s\n", eth.getIPAddress());
+    
+    TCPSocketServer server;
+    server.bind(myPort);
+    server.listen();
+    
     while (true) {
-
-        printf("waiting for client\r\n");
-        /* Can handle 5 simultaneous connections */
-        int err= srv.listen(1);
-        printf("server listening error : %d\r\n",err);
-
-        Timeout_timer.start();
-        while(1){
-            /* Initialize variables */
-            memset(in_buf, '\0', sizeof(in_buf));
-            memset(out_buf, '\0', sizeof(out_buf));
-            printf("waiting for client connection\r\n");
-            err = srv.accept(&sock, &SockAddr_Receive);
-            Timeout_timer.reset();
+        printf("\nWait for new connection...\n");
+        TCPSocketConnection client;
+        server.accept(client);
+        //client.set_blocking(false, TIMEOUT_MS); // Timeout after (TIMEOUT_MS)ms
+        
+        printf("Connection from: %s\n", client.get_address());
+        
+        while (client.is_connected()) {
+            
             Ethconnect_LED = 1;
-            //https://docs.mbed.com/docs/vignesh/en/latest/api/group__netsocket.html#ga67a8f07758d2ee2a1809293fa52bdf14
-            switch(err){
-                case NSAPI_ERROR_OK:
-                    printf("client connected :%s:%d\r\n", SockAddr_Receive.get_ip_address(), SockAddr_Receive.get_port());
-                    while(1){
-                        int len = sock.recv(in_buf, sizeof(in_buf));
-                        if(Timeout_timer.read_ms() < TIMEOUT_MS){
-
-                            if(len > 0){
-                                Timeout_timer.reset();
-                                in_buf[len] = '\0';
-                                //Get data
-                                EthRecv_LED = !EthRecv_LED;
-                                memcpy(&pipe_pwmduty, &in_buf[0], 8);
-                                
-
-                                /* Send Data */
-                                //sock.send(out_buf, sizeof(out_buf));
-                                EthSend_LED = !EthSend_LED;
-                            }
-                        }
-                        else{//TIMEOUT
-                            printf("Time_out (over %d ms)\n",TIMEOUT_MS);
-                            memset(in_buf, '\0', sizeof(in_buf));
-                            pipe_pwmduty = 0.0;
-                            motor_P = 0.5;
-                            motor_N = 0.5;
-                            Ethconnect_LED = 0;
-                            sock.close();
-                            break;
-                        }
-                    }
-                    break;
-                case NSAPI_ERROR_WOULD_BLOCK :
-                    printf("NSAPI_ERROR_WOULD_BLOCK\n");
-                    memset(in_buf, '\0', sizeof(in_buf));
-                    INT_TIMER_LINKDOWN_CATCHER();
-                    break;
-                case NSAPI_ERROR_NO_CONNECTION :
-                    printf("NSAPI_ERROR_NO_CONNECTION\n");
-                    memset(in_buf, '\0', sizeof(in_buf));
-                    INT_TIMER_LINKDOWN_CATCHER();
-                    break;
-                case NSAPI_ERROR_NO_SOCKET :
-                    printf("NSAPI_ERROR_NO_SOCKET\n");
-                    memset(in_buf, '\0', sizeof(in_buf));
-                    INT_TIMER_LINKDOWN_CATCHER();
-                    break;
-                case NSAPI_ERROR_NO_ADDRESS :
-                    printf("NSAPI_ERROR_NO_ADDRESS\n");
-                    memset(in_buf, '\0', sizeof(in_buf));
-                    INT_TIMER_LINKDOWN_CATCHER();
-                    break;
-                default:
-                    printf("other error");
-                    break;
+            int len = client.receive(in_buf, sizeof(in_buf));
+            EthRecv_LED = !EthRecv_LED;
+            // print received message to terminal
+            in_buf[len] = '\0';
+            if(len > 0){
+               // Timeout_timer.reset();
+                in_buf[len] = '\0';
+                //Get data
+                EthRecv_LED = !EthRecv_LED;
+                g_mtx.lock();
+                memcpy(&pipe_pwmduty, &in_buf[0], 8);
+                //printf("Received message from Client :'%f'\n",pipe_pwmduty);
+                g_mtx.unlock();
+                Timeout_timer.reset();
+            }
+            if(Timeout_timer.read_ms() > TIMEOUT_MS){
+                break;
             }
         }
+        printf("\nclient closed\n");
+        Ethconnect_LED = 0;
+        timeout();
+        client.close();
     }
 }
 
@@ -200,6 +160,7 @@
  * @return OSに返します。
  */
 int main(){
+    Timeout_timer.start();
     /* Declaration of Local Variables */
     float pwmduty;
 
@@ -219,21 +180,29 @@
     
     /* Start Thread */
     Thread thread_get(ethernet_thread);
-
+    eth_pri = &thread_get;
+    eth_pri -> set_priority(osPriorityHigh);
     
     /* Main Routine */
     while(true){
         //disconnect timeout
         if(Timeout_timer.read_ms() > TIMEOUT_MS){
-            pipe_pwmduty = 0.0;
+            g_mtx.lock();
+            pipe_pwmduty = 0.0;  
+            g_mtx.unlock();
+            timeout();
             EthRecv_LED = 0;
             EthSend_LED = 0;
             Ethconnect_LED = 0;
+            printf("%d\n",Timeout_timer.read_ms());
+            Timeout_timer.reset();
             //return -1;
         }
         
         /* Calculate PWM Duty */
+        g_mtx.lock();
         pwmduty = pipe_pwmduty / 2.0;
+        g_mtx.unlock();
 
         /* Output PWM */
         motor_P = (0.5 + pwmduty);
--- a/mbed-os.lib	Fri Sep 28 00:04:25 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/users/elessair/code/mbed-os/#f269e3021894
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Mon Oct 22 15:47:42 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Oct 22 15:47:42 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/176b8275d35d
\ No newline at end of file