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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers W25X40BV.cpp Source File

W25X40BV.cpp

00001 // W25X40BV.cpp
00002 
00003 #include"W25X40BV.h"
00004 
00005 // CONSTRUCTOR 
00006 W25X40BV::W25X40BV(PinName mosi, PinName miso, PinName sclk, PinName cs) : SPI(mosi, miso, sclk), _cs(cs) {
00007     this->format(SPI_NBIT, SPI_MODE);
00008     this->frequency(SPI_FREQ);
00009     chipDisable();
00010 }
00011 
00012 
00013 // READING
00014 int W25X40BV::readByte(int addr) {
00015     chipEnable();
00016     this->write(R_INST);
00017     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00018     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00019     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00020     int response = this->write(DUMMY_ADDR);
00021     chipDisable();
00022     return response;
00023 }
00024 int W25X40BV::readByte(int a2, int a1, int a0) {
00025    chipEnable();
00026    this->write(R_INST);
00027    this->write(a2);
00028    this->write(a1);
00029    this->write(a0);
00030    int response = this->write(DUMMY_ADDR);
00031     chipDisable();
00032     return response;
00033 }
00034 void W25X40BV::readStream(int addr, char* buf, int count) {
00035     if (count < 1)
00036         return;
00037     chipEnable();
00038     this->write(R_INST);
00039     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00040     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00041     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00042     for (int i = 0; i < count; i++)
00043         buf[i] =  this->write(DUMMY_ADDR);
00044     chipDisable();
00045 }
00046 
00047 // WRITING
00048 void W25X40BV::writeByte(int addr, int data) {
00049     writeEnable();
00050     chipEnable();
00051     this->write(W_INST);
00052     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00053     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00054     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00055     this->write(data);
00056     chipDisable();
00057     writeDisable();
00058     wait(WAIT_TIME);
00059 }
00060 void W25X40BV::writeByte(int a2, int a1, int a0, int data) {
00061     writeEnable();
00062     chipEnable();
00063     this->write(W_INST);
00064     this->write(a2);
00065     this->write(a1);
00066     this->write(a0);
00067     this->write(data);
00068     chipDisable();
00069     writeDisable();
00070     wait(WAIT_TIME);
00071 }
00072 void W25X40BV::writeStream(int addr, char* buf, int count) {
00073     if (count < 1)
00074         return;
00075     writeEnable();
00076     chipEnable();
00077     this->write(W_INST);
00078     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00079     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00080     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00081     for (int i = 0; i < count; i++)
00082         this->write(buf[i]);
00083     chipDisable();
00084     writeDisable();
00085     wait(WAIT_TIME);
00086 }
00087 
00088 //ERASING
00089 void W25X40BV::chipErase() {
00090     writeEnable();
00091     chipEnable();
00092     this->write(C_ERASE_INST);
00093     chipDisable();
00094     writeDisable();
00095     wait(WAIT_TIME);
00096 }
00097     
00098 
00099 //ENABLE/DISABLE (private functions)
00100 void W25X40BV::writeEnable() {
00101     chipEnable();
00102     this->write(WE_INST);
00103     chipDisable();
00104 }
00105 void W25X40BV::writeDisable() {
00106     chipEnable();
00107     this->write(WD_INST);
00108     chipDisable();
00109 }
00110 void W25X40BV::chipEnable() {
00111     _cs = 0;
00112 }
00113 void W25X40BV::chipDisable() {
00114     _cs = 1;
00115 }