161006_BDK_SPImaster

Dependencies:   mbed

Fork of Nucleo_SPImaster_F042K6_KSS by CUBEBITE

Files at this revision

API Documentation at this revision

Comitter:
gandol2
Date:
Thu Oct 06 08:28:29 2016 +0000
Parent:
5:47353da6bca4
Commit message:
161006_implement master use cubebite spi eeprom

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 47353da6bca4 -r 50376de8d756 main.cpp
--- a/main.cpp	Thu Oct 06 00:11:07 2016 +0000
+++ b/main.cpp	Thu Oct 06 08:28:29 2016 +0000
@@ -1,4 +1,5 @@
 #include "mbed.h"
+#include <SPI.h>
 
 #define DEBUG_SPI
 
@@ -20,10 +21,10 @@
 
 void SPI_INIT()
 {
-    PRINTD("Set SPI init..\n");
-    PRINTD("Set SPI format..\n");
+    pc_serial.printf("Set SPI init..\n");
+    pc_serial.printf("Set SPI format..\n");
     spi_master.format(8,0);
-    PRINTD("Set frequency to default..\n");
+    pc_serial.printf("Set frequency to default..\n");
     spi_master.frequency(1000000); // default 1MHz
 }
 /*
@@ -84,21 +85,372 @@
 }
 */
 
+#define EEPROM_WREN     0x06        // Write Enable
+#define EEPROM_WRDI     0x04        // Write Disable
+#define EEPROM_RDSR     0x05        // Read Status Register
+#define EEPROM_WRSR     0x01        // Write Status Register
+#define EEPROM_READ     0x03        // Read EEPROM Memory
+#define EEPROM_WRITE    0x02        // Write EEPROM Memory
+
+
+enum cubeMenu { EEPROM_Write_Enable = 1 , 
+                EEPROM_Write_Disable,
+                EEPROM_Read_Status,
+                EEPROM_Write_Status,
+                EEPROM_Write_Memory,
+                EEPROM_Read_Memory,
+                TEST_Multi_Byte_Read,
+                EXIT_Program = 9
+                };
+
+void print_menu(void)
+{
+    pc_serial.printf("\n\n\n\n\n\n\n"); 
+     pc_serial.printf("-----------[MENU]----------\n");
+     pc_serial.printf("%d) Write Enable\n", cubeMenu.EEPROM_Write_Enable);
+     pc_serial.printf("%d) Write Disable\n", cubeMenu.EEPROM_Write_Disable);
+     pc_serial.printf("%d) Read Status Register\n", cubeMenu.EEPROM_Read_Status);
+     pc_serial.printf("%d) Write Status Register\n",cubeMenu.EEPROM_Write_Status);
+     pc_serial.printf("%d) Write Memory\n", cubeMenu.EEPROM_Write_Memory);
+     pc_serial.printf("%d) Read Memory\n", cubeMenu.EEPROM_Read_Memory);
+     pc_serial.printf("%d) multi byte read test\n", cubeMenu.TEST_Multi_Byte_Read);
+     pc_serial.printf("%d) Exit program\n", cubeMenu.EXIT_Program);
+     pc_serial.printf("---------------------------\n");
+     pc_serial.printf("input menu : ");
+}
+
+void spi_start(void)
+{
+    spi_master.lock();            
+    cs = 0; 
+    wait_us(0);
+}
+void spi_end(void)
+{
+    wait_us(10);
+    cs = 1;         
+    spi_master.unlock();
+}
+void spi_dummy_write(void)
+{
+    char dummy_read;
+    spi_start();
+    dummy_read = spi_master.write( 0x00 );
+    spi_end();
+}
+
+/*
+    @convertedData : converted Data
+    @sourceBuf : ASCII data array
+    @lenght : sourceBuf length
+    
+    @return : 0 is valid hex format, other is error
+*/
+int converHex(char * convertedData, char * sourceBuf, int length)
+{
+    int ret = 0;
+    char tempBuf[length];
+    *convertedData = 0;
+    
+    
+    for(int iCnt = 0 ; iCnt < length ; iCnt++)
+    {
+        if( ('0' <= *(sourceBuf+iCnt)) && ('9' >= *(sourceBuf+iCnt)) )
+        {
+            tempBuf[iCnt] = (*(sourceBuf+iCnt) - 48); 
+        }
+        else if( ('A' <= *(sourceBuf+iCnt)) && ('F' >= *(sourceBuf+iCnt)) )
+        {
+            tempBuf[iCnt] = (*(sourceBuf+iCnt) - 55); 
+        }
+        else if( ('a' <= *(sourceBuf+iCnt)) && ('f' >= *(sourceBuf+iCnt)) )
+        {          
+            tempBuf[iCnt] = (*(sourceBuf+iCnt) - 87); 
+        }
+        else    // error
+        {
+            ret = -1;
+            return ret;                    
+        }  
+    }
+    
+    //pc_serial.printf("[TEST] tempBuf[0] = 0x%02X\n", tempBuf[0]);
+    //pc_serial.printf("[TEST] tempBuf[1] = 0x%02X\n", tempBuf[1]);
+    *convertedData = ((tempBuf[0] & 0x0F) << 4) | (tempBuf[1] & 0x0F);
+    //pc_serial.printf("[TEST] convertedData = 0x%02X\n", *convertedData);
+    return ret;
+}
 
 // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ main();
 int main()
 {
+     char dummyRead;
+     char inputMenuNum = 0;
+     char statusReg = 0x00;
+     
+     
      int spiTxCnt = 0;
      int spiRxCnt = 0; 
      char spiTxReadyFlag = 0;
+     
+     
      int res;
+     
+     
      int spiRxTempBuf = 0;
      char serialRxBuf[255];
      char spiRxBuf[255];
      int serialRxLen = 0;
      
+     char eepAddrBuf[2] = {0};
+     char eepAddr = 0;
      
-     pc_serial.printf("\n\n========== KSS SPI Master [Start] ==========\n");
+     char eepDataBuf[2] = {0};
+     char eepData = 0;
+     int eepReadData = 0;   
+     int maxInputCnt = 0;
+     
+     
+     
+     int ret;
+     
+     SPI_INIT();
+     spi_dummy_write();
+     
+     
+     
+     
+     pc_serial.printf("\n\n========== SPI Master for EEPROM [Start] ==========\n");
+     
+     
+     while(1)
+     {
+         print_menu();
+         inputMenuNum = pc_serial.getc();
+         
+         pc_serial.printf("[DEBUG] input menu number = %c\n", inputMenuNum);
+         
+         if( ('0' < inputMenuNum) && ('9' >= inputMenuNum) )
+         {
+            inputMenuNum = inputMenuNum - 48;
+         }
+         else
+         {
+             pc_serial.printf("[INFO] correct input menu number.. please retry..\n");
+             wait(1);
+             continue;
+         }
+         
+                  
+         
+         spi_start();
+         switch(inputMenuNum)
+         {
+             case 1:        // Write Enable =================================[OK]
+                pc_serial.printf("[DEBUG] Write Enable progress..\n");
+                dummyRead = spi_master.write(EEPROM_WREN);            
+             break;
+             
+             
+             
+             case 2:        // Write Disable =================================[OK]             
+                pc_serial.printf("[DEBUG] Write Disable progress..\n");
+                dummyRead = spi_master.write(EEPROM_WRDI);                
+             break;
+             
+             
+             
+             case 3:        // Read Status Register =================================[OK]
+                pc_serial.printf("[DEBUG] Read Status Register progress..\n");
+                spi_master.write(EEPROM_RDSR);  
+                statusReg = spi_master.write(EEPROM_RDSR);     
+                
+                // TODO : print statusReg data to serial
+                pc_serial.printf("[INFO] Status Register Data : 0x%02X\n", statusReg);
+                                
+             break;
+             
+             
+             
+             
+             
+             case 4:        // Write Status Register =================================
+                pc_serial.printf("[DEBUG] Write Status Register progress..\n");
+                // TODO : input data for serial
+                
+                    spi_master.write(EEPROM_WRSR);
+                    spi_master.write(statusReg);
+             break;
+             
+             
+             
+             
+             
+             case 5:        // Write Memory =================================
+                pc_serial.printf("[DEBUG] Write Memory progress..\n");
+                
+                
+                
+                
+                // EEPROM Address input
+                
+                maxInputCnt = 3;
+                while(maxInputCnt--)
+                {
+                    pc_serial.printf("input Address (hex format : 00~FF) : ");
+                    for(int iCnt = 0 ; iCnt < 2 ; ++iCnt)                
+                    {   
+                        eepAddrBuf[iCnt] = pc_serial.getc();    
+                        pc_serial.printf("%c", eepAddrBuf[iCnt]); 
+                    }
+                    
+                    ret = converHex(&eepAddr , eepAddrBuf, 2);
+                    
+                    if(0 == ret)
+                    {
+                        //pc_serial.printf("\n[DEBUG] before convert hex [0x%02x] \n", eepAddr);
+                        break;
+                    }
+                    else        // error
+                    {
+                        pc_serial.printf("\n[INFO] hex formet error!\n");
+                        continue;
+                    }                     
+                }
+                
+                
+                if(0 == ret)
+                {
+                    // EEPROM data input
+                    maxInputCnt = 3;
+                    while(maxInputCnt--)
+                    {
+                        pc_serial.printf("\ninput Data (hex format : 00~FF) : ");
+                        for(int iCnt = 0 ; iCnt < 2 ; ++iCnt)                
+                        {   
+                            eepDataBuf[iCnt] = pc_serial.getc();    
+                            pc_serial.printf("%c", eepDataBuf[iCnt]); 
+                        }
+                        
+                        int ret = converHex(&eepData , eepDataBuf, 2);
+                        if(0 == ret)
+                        {
+                            
+                            pc_serial.printf("\n[INFO] SPI Write Memory.... Start! \n");
+                            //pc_serial.printf("\n[DEBUG] Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepData);
+                            
+                            spi_master.write(EEPROM_RDSR);  
+                            if(! (spi_master.write(EEPROM_RDSR) & 0x01) )
+                            {                           
+                                spi_master.write(EEPROM_WRITE);
+                                spi_master.write(eepAddr);
+                                spi_master.write(eepData);
+                                pc_serial.printf("\n[INFO] SPI Write success \n");
+                            }
+                            else
+                            {
+                                pc_serial.printf("\n[INFO] SPI Write fail.. device is busy! \n");
+                            }
+                            
+                            
+                            break;
+                        }
+                        else        // error
+                        {
+                            pc_serial.printf("\n[INFO] hex formet error!\n");
+                            continue;
+                        }            
+                    }
+                }
+                
+                
+             break;
+             
+             
+             
+             
+             
+             case 6:        // Read Memory =================================
+                pc_serial.printf("[DEBUG] Read Memory progress..\n");
+                
+                // EEPROM Address input
+                
+                maxInputCnt = 3;
+                while(maxInputCnt--)
+                {
+                    pc_serial.printf("input Address (hex format : 00~FF) : ");
+                    for(int iCnt = 0 ; iCnt < 2 ; ++iCnt)                
+                    {   
+                        eepAddrBuf[iCnt] = pc_serial.getc();    
+                        pc_serial.printf("%c", eepAddrBuf[iCnt]); 
+                    }
+                    
+                    ret = converHex(&eepAddr , eepAddrBuf, 2);
+                    
+                    if(0 == ret)
+                    {
+                        //pc_serial.printf("\n[DEBUG] before convert hex [0x%02x] \n", eepAddr);
+                        spi_master.write(EEPROM_READ);
+                        spi_master.write(eepAddr);
+                        
+                        
+                        eepReadData = spi_master.write(0);
+                        
+                        
+                        pc_serial.printf("\n[DEBUG] Read EEPROM Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepReadData);
+                        break;
+                    }
+                    else        // error
+                    {
+                        pc_serial.printf("\n[INFO] hex formet error!\n");
+                        continue;
+                    }                     
+                }
+                
+                
+                
+             break;
+             
+             
+             
+             
+             
+             case 9:
+                spi_end();
+                pc_serial.printf("exit program... thank you\n");
+                //return 0;   
+             default:             
+         }
+         spi_end();
+        
+     }
+     
+
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
+     
      
      
      while(1)
@@ -118,9 +470,10 @@
             for(spiTxCnt = 0 ; spiTxCnt < serialRxLen + 1 ; ++spiTxCnt)
             {
                 //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]);                         
-                res=spi_master.write(serialRxBuf[spiTxCnt]);
-                PRINTD("res=%c 0x[%02x]\n",res,res);
-                spiRxBuf[spiRxCnt++]=res;
+                spi_master.write(serialRxBuf[spiTxCnt]);
+                //res=spi_master.write(serialRxBuf[spiTxCnt]);
+                //PRINTD("res=%c 0x[%02x]\n",res,res);
+                //spiRxBuf[spiRxCnt++]=res;
             }   
             cs = 1;         
             spi_master.unlock();
@@ -141,14 +494,6 @@
 #endif
         
         
-        
-        
-        
-        
-        
-        
-        
-        
      }