Skip to content

Add EEPROM debug printouts, error check to example #6556

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

Merged
merged 2 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions libraries/EEPROM/EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "Arduino.h"
#include "EEPROM.h"
#include "debug.h"

extern "C" {
#include "c_types.h"
Expand Down Expand Up @@ -49,10 +50,14 @@ EEPROMClass::EEPROMClass(void)
}

void EEPROMClass::begin(size_t size) {
if (size <= 0)
if (size <= 0) {
DEBUGV("EEPROMClass::begin error, size == 0\n");
return;
if (size > SPI_FLASH_SEC_SIZE)
}
if (size > SPI_FLASH_SEC_SIZE) {
DEBUGV("EEPROMClass::begin error, %d > %d\n", size, SPI_FLASH_SEC_SIZE);
size = SPI_FLASH_SEC_SIZE;
}

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

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

noInterrupts();
spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
auto ret = spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
interrupts();
if (ret != SPI_FLASH_RESULT_OK) {
DEBUGV("EEPROMClass::begin spi_flash_read failed, %d\n", (int)ret);
}

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


uint8_t EEPROMClass::read(int const address) {
if (address < 0 || (size_t)address >= _size)
if (address < 0 || (size_t)address >= _size) {
DEBUGV("EEPROMClass::read error, address %d > %d or %d < 0\n", address, _size, address);
return 0;
if(!_data)
}
if (!_data) {
DEBUGV("EEPROMClass::read without ::begin\n");
return 0;
}

return _data[address];
}

void EEPROMClass::write(int const address, uint8_t const value) {
if (address < 0 || (size_t)address >= _size)
if (address < 0 || (size_t)address >= _size) {
DEBUGV("EEPROMClass::write error, address %d > %d or %d < 0\n", address, _size, address);
return;
if(!_data)
}
if(!_data) {
DEBUGV("EEPROMClass::read without ::begin\n");
return;
}

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

noInterrupts();
if(spi_flash_erase_sector(_sector) == SPI_FLASH_RESULT_OK) {
if(spi_flash_write(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size) == SPI_FLASH_RESULT_OK) {
auto flashret = spi_flash_erase_sector(_sector);
if (flashret == SPI_FLASH_RESULT_OK) {
flashret = spi_flash_write(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
if (flashret == SPI_FLASH_RESULT_OK) {
_dirty = false;
ret = true;
}
}
interrupts();

if (flashret != SPI_FLASH_RESULT_OK) {
DEBUGV("EEPROMClass::commit failed, %d\n", (int)flashret);
}

return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/EEPROM/examples/eeprom_read/eeprom_read.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ byte value;

void setup() {
// initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Expand Down
7 changes: 6 additions & 1 deletion libraries/EEPROM/examples/eeprom_write/eeprom_write.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
int addr = 0;

void setup() {
Serial.begin(115200);
EEPROM.begin(512);
}

Expand All @@ -33,7 +34,11 @@ void loop() {
addr = addr + 1;
if (addr == 512) {
addr = 0;
EEPROM.commit();
if (EEPROM.commit()) {
Serial.println("EEPROM successfully committed");
} else {
Serial.println("ERROR! EEPROM commit failed");
}
}

delay(100);
Expand Down