1 const int CE = 1, OE = 2, WE = 3, 2 CS1 = 4, CS2 = 5, 3 DQ0 = 6, DQ1 = 7, DQ2 = 8, DQ3 = 9, 4 DQ4 = 10, DQ5 = 11, DQ6 = 12, DQ7 = 13; 5 6 const int BUFSIZE = 8; 7 char buffer[BUFSIZE]; 8 9 int fromHex(char c) 10 { 11 if ((c >= 48) && (c <= 57)) 12 return c - 48; 13 if ((c >= 65) && (c <= 70)) 14 return c - 65 + 10; 15 if ((c >= 97) && (c <= 102)) 16 return c - 97 + 10; 17 return 0; 18 } 19 20 void setDataOut() 21 { 22 pinMode(DQ0, OUTPUT); 23 pinMode(DQ1, OUTPUT); 24 pinMode(DQ2, OUTPUT); 25 pinMode(DQ3, OUTPUT); 26 pinMode(DQ4, OUTPUT); 27 pinMode(DQ5, OUTPUT); 28 pinMode(DQ6, OUTPUT); 29 pinMode(DQ7, OUTPUT); 30 } 31 32 void setDataIn() 33 { 34 pinMode(DQ0, INPUT); 35 pinMode(DQ1, INPUT); 36 pinMode(DQ2, INPUT); 37 pinMode(DQ3, INPUT); 38 pinMode(DQ4, INPUT); 39 pinMode(DQ5, INPUT); 40 pinMode(DQ6, INPUT); 41 pinMode(DQ7, INPUT); 42 } 43 44 int readData() 45 { 46 int i = DQ7; 47 int data = 0; 48 49 setDataIn(); 50 delayMicroseconds(1); 51 52 while (1) 53 { 54 data += digitalRead(i); 55 if (i == DQ0) 56 break; 57 i--; 58 data <<= 1; 59 } 60 61 setDataOut(); 62 return data; 63 } 64 65 void writeData(int data) 66 { 67 int i = DQ0; 68 69 while (1) 70 { 71 digitalWrite(i, data % 2); 72 if (i == DQ7) 73 break; 74 i++; 75 data >>= 1; 76 } 77 } 78 79 void setAddress(int high, int low) 80 { 81 writeData(high); 82 delayMicroseconds(1); 83 digitalWrite(CS2, HIGH); 84 delayMicroseconds(1); 85 digitalWrite(CS2, LOW); 86 writeData(low); 87 delayMicroseconds(1); 88 digitalWrite(CS1, HIGH); 89 delayMicroseconds(1); 90 digitalWrite(CS1, LOW); 91 } 92 93 int readOutput(int nread) 94 { 95 int high, low, data; 96 97 if (nread < 5) 98 return 0; 99 100 high = (fromHex(buffer[1]) << 4) + (fromHex(buffer[2])); 101 low = (fromHex(buffer[3]) << 4) + (fromHex(buffer[4])); 102 103 setAddress(high, low); 104 digitalWrite(OE, LOW); 105 data = readData(); 106 digitalWrite(OE, HIGH); 107 108 return data; 109 } 110 111 bool writeOutput(int nread) 112 { 113 int high, low, data; 114 115 if (nread < 7) 116 return false; 117 118 high = (fromHex(buffer[1]) << 4) + (fromHex(buffer[2])); 119 low = (fromHex(buffer[3]) << 4) + (fromHex(buffer[4])); 120 data = (fromHex(buffer[5]) << 4) + (fromHex(buffer[6])); 121 122 setAddress(high, low); 123 digitalWrite(WE, LOW); 124 writeData(data); 125 digitalWrite(WE, HIGH); 126 127 return true; 128 } 129 130 void setup() 131 { 132 int nread = 0; 133 134 Serial.begin(115200); 135 pinMode(CE, OUTPUT); 136 pinMode(OE, OUTPUT); 137 pinMode(WE, OUTPUT); 138 pinMode(CS1, OUTPUT); 139 pinMode(CS2, OUTPUT); 140 setDataOut(); 141 142 // Initial state for the flash device. 143 144 digitalWrite(CE, HIGH); 145 digitalWrite(OE, HIGH); 146 digitalWrite(WE, HIGH); 147 148 // Initial state for the latches. 149 150 digitalWrite(CS1, LOW); 151 digitalWrite(CS2, LOW); 152 153 // Interface loop. 154 155 Serial.print("? "); 156 Serial.setTimeout(3600000); // 1 hour 157 158 while (nread = Serial.readBytesUntil('\n', buffer, BUFSIZE)) 159 { 160 switch (buffer[0]) 161 { 162 case 'R': 163 Serial.println(readOutput(nread), HEX); 164 break; 165 166 case 'W': 167 if (writeOutput(nread)) 168 Serial.println("Written."); 169 else 170 Serial.println("Command?"); 171 break; 172 173 default: 174 break; 175 } 176 177 Serial.print("? "); 178 } 179 180 Serial.println("Done."); 181 } 182 183 void loop() 184 { 185 } 186 187 extern "C" void __cxa_pure_virtual(void) { 188 while(1); 189 } 190 191 // tabstop=4 expandtab shiftwidth=4