From 84765a16b02425b6fd139051e423bbf6a7bf69a7 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 9 Dec 2014 22:20:30 +0300 Subject: [PATCH] EEPROM library --- libraries/EEPROM/EEPROM.cpp | 101 ++++++++++++++++++++++++++++ libraries/EEPROM/EEPROM.h | 45 +++++++++++++ libraries/EEPROM/keywords.txt | 18 +++++ libraries/EEPROM/library.properties | 8 +++ 4 files changed, 172 insertions(+) create mode 100644 libraries/EEPROM/EEPROM.cpp create mode 100644 libraries/EEPROM/EEPROM.h create mode 100644 libraries/EEPROM/keywords.txt create mode 100644 libraries/EEPROM/library.properties diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp new file mode 100644 index 000000000..29873ea76 --- /dev/null +++ b/libraries/EEPROM/EEPROM.cpp @@ -0,0 +1,101 @@ +/* + EEPROM.cpp - EEPROM library + Copyright (c) 2006 David A. Mellis. All right reserved. + + 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 + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/****************************************************************************** + * Includes + ******************************************************************************/ + + +#include "Arduino.h" +#include "EEPROM.h" + + extern "C" { +#include "c_types.h" +#include "ets_sys.h" +#include "os_type.h" +#include "osapi.h" +#include "spi_flash.h" +} + +#define CONFIG_START_SECTOR 0x3C +#define CONFIG_SECTOR (CONFIG_START_SECTOR + 0) +#define CONFIG_ADDR (SPI_FLASH_SEC_SIZE * CONFIG_SECTOR) + +EEPROMClass::EEPROMClass() +: _data(0), _size(0) +{ +} + +void EEPROMClass::begin(size_t size) +{ + if (size < 0) + return; + if (size > SPI_FLASH_SEC_SIZE) + size = SPI_FLASH_SEC_SIZE; + + _data = new uint8_t[size]; + _size = size; + + spi_flash_read(CONFIG_ADDR, reinterpret_cast(_data), _size); +} + +void EEPROMClass::end() +{ + if (!_size) + return; + + commit(); + + delete[] _data; + _data = 0; + _size = 0; +} + + +uint8_t EEPROMClass::read(int address) +{ + if (address < 0 || address >= _size) + return 0; + + return _data[address]; +} + +void EEPROMClass::write(int address, uint8_t value) +{ + if (address < 0 || address >= _size) + return; + + _data[address] = value; + _dirty = true; +} + +void EEPROMClass::commit() +{ + if (!_size || !_dirty) + return; + + ETS_UART_INTR_DISABLE(); + spi_flash_erase_sector(CONFIG_SECTOR); + spi_flash_write(CONFIG_ADDR, reinterpret_cast(_data), _size); + ETS_UART_INTR_ENABLE(); + _dirty = false; +} + + +EEPROMClass EEPROM; diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h new file mode 100644 index 000000000..b0b69e823 --- /dev/null +++ b/libraries/EEPROM/EEPROM.h @@ -0,0 +1,45 @@ +/* + EEPROM.h - EEPROM library + Copyright (c) 2006 David A. Mellis. All right reserved. + + 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 + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef EEPROM_h +#define EEPROM_h + +#include +#include + +class EEPROMClass +{ + public: + EEPROMClass(); + void begin(size_t size); + uint8_t read(int address); + void write(int address, uint8_t val); + void commit(); + void end(); + + protected: + uint8_t* _data; + size_t _size; + bool _dirty; +}; + +extern EEPROMClass EEPROM; + +#endif + diff --git a/libraries/EEPROM/keywords.txt b/libraries/EEPROM/keywords.txt new file mode 100644 index 000000000..d3218fe2a --- /dev/null +++ b/libraries/EEPROM/keywords.txt @@ -0,0 +1,18 @@ +####################################### +# Syntax Coloring Map For Ultrasound +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +EEPROM KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/libraries/EEPROM/library.properties b/libraries/EEPROM/library.properties new file mode 100644 index 000000000..d1e007085 --- /dev/null +++ b/libraries/EEPROM/library.properties @@ -0,0 +1,8 @@ +name=EEPROM +version=1.0 +author=Arduino +maintainer=Arduino +sentence=Enables reading and writing to the permanent board storage. For all Arduino boards BUT Arduino DUE. +paragraph= +url=http://arduino.cc/en/Reference/EEPROM +architectures=esp8266