0

I am writing a wrapper around the original EEPROM library for Arduino. I want to develop my own library for EEPROM wear leveling to write data in chunks and my program would automatically append a chunk if needed to increase the lifetime of EEPROM.

I am mostly concerned about the EEPROM.put() function which takes 2 arguments. 1 - starting address 2 - data

I have built a few methods for my eeprom_rotate class:

//CLASS CONSTRUCTOR

eeprom_rotate::eeprom_rotate(size_t size_of_eeprom, size_t chunk_size) {
    EEPROM.begin(size_of_eeprom);
    Serial.println("the class constructor has been created");
    this->size_of_eeprom = size_of_eeprom;
    this->chunk_size = chunk_size;
}





void eeprom_rotate::write_to_chunk(size_t chunk, int data[]){
    EEPROM.put(chunk,data);
    EEPROM.commit();
}

And I am calling the function as following:


  eeprom_rotate eeprom1(50,10);//creates a object constructor eeprom size 50 and chunk size is 10
  int  data_buffer[10] = {1,2,3,4,5,6,7,8,9,10};
  eeprom1.write_to_chunk(0,data_buffer);

So I am writing to chunk 0 (addresses from 0 to 10) and expect my EEPROM to have values 1,2,3,4,5,6,7,8,9,10, however, that is not the case, when I check on serial monitor the values are as following:

reading all eeprom
Data in EEPROM(0)= 104
Data in EEPROM(1)= 31
Data in EEPROM(2)= 251
Data in EEPROM(3)= 63
Data in EEPROM(4)= 255
Data in EEPROM(5)= 255
Data in EEPROM(6)= 255
Data in EEPROM(7)= 255
Data in EEPROM(8)= 255
Data in EEPROM(9)= 255
Data in EEPROM(10)= 255
Data in EEPROM(11)= 255
Data in EEPROM(12)= 255
Data in EEPROM(13)= 255
Data in EEPROM(14)= 255
Data in EEPROM(15)= 255
Data in EEPROM(16)= 255
Data in EEPROM(17)= 255
Data in EEPROM(18)= 255
Data in EEPROM(19)= 255
Data in EEPROM(20)= 255
Data in EEPROM(21)= 255
Data in EEPROM(22)= 255
Data in EEPROM(23)= 255
Data in EEPROM(24)= 255
Data in EEPROM(25)= 255
Data in EEPROM(26)= 255
Data in EEPROM(27)= 255
Data in EEPROM(28)= 255
Data in EEPROM(29)= 255
Data in EEPROM(30)= 255
Data in EEPROM(31)= 255
Data in EEPROM(32)= 255
Data in EEPROM(33)= 255
Data in EEPROM(34)= 255
Data in EEPROM(35)= 255
Data in EEPROM(36)= 255
Data in EEPROM(37)= 255
Data in EEPROM(38)= 255
Data in EEPROM(39)= 255
Data in EEPROM(40)= 255
Data in EEPROM(41)= 255
Data in EEPROM(42)= 255
Data in EEPROM(43)= 255
Data in EEPROM(44)= 255
Data in EEPROM(45)= 255
Data in EEPROM(46)= 255
Data in EEPROM(47)= 255
Data in EEPROM(48)= 255
Data in EEPROM(49)= 255

UPDATE

I have done some further testing and found out something interesting. In my arduino main program if I check the size of the array:

  uint8_t data_buffer[10] = {1,2,3,4,5,6,7,8,9,10};
  Serial.print("size of data buffer =");
  Serial.println(sizeof(data_buffer));
  eeprom1.write_to_chunk(0,data_buffer);

The result will be:

clearing eeprom
size of data buffer =10

However, when I try to print it in my class method as following:

void eeprom_rotate::write_to_chunk(size_t chunk, uint8_t data_to_write[]){
  Serial.print(ln"printing data to write received = ");
  Serial.println(data_to_write[0]);
  Serial.println(data_to_write[1]);
  Serial.println(data_to_write[2]);
  Serial.println(data_to_write[3]);
  Serial.println(data_to_write[4]);
  Serial.println(data_to_write[5]);
  Serial.println(data_to_write[6]);
  Serial.println(data_to_write[7]);
  Serial.println(data_to_write[8]);
  Serial.println(data_to_write[9]);
  Serial.print("size of data to write= ");
  Serial.println(sizeof(data_to_write));

    EEPROM.put(chunk,data_to_write);
    Serial.println("printing after put");
    for(int i = 0; i<size_of_eeprom;i++){
      Serial.println(EEPROM.read(i));
    }
}


The result is as following:

printing data to write received = 1
2
3
4
5
6
7
8
9
10
size of data to write= 4
printing after put
130
31
251
63
255
255
255
255
255
255
255
255

The sizeof my array inside the function for some reason is 4 even though I pass my array size of 10. This explains why it only write to first 4 addresses of EEPROM. However, it is still not fully clear to me what is wrong here

  • "The sizeof my array inside the function for some reason is 4 even though I pass my array size of 10" See https://stackoverflow.com/questions/1975128/why-isnt-the-size-of-an-array-parameter-the-same-as-within-main. This is very fundamental stuff, explained in any decent C programming book. – Lundin Jun 18 '21 at 12:01

0 Answers0