Shivanand Gowda / S25FL256S

Dependents:   SPI_FLASH_MEM

Fork of W25X40BV by Johnny Yam

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers S25FL256S.cpp Source File

S25FL256S.cpp

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