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

Commit does return an error code #12

Closed
LeoVe opened this issue May 30, 2021 · 18 comments
Closed

Commit does return an error code #12

LeoVe opened this issue May 30, 2021 · 18 comments
Labels
enhancement New feature or request

Comments

@LeoVe
Copy link

LeoVe commented May 30, 2021

Hi,

Thanks for the great work to this library !

I make use of the library in an Arduino (v1.8.13) script for the wemos mini D1 Pro.
Whatever I try, I keep on getting a returncode 'false' from the EEPROM.commit() and the values are not written to EEPROM. I'm using ESP_EEPROM version 2.1.1 with standard Arduino library management.

The put and get functions seem to work flawless as the get does return the expected values after the put. The problem is its not written to EEPROM with EEPROM.commit().

This is the code I use:

String RfDeviceID[] = {"xx", "74"};
const int RfDeviceIdCount = 2;
int eeAddress;
...
void setup() {
...
EEPROM.begin(RfDeviceIdCount * 10); //Initialize EEPROM
String initCode;
char arrayFromStore[10];
eeAddress=0;
EEPROM.get(eeAddress, arrayFromStore);
initCode = arrayFromStore;
Serial.println();
Serial.print("\nInitCode from EEPROM: ");
Serial.println(arrayFromStore);
if (initCode != RfDeviceID[0]) {
writeInitEEPROM();
}
for (int i=0; i<RfDeviceIdCount; i++ ) {
eeAddress=i10;
EEPROM.get(eeAddress, arrayFromStore);
RfDeviceID[i] = arrayFromStore;
Serial.print("init ");
Serial.print(i);
Serial.print(" read from EEPROM: ");
Serial.println(arrayFromStore);
}
}
...
void writeInitEEPROM() {
char arrayToStore[10];
for (int i=0; i<RfDeviceIdCount; i++ )
{
RfDeviceID[i].toCharArray(arrayToStore, RfDeviceID[i].length()+1);
Serial.print("write init ");
Serial.print(i);
Serial.print(" to EEPROM: ");
Serial.println(arrayToStore);
eeAddress=i
10;
EEPROM.put(eeAddress, arrayToStore);
}
boolean ok=EEPROM.commit(); //Store data to EEPROM
Serial.print((ok) ? "EEProm commit OK: " : "*** EEProm commit failed !!! ");
Serial.println(ok);
}

Example of the output:
13:03:57.000 -> InitCode from EEPROM: ⸮�
13:03:57.000 -> write init 0 to EEPROM: xx
13:03:57.000 -> write init 1 to EEPROM: 74
13:03:57.000 -> *** EEProm commit failed !!! 0
13:03:57.000 -> init 0 read from EEPROM: xx
13:03:57.000 -> init 1 read from EEPROM: 74

@n4mwd
Copy link

n4mwd commented Aug 26, 2021

I have the same problem here and also with a D1 Mini. I modified the ESP_EEPROM source commit() function to return a more useful error message and in my case, it is failing when it tries to erase the sector of flash that it uses for the first time. So, while I don't have an exact answer, it looks to me that a low level ESP erase flash sector function is failing.

@jwrw
Copy link
Owner

jwrw commented Oct 18, 2023

Hi @LeoVe - this issue is probably long past its sell by date - however I tried your code and got

InitCode from EEPROM: 
write init 0 to EEPROM: xx
write init 1 to EEPROM: 74
EEProm commit OK: 1
init 0 read from EEPROM: xx
init 1 read from EEPROM: 74

That being said , I also tried checking sizeof(RfDeviceID[0] and it gave me 12 (not the value 10 you have in your code). I would be careful about using Arduino 'String' in your stored value - it might be safer to use the underlying C string (e.g. my_string.c_str()) - or at least not make assumptions about its implementation by hard-coding the value 10.

@jwrw
Copy link
Owner

jwrw commented Oct 18, 2023

Hi @n4mwd - you make a valid point. If I do any further work on this I will probably add a 'lastError' property that you can query in case of a failure. Flagged as 'enhancement'

@jwrw jwrw added the enhancement New feature or request label Oct 18, 2023
@LeoVe
Copy link
Author

LeoVe commented Oct 19, 2023

Hi jwrw,

Thanks for your test and reply.
How can it be the coding is working with you?
I tried several wemos units and they all fail the EEProm commit?
What arduino type were you using and what library versions? Maybe that is where the solution is.

For your information:
I did not solve this with the wemos mini pro, I started using another arduino type, the NodeMCU v3 and a different EEProm library, SPIFFS.

Kind regards,

Leo

@jwrw
Copy link
Owner

jwrw commented Oct 20, 2023

Hi @LeoVe - I did manage to find an old Wemos mini pro and it worked fine until I updated my ESP8266 core to 3.1.2 then it broke as you described. It's now fixed in the latest release 2.2.0 as long as you're using ESP8266 core 3.1+.

SPIFF is fine and has some useful features if you need them but is a lot more heavy duty than the simple EEPROM scheme.
I'll leave this ticket open for @n4mwd but I'm not sure when/if something will get done. The sdk calls themselves don't have much more info than OK/fail

@LeoVe
Copy link
Author

LeoVe commented Oct 25, 2023

Hi jwrw,

Thanks for your actions, you're great ! 👍
I'll try it for sure.

@LeoVe
Copy link
Author

LeoVe commented Oct 25, 2023

I'm wondering though...
I stopped using ESP8266 core 3.+ because there are a number of libs which don't work as required with it. So I'm still on 2.7.4.
I hope the requirement of the EEPROM lib to have ESP8266 core 3.1+ doesn't run a lot of people into trouble.

@Ilkhomjon2002
Copy link

I am also facing with same issue. EEPROM is not saving my data and when I power off and on and read written data it is coming as false or 0. Also, I checked that EEPROM.commit() is returning 0. I am using latest library of eeprom and esp8266 core

@jwrw
Copy link
Owner

jwrw commented Feb 7, 2024

@Ilkhomjon2002 - any details of what board? Are you able to read the data back OK before you switch off? Do the examples work?

@LeoVe - should work (or not!) as before with earlier cores. New location finding only kicks in with newer core libs.

@Ilkhomjon2002
Copy link

@Ilkhomjon2002 - any details of what board? Are you able to read the data back OK before you switch off? Do the examples work?

@LeoVe - should work (or not!) as before with earlier cores. New location finding only kicks in with newer core libs.

Board is wemos d1 mini pro and yes I can read it before turning it off. I am using latest core and latest esp_eeprom library

@Ilkhomjon2002
Copy link

@Ilkhomjon2002 - any details of what board? Are you able to read the data back OK before you switch off? Do the examples work?
@LeoVe - should work (or not!) as before with earlier cores. New location finding only kicks in with newer core libs.

Board is wemos d1 mini pro and yes I can read it before turning it off. I am using latest core and latest esp_eeprom library

Examples sketches also didn`t work

@RikDrabs
Copy link

The same problem here.
I did an in-depth search and found that the esp_eeprom library can't find the start location in flash memory.

The symbols "EEPROM_start", and even "_FS_end" are both undefined when entering the library !
Of course, when the location in flash is not known, the library can't commit.
One or both missing defines should be defined somewhere deeper, in one of the files c_types.h, ets_sys.h, os_type.h, osapi.h, or spi_flash.h, but I couldn't find these files, even while the compiler finds them during compilation.
So it is probably a version problem somewhere deeper, and the problem probably doesn't appear with every user, depending on how up-to-date his system is ...

I tried other EEPROM libraries, with exactly the same problems on my PC & Arduino IDE: not being able to commit.
I use Arduino IDE 2.3.2 and the latest version of the library (master branch on GitHub).

@jwrw
Copy link
Owner

jwrw commented Apr 10, 2024

Trying to find a universal way to find the start of the flash area is clearly an issue with some combinations of core libraries. I may need to stand up a recent Arduino IDE to investigate further.

It might not be a legitimate 'fix' but for quite some time I've been using VSCode with the PlatformIO extension. This means I can use VSCode for pretty much all my projects, on whatever platform and in whatever language. Advantages include a much more robust way to define libraries/version in use and differing builds for different boards for the same code. There's been a lot of work done on the Arduino IDE to add features but I'd certainly recommend you give VSCode + PlatformIO a go.

@RikDrabs
Copy link

RikDrabs commented Apr 10, 2024

CHATGTP just answered this (and this one works !!!)

// Function to retrieve flash memory size
size_t getFlashChipSize() {
return ESP.getFlashChipSize();
}

// Function to calculate EEPROM start address based on flash size
size_t calculateEEPROMStartAddress() {
// Get flash chip size
size_t flashSize = getFlashChipSize();

// Calculate EEPROM start address
// For ESP8266, EEPROM emulation usually starts at the end of the flash memory
// Typically, last 4KB of flash memory is reserved for EEPROM emulation
return flashSize - 4096; // Assuming 4KB EEPROM size
}

void setup() {
Serial.begin(115200);

// Retrieve and print flash memory size
size_t flashSize = getFlashChipSize();
Serial.print("Flash memory size: ");
Serial.println(flashSize);

// Calculate and print EEPROM start address
size_t eepromStartAddress = calculateEEPROMStartAddress();
Serial.print("EEPROM start address: ");
Serial.println(eepromStartAddress);
}

void loop() {
// Your code here
}

Some ideas ?
I will solve my problem myself.

@RikDrabs
Copy link

RikDrabs commented Apr 10, 2024

Making the library work for a NodeMCU V1.0 ESP8266 is simple :
A patch in ESP_EEPROM.cpp will do the trick.
The file can be found in the Arduino\libraries\ESP_EEPROM\src map.
Replace :
.........................................................................................................................................................................
#ifndef EEPROM_start
#define EEPROM_start _FS_end
#endif
_sector(((EEPROM_start - 0x40200000) / SPI_FLASH_SEC_SIZE)), _data(
.........................................................................................................................................................................
with :
.........................................................................................................................................................................
_sector(((0x3FF000) / SPI_FLASH_SEC_SIZE)), _data(
.........................................................................................................................................................................
to correctly initialize the location in Flash.
For other models of ESP8266 other hex addresses may apply.

@nextguyover
Copy link

Making the library work for a NodeMCU V1.0 ESP8266 is simple : A patch in ESP_EEPROM.cpp will do the trick. The file can be found in the Arduino\libraries\ESP_EEPROM\src map. Replace : ......................................................................................................................................................................... #ifndef EEPROM_start #define EEPROM_start _FS_end #endif _sector(((EEPROM_start - 0x40200000) / SPI_FLASH_SEC_SIZE)), _data( ......................................................................................................................................................................... with : ......................................................................................................................................................................... _sector(((0x3FF000) / SPI_FLASH_SEC_SIZE)), _data( ......................................................................................................................................................................... to correctly initialize the location in Flash. For other models of ESP8266 other hex addresses may apply.

Can confirm that this fix works perfectly for the Wemos D1 mini.

I'm using ESP8266 core 3.0.2. The original ESP8266 EEPROM library worked for me, however EEPROM.commit() using this library kept failing, until I did this fix.

@jwrw
Copy link
Owner

jwrw commented Aug 1, 2024

Hardcoding the address for a specific board is not a fix I can use here. Bottom line is that EEPROM_start is the published method for getting the start in a hardware-independent way and so should be expected to work for all supported boards. If it's not defined then there's a problem in the build or in the board definition somewhere.

With latest IDE (2.3.2) and latest esp8266 package (3.1.2) and a Wemos D1 Mini Pro, the commit() is working as expected with all but one of the flash configurations (FS:14M, FS:15M and the 'Mapping defined by Hardware and Sketch'). For the FS:none I am getting a linker error - this is due to a missing file in the esp8266 distribution (...packages/esp8266/hardware/esp8266/3.1.2/tools/sdk/ld/eagle.flash.16m.ld). This is missing on github and I'm not clear why.

Here's a minimal test for commit()...

`// A minimal test of ESP_EEPROM commit()

#include <ESP_EEPROM.h>
// Hardware Abstraction Layer (HAL) where EEPROM variables are declared
// Actual board values are inserted at link time
// Only included here to allow us to check EEPROM_start exists
#include "flash_hal.h"

void setup() {
// Set your serial monitor to 74880 baud for boot diagnostics
Serial.begin(74880);
Serial.println();

#ifdef _FS_end
Serial.print("Legacy _FS_end is defined 0x");
Serial.println(_FS_end,16);
#endif

#ifdef EEPROM_start
Serial.print("EEPROM_start is defined 0x");
Serial.println(EEPROM_start,16);
if (EEPROM_start == 0) Serial.println("EEPROM_start should not be 0: check board definiton used in build");
#else
Serial.println("EEPROM_start is not defined - check build and board definiton");
#endif
// A minimal sized EEPROM area
EEPROM.begin(16);

// set the EEPROM data ready for writing
EEPROM.put(0, 99);
// ensure buffer registers a change
EEPROM.put(0, 100);

// write the data to EEPROM
boolean ok = EEPROM.commit();
Serial.println((ok) ? "Commit OK" : "Commit failed");
}

void loop() {
// do nothing
}
`

@jwrw
Copy link
Owner

jwrw commented Aug 13, 2024

Closing this issue with note that an enhancement re. last error should be developed

@jwrw jwrw closed this as completed Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants