Arduino-Storage
External Memory Storage library for Arduino
AT24CXX.ino
Go to the documentation of this file.
1 /* Results:
2  * ----------------------------------------
3  * TWI@100kHz
4  * ----------------------------------------
5  * write(N, us, us/byte, kbyte/s)
6  * 1, 408, 408.00, 2.45
7  * 10, 1236, 123.60, 8.09
8  * 100, 20540, 205.40, 4.87
9  * 1000, 206280, 206.28, 4.85
10  *
11  * read(N, us, us/byte, kbyte/s)
12  * 1, 528, 528.00, 1.89
13  * 10, 1352, 135.20, 7.40
14  * 100, 9680, 96.80, 10.33
15  * 1000, 93128, 93.13, 10.74
16  *
17  * ----------------------------------------
18  * TWI@400kHz
19  * ----------------------------------------
20  * write(N, us, us/byte, kbyte/s)
21  * 1, 132, 132.00, 7.58
22  * 10, 352, 35.20, 28.41
23  * 100, 12288, 122.88, 8.14
24  * 1000, 125164, 125.16, 7.99
25  *
26  * read(N, us, us/byte, kbyte/s)
27  * 1, 168, 168.00, 5.95
28  * 10, 392, 39.20, 25.51
29  * 100, 2640, 26.40, 37.88
30  * 1000, 25152, 25.15, 39.76
31  *
32  * ----------------------------------------
33  * TWI@800kHz
34  * ----------------------------------------
35  * write(N, us, us/byte, kbyte/s)
36  * 1, 88, 88.00, 11.36
37  * 10, 204, 20.40, 49.02
38  * 100, 10916, 109.16, 9.16
39  * 1000, 112656, 112.66, 8.88
40  *
41  * read(N, us, us/byte, kbyte/s)
42  * 1, 104, 104.00, 9.62
43  * 10, 228, 22.80, 43.86
44  * 100, 1468, 14.68, 68.12
45  * 1000, 13916, 13.92, 71.86
46  */
47 
48 #include "Storage.h"
49 #include "TWI.h"
50 #include "Driver/AT24CXX.h"
51 
52 // Configure: TWI bus manager variant
53 // #define USE_SOFTWARE_TWI
54 #define USE_HARDWARE_TWI
55 
56 // Configure: Hardware TWI bus clock frequency
57 // #define FREQ 800000UL
58 #define FREQ 400000UL
59 // #define FREQ 100000UL
60 
61 #if defined(USE_SOFTWARE_TWI)
62 #include "GPIO.h"
63 #include "Software/TWI.h"
64 Software::TWI<BOARD::D18, BOARD::D19> twi;
65 #elif defined(USE_HARDWARE_TWI)
66 #include "Hardware/TWI.h"
67 Hardware::TWI twi(FREQ);
68 #endif
69 
70 // EEPROM external storage
72 
73 // Local memory buffer
74 const int BUF_MAX = 1000;
75 uint8_t buf[BUF_MAX];
76 
77 // Buffer sizes
78 const int N_MAX = 7;
79 const unsigned int N[N_MAX] = {
80  1, 10,
82  100, 1000
83 };
84 
85 void setup()
86 {
87  Serial.begin(57600);
88  while (!Serial);
89 }
90 
91 void loop()
92 {
93  static uint8_t n = 0;
94  uint32_t start;
95  uint32_t us;
96  float uspb;
97 
98  for (int i = 0; i < BUF_MAX; i++) buf[i] = n + i;
99 
100  Serial.println();
101  Serial.print(F("twi.FREQ(kHz) = "));
102  Serial.println(FREQ / 1000);
103  Serial.print(F("eeprom.SIZE(kbyte) = "));
104  Serial.println(eeprom.SIZE / 1024);
105  Serial.println();
106 
107  // Benchmark#1: Measure write with increasing buffer size
108  Serial.println(F("write(N, us, us/byte, kbyte/s)"));
109  Serial.flush();
110  for (int i = 0; i < N_MAX; i++) {
111  start = micros();
112  eeprom.write(0x0000, buf, N[i]);
113  us = micros() - start;
114  uspb = ((float) us) / N[i];
115  Serial.print(N[i]);
116  Serial.print(F(", "));
117  Serial.print(us);
118  Serial.print(F(", "));
119  Serial.print(uspb);
120  Serial.print(F(", "));
121  Serial.println(1000 / uspb);
122  Serial.flush();
123  }
124  Serial.println();
125 
126  for (int i = 0; i < BUF_MAX; i++) buf[i] = 0;
127 
128  // Benchmark#2: Measure read with increasing buffer size
129  Serial.println(F("read(N, us, us/byte, kbyte/s)"));
130  Serial.flush();
131  for (int i = 0; i < N_MAX; i++) {
132  start = micros();
133  eeprom.read(buf, 0x0000, N[i]);
134  us = micros() - start;
135  uspb = ((float) us) / N[i];
136  Serial.print(N[i]);
137  Serial.print(F(", "));
138  Serial.print(us);
139  Serial.print(F(", "));
140  Serial.print(uspb);
141  Serial.print(F(", "));
142  Serial.println(1000 / uspb);
143  Serial.flush();
144  }
145 
146  for (int i = 0; i < BUF_MAX; i++) {
147  if (buf[i] != ((n + i) & 0xff)) {
148  Serial.print(i);
149  Serial.println(F(": verification error"));
150  break;
151  }
152  }
153  n += 1;
154 
155  delay(5000);
156 }
const uint32_t SIZE
Definition: Storage.h:30
uint8_t buf[BUF_MAX]
Definition: AT24CXX.ino:75
const uint16_t PAGE_MAX
Definition: AT24CXX.h:55
#define FREQ
Definition: AT24CXX.ino:58
void loop()
Definition: AT24CXX.ino:91
virtual int read(void *dst, uint32_t src, size_t count)
Definition: AT24CXX.h:79
virtual int write(uint32_t dst, const void *src, size_t count)
Definition: AT24CXX.h:106
AT24C32 eeprom(twi)
const unsigned int N[N_MAX]
Definition: AT24CXX.ino:79
void setup()
Definition: AT24CXX.ino:85
const int BUF_MAX
Definition: AT24CXX.ino:74
const int N_MAX
Definition: AT24CXX.ino:78
Hardware::TWI twi(FREQ)