|
| 1 | +/* |
| 2 | + EEPROM.cpp - ASR650x EEPROM emulation |
| 3 | +
|
| 4 | + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. |
| 5 | + This file is part of the esp8266 core for Arduino environment. |
| 6 | +
|
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +*/ |
| 21 | + |
| 22 | +/* |
| 23 | + * note that the chip has 1K user flash.; |
| 24 | + * the size of user flash row is 256; |
| 25 | + * user flash row 0-2 can be edited; |
| 26 | + * user flash row 3 is reservated, must not be edited; |
| 27 | + * |
| 28 | + * CY_FLASH_SIZEOF_ROW is 256 , CY_SFLASH_USERBASE is 0x0ffff400 |
| 29 | + * |
| 30 | + */ |
| 31 | + |
| 32 | +#include "Arduino.h" |
| 33 | +#include "EEPROM.h" |
| 34 | +#include "debug.h" |
| 35 | + |
| 36 | +#ifdef DEBUG_ASR_CORE |
| 37 | +#define DEBUGV(fmt, ...) ::printf((PGM_P)PSTR(fmt), ## __VA_ARGS__) |
| 38 | +#endif |
| 39 | + |
| 40 | +#ifndef DEBUGV |
| 41 | +#define DEBUGV(...) do { (void)0; } while (0) |
| 42 | +#endif |
| 43 | + |
| 44 | +#define _EEPROM_SIZE (CY_FLASH_SIZEOF_ROW * 3) |
| 45 | + |
| 46 | +EEPROMClass::EEPROMClass(uint32_t baddr) |
| 47 | +: _baddr(baddr) |
| 48 | +, _data(0) |
| 49 | +, _size(0) |
| 50 | +, _dirty(false) |
| 51 | +{ |
| 52 | +} |
| 53 | + |
| 54 | +EEPROMClass::EEPROMClass(void) |
| 55 | +: _baddr(CY_SFLASH_USERBASE) |
| 56 | +, _data(0) |
| 57 | +, _size(0) |
| 58 | +, _dirty(false) |
| 59 | +{ |
| 60 | +} |
| 61 | + |
| 62 | +void EEPROMClass::begin(size_t size) { |
| 63 | + if (size <= 0) { |
| 64 | + DEBUGV("EEPROMClass::begin error, size == 0\n"); |
| 65 | + return; |
| 66 | + } |
| 67 | + if (size > _EEPROM_SIZE) { |
| 68 | + DEBUGV("EEPROMClass::begin error, %d > %d\n", size, _EEPROM_SIZE); |
| 69 | + size = _EEPROM_SIZE; |
| 70 | + } |
| 71 | + |
| 72 | + size = (size + 3) & (~3); |
| 73 | + |
| 74 | + //In case begin() is called a 2nd+ time, don't reallocate if size is the same |
| 75 | + if(_data && size != _size) { |
| 76 | + delete[] _data; |
| 77 | + _data = new uint8_t[size]; |
| 78 | + } else if(!_data) { |
| 79 | + _data = new uint8_t[size]; |
| 80 | + } |
| 81 | + |
| 82 | + _size = size; |
| 83 | + |
| 84 | + if (FLASH_read_at(_baddr, reinterpret_cast<uint8_t *>(_data), _size) == -1) { |
| 85 | + DEBUGV("EEPROMClass::begin flash read failed\n"); |
| 86 | + } |
| 87 | + |
| 88 | + _dirty = false; //make sure dirty is cleared in case begin() is called 2nd+ time |
| 89 | +} |
| 90 | + |
| 91 | +void EEPROMClass::end() { |
| 92 | + if (!_size) |
| 93 | + return; |
| 94 | + |
| 95 | + commit(); |
| 96 | + if(_data) { |
| 97 | + delete[] _data; |
| 98 | + } |
| 99 | + _data = 0; |
| 100 | + _size = 0; |
| 101 | + _dirty = false; |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +uint8_t EEPROMClass::read(int const address) { |
| 106 | + if (address < 0 || (size_t)address >= _size) { |
| 107 | + DEBUGV("EEPROMClass::read error, address %d > %d or %d < 0\n", address, _size, address); |
| 108 | + return 0; |
| 109 | + } |
| 110 | + if (!_data) { |
| 111 | + DEBUGV("EEPROMClass::read without ::begin\n"); |
| 112 | + return 0; |
| 113 | + } |
| 114 | + |
| 115 | + return _data[address]; |
| 116 | +} |
| 117 | + |
| 118 | +void EEPROMClass::write(int const address, uint8_t const value) { |
| 119 | + if (address < 0 || (size_t)address >= _size) { |
| 120 | + DEBUGV("EEPROMClass::write error, address %d > %d or %d < 0\n", address, _size, address); |
| 121 | + return; |
| 122 | + } |
| 123 | + if(!_data) { |
| 124 | + DEBUGV("EEPROMClass::read without ::begin\n"); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + // Optimise _dirty. Only flagged if data written is different. |
| 129 | + uint8_t* pData = &_data[address]; |
| 130 | + if (*pData != value) |
| 131 | + { |
| 132 | + *pData = value; |
| 133 | + _dirty = true; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +bool EEPROMClass::commit() { |
| 138 | + if (!_size) |
| 139 | + return false; |
| 140 | + if(!_dirty) |
| 141 | + return true; |
| 142 | + if(!_data) |
| 143 | + return false; |
| 144 | + |
| 145 | + if (FLASH_update(_baddr, reinterpret_cast<const void *>(_data), _size) == 0) { |
| 146 | + _dirty = false; |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + DEBUGV("EEPROMClass::commit failed\n"); |
| 151 | + return false; |
| 152 | +} |
| 153 | + |
| 154 | +uint8_t * EEPROMClass::getDataPtr() { |
| 155 | + _dirty = true; |
| 156 | + return &_data[0]; |
| 157 | +} |
| 158 | + |
| 159 | +uint8_t const * EEPROMClass::getConstDataPtr() const { |
| 160 | + return &_data[0]; |
| 161 | +} |
| 162 | + |
| 163 | +#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM) |
| 164 | +EEPROMClass EEPROM; |
| 165 | +#endif |
| 166 | + |
0 commit comments