How to use external EEPROM (SPI)

I got an external EEPROM (AT25040B) using SPI, but I'm having a hard time understanding how to read and write to it. I understand the basics of SPI, but I can't seem to get it working.

Is there a library that works with this chip?

awwende:
I got an external EEPROM (AT25040B) using SPI, but I'm having a hard time understanding how to read and write to it. I understand the basics of SPI, but I can't seem to get it working.

Is there a library that works with this chip?

#include <SPI.h>
#define csPIN 10


uint8_t readByteAt(uint8_t cs,uint16_t adr){
SPI.beginTransaction(SPISettings(4000000,MSBFIRST,SPI_MODE0));
digitalWrite(cs,LOW);
uint8_t b=highByte(adr);
b = b <<3;  //move address bit 8 to bit 11
b = b | 0x03;  // read command
SPI.transfer(b); // send cmd + address bit 8
SPI.transfer(lowByte(adr)); // low byte of address
b= SPI.transfer(0); // read the actual byte
digitalWrite(cs,HIGH);
SPI.endTransaction();
return b;
}

void setup(){
Serial.begin(19200);
digitalWrite(csPIN,HIGH);
pinMode(csPIN,output);
SPI.begin();

}


void loop(){
char ch[30];
for (uint16_t i=0;i<512;i++){
  if((i&15)==0){
    sprintf(ch,"\n0x%03X: ",i);
    Serial.print(ch);
    }
  sprintf(ch," 0x%02X",readByteAt(csPIN,i));
  Serial.print(ch);
  }
}

This code assumes the device has CS pin connected to Arduino pin 10.
!WP is connected to VCC,!HOLD is connected to VCC

You will need to create 4 more functions:
bool writeByteAt(cs,adr,data); // set write enable, actually write byte, fail attemp to write to protected area
bool ready(cs); // read status register, to verify chip has completed prior write cmd
uint8_t readStatus(cs); // read status register, return write protection status and/or busy
void writeStatus(cs); // set write protection for areas of chip

Chuck.

Chuck,

Thank you! Yes, those are correct assumptions. Part of the reason I went with an external EEPROM was because I wanted to learn more about SPI, and how to transfer data. So if you could be kind enough to explain some of your comments.

bool writeByteAt(cs,adr,data); // set write enable, actually write byte, fail attemp to write to protected area

I'm going to try and see if I can make the function to write, assuming I can base it off of your readByteAt().

bool ready(cs); // read status register, to verify chip has completed prior write cmd

I believe I can make the ready function by reading the status register bit 0, correct?

uint8_t readStatus(cs); // read status register, return write protection status and/or busy

How is this not the same as ready(cs)?

void writeStatus(cs); // set write protection for areas of chip

What's the purpose of this function, or why do I want a write protection for areas of the chip?

awwende:
Chuck,

Thank you! Yes, those are correct assumptions. Part of the reason I went with an external EEPROM was because I wanted to learn more about SPI, and how to transfer data. So if you could be kind enough to explain some of your comments.

I'm going to try and see if I can make the function to write, assuming I can base it off of your readByteAt().

Pretty close, If you are going to use the 'write protection' feature of this chip, you will either have to verify that the addressed byte is in a 'writeable' section of the chip or verify after the write.

awwende:
Chuck,

bool ready(cs); // read status register, to verify chip has completed prior write cmd

I believe I can make the ready function by reading the status register bit 0, correct?

yep, just send the correct command code and parse bit zero.

awwende:
uint8_t readStatus(cs); // read status register, return write protection status and/or busy

How is this not the same as ready(cs)?

The status byte also has the 'write protect' map. if you made readStatus() you could use it to make a ready() like this:

bool ready(uint8_t csPin){
return (readStatus(csPin)&1);
}

awwende:
void writeStatus(cs); // set write protection for areas of chip

What's the purpose of this function, or why do I want a write protection for areas of the chip?

I kind of messed up that one, it should be:

void writeStatus(cs,writeProtectMap);

The only reason to make this feature would be in case the data in the EEPROM never needs to change.

Chuck.