Skip to content

Commit 2b9fcdb

Browse files
earlephilhowerdevyte
authored andcommitted
Add EEPROM debug printouts, error check to example (#6556)
* Add EEPROM debug printouts, error check to example Add debug printouts when EEPROM calls fail, even if the API doesn't allow returning a success/failure code. Adds error checking to the example to make it explicit that when you call EEPROM::commit(), you need to look at the result code in your code. Fixes #6551 , or would fix it if there was error checking in the MCVE. * Clarify example error message
1 parent 244dbd7 commit 2b9fcdb

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

libraries/EEPROM/EEPROM.cpp

+31-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "Arduino.h"
2323
#include "EEPROM.h"
24+
#include "debug.h"
2425

2526
extern "C" {
2627
#include "c_types.h"
@@ -49,10 +50,14 @@ EEPROMClass::EEPROMClass(void)
4950
}
5051

5152
void EEPROMClass::begin(size_t size) {
52-
if (size <= 0)
53+
if (size <= 0) {
54+
DEBUGV("EEPROMClass::begin error, size == 0\n");
5355
return;
54-
if (size > SPI_FLASH_SEC_SIZE)
56+
}
57+
if (size > SPI_FLASH_SEC_SIZE) {
58+
DEBUGV("EEPROMClass::begin error, %d > %d\n", size, SPI_FLASH_SEC_SIZE);
5559
size = SPI_FLASH_SEC_SIZE;
60+
}
5661

5762
size = (size + 3) & (~3);
5863

@@ -67,8 +72,11 @@ void EEPROMClass::begin(size_t size) {
6772
_size = size;
6873

6974
noInterrupts();
70-
spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
75+
auto ret = spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
7176
interrupts();
77+
if (ret != SPI_FLASH_RESULT_OK) {
78+
DEBUGV("EEPROMClass::begin spi_flash_read failed, %d\n", (int)ret);
79+
}
7280

7381
_dirty = false; //make sure dirty is cleared in case begin() is called 2nd+ time
7482
}
@@ -88,19 +96,27 @@ void EEPROMClass::end() {
8896

8997

9098
uint8_t EEPROMClass::read(int const address) {
91-
if (address < 0 || (size_t)address >= _size)
99+
if (address < 0 || (size_t)address >= _size) {
100+
DEBUGV("EEPROMClass::read error, address %d > %d or %d < 0\n", address, _size, address);
92101
return 0;
93-
if(!_data)
102+
}
103+
if (!_data) {
104+
DEBUGV("EEPROMClass::read without ::begin\n");
94105
return 0;
106+
}
95107

96108
return _data[address];
97109
}
98110

99111
void EEPROMClass::write(int const address, uint8_t const value) {
100-
if (address < 0 || (size_t)address >= _size)
112+
if (address < 0 || (size_t)address >= _size) {
113+
DEBUGV("EEPROMClass::write error, address %d > %d or %d < 0\n", address, _size, address);
101114
return;
102-
if(!_data)
115+
}
116+
if(!_data) {
117+
DEBUGV("EEPROMClass::read without ::begin\n");
103118
return;
119+
}
104120

105121
// Optimise _dirty. Only flagged if data written is different.
106122
uint8_t* pData = &_data[address];
@@ -121,14 +137,20 @@ bool EEPROMClass::commit() {
121137
return false;
122138

123139
noInterrupts();
124-
if(spi_flash_erase_sector(_sector) == SPI_FLASH_RESULT_OK) {
125-
if(spi_flash_write(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size) == SPI_FLASH_RESULT_OK) {
140+
auto flashret = spi_flash_erase_sector(_sector);
141+
if (flashret == SPI_FLASH_RESULT_OK) {
142+
flashret = spi_flash_write(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
143+
if (flashret == SPI_FLASH_RESULT_OK) {
126144
_dirty = false;
127145
ret = true;
128146
}
129147
}
130148
interrupts();
131149

150+
if (flashret != SPI_FLASH_RESULT_OK) {
151+
DEBUGV("EEPROMClass::commit failed, %d\n", (int)flashret);
152+
}
153+
132154
return ret;
133155
}
134156

libraries/EEPROM/examples/eeprom_read/eeprom_read.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ byte value;
1414

1515
void setup() {
1616
// initialize serial and wait for port to open:
17-
Serial.begin(9600);
17+
Serial.begin(115200);
1818
while (!Serial) {
1919
; // wait for serial port to connect. Needed for Leonardo only
2020
}

libraries/EEPROM/examples/eeprom_write/eeprom_write.ino

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
int addr = 0;
1414

1515
void setup() {
16+
Serial.begin(115200);
1617
EEPROM.begin(512);
1718
}
1819

@@ -33,7 +34,11 @@ void loop() {
3334
addr = addr + 1;
3435
if (addr == 512) {
3536
addr = 0;
36-
EEPROM.commit();
37+
if (EEPROM.commit()) {
38+
Serial.println("EEPROM successfully committed");
39+
} else {
40+
Serial.println("ERROR! EEPROM commit failed");
41+
}
3742
}
3843

3944
delay(100);

0 commit comments

Comments
 (0)