128K Serial EEPROM read write erase chip erase functions SPI EEPROM Nucleo F767ZI
Revision 0:81848bf6dd4a, committed 2018-06-01
- Comitter:
- shivanandgowdakr
- Date:
- Fri Jun 01 12:54:02 2018 +0000
- Child:
- 1:c389d5f4913d
- Child:
- 2:156c427681f0
- Commit message:
- NUCLEO F767 SPI Serial EEPROM 25LC1024
Changed in this revision
| EE25LC1024.cpp | Show annotated file Show diff for this revision Revisions of this file |
| EE25LC1024.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/EE25LC1024.cpp Fri Jun 01 12:54:02 2018 +0000
@@ -0,0 +1,257 @@
+// EE25LC1024.cpp
+
+#include"EE25LC1024.h"
+
+// CONSTRUCTOR
+EE25LC1024::EE25LC1024(PinName mosi, PinName miso, PinName sclk, PinName cs) : SPI(mosi, miso, sclk), _cs(cs)
+{
+ this->format(SPI_NBIT, SPI_MODE);
+ this->frequency(SPI_FREQ);
+ chipDisable();
+}
+// READING
+
+
+void EE25LC1024::deepPowerDown(void)
+{
+ chipEnable();
+ this->write(DPD);
+ chipDisable();
+}
+
+ int EE25LC1024::ReleaseDPD_ReadSign(void)
+{
+ chipEnable();
+ this->write(RDID);
+ this->write(DUMMY_ADDR);
+ this->write(DUMMY_ADDR);
+ this->write(DUMMY_ADDR);
+ int response = this->write(DUMMY_ADDR);
+ chipDisable();
+ return response;
+}
+
+
+int EE25LC1024::readByte(int addr)
+{
+ chipEnable();
+ this->write(READ);
+ this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+ this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+ this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+ int response = this->write(DUMMY_ADDR);
+ chipDisable();
+ return response;
+}
+
+void EE25LC1024::readStream(int addr, char* buf, int count)
+{
+ if (count < 1)
+ return;
+ chipEnable();
+ this->write(READ);
+ this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+ this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+ this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+ for (int i = 0; i < count; i++) {
+ buf[i] = this->write(DUMMY_ADDR);
+ printf("i= %d :%c \r\n",i,buf[i]);
+ }
+ chipDisable();
+}
+
+// WRITING
+void EE25LC1024::writeByte(int addr, int data)
+{
+ writeEnable();
+ chipEnable();
+ this->write(WRITE);
+ this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+ this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+ this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+ this->write(data);
+ chipDisable();
+ writeDisable();
+ wait_ms(6);
+ // wait(WAIT_TIME);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails
+}
+
+void EE25LC1024::writeStream(int addr, char* buf, int count)
+{
+ if (count < 1)
+ return;
+ writeEnable();
+ chipEnable();
+ this->write(WRITE);
+ this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+ this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+ this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+ for (int i = 0; i < count; i++) {
+ this->write(buf[i]);
+ }
+ chipDisable();
+ writeDisable();
+ wait_ms(6);
+}
+
+void EE25LC1024::writeString(int addr, string str)
+{
+ if (str.length() < 1)
+ return;
+ writeEnable();
+ chipEnable();
+ this->write(WRITE);
+ this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+ this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+ this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+ for (int i = 0; i < str.length(); i++)
+ this->write(str.at(i));
+ chipDisable();
+ writeDisable();
+ wait_ms(6);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails
+}
+
+
+
+uint8_t EE25LC1024::readRegister()
+{
+
+ chipEnable();
+ this->write(RDSR);
+ uint8_t val=this->write(DUMMY_ADDR);
+ chipDisable();
+ //wait(WAIT_TIME);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails
+ //printf("value of reg is %X \r\n",val);
+ return(val);
+}
+//ERASING
+void EE25LC1024::chipErase()
+{
+ writeEnable();
+ chipEnable();
+ this->write(CE);
+ chipDisable();
+ writeDisable();
+ wait_ms(10);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails
+}
+
+
+void EE25LC1024::sectorErase(int addr)
+{
+ writeEnable();
+ chipEnable();
+ this->write(SE);
+ this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+ this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+ this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+ chipDisable();
+ writeDisable();
+ wait_ms(10);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails
+}
+
+void EE25LC1024::pageErase(int addr)
+{
+
+ writeEnable();
+ chipEnable();
+ this->write(SE);
+ this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+ this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+ this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+ chipDisable();
+ writeDisable();
+ wait_ms(6);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails
+
+
+}
+
+
+uint8_t EE25LC1024::checkIfBusy()
+{
+ uint8_t value=readRegister();
+ printf("Value of Status Reg=%X\r\n\r\n",value);
+ if((value&0x01)==0x01)
+ return 1;
+ else
+ return 0;
+}
+
+void EE25LC1024::writeRegister(uint8_t regValue)
+{
+ writeEnable();
+ chipEnable();
+ this->write(WRSR);
+ this->write(regValue);
+ chipDisable();
+ writeDisable();
+ wait(WAIT_TIME);//instead of wait poll for WIP flag of status reg or use checkIfBusy() function...see main for more dtails
+
+}
+
+
+void EE25LC1024::writeLong(int addr, long value)
+{
+ //Decomposition from a long to 4 bytes by using bitshift.
+ //One = Most significant -> Four = Least significant byte
+ uint8_t four = (value & 0xFF);
+ uint8_t three = ((value >> 8) & 0xFF);
+ uint8_t two = ((value >> 16) & 0xFF);
+ uint8_t one = ((value >> 24) & 0xFF);
+
+ writeEnable();
+ chipEnable();
+ this->write(WRITE);
+ this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+ this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+ this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+ this->write(four);
+ this->write(three);
+ this->write(two);
+ this->write(one);
+ chipDisable();
+ writeDisable();
+ wait_ms(6);
+}
+
+long EE25LC1024::raedLong(int addr)
+{
+ //Read the 4 bytes from the eeprom memory.
+ chipEnable();
+ this->write(READ);
+ this->write((addr & ADDR_BMASK2) >> ADDR_BSHIFT2);
+ this->write((addr & ADDR_BMASK1) >> ADDR_BSHIFT1);
+ this->write((addr & ADDR_BMASK0) >> ADDR_BSHIFT0);
+
+ long four = this->write(DUMMY_ADDR);
+ long three = this->write(DUMMY_ADDR);
+ long two = this->write(DUMMY_ADDR);
+ long one = this->write(DUMMY_ADDR);
+ chipDisable();
+ //Return the recomposed long by using bitshift.
+ return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
+
+}
+
+
+//ENABLE/DISABLE (private functions)
+void EE25LC1024::writeEnable()
+{
+ chipEnable();
+ this->write(WREN);
+ chipDisable();
+}
+void EE25LC1024::writeDisable()
+{
+ chipEnable();
+ this->write(WRDI);
+ chipDisable();
+}
+void EE25LC1024::chipEnable()
+{
+ _cs = 0;
+}
+void EE25LC1024::chipDisable()
+{
+ _cs = 1;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/EE25LC1024.h Fri Jun 01 12:54:02 2018 +0000
@@ -0,0 +1,74 @@
+// EE25LC1024.h
+
+#ifndef EE25LC1024_H
+#define EE25LC1024_H
+
+#include "mbed.h"
+#include <string>
+
+#define SPI_FREQ 5000000 //Change SPI Frequency Here
+#define SPI_MODE 0 // SPI Mode can be 0 or 3 . see data sheet
+#define SPI_NBIT 8 // Number of bits 8.
+
+
+
+#define DUMMY_ADDR 0x00
+#define WAIT_TIME 1
+
+#define ADDR_BMASK3 0xff000000
+#define ADDR_BMASK2 0x00ff0000
+#define ADDR_BMASK1 0x0000ff00
+#define ADDR_BMASK0 0x000000ff
+
+#define ADDR_BSHIFT3 24
+#define ADDR_BSHIFT2 16
+#define ADDR_BSHIFT1 8
+#define ADDR_BSHIFT0 0
+
+
+#define READ 0x03 //0000 0011 Read data from memory array beginning at selected address
+#define WRITE 0x02 //0000 0010 Write data to memory array beginning at selected address
+#define WREN 0x06 //0000 0110 Set the write enable latch (enable write operations)
+#define WRDI 0x04 //0000 0100 Reset the write enable latch (disable write operations)
+#define RDSR 0x05 //0000 0101 Read STATUS register
+#define WRSR 0x01 //0000 0001 Write STATUS register
+#define PE 0x42 //0100 0010 Page Erase – erase one page in memory array
+#define SE 0xD8 //1101 1000 Sector Erase – erase one sector in memory array
+#define CE 0xC7 //1100 0111 Chip Erase – erase all sectors in memory array
+#define RDID 0xAB //1010 1011 Release from Deep power-down and read electronic signature
+#define DPD 0xB9 //1011 1001 Deep Power-Down mode
+#define DUMMYBYTE 0x00 //Dummy byte for Read Operation
+
+class EE25LC1024: public SPI {
+public:
+ EE25LC1024(PinName mosi, PinName miso, PinName sclk, PinName cs);
+
+ void deepPowerDown(void);
+
+
+ int readByte(int addr); // takes a 24-bit (3 bytes) address and returns the data (1 byte) at that location
+ void readStream(int addr, char* buf, int count); // takes a 24-bit address, reads count bytes, and stores results in buf
+ int ReleaseDPD_ReadSign(void);
+ void writeByte(int addr, int data); // takes a 24-bit (3 bytes) address and a byte of data to write at that location
+ void writeStream(int addr, char* buf, int count); // write count bytes of data from buf to memory, starting at addr
+ void writeString(int add, string str);
+ void sectorErase(int addr);
+ void pageErase(int addr);
+ void chipErase(); //erase all data on chip
+ uint8_t readRegister();
+ uint8_t checkIfBusy(); // Check if IC is bury writing or erasing
+ void writeRegister(uint8_t regValue); // Write status register or configuration register
+ long raedLong(int address); // Read long int number
+ void writeLong(int addr, long value); // Write Long Integer Number
+private:
+ void writeEnable(); // write enable
+ void writeDisable(); // write disable
+ void chipEnable(); // chip enable
+ void chipDisable();
+ // chip disable
+
+ // SPI _spi;
+ DigitalOut _cs;
+};
+
+#endif
\ No newline at end of file