Shivanand Gowda / S70FL01GS

Dependents:   SPI_FLASH_MEM_1Gb

Fork of S25FL256S by Shivanand Gowda

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers S70FL01GS.cpp Source File

S70FL01GS.cpp

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