Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EEPROM emulation #205

Open
sstaub opened this issue Apr 22, 2021 · 7 comments
Open

EEPROM emulation #205

sstaub opened this issue Apr 22, 2021 · 7 comments

Comments

@sstaub
Copy link

sstaub commented Apr 22, 2021

I'm missing an EEPROM emulation like on STM32duino

@facchinm
Copy link
Member

Hi @sstaub ,
using mbed's blockdevice infrastructure you can create very easily something much more powerful and reliable (or even a library that wraps EEPROM APIs).
For example, to store some data in a Pico, you can write something like this

#include "FlashIAPBlockDevice.h"
#include "KVStore.h"
#include "TDBStore.h"

// 512KB block device, starting 1MB inside the flash
FlashIAPBlockDevice bd(XIP_BASE + 1024*1024, 1024*512);
mbed::TDBStore eeprom(&bd);

uint8_t ram_buffer[50];
char* data = "quitealongstring";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial); 
  eeprom.init();
  mbed::KVStore::info_t info;
  if (eeprom.get_info("mydata", &info) != MBED_ERROR_ITEM_NOT_FOUND) {
    Serial.println("Key size: " + String(info.size));
    eeprom.get("mydata", ram_buffer, info.size);
    Serial.print("Data: ");
    Serial.println((char*)ram_buffer);
  } else {
    Serial.println("Setting data");
    eeprom.reset();
    eeprom.set("mydata", data, strlen(data), 0);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

Or even use filesystems; you can find more examples in this thread arduino/ArduinoCore-nRF528x-mbedos#16

@sstaub
Copy link
Author

sstaub commented Apr 26, 2021

Thank you for the advices.

@rmlearney-digicatapult
Copy link
Contributor

rmlearney-digicatapult commented Apr 27, 2021

Problem I've found with KVSTORE is that it doesn't support integers or any numerical values, only strings (more specifically char*). This is a real problem when wanting to store any data in a non-volatile manner.

Are there any lightweight solutions to this?

@technoblogy
Copy link

@rmlearney-digicatapult I'm not an expert on this, but as no one has answered I'll try and help.

You can use the Block Device functions with different types of variable, not just char*. For example if you've got:

uint32_t imagesize;
char SymbolTable[SYMBOLTABLESIZE];
uint32_t Workspace[WORKSPACESIZE];

you can save them all with the sequence:

eeprom.set("size", &imagesize, 4, 0);
eeprom.set("symb", &SymbolTable, SYMBOLTABLESIZE, 0);
eeprom.set("work", &Workspace, WORKSPACESIZE*4, 0);

and load them back in with:

eeprom.get("size", &imagesize, 4);
eeprom.get("symb", &SymbolTable, SYMBOLTABLESIZE);
eeprom.get("work", &Workspace, WORKSPACESIZE*4);

The sizes all need to be in bytes. I'm not sure how long the keys can be; I've chosen four characters.

@lyomov
Copy link

lyomov commented Feb 18, 2024

Hi @facchinm,

I was studying and try to implement a code you suggested at [Implementation of files read/write on flash memory.] discussion.
I like it and find it a very helpful - thanks and congratulations.

Employing your example code, given there, in application I try to create, I got some error codes, like -22, 33.

My question is: Where I can find the meaning of the code errors?

Sorry I am asking here, since the mentioned discussion is closed!

Thank you!

@manchoz
Copy link
Contributor

manchoz commented Feb 19, 2024

Hey @lyomov,
Look at Creating a Flash-Optimized Key-Value Store.
In this example, there are a few expedients that might help you solve your issues.

@lyomov
Copy link

lyomov commented Feb 21, 2024

@facchinm,

Thanks a lot!!! I'll have a look.

Best wishes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants