Arduino-Storage
External Memory Storage library for Arduino
Cache.ino
Go to the documentation of this file.
1 #include "Storage.h"
2 #include "TWI.h"
3 #include "Driver/AT24CXX.h"
4 #include "Driver/EEPROM.h"
5 
6 // Configure: Force initialization of eeprom
7 // #define INITIATE_EEPROM
8 
9 // Configure: TWI bus manager
10 #define USE_SOFTWARE_TWI
11 // #define USE_HARDWARE_TWI
12 
13 // Configure: Hardware TWI bus clock frequency
14 #define FREQ 800000UL
15 // #define FREQ 400000UL
16 // #define FREQ 100000UL
17 
18 #if defined(USE_SOFTWARE_TWI)
19 #include "GPIO.h"
20 #include "Software/TWI.h"
21 Software::TWI<BOARD::D18, BOARD::D19> twi;
22 #elif defined(USE_HARDWARE_TWI)
23 #include "Hardware/TWI.h"
24 Hardware::TWI twi(FREQ);
25 #endif
26 
27 // Configure: EEPROM storage
29 // EEPROM eeprom;
30 
31 // Macro to enforce naming convention
32 #define CACHE(v) cache_ ## v
33 
34 // Local data and storage cache block
35 int16_t v[32];
36 Storage::Cache CACHE(v)(eeprom, v, sizeof(v));
37 
38 char w[32];
39 Storage::Cache CACHE(w)(eeprom, w, sizeof(w));
40 
41 // Function to demonstrate dynamic allocation and caching
42 void calc()
43 {
44  // Local cached variable with external storage
45  int16_t u;
46  Storage::Cache CACHE(u)(eeprom, &u, sizeof(u), 16);
47 
48  // Print cached variable; size, local and external memory address
49  Serial.print(F("calc:eeprom.SIZE = "));
50  Serial.print(eeprom.SIZE);
51  Serial.print(F(", addr = "));
52  Serial.println(eeprom.alloc(0));
53 
54  Serial.print(F("calc:CACHE(u).SIZE = "));
55  Serial.print(CACHE(u).SIZE);
56  Serial.print(F(", buf = "));
57  Serial.print((int) &u);
58  Serial.print(F(", addr = "));
59  Serial.println(CACHE(u).addr());
60 
61  // Collect some data and write to external memory
62  for (int i = 0; i < CACHE(u).NMEMB; i++) {
63  u = analogRead(A0);
64  CACHE(u).write(i);
65  }
66  Serial.println();
67 
68  // Read external memory and do some calculation
69  int32_t sum = 0;
70  for (int i = 0; i < CACHE(u).NMEMB; i++) {
71  CACHE(u).read(i);
72  Serial.print(u);
73  Serial.print(' ');
74  sum += u;
75  }
76  Serial.println();
77 
78  // Cached variable is deallocated from storage
79 }
80 
81 void setup()
82 {
83  Serial.begin(57600);
84  while (!Serial);
85 
86  // Initiate vector (or read from eeprom)
87 #if defined(INITIATE_EEPROM)
88  Serial.println(F("setup:init"));
89  for (size_t i = 0; i < sizeof(v) / sizeof(v[0]); i++) {
90  v[i] = i;
91  }
92  strcat(w, "Nisse badar");
93  CACHE(w).write();
94 #else
95  Serial.print(F("setup:CACHE(v).read = "));
96  Serial.println(CACHE(v).read());
97 #endif
98  Serial.print(F("setup:CACHE(w).read = "));
99  Serial.print(CACHE(w).read());
100  Serial.print(F(", \""));
101  Serial.print(w);
102  Serial.println('"');
103  Serial.println();
104 }
105 
106 void loop()
107 {
108  delay(5000);
109 
110  Serial.print(F("loop:eeprom.SIZE = "));
111  Serial.print(eeprom.SIZE);
112  Serial.print(F(", addr = "));
113  Serial.println(eeprom.alloc(0));
114 
115  Serial.print(F("loop:CACHE(v).SIZE = "));
116  Serial.print(CACHE(v).SIZE);
117  Serial.print(F(", buf = "));
118  Serial.print((int) &v);
119  Serial.print(F(", addr = "));
120  Serial.println(CACHE(v).addr());
121 
122  Serial.print(F("loop:CACHE(w).SIZE = "));
123  Serial.print(CACHE(w).SIZE);
124  Serial.print(F(", buf = "));
125  Serial.print((int) &w);
126  Serial.print(F(", addr = "));
127  Serial.println(CACHE(w).addr());
128 
129  // Print contents of vector and increment elements
130  for (size_t i = 0; i < sizeof(v) / sizeof(v[0]); i++) {
131  Serial.print(v[i]);
132  Serial.print(' ');
133  v[i] += 1;
134  }
135  Serial.println();
136 
137  // Write vector to eeprom
138  Serial.print(F("loop:CACHE(v).write = "));
139  Serial.println(CACHE(v).write());
140 
141  // Call function that uses cached data on stack
142  calc();
143 
144  // Clear vector to verify read
145  for (size_t i = 0; i < sizeof(v) / sizeof(v[0]); i++) {
146  v[i] = 0;
147  }
148 
149  // Read vector from eeprom
150  Serial.print(F("loop:CACHE(v).read = "));
151  Serial.println(CACHE(v).read());
152 }
const uint32_t SIZE
Definition: Storage.h:156
void loop()
Definition: Cache.ino:106
Software::TWI< BOARD::D18, BOARD::D19 > twi
Definition: Cache.ino:21
char w[32]
Definition: Cache.ino:38
void setup()
Definition: Cache.ino:81
AT24C32 eeprom(twi)
int16_t v[32]
Definition: Cache.ino:35
#define CACHE(v)
Definition: Cache.ino:32
#define FREQ
Definition: Cache.ino:14
void calc()
Definition: Cache.ino:42