Skip to content

Commit 706bf48

Browse files
pbecchime-no-dev
authored andcommitted
EEPROM_library_ported_Partition_table_updated (espressif#535)
* EEPROM_library_ported_Partition_table_updated * Cleanup & formal corrections * Make formatting of file consistent. Readability change, Concern about the SubType being 0x99. * Code formatting cleanup * Code formatting cleanup * Code formatting cleanup * Remove commented out code * Restore dropped bracket * Update EEPROM.cpp * Format Corrections * deletedExample * Corrected example * Deleted interrupts/nointerrupts * Update EEPROM.cpp
1 parent 0accabc commit 706bf48

File tree

6 files changed

+331
-1
lines changed

6 files changed

+331
-1
lines changed

libraries/EEPROM/EEPROM.cpp

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
EEPROM.cpp -ported by Paolo Becchi to Esp32
3+
Op
4+
from esp8266 EEPROM emulation
5+
6+
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
7+
This file is part of the esp8266 core for Arduino environment.
8+
9+
This library is free software; you can redistribute it and/or
10+
modify it under the terms of the GNU Lesser General Public
11+
License as published by the Free Software Foundation; either
12+
version 2.1 of the License, or (at your option) any later version.
13+
14+
This library is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
Lesser General Public License for more details.
18+
19+
You should have received a copy of the GNU Lesser General Public
20+
License along with this library; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
*/
23+
24+
#include "Arduino.h"
25+
#include "EEPROM.h"
26+
27+
#include <esp_log.h>
28+
29+
static const char* TAG = "eeprom";
30+
31+
EEPROMClass::EEPROMClass(uint32_t sector)
32+
: _sector(sector)
33+
, _data(0)
34+
, _size(0)
35+
, _dirty(false)
36+
{
37+
}
38+
39+
EEPROMClass::EEPROMClass(void)
40+
: _sector(0)// (((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE))
41+
, _data(0)
42+
, _size(0)
43+
, _dirty(false)
44+
{
45+
}
46+
47+
bool EEPROMClass::begin(size_t size) {
48+
if (size <= 0) {
49+
return false;
50+
}
51+
if (size > SPI_FLASH_SEC_SIZE) {
52+
size = SPI_FLASH_SEC_SIZE;
53+
}
54+
_mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,ESP_PARTITION_SUBTYPE_ANY, EEPROM_FLASH_PARTITION_NAME);
55+
if (_mypart == NULL) {
56+
return false;
57+
}
58+
size = (size + 3) & (~3);
59+
60+
if (_data) {
61+
delete[] _data;
62+
}
63+
64+
_data = new uint8_t[size];
65+
_size = size;
66+
bool ret = false;
67+
if (esp_partition_read (_mypart,0, (void *) _data,_size)==ESP_OK) {
68+
ret=true;
69+
}
70+
71+
return ret;
72+
}
73+
74+
void EEPROMClass::end() {
75+
if (!_size) {
76+
return;
77+
}
78+
79+
commit();
80+
if (_data) {
81+
delete[] _data;
82+
}
83+
_data = 0;
84+
_size = 0;
85+
}
86+
87+
uint8_t EEPROMClass::read(int address) {
88+
if (address < 0 || (size_t)address >= _size) {
89+
return 0;
90+
}
91+
if (!_data) {
92+
return 0;
93+
}
94+
95+
return _data[address];
96+
}
97+
98+
void EEPROMClass::write(int address, uint8_t value) {
99+
if (address < 0 || (size_t)address >= _size)
100+
return;
101+
if(!_data)
102+
return;
103+
104+
// Optimise _dirty. Only flagged if data written is different.
105+
uint8_t* pData = &_data[address];
106+
if (*pData != value)
107+
{
108+
*pData = value;
109+
_dirty = true;
110+
}
111+
}
112+
113+
bool EEPROMClass::commit() {
114+
bool ret = false;
115+
if (!_size)
116+
return false;
117+
if (!_dirty)
118+
return true;
119+
if (!_data)
120+
return false;
121+
122+
123+
if (esp_partition_erase_range(_mypart, 0, SPI_FLASH_SEC_SIZE) != ESP_OK)
124+
{
125+
log_e( "partition erase err.");
126+
}
127+
else
128+
{
129+
if (esp_partition_write(_mypart, 0, (void *)_data, _size) == ESP_ERR_INVALID_SIZE)
130+
{
131+
log_e( "error in Write");
132+
}
133+
else
134+
{
135+
_dirty = false;
136+
ret = true;
137+
}
138+
}
139+
140+
return ret;
141+
}
142+
143+
uint8_t * EEPROMClass::getDataPtr() {
144+
_dirty = true;
145+
return &_data[0];
146+
}
147+
148+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
149+
EEPROMClass EEPROM;
150+
#endif

libraries/EEPROM/EEPROM.h

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
EEPROM.h -ported by Paolo Becchi to Esp32
3+
4+
use a one sector flash partition defined in partition table
5+
6+
from esp8266 EEPROM
7+
8+
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
9+
This file is part of the esp8266 core for Arduino environment.
10+
11+
This library is free software; you can redistribute it and/or
12+
modify it under the terms of the GNU Lesser General Public
13+
License as published by the Free Software Foundation; either
14+
version 2.1 of the License, or (at your option) any later version.
15+
16+
This library is distributed in the hope that it will be useful,
17+
but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
Lesser General Public License for more details.
20+
21+
You should have received a copy of the GNU Lesser General Public
22+
License along with this library; if not, write to the Free Software
23+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24+
*/
25+
26+
#ifndef EEPROM_h
27+
#define EEPROM_h
28+
#ifndef EEPROM_FLASH_PARTITION_NAME
29+
#define EEPROM_FLASH_PARTITION_NAME "eeprom"
30+
#endif
31+
extern "C" {
32+
33+
#include <stddef.h>
34+
#include <stdint.h>
35+
#include <string.h>
36+
#include <esp_partition.h>
37+
}
38+
39+
//
40+
// need to define a flash partition for EEPROM with above name
41+
//
42+
// eeprom , data , 0x99, start address, 0x1000
43+
//
44+
class EEPROMClass {
45+
public:
46+
EEPROMClass(uint32_t sector);
47+
EEPROMClass(void);
48+
49+
bool begin(size_t size);
50+
uint8_t read(int address);
51+
void write(int address, uint8_t val);
52+
bool commit();
53+
void end();
54+
55+
uint8_t * getDataPtr();
56+
57+
template<typename T>
58+
T &get(int address, T &t) {
59+
if (address < 0 || address + sizeof(T) > _size)
60+
return t;
61+
62+
memcpy((uint8_t*) &t, _data + address, sizeof(T));
63+
return t;
64+
}
65+
66+
template<typename T>
67+
const T &put(int address, const T &t) {
68+
if (address < 0 || address + sizeof(T) > _size)
69+
return t;
70+
71+
memcpy(_data + address, (const uint8_t*) &t, sizeof(T));
72+
_dirty = true;
73+
return t;
74+
}
75+
76+
protected:
77+
uint32_t _sector;
78+
uint8_t* _data;
79+
size_t _size;
80+
bool _dirty;
81+
const esp_partition_t * _mypart;
82+
};
83+
84+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
85+
extern EEPROMClass EEPROM;
86+
#endif
87+
88+
#endif
89+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
EEPROM Write
3+
4+
Stores random values into the EEPROM.
5+
These values will stay in the EEPROM when the board is
6+
turned off and may be retrieved later by another sketch.
7+
*/
8+
9+
#include "EEPROM.h"
10+
11+
// the current address in the EEPROM (i.e. which byte
12+
// we're going to write to next)
13+
int addr = 0;
14+
#define EEPROM_SIZE 64
15+
void setup()
16+
{
17+
Serial.begin(115200);
18+
Serial.println("start...");
19+
if (!EEPROM.begin(EEPROM_SIZE))
20+
{
21+
Serial.println("failed to initialise EEPROM"); delay(1000000);
22+
}
23+
Serial.println(" bytes read from Flash . Values are:");
24+
for (int i = 0; i < EEPROM_SIZE; i++)
25+
{
26+
Serial.print(byte(EEPROM.read(i))); Serial.print(" ");
27+
}
28+
Serial.println();
29+
Serial.println("writing random n. in memory");
30+
}
31+
32+
void loop()
33+
{
34+
// need to divide by 4 because analog inputs range from
35+
// 0 to 1023 and each byte of the EEPROM can only hold a
36+
// value from 0 to 255.
37+
// int val = analogRead(10) / 4;
38+
int val = byte(random(10020));
39+
// write the value to the appropriate byte of the EEPROM.
40+
// these values will remain there when the board is
41+
// turned off.
42+
EEPROM.write(addr, val);
43+
Serial.print(val); Serial.print(" ");
44+
// advance to the next address. there are 512 bytes in
45+
// the EEPROM, so go back to 0 when we hit 512.
46+
// save all changes to the flash.
47+
addr = addr + 1;
48+
if (addr == EEPROM_SIZE)
49+
{
50+
Serial.println();
51+
addr = 0;
52+
EEPROM.commit();
53+
Serial.print(EEPROM_SIZE);
54+
Serial.println(" bytes written on Flash . Values are:");
55+
for (int i = 0; i < EEPROM_SIZE; i++)
56+
{
57+
Serial.print(byte(EEPROM.read(i))); Serial.print(" ");
58+
}
59+
Serial.println(); Serial.println("----------------------------------");
60+
}
61+
62+
delay(100);
63+
}

libraries/EEPROM/keywords.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#######################################
2+
# Syntax Coloring Map For Ultrasound
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
EEPROM KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
#######################################
16+
# Constants (LITERAL1)
17+
#######################################
18+

libraries/EEPROM/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=EEPROM
2+
version=1.0
3+
author=Ivan Grokhotkov
4+
maintainer=Paolo Becchi <pbecchi@aerobusiness.it>
5+
sentence=Enables reading and writing data to the permanent FLASH storage, up to 4kb.
6+
paragraph=
7+
category=Data Storage
8+
url=http://arduino.cc/en/Reference/EEPROM
9+
architectures=esp32

tools/partitions/default.csv

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x140000,
55
app1, app, ota_1, 0x150000,0x140000,
6-
spiffs, data, spiffs, 0x290000,0x170000,
6+
eeprom, data, 0x99, 0x290000,0x1000,
7+
spiffs, data, spiffs, 0x291000,0x169000

0 commit comments

Comments
 (0)