11/*
2- ESP32 eeprom_class example with EEPROM library
2+ ESP32 eeprom_class example with EEPROM library and custom partiton
3+
34 This simple example demonstrates using EEPROM library to store different data in
4- ESP32 Flash memory in a multiple user-defined EEPROM class objects.
5+ ESP32 Flash memory in a multiple user-defined EEPROM partition (0x1000 or 4KB max size or less).
6+ Note: You must maintain a minimum sector address/offset of 4KB between partitions
7+ Usage: EEPROMClass ANY_OBJECT_NAME("partition_name", size);
8+
9+ Partition example created in the same sketch folder as this example
10+ (See ./eeprom_class/partitions.csv)
511
12+ #Name, Type, SubType, Offset, Size, Flags
13+ nvs, data, nvs, 0x9000, 0x5000,
14+ otadata, data, ota, 0xe000, 0x2000,
15+ app0, app, ota_0, 0x10000, 0x140000,
16+ app1, app, ota_1, 0x150000, 0x140000,
17+ eeprom0, data, 0x99, 0x290000, 0x1000,
18+ eeprom1, data, 0x9a, 0x291000, 0x500,
19+ eeprom2, data, 0x9b, 0x292000, 0x100,
20+ spiffs, data, spiffs, 0x293000, 0x16d000,
21+
622 Created for arduino-esp32 on 25 Dec, 2017
723 by Elochukwu Ifediora (fedy0)
8- converted to nvs by lbernstone - 06/22/2019
924*/
1025
1126#include " EEPROM.h"
1227
13- // Instantiate eeprom objects with parameter/argument names and sizes
14- EEPROMClass NAMES (" eeprom0" );
15- EEPROMClass HEIGHT (" eeprom1" );
16- EEPROMClass AGE (" eeprom2" );
28+ // Instantiate eeprom objects with parameter/argument names and size same as in the partition table
29+ EEPROMClass NAMES (" eeprom0" , 0x1000 );
30+ EEPROMClass HEIGHT (" eeprom1" , 0x500 );
31+ EEPROMClass AGE (" eeprom2" , 0x100 );
1732
1833void setup () {
34+ // put your setup code here, to run once:
1935 Serial.begin (115200 );
20- delay (1000 );
2136 Serial.println (" Testing EEPROMClass\n " );
22- if (!NAMES.begin (0x500 )) {
37+ if (!NAMES.begin (NAMES. length () )) {
2338 Serial.println (" Failed to initialise NAMES" );
2439 Serial.println (" Restarting..." );
2540 delay (1000 );
2641 ESP.restart ();
2742 }
28- if (!HEIGHT.begin (0x200 )) {
43+ if (!HEIGHT.begin (HEIGHT. length () )) {
2944 Serial.println (" Failed to initialise HEIGHT" );
3045 Serial.println (" Restarting..." );
3146 delay (1000 );
3247 ESP.restart ();
3348 }
34- if (!AGE.begin (0x100 )) {
49+ if (!AGE.begin (AGE. length () )) {
3550 Serial.println (" Failed to initialise AGE" );
3651 Serial.println (" Restarting..." );
3752 delay (1000 );
3853 ESP.restart ();
3954 }
4055
41- const char * name = " Teo Swee Ann" ;
42- char rname[32 ];
56+ char * name = " Teo Swee Ann" ;
4357 double height = 5.8 ;
4458 uint32_t age = 47 ;
4559
46- // Write: Variables ---> EEPROM stores
47- NAMES.writeString (0 , name);
60+ // Write: Variables ---> EEPROM partitions
61+ NAMES.put (0 , name);
4862 HEIGHT.put (0 , height);
4963 AGE.put (0 , age);
5064 Serial.print (" name: " ); Serial.println (name);
@@ -53,25 +67,26 @@ void setup() {
5367 Serial.println (" ------------------------------------\n " );
5468
5569 // Clear variables
56- rname[ 0 ] = ' \0 ' ;
70+ name = NULL ;
5771 height = 0 ;
5872 age = 0 ;
59- Serial.print (" name: " ); Serial.println (rname );
73+ Serial.print (" name: " ); Serial.println (name );
6074 Serial.print (" height: " ); Serial.println (height);
6175 Serial.print (" age: " ); Serial.println (age);
6276 Serial.println (" ------------------------------------\n " );
6377
64- // Read: Variables <--- EEPROM stores
65- NAMES.get (0 , rname );
78+ // Read: Variables <--- EEPROM partitions
79+ NAMES.get (0 , name );
6680 HEIGHT.get (0 , height);
6781 AGE.get (0 , age);
68- Serial.print (" name: " ); Serial.println (rname );
82+ Serial.print (" name: " ); Serial.println (name );
6983 Serial.print (" height: " ); Serial.println (height);
7084 Serial.print (" age: " ); Serial.println (age);
7185
7286 Serial.println (" Done!" );
7387}
7488
7589void loop () {
76- delay (0xFFFFFFFF );
90+ // put your main code here, to run repeatedly:
91+
7792}
0 commit comments