128K Serial EEPROM read write erase chip erase functions SPI EEPROM Nucleo F767ZI

Dependents:   SPI_EEPROM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EE25LC1024.cpp Source File

EE25LC1024.cpp

00001 // EE25LC1024.cpp
00002 #include "mbed.h"
00003 #include"EE25LC1024.h"
00004 
00005 // CONSTRUCTOR
00006 EE25LC1024::EE25LC1024(PinName mosi, PinName miso, PinName sclk, PinName cs) : SPI(mosi, miso, sclk), _cs(cs)
00007 {
00008     this->format(SPI_NBIT, SPI_MODE);
00009     this->frequency(SPI_Freq);
00010     chipDisable();
00011 
00012 }
00013 // READING
00014 
00015 
00016 void EE25LC1024::deepPowerDown(void)
00017 { 
00018     chipEnable();
00019     this->write(DPD);
00020     chipDisable();  
00021 }
00022     
00023   int EE25LC1024::ReleaseDPD_ReadSign(void)
00024 { 
00025     chipEnable();
00026     this->write(Readid);
00027     this->write(DUMMY_ADDR);
00028     this->write(DUMMY_ADDR);
00029     this->write(DUMMY_ADDR);
00030      int response = this->write(DUMMY_ADDR);
00031     chipDisable(); 
00032     return response; 
00033 }  
00034     
00035     
00036 int EE25LC1024::readByte(int addr)
00037 {
00038     chipEnable();
00039     this->write(READ);
00040     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00041     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00042     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00043     int response = this->write(DUMMY_ADDR);
00044     chipDisable();
00045     return response;
00046 }
00047 
00048 void EE25LC1024::readStream(int addr, char* buf, int count)
00049 {
00050     if (count < 1)
00051         return;
00052     chipEnable();
00053     this->write(READ);
00054     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00055     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00056     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00057     for (int i = 0; i < count; i++) {
00058         buf[i] =  this->write(DUMMY_ADDR);
00059        // printf("i= %d   :%c \r\n",i,buf[i]);
00060     }
00061     chipDisable();
00062   //  wait_ms(2);
00063 }
00064 
00065 // WRITING
00066 void EE25LC1024::writeByte(int addr, int data)
00067 {
00068     writeEnable();
00069     chipEnable();
00070     this->write(WRITE);
00071     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00072     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00073     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00074     this->write(data);
00075     chipDisable();
00076     writeDisable();
00077     wait_ms(6);
00078     // wait(WAIT_TIME);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails 
00079 }
00080 
00081 void EE25LC1024::writeStream(int addr, char* buf, int count)
00082 {
00083 
00084     if (count < 1)
00085         return;
00086         
00087     writeEnable();
00088     
00089  
00090     
00091     chipEnable();
00092     this->write(WRITE);
00093     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00094     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00095     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00096     for (int i = 0; i < count; i++) {
00097         this->write(buf[i]);
00098     }
00099     chipDisable();
00100     writeDisable();
00101      wait_ms(2);
00102    uint8_t busy= checkIfBusy(); 
00103    while(busy==1)
00104    {
00105        //printf("Busy   :%d\r\n",busy);
00106        wait_ms(1);
00107        busy= checkIfBusy();
00108     }
00109     }
00110 
00111 void EE25LC1024::writeString(int addr, string str)
00112 {
00113     if (str.length() < 1)
00114         return;
00115     writeEnable();
00116     chipEnable();
00117     this->write(WRITE);
00118     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00119     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00120     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00121     for (int i = 0; i < str.length(); i++)
00122         this->write(str.at(i));
00123     chipDisable();
00124     writeDisable();
00125     wait_ms(6);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails 
00126 }
00127 
00128 
00129 
00130 uint8_t EE25LC1024::readRegister()
00131 {
00132 
00133     chipEnable();
00134     this->write(RDSR);
00135     uint8_t val=this->write(DUMMY_ADDR);
00136     chipDisable();
00137    //wait(WAIT_TIME);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails 
00138    //printf("value of reg is %X  \r\n",val);
00139     return(val);
00140 }
00141 //ERASING
00142 void EE25LC1024::chipErase()
00143 {
00144     writeEnable();
00145     chipEnable();
00146     this->write(CE);
00147     chipDisable();
00148     writeDisable();
00149     wait_ms(10);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails 
00150 }
00151 
00152 
00153 void EE25LC1024::sectorErase(int addr)
00154 {
00155     writeEnable();
00156     chipEnable();
00157     this->write(SE);
00158     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00159     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00160     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00161     chipDisable();
00162     writeDisable();
00163     wait_ms(10);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails 
00164 }
00165 
00166 void EE25LC1024::pageErase(int addr)
00167 {
00168     
00169     writeEnable();
00170     
00171     chipEnable();
00172     
00173     this->write(SE);
00174     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00175     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00176     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00177     chipDisable();
00178     writeDisable();
00179     wait_ms(6);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails 
00180      
00181     
00182 }
00183 
00184 
00185 uint8_t EE25LC1024::checkIfBusy()
00186 {
00187     uint8_t value=readRegister();
00188    // printf("\r\n Value of Status Reg=%X\r\n\r\n",value);
00189     if((value & 0x01)==0x01 )
00190     {
00191         wait_ms(1);
00192         return 1;
00193         }
00194     else
00195     {
00196         wait_ms(1);
00197         return 0;
00198         }
00199 }
00200 
00201 void EE25LC1024::writeRegister(uint8_t regValue)
00202 {
00203     writeEnable();
00204     chipEnable();
00205     this->write(WRSR);
00206     this->write(regValue);
00207     chipDisable();
00208     writeDisable();
00209     wait(WAIT_TIME);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails 
00210 
00211 }
00212 
00213 
00214 void EE25LC1024::writeLong(int addr, long value)
00215 {
00216     //Decomposition from a long to 4 bytes by using bitshift.
00217     //One = Most significant -> Four = Least significant byte
00218     uint8_t four = (value & 0xFF);
00219     uint8_t three = ((value >> 8) & 0xFF);
00220     uint8_t two = ((value >> 16) & 0xFF);
00221     uint8_t one = ((value >> 24) & 0xFF);
00222 
00223     writeEnable();
00224     chipEnable();
00225     this->write(WRITE);
00226     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00227     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00228     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00229     this->write(four);
00230     this->write(three);
00231     this->write(two);
00232     this->write(one);
00233     chipDisable();
00234     writeDisable();
00235     wait_ms(6);
00236 }
00237 
00238 long EE25LC1024::readLong(int addr)
00239 {
00240     //Read the 4 bytes from the eeprom memory.
00241     chipEnable();
00242     this->write(READ);
00243     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00244     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00245     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00246 
00247     long four = this->write(DUMMY_ADDR);
00248     long three = this->write(DUMMY_ADDR);
00249     long two = this->write(DUMMY_ADDR);
00250     long one = this->write(DUMMY_ADDR);
00251     chipDisable();
00252     //Return the recomposed long by using bitshift.
00253     return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
00254     
00255 }
00256 
00257 
00258 //ENABLE/DISABLE (private functions)
00259 void EE25LC1024::writeEnable()
00260 {
00261     chipEnable();
00262     this->write(WREN);
00263     chipDisable();
00264 }
00265 void EE25LC1024::writeDisable()
00266 {
00267     chipEnable();
00268     this->write(WRDI);
00269     chipDisable();
00270 }
00271 void EE25LC1024::chipEnable()
00272 {
00273     _cs = 0;
00274 }
00275 void EE25LC1024::chipDisable()
00276 {
00277     _cs = 1;
00278 }
00279