Arduino-Storage
External Memory Storage library for Arduino
Stream.ino
Go to the documentation of this file.
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 Software::SPI<BOARD::D11, BOARD::D12, BOARD::D13> spi;
12 #elif defined(USE_HARDWARE_SPI)
13 #include "Hardware/SPI.h"
14 Hardware::SPI spi;
15 #endif
16 
17 // External memory storage
19 
20 // Storage streams; 10000 bytes each
21 Storage::Stream ios(sram, 10000);
22 Storage::Stream temps(sram, 10000);
23 
24 // Sample size and performance measurements
25 const int N = 1000;
26 int count = 0;
27 uint32_t s0, m0, m1, m2, m3, m4, m5, m6, m7;
28 
29 void setup()
30 {
31  Serial.begin(57600);
32  while (!Serial);
33 
34  // Print size of SRAM and number of bytes available after allocation
35  Serial.print(F("sram.SIZE = "));
36  Serial.println(sram.SIZE);
37  Serial.print(F("sram.room = "));
38  Serial.println(sram.room());
39  Serial.flush();
40 }
41 
42 void loop()
43 {
44  // Print N analog samples to serial stream
45  s0 = micros();
46  for (int i = 0; i < N; i++) {
47  Serial.println(analogRead(A0));
48  }
49  Serial.println();
50  Serial.flush();
51  m0 = micros() - s0;
52 
53  // Print N analog samples to sram stream
54  s0 = micros();
55  for (int i = 0; i < N; i++) {
56  ios.println(analogRead(A0));
57  }
58  ios.println();
59  m1 = micros() - s0;
60 
61  // Transfer data between two sram streams
62  count = ios.available();
63  s0 = micros();
64  while (ios.available())
65  temps.write(ios.read());
66  m2 = micros() - s0;
67 
68  // Print data from sram stream to serial stream
69  s0 = micros();
70  while (temps.available())
71  Serial.write(temps.read());
72  Serial.flush();
73  m3 = micros() - s0;
74 
75  // Write to sram stream
76  s0 = micros();
77  for (int i = 0; i < count; i++) ios.write(i);
78  m4 = micros() - s0;
79 
80  // Read from sram stream
81  s0 = micros();
82  uint16_t sum = 0;
83  for (int i = 0; i < count; i++) sum += ios.read();
84  m5 = micros() - s0;
85 
86  // Write N analog samples to sram stream
87  s0 = micros();
88  int sample;
89  for (int i = 0; i < N; i++) {
90  sample = analogRead(A0);
91  ios.write((const uint8_t*) &sample, sizeof(sample));
92  }
93  m6 = micros() - s0;
94 
95  // Read N analog samples from sram stream and print to serial
96  s0 = micros();
97  while (ios.available()) {
98  ios.readBytes((uint8_t*) &sample, sizeof(sample));
99  Serial.println(sample);
100  }
101  Serial.flush();
102  m7 = micros() - s0;
103 
104  // Print the results
105  Serial.print(F("ios.addr = "));
106  Serial.println(ios.addr());
107  Serial.print(F("temps.addr = "));
108  Serial.println(temps.addr());
109  Serial.print(F("sram.addr = "));
110  Serial.println(sram.alloc(0));
111  Serial.print(F("Samples, N = "));
112  Serial.println(N);
113  Serial.print(F("Serial.print, m0 = "));
114  Serial.println(m0 / N);
115  Serial.print(F("SRAM::Stream.print, m1 = "));
116  Serial.println(m1 / N);
117  Serial.println();
118  Serial.print(F("SRAM::Stream.available, count = "));
119  Serial.println(count);
120  Serial.print(F("SRAM::Stream.write/read, m2 = "));
121  Serial.println(m2 / count);
122  Serial.print(F("SRAM::Stream.read/Serial.write, m3 = "));
123  Serial.println(m3 / count);
124  Serial.print(F("SRAM::Stream.write, m4 = "));
125  Serial.println(m4 / count);
126  Serial.print(F("SRAM::Stream.read, m5 = "));
127  Serial.println(m5 / count);
128  Serial.println();
129  Serial.print(F("SRAM::Stream.available, N*2 = "));
130  Serial.println(N * 2);
131  Serial.print(F("SRAM::Stream.write(2), m6 = "));
132  Serial.println(m6 / N);
133  Serial.print(F("SRAM::Stream.read(2)/Serial.print, m7 = "));
134  Serial.println(m7 / N);
135  Serial.println();
136 
137  delay(1000);
138 }
sample_t sample
Definition: Vector.ino:44
uint32_t m1
Definition: Stream.ino:27
uint32_t m6
Definition: Stream.ino:27
void loop()
Definition: Stream.ino:42
Storage::Stream temps(sram, 10000)
uint32_t m7
Definition: Stream.ino:27
virtual int read()
Definition: Storage.h:354
uint32_t m5
Definition: Stream.ino:27
virtual int available()
Definition: Storage.h:329
Hardware::SPI spi
Definition: Stream.ino:14
uint32_t m4
Definition: Stream.ino:27
Storage::Stream ios(sram, 10000)
uint32_t s0
Definition: Stream.ino:27
uint32_t m2
Definition: Stream.ino:27
virtual size_t write(uint8_t byte)
Definition: Storage.h:286
MC23LC1024< BOARD::D10 > sram(spi)
void setup()
Definition: Stream.ino:29
virtual void flush()
Definition: Storage.h:369
uint32_t m0
Definition: Stream.ino:27
uint32_t m3
Definition: Stream.ino:27
uint32_t addr()
Definition: Storage.h:274
const int N
Definition: Stream.ino:25
int count
Definition: Stream.ino:26