4141 * See the License for the specific language governing permissions and
4242 * limitations under the License.
4343 */
44- enum MODE {READ, WRITE};
44+ enum MODE {STANDBY, READ, WRITE};
4545const unsigned int MAX_PAYLOAD = 63 ;
4646const unsigned int DELAY_US = 10 ;
4747
4848// AT28C256 contol lines
49- const unsigned int EEPROM_CE = A0;
49+ const unsigned int EEPROM_WE = A0;
5050const unsigned int EEPROM_OE = A1;
51- const unsigned int EEPROM_WE = A2;
51+ const unsigned int EEPROM_CE = A2;
5252
5353// 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 ;
5757
5858// Activity indicator LED
59- const unsigned int ACT_LED = 13 ;
59+ const unsigned int ACT_LED = 10 ;
6060
6161// Data pins (LSB to MSB)
6262const unsigned int dataPins[] = {2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
@@ -79,7 +79,7 @@ void setup() {
7979 pinMode (ACT_LED, OUTPUT);
8080 digitalWrite (ACT_LED, LOW);
8181
82- readMode ();
82+ standbyMode ();
8383}
8484
8585/*
@@ -151,13 +151,15 @@ void loadShiftAddr(unsigned int addr) {
151151 * Returns the byte at the specified address.
152152 */
153153byte readAddr (unsigned int addr) {
154+ readMode ();
154155 loadShiftAddr (addr);
155156 delayMicroseconds (DELAY_US);
156157
157158 byte val = 0 ;
158159 for (unsigned int i = 0 ; i < 8 ; i++) {
159160 val |= (digitalRead (dataPins[i]) << i);
160161 }
162+ standbyMode ();
161163 return val;
162164}
163165
@@ -169,18 +171,20 @@ byte readAddr(unsigned int addr) {
169171void writeAddr (unsigned int addr, byte val) {
170172 loadShiftAddr (addr);
171173
174+ writeMode ();
175+
172176 // load data byte
173177 for (unsigned int i = 0 ; i < 8 ; i++) {
174178 digitalWrite (dataPins[i], (val >> i) & 1 );
175179 }
176180 delayMicroseconds (DELAY_US);
177181
178- // if (addr == 0x3f) {
179- // delay(1000);
180- // }
181182 digitalWrite (EEPROM_WE, LOW);
182183 delayMicroseconds (DELAY_US);
183184 digitalWrite (EEPROM_WE, HIGH);
185+
186+ delayMicroseconds (DELAY_US);
187+ standbyMode ();
184188}
185189
186190/*
@@ -222,6 +226,7 @@ void load(unsigned int len) {
222226
223227 for (unsigned int i = 0 ; i < cnt; i++) {
224228 writeAddr (addr++, buf[i]);
229+ delay (5 );
225230 }
226231 }
227232}
@@ -231,18 +236,16 @@ void load(unsigned int len) {
231236 * EEPROM_OE HIGH.
232237 */
233238void 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);
242242
243- delay ( 5 );
244- mode = WRITE ;
243+ for ( unsigned int i = 0 ; i < 8 ; i++) {
244+ pinMode (dataPins[i], OUTPUT) ;
245245 }
246+
247+ delayMicroseconds (DELAY_US);
248+ mode = WRITE;
246249}
247250
248251/* *
@@ -251,19 +254,32 @@ void writeMode() {
251254 */
252255void readMode () {
253256 if (mode != READ) {
254- digitalWrite (EEPROM_CE, LOW);
255- digitalWrite (EEPROM_OE, LOW);
256- digitalWrite (EEPROM_WE, HIGH);
257-
258257 for (unsigned int i = 0 ; i < 8 ; i++) {
259258 pinMode (dataPins[i], INPUT);
260259 }
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);
263266 mode = READ;
264267 }
265268}
266269
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+
267283void error () {
268284 for (int i = 0 ; i < 5 ; i++) {
269285 digitalWrite (ACT_LED, LOW);
@@ -285,20 +301,16 @@ void loop() {
285301 send (&val, 1 , false );
286302
287303 } else if (buf[0 ] == 0x77 && len == 4 ) {
288- writeMode ();
289304 writeAddr ((buf[1 ] << 8 ) + buf[2 ], buf[3 ]);
290305 // signal operation completion
291306 send (NULL , 0 , false );
292- readMode ();
293-
307+
294308 } else if (buf[0 ] == 0x64 && len == 1 ) {
295309 dump ();
296310
297311 } else if (buf[0 ] == 0x6c && len == 3 ) {
298312 send (NULL , 0 , false ); // acknowledge cmd message
299- writeMode ();
300313 load ((buf[1 ] << 8 ) + buf[2 ]);
301- readMode ();
302314
303315 } else {
304316 error ();
0 commit comments