SPI to Ethernet Slave

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:15:23 2016 +0000
Parent:
2:34bd7b8d30f9
Child:
4:eec728094dbc
Commit message:
SPI to Ethernet Slave

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:15:23 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:15:23 2016 +0000
@@ -1,42 +1,121 @@
-/* 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);
+#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 = 10000;
+char* test=NULL;
+
+SPISlave slave(PA_8, PA_7, PA_6, PA_5); // mosi, miso, sclk, ssel
  
 int main() {
-    // Chip must be deselected
-    cs = 1;
+
+    char txbuf[256]={0,};
+    char rxbuf[256]={0,};
+    int i, n, close_cnt, length, send_flag=0, cnt_flag=0, send_cnt=0;
+    int slave_h=0;
 
-    // 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;
+    printf("SPI to Ethernet Slave\r\n");
+  
+    /*
+    * SPI Setting
+    */
+    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) 
+    { 
+        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);
+            if(slave.receive())
+            {
+                slave_h = slave.read();
+                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");
+                    if(n > 0)
+                    {
+                        printf("check loop n if\r\n");
+                        slave.reply(0x55);
+                        cnt_flag = 1;
+                    }
+                    else
+                    {
+                        slave.reply(0x00);
+                    }
+                }
+                else if((slave_h==0x01)&&(cnt_flag==1))   
+                {
+                    printf("check loop slave_h + cnt_flag if\r\n");
+                    slave.reply(n);
+                    cnt_flag = 0;
+                    send_flag = 1;
+                }
+                else
+                {
+                    printf("else\r\n");
+                    test = (char*)slave_h;
+                    socket.send(test, 1);
+                } 
+                
+                if(send_flag)
+                {
+                    printf("send_flag\r\n");
+                    slave.reply(txbuf[send_cnt]);
+                    send_cnt++;
+                    printf("send_cnt = char:%c, dec:%d, hex:%x\r\n", send_cnt, send_cnt, send_cnt);
+                    if(send_cnt==n)
+                    {
+                           printf("send_cnt\r\n");
+                           send_cnt=0;
+                           send_flag=0;      
+                    }
+                }      
+            }
+        }
+    }
 }
\ No newline at end of file
--- a/mbed.bld	Fri Mar 27 20:16:45 2015 +0000
+++ b/mbed.bld	Wed Mar 09 08:15:23 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