Kazuki Yamamoto / W25X40BV

Dependents:   11u35_usbLocalFilesystem

Fork of W25X40BV by Johnny Yam

Files at this revision

API Documentation at this revision

Comitter:
k4zuki
Date:
Sun Feb 28 15:22:40 2016 +0000
Parent:
3:6e3c0b23dc6e
Child:
5:4f7e8756d1c4
Commit message:
use SWSPI library; typedef; add page and block(4K/32K) erase

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
--- a/W25X40BV.cpp	Mon Mar 26 04:30:32 2012 +0000
+++ b/W25X40BV.cpp	Sun Feb 28 15:22:40 2016 +0000
@@ -1,37 +1,38 @@
-// W25X40BV.cpp
+/** W25X40BV.cpp
+*/
+#include "W25X40BV.h"
+#include "SWSPI.h"
 
-#include"W25X40BV.h"
-
-// CONSTRUCTOR 
-W25X40BV::W25X40BV(PinName mosi, PinName miso, PinName sclk, PinName cs) : SPI(mosi, miso, sclk), _cs(cs) {
+//! CONSTRUCTOR 
+W25X40BV::W25X40BV(PinName mosi, PinName miso, PinName sclk, PinName cs) : SWSPI(mosi, miso, sclk), _cs(cs) {
     this->format(SPI_NBIT, SPI_MODE);
     this->frequency(SPI_FREQ);
     chipDisable();
 }
 
 
-// READING
-int W25X40BV::readByte(int addr) {
+//! READING
+uint32_t W25X40BV::readByte(uint32_t addr) {
     chipEnable();
     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);
+    uint32_t response = this->write(DUMMY_ADDR);
     chipDisable();
     return response;
 }
-int W25X40BV::readByte(int a2, int a1, int a0) {
+uint32_t W25X40BV::readByte(uint32_t a2, uint32_t a1, uint32_t a0) {
    chipEnable();
    this->write(R_INST);
    this->write(a2);
    this->write(a1);
    this->write(a0);
-   int response = this->write(DUMMY_ADDR);
+   uint32_t response = this->write(DUMMY_ADDR);
     chipDisable();
     return response;
 }
-void W25X40BV::readStream(int addr, char* buf, int count) {
+void W25X40BV::readStream(uint32_t addr, uint8_t* buf, uint32_t count) {
     if (count < 1)
         return;
     chipEnable();
@@ -39,13 +40,13 @@
     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++)
+    for (uint32_t i = 0; i < count; i++)
         buf[i] =  this->write(DUMMY_ADDR);
     chipDisable();
 }
 
 // WRITING
-void W25X40BV::writeByte(int addr, int data) {
+void W25X40BV::writeByte(uint32_t addr, uint32_t data) {
     writeEnable();
     chipEnable();
     this->write(W_INST);
@@ -57,7 +58,7 @@
     writeDisable();
     wait(WAIT_TIME);
 }
-void W25X40BV::writeByte(int a2, int a1, int a0, int data) {
+void W25X40BV::writeByte(uint32_t a2, uint32_t a1, uint32_t a0, uint32_t data) {
     writeEnable();
     chipEnable();
     this->write(W_INST);
@@ -69,7 +70,7 @@
     writeDisable();
     wait(WAIT_TIME);
 }
-void W25X40BV::writeStream(int addr, char* buf, int count) {
+void W25X40BV::writeStream(uint32_t addr, uint8_t* buf, uint32_t count) {
     if (count < 1)
         return;
     writeEnable();
@@ -78,7 +79,7 @@
     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++)
+    for (uint32_t i = 0; i < count; i++)
         this->write(buf[i]);
     chipDisable();
     writeDisable();
@@ -86,6 +87,45 @@
 }
 
 //ERASING
+void W25X40BV::pageErase(uint8_t page){
+    writeEnable();
+    chipEnable();
+    this->write(C_ERASE_INST);
+    this->write(DUMMY_ADDR);
+    this->write((int)page);
+    this->write(DUMMY_ADDR);
+    chipDisable();
+    writeDisable();
+    wait(WAIT_TIME);
+}
+
+void W25X40BV::block4Erase(uint16_t block){
+    uint8_t msb = (uint8_t)(block >> 4);
+    block = (block << 4) & 0xF0;
+    writeEnable();
+    chipEnable();
+    this->write(B4K_ERASE_INST);
+    this->write((int)msb);
+    this->write((int)block);
+    this->write(DUMMY_ADDR);
+    chipDisable();
+    writeDisable();
+    wait(WAIT_TIME);
+}
+
+void W25X40BV::block32Erase(uint8_t block){
+    block = (block << 3) & 0x18;
+    writeEnable();
+    chipEnable();
+    this->write(B32K_ERASE_INST);
+    this->write((int)block);
+    this->write(DUMMY_ADDR);
+    this->write(DUMMY_ADDR);
+    chipDisable();
+    writeDisable();
+    wait(WAIT_TIME);
+}
+
 void W25X40BV::chipErase() {
     writeEnable();
     chipEnable();
@@ -112,4 +152,4 @@
 }
 void W25X40BV::chipDisable() {
     _cs = 1;
-}
\ No newline at end of file
+}
--- a/W25X40BV.h	Mon Mar 26 04:30:32 2012 +0000
+++ b/W25X40BV.h	Sun Feb 28 15:22:40 2016 +0000
@@ -1,9 +1,11 @@
-// W25X40BV.h
+/** W25X40BV.h
+*/
 
 #ifndef W25X40BV_H
 #define W25X40BV_H
 
 #include "mbed.h"
+#include "SWSPI.h"
 
 #define SPI_FREQ        1000000
 #define SPI_MODE        0
@@ -14,11 +16,14 @@
 #define R_INST          0x03
 #define W_INST          0x02
 #define C_ERASE_INST    0x60
+#define P_ERASE_INST    0x81
+#define B4K_ERASE_INST  0x20
+#define B32K_ERASE_INST     0x52
 
 #define DUMMY_ADDR      0x00
 #define WAIT_TIME       1
 
-#define ADDR_BMASK2     0x00ff0000
+#define ADDR_BMASK2     0x001f0000
 #define ADDR_BMASK1     0x0000ff00
 #define ADDR_BMASK0     0x000000ff
 
@@ -26,28 +31,60 @@
 #define ADDR_BSHIFT1    8
 #define ADDR_BSHIFT0    0
 
-class W25X40BV: public SPI {
+/**
+W25X40BV
+*/
+class W25X40BV: public SWSPI {
 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 readStream(int addr, char* buf, int count);        // takes a 24-bit address, reads count bytes, and stores results in buf
+    //! takes a 24-bit (3 bytes) address and returns the data (1 byte) at that location
+    uint32_t readByte(uint32_t addr);
+    //! takes the address in 3 separate bytes A[23,16], A[15,8], A[7,0]
+    uint32_t readByte(uint32_t a2, uint32_t a1,uint32_t a0);
+    //! takes a 24-bit address, reads count bytes, and stores results in buf
+    void readStream(uint32_t addr, uint8_t* buf, uint32_t count);
+
+    //! takes a 24-bit (3 bytes) address and a byte of data to write at that location
+    void writeByte(uint32_t addr, uint32_t data);
+    //! takes the address in 3 separate bytes A[23,16], A[15,8], A[7,0]
+    void writeByte(uint32_t a2, uint32_t a1, uint32_t a0, uint32_t data);
+    /** write count bytes of data from buf to memory, starting at addr
+    * addr is expected to be 256 byte alignment: 0x000000,0x000100,...,0x01FF00
+    * even if not, it works anyway. but should care count and 256byte edge: 
+    * if count=3 while addr=0x????FE, 3rd byte will be written in 0x????00
+    * @param addr 24 bit address
+    * @param buf data buffer
+    * @param count data count in byte
+    */
+    void writeStream(uint32_t addr, uint8_t* buf, uint32_t count);
     
-    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  
+    //! erase a page data(256bytes) on chip
+    void pageErase(uint8_t page);
+
+    /** erase a block data(4Kbytes) on chip
+    * @param block block number 0x0000 - 0x001F
+    * block number will be left-shifted by 4 bits: 0x0000 - 0x01F0
+    * and a dummy address byte (0x00) will be added
+    */
+    void block4Erase(uint16_t block);
     
-    void chipErase();                                       // erase all data on chip
+    /** erase a block data(32Kbytes) on chip
+    * @param block number 0x00 - 0x03
+    * block number will be left-shifted by 3 bits: 0x00 - 0x18
+    * and two dummy address bytes (0x0000) will be added
+    */
+    void block32Erase(uint8_t block);
+    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;
 };
 
-#endif
\ No newline at end of file
+#endif