test

Dependencies:   mbed

Fork of 161006_BDK_SPIslave2 by CUBEBITE

Revision:
3:4bb95aa46ebc
Parent:
2:5311ad7c83e6
Child:
4:cc97d7a33efc
--- a/main.cpp	Wed Oct 05 07:09:51 2016 +0000
+++ b/main.cpp	Wed Oct 05 08:59:53 2016 +0000
@@ -6,15 +6,22 @@
 #define PRINTD(arg1,arg2...)    printf(arg1,##arg2)
 #endif
 
-SPISlave spislave(PA_7, PA_6, PA_5, PA_4); // MOSI, MISO, SCLK(CLK), SSEL(CS)=NC
-Serial pc(USBTX, USBRX);
+
+#define SPI_MOSI    PA_7
+#define SPI_MISO    PA_6
+#define SPI_SCLK    PA_5
+#define SPI_SSEL    PA_4
+
+
+SPISlave spi_slave(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_SSEL); // MOSI, MISO, SCLK(CLK), SSEL(CS)=NC
+Serial pc_serial(USBTX, USBRX);
 
 void SPI_SlaveInit()
 {
     PRINTD("Set the SPI SLAVE format\n");
-    spislave.format(8,0); // setup 8bit 0 mode
+    spi_slave.format(8,0); // setup 8bit 0 mode
     PRINTD("Set the SPI SLAVE frequency\n");
-    spislave.frequency(); // default 1MHz
+    spi_slave.frequency(); // default 1MHz
 }
 
 void SPI_SlaveWrite()
@@ -44,7 +51,7 @@
             {
                 value=tx_buffer[i];
                 PRINTD("write[%d]=%c[%02x]\n",i,value,value);
-                spislave.reply(value);
+                spi_slave.reply(value);
             }
             for(i=0;i<tx_cnt;++i)
             {
@@ -65,6 +72,93 @@
 
 int main()
 {
+    char serialTxReadyFlag = 0;
+    int spiRxTempBuf = 0;
+    char spiRxBuf[255];
+    int spiRxTempCnt = 0;
+    int spiRxLen = 0;
+    
+    
+    int spiTxCnt = 0;    
+    char spiTxReadyFlag = 0;
+    
+    char serialRxBuf[255];
+    int serialRxLen = 0;
+    
+    SPI_SlaveInit();
+    spi_slave.reply(0x00);     // Prime SPI with first reply
+
+    
+    pc_serial.printf("\n\n========== KSS SPI Slave [Start] ==========\n");
+    
+    while(1)
+    {
+        if(spi_slave.receive())     // wait SPI data input...
+        {            
+            spiRxTempBuf = spi_slave.read();
+            spiRxBuf[spiRxTempCnt++] = spiRxTempBuf;
+            
+            if(0x00 == spiRxTempBuf)
+            {                
+                serialTxReadyFlag = 1;                
+                //pc_serial.printf("SPI Recive Byte [%d]\n", spiRxTempCnt-1);
+                spiRxLen = strlen(spiRxBuf);
+                spiRxTempCnt = 0;
+            }
+        }
+        if(1 == serialTxReadyFlag)
+        {
+            serialTxReadyFlag = 0;
+            pc_serial.printf("len=[%d] %s\n", spiRxLen, spiRxBuf);
+        }
+        
+        
+        
+        
+        /* TODO "serial -> slave ----(SPI)-----> "
+        
+        if(0 != pc_serial.readable())       // wait serial input..
+        {   
+            pc_serial.scanf("%s", serialRxBuf);
+            serialRxLen = strlen(serialRxBuf);
+            pc_serial.printf("len=[%d] %s\n", serialRxLen, serialRxBuf);
+            spiTxReadyFlag = 1;
+        }        
+        if(1 == spiTxReadyFlag)
+        {
+            // SPI Send Start
+            
+            
+            for(spiTxCnt = 0 ; spiTxCnt < serialRxLen + 1 ; ++spiTxCnt)
+            {
+                //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]);                         
+                spi_slave.reply(serialRxBuf[spiTxCnt]);    
+            }   
+            
+            
+            spiTxReadyFlag = 0;
+        }
+        
+        */
+        
+        
+        
+        
+        
+        
+        
+        
+    }
+    
+    
+    
+    
+    
+    
+    
+    
+    
+#if 0   // 161005_BDK_slave backup start
     int i;
     char valueFromMaster;
     char rx_buffer[255]={0};
@@ -75,7 +169,7 @@
     while(1)
     {
 
-        if(spislave.receive())
+        if(spi_slave.receive())
         { 
             PRINTD("----1\n");
             if(pc.readable())
@@ -83,7 +177,7 @@
                 SPI_SlaveWrite();
             }
             PRINTD("----2\n");
-            valueFromMaster = spislave.read();
+            valueFromMaster = spi_slave.read();
             PRINTD("----3\n");
             //PRINTD("valueFromMaster=");
             //PRINTD("[%c]%x\n",valueFromMaster,valueFromMaster);
@@ -105,9 +199,10 @@
                
             }//valueFromMaster if
             
-        }// spislave.receive() if
+        }// spi_slave.receive() if
      /*
                 
             */
     }//end of while
+#endif  // 161005_BDK_slave backup end
 }//end of main