Skip to content

Commit 84765a1

Browse files
committed
EEPROM library
1 parent 8de5489 commit 84765a1

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

libraries/EEPROM/EEPROM.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
EEPROM.cpp - EEPROM library
3+
Copyright (c) 2006 David A. Mellis. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
/******************************************************************************
21+
* Includes
22+
******************************************************************************/
23+
24+
25+
#include "Arduino.h"
26+
#include "EEPROM.h"
27+
28+
extern "C" {
29+
#include "c_types.h"
30+
#include "ets_sys.h"
31+
#include "os_type.h"
32+
#include "osapi.h"
33+
#include "spi_flash.h"
34+
}
35+
36+
#define CONFIG_START_SECTOR 0x3C
37+
#define CONFIG_SECTOR (CONFIG_START_SECTOR + 0)
38+
#define CONFIG_ADDR (SPI_FLASH_SEC_SIZE * CONFIG_SECTOR)
39+
40+
EEPROMClass::EEPROMClass()
41+
: _data(0), _size(0)
42+
{
43+
}
44+
45+
void EEPROMClass::begin(size_t size)
46+
{
47+
if (size < 0)
48+
return;
49+
if (size > SPI_FLASH_SEC_SIZE)
50+
size = SPI_FLASH_SEC_SIZE;
51+
52+
_data = new uint8_t[size];
53+
_size = size;
54+
55+
spi_flash_read(CONFIG_ADDR, reinterpret_cast<uint32_t*>(_data), _size);
56+
}
57+
58+
void EEPROMClass::end()
59+
{
60+
if (!_size)
61+
return;
62+
63+
commit();
64+
65+
delete[] _data;
66+
_data = 0;
67+
_size = 0;
68+
}
69+
70+
71+
uint8_t EEPROMClass::read(int address)
72+
{
73+
if (address < 0 || address >= _size)
74+
return 0;
75+
76+
return _data[address];
77+
}
78+
79+
void EEPROMClass::write(int address, uint8_t value)
80+
{
81+
if (address < 0 || address >= _size)
82+
return;
83+
84+
_data[address] = value;
85+
_dirty = true;
86+
}
87+
88+
void EEPROMClass::commit()
89+
{
90+
if (!_size || !_dirty)
91+
return;
92+
93+
ETS_UART_INTR_DISABLE();
94+
spi_flash_erase_sector(CONFIG_SECTOR);
95+
spi_flash_write(CONFIG_ADDR, reinterpret_cast<uint32_t*>(_data), _size);
96+
ETS_UART_INTR_ENABLE();
97+
_dirty = false;
98+
}
99+
100+
101+
EEPROMClass EEPROM;

libraries/EEPROM/EEPROM.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
EEPROM.h - EEPROM library
3+
Copyright (c) 2006 David A. Mellis. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef EEPROM_h
21+
#define EEPROM_h
22+
23+
#include <stddef.h>
24+
#include <stdint.h>
25+
26+
class EEPROMClass
27+
{
28+
public:
29+
EEPROMClass();
30+
void begin(size_t size);
31+
uint8_t read(int address);
32+
void write(int address, uint8_t val);
33+
void commit();
34+
void end();
35+
36+
protected:
37+
uint8_t* _data;
38+
size_t _size;
39+
bool _dirty;
40+
};
41+
42+
extern EEPROMClass EEPROM;
43+
44+
#endif
45+

libraries/EEPROM/keywords.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#######################################
2+
# Syntax Coloring Map For Ultrasound
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
EEPROM KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
#######################################
16+
# Constants (LITERAL1)
17+
#######################################
18+

libraries/EEPROM/library.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name=EEPROM
2+
version=1.0
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Enables reading and writing to the permanent board storage. For all Arduino boards BUT Arduino DUE.
6+
paragraph=
7+
url=http://arduino.cc/en/Reference/EEPROM
8+
architectures=esp8266

0 commit comments

Comments
 (0)