SPI to Ethernet Slave
Dependencies: WIZnetInterface mbed
Fork of SPI_HelloWorld_Mbed by
Revision 4:eec728094dbc, committed 2016-03-09
- Comitter:
- Ricky_Kwon
- Date:
- Wed Mar 09 08:23:53 2016 +0000
- Parent:
- 3:30b262b6c2f9
- Child:
- 5:709f1ad85e97
- Commit message:
- simple
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Mar 09 08:15:23 2016 +0000 +++ b/main.cpp Wed Mar 09 08:23:53 2016 +0000 @@ -1,26 +1,9 @@ #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 = 10000; -char* test=NULL; - SPISlave slave(PA_8, PA_7, PA_6, PA_5); // mosi, miso, sclk, ssel int main() { - - 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; - printf("SPI to Ethernet Slave\r\n"); /* @@ -28,94 +11,13 @@ */ 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) + if(slave.receive()) { - 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; - } - } - } + int SPI_re = slave.read(); // Read byte from master + printf("SPI_re=%c\r\n",SPI_re); } } } \ No newline at end of file