SPI to Ethernet Slave

Dependencies:   WIZnetInterface mbed

Fork of SPI_HelloWorld_Mbed by mbed official

Revision:
5:709f1ad85e97
Parent:
4:eec728094dbc
Child:
6:6edad6566ab0
--- a/main.cpp	Wed Mar 09 08:23:53 2016 +0000
+++ b/main.cpp	Thu Mar 10 01:28:35 2016 +0000
@@ -1,9 +1,23 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
 
+#define DHCP 1
+
+uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x45, 0x56, 0x67};
+const char ip_addr[] = "xxx.xxx.xxx.xxx"; 
+const char mask_addr[] = "xxx.xxx.xxx.xxx"; 
+const char gateway_addr[] = "xxx.xxx.xxx.xxx"; 
+
+const char* Target_addr = "192.168.0.2";
+const int Target_port = 22222;
+
+
 SPISlave slave(PA_8, PA_7, PA_6, PA_5); // mosi, miso, sclk, ssel
  
 int main() {
+    int spidata=0;
+    char EthTx[1];
+
     printf("SPI to Ethernet Slave\r\n");
   
     /*
@@ -11,13 +25,51 @@
     */
     slave.format(8,3);
     slave.frequency(1000000);
-
+    /*
+    * Network Setting
+    */
+    printf("Wait a second...\r\n");
+    EthernetInterface eth;
+    #if DHCP==1
+        printf("Network Setting DHCP\r\n");
+        eth.init(mac_addr); 
+    #else
+        printf("Network Setting Static\r\n");
+        eth.init(mac_addr, ip_addr, mask_addr, gateway_addr);
+    #endif
+    eth.connect();
+    printf("IP Address is %s\r\n", eth.getIPAddress());
     while(1) 
     { 
-        if(slave.receive()) 
+        printf("Check Ethernet Link\r\n");
+        if(eth.link() == true)
         {
-            int SPI_re = slave.read();   // Read byte from master
-            printf("SPI_re=%c\r\n",SPI_re);
+            printf("Link up\r\n");
+            break;
+        }
+    }
+    
+    /*
+    * Create Client Socket and Connecting
+    */
+    TCPSocketConnection socket;
+    while(1)
+    {       
+        while (socket.connect(Target_addr, Target_port) < 0) 
+        {
+            printf("Unable to connect to (%s) on port (%d)\r\n", Target_addr, Target_port);
+            wait(1);
+        }
+        printf("Connected to Server at %s\r\n",Target_addr);
+        
+        while(1)
+        {
+            if(slave.receive())
+            {
+                spidata = slave.read();
+                EthTx[0] = spidata;
+                socket.send(EthTx, 1);   
+            }
         }
     }
 }
\ No newline at end of file