SPI Flash AT45DBXXXD

Fork of at45db161d by Suga koubou

Revision:
5:ef7247c6f073
Parent:
4:943690efda8b
Child:
6:1872f591d604
diff -r 943690efda8b -r ef7247c6f073 at45db161d.cpp
--- a/at45db161d.cpp	Fri Mar 06 09:03:45 2015 +0800
+++ b/at45db161d.cpp	Fri Mar 06 09:18:42 2015 +0800
@@ -10,46 +10,22 @@
  */
 #include "at45db161d.h"
 
-/** CTOR **/
-ATD45DB161D::ATD45DB161D(PinName mosi, PinName miso, PinName sclk, PinName cs)
-  : _spi(mosi, miso, sclk), _cs(cs)
-{}
-
-ATD45DB161D::ATD45DB161D(SPI &spi, PinName cs)
+ATD45DB161D::ATD45DB161D(SPI *spi, PinName cs)
   : _spi(spi), _cs(cs)
 {
     _bytes = 264;
     _pages = 4095;
 }
 
-/** DTOR **/
-ATD45DB161D::~ATD45DB161D()
-{}
-
 /** Setup SPI and pinout **/
 void ATD45DB161D::Init()
 {
-//    uint8_t clr;
-    
-    /* Initialize pinout */
-/*
-    pinMode(DATAOUT, OUTPUT);
-    pinMode(DATAIN, INPUT);
-    pinMode(SPICLOCK, OUTPUT);
-    pinMode(SLAVESELECT, OUTPUT);
-    pinMode(DATAIN, INPUT);
-*/  
       /* Disable device */
       DF_CS_inactive;
   
     /* Setup SPI */
-//    SPCR = (1 << SPE) | (1 << MSTR) | (1 << CPOL) | (1 << CPHA);
-    _spi.format(8, 0);
-    _spi.frequency(10000000);
-
-    /* Cleanup registers */
-//    clr = SPSR;
-//    clr = SPDR;
+    _spi->format(8, 0);
+    _spi->frequency(10000000);
 }
 
 /** 
@@ -64,9 +40,9 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
   
     /* Send status read command */
-    spi_transfer(AT45DB161D_STATUS_REGISTER_READ);
+    _spi->write(AT45DB161D_STATUS_REGISTER_READ);
     /* Get result with a dummy write */
-    status = spi_transfer(0x00);
+    status = _spi->write(0x00);
 
     return status;
 }
@@ -74,7 +50,7 @@
 /** 
  * Read Manufacturer and Device ID 
  * @note if id.extendedInfoLength is not equal to zero,
- *       successive calls to spi_transfer(0xff) will return
+ *       successive calls to _spi->write(0xff) will return
  *       the extended device information string bytes.
  * @param id Pointer to the ID structure to initialize
  **/
@@ -85,16 +61,16 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
   
     /* Send status read command */
-    spi_transfer(AT45DB161D_READ_MANUFACTURER_AND_DEVICE_ID);
+    _spi->write(AT45DB161D_READ_MANUFACTURER_AND_DEVICE_ID);
 
     /* Manufacturer ID */
-    id->manufacturer = spi_transfer(0xff);
+    id->manufacturer = _spi->write(0xff);
     /* Device ID (part 1) */
-    id->device[0] = spi_transfer(0xff);
+    id->device[0] = _spi->write(0xff);
     /* Device ID (part 2) */
-    id->device[1] = spi_transfer(0xff);
+    id->device[1] = _spi->write(0xff);
     /* Extended Device Information String Length */
-    id->extendedInfoLength = spi_transfer(0xff);
+    id->extendedInfoLength = _spi->write(0xff);
     
 }
 
@@ -113,18 +89,18 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
 
     /* Send opcode */
-    spi_transfer(AT45DB161D_PAGE_READ);
+    _spi->write(AT45DB161D_PAGE_READ);
     
     /* Address (page | offset)  */
-    spi_transfer((uint8_t)(page >> 6));
-    spi_transfer((uint8_t)((page << 2) | (offset >> 8)));
-    spi_transfer((uint8_t)(offset & 0xff));
+    _spi->write((uint8_t)(page >> 6));
+    _spi->write((uint8_t)((page << 2) | (offset >> 8)));
+    _spi->write((uint8_t)(offset & 0xff));
     
     /* 4 "don't care" bytes */
-    spi_transfer(0x00);
-    spi_transfer(0x00);
-    spi_transfer(0x00);
-    spi_transfer(0x00);
+    _spi->write(0x00);
+    _spi->write(0x00);
+    _spi->write(0x00);
+    _spi->write(0x00);
 }
 
 /** 
@@ -141,15 +117,15 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
 
     /* Send opcode */
-    spi_transfer( low ? AT45DB161D_CONTINUOUS_READ_LOW_FREQ :
+    _spi->write( low ? AT45DB161D_CONTINUOUS_READ_LOW_FREQ :
                         AT45DB161D_CONTINUOUS_READ_HIGH_FREQ );
 
     /* Address (page | offset)  */
-    spi_transfer((uint8_t)(page >> 6));
-    spi_transfer((uint8_t)((page << 2) | (offset >> 8)));
-    spi_transfer((uint8_t)(offset & 0xff));
+    _spi->write((uint8_t)(page >> 6));
+    _spi->write((uint8_t)((page << 2) | (offset >> 8)));
+    _spi->write((uint8_t)(offset & 0xff));
     
-    if (!low) { spi_transfer(0x00); }
+    if (!low) { _spi->write(0x00); }
 }
 
 void ATD45DB161D::read(uint16_t addr, void *ptr, uint16_t len) {
@@ -158,7 +134,7 @@
     PageToBuffer(addr / _bytes, 1);
     BufferRead(1, addr % _bytes, 1);
     for (i = 0; i < len; i++) {
-        buf[i] = _spi.write(0xff);
+        buf[i] = _spi->write(0xff);
     }
 }
 
@@ -169,7 +145,7 @@
         BufferWrite(2, addr % _bytes);
         uint16_t wlen = (_bytes < len ? _bytes : len);
         for (i = 0; i < wlen; i++) {
-            _spi.write(*buf++);
+            _spi->write(*buf++);
         }
         len -= wlen;
         BufferToPage(2, addr / _bytes, 1);
@@ -191,22 +167,22 @@
     /* Send opcode */
     if(bufferNum == 1)
     {
-        spi_transfer(low ? AT45DB161D_BUFFER_1_READ_LOW_FREQ :
+        _spi->write(low ? AT45DB161D_BUFFER_1_READ_LOW_FREQ :
                            AT45DB161D_BUFFER_1_READ);
     }
     else
     {
-        spi_transfer(low ? AT45DB161D_BUFFER_2_READ_LOW_FREQ :
+        _spi->write(low ? AT45DB161D_BUFFER_2_READ_LOW_FREQ :
                            AT45DB161D_BUFFER_2_READ);
 
     }
     
     /* 14 "Don't care" bits */
-    spi_transfer(0x00);
+    _spi->write(0x00);
     /* Rest of the "don't care" bits + bits 8,9 of the offset */
-    spi_transfer((uint8_t)(offset >> 8));
+    _spi->write((uint8_t)(offset >> 8));
     /* bits 7-0 of the offset */
-    spi_transfer((uint8_t)(offset & 0xff));
+    _spi->write((uint8_t)(offset & 0xff));
 }
 
 /** 
@@ -223,15 +199,15 @@
     DF_CS_inactive;    /* Make sure to toggle CS signal in order */
     DF_CS_active;      /* to reset Dataflash command decoder     */
 
-    spi_transfer( (bufferNum == 1) ? AT45DB161D_BUFFER_1_WRITE :
+    _spi->write( (bufferNum == 1) ? AT45DB161D_BUFFER_1_WRITE :
                                      AT45DB161D_BUFFER_2_WRITE);
     
     /* 14 "Don't care" bits */
-    spi_transfer(0x00);
+    _spi->write(0x00);
     /* Rest of the "don't care" bits + bits 8,9 of the offset */
-    spi_transfer((uint8_t)(offset >> 8));
+    _spi->write((uint8_t)(offset >> 8));
     /* bits 7-0 of the offset */
-    spi_transfer((uint8_t)(offset & 0xff));
+    _spi->write((uint8_t)(offset & 0xff));
 }
 
 /**
@@ -249,12 +225,12 @@
     /* Opcode */
     if(erase)
     {
-        spi_transfer( (bufferNum == 1) ? AT45DB161D_BUFFER_1_TO_PAGE_WITH_ERASE :
+        _spi->write( (bufferNum == 1) ? AT45DB161D_BUFFER_1_TO_PAGE_WITH_ERASE :
                                          AT45DB161D_BUFFER_2_TO_PAGE_WITH_ERASE);
     }
     else
     {
-        spi_transfer( (bufferNum == 1) ? AT45DB161D_BUFFER_1_TO_PAGE_WITHOUT_ERASE :
+        _spi->write( (bufferNum == 1) ? AT45DB161D_BUFFER_1_TO_PAGE_WITHOUT_ERASE :
                                          AT45DB161D_BUFFER_2_TO_PAGE_WITHOUT_ERASE);
     }
     
@@ -265,9 +241,9 @@
      *       the main memory to be written
      *     - 10 don&#65533;ft care bits
      */
-    spi_transfer((uint8_t)(page >> 6));
-    spi_transfer((uint8_t)(page << 2));
-    spi_transfer(0x00);
+    _spi->write((uint8_t)(page >> 6));
+    _spi->write((uint8_t)(page << 2));
+    _spi->write(0x00);
     
     DF_CS_inactive;  /* Start transfer */
     DF_CS_active;    /* If erase was set, the page will first be erased */
@@ -288,7 +264,7 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
  
     /* Send opcode */
-    spi_transfer((bufferNum == 1) ? AT45DB161D_TRANSFER_PAGE_TO_BUFFER_1 :
+    _spi->write((bufferNum == 1) ? AT45DB161D_TRANSFER_PAGE_TO_BUFFER_1 :
                                     AT45DB161D_TRANSFER_PAGE_TO_BUFFER_2);
 
     /*
@@ -298,9 +274,9 @@
      *       the main memory to be written
      *     - 10 don&#65533;ft care bits
      */
-    spi_transfer((uint8_t)(page >> 6));
-    spi_transfer((uint8_t)(page << 2));
-    spi_transfer(0x00);
+    _spi->write((uint8_t)(page >> 6));
+    _spi->write((uint8_t)(page << 2));
+    _spi->write(0x00);
         
     DF_CS_inactive;  /* Start page transfer */
     DF_CS_active;
@@ -320,7 +296,7 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
 
     /* Send opcode */
-    spi_transfer(AT45DB161D_PAGE_ERASE);
+    _spi->write(AT45DB161D_PAGE_ERASE);
     
     /*
      * 3 address bytes consist of :
@@ -329,9 +305,9 @@
      *       the main memory to be written
      *     - 10 don&#65533;ft care bits
      */
-    spi_transfer((uint8_t)(page >> 6));
-    spi_transfer((uint8_t)(page << 2));
-    spi_transfer(0x00);
+    _spi->write((uint8_t)(page >> 6));
+    _spi->write((uint8_t)(page << 2));
+    _spi->write(0x00);
         
     DF_CS_inactive;  /* Start block erase */
     DF_CS_active;
@@ -351,7 +327,7 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
 
     /* Send opcode */
-    spi_transfer(AT45DB161D_BLOCK_ERASE);
+    _spi->write(AT45DB161D_BLOCK_ERASE);
     
     /*
      * 3 address bytes consist of :
@@ -359,9 +335,9 @@
      *     - 9 block address bits (PA11 - PA3)
      *     - 13 don&#65533;ft care bits
      */
-    spi_transfer((uint8_t)(block >> 3));
-    spi_transfer((uint8_t)(block << 5));
-    spi_transfer(0x00);
+    _spi->write((uint8_t)(block >> 3));
+    _spi->write((uint8_t)(block << 5));
+    _spi->write(0x00);
         
     DF_CS_inactive;  /* Start block erase */
     DF_CS_active;
@@ -382,7 +358,7 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
 
     /* Send opcode */
-    spi_transfer(AT45DB161D_SECTOR_ERASE);
+    _spi->write(AT45DB161D_SECTOR_ERASE);
     
     /*
      * 3 address bytes consist of :
@@ -394,9 +370,9 @@
          *  - 
          *  - 12 don&#65533;ft care bits
          */
-        spi_transfer(0x00);
-        spi_transfer(((sector & 0x01) << 4));
-        spi_transfer(0x00);
+        _spi->write(0x00);
+        _spi->write(((sector & 0x01) << 4));
+        _spi->write(0x00);
     }
     else
     {
@@ -405,9 +381,9 @@
          *  - 4 sector number bits
          *  - 18 don't care bits 
          */
-        spi_transfer(sector << 1);
-        spi_transfer(0x00);
-        spi_transfer(0x00);
+        _spi->write(sector << 1);
+        _spi->write(0x00);
+        _spi->write(0x00);
     }
                 
     DF_CS_inactive;  /* Start block erase */
@@ -428,10 +404,10 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
 
     /* Send chip erase sequence */
-    spi_transfer(AT45DB161D_CHIP_ERASE_0);
-    spi_transfer(AT45DB161D_CHIP_ERASE_1);
-    spi_transfer(AT45DB161D_CHIP_ERASE_2);
-    spi_transfer(AT45DB161D_CHIP_ERASE_3);
+    _spi->write(AT45DB161D_CHIP_ERASE_0);
+    _spi->write(AT45DB161D_CHIP_ERASE_1);
+    _spi->write(AT45DB161D_CHIP_ERASE_2);
+    _spi->write(AT45DB161D_CHIP_ERASE_3);
                 
     DF_CS_inactive;  /* Start chip erase */
     DF_CS_active;
@@ -456,13 +432,13 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
 
     /* Send opcode */
-    spi_transfer((bufferNum == 1) ? AT45DB161D_PAGE_THROUGH_BUFFER_1 :
+    _spi->write((bufferNum == 1) ? AT45DB161D_PAGE_THROUGH_BUFFER_1 :
                                     AT45DB161D_PAGE_THROUGH_BUFFER_2);
 
     /* Address */
-    spi_transfer((uint8_t)(page >> 6));
-    spi_transfer((uint8_t)((page << 2) | (offset >> 8)));
-    spi_transfer((uint8_t)offset);
+    _spi->write((uint8_t)(page >> 6));
+    _spi->write((uint8_t)((page << 2) | (offset >> 8)));
+    _spi->write((uint8_t)offset);
 }
 
 /**
@@ -499,13 +475,13 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
 
     /* Send opcode */
-    spi_transfer((bufferNum == 1) ? AT45DB161D_COMPARE_PAGE_TO_BUFFER_1 :
+    _spi->write((bufferNum == 1) ? AT45DB161D_COMPARE_PAGE_TO_BUFFER_1 :
                                     AT45DB161D_COMPARE_PAGE_TO_BUFFER_2);
     
     /* Page address */
-    spi_transfer((uint8_t)(page >> 6));
-    spi_transfer((uint8_t)(page << 2));
-    spi_transfer(0x00);
+    _spi->write((uint8_t)(page >> 6));
+    _spi->write((uint8_t)(page << 2));
+    _spi->write(0x00);
     
     DF_CS_inactive;  /* Start comparaison */
     DF_CS_active;
@@ -531,7 +507,7 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
     
     /* Send opcode */
-    spi_transfer(AT45DB161D_DEEP_POWER_DOWN);
+    _spi->write(AT45DB161D_DEEP_POWER_DOWN);
     
     /* Enter Deep Power-Down mode */
     DF_CS_inactive;
@@ -550,7 +526,7 @@
     DF_CS_active;      /* to reset Dataflash command decoder     */
     
     /* Send opcode */
-    spi_transfer(AT45DB161D_RESUME_FROM_DEEP_POWER_DOWN);
+    _spi->write(AT45DB161D_RESUME_FROM_DEEP_POWER_DOWN);
     
     /* Resume device */
     DF_CS_inactive;