Winbond W25Q80BV SPI library

Dependents:   data_log

Committer:
perezalvarezhi
Date:
Thu Jan 30 00:51:50 2014 +0000
Revision:
1:72b556f51896
Parent:
0:6f374c3ed666
Child:
2:e8dc659cf54e
Software SPI byte write function done, still needs to be tested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
perezalvarezhi 0:6f374c3ed666 1 // W25Q80BV.cpp
perezalvarezhi 0:6f374c3ed666 2 //Changes by Ciprijal
perezalvarezhi 0:6f374c3ed666 3
perezalvarezhi 0:6f374c3ed666 4 #include"W25Q80BV.h"
perezalvarezhi 0:6f374c3ed666 5
perezalvarezhi 0:6f374c3ed666 6 // Pinout: PTD2,PTD3, PTD1, PTC7
perezalvarezhi 0:6f374c3ed666 7
perezalvarezhi 0:6f374c3ed666 8 // CONSTRUCTOR
perezalvarezhi 0:6f374c3ed666 9 W25Q80BV::W25Q80BV(PinName mosi, PinName miso, PinName sclk, PinName cs):_cs(cs),_mosi(mosi),_miso(miso),_sclk(sclk){
perezalvarezhi 0:6f374c3ed666 10
perezalvarezhi 0:6f374c3ed666 11 }
perezalvarezhi 0:6f374c3ed666 12
perezalvarezhi 0:6f374c3ed666 13
perezalvarezhi 0:6f374c3ed666 14
perezalvarezhi 0:6f374c3ed666 15 //Get ID
perezalvarezhi 0:6f374c3ed666 16 int W25Q80BV::getMID( ){
perezalvarezhi 0:6f374c3ed666 17 int ID = 0;
perezalvarezhi 0:6f374c3ed666 18 chipEnable();
perezalvarezhi 0:6f374c3ed666 19 this->writeSPI(D_ID);
perezalvarezhi 0:6f374c3ed666 20 this->writeSPI(DUMMY_ADDR);
perezalvarezhi 0:6f374c3ed666 21 this->writeSPI(DUMMY_ADDR);
perezalvarezhi 0:6f374c3ed666 22 this->writeSPI(DUMMY_ADDR);
perezalvarezhi 0:6f374c3ed666 23 this->writeSPI(DUMMY_ADDR);
perezalvarezhi 0:6f374c3ed666 24 ID = this->writeSPI(DUMMY_ADDR);
perezalvarezhi 0:6f374c3ed666 25 chipDisable();
perezalvarezhi 0:6f374c3ed666 26 return ID;
perezalvarezhi 0:6f374c3ed666 27 }
perezalvarezhi 0:6f374c3ed666 28
perezalvarezhi 0:6f374c3ed666 29
perezalvarezhi 0:6f374c3ed666 30 // READING
perezalvarezhi 0:6f374c3ed666 31 int W25Q80BV::readByte(int addr) {
perezalvarezhi 0:6f374c3ed666 32 chipEnable();
perezalvarezhi 0:6f374c3ed666 33 this->writeSPI(R_INST);
perezalvarezhi 0:6f374c3ed666 34 this->writeSPI((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
perezalvarezhi 0:6f374c3ed666 35 this->writeSPI((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
perezalvarezhi 0:6f374c3ed666 36 this->writeSPI((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
perezalvarezhi 0:6f374c3ed666 37 int response = this->writeSPI(DUMMY_ADDR);
perezalvarezhi 0:6f374c3ed666 38 chipDisable();
perezalvarezhi 0:6f374c3ed666 39 return response;
perezalvarezhi 0:6f374c3ed666 40 }
perezalvarezhi 0:6f374c3ed666 41 int W25Q80BV::readByte(int a2, int a1, int a0) {
perezalvarezhi 0:6f374c3ed666 42 chipEnable();
perezalvarezhi 0:6f374c3ed666 43 this->writeSPI(R_INST);
perezalvarezhi 0:6f374c3ed666 44 this->writeSPI(a2);
perezalvarezhi 0:6f374c3ed666 45 this->writeSPI(a1);
perezalvarezhi 0:6f374c3ed666 46 this->writeSPI(a0);
perezalvarezhi 0:6f374c3ed666 47 int response = this->writeSPI(DUMMY_ADDR);
perezalvarezhi 0:6f374c3ed666 48 chipDisable();
perezalvarezhi 0:6f374c3ed666 49 return response;
perezalvarezhi 0:6f374c3ed666 50 }
perezalvarezhi 0:6f374c3ed666 51 void W25Q80BV::readStream(int addr, char* buf, int count) {
perezalvarezhi 0:6f374c3ed666 52 if (count < 1)
perezalvarezhi 0:6f374c3ed666 53 return;
perezalvarezhi 0:6f374c3ed666 54 chipEnable();
perezalvarezhi 0:6f374c3ed666 55 this->writeSPI(R_INST);
perezalvarezhi 0:6f374c3ed666 56 this->writeSPI((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
perezalvarezhi 0:6f374c3ed666 57 this->writeSPI((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
perezalvarezhi 0:6f374c3ed666 58 this->writeSPI((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
perezalvarezhi 0:6f374c3ed666 59 for (int i = 0; i < count; i++)
perezalvarezhi 0:6f374c3ed666 60 buf[i] = this->writeSPI(DUMMY_ADDR);
perezalvarezhi 0:6f374c3ed666 61 chipDisable();
perezalvarezhi 0:6f374c3ed666 62 }
perezalvarezhi 0:6f374c3ed666 63
perezalvarezhi 0:6f374c3ed666 64 // WRITING
perezalvarezhi 0:6f374c3ed666 65 void W25Q80BV::writeByte(int addr, int data) {
perezalvarezhi 0:6f374c3ed666 66 writeEnable();
perezalvarezhi 0:6f374c3ed666 67 chipEnable();
perezalvarezhi 0:6f374c3ed666 68 this->writeSPI(W_INST);
perezalvarezhi 0:6f374c3ed666 69 this->writeSPI((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
perezalvarezhi 0:6f374c3ed666 70 this->writeSPI((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
perezalvarezhi 0:6f374c3ed666 71 this->writeSPI((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
perezalvarezhi 0:6f374c3ed666 72 this->writeSPI(data);
perezalvarezhi 0:6f374c3ed666 73 chipDisable();
perezalvarezhi 0:6f374c3ed666 74 writeDisable();
perezalvarezhi 0:6f374c3ed666 75 wait(WAIT_TIME);
perezalvarezhi 0:6f374c3ed666 76 }
perezalvarezhi 0:6f374c3ed666 77 void W25Q80BV::writeByte(int a2, int a1, int a0, int data) {
perezalvarezhi 0:6f374c3ed666 78 writeEnable();
perezalvarezhi 0:6f374c3ed666 79 chipEnable();
perezalvarezhi 0:6f374c3ed666 80 this->writeSPI(W_INST);
perezalvarezhi 0:6f374c3ed666 81 this->writeSPI(a2);
perezalvarezhi 0:6f374c3ed666 82 this->writeSPI(a1);
perezalvarezhi 0:6f374c3ed666 83 this->writeSPI(a0);
perezalvarezhi 0:6f374c3ed666 84 this->writeSPI(data);
perezalvarezhi 0:6f374c3ed666 85 chipDisable();
perezalvarezhi 0:6f374c3ed666 86 writeDisable();
perezalvarezhi 0:6f374c3ed666 87 wait(WAIT_TIME);
perezalvarezhi 0:6f374c3ed666 88 }
perezalvarezhi 0:6f374c3ed666 89 void W25Q80BV::writeStream(int addr, char* buf, int count) {
perezalvarezhi 0:6f374c3ed666 90 if (count < 1)
perezalvarezhi 0:6f374c3ed666 91 return;
perezalvarezhi 0:6f374c3ed666 92 writeEnable();
perezalvarezhi 0:6f374c3ed666 93 chipEnable();
perezalvarezhi 0:6f374c3ed666 94 this->writeSPI(W_INST);
perezalvarezhi 0:6f374c3ed666 95 this->writeSPI((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
perezalvarezhi 0:6f374c3ed666 96 this->writeSPI((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
perezalvarezhi 0:6f374c3ed666 97 this->writeSPI((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
perezalvarezhi 0:6f374c3ed666 98 for (int i = 0; i < count; i++)
perezalvarezhi 0:6f374c3ed666 99 this->writeSPI(buf[i]);
perezalvarezhi 0:6f374c3ed666 100 chipDisable();
perezalvarezhi 0:6f374c3ed666 101 writeDisable();
perezalvarezhi 0:6f374c3ed666 102 wait(WAIT_TIME);
perezalvarezhi 0:6f374c3ed666 103 }
perezalvarezhi 0:6f374c3ed666 104
perezalvarezhi 0:6f374c3ed666 105 //ERASING
perezalvarezhi 0:6f374c3ed666 106 void W25Q80BV::chipErase() {
perezalvarezhi 0:6f374c3ed666 107 writeEnable();
perezalvarezhi 0:6f374c3ed666 108 chipEnable();
perezalvarezhi 0:6f374c3ed666 109 this->writeSPI(C_ERASE_INST);
perezalvarezhi 0:6f374c3ed666 110 chipDisable();
perezalvarezhi 0:6f374c3ed666 111 writeDisable();
perezalvarezhi 0:6f374c3ed666 112 wait(WAIT_TIME);
perezalvarezhi 0:6f374c3ed666 113 }
perezalvarezhi 0:6f374c3ed666 114
perezalvarezhi 0:6f374c3ed666 115
perezalvarezhi 1:72b556f51896 116 //Private Functions
perezalvarezhi 0:6f374c3ed666 117 void W25Q80BV::writeEnable() {
perezalvarezhi 0:6f374c3ed666 118 chipEnable();
perezalvarezhi 0:6f374c3ed666 119 this->writeSPI(WE_INST);
perezalvarezhi 0:6f374c3ed666 120 chipDisable();
perezalvarezhi 0:6f374c3ed666 121 }
perezalvarezhi 0:6f374c3ed666 122 void W25Q80BV::writeDisable() {
perezalvarezhi 0:6f374c3ed666 123 chipEnable();
perezalvarezhi 0:6f374c3ed666 124 this->writeSPI(WD_INST);
perezalvarezhi 0:6f374c3ed666 125 chipDisable();
perezalvarezhi 0:6f374c3ed666 126 }
perezalvarezhi 0:6f374c3ed666 127 void W25Q80BV::chipEnable() {
perezalvarezhi 0:6f374c3ed666 128 _cs = 0;
perezalvarezhi 0:6f374c3ed666 129 }
perezalvarezhi 0:6f374c3ed666 130 void W25Q80BV::chipDisable() {
perezalvarezhi 0:6f374c3ed666 131 _cs = 1;
perezalvarezhi 0:6f374c3ed666 132 }
perezalvarezhi 0:6f374c3ed666 133 //Sends and receives 1 byte of SPI data MSB endianness
perezalvarezhi 1:72b556f51896 134 int W25Q80BV::writeSPI(int data){
perezalvarezhi 0:6f374c3ed666 135 int aux =0,aux2=0,i=0,read=0;
perezalvarezhi 0:6f374c3ed666 136 //aux=data;
perezalvarezhi 0:6f374c3ed666 137 for(i=0;i<8;i++){
perezalvarezhi 0:6f374c3ed666 138 _sclk = 0;
perezalvarezhi 0:6f374c3ed666 139 //output
perezalvarezhi 0:6f374c3ed666 140 aux = data&0x80;
perezalvarezhi 1:72b556f51896 141 aux >>= 7;
perezalvarezhi 0:6f374c3ed666 142 _mosi = aux;
perezalvarezhi 1:72b556f51896 143 data <<= 1;
perezalvarezhi 0:6f374c3ed666 144 //input
perezalvarezhi 1:72b556f51896 145 read <<= 1;
perezalvarezhi 0:6f374c3ed666 146 aux2 = _miso;
perezalvarezhi 1:72b556f51896 147 read |= aux2;
perezalvarezhi 1:72b556f51896 148 _sclk = 1;
perezalvarezhi 0:6f374c3ed666 149 }
perezalvarezhi 0:6f374c3ed666 150 return read;
perezalvarezhi 0:6f374c3ed666 151 }