161006_BDK_SPIslave

Dependencies:   mbed

Fork of Nucleo_SPIslave_F303K8_KSS by CUBEBITE

Revision:
2:5311ad7c83e6
Parent:
1:3a338e553a54
Child:
3:4bb95aa46ebc
--- a/main.cpp	Wed Oct 05 07:04:31 2016 +0000
+++ b/main.cpp	Wed Oct 05 07:09:51 2016 +0000
@@ -6,36 +6,31 @@
 #define PRINTD(arg1,arg2...)    printf(arg1,##arg2)
 #endif
 
-SPI spi(PA_7,PA_6,PA_5); // MOSI, MISO, SCLK(CLK,SCK)
-DigitalOut cs(PA_4);
+SPISlave spislave(PA_7, PA_6, PA_5, PA_4); // MOSI, MISO, SCLK(CLK), SSEL(CS)=NC
+Serial pc(USBTX, USBRX);
 
-void SPI_INIT()
+void SPI_SlaveInit()
 {
-    PRINTD("Set SPI init..\n");
-    PRINTD("Set SPI format..\n");
-    spi.format(8,0);
-    PRINTD("Set frequency to default..\n");
-    spi.frequency(1000000); // default 1MHz
+    PRINTD("Set the SPI SLAVE format\n");
+    spislave.format(8,0); // setup 8bit 0 mode
+    PRINTD("Set the SPI SLAVE frequency\n");
+    spislave.frequency(); // default 1MHz
 }
 
-void SPI_Write()
+void SPI_SlaveWrite()
 {
+    char tx_buffer[255]={0};
+    char reply;
+    char i;
     char temp;
-    int i,value;
-    char response;
-    char tx_cnt = -1;
-    char rx_cnt = -1;
-    char tx_buffer[255]={0};
-    char rx_buffer[255]={0};
-    PRINTD("\n==========MASTER==========\n");
-    PRINTD("DATA SEND START...\n");
-    PRINTD("Lock SPI BUS..\n");
-
+    char tx_cnt = 0;
+    int value;
+    PRINTD("Input Strging=");
     while(1)
     {
         
         temp=getchar();
-        tx_buffer[++tx_cnt]=temp;
+        tx_buffer[tx_cnt++]=temp;
         if(temp==0x0d)
         {
             tx_buffer[tx_cnt]=0;
@@ -45,42 +40,74 @@
                 PRINTD("%c[%02x]",tx_buffer[i],tx_buffer[i]);
             }
             PRINTD("\n\n");
-            spi.lock();
             for(i=0;i<=tx_cnt;++i)
             {
                 value=tx_buffer[i];
-                PRINTD("[M]write[%d]=%c[%02x]\n",i,value,value);
-                cs=0;
-                response=spi.write(value);
-                cs=-1;
-                PRINTD("[M]receive=%c[%x]\n",response,response);
-                rx_buffer[++rx_cnt]=response;
+                PRINTD("write[%d]=%c[%02x]\n",i,value,value);
+                spislave.reply(value);
             }
-            spi.unlock();
-            for(i=0;i<255;++i)
+            for(i=0;i<tx_cnt;++i)
             {
                 tx_buffer[i]=0;
+                PRINTD("init_tx_buf[%d]=%c\n",i,tx_buffer[i]);
             }
-            for(i=0;i<=tx_cnt;i++)
-            {
-                PRINTD("init_tx_buffer[%d]=%c\n",i,tx_buffer[i]);
-            }
-            tx_cnt=-1;
+            tx_cnt=0;
+            PRINTD("break\n");
+            break;
         }
         else
         {
             PRINTD("%c[%02x]",tx_buffer[tx_cnt],tx_buffer[tx_cnt]);
         }
     }
+    return;
 }
 
 int main()
 {
-    int send_data;
-    SPI_INIT();    
-   
+    int i;
+    char valueFromMaster;
+    char rx_buffer[255]={0};
+    char rx_cnt = -1;
+    PRINTD("\n=========SLAVE=========\n");
+    SPI_SlaveInit();
+    
     while(1)
     {
-        SPI_Write();   
-    }
-}
+
+        if(spislave.receive())
+        { 
+            PRINTD("----1\n");
+            if(pc.readable())
+            {
+                SPI_SlaveWrite();
+            }
+            PRINTD("----2\n");
+            valueFromMaster = spislave.read();
+            PRINTD("----3\n");
+            //PRINTD("valueFromMaster=");
+            //PRINTD("[%c]%x\n",valueFromMaster,valueFromMaster);
+            rx_buffer[++rx_cnt]=valueFromMaster;
+            if(valueFromMaster==0)
+            {   
+                PRINTD("rx_string=");
+                for(i=0;i<rx_cnt;i++) 
+                {    
+                    PRINTD("%c",rx_buffer[i]);
+                }
+                PRINTD("\n");
+                for(i=0;i<=rx_cnt;i++)
+                {
+                    rx_buffer[i]=0;
+                    PRINTD("Init_rx_buf[%d]=%c\n",i,rx_buffer[i]);
+                }
+                rx_cnt=-1;
+               
+            }//valueFromMaster if
+            
+        }// spislave.receive() if
+     /*
+                
+            */
+    }//end of while
+}//end of main