161005_F0426K_SPIMaster_KSS

Dependencies:   mbed

Fork of Nucleo_SPImaster_F042K6 by CUBEBITE

Files at this revision

API Documentation at this revision

Comitter:
gandol2
Date:
Wed Oct 05 08:58:51 2016 +0000
Parent:
3:c6f8271b2d4e
Commit message:
161005_F0426K_SPIMaster

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Oct 05 07:11:05 2016 +0000
+++ b/main.cpp	Wed Oct 05 08:58:51 2016 +0000
@@ -6,18 +6,27 @@
 #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);
+
+#define SPI_MOSI    PA_7
+#define SPI_MISO    PA_6
+#define SPI_SCLK    PA_5
+#define SPI_SSEL    PA_4
+
+SPI spi_master(SPI_MOSI,SPI_MISO,SPI_SCLK); // MOSI, MISO, SCLK(CLK,SCK)
+DigitalOut cs(SPI_SSEL);
+
+Serial pc_serial(USBTX, USBRX);
+
 
 void SPI_INIT()
 {
     PRINTD("Set SPI init..\n");
     PRINTD("Set SPI format..\n");
-    spi.format(8,0);
+    spi_master.format(8,0);
     PRINTD("Set frequency to default..\n");
-    spi.frequency(1000000); // default 1MHz
+    spi_master.frequency(1000000); // default 1MHz
 }
-
+/*
 void SPI_Write()
 {
     char temp;
@@ -45,18 +54,18 @@
                 PRINTD("%c[%02x]",tx_buffer[i],tx_buffer[i]);
             }
             PRINTD("\n\n");
-            spi.lock();
+            spi_master.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);
+                response=spi_master.write(value);
                 cs=-1;
                 PRINTD("[M]receive=%c[%x]\n",response,response);
                 rx_buffer[++rx_cnt]=response;
             }
-            spi.unlock();
+            spi_master.unlock();
             for(i=0;i<255;++i)
             {
                 tx_buffer[i]=0;
@@ -73,14 +82,81 @@
         }
     }
 }
+*/
 
+
+// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ main();
 int main()
 {
+     int spiTxCnt = 0;     
+     char spiTxReadyFlag = 0;
+     
+     char serialRxBuf[255];
+     int serialRxLen = 0;
+     
+     pc_serial.printf("\n\n========== KSS SPI Master [Start] ==========\n");
+     
+     
+     while(1)
+     {        
+        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
+            spi_master.lock();            
+            cs = 0;   
+            for(spiTxCnt = 0 ; spiTxCnt < serialRxLen + 1 ; ++spiTxCnt)
+            {
+                //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]);                         
+                spi_master.write(serialRxBuf[spiTxCnt]);    
+            }   
+            cs = 1;         
+            spi_master.unlock();
+            spiTxReadyFlag = 0;
+        }
+        
+        
+        
+        /* TODO " ----(SPI)-----> master --> serial "
+        
+        */
+        
+     }
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+#if 0   // 161005_BDK_backup2 start
     int send_data;
-    SPI_INIT();    
-   
+    SPI_INIT();   
     while(1)
     {
         SPI_Write();   
     }
+#endif  // 161005_BDK_backup2 end
+    return 0;
 }