mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
EEPROM library
This commit is contained in:
parent
8de54890c2
commit
84765a16b0
101
libraries/EEPROM/EEPROM.cpp
Normal file
101
libraries/EEPROM/EEPROM.cpp
Normal file
@ -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<uint32_t*>(_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<uint32_t*>(_data), _size);
|
||||||
|
ETS_UART_INTR_ENABLE();
|
||||||
|
_dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
EEPROMClass EEPROM;
|
45
libraries/EEPROM/EEPROM.h
Normal file
45
libraries/EEPROM/EEPROM.h
Normal file
@ -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 <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
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
|
||||||
|
|
18
libraries/EEPROM/keywords.txt
Normal file
18
libraries/EEPROM/keywords.txt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#######################################
|
||||||
|
# Syntax Coloring Map For Ultrasound
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Datatypes (KEYWORD1)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
EEPROM KEYWORD1
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Methods and Functions (KEYWORD2)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Constants (LITERAL1)
|
||||||
|
#######################################
|
||||||
|
|
8
libraries/EEPROM/library.properties
Normal file
8
libraries/EEPROM/library.properties
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
name=EEPROM
|
||||||
|
version=1.0
|
||||||
|
author=Arduino
|
||||||
|
maintainer=Arduino <info@arduino.cc>
|
||||||
|
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
|
Loading…
x
Reference in New Issue
Block a user