161010_bug fix multi byte read

Dependencies:   mbed

Fork of 161006_BDK_EEPROM by CUBEBITE

Branch:
KSS
Revision:
6:21b3128fbaef
Parent:
5:1ccdbfe5e4bb
Child:
7:45b16ef560a9
--- a/main.cpp	Thu Oct 06 00:17:28 2016 +0000
+++ b/main.cpp	Thu Oct 06 08:27:32 2016 +0000
@@ -12,6 +12,12 @@
 #define SPI_SCLK    PA_5
 #define SPI_SSEL    PA_4
 
+#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
 
 SPISlave spi_slave(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_SSEL); // MOSI, MISO, SCLK(CLK), SSEL(CS)=NC
 Serial pc_serial(USBTX, USBRX);
@@ -21,7 +27,7 @@
     PRINTD("Set the SPI SLAVE format\n");
     spi_slave.format(8,0); // setup 8bit 0 mode
     PRINTD("Set the SPI SLAVE frequency\n");
-    spi_slave.frequency(); // default 1MHz
+    spi_slave.frequency(1000000); // default 1MHz
 }
 
 void SPI_SlaveWrite()
@@ -70,8 +76,52 @@
     return;
 }
 
+void spi_dummy_reply(void)
+{
+    spi_slave.reply( 0xA0 );
+}
+
+#define EEPROM_SIZE 256
+char statusReg;
+char eepBuf[EEPROM_SIZE];
+
+void printBuf(char * buf, int length)
+{
+    pc_serial.printf("=======================[EEPROM]========================\n");
+    pc_serial.printf("      |");
+    for(int iCnt = 0 ; iCnt < 16 ; ++iCnt)
+    {
+        pc_serial.printf("%02X|", iCnt);
+    }
+    pc_serial.printf("\n");
+    
+    
+    for(int iCnt = 0 ; iCnt < length ; ++iCnt)
+    {
+        if(0 == ((iCnt) % 16))
+        {
+            pc_serial.printf("[0x%02X] ", iCnt);
+        }
+        
+        pc_serial.printf("%02X ", *(buf + iCnt));
+        
+        if(0 == ((iCnt+1) % 16))
+        {
+            pc_serial.printf("\n");
+            
+        }
+        
+    }
+    pc_serial.printf("=======================================================\n");
+}
+
 int main()
 {
+
+    
+     char eepAddr = 0;     
+     char eepData = 0;
+    
     char serialTxReadyFlag = 0;
     int spiRxTempBuf = 0;
     char spiRxBuf[255];
@@ -86,7 +136,12 @@
     int serialRxLen = 0;
     
     SPI_SlaveInit();
-    spi_slave.reply(0x00);     // Prime SPI with first reply
+    
+    // test code start
+    NVIC_SetVector( SPI_IRQn , ( uint32_t ) executa_spi_slave_hw ) ;
+    NVIC_SetPriority( SPI_IRQn , 2 ) ;
+    NVIC_EnableIRQ( SPI_IRQn ) ;
+    // test code end
 
     
     pc_serial.printf("\n\n========== KSS SPI Slave2 [Start] ==========\n");
@@ -96,6 +151,92 @@
         if(spi_slave.receive())     // wait SPI data input...
         {            
             spiRxTempBuf = spi_slave.read();
+            //spi_slave.reply( statusReg++ );
+            //continue;
+            
+            switch(spiRxTempBuf)
+            {
+                case EEPROM_WREN:
+                    //spi_dummy_reply();
+                    pc_serial.printf("[INFO] SPI_MOSI(RX) : WREN\n"); 
+                    statusReg = statusReg | (1<<1) ;
+                    pc_serial.printf("[INFO] WREN sucess [0x%02X]\n", statusReg);                     
+                break;
+                
+                case EEPROM_WRDI:
+                    //spi_dummy_reply();
+                    pc_serial.printf("[INFO] SPI_MOSI(RX) : WRDI\n"); 
+                    statusReg = statusReg & (~(1 << 1)) ;
+                    pc_serial.printf("[INFO] WRDI sucess [0x%02X]\n", statusReg);                     
+                break;
+
+                case EEPROM_RDSR:
+                    spi_slave.reply( statusReg );                    
+                    pc_serial.printf("[INFO] SPI_MOSI(RX) : RDSR\n");                     
+                    pc_serial.printf("[INFO] SPI_MISO(TX) : RDSR[0x%02X] \n", statusReg); 
+                break;
+                
+                
+                case EEPROM_WRITE:
+                    // EEPROM Address read..
+                    while(!spi_slave.receive());     
+                    eepAddr = spi_slave.read();
+                    
+                    // EEPROM Data read..
+                    while(!spi_slave.receive());     
+                    eepData = spi_slave.read();
+                    
+                    pc_serial.printf("\n[DEBUG] Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepData);    
+                    
+                    if(statusReg & 0x02)
+                    {
+                        statusReg |= 0x01;
+                        eepBuf[eepAddr] = eepData;
+                        printBuf(eepBuf, EEPROM_SIZE);           
+                        statusReg &= (~0x01);
+                        
+                    }
+                    else
+                    {
+                        pc_serial.printf("\nwrite command is disabled\n");        
+                    }                    
+                break;
+                
+                
+                case EEPROM_READ:
+                    // EEPROM Address read..
+                    while(!spi_slave.receive());     
+                    eepAddr = spi_slave.read();
+                    
+                    spi_slave.reply( eepBuf[eepAddr] );
+                    
+                    pc_serial.printf("\n[DEBUG] Addr[0x%02X]\n", eepAddr); 
+                    
+                    
+                    
+                    
+                break;
+                
+                
+                default:
+                //spi_dummy_reply();
+            }
+            
+            //pc_serial.printf("------------------------ end SPI Communication\n"); 
+            
+        }
+    }
+    
+    
+    
+    
+    
+    
+    
+    
+            
+            
+            /*
             spiRxBuf[spiRxTempCnt++] = spiRxTempBuf;
             
             if(0x00 == spiRxTempBuf)
@@ -105,16 +246,19 @@
                 spiRxLen = strlen(spiRxBuf);
                 spiRxTempCnt = 0;
             }
+            
         }
         if(1 == serialTxReadyFlag)
         {
             serialTxReadyFlag = 0;
-            pc_serial.printf("len=[%d] %s\n", spiRxLen, spiRxBuf);
+            pc_serial.printf("SPI_RX Data : len=[%d] %s\n", spiRxLen, spiRxBuf);
         }
         
+        */
+        
         
         
-        
+#if 0        
         /* TODO "serial -> slave ----(SPI)-----> " */
         
         if(0 != pc_serial.readable())       // wait serial input..
@@ -129,13 +273,13 @@
             PRINTD("spiTxReadyFlag=%d\n",spiTxReadyFlag);
             // SPI Send Start
             
-#if 0
+
             for(spiTxCnt = 0 ; spiTxCnt < serialRxLen + 1 ; ++spiTxCnt)
             {
                 //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]);                         
                 spi_slave.reply(serialRxBuf[spiTxCnt]);    
             } 
-#endif        
+
             for(spiTxCnt = 0 ; spiTxCnt < 1 ; ++spiTxCnt)
             {
                 //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]);
@@ -148,22 +292,9 @@
             spiTxReadyFlag = 0;
             PRINTD("spiTxReadyFlag =0\n");
         }
-        
+#endif                
      
         
-        
-        
-        
-        
-        
-        
-    }
-    
-    
-    
-    
-    
-