Go Down

Topic: EEPROM Nogo :( (Read 2650 times) previous topic - next topic

mattlamb99

Hi All,

I've built a custom board with a mega2560 on it. Extern crystal etc, some LEDS, buttons, UARTS, the works.
Programming is via ISP and working fine.
Everything is working to plan so far, except the EEPROM.
using EEPROM.write and EEPROM.read always returns 255.. nomatter what I write to it.
If i load up the eeprom example scripts, the same thing. always reads 255.

Some more info..
Mega2560 on custom board
I used the 'burn arduino bootloader via AVRISP mkII" When I first put the board together to set the fuses etc..
Now I am using the arduino IDE and uploading via ISP.

Code: [Select]

#include <EEPROM.h>
int addr =0;
void setup() {
}

void loop() {
EEPROM.write(addr, addr);
addr++;
if (addr == 254) {
addr = 0;
}
}


Any ideas where I'm going wrong. I have to assume its a fuse or something odd, but if I used the 'burn bootloader' I should be ok?

MonkeyKnight


Give this a whirl. This is a similar method I used when I was using a bank of eight external EEPROMs, to test read/write.

Post your output of it.



Code: [Select]

#include <EEPROM.h>

int address = 0;
byte value;

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

void loop(){
  int val = 55; // TODO stick a random number here
  EEPROM.write(address, val);
  int memLimit = 254;
 
  delay(100);//chill for a bit
 
  value = EEPROM.read(address);
 
  Serial.print(address);
  Serial.print("\t");
  Serial.print(value, DEC);
  if(value == val){
    Serial.print("\t pass ");
  }else{
    Serial.print("\t FAIL ");
  }
  Serial.println();
 
  address++;
 

  if (address == memLimit){
    address = 0;
  }
  delay(300);// chill again
}





***WARNING***

I did not actually upload this at this time. I am away from an Arduino. But it does compile.



mattlamb99

Thanks MonkeyKnight,

Code: [Select]
0       55       pass
1       55       pass
2       55       pass
3       55       pass
4       55       pass
5       55       pass
6       55       pass
7       55       pass
8       55       pass
9       55       pass
10      55       pass


Yes, that appears to work.

But when I comment out EEPROM.write(address, val) and reupload (it should read back 55's from all the addresses it just wrote to,
it reads:
Code: [Select]

0       255      FAIL
1       255      FAIL
2       255      FAIL
3       255      FAIL
4       255      FAIL
5       255      FAIL
6       255      FAIL


Why would it not read the 55's we set in the previous sketch?

Cheers

M.

MonkeyKnight



Whoa, that's crazy. Hummm... I do recall some odd issues when I was trying to address different chips once. But this is all internal EEPROM...

Can you post your board and upload options, and maybe the schematic to your board. Maybe there are some odd forces wiping out the EEPROM.


Try this. I only added a bool to write the first loop, then not write on even numbered loops. Lets see if it is erasing durring a reboot, or not. This should tell us that. Let it loop at least into the 10th address in the second loop; (you could just shorten the memLimit to speed it up)


Code: [Select]
#include <EEPROM.h>

int address = 0;
byte value;
bool firstPass = true;

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

void loop(){
  int val = 55; // TODO stick a random number here
 
  if(firstPass){
    EEPROM.write(address, val); 
  }
 
 
  int memLimit = 254;
 
  delay(100);//chill for a bit
 
  value = EEPROM.read(address);
 
  Serial.print(address);
  Serial.print("\t");
  Serial.print(value, DEC);
  if(value == val){
    Serial.print("\t pass ");
  }else{
    Serial.print("\t FAIL ");
  }
  Serial.println();
 
  address++;
 

  if (address == memLimit){
    address = 0;
    firstPass = !firstPass;
  }
  delay(300);// chill again
}

mattlamb99

What do you mean by board and upload options?
This is what I've added into boards.txt to allow upload over ISP :
Code: [Select]
##############################################################

mega2560isp.name=Arduino Mega 2560 via AVRISPmkII

mega2560isp.upload.protocol=stk500v2
mega2560isp.upload.maximum_size=258048
mega2560isp.upload.speed=115200
mega2560isp.upload.using=avrispmkii

mega2560isp.bootloader.low_fuses=0xFF
mega2560isp.bootloader.high_fuses=0xD8
mega2560isp.bootloader.extended_fuses=0xFD
mega2560isp.bootloader.path=stk500v2
mega2560isp.bootloader.file=stk500boot_v2_mega2560.hex
mega2560isp.bootloader.unlock_bits=0×3F
mega2560isp.bootloader.lock_bits=0×0F

mega2560isp.build.mcu=atmega2560
mega2560isp.build.f_cpu=16000000L
mega2560isp.build.core=arduino

##############################################################

Any idea if that would cause blanking of EEPROM on upload?

Your updated script has run through a few times, all PASS.
Seems to be if I upload a new sketch with READ only not write, it wont READ those values we wrote last time..

Cheers

M.

MonkeyKnight

Yes, thats what I was talking about.


So, that is a good report.

Can you now try this:

Code: [Select]
#include <EEPROM.h>

int address = 0;
byte value;
bool firstPass = false;

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

void loop(){
  int val = 55; // TODO stick a random number here
 
  if(firstPass){
    EEPROM.write(address, val); 
  }
 
 
  int memLimit = 10;
 
  delay(100);//chill for a bit
 
  value = EEPROM.read(address);
 
  Serial.print(address);
  Serial.print("\t");
  Serial.print(value, DEC);
  if(value == val){
    Serial.print("\t pass ");
  }else{
    Serial.print("\t FAIL ");
  }
  Serial.println();
 
  address++;
 

  if (address == memLimit){
    address = 0;
    firstPass = !firstPass;
  }
  delay(300);// chill again
}



But now, this time: I suspect that you will have a fail the first ten times. Let it cycle a couple times, making sure that it only fails the first ten, as planned.

Then reset your microcontroller,WITHOUT reprogramming it. Lets see what happens.


I think that what is happening, is the EEPROM is getting erased durring an upload.

mattlamb99

You are correct. Fail first 10 times, then pass. Any reboot of micro is still a PASS, but when I upload again it fails first 10 times.

I guess its to do with the ISP upload. OK. I can use a different method to deal with that.

I'll put the 'factory defaults' in the program, and use a magic bit (eeprom address254)  like such :
(pseudocode)
Code: [Select]

if (eeprom.read(254) != 1) {
//eeprom not been written, write factory defaults.
eeprom.write(254,1);
} else {

//eeprom has data we can read out now..

eeprom.read(xxx...);

}
}


Thanks for your help!

MonkeyKnight

No problem!

But to further help you, it looks like the fourth bit in the high_fuses is set. Unset it, and it should preserve the EEPROM.

***WARNING***  -  Setting any fuses could (although rare) brick your micro. Read spec docs appropriately.



mattlamb99

I've never really known anything about fuses. So I put those hex values from the boards.txt file into an online calculator which shows me what each bit does..

So yes, you are right! Set that bit and my problems might go away. But however I've already coded my work around, and to be honest its probably a better way of doing it. This way when I make more units I don't need to load a special sketch to set eeprom values, it can all be self contained now.

If you are curious as to my project.. take a look back here posted nearly a year ago!
http://arduino.cc/forum/index.php/topic,55714.msg399366.html

Go Up