Shivanand Gowda / S2568FBLL

Dependents:   SPI_SRAM_READ_WRITE

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers S2568FBLL.cpp Source File

S2568FBLL.cpp

00001 // S2568FBLL.cpp
00002 
00003 #include"S2568FBLL.h"
00004 
00005 // CONSTRUCTOR
00006 S2568FBLL::S2568FBLL(PinName mosi, PinName miso, PinName sclk, PinName cs,PinName hold) : SPI(mosi, miso, sclk), _cs(cs), _hold(hold)
00007 {
00008     this->format(SPI_NBIT, SPI_MODE);
00009     this->frequency(SPI_FREQ);
00010     chipDisable();
00011     holdEnable();       // Keep Hold High Always.... for read write operations.
00012     // if you want to abort while transaction is happening use holdDiasable();
00013     // Read Data Sheet Before Using Hold Disable
00014     
00015 }
00016 // READING
00017 int S2568FBLL::readByte(int addr)
00018 {
00019     writeRegister(RWMODE_BYTE);
00020     chipEnable();
00021     this->write(READ);
00022     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00023     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00024     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00025     int response = this->write(DUMMY_ADDR);
00026     chipDisable();
00027     return response;
00028 }
00029 
00030 void S2568FBLL::readStream(int addr, char* buf, int count)
00031 {
00032     if (count < 1)
00033         return;
00034     writeRegister(RWMODE_SEQ);
00035     chipEnable();
00036     this->write(READ);
00037     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00038     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00039     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00040     for (int i = 0; i < count; i++) {
00041         buf[i] =  this->write(DUMMY_ADDR);
00042         printf("i= %d   :%c \r\n",i,buf[i]);
00043     }
00044     chipDisable();
00045 }
00046 
00047 // WRITING
00048 void S2568FBLL::writeByte(int addr, int data)
00049 {
00050     writeRegister(RWMODE_BYTE);
00051     chipEnable();
00052     this->write(WRITE);
00053     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00054     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00055     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00056     this->write(data);
00057     chipDisable();
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 S2568FBLL::writeStream(int addr, char* buf, int count)
00062 {
00063     if (count < 1)
00064         return;
00065     writeRegister(RWMODE_SEQ);
00066     chipEnable();
00067     this->write(WRITE);
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     wait(WAIT_TIME);
00077 }
00078 
00079 void S2568FBLL::writeString(int addr, string str)
00080 {
00081     if (str.length() < 1)
00082         return;
00083     writeRegister(RWMODE_SEQ);
00084     chipEnable();
00085     this->write(WRITE);
00086     this->write((addr & ADDR_BMASK3) >> ADDR_BSHIFT3);
00087     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00088     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00089     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00090     for (int i = 0; i < str.length(); i++)
00091         this->write(str.at(i));
00092     chipDisable();
00093     wait(WAIT_TIME);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails 
00094 }
00095 
00096 
00097 
00098 uint8_t S2568FBLL::readRegister(void)
00099 {
00100     chipEnable();
00101     this->write(RDMR);
00102     uint8_t val=this->write(DUMMY_ADDR);
00103     chipDisable();
00104    //wait(WAIT_TIME);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails 
00105     //printf("value of reg is %X  \r\n",val);
00106     return(val);
00107 
00108 }
00109 
00110 
00111 
00112 void S2568FBLL::writeRegister(uint8_t regValue)
00113 {
00114    
00115     chipEnable();
00116     this->write(WRMR);
00117     this->write(regValue);
00118     chipDisable();
00119   
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 
00124 
00125 
00126 void S2568FBLL::writeLong(int addr, long value)
00127 {
00128     //Decomposition from a long to 4 bytes by using bitshift.
00129     //One = Most significant -> Four = Least significant byte
00130     uint8_t four = (value & 0xFF);
00131     uint8_t three = ((value >> 8) & 0xFF);
00132     uint8_t two = ((value >> 16) & 0xFF);
00133     uint8_t one = ((value >> 24) & 0xFF);
00134 
00135     writeRegister(RWMODE_SEQ);
00136     chipEnable();
00137     this->write(WRITE);
00138  
00139     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00140     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00141     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00142     this->write(four);
00143     this->write(three);
00144     this->write(two);
00145     this->write(one);
00146     chipDisable();
00147     wait(0.1);
00148 }
00149 
00150 long S2568FBLL::raedLong(int addr)
00151 {
00152     //Read the 4 bytes from the eeprom memory.
00153    writeRegister(RWMODE_SEQ);
00154     chipEnable();
00155     this->write(READ);
00156     this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
00157     this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
00158     this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
00159 
00160     long four = this->write(DUMMY_ADDR);
00161     long three = this->write(DUMMY_ADDR);
00162     long two = this->write(DUMMY_ADDR);
00163     long one = this->write(DUMMY_ADDR);
00164     chipDisable();
00165     //Return the recomposed long by using bitshift.
00166     return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
00167    
00168 }
00169 
00170 
00171 //ENABLE/DISABLE (private functions)
00172 
00173 void S2568FBLL::chipEnable()
00174 {
00175     _cs = 0;
00176 }
00177 void S2568FBLL::chipDisable()
00178 {
00179     _cs = 1;
00180 }
00181 
00182 void S2568FBLL::holdEnable()
00183 {
00184     _hold = 1;
00185 }
00186 
00187 void S2568FBLL::holdDisable()
00188 {
00189     _hold = 0;
00190 }