Pages: 1 [2]   Go Down
Author Topic: Arduino Uno and ATMEL memory chip using SPI  (Read 6599 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You must be using a different chip than the AT25DF641 you originally used if 239 is the ID. Without knowing what you are using I can't check and see if the opcodes you are using are correct.

Also, I think your
Code:
myByte = SPI.transfer(0x255);
is incorrect as that is a hex 255 not a decimal 255. I think you would want
Code:
myByte = SPI.transfer(0xFF);
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I should have included the memory chip I am using now in my post. Its a winbond: http://www.winbond.com.tw/NR/rdonlyres/591A37FF-007C-4E99-956C-F7EE4A6D9A8F/0/W25Q64BV.pdf

This chip requires three address bytes of B00000000 after the read manufacturer id command.

And thanks sark, I didnt catch that.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18778
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Based on the ID you gave (239 = 0b11101111) and looking up this page:

http://www.idhw.com/textual/chip/jedec_spd_man.html

I am guessing you have a NEXCOM chip of some sort. Possibly something like the NX25P40.

Their data sheet does indeed say you get the manufacturer ID with 0x90, you then clock in 3 lots of zero, and on the fourth byte it responds with the manufacturer ID, and then the device ID.

As to why that only sometimes works? Well maybe read out the device ID as well. Maybe the chip is sending it, but you haven't clocked it out, so it is waiting? Just a guess. Try adding in the extra transfer and see what happens.

...

Your more recent post says you are now using the W25Q64BV ... I see you can query the manufacturer ID using 0x9F (as well as 0x90 which requires the 3 extra bytes) so maybe try that.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18778
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I got the Atmel AT25DF041A chip today, and after quite a struggle, got it to program. smiley

Some of the challenges were:

  • Soldering the 8-SOIC SMD device without destroying it smiley-wink
  • Getting the buffer chip to work
  • Understanding the AT25DF041A documentation
  • Working out how to disable write-protect, and sector protection
  • Reading the status register to know when the chip wasn't busy any more

Anyway, the code below works. I used the Atmel AT25DF041A "4-megabit 2.3-volt or 2.7-volt Minimum SPI Serial Flash Memory" with the SN54AHC125 "Quadruple Bus Buffer Gates with 3-state outputs" as a 3.3 to 5v buffer.

The buffer and EEPROM chip were powered off the 3.3V Arduino output pin.

I didn't need to do custom SPI software, the standard library worked fine.

To avoid spurious data on the SPI lines I wired up pin 9 to the buffer chip "3-state enable" pins so that the buffer could be enabled in an orderly way. This is brought low to allow communication with the EEPROM.

Code:
// Written by Nick Gammon
// 10th March 2011

#include <SPI.h>

#define CHIP_SELECT 10   // for EEPROM
#define BUFFER_ENABLE 9  // for SN54AHC125 buffer

// AT25DF041A EEPROM commands

// reading
#define ReadArray             0x0B
#define ReadArrayLowFrequency 0x03

// programming
#define BlockErase4Kb       0x20
#define BlockErase32Kb      0x52
#define BlockErase64Kb      0xD8
#define ChipErase           0x60
#define ByteProgram         0x02
#define SequentialProgram   0xAD

// protection
#define WriteEnable           0x06
#define WriteDisable          0x04
#define ProtectSector         0x36
#define UnProtectSector       0x39
#define ReadSectorProtection  0x3C

// status
#define ReadStatus 0x05
#define WriteStatus 0x01

// miscellaneous
#define ReadManufacturer     0x9F
#define DeepPowerDown        0xB9
#define ResumeFromPowerDown  0xAB

// wait until chip not busy
void notBusy ()
{
  digitalWrite (CHIP_SELECT, LOW);
  SPI.transfer (ReadStatus);       
  // wait until busy bit cleared
  while (SPI.transfer (0) & 1)
     {}
  digitalWrite (CHIP_SELECT, HIGH); 
}  // end notBusy

// enable writing
void writeEnable ()
{
 notBusy ();
 
 digitalWrite (CHIP_SELECT, LOW);
 SPI.transfer (WriteEnable);       
 digitalWrite (CHIP_SELECT, HIGH); 
}  // end of writeEnable

// read device status
byte readStatus (void)
{
 digitalWrite (CHIP_SELECT, LOW);
 SPI.transfer (ReadStatus);       
 byte status = SPI.transfer (status);       
 digitalWrite (CHIP_SELECT, HIGH); 
 
 return status;
}  // end of readStatus

// write status register
void writeStatus (const byte status)
{
   writeEnable ();
   notBusy ();  // wait until ready
   
   digitalWrite (CHIP_SELECT, LOW);
   SPI.transfer (WriteStatus);       
   SPI.transfer (status);       
   digitalWrite (CHIP_SELECT, HIGH); 
}  // end of writeStatus

// send a command to the EEPROM followed by a 3-byte address
void sendCommandAndAddress (const byte command, const unsigned long addr)
{
  SPI.transfer (command);       
  SPI.transfer ((addr >> 16) & 0xFF);       
  SPI.transfer ((addr >> 8) & 0xFF);       
  SPI.transfer (addr & 0xFF);       
}  // end of sendCommandAndAddress

// write len (max 256) bytes to device

// Note that if writing multiple bytes the address plus
//  length must not cross a 256-byte boundary or it will "wrap"
void writeEEPROM (const unsigned long addr, byte * data, byte len)
{
  // now write to it
  writeEnable ();
 
  notBusy ();  // wait until ready
  digitalWrite (CHIP_SELECT, LOW);
  sendCommandAndAddress (ByteProgram, addr);
  for ( ; len ; --len)
    SPI.transfer (*data++);       
  digitalWrite (CHIP_SELECT, HIGH); 
  notBusy ();
} // end of writeEEPROM

// write one byte to device
void writeEEPROM (unsigned long addr, byte data)
{
  writeEEPROM (addr, &data, 1);
} // end of writeEEPROM

// read len bytes from device
void readEEPROM (const unsigned long addr, byte * data, unsigned int len)
{
  notBusy ();  // wait until ready
  digitalWrite (CHIP_SELECT, LOW);
  sendCommandAndAddress (ReadArray, addr);

  SPI.transfer (0);  // clock in "don't care" byte
 
  for ( ; len ; --len)
   *data++ = SPI.transfer (0);       
  digitalWrite (CHIP_SELECT, HIGH); 
 
}  // end of readEEPROM

// erase a 4Kb block of bytes which contains addr
void eraseEEPROM (const unsigned long addr)
{
  writeEnable ();

  notBusy ();  // wait until ready
  digitalWrite (CHIP_SELECT, LOW);
  sendCommandAndAddress (BlockErase4Kb, addr);
  digitalWrite (CHIP_SELECT, HIGH); 
 
}  // end of eraseEEPROM

// show device info and status
void info ()
{
 
  notBusy (); // wait until ready
 
  digitalWrite (CHIP_SELECT, LOW);
  SPI.transfer (ReadManufacturer);       
 
  Serial.print ("Manufacturer: ");
  Serial.println (SPI.transfer (0), HEX);
  Serial.print ("Device ID Part 1: ");
  Serial.println (SPI.transfer (0), HEX);
  Serial.print ("Device ID Part 2: ");
  Serial.println (SPI.transfer (0), HEX);
  Serial.print ("Extended Information Length: ");
  Serial.println (SPI.transfer (0),HEX);

  digitalWrite (CHIP_SELECT, HIGH);
 
  Serial.print ("Status: ");
  Serial.println (readStatus (), HEX);

} // end of info

void setup ()
{
 
  Serial.begin (9600);
  SPI.begin ();
   
  // enable the SN54AHC125 output enable peons (more work?)
  pinMode (BUFFER_ENABLE, OUTPUT);  // buffer chip enable
  digitalWrite (BUFFER_ENABLE,LOW);  // enable

  // global unprotect
  writeStatus (0);
 
  Serial.println ("Status:");
  info ();
   
  // test: write a string to address 0x1000

#define TESTADDRESS 0x1000

  eraseEEPROM (TESTADDRESS);
 
  byte hello [] = "Hello, World!";
 
  writeEEPROM (TESTADDRESS, hello, sizeof hello);
 
  // read back to confirm
  byte test [sizeof hello] = { 0 } ;
  readEEPROM (TESTADDRESS, test, sizeof test);

  Serial.println ((char *) test);  // display to confirm
 
}  // end of setup

void loop()
{
 
}  // end of loop

This is basically a demo program, but incorporates routines to write up to 256 bytes and read up to 65536 bytes. There is a lot of complexity in the chip itself which would have obscured the demo if I tried to incorporate it all, like sector protection, writing pages, erasing blocks of various sizes and so on.

One point which wasn't initially obvious is that you have to keep enabling write-enable. Every write operation cancels it (as a safety precaution no doubt). So to disable software protection you first do a write-enable. Then to erase some memory you have to first do a write-enable again. And then to write to the memory you have to first do a write-enable.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Awesome! Got mine working with your code Nick, thanks A LOT!

Now to get the sequential program mode to work so I don't have to mess with write enabled every single time.  smiley-roll-sweat
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18778
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My pleasure.

I've done an extended version here:

http://www.gammon.com.au/forum/?id=10990

That includes pinouts, a wiring diagram, and timing figures.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nick,

Do you have any pointers (pun intended) to change the byte variable "hello" in your code:
Code:
byte hello [] = "Hello, World!";

to actually be variable, rather than static text. I'm planning on reading a value from an analog pin, then writing that variable to the eeprom, so it will be changing for every write. I've never worked with c pointers much and am rather confused on what I need to do with the pointers.

Any help is appreciated.

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18778
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Er, well, instead of:

Code:
byte hello [] = "something";

You need a pointer to byte, eg.

Code:
byte * hello = "something";

Now instead of using sizeof (which will just return 2, which is the size of the pointer) you use strlen, eg.

Code:
writeEEPROM (TESTADDRESS, hello, strlen (hello) + 1);

The "+ 1" is because you need the size of the string which is pointed to, plus 1 byte for the 0x00 which marks the end of the string.

It's hard to give more advice without seeing what you are trying to do. Pointers need to point to something, and that something needs to be allocated somewhere. And of course the address you are writing to is likely to change too.

If you are pulling in an analog variable (an int, I think) then you could do something like this:

Code:
int addr = 1000;

void loop ()
  {
  int reading = analogRead (1);  // read A1
  writeEEPROM (addr, (byte *) &reading, sizeof reading);
  addr += sizeof reading;
  }


The "&" operator takes the address of "reading" - in other words, a pointer - and thus you can write that. Then we increment the address by the number of bytes (2, I think) and then we are ready for the next reading. That is the general idea, anyway. The actual program is likely to be a bit more complex. For example, you somehow need to know how many readings you took. I think the EEPROM starts of with 0xFF in every byte, so you could read back the readings until you got 0xFFFF, in which case you know you came to the end of them.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So this is what i've got right now:
Code:
// Written by Nick Gammon
// 10th March 2011

#include <SPI.h>

#define CHIP_SELECT 10   // for EEPROM

// AT25DF041A EEPROM commands

// reading
#define ReadArray             0x0B
#define ReadArrayLowFrequency 0x03

// programming
#define BlockErase4Kb       0x20
#define BlockErase32Kb      0x52
#define BlockErase64Kb      0xD8
#define ChipErase           0x60
#define ByteProgram         0x02
#define SequentialProgram   0xAD

// protection
#define WriteEnable           0x06
#define WriteDisable          0x04
#define ProtectSector         0x36
#define UnProtectSector       0x39
#define ReadSectorProtection  0x3C

// status
#define ReadStatus 0x05
#define WriteStatus 0x01

// miscellaneous
#define ReadManufacturer     0x9F
#define DeepPowerDown        0xB9
#define ResumeFromPowerDown  0xAB

// wait until chip not busy
void notBusy ()
{
  digitalWrite (CHIP_SELECT, LOW);
  SPI.transfer (ReadStatus);       
  // wait until busy bit cleared
  while (SPI.transfer (0) & 1)
     {}
  digitalWrite (CHIP_SELECT, HIGH); 
}  // end notBusy

// enable writing
void writeEnable ()
{
 notBusy ();
 
 digitalWrite (CHIP_SELECT, LOW);
 SPI.transfer (WriteEnable);       
 digitalWrite (CHIP_SELECT, HIGH); 
}  // end of writeEnable

// read device status
byte readStatus (void)
{
 digitalWrite (CHIP_SELECT, LOW);
 SPI.transfer (ReadStatus);       
 byte status = SPI.transfer (status);       
 digitalWrite (CHIP_SELECT, HIGH); 
 
 return status;
}  // end of readStatus

// write status register
void writeStatus (const byte status)
{
   writeEnable ();
   notBusy ();  // wait until ready
   
   digitalWrite (CHIP_SELECT, LOW);
   SPI.transfer (WriteStatus);       
   SPI.transfer (status);       
   digitalWrite (CHIP_SELECT, HIGH); 
}  // end of writeStatus

// send a command to the EEPROM followed by a 3-byte address
void sendCommandAndAddress (const byte command, const unsigned long addr)
{
  SPI.transfer (command);       
  SPI.transfer ((addr >> 16) & 0xFF);       
  SPI.transfer ((addr >> 8) & 0xFF);       
  SPI.transfer (addr & 0xFF);       
}  // end of sendCommandAndAddress

// write len (max 256) bytes to device

// Note that if writing multiple bytes the address plus
//  length must not cross a 256-byte boundary or it will "wrap"
void writeEEPROM (const unsigned long addr, byte * data, byte len)
{
  // now write to it
  writeEnable ();
 
  notBusy ();  // wait until ready
  digitalWrite (CHIP_SELECT, LOW);
  sendCommandAndAddress (ByteProgram, addr);
  for ( ; len ; --len)
    SPI.transfer (*data++);       
  digitalWrite (CHIP_SELECT, HIGH); 
  notBusy ();
} // end of writeEEPROM

// write one byte to device
void writeEEPROM (unsigned long addr, byte data)
{
  writeEEPROM (addr, &data, 1);
} // end of writeEEPROM

// read len bytes from device
void readEEPROM (const unsigned long addr, byte * data, unsigned int len)
{
  notBusy ();  // wait until ready
  digitalWrite (CHIP_SELECT, LOW);
  sendCommandAndAddress (ReadArray, addr);

  SPI.transfer (0);  // clock in "don't care" byte
 
  for ( ; len ; --len)
   *data++ = SPI.transfer (0);       
  digitalWrite (CHIP_SELECT, HIGH); 
 
}  // end of readEEPROM

// erase a 4Kb block of bytes which contains addr
void eraseEEPROM (const unsigned long addr)
{
  writeEnable ();

  notBusy ();  // wait until ready
  digitalWrite (CHIP_SELECT, LOW);
  sendCommandAndAddress (BlockErase4Kb, addr);
  digitalWrite (CHIP_SELECT, HIGH); 
 
}  // end of eraseEEPROM

// show device info and status
void info ()
{
 
  notBusy (); // wait until ready
 
  digitalWrite (CHIP_SELECT, LOW);
  SPI.transfer (ReadManufacturer);       
 
  Serial.print ("Manufacturer: ");
  Serial.println (SPI.transfer (0), HEX);
  Serial.print ("Device ID Part 1: ");
  Serial.println (SPI.transfer (0), HEX);
  Serial.print ("Device ID Part 2: ");
  Serial.println (SPI.transfer (0), HEX);
  Serial.print ("Extended Information Length: ");
  Serial.println (SPI.transfer (0),HEX);

  digitalWrite (CHIP_SELECT, HIGH);
 
  Serial.print ("Status: ");
  Serial.println (readStatus (), HEX);

} // end of info

void setup ()
{
 
  Serial.begin (9600);
  SPI.begin ();

  // global unprotect
  writeStatus (0);
 
  Serial.println ("Status:");
  info ();
   
  // test: write a string to address 0x1000

#define TESTADDRESS 0x1000

  eraseEEPROM (TESTADDRESS);
 
  byte hello [] = "Hello, World!";
 
  writeEEPROM (TESTADDRESS, hello, sizeof hello);
 
  // read back to confirm
  byte test [sizeof hello] = { 0 } ;
  readEEPROM (TESTADDRESS, test, sizeof test);

  Serial.println ((char *) test);  // display to confirm
 
}  // end of setup

int address = 0;
int analogInPin = A6;
int sensorValue = 0;

void loop ()
  {
  sensorValue = analogRead (analogInPin);
  writeEEPROM (address, (byte *) &sensorValue, sizeof sensorValue);
 
  byte test2 [2]
  readEEPROM (address, test2, sizeof test2);
  Serial.println ((char *) test2);
 
  addr += sizeof sensorValue;
  Serial.print("analog pin: ");Serial.print (sensorValue, DEC);Serial.print('\n',BYTE); //debug
  Serial.print("address: ");Serial.print (address, DEC);Serial.print('\n',BYTE); //debug
  }  // end of loop

I'm confused on how to read the int after it has been written. I had the test2 byte in there just so I could try to read something, but all I got out was garbage.

The outcome I'm hoping to have is to be able to write data to the eeprom that is read from the analog pin over a period of time, then go back and read all that data. Right now I'm just testing with a potentiometer to change the values on the analog pin from 0-1023. Any suggestions are much appreciated.  smiley
Logged

Smithfield, Rhode Island
Offline Offline
God Member
*****
Karma: 3
Posts: 843
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just wondering, why did you select the 25 series chips over the 45 series?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18778
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop ()
  {
  sensorValue = analogRead (analogInPin);
  writeEEPROM (address, (byte *) &sensorValue, sizeof sensorValue);
 
  byte test2 [2]
  readEEPROM (address, test2, sizeof test2);
  Serial.println ((char *) test2);
 
  addr += sizeof sensorValue;
  Serial.print("analog pin: ");Serial.print (sensorValue, DEC);Serial.print('\n',BYTE); //debug
  Serial.print("address: ");Serial.print (address, DEC);Serial.print('\n',BYTE); //debug
  }  // end of loop

I'm confused on how to read the int after it has been written. I had the test2 byte in there just so I could try to read something, but all I got out was garbage.

When writing we passed the address of sensorValue (ie. &sensorValue) so you have to do the same thing when reading back. That is:

Code:
int test;
 readEEPROM (address, &test, sizeof test);

That reads back into the 2-byte integer in the same way we wrote the data tout.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So now I'm trying to figure out how to read data back after it has been powered off. I execute the readEEPROM command right in the setup function before anything else has been executed.
Code:
int reading;
for (int I=0;I<100;I=I+2)
  {
    readEEPROM (I, &reading, sizeof reading);
    Serial.println (reading);
  }
The results I get back are all 255 (hex FF). I know these addresses were previously written to with different values, and I'm not sure what to do from here. Any ideas?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18778
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does it work before being powered off? Can you post all your code please?
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yah, it seems to be working fine, except every time it gets powered on it will read 255 for all the values in the for loop in the setup function. Then it continues to the loop function and works fine. I have a potentiometer on the analog pin to change the values that are written to make sure the values are actually changing when getting written. When I power it off and back on I would expect those values that were written previously to be read out right away, but that's not happening.

Code:
// Written by Nick Gammon
// 10th March 2011

#include <SPI.h>

#define CHIP_SELECT 10   // for EEPROM

// AT25DF041A EEPROM commands

// reading
#define ReadArray             0x0B
#define ReadArrayLowFrequency 0x03

// programming
#define BlockErase4Kb       0x20
#define BlockErase32Kb      0x52
#define BlockErase64Kb      0xD8
#define ChipErase           0x60
#define ByteProgram         0x02
#define SequentialProgram   0xAD

// protection
#define WriteEnable           0x06
#define WriteDisable          0x04
#define ProtectSector         0x36
#define UnProtectSector       0x39
#define ReadSectorProtection  0x3C

// status
#define ReadStatus 0x05
#define WriteStatus 0x01

// miscellaneous
#define ReadManufacturer     0x9F
#define DeepPowerDown        0xB9
#define ResumeFromPowerDown  0xAB

int address = 0;
int analogInPin = A6;
int sensorValue = 0;
int reading;

// wait until chip not busy
void notBusy ()
{
  digitalWrite (CHIP_SELECT, LOW);
  SPI.transfer (ReadStatus);       
  // wait until busy bit cleared
  while (SPI.transfer (0) & 1)
     {}
  digitalWrite (CHIP_SELECT, HIGH); 
}  // end notBusy

// enable writing
void writeEnable ()
{
 notBusy ();
 
 digitalWrite (CHIP_SELECT, LOW);
 SPI.transfer (WriteEnable);       
 digitalWrite (CHIP_SELECT, HIGH); 
}  // end of writeEnable

// read device status
byte readStatus (void)
{
 digitalWrite (CHIP_SELECT, LOW);
 SPI.transfer (ReadStatus);       
 byte status = SPI.transfer (status);       
 digitalWrite (CHIP_SELECT, HIGH); 
 
 return status;
}  // end of readStatus

// write status register
void writeStatus (const byte status)
{
   writeEnable ();
   notBusy ();  // wait until ready
   
   digitalWrite (CHIP_SELECT, LOW);
   SPI.transfer (WriteStatus);       
   SPI.transfer (status);       
   digitalWrite (CHIP_SELECT, HIGH); 
}  // end of writeStatus

// send a command to the EEPROM followed by a 3-byte address
void sendCommandAndAddress (const byte command, const unsigned long addr)
{
  SPI.transfer (command);       
  SPI.transfer ((addr >> 16) & 0xFF);       
  SPI.transfer ((addr >> 8) & 0xFF);       
  SPI.transfer (addr & 0xFF);       
}  // end of sendCommandAndAddress

// write len (max 256) bytes to device

// Note that if writing multiple bytes the address plus
//  length must not cross a 256-byte boundary or it will "wrap"
void writeEEPROM (const unsigned long addr, byte * data, byte len)
{
  // now write to it
  writeEnable ();
 
  notBusy ();  // wait until ready
  digitalWrite (CHIP_SELECT, LOW);
  sendCommandAndAddress (ByteProgram, addr);
  for ( ; len ; --len)
    SPI.transfer (*data++);       
  digitalWrite (CHIP_SELECT, HIGH); 
  notBusy ();
} // end of writeEEPROM

// write one byte to device
void writeEEPROM (unsigned long addr, byte data)
{
  writeEEPROM (addr, &data, 1);
} // end of writeEEPROM

// read len bytes from device
void readEEPROM (const unsigned long addr, int * data, unsigned int len)
{
  notBusy ();  // wait until ready
  digitalWrite (CHIP_SELECT, LOW);
  sendCommandAndAddress (ReadArray, addr);

  SPI.transfer (0);  // clock in "don't care" byte
 
  for ( ; len ; --len)
   *data++ = SPI.transfer (0);       
  digitalWrite (CHIP_SELECT, HIGH); 
 
}  // end of readEEPROM

// erase a 4Kb block of bytes which contains addr
void eraseEEPROM (const unsigned long addr)
{
  writeEnable ();

  notBusy ();  // wait until ready
  digitalWrite (CHIP_SELECT, LOW);
  sendCommandAndAddress (BlockErase4Kb, addr);
  digitalWrite (CHIP_SELECT, HIGH); 
 
}  // end of eraseEEPROM

// show device info and status
void info ()
{
 
  notBusy (); // wait until ready
 
  digitalWrite (CHIP_SELECT, LOW);
  SPI.transfer (ReadManufacturer);       
 
  Serial.print ("Manufacturer: ");
  Serial.println (SPI.transfer (0), HEX);
  Serial.print ("Device ID Part 1: ");
  Serial.println (SPI.transfer (0), HEX);
  Serial.print ("Device ID Part 2: ");
  Serial.println (SPI.transfer (0), HEX);
  Serial.print ("Extended Information Length: ");
  Serial.println (SPI.transfer (0),HEX);

  digitalWrite (CHIP_SELECT, HIGH);
 
  Serial.print ("Status: ");
  Serial.println (readStatus (), HEX);

} // end of info

void setup ()
{
 
  Serial.begin (115200);
  SPI.begin ();

  // global unprotect
  writeStatus (0);
 
  Serial.print('\n',BYTE);
  Serial.println ("Status:");
  info ();

  for (int I=0;I<100;I=I+2)
  {
    readEEPROM (I, &reading, sizeof reading);
    Serial.println (reading);
  } 
}  // end of setup

void loop ()
  {
   
  eraseEEPROM (address);
  sensorValue = analogRead (analogInPin);
  writeEEPROM (address, (byte *) &sensorValue, sizeof sensorValue);
 
  readEEPROM (address, &reading, sizeof reading); 
  Serial.println (reading);
 
  address += sizeof sensorValue;
  }  // end of loop

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18778
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm a little troubled by this:

Code:
void loop ()
  {
   
  eraseEEPROM (address);
  sensorValue = analogRead (analogInPin);
  writeEEPROM (address, (byte *) &sensorValue, sizeof sensorValue);
 
  readEEPROM (address, &reading, sizeof reading); 
  Serial.println (reading);
 
  address += sizeof sensorValue;
  }  // end of loop

Your main loop is going to take a reading and write it to the EEPROM, very rapidly. These chips are not designed for billions of writes. I would have expected you to place a delay (eg. a second) before writing another reading. That way your chip is written to much more slowly. I can't see on their spec sheet how many writes or erases it can tolerate, but it wouldn't be infinite.

The second issue is this line:

Code:
  eraseEEPROM (address);

According to the comments in my code:

Code:
// erase a 4Kb block of bytes which contains addr
void eraseEEPROM (const unsigned long addr)
{
  writeEnable ();

  notBusy ();  // wait until ready
  digitalWrite (CHIP_SELECT, LOW);
  sendCommandAndAddress (BlockErase4Kb, addr);
  digitalWrite (CHIP_SELECT, HIGH); 
 
}  // end of eraseEEPROM

That erase 4Kb every time (in other words, through your main loop you erase all 4 Kb of the initial memory, every time you take a reading. I take it this is unintentional?

The flash chips are designed so that you have to erase "pages" of data back to 0xFF, and then you write to them, which changes some 1s to 0s. You can't erase individual bytes.

So you really need to design in such a way that, when you change to a new 4Kb block, you erase it first. And then don't do it again until you move onto another 4Kb block.

This also relates to the issue of writing very quickly. Without some pause, you will have written to all the chip, erased, the data, and written over it, probably every minute or so.

You may also want to consider keeping track of the current position (to write to). Another recent post had the poster writing his current position to addresses 0/1, so he read that in at setup, to find where to write his next reading to. You might want to do something like that.
Logged


Pages: 1 [2]   Go Up
Jump to: