diff --git a/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino b/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino index 1676539b..83652495 100644 --- a/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino +++ b/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino @@ -65,4 +65,4 @@ void setup() { void loop() { ArduinoOTA.handle(); -} +} \ No newline at end of file diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp index dcad0f06..62e15af1 100644 --- a/libraries/EEPROM/EEPROM.cpp +++ b/libraries/EEPROM/EEPROM.cpp @@ -1,7 +1,10 @@ /* - EEPROM.cpp -ported by Paolo Becchi to Esp32 - Op - from esp8266 EEPROM emulation + 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. @@ -29,21 +32,37 @@ static const char* TAG = "eeprom"; EEPROMClass::EEPROMClass(uint32_t sector) -: _sector(sector) -, _data(0) -, _size(0) -, _dirty(false) + : _sector(sector) + , _data(0) + , _size(0) + , _dirty(false) + , _name("eeprom") +{ +} + +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) { } EEPROMClass::EEPROMClass(void) - : _sector(0)// (((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE)) -, _data(0) -, _size(0) -, _dirty(false) + : _sector(0)// (((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE)) + , _data(0) + , _size(0) + , _dirty(false) + , _name("eeprom") { } +EEPROMClass::~EEPROMClass() { + // end(); +} + bool EEPROMClass::begin(size_t size) { if (size <= 0) { return false; @@ -51,7 +70,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 +82,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; + if (esp_partition_read (_mypart, 0, (void *) _data, _size) == ESP_OK) { + ret = true; } - + return ret; } @@ -98,7 +118,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. @@ -121,20 +141,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 +165,286 @@ uint8_t * EEPROMClass::getDataPtr() { return &_data[0]; } +/* + Get EEPROM total size in byte defined by the user +*/ +uint16_t EEPROMClass::length () +{ + return _user_defined_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 a052e7c2..2e4383c9 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -1,13 +1,14 @@ -/* - EEPROM.h -ported by Paolo Becchi to Esp32 +/* + EEPROM.h -ported by Paolo Becchi to Esp32 from esp8266 EEPROM + -Modified by Elochukwu Ifediora - use a one sector flash partition defined in 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. - + 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 @@ -26,7 +27,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,48 +38,91 @@ 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(void); + public: + EEPROMClass(uint32_t sector); + EEPROMClass(const char* name, uint32_t user_defined_size); + 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(); + 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(); + uint8_t * getDataPtr(); - template - T &get(int address, T &t) { - if (address < 0 || address + sizeof(T) > _size) + template + T &get(int address, T &t) { + if (address < 0 || address + sizeof(T) > _size) + return t; + + memcpy((uint8_t*) &t, _data + address, sizeof(T)); return t; + } - memcpy((uint8_t*) &t, _data + address, sizeof(T)); - return t; - } + template + const T &put(int address, const 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(_data + address, (const uint8_t*) &t, sizeof(T)); + _dirty = true; return t; + } - memcpy(_data + address, (const uint8_t*) &t, sizeof(T)); - _dirty = true; - return t; - } + 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 &); -protected: - uint32_t _sector; - uint8_t* _data; - size_t _size; - bool _dirty; - const esp_partition_t * _mypart; + 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; + uint32_t _user_defined_size; }; #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) @@ -86,4 +130,3 @@ 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 00000000..af098809 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_class/eeprom_class.ino @@ -0,0 +1,92 @@ +/* + 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 or less). + + Install 'ESP32 Partiton Manager' ONCE from https://github.com/francis94c/ESP32Partitions + 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) +*/ + +#include "EEPROM.h" + +// 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: + 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 00000000..b2ea72f7 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino @@ -0,0 +1,90 @@ +/* + 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 Elochukwu Ifediora (fedy0) +*/ + +#include "EEPROM.h" + +void setup() { + // put your setup code here, to run once: + Serial.begin(115200); + 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/libraries/EEPROM/keywords.txt b/libraries/EEPROM/keywords.txt index d3218fe2..0e2552e1 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 283b2ebb..eabbf0cb 100644 Binary files a/tools/partitions/default.bin and b/tools/partitions/default.bin differ