SPI to Ethernet Master

Dependencies:   WIZnetInterface mbed

Fork of SPI_HelloWorld_Mbed by mbed official

Files at this revision

API Documentation at this revision

Comitter:
Ricky_Kwon
Date:
Wed Mar 09 08:14:30 2016 +0000
Parent:
2:34bd7b8d30f9
Child:
4:1b4fc2571fd4
Commit message:
SPI to Ethernet Master

Changed in this revision

WIZnetInterface.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.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZnetInterface.lib	Wed Mar 09 08:14:30 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#bda61525ac71
--- a/main.cpp	Fri Mar 27 20:16:45 2015 +0000
+++ b/main.cpp	Wed Mar 09 08:14:30 2016 +0000
@@ -1,42 +1,101 @@
-/* mbed Example Program
- * Copyright (c) 2006-2014 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "mbed.h"
- 
-SPI spi(p5, p6, p7); // mosi, miso, sclk
-DigitalOut cs(p8);
- 
-int main() {
-    // Chip must be deselected
-    cs = 1;
-
-    // Setup the spi for 8 bit data, high steady state clock,
-    // second edge capture, with a 1MHz clock rate
-    spi.format(8,3);
-    spi.frequency(1000000);
- 
-    // Select the device by seting chip select low
-    cs = 0;
- 
-    // Send 0x8f, the command to read the WHOAMI register
-    spi.write(0x8F);
- 
-    // Send a dummy byte to receive the contents of the WHOAMI register
-    int whoami = spi.write(0x00);
-    printf("WHOAMI register = 0x%X\n", whoami);
- 
-    // Deselect the device
-    cs = 1;
-}
\ No newline at end of file
+#include "mbed.h"
+#include "EthernetInterface.h"
+
+#define DHCP 1
+
+uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x12, 0x34, 0x45};
+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 = 10001;
+
+SPI master(PA_8, PA_7, PA_6, PA_5); // mosi, miso, sclk
+ 
+int main() {
+
+    char txbuf[256]={0,};
+    char rxbuf[256]={0,};
+    int i, n, closecount, slave_h, length;
+
+    printf("SPI to Ethernet Master\r\n");
+    
+    /*
+    * SPI Setting
+    */
+    master.format(8,3);
+    master.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) 
+    { 
+        printf("Check Ethernet Link\r\n");
+        if(eth.link() == true)
+        {
+            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)
+        {
+            
+            n = socket.receive(txbuf, sizeof(txbuf));
+            printf("n = %d\r\n", n);
+            slave_h = master.write(0x55);
+            printf("slave_h = char:%c, dec:%d, hex:%x\r\n", slave_h, slave_h, slave_h);
+            if(slave_h==0x55)
+            {
+                printf("check loop slave_h if\r\n");
+                length = master.write(0x01);
+                printf("length = char:%c, dec:%d, hex:%x\r\n", length, length, length);
+                for(i=0; i<length; i++)
+                {
+                    rxbuf[i]=master.write(0x00);
+                    printf("rxbuf[%d] = char:%c, dec:%d, hex:%x\r\n", i, rxbuf[i], rxbuf[i], rxbuf[i]);
+                }      
+                socket.send(rxbuf, strlen(rxbuf));
+            }
+            if(n > 0)
+            {
+                printf("check loop n if\r\n");
+                for(i=0; i<n; i++)
+                {
+                    master.write(txbuf[i]);
+                    printf("check loop for\r\n");
+                }
+                memset(txbuf, 0, strlen(txbuf));
+                closecount=0;
+            }
+        }
+    }
+}
+
+
--- a/mbed.bld	Fri Mar 27 20:16:45 2015 +0000
+++ b/mbed.bld	Wed Mar 09 08:14:30 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/487b796308b0
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/87f2f5183dfb
\ No newline at end of file