Working with FRAM

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, I am wanting to write a counter that writes to the FRAM chip Ic2,  so if I lose power I will be able to recover the last count.  I found splitting the value into 2 pieces works great but I am still limited, and I am wondering what do I do with strings of text.  Is there any elegant way to read and write to the FRAM chip other than what I am doing already.

 

  void writeMem(int address, long value) {
    unsigned int MSB = value / 256L;      // Break the value into 2 Byte-parts for storing
    unsigned int LSB = value % 256L;      // Above is MSB, remainder (Modulo) is LSB
    fram.write8(address, MSB);              // Store the value MSB at address add1
    fram.write8(address + 1, LSB);          // Store the value LSB at address add1 + 1
  }

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Isin't fram.write16 or fram.write32 implemented? That should do the trick! You also have to consider what happens if you lose power while writing the memory. Ideally updating the counter should be one atomic action.

Last Edited: Sun. Apr 11, 2021 - 10:20 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sukuwc wrote:

Isin't fram.write16 or fram.write32 implemented? That should do the trick! You also have to consider what happens if you lose power while writing the memory. Ideally updating the counter should be one atomic action.

Thank you, it’s all very confusing, with the esp32 writing to its memory is considerably easier. Having to breakup values larger than 256 into different segment locations.

If I have text like a paragraph, I have no clue what to do.

Are there any technical materials that will help me figure it out?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

uint32_t base_address = 0x1234;

 

char* text = "Hello this is very long text\n";

uint32_t text_length = strlen(text);

 

for (uint32_t i=0; i<text_length; i++){

 

     fram.write8(base_address + i, text[i]);

 

}