1
1
/*
2
2
EEPROM.h -ported by Paolo Becchi to Esp32 from esp8266 EEPROM
3
3
-Modified by Elochukwu Ifediora <[email protected] >
4
+ -Converted to nvs [email protected]
4
5
5
- Uses a one sector flash partition defined in partition table
6
- OR
7
- Multiple sector flash partitions defined by the name column in the partition table
6
+ Uses a nvs byte array to emulate EEPROM
8
7
9
8
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
10
9
This file is part of the esp8266 core for Arduino environment.
28
27
29
28
#include < esp_log.h>
30
29
31
- EEPROMClass::EEPROMClass (uint32_t sector)
32
- : _sector(sector)
33
- , _data(0 )
30
+ EEPROMClass::EEPROMClass (void )
31
+ : _data(0 )
34
32
, _size(0 )
35
33
, _dirty(false )
36
- , _mypart (NULL )
34
+ , _handle (NULL )
37
35
, _name(" eeprom" )
38
36
, _user_defined_size(0 )
39
37
{
40
38
}
41
39
42
- EEPROMClass::EEPROMClass (const char * name, uint32_t user_defined_size )
43
- : _sector( 0 )
44
- , _data(0 )
40
+ EEPROMClass::EEPROMClass (uint32_t sector )
41
+ // Only for compatiility, no sectors in nvs!
42
+ : _data(0 )
45
43
, _size(0 )
46
44
, _dirty(false )
47
- , _mypart (NULL )
48
- , _name(name )
49
- , _user_defined_size(user_defined_size )
45
+ , _handle (NULL )
46
+ , _name(" eeprom " )
47
+ , _user_defined_size(0 )
50
48
{
51
49
}
52
50
53
- EEPROMClass::EEPROMClass (void )
54
- : _sector(0 )// (((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE))
55
- , _data(0 )
51
+ EEPROMClass::EEPROMClass (const char * name, uint32_t user_defined_size)
52
+ : _data(0 )
56
53
, _size(0 )
57
54
, _dirty(false )
58
- , _mypart (NULL )
59
- , _name(" eeprom " )
60
- , _user_defined_size(0 )
55
+ , _handle (NULL )
56
+ , _name(name )
57
+ , _user_defined_size(user_defined_size )
61
58
{
62
59
}
63
60
@@ -66,31 +63,59 @@ EEPROMClass::~EEPROMClass() {
66
63
}
67
64
68
65
bool EEPROMClass::begin (size_t size) {
69
- if (size <= 0 ) {
70
- return false ;
66
+ if (size <= 0 ) return false ;
67
+
68
+ esp_err_t res = nvs_open (_name, NVS_READWRITE, &_handle);
69
+ if (res != ESP_OK) {
70
+ log_e (" Unable to open NVS namespace: %d" , res);
71
+ return false ;
72
+ }
73
+
74
+ size_t key_size = 0 ;
75
+ res = nvs_get_blob (_handle, _name, NULL , &key_size);
76
+ if (res != ESP_OK && res != ESP_ERR_NVS_NOT_FOUND) {
77
+ log_e (" Unable to read NVS key: %d" , res);
78
+ return false ;
71
79
}
72
- if (size > SPI_FLASH_SEC_SIZE) {
73
- size = SPI_FLASH_SEC_SIZE;
80
+ if (size < key_size) { // truncate
81
+ log_w (" truncating EEPROM from %d to %d" , key_size, size);
82
+ uint8_t * trunc_key = (uint8_t *) malloc (size);
83
+ nvs_get_blob (_handle, _name, trunc_key, &size);
84
+ nvs_set_blob (_handle, _name, trunc_key, size);
85
+ nvs_commit (_handle);
86
+ free (trunc_key);
74
87
}
75
- // _mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,ESP_PARTITION_SUBTYPE_ANY, EEPROM_FLASH_PARTITION_NAME);
76
- _mypart = esp_partition_find_first (ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, _name);
77
- if (_mypart == NULL ) {
78
- return false ;
88
+ if (size > key_size) { // expand or new
89
+ size_t expand_size = size - key_size;
90
+ uint8_t * expand_key = (uint8_t *) malloc (expand_size);
91
+ if (nvs_set_blob (_handle, " expand" , expand_key, expand_size)) {
92
+ log_e (" Not enough space to expand EEPROM from %d to %d" , key_size, size);
93
+ free (expand_key);
94
+ return false ;
95
+ }
96
+ free (expand_key);
97
+ nvs_erase_key (_handle, " expand" );
98
+ uint8_t * hold_data = (uint8_t *) malloc (size);
99
+ memset (hold_data, 0 , size);
100
+ if (key_size) {
101
+ log_i (" Expanding EEPROM from %d to %d" , key_size, size);
102
+ nvs_get_blob (_handle, _name, hold_data, &key_size);
103
+ nvs_erase_key (_handle, _name);
104
+ }
105
+ nvs_commit (_handle);
106
+ nvs_set_blob (_handle, _name, hold_data, size);
107
+ free (hold_data);
108
+ nvs_commit (_handle);
79
109
}
80
- size = (size + 3 ) & (~3 );
81
110
82
111
if (_data) {
83
112
delete[] _data;
84
113
}
85
114
86
115
_data = new uint8_t [size];
87
116
_size = size;
88
- bool ret = false ;
89
- if (esp_partition_read (_mypart, 0 , (void *) _data, _size) == ESP_OK) {
90
- ret = true ;
91
- }
92
-
93
- return ret;
117
+ nvs_get_blob (_handle, _name, _data, &_size);
118
+ return true ;
94
119
}
95
120
96
121
void EEPROMClass::end () {
@@ -134,29 +159,15 @@ void EEPROMClass::write(int address, uint8_t value) {
134
159
135
160
bool EEPROMClass::commit () {
136
161
bool ret = false ;
137
- if (!_size)
138
- return false ;
139
- if (!_dirty)
140
- return true ;
141
- if (!_data)
142
- return false ;
143
-
162
+ if (!_size) return false ;
163
+ if (!_data) return false ;
164
+ if (!_dirty) return true ;
144
165
145
- if (esp_partition_erase_range (_mypart, 0 , SPI_FLASH_SEC_SIZE) != ESP_OK)
146
- {
147
- log_e ( " partition erase err." );
148
- }
149
- else
150
- {
151
- if (esp_partition_write (_mypart, 0 , (void *)_data, _size) == ESP_ERR_INVALID_SIZE)
152
- {
153
- log_e ( " error in Write" );
154
- }
155
- else
156
- {
166
+ if (ESP_OK != nvs_set_blob (_handle, _name, _data, _size)) {
167
+ log_e ( " error in write" );
168
+ } else {
157
169
_dirty = false ;
158
170
ret = true ;
159
- }
160
171
}
161
172
162
173
return ret;
0 commit comments