Winbond W25Q80BV SPI library
Diff: W25Q80BV.cpp
- Revision:
- 0:6f374c3ed666
- Child:
- 1:72b556f51896
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/W25Q80BV.cpp Wed Jan 29 02:05:38 2014 +0000 @@ -0,0 +1,152 @@ +// W25Q80BV.cpp +//Changes by Ciprijal + +#include"W25Q80BV.h" + +// Pinout: PTD2,PTD3, PTD1, PTC7 + +// CONSTRUCTOR +W25Q80BV::W25Q80BV(PinName mosi, PinName miso, PinName sclk, PinName cs):_cs(cs),_mosi(mosi),_miso(miso),_sclk(sclk){ + +} + + + +//Get ID + int W25Q80BV::getMID( ){ + int ID = 0; + chipEnable(); + this->writeSPI(D_ID); + this->writeSPI(DUMMY_ADDR); + this->writeSPI(DUMMY_ADDR); + this->writeSPI(DUMMY_ADDR); + this->writeSPI(DUMMY_ADDR); + ID = this->writeSPI(DUMMY_ADDR); + chipDisable(); + return ID; +} + + +// READING +int W25Q80BV::readByte(int addr) { + chipEnable(); + this->writeSPI(R_INST); + this->writeSPI((addr & ADDR_BMASK2) >> ADDR_BSHIFT2); + this->writeSPI((addr & ADDR_BMASK1) >> ADDR_BSHIFT1); + this->writeSPI((addr & ADDR_BMASK0) >> ADDR_BSHIFT0); + int response = this->writeSPI(DUMMY_ADDR); + chipDisable(); + return response; +} +int W25Q80BV::readByte(int a2, int a1, int a0) { + chipEnable(); + this->writeSPI(R_INST); + this->writeSPI(a2); + this->writeSPI(a1); + this->writeSPI(a0); + int response = this->writeSPI(DUMMY_ADDR); + chipDisable(); + return response; +} +void W25Q80BV::readStream(int addr, char* buf, int count) { + if (count < 1) + return; + chipEnable(); + this->writeSPI(R_INST); + this->writeSPI((addr & ADDR_BMASK2) >> ADDR_BSHIFT2); + this->writeSPI((addr & ADDR_BMASK1) >> ADDR_BSHIFT1); + this->writeSPI((addr & ADDR_BMASK0) >> ADDR_BSHIFT0); + for (int i = 0; i < count; i++) + buf[i] = this->writeSPI(DUMMY_ADDR); + chipDisable(); +} + +// WRITING +void W25Q80BV::writeByte(int addr, int data) { + writeEnable(); + chipEnable(); + this->writeSPI(W_INST); + this->writeSPI((addr & ADDR_BMASK2) >> ADDR_BSHIFT2); + this->writeSPI((addr & ADDR_BMASK1) >> ADDR_BSHIFT1); + this->writeSPI((addr & ADDR_BMASK0) >> ADDR_BSHIFT0); + this->writeSPI(data); + chipDisable(); + writeDisable(); + wait(WAIT_TIME); +} +void W25Q80BV::writeByte(int a2, int a1, int a0, int data) { + writeEnable(); + chipEnable(); + this->writeSPI(W_INST); + this->writeSPI(a2); + this->writeSPI(a1); + this->writeSPI(a0); + this->writeSPI(data); + chipDisable(); + writeDisable(); + wait(WAIT_TIME); +} +void W25Q80BV::writeStream(int addr, char* buf, int count) { + if (count < 1) + return; + writeEnable(); + chipEnable(); + this->writeSPI(W_INST); + this->writeSPI((addr & ADDR_BMASK2) >> ADDR_BSHIFT2); + this->writeSPI((addr & ADDR_BMASK1) >> ADDR_BSHIFT1); + this->writeSPI((addr & ADDR_BMASK0) >> ADDR_BSHIFT0); + for (int i = 0; i < count; i++) + this->writeSPI(buf[i]); + chipDisable(); + writeDisable(); + wait(WAIT_TIME); +} + +//ERASING +void W25Q80BV::chipErase() { + writeEnable(); + chipEnable(); + this->writeSPI(C_ERASE_INST); + chipDisable(); + writeDisable(); + wait(WAIT_TIME); +} + + +//ENABLE/DISABLE (private functions) +void W25Q80BV::writeEnable() { + chipEnable(); + this->writeSPI(WE_INST); + chipDisable(); +} +void W25Q80BV::writeDisable() { + chipEnable(); + this->writeSPI(WD_INST); + chipDisable(); +} +void W25Q80BV::chipEnable() { + _cs = 0; +} +void W25Q80BV::chipDisable() { + _cs = 1; +} +//Sends and receives 1 byte of SPI data MSB endianness +int writeSPI(int data){ + + int aux =0,aux2=0,i=0,read=0; + //aux=data; + for(i=0;i<8;i++){ + _sclk = 0; + //output + aux = data&0x80; + aux = aux>>7; + _mosi = aux; + data = data>>1; + //input + read = read<<1; + aux2 = _miso; + read = read|aux2; + } + read = read>1; //This might not be needed + return read; +} \ No newline at end of file