Skip to content

Commit d0b17af

Browse files
committed
Initial commit
0 parents  commit d0b17af

File tree

11 files changed

+3355
-0
lines changed

11 files changed

+3355
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*~
2+
doc

Doxyfile

Lines changed: 2436 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Arduino-Storage
2+
3+
The External Memory Storage library for Arduino is designed to
4+
abstract handling of external memory, and allow block read/write and
5+
streaming of data. The library includes device drivers for SPI SRAM
6+
(23LC1024) and 2-Wire EEPROM (AT24CXX).
7+
8+
Version: 1.0
9+
10+
## Classes
11+
12+
* [Abstract Storage Manager, Storage](./src/Storage.h)
13+
* [Storage Block, Storage::Block](./src/Storage.h)
14+
* [Storage Stream, Storage::Stream](./src/Storage.h)
15+
16+
## Drivers
17+
18+
* [1 Mbit Serial SRAM, 23LC1024](./src/Driver/MC23LC1024.h)
19+
* [2-Wire EEPROM, AT24CXX](./src/Driver/AT24CXX.h)
20+
21+
## Example Sketches
22+
23+
* [Block](./examples/Block)
24+
* [Storage](./examples/Storage)
25+
* [Stream](./examples/Stream)
26+
27+
## Dependencies
28+
29+
* [General Purpose Input/Output library for Arduino](https://github.com/mikaelpatel/Arduino-GPIO)
30+
* [I2C Bus Manager and Device Driver Support](https://github.com/mikaelpatel/Arduino-TWI)
31+
* [Serial Peripheral Interface Bus Manager and Device Driver Support](https://github.com/mikaelpatel/Arduino-SPI)

examples/Block/Block.ino

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "TWI.h"
2+
#include "Storage.h"
3+
#include "Driver/AT24CXX.h"
4+
5+
#define USE_SOFTWARE_TWI
6+
// #define USE_HARDWARE_TWI
7+
8+
#if defined(USE_SOFTWARE_TWI)
9+
#include "GPIO.h"
10+
#include "Software/TWI.h"
11+
#if defined(ARDUINO_attiny)
12+
#include "Software/Serial.h"
13+
Software::Serial<BOARD::D0> Serial;
14+
Software::TWI<BOARD::D1, BOARD::D2> twi;
15+
#else
16+
Software::TWI<BOARD::D6, BOARD::D7> twi;
17+
#endif
18+
#elif defined(USE_HARDWARE_TWI)
19+
#include "Hardware/TWI.h"
20+
Hardware::TWI twi;
21+
#endif
22+
23+
// EEPROM external storage
24+
AT24C32 eeprom(twi);
25+
26+
// Address of data on storage
27+
const uint32_t v_addr = eeprom.PAGE_MAX / 2;
28+
29+
// Local data
30+
int16_t v[32];
31+
32+
// Block on storage for local data
33+
Storage::Block v_eeprom(eeprom, v_addr, v, sizeof(v));
34+
35+
void setup()
36+
{
37+
Serial.begin(57600);
38+
while (!Serial);
39+
40+
// Initiate vector (or read from eeprom)
41+
#if 0
42+
Serial.println(F("setup:init"));
43+
for (size_t i = 0; i < sizeof(v) / sizeof(v[0]); i++) {
44+
v[i] = i;
45+
}
46+
#else
47+
Serial.print(F("setup:read: "));
48+
Serial.println(v_eeprom.read());
49+
#endif
50+
}
51+
52+
void loop()
53+
{
54+
// Print contents of vector and increment elements
55+
for (size_t i = 0; i < sizeof(v) / sizeof(v[0]); i++) {
56+
Serial.print(v[i]);
57+
Serial.print(' ');
58+
v[i] += 1;
59+
}
60+
Serial.println();
61+
delay(2000);
62+
63+
// Write vector to eeprom
64+
Serial.print(F("loop:write: "));
65+
Serial.println(v_eeprom.write());
66+
67+
// Clear vector to verify read
68+
for (size_t i = 0; i < sizeof(v) / sizeof(v[0]); i++) {
69+
v[i] = 0;
70+
}
71+
72+
// Read vector from eeprom
73+
Serial.print(F("loop:read: "));
74+
Serial.println(v_eeprom.read());
75+
}

examples/Storage/Storage.ino

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "Storage.h"
2+
#include "GPIO.h"
3+
#include "SPI.h"
4+
#include "Driver/MC23LC1024.h"
5+
6+
// #define USE_SOFTWARE_SPI
7+
#define USE_HARDWARE_SPI
8+
9+
#if defined(USE_SOFTWARE_SPI)
10+
#include "Software/SPI.h"
11+
#if defined(ARDUINO_attiny)
12+
Software::SPI<BOARD::D1, BOARD::D2, BOARD::D3> spi;
13+
#else
14+
Software::SPI<BOARD::D11, BOARD::D12, BOARD::D13> spi;
15+
#endif
16+
#elif defined(USE_HARDWARE_SPI)
17+
#include "Hardware/SPI.h"
18+
Hardware::SPI spi;
19+
#endif
20+
21+
// External memory device
22+
MC23LC1024<BOARD::D10> sram(spi);
23+
24+
// Local memory buffer
25+
const size_t BUF_MAX = 1000;
26+
static uint8_t buf[BUF_MAX];
27+
28+
void setup()
29+
{
30+
Serial.begin(57600);
31+
while (!Serial);
32+
33+
for (size_t i = 0; i < BUF_MAX; i++) buf[i] = i;
34+
}
35+
36+
void loop()
37+
{
38+
// SRAM performance: N is number of bytes
39+
// write: 9.875 + N*1.4275 us, max. 700 kbyte/s
40+
// read: 9.875 + N*1.5 us, max. 670 kbyte/s
41+
42+
// 9.875/9.875 us, protocol overhead
43+
sram.write(0x0000, buf, 0);
44+
sram.read(buf, 0x0000, 0);
45+
46+
// 11.56/11.62 us/byte, 90 kbyte/s, 1 byte
47+
sram.write(0x0001, buf, 1);
48+
sram.read(buf, 0x0001, 1);
49+
50+
// 2.45/2.49 us/byte, 410 kbyte/s, 10 byte
51+
sram.write(0x0010, buf, 10);
52+
sram.read(buf, 0x0010, 10);
53+
54+
// 1.539/1.599 us/byte, 650 kbyte/s, 100 byte
55+
sram.write(0x0100, buf, 100);
56+
sram.read(buf, 0x00100, 100);
57+
58+
// 1.454/1.522 us/byte, 690 kbyte/s, 1000 byte
59+
sram.write(0x1000, buf, sizeof(buf));
60+
sram.read(buf, 0x1000, sizeof(buf));
61+
62+
// Print buffer and increment values
63+
for (size_t i = 0; i < BUF_MAX; i++) {
64+
Serial.print(buf[i]);
65+
Serial.print(' ');
66+
buf[i] = buf[i] + 1;
67+
}
68+
Serial.println();
69+
delay(500);
70+
}

examples/Stream/Stream.ino

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include "Storage.h"
2+
#include "GPIO.h"
3+
#include "SPI.h"
4+
#include "Driver/MC23LC1024.h"
5+
6+
// #define USE_SOFTWARE_SPI
7+
#define USE_HARDWARE_SPI
8+
9+
#if defined(USE_SOFTWARE_SPI)
10+
#include "Software/SPI.h"
11+
#if defined(ARDUINO_attiny)
12+
Software::SPI<BOARD::D1, BOARD::D2, BOARD::D3> spi;
13+
#else
14+
Software::SPI<BOARD::D11, BOARD::D12, BOARD::D13> spi;
15+
#endif
16+
#elif defined(USE_HARDWARE_SPI)
17+
#include "Hardware/SPI.h"
18+
Hardware::SPI spi;
19+
#endif
20+
21+
// External memory storage
22+
MC23LC1024<BOARD::D10> sram(spi);
23+
24+
// Storage streams; address = 0 and 10000, size = 10000 each
25+
Storage::Stream<10000> ios(sram);
26+
Storage::Stream<10000> temps(sram, 10000);
27+
28+
// Sample size and performance measurements
29+
const int N = 1000;
30+
int count = 0;
31+
uint32_t s0, m0, m1, m2, m3, m4, m5, m6, m7;
32+
33+
void setup()
34+
{
35+
Serial.begin(57600);
36+
while (!Serial);
37+
}
38+
39+
void loop()
40+
{
41+
// Print N analog samples to serial stream
42+
s0 = micros();
43+
for (int i = 0; i < N; i++) {
44+
Serial.println(analogRead(A0));
45+
}
46+
Serial.println();
47+
Serial.flush();
48+
m0 = micros() - s0;
49+
50+
// Print N analog samples to sram stream
51+
s0 = micros();
52+
for (int i = 0; i < N; i++) {
53+
ios.println(analogRead(A0));
54+
}
55+
ios.println();
56+
m1 = micros() - s0;
57+
58+
// Transfer data between two sram streams
59+
count = ios.available();
60+
s0 = micros();
61+
while (ios.available())
62+
temps.write(ios.read());
63+
m2 = micros() - s0;
64+
65+
// Print data from sram stream to serial stream
66+
s0 = micros();
67+
while (temps.available())
68+
Serial.write(temps.read());
69+
Serial.flush();
70+
m3 = micros() - s0;
71+
72+
// Write to sram stream
73+
s0 = micros();
74+
for (int i = 0; i < count; i++) ios.write(i);
75+
m4 = micros() - s0;
76+
77+
// Read from sram stream
78+
s0 = micros();
79+
uint16_t sum = 0;
80+
for (int i = 0; i < count; i++) sum += ios.read();
81+
m5 = micros() - s0;
82+
83+
// Write N analog samples to sram stream
84+
s0 = micros();
85+
int sample;
86+
for (int i = 0; i < N; i++) {
87+
sample = analogRead(A0);
88+
ios.write((const uint8_t*) &sample, sizeof(sample));
89+
}
90+
m6 = micros() - s0;
91+
92+
// Read N analog samples from sram stream and print to serial
93+
s0 = micros();
94+
while (ios.available()) {
95+
ios.readBytes((uint8_t*) &sample, sizeof(sample));
96+
Serial.println(sample);
97+
}
98+
Serial.flush();
99+
m7 = micros() - s0;
100+
101+
Serial.print(F("Samples, N = "));
102+
Serial.println(N);
103+
Serial.print(F("Serial.print, m0 = "));
104+
Serial.println(m0 / N);
105+
Serial.print(F("SRAM::Stream.print, m1 = "));
106+
Serial.println(m1 / N);
107+
Serial.println();
108+
Serial.print(F("SRAM::Stream.available, count = "));
109+
Serial.println(count);
110+
Serial.print(F("SRAM::Stream.write/read, m2 = "));
111+
Serial.println(m2 / count);
112+
Serial.print(F("SRAM::Stream.read/Serial.write, m3 = "));
113+
Serial.println(m3 / count);
114+
Serial.print(F("SRAM::Stream.write, m4 = "));
115+
Serial.println(m4 / count);
116+
Serial.print(F("SRAM::Stream.read, m5 = "));
117+
Serial.println(m5 / count);
118+
Serial.println();
119+
Serial.print(F("SRAM::Stream.available, N*2 = "));
120+
Serial.println(N * 2);
121+
Serial.print(F("SRAM::Stream.write(2), m6 = "));
122+
Serial.println(m6 / N);
123+
Serial.print(F("SRAM::Stream.read(2)/Serial.print, m7 = "));
124+
Serial.println(m7 / N);
125+
Serial.println();
126+
127+
delay(1000);
128+
}

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Arduino-Storage
2+
version=1.0
3+
author=Mikael Patel
4+
maintainer=Mikael Patel <[email protected]>
5+
sentence=External Memory Storage library for Arduino.
6+
paragraph=This library is designed to abstract handling of external memory and allow block read/write and streaming of data. The library includes device drivers for SPI SRAM (23LC1024) and 2-Wire EEPROM (AT24CXX).
7+
category=Data Storage
8+
url=https://github.com/mikaelpatel/Arduino-Storage
9+
architectures=avr

mainpage.dox

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/** @mainpage Arduino-Storage
2+
3+
The External Memory Storage library for Arduino is designed to
4+
abstract handling of external memory, and allow block read/write
5+
(Storage::Block) and streaming of data (Storage::Stream). The library
6+
includes device drivers for SPI SRAM (MC23LC1024) and 2-Wire EEPROM
7+
(AT24CXX).
8+
9+
Version: 1.0
10+
*/
11+
12+
/** @page License
13+
14+
Copyright (C) 2017, Mikael Patel
15+
16+
This library is free software; you can redistribute it and/or
17+
modify it under the terms of the GNU Lesser General Public
18+
License as published by the Free Software Foundation; either
19+
version 2.1 of the License, or (at your option) any later version.
20+
21+
This library is distributed in the hope that it will be useful,
22+
but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24+
Lesser General Public License for more details.
25+
26+
You should have received a copy of the GNU Lesser General
27+
Public License along with this library; if not, write to the
28+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
29+
Boston, MA 02111-1307 USA
30+
*/

0 commit comments

Comments
 (0)