/
attiny202-eeprom-test-pattern.ino
81 lines (63 loc) · 1.96 KB
/
attiny202-eeprom-test-pattern.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <EEPROM.h>
// NOTE: for this sketch, it is not recommended to use the "minimal"
// printf library because it does not support zero padding.
// ------------------------------------------------------------------------
// Serial defines
// ------------------------------------------------------------------------
#define SERIAL_ENABLED 1
#define SERIAL_BAUD_RATE 115200
#if SERIAL_ENABLED
char serial_msg_buffer[100];
#define SERIAL_MSG(...) \
do { \
sprintf(serial_msg_buffer, __VA_ARGS__); \
Serial.println(serial_msg_buffer); \
} while (false);
#define SERIAL_INIT(x) \
Serial.begin(x); \
while (!Serial) {} // wait for Serial to initialize
#else
#define SERIAL_MSG(...)
#define SERIAL_INIT(x)
#endif
void setup() {
SERIAL_INIT(SERIAL_BAUD_RATE);
SERIAL_MSG("Serial comm enabled and initialized...");
SERIAL_MSG("EEPROM length: %d", EEPROM.length());
print_eeprom();
SERIAL_MSG("\nWriting test pattern to EEPROM...\n");
// if serial is enabled, the sketch will be restarted and run
// a second time after about 200ms. Put this delay in here so
// we don't unintentionally restart the sketch during an
// EEPROM write.
delay(2000);
write_eeprom_test_pattern();
print_eeprom();
}
void loop() {
}
void print_eeprom() {
#ifdef SERIAL_ENABLED
int row_size = 16;
char* pos = serial_msg_buffer;
pos += sprintf(pos, " ");
for (int h = 0; h < row_size; ++h) {
pos += sprintf(pos, "% 4d ", h);
}
Serial.println(serial_msg_buffer);
pos = serial_msg_buffer;
for (int r = 0; r < EEPROM.length(); r += row_size) {
pos += sprintf(pos, "%04d: ", r);
for (int c = r; c < r + row_size; ++c) {
pos += sprintf(pos, "0x%02X ", (uint8_t)EEPROM.read(c));
}
Serial.println(serial_msg_buffer);
pos = serial_msg_buffer;
}
#endif
}
void write_eeprom_test_pattern() {
for (int i = 0; i < EEPROM.length(); ++i) {
EEPROM.update(i, i);
}
}