Skip to content

Commit 684978b

Browse files
committed
Improved benchmarks and example sketches
1 parent a8d61c1 commit 684978b

File tree

7 files changed

+365
-128
lines changed

7 files changed

+365
-128
lines changed

examples/Benchmarks/AT24CXX/AT24CXX.ino

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* Results:
22
* ----------------------------------------
3-
* twi@100kHz
3+
* TWI@100kHz
44
* ----------------------------------------
55
* write(N, us, us/byte, kbyte/s)
66
* 1, 408, 408.00, 2.45
@@ -15,7 +15,7 @@
1515
* 1000, 93128, 93.13, 10.74
1616
*
1717
* ----------------------------------------
18-
* twi@400kHz
18+
* TWI@400kHz
1919
* ----------------------------------------
2020
* write(N, us, us/byte, kbyte/s)
2121
* 1, 132, 132.00, 7.58
@@ -30,7 +30,7 @@
3030
* 1000, 25152, 25.15, 39.76
3131
*
3232
* ----------------------------------------
33-
* twi@800kHz
33+
* TWI@800kHz
3434
* ----------------------------------------
3535
* write(N, us, us/byte, kbyte/s)
3636
* 1, 88, 88.00, 11.36
@@ -53,17 +53,17 @@
5353
// #define USE_SOFTWARE_TWI
5454
#define USE_HARDWARE_TWI
5555

56+
// Configure: Hardware TWI bus clock frequency
57+
#define FREQ 800000UL
58+
// #define FREQ 400000UL
59+
// #define FREQ 100000UL
60+
5661
#if defined(USE_SOFTWARE_TWI)
5762
#include "GPIO.h"
5863
#include "Software/TWI.h"
59-
Software::TWI<BOARD::D6, BOARD::D7> twi;
64+
Software::TWI<BOARD::D18, BOARD::D19> twi;
6065
#elif defined(USE_HARDWARE_TWI)
6166
#include "Hardware/TWI.h"
62-
// Configure: Hardware TWI bus clock frequency
63-
//
64-
#define FREQ 800000UL
65-
// #define FREQ 400000UL
66-
// #define FREQ 100000UL
6767
Hardware::TWI twi(FREQ);
6868
#endif
6969

@@ -75,8 +75,12 @@ const int BUF_MAX = 1000;
7575
uint8_t buf[BUF_MAX];
7676

7777
// Buffer sizes
78-
const int N_MAX = 6;
79-
const int N[N_MAX] = { 1, 10, eeprom.PAGE_MAX, eeprom.PAGE_MAX*2, 100, 1000 };
78+
const int N_MAX = 7;
79+
const unsigned int N[N_MAX] = {
80+
1, 10,
81+
eeprom.PAGE_MAX, 2*eeprom.PAGE_MAX, 3*eeprom.PAGE_MAX,
82+
100, 1000
83+
};
8084

8185
void setup()
8286
{

examples/Benchmarks/EEPROM/EEPROM.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* eeprom.SIZE: 1024
2+
* eeprom.SIZE(bytes): 1024
33
*
44
* write(N, us, us/byte, kbyte/s)
55
* 1, 4, 4.00, 250.00
@@ -35,7 +35,7 @@ void loop()
3535
static uint8_t n = 0;
3636
for (int i = 0; i < BUF_MAX; i++) buf[i] = n + i;
3737

38-
Serial.print(F("eeprom.SIZE: "));
38+
Serial.print(F("eeprom.SIZE(bytes): "));
3939
Serial.println(eeprom.SIZE);
4040
Serial.println();
4141

examples/Block/Block.ino

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,68 @@
1-
#include "TWI.h"
21
#include "Storage.h"
3-
#include "Driver/EEPROM.h"
2+
#include "TWI.h"
43
#include "Driver/AT24CXX.h"
4+
#include "Driver/EEPROM.h"
55

6+
// Configure: TWI bus manager
67
#define USE_SOFTWARE_TWI
78
// #define USE_HARDWARE_TWI
89

10+
// Configure: Hardware TWI bus clock frequency
11+
// #define FREQ 800000UL
12+
#define FREQ 400000UL
13+
// #define FREQ 100000UL
14+
915
#if defined(USE_SOFTWARE_TWI)
1016
#include "GPIO.h"
1117
#include "Software/TWI.h"
12-
Software::TWI<BOARD::D6, BOARD::D7> twi;
18+
Software::TWI<BOARD::D18, BOARD::D19> twi;
1319
#elif defined(USE_HARDWARE_TWI)
1420
#include "Hardware/TWI.h"
15-
Hardware::TWI twi;
21+
Hardware::TWI twi(FREQ);
1622
#endif
1723

18-
// EEPROM external storage
24+
// Configure: EEPROM storage
1925
AT24C32 eeprom(twi);
2026
// EEPROM eeprom;
2127

22-
// Local data and storage block
23-
int16_t v[32];
24-
Storage::Block v_eeprom(eeprom, v, sizeof(v));
25-
26-
char w[32];
27-
Storage::Block w_eeprom(eeprom, w, sizeof(w));
28+
const uint32_t BLOCK_MAX = 16*eeprom.PAGE_MAX;
29+
Storage::Block block(eeprom, BLOCK_MAX);
2830

2931
void setup()
3032
{
3133
Serial.begin(57600);
3234
while (!Serial);
3335

34-
Serial.print(F("setup:v_eeprom.addr: "));
35-
Serial.println(v_eeprom.addr());
36-
37-
Serial.print(F("setup:w_eeprom.addr: "));
38-
Serial.println(w_eeprom.addr());
39-
40-
Serial.print(F("setup:eeprom.addr: "));
41-
Serial.println(eeprom.alloc(0));
42-
43-
// Initiate vector (or read from eeprom)
44-
#if 0
45-
Serial.println(F("setup:init"));
46-
for (size_t i = 0; i < sizeof(v) / sizeof(v[0]); i++) {
47-
v[i] = i;
36+
// Initiate eeprom; fill block with zero
37+
uint8_t page[eeprom.PAGE_MAX];
38+
memset(page, 0, sizeof(page));
39+
for (uint32_t offset = 0; offset < BLOCK_MAX; offset += eeprom.PAGE_MAX) {
40+
block.write(offset, page, sizeof(page));
4841
}
49-
strcat(w, "Nisse badar");
50-
w_eeprom.write();
51-
#else
52-
Serial.print(F("setup:read: "));
53-
Serial.println(v_eeprom.read());
54-
#endif
55-
Serial.print(F("setup:w_eeprom.addr: "));
56-
Serial.print(w_eeprom.read());
57-
Serial.print(F(", \""));
58-
Serial.print(w);
59-
Serial.println('"');
6042
}
6143

6244
void loop()
6345
{
64-
// Print contents of vector and increment elements
65-
for (size_t i = 0; i < sizeof(v) / sizeof(v[0]); i++) {
66-
Serial.print(v[i]);
67-
Serial.print(' ');
68-
v[i] += 1;
46+
uint8_t page[eeprom.PAGE_MAX];
47+
for (uint32_t offset = 0; offset < BLOCK_MAX; offset += eeprom.PAGE_MAX) {
48+
// Read page buffer from block storage
49+
block.read(page, offset, sizeof(page));
50+
// Print page contents and update
51+
if (offset < 0x10) Serial.print('0');
52+
if (offset < 0x100) Serial.print('0');
53+
Serial.print(offset, HEX);
54+
Serial.print(':');
55+
for (size_t i = 0; i < sizeof(page); i++) {
56+
uint8_t v = page[i];
57+
Serial.print(' ');
58+
if (v < 0x10) Serial.print('0');
59+
Serial.print(v, HEX);
60+
page[i] = v + 1;
61+
}
62+
Serial.println();
63+
// Write page back to block storage
64+
block.write(offset, page, sizeof(page));
6965
}
7066
Serial.println();
71-
delay(2000);
72-
73-
// Write vector to eeprom
74-
Serial.print(F("loop:write: "));
75-
Serial.println(v_eeprom.write());
76-
77-
// Clear vector to verify read
78-
for (size_t i = 0; i < sizeof(v) / sizeof(v[0]); i++) {
79-
v[i] = 0;
80-
}
81-
82-
// Read vector from eeprom
83-
Serial.print(F("loop:read: "));
84-
Serial.println(v_eeprom.read());
67+
delay(5000);
8568
}

examples/Cache/Cache.ino

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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
28+
AT24C32 eeprom(twi);
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+
}

0 commit comments

Comments
 (0)