Arduino-Storage
External Memory Storage library for Arduino
Vector.ino
Go to the documentation of this file.
1 /*
2  * SPI@8MHz
3  *
4  * sram.SIZE = 131072
5  * vector.MSIZE = 6
6  * vector.NMEMB = 10000
7  * vector.SIZE = 60000
8  *
9  * us/sample = 144
10  * samples/s = 6944
11  * min = 678
12  * max = 680
13  * avg = 679.05
14  */
15 
16 #include "Storage.h"
17 #include "GPIO.h"
18 #include "SPI.h"
19 #include "Driver/MC23LC1024.h"
20 
21 // Configure: SPI bus manager variant
22 // #define USE_SOFTWARE_SPI
23 #define USE_HARDWARE_SPI
24 
25 #if defined(USE_SOFTWARE_SPI)
26 #include "Software/SPI.h"
27 Software::SPI<BOARD::D11, BOARD::D12, BOARD::D13> spi;
28 #elif defined(USE_HARDWARE_SPI)
29 #include "Hardware/SPI.h"
30 Hardware::SPI spi;
31 #endif
32 
33 // External memory storage (128 Kbyte)
35 
36 // Sample with timestamp and value (analog)
37 struct sample_t {
38  uint32_t timestamp;
39  uint16_t value;
40 };
41 
42 // Sample element, number of elements and external storage (60 Kbyte)
43 // Single sample in memory and 10000 vector members on storage
45 const size_t NMEMB = 10000;
46 Storage::Cache vector(sram, &sample, sizeof(sample), NMEMB);
47 
48 void setup()
49 {
50  Serial.begin(57600);
51  while (!Serial);
52 
53  // Print size of storage, vector, sample and number of members
54  Serial.print(F("sram.SIZE = "));
55  Serial.println(sram.SIZE);
56  Serial.print(F("vector.MSIZE = "));
57  Serial.println(vector.MSIZE);
58  Serial.print(F("vector.NMEMB = "));
59  Serial.println(vector.NMEMB);
60  Serial.print(F("vector.SIZE = "));
61  Serial.println(vector.SIZE);
62  Serial.flush();
63 }
64 
65 void loop()
66 {
67  // Sample analog values, timestamp and write to storage
68  for (size_t i = 0; i < vector.NMEMB; i++) {
69  sample.timestamp = micros();
70  sample.value = analogRead(A0);
71  vector.write(i);
72  }
73 
74  // Read back samples and calculate min, max and sum
75  uint16_t min = UINT16_MAX;
76  uint16_t max = 0;
77  uint64_t sum = 0;
78  for (size_t i = 0; i < vector.NMEMB; i++) {
79  vector.read(i);
80  if (sample.value < min) min = sample.value;
81  if (sample.value > max) max = sample.value;
82  sum += sample.value;
83  }
84 
85  // Read back first and last sample timestamp to calculate
86  // average microseconds per sample and write to storage
87  uint32_t us = 0;
88  vector.read(vector.NMEMB - 1);
89  us = sample.timestamp;
90  vector.read(0);
91  us -= sample.timestamp;
92  uint32_t usps = us / vector.NMEMB;
93 
94  // Print the results; samples per second, min, max and average values
95  Serial.println();
96  Serial.print(F("us/sample = "));
97  Serial.println(usps);
98  Serial.print(F("samples/s = "));
99  Serial.println(1000000 / usps);
100  Serial.print(F("min = "));
101  Serial.println(min);
102  Serial.print(F("max = "));
103  Serial.println(max);
104  Serial.print(F("avg = "));
105  Serial.println((float) sum / vector.NMEMB);
106  Serial.flush();
107 
108  delay(5000);
109 }
sample_t sample
Definition: Vector.ino:44
const uint32_t SIZE
Definition: Storage.h:156
MC23LC1024< BOARD::D10 > sram(spi)
void loop()
Definition: Vector.ino:65
Hardware::SPI spi
Definition: Vector.ino:30
void setup()
Definition: Vector.ino:48
const size_t MSIZE
Definition: Storage.h:238
Storage::Cache vector(sram,&sample, sizeof(sample), NMEMB)
int read(size_t ix=0)
Definition: Storage.h:212
const size_t NMEMB
Definition: Storage.h:241
const size_t NMEMB
Definition: Vector.ino:45
int write(size_t ix=0)
Definition: Storage.h:228
uint32_t timestamp
Definition: Vector.ino:38
uint16_t value
Definition: Vector.ino:39