# HG changeset patch # User Paul Boddie # Date 1421788993 -3600 # Node ID e47ee6e49a2a6965563763285c1da8b9826fd7de # Parent 44bf682cef17b41df3c6a934e910b769baee5477 Changed pin usage to avoid potential problems with pin 13. Made the serial reading more robust. diff -r 44bf682cef17 -r e47ee6e49a2a ArduinoAm29F010.cpp --- a/ArduinoAm29F010.cpp Tue Jan 20 19:33:23 2015 +0100 +++ b/ArduinoAm29F010.cpp Tue Jan 20 22:23:13 2015 +0100 @@ -1,10 +1,11 @@ -const int CE = A5, OE = 2, WE = 3, - CS1 = 4, CS2 = 5, - DQ0 = 6, DQ1 = 7, DQ2 = 8, DQ3 = 9, - DQ4 = 10, DQ5 = 11, DQ6 = 12, DQ7 = 13; +const int CE = A5, OE = A4, WE = A3, + CS1 = 2, CS2 = 3, + DQ0 = 4, DQ1 = 5, DQ2 = 6, DQ3 = 7, + DQ4 = 8, DQ5 = 9, DQ6 = 10, DQ7 = 11; const int BUFSIZE = 8; char inbuffer[BUFSIZE]; +int nread = 0; const char ERASE[][8] = { "W5555AA", "W2AAA55", "W555580", "W5555AA", "W2AAA55" @@ -132,26 +133,20 @@ digitalWrite(CE, HIGH); } -int readCommand(int nread, const char buffer[]) +int readCommand(const char buffer[]) { int high, low; - if (nread < 5) - return 0; - high = (fromHex(buffer[1]) << 4) + (fromHex(buffer[2])); low = (fromHex(buffer[3]) << 4) + (fromHex(buffer[4])); return readOp(high, low); } -bool writeCommand(int nread, const char buffer[]) +bool writeCommand(const char buffer[]) { int high, low, data; - if (nread < 7) - return false; - high = (fromHex(buffer[1]) << 4) + (fromHex(buffer[2])); low = (fromHex(buffer[3]) << 4) + (fromHex(buffer[4])); data = (fromHex(buffer[5]) << 4) + (fromHex(buffer[6])); @@ -190,19 +185,16 @@ { for (int i = 0; i < 5; i++) { - writeCommand(7, ERASE[i]); + writeCommand(ERASE[i]); } writeOp(0x55, 0x55, 0x30); return waitForCompletion(0, 0, 0xff); } -bool sectorErase(int nread, const char buffer[]) +bool sectorErase(const char buffer[]) { int high; - if (nread < 2) - return false; - // 3-bit number shifted to A16...A14 with A16 discarded. // Thus, only sectors from 0 to 3 are supported. @@ -210,7 +202,7 @@ for (int i = 0; i < 5; i++) { - writeCommand(7, ERASE[i]); + writeCommand(ERASE[i]); } writeOp(high, 0, 0x30); @@ -219,20 +211,17 @@ return waitForCompletion(high, 0, 0xff); } -bool program(int nread, const char buffer[]) +bool program(const char buffer[]) { int high, low, data; - if (nread < 7) - return false; - high = (fromHex(buffer[1]) << 4) + (fromHex(buffer[2])); low = (fromHex(buffer[3]) << 4) + (fromHex(buffer[4])); data = (fromHex(buffer[5]) << 4) + (fromHex(buffer[6])); for (int i = 0; i < 3; i++) { - writeCommand(7, PROGRAM[i]); + writeCommand(PROGRAM[i]); } writeOp(high, low, data); @@ -263,26 +252,31 @@ // Interface loop. Serial.println("?"); - Serial.setTimeout(3600000); // 1 hour } void loop() { - int nread = 0; - - if (nread = Serial.readBytesUntil('\n', inbuffer, BUFSIZE)) + if (nread += Serial.readBytesUntil('\n', inbuffer + nread, BUFSIZE - nread)) { switch (inbuffer[0]) { case 'R': - Serial.println(readCommand(nread, inbuffer), HEX); + if (nread >= 5) + { + Serial.println(readCommand(inbuffer), HEX); + nread = 0; + } break; case 'W': - if (writeCommand(nread, inbuffer)) - Serial.println("W"); - else - Serial.println("C"); + if (nread >= 7) + { + if (writeCommand(inbuffer)) + Serial.println("W"); + else + Serial.println("C"); + nread = 0; + } break; case 'E': @@ -290,24 +284,34 @@ Serial.println("E"); else Serial.println("C"); + nread = 0; break; case 'S': - if (sectorErase(nread, inbuffer)) - Serial.println("S"); - else - Serial.println("C"); + if (nread >= 2) + { + if (sectorErase(inbuffer)) + Serial.println("S"); + else + Serial.println("C"); + nread = 0; + } break; case 'P': - if (program(nread, inbuffer)) - Serial.println("P"); - else - Serial.println("C"); + if (nread >= 7) + { + if (program(inbuffer)) + Serial.println("P"); + else + Serial.println("C"); + nread = 0; + } break; default: Serial.println(inbuffer[0]); + nread = 0; break; }