Teensy 3.2 EEPROM issues

Status
Not open for further replies.

Epyon

Well-known member
Hi all

For a project I'm working on I require to store quite a bit of data on the Teensy's EEPROM. I calculated it to be 1830 bytes in total. However it looks like I'm running out of EEPROM space, while the T3.2 should have 2048 bytes available.

I use the default EEPROM lib and a struct to store my arbitrary variables:
Code:
struct config_t{
  //Configuration parameters
  //IP
  byte IPaddress[4];
  boolean useDHCP;
  //Wireless
  char ssid[32];
  char wifiPass[32];
  //Server
  char remoteAddress[64];
  byte remoteIP[4];
  int remotePort;
  char remoteUser[32];
  char remotePass[32];
  //Slaves
  byte slaveID[numSlaves];
  unsigned int slaveRegs[numSlaves][numRegs];
  char regNames[numSlaves][numRegs][8];
  byte regType[numSlaves][numRegs];
  //Pulse channels
  int pulseWeight[numPulses];
  char pulseNames[numPulses][8];
  //Log variables
  byte pollFreq;
  byte uplFreq;
}configData;
The four last variables aren't retained in EEPROM during power cycles. When I move then up the ladder, before the big arrays, they do keep retained.

Is the T3.2's EEPROM not exactly 2kb, or does the EEPROM lib have too much overhead when storing arbitrary variable types?
 
You might want to reorder the structure so that all byte/char/uint8_t items are first, all short/uint16_t iterms are second, and all uint32_t/etc. items are third. This way you reduce the number of holes that the compiler creates to align types correctly.

If you had multiple booleans, consider using a bitfield to pack all of the booleans into a byte.

Second are you sure you need remotePort, slaveRegs, and pulseWeight to be int? Remember on ARM processors, int is 32-bits. Perhaps using int8_t if the value's range is -128..127, uint8_t if the value's range is 0..255, int16_t if the value's range is -32768..32767, or uint16_t if the value's range is 0..65535.

You might want to put in code like the following into your setup function so that you can see if the structure is actually more than 2,048 bytes:

Code:
void setup (void)
{
  // ...
  Serial.print ("Structure size is ");
  Serial.println (sizeof (struct config_t));
  // ...
}
 
Last edited:
Second are you sure you need remotePort, slaveRegs, and pulseWeight to be int? Remember on ARM processors, int is 32-bits. Perhaps using int8_t if the value's range is -128..127 or int16_t if the value's range is -32768..32767.
Thanks for the excellent suggestion, this was the problem. I assumed int stored a 16bit value, but of course it's double the size. That made my struct way too big. Guess old AVR habits don't die fast :rolleyes: .
 
Might not be an issue in this case, but something to consider is the compiler will sometimes allocate structs with "holes" to align the fields onto 16 or 32 bit memory boundaries. Using sizeof() gives the correct total size.
 
Status
Not open for further replies.
Back
Top