-
Notifications
You must be signed in to change notification settings - Fork 0
/
sram-tester.ino
278 lines (232 loc) · 6 KB
/
sram-tester.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// SPDX-License-Identifier: MIT
/*
* Universal Static RAM Tester - Tests SRAM by writing and reading patterns
*
* Derived from the 2114 SRAM tester by Carsten Skjerk
* https://github.com/skjerk/Arduino-2114-SRAM-tester
*
* (c) Dennis Marttinen 2022
* Licensed under the MIT license
*/
struct WritePin {
uint8_t pin;
bool inverted;
};
// ------------------------------
/*
* 6116 SRAM (ATMega 2560 pinout)
* Pin 12 (Vss) is wired to GND
* Pin 18 (CS) is wired to GND
* Pin 20 (OE) is wired to GND
* Pin 24 (Vcc) is wired to +5V
*/
// Address Pins
const uint8_t addressPins[] = {
47, 45, 43, 41, 39, 37, 35, 33, 32, 34, 38
};
// Data Pins
const uint8_t dataPins[] = {
49, 51, 53, 48, 46, 44, 42, 40
};
// Write Pins
const WritePin writePins[] = {
{36, true}, // (LOW = WRITE)
};
// The number of addressable memory locations
const uint32_t addressCount = 2048;
///*
// * 2114 SRAM (ATMega 2560 pinout)
// * Pin 12 (Vss) is wired to GND
// * Pin 18 (CS) is wired to GND
// * Pin 24 (Vcc) is wired to +5V
//*/
//
//// Address Pins
//const uint8_t addressPins[] = {
// 47, 49, 51, 45, 43, 41, 39, 38, 40, 42
//};
//
//// Data Pins
//const uint8_t dataPins[] = {
// 44, 46, 48, 50
//};
//
//// Write Pins
//const WritePin writePins[] = {
// {52, true}, // (LOW = WRITE)
//};
//
//// The number of addressable memory locations
//const uint32_t addressCount = 1024;
// ------------------------------
#define NX(x) (sizeof(x) / sizeof((x)[0]))
const size_t NA = NX(addressPins);
const size_t ND = NX(dataPins);
const size_t NW = NX(writePins);
// Set Address pins to output
void setupAddressPins() {
for (size_t i = 0; i < NA; i++) {
pinMode(addressPins[i], OUTPUT);
}
}
// Set Data pins to output
void setDataPinsOutput() {
for (size_t i = 0; i < ND; i++) {
pinMode(dataPins[i], OUTPUT);
}
}
// Set Data pins to input
void setDataPinsInput() {
for (size_t i = 0; i < ND; i++) {
// Explicitly set them LOW or else this reads its own output
digitalWrite(dataPins[i], LOW);
pinMode(dataPins[i], INPUT);
}
}
// Set Write pins to output
void setupWritePins() {
for (size_t i = 0; i < NW; i++) {
pinMode(writePins[i].pin, OUTPUT);
}
}
// Initial setup of pins and serial monitor
void setup() {
// Initialize all pins
setupAddressPins();
setDataPinsOutput();
setupWritePins();
// Initialize Serial Port
Serial.begin(115200);
Serial.println("Universal Static RAM Tester by Dennis Marttinen");
Serial.print(NA);
Serial.print('/');
Serial.print(ND);
Serial.print('/');
Serial.print(NW);
Serial.println(" address/data/write pin(s) configured");
}
// Set the address pins to match the specified address
void setAddressBits(size_t address) {
for (size_t i = 0; i < NA; i++) {
digitalWrite(addressPins[i], bitRead(address, i));
}
}
// Set the data pins to match the specified value
void setDataBits(size_t value) {
for (size_t i = 0; i < ND; i++) {
digitalWrite(dataPins[i], bitRead(value, i));
}
}
void enableWritePins() {
for (size_t i = 0; i < NW; i++) {
const WritePin* p = &writePins[i];
digitalWrite(p->pin, !p->inverted);
}
}
void disableWritePins() {
for (size_t i = 0; i < NW; i++) {
const WritePin* p = &writePins[i];
digitalWrite(p->pin, p->inverted);
}
}
// Write data to the specified memory address
void writeData(size_t address, size_t data) {
// Set data pins to output
setDataPinsOutput();
// Set address bits
setAddressBits(address);
// Set data bits
setDataBits(data);
// Enable Write pins
enableWritePins();
// Wait for the logic to be stabilized
//delay(1);
// Disable Write pins
disableWritePins();
// Wait a bit for the write to commit
//delay(1);
}
// Read data from the specified memory address
// Note that the Write pins must already be in READ mode
size_t readData(size_t address) {
// Set data pins to input
setDataPinsInput();
// Set address bits
setAddressBits(address);
// Wait for the logic to be stabilized
//delay(1);
// Read each data bit one by one
size_t result = 0;
for (size_t i = 0; i < ND; i++) {
bitWrite(result, i, digitalRead(dataPins[i]));
}
return result;
}
// Output binary value from the ND bit data
void printBinary(size_t data) {
for (size_t b = ND; b > 0; b--) {
Serial.print(bitRead(data, b - 1));
}
}
// Print an unsigned 64-bit integer
void printU64(uint64_t value) {
if (value == 0) {
Serial.print('0');
return;
}
unsigned char buf[20];
uint8_t i = 0;
while (value > 0) {
uint64_t q = value/10;
buf[i++] = value - q * 10;
value = q;
}
for (; i > 0; i--) {
Serial.print((char) ('0' + buf[i - 1]));
}
}
void loop() {
uint32_t firstError = 0;
uint32_t lastError = 0;
uint64_t errorCount = 0;
// Use all possible values for data patterns
for (size_t pattern = 0; pattern < bit(ND); pattern++) {
Serial.print("Running test pattern ");
printBinary(pattern);
Serial.println();
// Loop through all addresses in the SRAM
for (uint32_t addr = 0; addr < addressCount; addr++) {
// Write test pattern to the SRAM
writeData(addr, pattern);
// Read data from the SRAM
size_t data = readData(addr);
// Verify
if (data != pattern) {
lastError = addr;
if (errorCount++ == 0) {
firstError = addr;
}
Serial.print("Error at address 0x");
Serial.print(addr, HEX);
Serial.print(" - Got: ");
printBinary(data);
Serial.print(", expected: ");
printBinary(pattern);
Serial.println();
}
}
}
Serial.println("Test complete");
printU64(errorCount);
Serial.print(" errors found (");
Serial.print((100.f * errorCount) / ((float)bit(ND) * addressCount));
Serial.println("% failed)");
if (errorCount > 0) {
Serial.print("Error span: 0x");
Serial.print(firstError, HEX);
Serial.print(" to 0x");
Serial.println(lastError, HEX);
}
// Nothing more to do, so loop indefinitely - or until Reset is pressed
while (1) {};
}