ArduinoAm29F010

Changeset

4:47737a873a56
2015-01-19 Paul Boddie raw files shortlog changelog graph Renamed the source file to reflect the actual IC in use.
ArduinoAm29F010.cpp (file) ArduinoAm29F010B.cpp
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ArduinoAm29F010.cpp	Mon Jan 19 01:27:08 2015 +0100
     1.3 @@ -0,0 +1,198 @@
     1.4 +const int CE = A5, OE = 2, WE = 3,
     1.5 +          CS1 = 4, CS2 = 5,
     1.6 +          DQ0 = 6, DQ1 = 7, DQ2 = 8, DQ3 = 9,
     1.7 +          DQ4 = 10, DQ5 = 11, DQ6 = 12, DQ7 = 13;
     1.8 +
     1.9 +const int BUFSIZE = 8;
    1.10 +char buffer[BUFSIZE];
    1.11 +
    1.12 +int fromHex(char c)
    1.13 +{
    1.14 +    if ((c >= 48) && (c <= 57))
    1.15 +        return c - 48;
    1.16 +    if ((c >= 65) && (c <= 70))
    1.17 +        return c - 65 + 10;
    1.18 +    if ((c >= 97) && (c <= 102))
    1.19 +        return c - 97 + 10;
    1.20 +    return 0;
    1.21 +}
    1.22 +
    1.23 +void setDataOut()
    1.24 +{
    1.25 +    pinMode(DQ0, OUTPUT);
    1.26 +    pinMode(DQ1, OUTPUT);
    1.27 +    pinMode(DQ2, OUTPUT);
    1.28 +    pinMode(DQ3, OUTPUT);
    1.29 +    pinMode(DQ4, OUTPUT);
    1.30 +    pinMode(DQ5, OUTPUT);
    1.31 +    pinMode(DQ6, OUTPUT);
    1.32 +    pinMode(DQ7, OUTPUT);
    1.33 +}
    1.34 +
    1.35 +void setDataIn()
    1.36 +{
    1.37 +    pinMode(DQ0, INPUT);
    1.38 +    pinMode(DQ1, INPUT);
    1.39 +    pinMode(DQ2, INPUT);
    1.40 +    pinMode(DQ3, INPUT);
    1.41 +    pinMode(DQ4, INPUT);
    1.42 +    pinMode(DQ5, INPUT);
    1.43 +    pinMode(DQ6, INPUT);
    1.44 +    pinMode(DQ7, INPUT);
    1.45 +}
    1.46 +
    1.47 +int readData()
    1.48 +{
    1.49 +    int i = DQ7;
    1.50 +    int data = 0;
    1.51 +
    1.52 +    setDataIn();
    1.53 +    delay(10);
    1.54 +
    1.55 +    while (1)
    1.56 +    {
    1.57 +        data += digitalRead(i);
    1.58 +        if (i == DQ0)
    1.59 +            break;
    1.60 +        i--;
    1.61 +        data <<= 1;
    1.62 +    }
    1.63 +
    1.64 +    setDataOut();
    1.65 +    return data;
    1.66 +}
    1.67 +
    1.68 +void writeData(int data)
    1.69 +{
    1.70 +    int i = DQ0;
    1.71 +
    1.72 +    while (1)
    1.73 +    {
    1.74 +        digitalWrite(i, data % 2);
    1.75 +        if (i == DQ7)
    1.76 +            break;
    1.77 +        i++;
    1.78 +        data >>= 1;
    1.79 +    }
    1.80 +}
    1.81 +
    1.82 +void setAddress(int high, int low)
    1.83 +{
    1.84 +    writeData(high);
    1.85 +    delayMicroseconds(1);
    1.86 +    digitalWrite(CS2, HIGH);
    1.87 +    delayMicroseconds(1);
    1.88 +    digitalWrite(CS2, LOW);
    1.89 +    writeData(low);
    1.90 +    delayMicroseconds(1);
    1.91 +    digitalWrite(CS1, HIGH);
    1.92 +    delayMicroseconds(1);
    1.93 +    digitalWrite(CS1, LOW);
    1.94 +}
    1.95 +
    1.96 +int readOutput(int nread)
    1.97 +{
    1.98 +    int high, low, data;
    1.99 +
   1.100 +    if (nread < 5)
   1.101 +        return 0;
   1.102 +
   1.103 +    high = (fromHex(buffer[1]) << 4) + (fromHex(buffer[2]));
   1.104 +    low = (fromHex(buffer[3]) << 4) + (fromHex(buffer[4]));
   1.105 +
   1.106 +    digitalWrite(CE, LOW);
   1.107 +    setAddress(high, low);
   1.108 +    delay(10);
   1.109 +    digitalWrite(OE, LOW);
   1.110 +    data = readData();
   1.111 +    delay(10);
   1.112 +    digitalWrite(OE, HIGH);
   1.113 +    digitalWrite(CE, HIGH);
   1.114 +
   1.115 +    return data;
   1.116 +}
   1.117 +
   1.118 +bool writeOutput(int nread)
   1.119 +{
   1.120 +    int high, low, data;
   1.121 +
   1.122 +    if (nread < 7)
   1.123 +        return false;
   1.124 +
   1.125 +    high = (fromHex(buffer[1]) << 4) + (fromHex(buffer[2]));
   1.126 +    low = (fromHex(buffer[3]) << 4) + (fromHex(buffer[4]));
   1.127 +    data = (fromHex(buffer[5]) << 4) + (fromHex(buffer[6]));
   1.128 +
   1.129 +    digitalWrite(CE, LOW);
   1.130 +    setAddress(high, low);
   1.131 +    delay(10);
   1.132 +    digitalWrite(WE, LOW);
   1.133 +    writeData(data);
   1.134 +    delay(10);
   1.135 +    digitalWrite(WE, HIGH);
   1.136 +    digitalWrite(CE, HIGH);
   1.137 +
   1.138 +    return true;
   1.139 +}
   1.140 +
   1.141 +void setup()
   1.142 +{
   1.143 +    int nread = 0;
   1.144 +
   1.145 +    Serial.begin(115200);
   1.146 +    pinMode(CE, OUTPUT);
   1.147 +    pinMode(OE, OUTPUT);
   1.148 +    pinMode(WE, OUTPUT);
   1.149 +    pinMode(CS1, OUTPUT);
   1.150 +    pinMode(CS2, OUTPUT);
   1.151 +    setDataOut();
   1.152 +
   1.153 +    // Initial state for the flash device.
   1.154 +
   1.155 +    digitalWrite(CE, HIGH);
   1.156 +    digitalWrite(OE, HIGH);
   1.157 +    digitalWrite(WE, HIGH);
   1.158 +
   1.159 +    // Initial state for the latches.
   1.160 +
   1.161 +    digitalWrite(CS1, LOW);
   1.162 +    digitalWrite(CS2, LOW);
   1.163 +
   1.164 +    // Interface loop.
   1.165 +
   1.166 +    Serial.println("?");
   1.167 +    Serial.setTimeout(3600000); // 1 hour
   1.168 +
   1.169 +    while (nread = Serial.readBytesUntil('\n', buffer, BUFSIZE))
   1.170 +    {
   1.171 +        switch (buffer[0])
   1.172 +        {
   1.173 +            case 'R':
   1.174 +            Serial.println(readOutput(nread), HEX);
   1.175 +            break;
   1.176 +
   1.177 +            case 'W':
   1.178 +            if (writeOutput(nread))
   1.179 +                Serial.println("W");
   1.180 +            else
   1.181 +                Serial.println("C");
   1.182 +            break;
   1.183 +
   1.184 +            default:
   1.185 +            Serial.println("?");
   1.186 +            break;
   1.187 +        }
   1.188 +    }
   1.189 +
   1.190 +    Serial.println("Done.");
   1.191 +}
   1.192 +
   1.193 +void loop()
   1.194 +{
   1.195 +}
   1.196 +
   1.197 +extern "C" void __cxa_pure_virtual(void) {
   1.198 +    while(1);
   1.199 +}
   1.200 +
   1.201 +// tabstop=4 expandtab shiftwidth=4
     2.1 --- a/ArduinoAm29F010B.cpp	Mon Jan 19 01:26:38 2015 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,198 +0,0 @@
     2.4 -const int CE = A5, OE = 2, WE = 3,
     2.5 -          CS1 = 4, CS2 = 5,
     2.6 -          DQ0 = 6, DQ1 = 7, DQ2 = 8, DQ3 = 9,
     2.7 -          DQ4 = 10, DQ5 = 11, DQ6 = 12, DQ7 = 13;
     2.8 -
     2.9 -const int BUFSIZE = 8;
    2.10 -char buffer[BUFSIZE];
    2.11 -
    2.12 -int fromHex(char c)
    2.13 -{
    2.14 -    if ((c >= 48) && (c <= 57))
    2.15 -        return c - 48;
    2.16 -    if ((c >= 65) && (c <= 70))
    2.17 -        return c - 65 + 10;
    2.18 -    if ((c >= 97) && (c <= 102))
    2.19 -        return c - 97 + 10;
    2.20 -    return 0;
    2.21 -}
    2.22 -
    2.23 -void setDataOut()
    2.24 -{
    2.25 -    pinMode(DQ0, OUTPUT);
    2.26 -    pinMode(DQ1, OUTPUT);
    2.27 -    pinMode(DQ2, OUTPUT);
    2.28 -    pinMode(DQ3, OUTPUT);
    2.29 -    pinMode(DQ4, OUTPUT);
    2.30 -    pinMode(DQ5, OUTPUT);
    2.31 -    pinMode(DQ6, OUTPUT);
    2.32 -    pinMode(DQ7, OUTPUT);
    2.33 -}
    2.34 -
    2.35 -void setDataIn()
    2.36 -{
    2.37 -    pinMode(DQ0, INPUT);
    2.38 -    pinMode(DQ1, INPUT);
    2.39 -    pinMode(DQ2, INPUT);
    2.40 -    pinMode(DQ3, INPUT);
    2.41 -    pinMode(DQ4, INPUT);
    2.42 -    pinMode(DQ5, INPUT);
    2.43 -    pinMode(DQ6, INPUT);
    2.44 -    pinMode(DQ7, INPUT);
    2.45 -}
    2.46 -
    2.47 -int readData()
    2.48 -{
    2.49 -    int i = DQ7;
    2.50 -    int data = 0;
    2.51 -
    2.52 -    setDataIn();
    2.53 -    delay(10);
    2.54 -
    2.55 -    while (1)
    2.56 -    {
    2.57 -        data += digitalRead(i);
    2.58 -        if (i == DQ0)
    2.59 -            break;
    2.60 -        i--;
    2.61 -        data <<= 1;
    2.62 -    }
    2.63 -
    2.64 -    setDataOut();
    2.65 -    return data;
    2.66 -}
    2.67 -
    2.68 -void writeData(int data)
    2.69 -{
    2.70 -    int i = DQ0;
    2.71 -
    2.72 -    while (1)
    2.73 -    {
    2.74 -        digitalWrite(i, data % 2);
    2.75 -        if (i == DQ7)
    2.76 -            break;
    2.77 -        i++;
    2.78 -        data >>= 1;
    2.79 -    }
    2.80 -}
    2.81 -
    2.82 -void setAddress(int high, int low)
    2.83 -{
    2.84 -    writeData(high);
    2.85 -    delayMicroseconds(1);
    2.86 -    digitalWrite(CS2, HIGH);
    2.87 -    delayMicroseconds(1);
    2.88 -    digitalWrite(CS2, LOW);
    2.89 -    writeData(low);
    2.90 -    delayMicroseconds(1);
    2.91 -    digitalWrite(CS1, HIGH);
    2.92 -    delayMicroseconds(1);
    2.93 -    digitalWrite(CS1, LOW);
    2.94 -}
    2.95 -
    2.96 -int readOutput(int nread)
    2.97 -{
    2.98 -    int high, low, data;
    2.99 -
   2.100 -    if (nread < 5)
   2.101 -        return 0;
   2.102 -
   2.103 -    high = (fromHex(buffer[1]) << 4) + (fromHex(buffer[2]));
   2.104 -    low = (fromHex(buffer[3]) << 4) + (fromHex(buffer[4]));
   2.105 -
   2.106 -    digitalWrite(CE, LOW);
   2.107 -    setAddress(high, low);
   2.108 -    delay(10);
   2.109 -    digitalWrite(OE, LOW);
   2.110 -    data = readData();
   2.111 -    delay(10);
   2.112 -    digitalWrite(OE, HIGH);
   2.113 -    digitalWrite(CE, HIGH);
   2.114 -
   2.115 -    return data;
   2.116 -}
   2.117 -
   2.118 -bool writeOutput(int nread)
   2.119 -{
   2.120 -    int high, low, data;
   2.121 -
   2.122 -    if (nread < 7)
   2.123 -        return false;
   2.124 -
   2.125 -    high = (fromHex(buffer[1]) << 4) + (fromHex(buffer[2]));
   2.126 -    low = (fromHex(buffer[3]) << 4) + (fromHex(buffer[4]));
   2.127 -    data = (fromHex(buffer[5]) << 4) + (fromHex(buffer[6]));
   2.128 -
   2.129 -    digitalWrite(CE, LOW);
   2.130 -    setAddress(high, low);
   2.131 -    delay(10);
   2.132 -    digitalWrite(WE, LOW);
   2.133 -    writeData(data);
   2.134 -    delay(10);
   2.135 -    digitalWrite(WE, HIGH);
   2.136 -    digitalWrite(CE, HIGH);
   2.137 -
   2.138 -    return true;
   2.139 -}
   2.140 -
   2.141 -void setup()
   2.142 -{
   2.143 -    int nread = 0;
   2.144 -
   2.145 -    Serial.begin(115200);
   2.146 -    pinMode(CE, OUTPUT);
   2.147 -    pinMode(OE, OUTPUT);
   2.148 -    pinMode(WE, OUTPUT);
   2.149 -    pinMode(CS1, OUTPUT);
   2.150 -    pinMode(CS2, OUTPUT);
   2.151 -    setDataOut();
   2.152 -
   2.153 -    // Initial state for the flash device.
   2.154 -
   2.155 -    digitalWrite(CE, HIGH);
   2.156 -    digitalWrite(OE, HIGH);
   2.157 -    digitalWrite(WE, HIGH);
   2.158 -
   2.159 -    // Initial state for the latches.
   2.160 -
   2.161 -    digitalWrite(CS1, LOW);
   2.162 -    digitalWrite(CS2, LOW);
   2.163 -
   2.164 -    // Interface loop.
   2.165 -
   2.166 -    Serial.println("?");
   2.167 -    Serial.setTimeout(3600000); // 1 hour
   2.168 -
   2.169 -    while (nread = Serial.readBytesUntil('\n', buffer, BUFSIZE))
   2.170 -    {
   2.171 -        switch (buffer[0])
   2.172 -        {
   2.173 -            case 'R':
   2.174 -            Serial.println(readOutput(nread), HEX);
   2.175 -            break;
   2.176 -
   2.177 -            case 'W':
   2.178 -            if (writeOutput(nread))
   2.179 -                Serial.println("W");
   2.180 -            else
   2.181 -                Serial.println("C");
   2.182 -            break;
   2.183 -
   2.184 -            default:
   2.185 -            Serial.println("?");
   2.186 -            break;
   2.187 -        }
   2.188 -    }
   2.189 -
   2.190 -    Serial.println("Done.");
   2.191 -}
   2.192 -
   2.193 -void loop()
   2.194 -{
   2.195 -}
   2.196 -
   2.197 -extern "C" void __cxa_pure_virtual(void) {
   2.198 -    while(1);
   2.199 -}
   2.200 -
   2.201 -// tabstop=4 expandtab shiftwidth=4