Teensy 4.1 EEPROM Limit: Cannot write more than 29 values

Status
Not open for further replies.

jimmie

Well-known member
I would appreciate the community's help with the EEPROM issue I am having.

I am storing in EEPROM 33 String values. Each value is a String that is a maximum of 25 bytes. Given that the EEPROM for the Teensy 4.1 is 4284 bytes, I should have more than enough space.

However, using the code below, I can write a maximum of 29 values. After that, nothing is written to the EEPROM. What is causing this problem?

Code:
void fillEEPROM()
{
  String myStr[33];

  myStr[0] = "1";
  myStr[1] = "300";
  myStr[2] = "168";
  myStr[3] = "10";
...
...

  for (int i = 0; i < 33; i++)
   {
     writeStringToEEPROM((25 * i), myStr[i].trim());
   }
}

void writeStringToEEPROM(int addrOffset, const String &strToWrite)
{
  byte len = strToWrite.length();
  EEPROM.write(addrOffset, len);

  for (int i = 0; i < len; i++)
  {
    EEPROM.write(addrOffset  + i, strToWrite[i]);
  }
}
 
Could you give a complete program, so I can just copy it into Arduino and upload to a Teensy 4.1 to quickly reproduce the problem?
 
I tried to fill in the missing code, but I was not able to reproduce the problem.

Here's the complete program I ran to write all 33 strings.

Code:
#include <EEPROM.h>

void setup() {
  while (!Serial) ;
  Serial.println("Writing 33 strings to EEPROM");
  
  String myStr[33];

  myStr[0] = "1";
  myStr[1] = "300";
  myStr[2] = "168";
  myStr[3] = "10";
  myStr[4] = "1032";
  myStr[5] = "test";
  myStr[6] = "of";
  myStr[7] = "string";
  myStr[8] = "in";
  myStr[9] = "eeprom";

  myStr[10] = "abc1";
  myStr[11] = "abc300";
  myStr[12] = "abc168";
  myStr[13] = "abc10";
  myStr[14] = "abc1032";
  myStr[15] = "testabc";
  myStr[16] = "ofabc";
  myStr[17] = "stringabc";
  myStr[18] = "inabc";
  myStr[19] = "eepromabc";

  myStr[20] = "xyz+1";
  myStr[21] = "xyz+300";
  myStr[22] = "xyz+168";
  myStr[23] = "xyz+10";
  myStr[24] = "xyz+1032";
  myStr[25] = "xyz+test";
  myStr[26] = "xyz+of";
  myStr[27] = "xyz+string";
  myStr[28] = "xyz+in";
  myStr[29] = "xyz+eeprom";

  myStr[30] = "this";
  myStr[31] = "is the";
  myStr[32] = "end";

  for (int i = 0; i < 33; i++)  {
    writeStringToEEPROM((25 * i), myStr[i].trim());
  }
  Serial.println("finished writing");
}

void writeStringToEEPROM(int addrOffset, const String &strToWrite) {
  byte len = strToWrite.length();
  EEPROM.write(addrOffset, len);

  addrOffset = addrOffset + 1; // start writing string AFTER length byte
  for (int i = 0; i < len; i++) {
    EEPROM.write(addrOffset + i, strToWrite[i]);
  }
}

void loop() {
}

Then I ran this program which reads them all back and prints to the serial monitor.

Code:
#include <EEPROM.h>

void setup() {
  while (!Serial) ;
  Serial.println("Reading 33 strings to EEPROM");
  
  for (int i=0; i < 33; i++) {
    Serial.print(i);
    Serial.print(": ");
    char buf[26];
    readstring(i, buf);
    Serial.println(buf);
  }
}

void readstring(int num, char *buf)
{
  int addr = num * 25;
  int len = EEPROM.read(addr);
  if (len > 25) len = 25;
  addr = addr + 1;
  for (int i = 0; i < len; i++) {
    buf[i] = EEPROM.read(addr + i);
  }
  buf[len] = 0;
}

void loop() {
}

Here's the result when I run it on Teensy 4.1. All 33 strings print as they should.

screenshot.png
 
Maybe the cause of your problems is with this code?

Code:
  byte len = strToWrite.length();
  EEPROM.write(addrOffset, len);

  for (int i = 0; i < len; i++) {
    EEPROM.write(addrOffset  + i, strToWrite[i]);
  }

The length byte is written to addrOffset, but then the first byte of the string overwrites it.

Why that would manifest as the first 29 strings appearing to work and the last 4 failing, I can't say. Maybe there's something about the actual text you're writing that gets misinterpreted as the length when read back?

Or if could be a bug somewhere else in your code. That's why we ask for complete programs to be posted on this forum.

But in this case, I tried anyway. Looks like EEPROM works fine. Hopefully these 2 program can show you the hardware really does work and maybe help you find whatever's wrong in your code.
 
Good morning Paul,

Thank you very much for your time.

In making a smaller version of my program to send to you, I discovered a mistake in setting the array which prevented entries > 29 from being written.

Thank you again for your time and help and my apologies.
 
Status
Not open for further replies.
Back
Top