Simple read/write API for the W25X40BV SPI Flash IC wiki: http://mbed.org/cookbook/W25X40BV

Files at this revision

API Documentation at this revision

Comitter:
jyam
Date:
Mon Mar 26 04:30:32 2012 +0000
Parent:
2:2b0daec9f8c0
Commit message:
now a derived class of SPI from \"mbedh.h\" (can access SPI\s public member functions; more flexible);

modified W25X40BV API read() to readStream() and write() to writeStream() to avoid overwriting SPI\s write() function (and it is now a more descriptive function name)

Changed in this revision

W25X40BV.cpp Show annotated file Show diff for this revision Revisions of this file
W25X40BV.h Show annotated file Show diff for this revision Revisions of this file
diff -r 2b0daec9f8c0 -r 6e3c0b23dc6e W25X40BV.cpp
--- a/W25X40BV.cpp	Mon Mar 19 15:05:56 2012 +0000
+++ b/W25X40BV.cpp	Mon Mar 26 04:30:32 2012 +0000
@@ -3,9 +3,9 @@
 #include"W25X40BV.h"
 
 // CONSTRUCTOR 
-W25X40BV::W25X40BV(PinName mosi, PinName miso, PinName sclk, PinName cs) : _spi(mosi, miso, sclk), _cs(cs) {
-    _spi.format(SPI_NBIT, SPI_MODE);
-    _spi.frequency(SPI_FREQ);
+W25X40BV::W25X40BV(PinName mosi, PinName miso, PinName sclk, PinName cs) : SPI(mosi, miso, sclk), _cs(cs) {
+    this->format(SPI_NBIT, SPI_MODE);
+    this->frequency(SPI_FREQ);
     chipDisable();
 }
 
@@ -13,34 +13,34 @@
 // READING
 int W25X40BV::readByte(int addr) {
     chipEnable();
-    _spi.write(R_INST);
-    _spi.write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
-    _spi.write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
-    _spi.write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
-    int response = _spi.write(DUMMY_ADDR);
+    this->write(R_INST);
+    this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+    this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+    this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+    int response = this->write(DUMMY_ADDR);
     chipDisable();
     return response;
 }
 int W25X40BV::readByte(int a2, int a1, int a0) {
    chipEnable();
-   _spi.write(R_INST);
-   _spi.write(a2);
-   _spi.write(a1);
-   _spi.write(a0);
-   int response = _spi.write(DUMMY_ADDR);
+   this->write(R_INST);
+   this->write(a2);
+   this->write(a1);
+   this->write(a0);
+   int response = this->write(DUMMY_ADDR);
     chipDisable();
     return response;
 }
-void W25X40BV::read(int addr, char* buf, int count) {
+void W25X40BV::readStream(int addr, char* buf, int count) {
     if (count < 1)
         return;
     chipEnable();
-    _spi.write(R_INST);
-    _spi.write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
-    _spi.write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
-    _spi.write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+    this->write(R_INST);
+    this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+    this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+    this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
     for (int i = 0; i < count; i++)
-        buf[i] =  _spi.write(DUMMY_ADDR);
+        buf[i] =  this->write(DUMMY_ADDR);
     chipDisable();
 }
 
@@ -48,11 +48,11 @@
 void W25X40BV::writeByte(int addr, int data) {
     writeEnable();
     chipEnable();
-    _spi.write(W_INST);
-    _spi.write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
-    _spi.write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
-    _spi.write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
-    _spi.write(data);
+    this->write(W_INST);
+    this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+    this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+    this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+    this->write(data);
     chipDisable();
     writeDisable();
     wait(WAIT_TIME);
@@ -60,26 +60,26 @@
 void W25X40BV::writeByte(int a2, int a1, int a0, int data) {
     writeEnable();
     chipEnable();
-    _spi.write(W_INST);
-    _spi.write(a2);
-    _spi.write(a1);
-    _spi.write(a0);
-    _spi.write(data);
+    this->write(W_INST);
+    this->write(a2);
+    this->write(a1);
+    this->write(a0);
+    this->write(data);
     chipDisable();
     writeDisable();
     wait(WAIT_TIME);
 }
-void W25X40BV::write(int addr, char* buf, int count) {
+void W25X40BV::writeStream(int addr, char* buf, int count) {
     if (count < 1)
         return;
     writeEnable();
     chipEnable();
-    _spi.write(W_INST);
-    _spi.write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
-    _spi.write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
-    _spi.write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+    this->write(W_INST);
+    this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+    this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+    this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
     for (int i = 0; i < count; i++)
-        _spi.write(buf[i]);
+        this->write(buf[i]);
     chipDisable();
     writeDisable();
     wait(WAIT_TIME);
@@ -89,7 +89,7 @@
 void W25X40BV::chipErase() {
     writeEnable();
     chipEnable();
-    _spi.write(C_ERASE_INST);
+    this->write(C_ERASE_INST);
     chipDisable();
     writeDisable();
     wait(WAIT_TIME);
@@ -99,12 +99,12 @@
 //ENABLE/DISABLE (private functions)
 void W25X40BV::writeEnable() {
     chipEnable();
-    _spi.write(WE_INST);
+    this->write(WE_INST);
     chipDisable();
 }
 void W25X40BV::writeDisable() {
     chipEnable();
-    _spi.write(WD_INST);
+    this->write(WD_INST);
     chipDisable();
 }
 void W25X40BV::chipEnable() {
diff -r 2b0daec9f8c0 -r 6e3c0b23dc6e W25X40BV.h
--- a/W25X40BV.h	Mon Mar 19 15:05:56 2012 +0000
+++ b/W25X40BV.h	Mon Mar 26 04:30:32 2012 +0000
@@ -26,27 +26,27 @@
 #define ADDR_BSHIFT1    8
 #define ADDR_BSHIFT0    0
 
-class W25X40BV {
+class W25X40BV: public SPI {
 public:
     W25X40BV(PinName mosi, PinName miso, PinName sclk, PinName cs);
     
-    int readByte(int addr);                             // takes a 24-bit (3 bytes) address and returns the data (1 byte) at that location
-    int readByte(int a2, int a1, int a0);               // takes the address in 3 separate bytes A[23,16], A[15,8], A[7,0]
-    void read(int addr, char* buf, int count);          // takes a 24-bit address, reads count bytes, and stores results in buf
+    int readByte(int addr);                                 // takes a 24-bit (3 bytes) address and returns the data (1 byte) at that location
+    int readByte(int a2, int a1, int a0);                   // takes the address in 3 separate bytes A[23,16], A[15,8], A[7,0]
+    void readStream(int addr, char* buf, int count);        // takes a 24-bit address, reads count bytes, and stores results in buf
     
-    void writeByte(int addr, int data);                 // takes a 24-bit (3 bytes) address and a byte of data to write at that location
-    void writeByte(int a2, int a1, int a0, int data);   // takes the address in 3 separate bytes A[23,16], A[15,8], A[7,0]
-    void write(int addr, char* buf, int count);         // write count bytes of data from buf to memory, starting at addr  
+    void writeByte(int addr, int data);                     // takes a 24-bit (3 bytes) address and a byte of data to write at that location
+    void writeByte(int a2, int a1, int a0, int data);       // takes the address in 3 separate bytes A[23,16], A[15,8], A[7,0]
+    void writeStream(int addr, char* buf, int count);       // write count bytes of data from buf to memory, starting at addr  
     
-    void chipErase();                               // erase all data on chip
+    void chipErase();                                       // erase all data on chip
     
 private:
-    void writeEnable();                             // write enable
-    void writeDisable();                            // write disable
-    void chipEnable();                              // chip enable
-    void chipDisable();                             // chip disable
+    void writeEnable();                                     // write enable
+    void writeDisable();                                    // write disable
+    void chipEnable();                                      // chip enable
+    void chipDisable();                                     // chip disable
     
-    SPI _spi;
+   // SPI _spi;
     DigitalOut _cs;
 };