From af8f198d7b00c33339c3b0a738fe080fe7c81a80 Mon Sep 17 00:00:00 2001 From: fedy0 Date: Sat, 30 Dec 2017 03:10:39 +0100 Subject: [PATCH 1/9] enchanced EEPROMClass --- libraries/EEPROM/EEPROM.cpp | 333 ++++++++++++++++++++++++++++++++++-- libraries/EEPROM/EEPROM.h | 65 +++++-- 2 files changed, 369 insertions(+), 29 deletions(-) diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp index dcad0f06db2..fbab3bae4bc 100644 --- a/libraries/EEPROM/EEPROM.cpp +++ b/libraries/EEPROM/EEPROM.cpp @@ -1,7 +1,8 @@ /* - EEPROM.cpp -ported by Paolo Becchi to Esp32 + EEPROM.cpp -ported by Paolo Becchi to Esp32 Op from esp8266 EEPROM emulation + -Modified by Ifediora Elochukwu C. Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. This file is part of the esp8266 core for Arduino environment. @@ -33,6 +34,16 @@ EEPROMClass::EEPROMClass(uint32_t sector) , _data(0) , _size(0) , _dirty(false) +, _name("eeprom0") +{ +} + +EEPROMClass::EEPROMClass(const char* name) +: _sector(0) +, _data(0) +, _size(0) +, _dirty(false) +, _name(name) { } @@ -41,9 +52,14 @@ EEPROMClass::EEPROMClass(void) , _data(0) , _size(0) , _dirty(false) +, _name("eeprom0") { } +EEPROMClass::~EEPROMClass(){ + // end(); +} + bool EEPROMClass::begin(size_t size) { if (size <= 0) { return false; @@ -51,7 +67,8 @@ bool EEPROMClass::begin(size_t size) { if (size > SPI_FLASH_SEC_SIZE) { size = SPI_FLASH_SEC_SIZE; } - _mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,ESP_PARTITION_SUBTYPE_ANY, EEPROM_FLASH_PARTITION_NAME); +// _mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,ESP_PARTITION_SUBTYPE_ANY, EEPROM_FLASH_PARTITION_NAME); + _mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,ESP_PARTITION_SUBTYPE_ANY, _name); if (_mypart == NULL) { return false; } @@ -62,12 +79,12 @@ bool EEPROMClass::begin(size_t size) { } _data = new uint8_t[size]; - _size = size; + _size = size; bool ret = false; if (esp_partition_read (_mypart,0, (void *) _data,_size)==ESP_OK) { ret=true; } - + return ret; } @@ -121,20 +138,20 @@ bool EEPROMClass::commit() { if (esp_partition_erase_range(_mypart, 0, SPI_FLASH_SEC_SIZE) != ESP_OK) - { - log_e( "partition erase err."); + { + log_e( "partition erase err."); } else { - if (esp_partition_write(_mypart, 0, (void *)_data, _size) == ESP_ERR_INVALID_SIZE) - { - log_e( "error in Write"); - } - else - { - _dirty = false; - ret = true; - } + if (esp_partition_write(_mypart, 0, (void *)_data, _size) == ESP_ERR_INVALID_SIZE) + { + log_e( "error in Write"); + } + else + { + _dirty = false; + ret = true; + } } return ret; @@ -145,6 +162,286 @@ uint8_t * EEPROMClass::getDataPtr() { return &_data[0]; } -#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) -EEPROMClass EEPROM; -#endif +/* + * Get EEPROM total size in byte + */ +uint16_t EEPROMClass::length () +{ + return SPI_FLASH_SEC_SIZE; +} + +/* + * Read 'value' from 'address' + */ +uint8_t EEPROMClass::readByte (int address) +{ + uint8_t value; + return EEPROMClass::readAll (address, value); +} + +int8_t EEPROMClass::readChar (int address) +{ + int8_t value; + return EEPROMClass::readAll (address, value); +} + +uint8_t EEPROMClass::readUChar (int address) +{ + uint8_t value; + return EEPROMClass::readAll (address, value); +} + +int16_t EEPROMClass::readShort (int address) +{ + int16_t value; + return EEPROMClass::readAll (address, value); +} + +uint16_t EEPROMClass::readUShort (int address) +{ + uint16_t value; + return EEPROMClass::readAll (address, value); +} + +int32_t EEPROMClass::readInt (int address) +{ + int32_t value; + return EEPROMClass::readAll (address, value); +} + +uint32_t EEPROMClass::readUInt (int address) +{ + uint32_t value; + return EEPROMClass::readAll (address, value); +} + +int32_t EEPROMClass::readLong (int address) +{ + int32_t value; + return EEPROMClass::readAll (address, value); +} + +uint32_t EEPROMClass::readULong (int address) +{ + uint32_t value; + return EEPROMClass::readAll (address, value); +} + +int64_t EEPROMClass::readLong64 (int address) +{ + int64_t value; + return EEPROMClass::readAll (address, value); +} + +uint64_t EEPROMClass::readULong64 (int address) +{ + uint64_t value; + return EEPROMClass::readAll (address, value); +} + +float_t EEPROMClass::readFloat (int address) +{ + float_t value; + return EEPROMClass::readAll (address, value); +} + +double_t EEPROMClass::readDouble (int address) +{ + double_t value; + return EEPROMClass::readAll (address, value); +} + +bool EEPROMClass::readBool (int address) +{ + int8_t value; + return EEPROMClass::readAll (address, value) ? 1 : 0; +} + +size_t EEPROMClass::readString (int address, char* value, size_t maxLen) +{ + if(!value) + return 0; + + if (address < 0 || address + maxLen > _size) + return 0; + + uint16_t len; + for (len = 0; len <= _size; len++) + if (_data[address + len] == 0) + break; + + if (address + len > _size) + return 0; + + memcpy((uint8_t*) value, _data + address, len); + return len; +} + +String EEPROMClass::readString (int address) +{ + if (address < 0 || address > _size) + return String(0); + + uint16_t len; + for (len = 0; len <= _size; len++) + if (_data[address + len] == 0) + break; + + if (address + len > _size) + return String(0); + + char value[len+1]; + memcpy((uint8_t*) value, _data + address, len); + value[len+1] = 0; + return String(value); +} + +size_t EEPROMClass::readBytes (int address, void* value, size_t maxLen) +{ + if(!value || !maxLen) + return 0; + + if (address < 0 || address + maxLen > _size) + return 0; + + memcpy((void*) value, _data + address, maxLen); + return maxLen; +} + +template T EEPROMClass::readAll (int address, T &value) +{ + if (address < 0 || address + sizeof(T) > _size) + return value; + + memcpy((uint8_t*) &value, _data + address, sizeof(T)); + return value; +} + +/* + * Write 'value' to 'address' + */ +size_t EEPROMClass::writeByte (int address, uint8_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeChar (int address, int8_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeUChar (int address, uint8_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeShort (int address, int16_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeUShort (int address, uint16_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeInt (int address, int32_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeUInt (int address, uint32_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeLong (int address, int32_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeULong (int address, uint32_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeLong64 (int address, int64_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeULong64 (int address, uint64_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeFloat (int address, float_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeDouble (int address, double_t value) +{ + return EEPROMClass::writeAll (address, value); +} + +size_t EEPROMClass::writeBool (int address, bool value) +{ + int8_t Bool; + value ? Bool=1 : Bool=0; + return EEPROMClass::writeAll (address, Bool); +} + +size_t EEPROMClass::writeString (int address, const char* value) +{ + if(!value) + return 0; + + if (address < 0 || address > _size) + return 0; + + uint16_t len; + for (len = 0; len <= _size; len++) + if (value[len] == 0) + break; + + if (address + len > _size) + return 0; + + memcpy(_data + address, (const uint8_t*) value, len+1); + _dirty = true; + return strlen(value); +} + +size_t EEPROMClass::writeString (int address, String value) +{ + return EEPROMClass::writeString (address, value.c_str()); +} + +size_t EEPROMClass::writeBytes (int address, const void* value, size_t len) +{ + if(!value || !len) + return 0; + + if (address < 0 || address + len > _size) + return 0; + + memcpy(_data + address, (const void*) value, len); + _dirty = true; + return len; +} + +template T EEPROMClass::writeAll (int address, const T &value) +{ + if (address < 0 || address + sizeof(T) > _size) + return value; + + memcpy(_data + address, (const uint8_t*) &value, sizeof(T)); + _dirty = true; + + return sizeof (value); +} + +//#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) +//EEPROMClass EEPROM; +//#endif diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index a052e7c2c5b..d70e2835ba3 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -1,13 +1,13 @@ -/* - EEPROM.h -ported by Paolo Becchi to Esp32 - +/* + EEPROM.h -ported by Paolo Becchi to Esp32 + -Modified by Ifediora Elochukwu C. use a one sector flash partition defined in partition table - - from esp8266 EEPROM + + from esp8266 EEPROM Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. This file is part of the esp8266 core for Arduino environment. - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -44,7 +44,9 @@ extern "C" { class EEPROMClass { public: EEPROMClass(uint32_t sector); + EEPROMClass(const char* name); EEPROMClass(void); + ~EEPROMClass(void); bool begin(size_t size); uint8_t read(int address); @@ -54,7 +56,7 @@ class EEPROMClass { uint8_t * getDataPtr(); - template + template T &get(int address, T &t) { if (address < 0 || address + sizeof(T) > _size) return t; @@ -63,7 +65,7 @@ class EEPROMClass { return t; } - template + template const T &put(int address, const T &t) { if (address < 0 || address + sizeof(T) > _size) return t; @@ -73,17 +75,58 @@ class EEPROMClass { return t; } + uint16_t length(); + + uint8_t readByte(int address); + int8_t readChar(int address); + uint8_t readUChar(int address); + int16_t readShort(int address); + uint16_t readUShort(int address); + int32_t readInt(int address); + uint32_t readUInt(int address); + int32_t readLong(int address); + uint32_t readULong(int address); + int64_t readLong64(int address); + uint64_t readULong64(int address); + float_t readFloat(int address); + double_t readDouble(int address); + bool readBool(int address); + size_t readString(int address, char* value, size_t maxLen); + String readString(int address); + size_t readBytes(int address, void * value, size_t maxLen); + template T readAll (int address, T &); + + size_t writeByte(int address, uint8_t value); + size_t writeChar(int address, int8_t value); + size_t writeUChar(int address, uint8_t value); + size_t writeShort(int address, int16_t value); + size_t writeUShort(int address, uint16_t value); + size_t writeInt(int address, int32_t value); + size_t writeUInt(int address, uint32_t value); + size_t writeLong(int address, int32_t value); + size_t writeULong(int address, uint32_t value); + size_t writeLong64(int address, int64_t value); + size_t writeULong64(int address, uint64_t value); + size_t writeFloat(int address, float_t value); + size_t writeDouble(int address, double_t value); + size_t writeBool(int address, bool value); + size_t writeString(int address, const char* value); + size_t writeString(int address, String value); + size_t writeBytes(int address, const void* value, size_t len); + template T writeAll (int address, const T &); + protected: uint32_t _sector; uint8_t* _data; size_t _size; bool _dirty; const esp_partition_t * _mypart; + const char* _name; }; -#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) -extern EEPROMClass EEPROM; -#endif +//#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) +//extern EEPROMClass EEPROM; +//#endif #endif From 582c657ddb0fa57506b9dcd6f11e3b9733e6cd77 Mon Sep 17 00:00:00 2001 From: fedy0 Date: Thu, 4 Jan 2018 03:11:44 +0100 Subject: [PATCH 2/9] Added eeprom examles and modified partition --- libraries/EEPROM/EEPROM.cpp | 178 +++++++++--------- libraries/EEPROM/EEPROM.h | 89 ++++----- .../examples/eeprom_class/eeprom_class.ino | 80 ++++++++ .../examples/eeprom_extra/eeprom_extra.ino | 27 +++ libraries/EEPROM/keywords.txt | 1 + tools/partitions/default.bin | Bin 3072 -> 3072 bytes tools/partitions/default.csv | 14 +- tools/partitions/default_plus_eeproms.csv | 10 + 8 files changed, 259 insertions(+), 140 deletions(-) create mode 100644 libraries/EEPROM/examples/eeprom_class/eeprom_class.ino create mode 100644 libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino create mode 100644 tools/partitions/default_plus_eeproms.csv diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp index fbab3bae4bc..4320be12fcc 100644 --- a/libraries/EEPROM/EEPROM.cpp +++ b/libraries/EEPROM/EEPROM.cpp @@ -2,7 +2,7 @@ EEPROM.cpp -ported by Paolo Becchi to Esp32 Op from esp8266 EEPROM emulation - -Modified by Ifediora Elochukwu C. + -Modified by Ifediora Elochukwu C. Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. This file is part of the esp8266 core for Arduino environment. @@ -30,34 +30,34 @@ static const char* TAG = "eeprom"; EEPROMClass::EEPROMClass(uint32_t sector) -: _sector(sector) -, _data(0) -, _size(0) -, _dirty(false) -, _name("eeprom0") + : _sector(sector) + , _data(0) + , _size(0) + , _dirty(false) + , _name("eeprom") { } EEPROMClass::EEPROMClass(const char* name) -: _sector(0) -, _data(0) -, _size(0) -, _dirty(false) -, _name(name) + : _sector(0) + , _data(0) + , _size(0) + , _dirty(false) + , _name(name) { } EEPROMClass::EEPROMClass(void) - : _sector(0)// (((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE)) -, _data(0) -, _size(0) -, _dirty(false) -, _name("eeprom0") + : _sector(0)// (((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE)) + , _data(0) + , _size(0) + , _dirty(false) + , _name("eeprom") { } -EEPROMClass::~EEPROMClass(){ - // end(); +EEPROMClass::~EEPROMClass() { + // end(); } bool EEPROMClass::begin(size_t size) { @@ -67,8 +67,8 @@ bool EEPROMClass::begin(size_t size) { if (size > SPI_FLASH_SEC_SIZE) { size = SPI_FLASH_SEC_SIZE; } -// _mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,ESP_PARTITION_SUBTYPE_ANY, EEPROM_FLASH_PARTITION_NAME); - _mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,ESP_PARTITION_SUBTYPE_ANY, _name); + // _mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,ESP_PARTITION_SUBTYPE_ANY, EEPROM_FLASH_PARTITION_NAME); + _mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, _name); if (_mypart == NULL) { return false; } @@ -81,8 +81,8 @@ bool EEPROMClass::begin(size_t size) { _data = new uint8_t[size]; _size = size; bool ret = false; - if (esp_partition_read (_mypart,0, (void *) _data,_size)==ESP_OK) { - ret=true; + if (esp_partition_read (_mypart, 0, (void *) _data, _size) == ESP_OK) { + ret = true; } return ret; @@ -115,7 +115,7 @@ uint8_t EEPROMClass::read(int address) { void EEPROMClass::write(int address, uint8_t value) { if (address < 0 || (size_t)address >= _size) return; - if(!_data) + if (!_data) return; // Optimise _dirty. Only flagged if data written is different. @@ -163,103 +163,103 @@ uint8_t * EEPROMClass::getDataPtr() { } /* - * Get EEPROM total size in byte - */ + Get EEPROM total size in byte +*/ uint16_t EEPROMClass::length () { - return SPI_FLASH_SEC_SIZE; + return SPI_FLASH_SEC_SIZE; } /* - * Read 'value' from 'address' - */ + Read 'value' from 'address' +*/ uint8_t EEPROMClass::readByte (int address) { - uint8_t value; - return EEPROMClass::readAll (address, value); + uint8_t value; + return EEPROMClass::readAll (address, value); } int8_t EEPROMClass::readChar (int address) { - int8_t value; - return EEPROMClass::readAll (address, value); + int8_t value; + return EEPROMClass::readAll (address, value); } uint8_t EEPROMClass::readUChar (int address) { - uint8_t value; - return EEPROMClass::readAll (address, value); + uint8_t value; + return EEPROMClass::readAll (address, value); } int16_t EEPROMClass::readShort (int address) { - int16_t value; - return EEPROMClass::readAll (address, value); + int16_t value; + return EEPROMClass::readAll (address, value); } uint16_t EEPROMClass::readUShort (int address) { - uint16_t value; - return EEPROMClass::readAll (address, value); + uint16_t value; + return EEPROMClass::readAll (address, value); } int32_t EEPROMClass::readInt (int address) { - int32_t value; - return EEPROMClass::readAll (address, value); + int32_t value; + return EEPROMClass::readAll (address, value); } uint32_t EEPROMClass::readUInt (int address) { - uint32_t value; - return EEPROMClass::readAll (address, value); + uint32_t value; + return EEPROMClass::readAll (address, value); } int32_t EEPROMClass::readLong (int address) { - int32_t value; - return EEPROMClass::readAll (address, value); + int32_t value; + return EEPROMClass::readAll (address, value); } uint32_t EEPROMClass::readULong (int address) { - uint32_t value; - return EEPROMClass::readAll (address, value); + uint32_t value; + return EEPROMClass::readAll (address, value); } int64_t EEPROMClass::readLong64 (int address) { - int64_t value; - return EEPROMClass::readAll (address, value); + int64_t value; + return EEPROMClass::readAll (address, value); } uint64_t EEPROMClass::readULong64 (int address) { - uint64_t value; - return EEPROMClass::readAll (address, value); + uint64_t value; + return EEPROMClass::readAll (address, value); } float_t EEPROMClass::readFloat (int address) { - float_t value; - return EEPROMClass::readAll (address, value); + float_t value; + return EEPROMClass::readAll (address, value); } double_t EEPROMClass::readDouble (int address) { - double_t value; - return EEPROMClass::readAll (address, value); + double_t value; + return EEPROMClass::readAll (address, value); } bool EEPROMClass::readBool (int address) { - int8_t value; - return EEPROMClass::readAll (address, value) ? 1 : 0; + int8_t value; + return EEPROMClass::readAll (address, value) ? 1 : 0; } size_t EEPROMClass::readString (int address, char* value, size_t maxLen) { - if(!value) + if (!value) return 0; if (address < 0 || address + maxLen > _size) @@ -274,7 +274,7 @@ size_t EEPROMClass::readString (int address, char* value, size_t maxLen) return 0; memcpy((uint8_t*) value, _data + address, len); - return len; + return len; } String EEPROMClass::readString (int address) @@ -290,22 +290,22 @@ String EEPROMClass::readString (int address) if (address + len > _size) return String(0); - char value[len+1]; + char value[len + 1]; memcpy((uint8_t*) value, _data + address, len); - value[len+1] = 0; + value[len + 1] = 0; return String(value); } size_t EEPROMClass::readBytes (int address, void* value, size_t maxLen) { - if(!value || !maxLen) + if (!value || !maxLen) return 0; if (address < 0 || address + maxLen > _size) return 0; memcpy((void*) value, _data + address, maxLen); - return maxLen; + return maxLen; } template T EEPROMClass::readAll (int address, T &value) @@ -314,87 +314,87 @@ template T EEPROMClass::readAll (int address, T &value) return value; memcpy((uint8_t*) &value, _data + address, sizeof(T)); - return value; + return value; } /* - * Write 'value' to 'address' - */ + Write 'value' to 'address' +*/ size_t EEPROMClass::writeByte (int address, uint8_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeChar (int address, int8_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeUChar (int address, uint8_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeShort (int address, int16_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeUShort (int address, uint16_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeInt (int address, int32_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeUInt (int address, uint32_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeLong (int address, int32_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeULong (int address, uint32_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeLong64 (int address, int64_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeULong64 (int address, uint64_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeFloat (int address, float_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeDouble (int address, double_t value) { - return EEPROMClass::writeAll (address, value); + return EEPROMClass::writeAll (address, value); } size_t EEPROMClass::writeBool (int address, bool value) { int8_t Bool; - value ? Bool=1 : Bool=0; - return EEPROMClass::writeAll (address, Bool); + value ? Bool = 1 : Bool = 0; + return EEPROMClass::writeAll (address, Bool); } size_t EEPROMClass::writeString (int address, const char* value) { - if(!value) + if (!value) return 0; if (address < 0 || address > _size) @@ -408,9 +408,9 @@ size_t EEPROMClass::writeString (int address, const char* value) if (address + len > _size) return 0; - memcpy(_data + address, (const uint8_t*) value, len+1); + memcpy(_data + address, (const uint8_t*) value, len + 1); _dirty = true; - return strlen(value); + return strlen(value); } size_t EEPROMClass::writeString (int address, String value) @@ -420,7 +420,7 @@ size_t EEPROMClass::writeString (int address, String value) size_t EEPROMClass::writeBytes (int address, const void* value, size_t len) { - if(!value || !len) + if (!value || !len) return 0; if (address < 0 || address + len > _size) @@ -428,7 +428,7 @@ size_t EEPROMClass::writeBytes (int address, const void* value, size_t len) memcpy(_data + address, (const void*) value, len); _dirty = true; - return len; + return len; } template T EEPROMClass::writeAll (int address, const T &value) @@ -439,9 +439,9 @@ template T EEPROMClass::writeAll (int address, const T &value) memcpy(_data + address, (const uint8_t*) &value, sizeof(T)); _dirty = true; - return sizeof (value); + return sizeof (value); } -//#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) -//EEPROMClass EEPROM; -//#endif +#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) +EEPROMClass EEPROM; +#endif diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index d70e2835ba3..d991c3c2db6 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -1,7 +1,10 @@ /* EEPROM.h -ported by Paolo Becchi to Esp32 - -Modified by Ifediora Elochukwu C. - use a one sector flash partition defined in partition table + -Modified by Ifediora Elochukwu C. + + Uses a one sector flash partition defined in partition table + OR + Multiple sector flash partitions defined by the name column in the partition table from esp8266 EEPROM @@ -26,7 +29,7 @@ #ifndef EEPROM_h #define EEPROM_h #ifndef EEPROM_FLASH_PARTITION_NAME - #define EEPROM_FLASH_PARTITION_NAME "eeprom" +#define EEPROM_FLASH_PARTITION_NAME "eeprom" #endif extern "C" { @@ -37,45 +40,44 @@ extern "C" { } // -// need to define a flash partition for EEPROM with above name +// need to define AT LEAST a flash partition for EEPROM with above name // // eeprom , data , 0x99, start address, 0x1000 // class EEPROMClass { -public: - EEPROMClass(uint32_t sector); - EEPROMClass(const char* name); - EEPROMClass(void); - ~EEPROMClass(void); - - bool begin(size_t size); - uint8_t read(int address); - void write(int address, uint8_t val); - bool commit(); - void end(); - - uint8_t * getDataPtr(); - - template - T &get(int address, T &t) { - if (address < 0 || address + sizeof(T) > _size) - return t; + public: + EEPROMClass(uint32_t sector); + EEPROMClass(const char* name); + EEPROMClass(void); + ~EEPROMClass(void); + + bool begin(size_t size); + uint8_t read(int address); + void write(int address, uint8_t val); + uint16_t length(); + bool commit(); + void end(); + + uint8_t * getDataPtr(); - memcpy((uint8_t*) &t, _data + address, sizeof(T)); - return t; - } + template + T &get(int address, T &t) { + if (address < 0 || address + sizeof(T) > _size) + return t; - template - const T &put(int address, const T &t) { - if (address < 0 || address + sizeof(T) > _size) + memcpy((uint8_t*) &t, _data + address, sizeof(T)); return t; + } - memcpy(_data + address, (const uint8_t*) &t, sizeof(T)); - _dirty = true; - return t; - } + template + const T &put(int address, const T &t) { + if (address < 0 || address + sizeof(T) > _size) + return t; - uint16_t length(); + memcpy(_data + address, (const uint8_t*) &t, sizeof(T)); + _dirty = true; + return t; + } uint8_t readByte(int address); int8_t readChar(int address); @@ -115,18 +117,17 @@ class EEPROMClass { size_t writeBytes(int address, const void* value, size_t len); template T writeAll (int address, const T &); -protected: - uint32_t _sector; - uint8_t* _data; - size_t _size; - bool _dirty; - const esp_partition_t * _mypart; - const char* _name; + protected: + uint32_t _sector; + uint8_t* _data; + size_t _size; + bool _dirty; + const esp_partition_t * _mypart; + const char* _name; }; -//#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) -//extern EEPROMClass EEPROM; -//#endif - +#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) +extern EEPROMClass EEPROM; #endif +#endif diff --git a/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino b/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino new file mode 100644 index 00000000000..980cd35d201 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino @@ -0,0 +1,80 @@ +/* + ESP32 eeprom_class example with EEPROM library + + This simple example demonstrates using EEPROM library to store different data in + ESP32 Flash memory in a multiple user-defined EEPROM partition (0x1000 or 4KB max size). + Usage: EEPROMClass ANY_OBJECT_NAME("partition name"); + ANY_OBJECT_NAME.method() + ANY_OBJECT_NAME.member + + Created for arduino-esp32 on 25 Dec, 2017 + by Ifediora Elochukwu (fedy0) +*/ + +#include "EEPROM.h" + +// Instantiate eeprom objects with parameter/argument names same as in the partition table +EEPROMClass NAMES("eeprom0"); +EEPROMClass HEIGHT("eeprom1"); +EEPROMClass AGE("eeprom2"); + +void setup() { + // put your setup code here, to run once: + Serial.begin(115200); + Serial.println("Testing EEPROMClass\n"); + if (!NAMES.begin(NAMES.length())) { + Serial.println("Failed to initialise NAMES"); + Serial.println("Restarting..."); + delay(1000); + ESP.restart(); + } + if (!HEIGHT.begin(HEIGHT.length())) { + Serial.println("Failed to initialise HEIGHT"); + Serial.println("Restarting..."); + delay(1000); + ESP.restart(); + } + if (!AGE.begin(AGE.length())) { + Serial.println("Failed to initialise AGE"); + Serial.println("Restarting..."); + delay(1000); + ESP.restart(); + } + + char* name = "Teo Swee Ann"; + double height = 5.8; + uint32_t age = 47; + + // Write: Variables ---> EEPROM partitions + NAMES.put(0, name); + HEIGHT.put(0, height); + AGE.put(0, age); + Serial.print("name: "); Serial.println(name); + Serial.print("height: "); Serial.println(height); + Serial.print("age: "); Serial.println(age); + Serial.println("------------------------------------\n"); + + // Clear variables + name = '\0'; + height = 0; + age = 0; + Serial.print("name: "); Serial.println(name); + Serial.print("height: "); Serial.println(height); + Serial.print("age: "); Serial.println(age); + Serial.println("------------------------------------\n"); + + // Read: Variables <--- EEPROM partitions + NAMES.get(0, name); + HEIGHT.get(0, height); + AGE.get(0, age); + Serial.print("name: "); Serial.println(name); + Serial.print("height: "); Serial.println(height); + Serial.print("age: "); Serial.println(age); + + Serial.println("Done!"); +} + +void loop() { + // put your main code here, to run repeatedly: + +} diff --git a/libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino b/libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino new file mode 100644 index 00000000000..8feb18d5eba --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino @@ -0,0 +1,27 @@ +/* + ESP32 eeprom_extra example with EEPROM library + + This simple example demonstrates using other EEPROM library resources + + Created for arduino-esp32 on 25 Dec, 2017 + by Ifediora Elochukwu (fedy0) +*/ + +#include "EEPROM.h" + +void setup() { + // put your setup code here, to run once: + Serial.begin(115200); + Serial.println("Testing EEPROM Library\n"); + if (!EEPROM.begin(EEPROM.length())) { + Serial.println("Failed to initialise EEPROM"); + Serial.println("Restarting..."); + delay(1000); + ESP.restart(); + } +} + +void loop() { + // put your main code here, to run repeatedly: + +} diff --git a/libraries/EEPROM/keywords.txt b/libraries/EEPROM/keywords.txt index d3218fe2ac4..0e2552e12c4 100644 --- a/libraries/EEPROM/keywords.txt +++ b/libraries/EEPROM/keywords.txt @@ -7,6 +7,7 @@ ####################################### EEPROM KEYWORD1 +EEPROMClass KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) diff --git a/tools/partitions/default.bin b/tools/partitions/default.bin index 283b2ebbeebae41abfdb03e025c5d3792cb3168a..eabbf0cb455c430b77e8d5b5675030b1a6feff77 100644 GIT binary patch delta 85 zcmZpWXporT#V9h-TiR8Wfq_AUA+eyq5Xe9Qs{$BjGB9W|FbFU(q^1@W<>#VEGXUkA P7zBXwAH+6xhjIe|JN^x% delta 56 ycmZpWXporT#V9b*TUu3+fq_AQA+eyq5Xe9Qs{$CC7#I|RV)7eThjLH6pa1{~0|~_d diff --git a/tools/partitions/default.csv b/tools/partitions/default.csv index 3e4235d739f..0e062537b22 100644 --- a/tools/partitions/default.csv +++ b/tools/partitions/default.csv @@ -1,7 +1,7 @@ -# Name, Type, SubType, Offset, Size, Flags -nvs, data, nvs, 0x9000, 0x5000, -otadata, data, ota, 0xe000, 0x2000, -app0, app, ota_0, 0x10000, 0x140000, -app1, app, ota_1, 0x150000,0x140000, -eeprom, data, 0x99, 0x290000,0x1000, -spiffs, data, spiffs, 0x291000,0x16F000, +# Name,Type,SubType,Offset,Size,Flags +nvs,data,nvs,0x9000,0x5000, +otadata,data,ota,0xe000,0x2000, +app0,app,ota_0,0x10000,0x140000, +app1,app,ota_1,0x150000,0x140000, +eeprom,data,0x99,0x290000,0x1000, +spiffs,data,spiffs,0x291000,0x16F000, diff --git a/tools/partitions/default_plus_eeproms.csv b/tools/partitions/default_plus_eeproms.csv new file mode 100644 index 00000000000..88eeac44d00 --- /dev/null +++ b/tools/partitions/default_plus_eeproms.csv @@ -0,0 +1,10 @@ +# Name,Type,SubType,Offset,Size,Flags +nvs,data,nvs,0x9000,0x5000, +otadata,data,ota,0xe000,0x2000, +app0,app,ota_0,0x10000,0x140000, +app1,app,ota_1,0x150000,0x140000, +eeprom,data,0x99,0x290000,0x1000, +eeprom0,data,0xa7,0x291000,0x1000, +eeprom1,data,0xa8,0x292000,0x1000, +eeprom2,data,0xa9,0x293000,0x1000, +spiffs,data,spiffs,0x294000,0x16C000, From 81cf573935446fd27b00b39ad7478e2e49bd1fb8 Mon Sep 17 00:00:00 2001 From: fedy0 Date: Thu, 25 Jan 2018 22:22:05 +0100 Subject: [PATCH 3/9] added eeprom class and extra examples --- .../ArduinoOTA/examples/BasicOTA/BasicOTA.ino | 4 +- .../examples/eeprom_class/eeprom_class.ino | 7 +- .../examples/eeprom_extra/eeprom_extra.ino | 67 ++++++++++++++++++- tools/partitions/default_plus_eeproms.csv | 10 --- 4 files changed, 71 insertions(+), 17 deletions(-) delete mode 100644 tools/partitions/default_plus_eeproms.csv diff --git a/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino b/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino index 1676539ba39..72ba41de16c 100644 --- a/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino +++ b/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino @@ -3,8 +3,8 @@ #include #include -const char* ssid = ".........."; -const char* password = ".........."; +const char* ssid = "BET9JARUMUOSI"; +const char* password = "41363659"; void setup() { Serial.begin(115200); diff --git a/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino b/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino index 980cd35d201..cdc58d0b741 100644 --- a/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino +++ b/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino @@ -3,9 +3,10 @@ This simple example demonstrates using EEPROM library to store different data in ESP32 Flash memory in a multiple user-defined EEPROM partition (0x1000 or 4KB max size). - Usage: EEPROMClass ANY_OBJECT_NAME("partition name"); - ANY_OBJECT_NAME.method() - ANY_OBJECT_NAME.member + + Install 'ESP32 Partiton Manager' ONCE from https://github.com/francis94c/ESP32Partitions + And create different partitions with 'partition_name' + Usage: EEPROMClass ANY_OBJECT_NAME("partition_name"); Created for arduino-esp32 on 25 Dec, 2017 by Ifediora Elochukwu (fedy0) diff --git a/libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino b/libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino index 8feb18d5eba..450e5cf78bf 100644 --- a/libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino +++ b/libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino @@ -12,16 +12,79 @@ void setup() { // put your setup code here, to run once: Serial.begin(115200); - Serial.println("Testing EEPROM Library\n"); + Serial.println("\nTesting EEPROM Library\n"); if (!EEPROM.begin(EEPROM.length())) { Serial.println("Failed to initialise EEPROM"); Serial.println("Restarting..."); delay(1000); ESP.restart(); } + + int address = 0; // Same address is used through the example + + EEPROM.writeByte(address, -128); // -2^7 + Serial.println(EEPROM.readByte(address)); + + EEPROM.writeChar(address, 'A'); // Same as writyByte and readByte + Serial.println(char(EEPROM.readChar(address))); + + EEPROM.writeUChar(address, 255); // 2^8 - 1 + Serial.println(EEPROM.readUChar(address)); + + EEPROM.writeShort(address, -32768); // -2^15 + Serial.println(EEPROM.readShort(address)); + + EEPROM.writeUShort(address, 65535); // 2^16 - 1 + Serial.println(EEPROM.readUShort(address)); + + EEPROM.writeInt(address, -2147483648); // -2^31 + Serial.println(EEPROM.readInt(address)); + + EEPROM.writeUInt(address, 4294967295); // 2^32 - 1 + Serial.println(EEPROM.readUInt(address)); + + EEPROM.writeLong(address, -2147483648); // Same as writeInt and readInt + Serial.println(EEPROM.readLong(address)); + + EEPROM.writeULong(address, 4294967295); // Same as writeUInt and readUInt + Serial.println(EEPROM.readULong(address)); + + int64_t value = -9223372036854775808; // -2^63 + EEPROM.writeLong64(address, value); + value = 0; // Clear value + value = EEPROM.readLong64(value); + Serial.printf("0x%08X", (uint32_t)(value >> 32)); // Print High 4 bytes in HEX + Serial.printf("%08X\n", (uint32_t)value); // Print Low 4 bytes in HEX + + uint64_t Value = 18446744073709551615; // 2^64 - 1 + EEPROM.writeULong64(address, Value); + Value = 0; // Clear Value + Value = EEPROM.readULong64(Value); + Serial.printf("0x%08X", (uint32_t)(Value >> 32)); // Print High 4 bytes in HEX + Serial.printf("%08X\n", (uint32_t)Value); // Print Low 4 bytes in HEX + + EEPROM.writeFloat(address, 1234.1234); + Serial.println(EEPROM.readFloat(address), 4); + + EEPROM.writeDouble(address, 123456789.123456789); + Serial.println(EEPROM.readDouble(address), 8); + + EEPROM.writeBool(address, true); + Serial.println(EEPROM.readBool(address)); + + String sentence = "I love ESP32."; + EEPROM.writeString(address, sentence); + Serial.println(EEPROM.readString(address)); + + char gratitude[] = "Thank You Espressif!"; + EEPROM.writeString(address, gratitude); + Serial.println(EEPROM.readString(address)); + + // See also the general purpose writeBytes() and readBytes() for BLOB in EEPROM library + // To avoid data overwrite, next address should be chosen/offset by using "address =+ sizeof(previousData)" } void loop() { // put your main code here, to run repeatedly: -} +} \ No newline at end of file diff --git a/tools/partitions/default_plus_eeproms.csv b/tools/partitions/default_plus_eeproms.csv deleted file mode 100644 index 88eeac44d00..00000000000 --- a/tools/partitions/default_plus_eeproms.csv +++ /dev/null @@ -1,10 +0,0 @@ -# Name,Type,SubType,Offset,Size,Flags -nvs,data,nvs,0x9000,0x5000, -otadata,data,ota,0xe000,0x2000, -app0,app,ota_0,0x10000,0x140000, -app1,app,ota_1,0x150000,0x140000, -eeprom,data,0x99,0x290000,0x1000, -eeprom0,data,0xa7,0x291000,0x1000, -eeprom1,data,0xa8,0x292000,0x1000, -eeprom2,data,0xa9,0x293000,0x1000, -spiffs,data,spiffs,0x294000,0x16C000, From 45846b09e328a1aa0155f647fe6c099b5b677827 Mon Sep 17 00:00:00 2001 From: fedy0 Date: Thu, 25 Jan 2018 22:30:57 +0100 Subject: [PATCH 4/9] No changes --- libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino b/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino index 72ba41de16c..83652495295 100644 --- a/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino +++ b/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino @@ -3,8 +3,8 @@ #include #include -const char* ssid = "BET9JARUMUOSI"; -const char* password = "41363659"; +const char* ssid = ".........."; +const char* password = ".........."; void setup() { Serial.begin(115200); @@ -65,4 +65,4 @@ void setup() { void loop() { ArduinoOTA.handle(); -} +} \ No newline at end of file From 666c3a7ab86ac475418e6f1dcd2cb03a33bfaa38 Mon Sep 17 00:00:00 2001 From: fedy0 Date: Thu, 25 Jan 2018 22:42:47 +0100 Subject: [PATCH 5/9] No changes --- tools/partitions/default.csv | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tools/partitions/default.csv b/tools/partitions/default.csv index 0e062537b22..3e4235d739f 100644 --- a/tools/partitions/default.csv +++ b/tools/partitions/default.csv @@ -1,7 +1,7 @@ -# Name,Type,SubType,Offset,Size,Flags -nvs,data,nvs,0x9000,0x5000, -otadata,data,ota,0xe000,0x2000, -app0,app,ota_0,0x10000,0x140000, -app1,app,ota_1,0x150000,0x140000, -eeprom,data,0x99,0x290000,0x1000, -spiffs,data,spiffs,0x291000,0x16F000, +# Name, Type, SubType, Offset, Size, Flags +nvs, data, nvs, 0x9000, 0x5000, +otadata, data, ota, 0xe000, 0x2000, +app0, app, ota_0, 0x10000, 0x140000, +app1, app, ota_1, 0x150000,0x140000, +eeprom, data, 0x99, 0x290000,0x1000, +spiffs, data, spiffs, 0x291000,0x16F000, From a466c5b5d6da7d44cd2c03f9bd4b65d3feb41e79 Mon Sep 17 00:00:00 2001 From: fedy0 Date: Thu, 25 Jan 2018 22:49:11 +0100 Subject: [PATCH 6/9] added eeprom class and examples --- libraries/EEPROM/EEPROM.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index d991c3c2db6..9ecce1abbaa 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -1,12 +1,10 @@ /* - EEPROM.h -ported by Paolo Becchi to Esp32 - -Modified by Ifediora Elochukwu C. + EEPROM.h -ported by Paolo Becchi to Esp32 from esp8266 EEPROM + -Modified by Ifediora Elochukwu C. - Uses a one sector flash partition defined in partition table - OR - Multiple sector flash partitions defined by the name column in the partition table - - from esp8266 EEPROM + Uses a one sector flash partition defined in partition table + OR + Multiple sector flash partitions defined by the name column in the partition table Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. This file is part of the esp8266 core for Arduino environment. From e4e87f5b563b346154ac203d4c69b023898c4a95 Mon Sep 17 00:00:00 2001 From: fedy0 Date: Sun, 28 Jan 2018 00:39:50 +0100 Subject: [PATCH 7/9] fixed typo --- libraries/EEPROM/EEPROM.cpp | 10 ++++++---- libraries/EEPROM/EEPROM.h | 2 +- .../EEPROM/examples/eeprom_class/eeprom_class.ino | 2 +- .../EEPROM/examples/eeprom_extra/eeprom_extra.ino | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp index 4320be12fcc..79e55ee9c79 100644 --- a/libraries/EEPROM/EEPROM.cpp +++ b/libraries/EEPROM/EEPROM.cpp @@ -1,8 +1,10 @@ /* - EEPROM.cpp -ported by Paolo Becchi to Esp32 - Op - from esp8266 EEPROM emulation - -Modified by Ifediora Elochukwu C. + EEPROM.h -ported by Paolo Becchi to Esp32 from esp8266 EEPROM + -Modified by Elochukwu Ifediora + + Uses a one sector flash partition defined in partition table + OR + Multiple sector flash partitions defined by the name column in the partition table Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. This file is part of the esp8266 core for Arduino environment. diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index 9ecce1abbaa..0582d7b61d6 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -1,6 +1,6 @@ /* EEPROM.h -ported by Paolo Becchi to Esp32 from esp8266 EEPROM - -Modified by Ifediora Elochukwu C. + -Modified by Elochukwu Ifediora Uses a one sector flash partition defined in partition table OR diff --git a/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino b/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino index cdc58d0b741..1f70873315a 100644 --- a/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino +++ b/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino @@ -9,7 +9,7 @@ Usage: EEPROMClass ANY_OBJECT_NAME("partition_name"); Created for arduino-esp32 on 25 Dec, 2017 - by Ifediora Elochukwu (fedy0) + by Elochukwu Ifediora (fedy0) */ #include "EEPROM.h" diff --git a/libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino b/libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino index 450e5cf78bf..b2ea72f7d73 100644 --- a/libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino +++ b/libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino @@ -4,7 +4,7 @@ This simple example demonstrates using other EEPROM library resources Created for arduino-esp32 on 25 Dec, 2017 - by Ifediora Elochukwu (fedy0) + by Elochukwu Ifediora (fedy0) */ #include "EEPROM.h" From b067fac20522ff00ad64fd13bb4a7a094cc74fcf Mon Sep 17 00:00:00 2001 From: fedy0 Date: Fri, 2 Feb 2018 02:56:33 +0100 Subject: [PATCH 8/9] length() returns user-defined sector size --- libraries/EEPROM/EEPROM.cpp | 7 ++++--- libraries/EEPROM/EEPROM.h | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp index 79e55ee9c79..62e15af174d 100644 --- a/libraries/EEPROM/EEPROM.cpp +++ b/libraries/EEPROM/EEPROM.cpp @@ -40,12 +40,13 @@ EEPROMClass::EEPROMClass(uint32_t sector) { } -EEPROMClass::EEPROMClass(const char* name) +EEPROMClass::EEPROMClass(const char* name, uint32_t user_defined_size) : _sector(0) , _data(0) , _size(0) , _dirty(false) , _name(name) + , _user_defined_size(user_defined_size) { } @@ -165,11 +166,11 @@ uint8_t * EEPROMClass::getDataPtr() { } /* - Get EEPROM total size in byte + Get EEPROM total size in byte defined by the user */ uint16_t EEPROMClass::length () { - return SPI_FLASH_SEC_SIZE; + return _user_defined_size; } /* diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index 0582d7b61d6..2e4383c9bd2 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -45,7 +45,7 @@ extern "C" { class EEPROMClass { public: EEPROMClass(uint32_t sector); - EEPROMClass(const char* name); + EEPROMClass(const char* name, uint32_t user_defined_size); EEPROMClass(void); ~EEPROMClass(void); @@ -122,6 +122,7 @@ class EEPROMClass { bool _dirty; const esp_partition_t * _mypart; const char* _name; + uint32_t _user_defined_size; }; #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) From be8b7565a43c79136e5c7a745cf30b99eccb2780 Mon Sep 17 00:00:00 2001 From: fedy0 Date: Fri, 2 Feb 2018 02:59:59 +0100 Subject: [PATCH 9/9] updated and annotated example --- .../examples/eeprom_class/eeprom_class.ino | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino b/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino index 1f70873315a..af098809643 100644 --- a/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino +++ b/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino @@ -2,11 +2,22 @@ ESP32 eeprom_class example with EEPROM library This simple example demonstrates using EEPROM library to store different data in - ESP32 Flash memory in a multiple user-defined EEPROM partition (0x1000 or 4KB max size). + ESP32 Flash memory in a multiple user-defined EEPROM partition (0x1000 or 4KB max size or less). Install 'ESP32 Partiton Manager' ONCE from https://github.com/francis94c/ESP32Partitions - And create different partitions with 'partition_name' - Usage: EEPROMClass ANY_OBJECT_NAME("partition_name"); + And generate different partitions with 'partition_name' + Usage: EEPROMClass ANY_OBJECT_NAME("partition_name", size); + + Generated partition that would work perfectly with this example + #Name, Type, SubType, Offset, Size, Flags + nvs, data, nvs, 0x9000, 0x5000, + otadata, data, ota, 0xe000, 0x2000, + app0, app, ota_0, 0x10000, 0x140000, + app1, app, ota_1, 0x150000, 0x140000, + eeprom0, data, 0x99, 0x290000, 0x1000, + eeprom1, data, 0x9a, 0x291000, 0x500, + eeprom2, data, 0x9b, 0x292000, 0x100, + spiffs, data, spiffs, 0x293000, 0x16d000, Created for arduino-esp32 on 25 Dec, 2017 by Elochukwu Ifediora (fedy0) @@ -14,10 +25,10 @@ #include "EEPROM.h" -// Instantiate eeprom objects with parameter/argument names same as in the partition table -EEPROMClass NAMES("eeprom0"); -EEPROMClass HEIGHT("eeprom1"); -EEPROMClass AGE("eeprom2"); +// Instantiate eeprom objects with parameter/argument names and size same as in the partition table +EEPROMClass NAMES("eeprom0", 0x1000); +EEPROMClass HEIGHT("eeprom1", 0x500); +EEPROMClass AGE("eeprom2", 0x100); void setup() { // put your setup code here, to run once: