Shivanand Gowda / M95M02D

Dependents:   SPI_EEPROM_2MB

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers M95M02D.cpp Source File

M95M02D.cpp

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