|
| 1 | +/* |
| 2 | + EEPROM.cpp -ported by Paolo Becchi to Esp32 |
| 3 | + Op |
| 4 | + from esp8266 EEPROM emulation |
| 5 | +
|
| 6 | + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. |
| 7 | + This file is part of the esp8266 core for Arduino environment. |
| 8 | +
|
| 9 | + This library is free software; you can redistribute it and/or |
| 10 | + modify it under the terms of the GNU Lesser General Public |
| 11 | + License as published by the Free Software Foundation; either |
| 12 | + version 2.1 of the License, or (at your option) any later version. |
| 13 | +
|
| 14 | + This library is distributed in the hope that it will be useful, |
| 15 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + Lesser General Public License for more details. |
| 18 | +
|
| 19 | + You should have received a copy of the GNU Lesser General Public |
| 20 | + License along with this library; if not, write to the Free Software |
| 21 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | +*/ |
| 23 | + |
| 24 | +#include "Arduino.h" |
| 25 | +#include "EEPROM.h" |
| 26 | + |
| 27 | +#include <esp_log.h> |
| 28 | + |
| 29 | +static const char* TAG = "eeprom"; |
| 30 | + |
| 31 | +EEPROMClass::EEPROMClass(uint32_t sector) |
| 32 | +: _sector(sector) |
| 33 | +, _data(0) |
| 34 | +, _size(0) |
| 35 | +, _dirty(false) |
| 36 | +{ |
| 37 | +} |
| 38 | + |
| 39 | +EEPROMClass::EEPROMClass(void) |
| 40 | + : _sector(0)// (((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE)) |
| 41 | +, _data(0) |
| 42 | +, _size(0) |
| 43 | +, _dirty(false) |
| 44 | +{ |
| 45 | +} |
| 46 | + |
| 47 | +bool EEPROMClass::begin(size_t size) { |
| 48 | + if (size <= 0) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + if (size > SPI_FLASH_SEC_SIZE) { |
| 52 | + size = SPI_FLASH_SEC_SIZE; |
| 53 | + } |
| 54 | + _mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,ESP_PARTITION_SUBTYPE_ANY, EEPROM_FLASH_PARTITION_NAME); |
| 55 | + if (_mypart == NULL) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + size = (size + 3) & (~3); |
| 59 | + |
| 60 | + if (_data) { |
| 61 | + delete[] _data; |
| 62 | + } |
| 63 | + |
| 64 | + _data = new uint8_t[size]; |
| 65 | + _size = size; |
| 66 | + bool ret = false; |
| 67 | + if (esp_partition_read (_mypart,0, (void *) _data,_size)==ESP_OK) { |
| 68 | + ret=true; |
| 69 | + } |
| 70 | + |
| 71 | + return ret; |
| 72 | +} |
| 73 | + |
| 74 | +void EEPROMClass::end() { |
| 75 | + if (!_size) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + commit(); |
| 80 | + if (_data) { |
| 81 | + delete[] _data; |
| 82 | + } |
| 83 | + _data = 0; |
| 84 | + _size = 0; |
| 85 | +} |
| 86 | + |
| 87 | +uint8_t EEPROMClass::read(int address) { |
| 88 | + if (address < 0 || (size_t)address >= _size) { |
| 89 | + return 0; |
| 90 | + } |
| 91 | + if (!_data) { |
| 92 | + return 0; |
| 93 | + } |
| 94 | + |
| 95 | + return _data[address]; |
| 96 | +} |
| 97 | + |
| 98 | +void EEPROMClass::write(int address, uint8_t value) { |
| 99 | + if (address < 0 || (size_t)address >= _size) |
| 100 | + return; |
| 101 | + if(!_data) |
| 102 | + return; |
| 103 | + |
| 104 | + // Optimise _dirty. Only flagged if data written is different. |
| 105 | + uint8_t* pData = &_data[address]; |
| 106 | + if (*pData != value) |
| 107 | + { |
| 108 | + *pData = value; |
| 109 | + _dirty = true; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +bool EEPROMClass::commit() { |
| 114 | + bool ret = false; |
| 115 | + if (!_size) |
| 116 | + return false; |
| 117 | + if (!_dirty) |
| 118 | + return true; |
| 119 | + if (!_data) |
| 120 | + return false; |
| 121 | + |
| 122 | + |
| 123 | + if (esp_partition_erase_range(_mypart, 0, SPI_FLASH_SEC_SIZE) != ESP_OK) |
| 124 | + { |
| 125 | + log_e( "partition erase err."); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + if (esp_partition_write(_mypart, 0, (void *)_data, _size) == ESP_ERR_INVALID_SIZE) |
| 130 | + { |
| 131 | + log_e( "error in Write"); |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + _dirty = false; |
| 136 | + ret = true; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return ret; |
| 141 | +} |
| 142 | + |
| 143 | +uint8_t * EEPROMClass::getDataPtr() { |
| 144 | + _dirty = true; |
| 145 | + return &_data[0]; |
| 146 | +} |
| 147 | + |
| 148 | +#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) |
| 149 | +EEPROMClass EEPROM; |
| 150 | +#endif |
0 commit comments