SPI Flash AT45DBXXXD

Fork of at45db161d by Suga koubou

Revision:
3:82157896d90d
Parent:
2:15198ba8f0d0
Child:
4:943690efda8b
--- a/at45db161d.cpp	Thu Mar 05 09:02:38 2015 +0800
+++ b/at45db161d.cpp	Thu Mar 05 18:07:15 2015 +0800
@@ -17,7 +17,10 @@
 
 ATD45DB161D::ATD45DB161D(SPI &spi, PinName cs)
   : _spi(spi), _cs(cs)
-{}
+{
+    _bytes = 264;
+    _pages = 4095;
+}
 
 /** DTOR **/
 ATD45DB161D::~ATD45DB161D()
@@ -149,6 +152,26 @@
     if (!low) { spi_transfer(0x00); }
 }
 
+void ATD45DB161D::read(int addr, char *buf, int len) {
+    int i;
+    PageToBuffer(addr / _bytes, 1);
+    BufferRead(1, addr % _bytes, 1);
+    for (i = 0; i < len; i++) {
+        buf[i] = _spi.write(0xff);
+    }
+}
+
+void ATD45DB161D::write(int addr, char *buf, int len) {
+    uint16_t i;
+    while (len > 0) {
+        BufferWrite(2, addr % _bytes);
+        for (i = 0; i < _bytes && len-- > 0; i++) {
+            _spi.write(*buf++);
+        }
+        BufferToPage(2, addr / _bytes, 1);
+        addr += _bytes;
+    }
+}
 
 /** 
  * Read the content of one of the SRAM data buffers (in low or high speed mode).