41
41
* See the License for the specific language governing permissions and
42
42
* limitations under the License.
43
43
*/
44
- enum MODE {READ, WRITE};
44
+ enum MODE {STANDBY, READ, WRITE};
45
45
const unsigned int MAX_PAYLOAD = 63 ;
46
46
const unsigned int DELAY_US = 10 ;
47
47
48
48
// AT28C256 contol lines
49
- const unsigned int EEPROM_CE = A0;
49
+ const unsigned int EEPROM_WE = A0;
50
50
const unsigned int EEPROM_OE = A1;
51
- const unsigned int EEPROM_WE = A2;
51
+ const unsigned int EEPROM_CE = A2;
52
52
53
53
// 74HC595 control lines
54
- const unsigned int SHIFT_SER = 10 ;
55
- const unsigned int SHIFT_RCLK = 11 ;
56
- const unsigned int SHIFT_SCLK = 12 ;
54
+ const unsigned int SHIFT_SER = A4 ;
55
+ const unsigned int SHIFT_RCLK = 12 ;
56
+ const unsigned int SHIFT_SCLK = 11 ;
57
57
58
58
// Activity indicator LED
59
- const unsigned int ACT_LED = 13 ;
59
+ const unsigned int ACT_LED = 10 ;
60
60
61
61
// Data pins (LSB to MSB)
62
62
const unsigned int dataPins[] = {2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
@@ -79,7 +79,7 @@ void setup() {
79
79
pinMode (ACT_LED, OUTPUT);
80
80
digitalWrite (ACT_LED, LOW);
81
81
82
- readMode ();
82
+ standbyMode ();
83
83
}
84
84
85
85
/*
@@ -151,13 +151,15 @@ void loadShiftAddr(unsigned int addr) {
151
151
* Returns the byte at the specified address.
152
152
*/
153
153
byte readAddr (unsigned int addr) {
154
+ readMode ();
154
155
loadShiftAddr (addr);
155
156
delayMicroseconds (DELAY_US);
156
157
157
158
byte val = 0 ;
158
159
for (unsigned int i = 0 ; i < 8 ; i++) {
159
160
val |= (digitalRead (dataPins[i]) << i);
160
161
}
162
+ standbyMode ();
161
163
return val;
162
164
}
163
165
@@ -169,18 +171,20 @@ byte readAddr(unsigned int addr) {
169
171
void writeAddr (unsigned int addr, byte val) {
170
172
loadShiftAddr (addr);
171
173
174
+ writeMode ();
175
+
172
176
// load data byte
173
177
for (unsigned int i = 0 ; i < 8 ; i++) {
174
178
digitalWrite (dataPins[i], (val >> i) & 1 );
175
179
}
176
180
delayMicroseconds (DELAY_US);
177
181
178
- // if (addr == 0x3f) {
179
- // delay(1000);
180
- // }
181
182
digitalWrite (EEPROM_WE, LOW);
182
183
delayMicroseconds (DELAY_US);
183
184
digitalWrite (EEPROM_WE, HIGH);
185
+
186
+ delayMicroseconds (DELAY_US);
187
+ standbyMode ();
184
188
}
185
189
186
190
/*
@@ -222,6 +226,7 @@ void load(unsigned int len) {
222
226
223
227
for (unsigned int i = 0 ; i < cnt; i++) {
224
228
writeAddr (addr++, buf[i]);
229
+ delay (5 );
225
230
}
226
231
}
227
232
}
@@ -231,18 +236,16 @@ void load(unsigned int len) {
231
236
* EEPROM_OE HIGH.
232
237
*/
233
238
void writeMode () {
234
- if (mode != WRITE) {
235
- digitalWrite (EEPROM_CE, LOW);
236
- digitalWrite (EEPROM_OE, HIGH);
237
- digitalWrite (EEPROM_WE, HIGH);
238
-
239
- for (unsigned int i = 0 ; i < 8 ; i++) {
240
- pinMode (dataPins[i], OUTPUT);
241
- }
239
+ digitalWrite (EEPROM_CE, LOW);
240
+ digitalWrite (EEPROM_OE, HIGH);
241
+ digitalWrite (EEPROM_WE, HIGH);
242
242
243
- delay ( 5 );
244
- mode = WRITE ;
243
+ for ( unsigned int i = 0 ; i < 8 ; i++) {
244
+ pinMode (dataPins[i], OUTPUT) ;
245
245
}
246
+
247
+ delayMicroseconds (DELAY_US);
248
+ mode = WRITE;
246
249
}
247
250
248
251
/* *
@@ -251,19 +254,32 @@ void writeMode() {
251
254
*/
252
255
void readMode () {
253
256
if (mode != READ) {
254
- digitalWrite (EEPROM_CE, LOW);
255
- digitalWrite (EEPROM_OE, LOW);
256
- digitalWrite (EEPROM_WE, HIGH);
257
-
258
257
for (unsigned int i = 0 ; i < 8 ; i++) {
259
258
pinMode (dataPins[i], INPUT);
260
259
}
261
-
262
- delay (5 );
260
+
261
+ digitalWrite (EEPROM_CE, LOW);
262
+ digitalWrite (EEPROM_OE, LOW);
263
+ digitalWrite (EEPROM_WE, HIGH);
264
+
265
+ delayMicroseconds (DELAY_US);
263
266
mode = READ;
264
267
}
265
268
}
266
269
270
+ void standbyMode () {
271
+ for (unsigned int i = 0 ; i < 8 ; i++) {
272
+ pinMode (dataPins[i], INPUT);
273
+ }
274
+
275
+ digitalWrite (EEPROM_CE, HIGH);
276
+ digitalWrite (EEPROM_OE, LOW);
277
+ digitalWrite (EEPROM_WE, HIGH);
278
+
279
+ delayMicroseconds (DELAY_US);
280
+ mode = STANDBY;
281
+ }
282
+
267
283
void error () {
268
284
for (int i = 0 ; i < 5 ; i++) {
269
285
digitalWrite (ACT_LED, LOW);
@@ -285,20 +301,16 @@ void loop() {
285
301
send (&val, 1 , false );
286
302
287
303
} else if (buf[0 ] == 0x77 && len == 4 ) {
288
- writeMode ();
289
304
writeAddr ((buf[1 ] << 8 ) + buf[2 ], buf[3 ]);
290
305
// signal operation completion
291
306
send (NULL , 0 , false );
292
- readMode ();
293
-
307
+
294
308
} else if (buf[0 ] == 0x64 && len == 1 ) {
295
309
dump ();
296
310
297
311
} else if (buf[0 ] == 0x6c && len == 3 ) {
298
312
send (NULL , 0 , false ); // acknowledge cmd message
299
- writeMode ();
300
313
load ((buf[1 ] << 8 ) + buf[2 ]);
301
- readMode ();
302
314
303
315
} else {
304
316
error ();
0 commit comments