execute all tests just once
[Arduino] / c2_ulx3s_test / edid.cpp
1 //VGA writer to write eeproms in vga displays
2 //Apr 2011 Kieran Levin kiram9@yahoo.com 
3 //If you want to modify some data scroll down and modify the bytes in command 'c'
4 //Then load from display - change custom bytes, generate checksum, write to save values back(r,c,g,w)
5 //See also http://en.wikipedia.org/wiki/Extended_display_identification_data
6 /*Help Commands:
7   r=read hex
8   b=read binary
9   l=load binary from serial(gets 128 bytes of binary data from serial then returns)
10   w=write
11   d=displaybuffer(binary)
12   e=print detailed info
13   g=generatechecksum
14   t=check checksum
15   c=change custom bytes (compiled in)
16 Connection 
17 Arduino         VGA   DVI
18 Analog 4(Data)  12    7  
19 Analog 5(Clk)   15    6
20 DIO 2           14          
21 Power5V         9      14
22 GND             11     15
23 */
24
25 #include <SoftwareWire.h>
26 extern SoftwareWire Wire; // defined in RTC
27
28 #define BYTE HEX
29
30 #define LED2 8
31 #define LED18 9
32
33 int i; 
34 int inByte = 0;         // incoming serial byte
35 int count = 0; 
36 char edidarray[128];
37
38 int edid_setup()
39 {
40   Wire.setClock(100000L); // 100000-400000
41   Wire.begin();        // join i2c bus (address optional for master)
42   // Serial.begin(115200);  // start serial for output
43   
44   //write to vclk pin on pin 2 
45   pinMode(LED2, OUTPUT);  
46   pinMode(LED18, OUTPUT);  
47   
48   //This puts i2c eeproms that are also clocked off the vsync into R/W mode
49   digitalWrite(LED18, HIGH);    // set the LED off
50   for(i = 0; i < 20; i++){
51     digitalWrite(LED2, HIGH);   // set the LED on
52     delay(10);                  // wait for a second
53     digitalWrite(LED2, LOW);    // set the LED off
54     delay(10);
55   }
56   digitalWrite(LED2, HIGH);   // set the LED on
57   delay(10);
58   digitalWrite(LED18, LOW);    // set the LED off
59   delay(10);
60   digitalWrite(LED2, LOW);   // set the LED on
61   
62   for(i = 0; i < 10; i++){
63     digitalWrite(LED2, HIGH);   // set the LED on
64     delay(10);                  // wait for a second
65     digitalWrite(LED2, LOW);    // set the LED off
66     delay(10);
67   }
68   //disable writing to eeprom 
69   digitalWrite(LED2, LOW);    // set the LED off
70   Serial.println("EDID Reader Writer C 2011 Kieran Levin kiram9@yahoo.com");
71 }
72
73 int edidreadbytes(char * array)
74 {
75   byte mycount = 0; 
76       Wire.beginTransmission(B1010000);
77       Wire.write(byte(mycount));
78       Wire.endTransmission();
79   for(int j = 0; j < 128; j++){
80      Wire.requestFrom(B1010000, 1);
81      if (Wire.available())    // slave may send less than requested
82      { 
83        array[j] = Wire.read(); // receive a byte as character
84        mycount++;
85      }
86      delay(10);
87   }
88  return mycount; 
89 }
90 int edidwritebytes(char * array){
91   byte mycount = 0;
92   digitalWrite(LED2, HIGH);    // set the LED off
93   delay(10);
94   for(byte j =0; j < 128;j++){
95       Wire.beginTransmission(B1010000);
96       #if 1
97       Wire.write(byte(j));
98       Wire.write(byte(array[j]));
99       Wire.endTransmission();
100       #endif
101      delay(10);
102   }
103  digitalWrite(LED2, LOW);    // set the LED off
104  return mycount; 
105 }
106 void printDetailedinfo(){
107 Serial.println("Detailed Information:");
108 Serial.print("\tManufacture ID \t");
109 Serial.println(*((uint16_t *)(edidarray+8)));
110 Serial.print("\tProduct ID code \t");
111 Serial.println(*((uint16_t *)(edidarray+10)));
112 Serial.print("\tSerial Number \t");
113 Serial.println(*((uint32_t *)(edidarray+12)));
114 Serial.print("\tWeek of Manufacture \t");
115 Serial.println(edidarray[16]);
116 Serial.print("\tYear of manufacture \t");
117 Serial.println(edidarray[17] + 1990);
118 Serial.print("\tEdid Version\t");
119 Serial.println(edidarray[18]);
120 Serial.print("\tEdid Revision \t");
121 Serial.println(edidarray[19]);
122 Serial.print("\tEstablished Timing 1 \t");
123 Serial.println(0x00FF & edidarray[35],BIN);
124 Serial.print("\tEstablished Timing 2 \t");
125 Serial.println(0x00FF & edidarray[36],BIN);
126   for(int i = 38; i < 53; i +=2){
127     Serial.print("Standard Timing ID \t");
128     Serial.println(i);
129     Serial.print("\tHoriz Res \t");
130     Serial.println(248+8*(0x00FF & edidarray[i]));
131     Serial.print("\tAspect Ratio \t");
132       switch(B11000000 & edidarray[i+1]){
133        case B00000000: 
134          if (edidarray[19] < 3)
135          Serial.println("1:1");
136          else
137          Serial.println("16:10");
138         break;
139        case B01000000: 
140         Serial.println("4:3");
141         break;
142        case B10000000: 
143         Serial.println("5:4");
144         break;
145        case B11000000: 
146         Serial.println("16:9");
147         break;
148       }
149     Serial.print("\tVertical Freq \t");  
150     Serial.println(60+(B00111111 & edidarray[i+1]));
151   }
152 }
153 byte edidchecksum(char * array){
154   byte checksum = 0; 
155   for (int i = 0; i < 128; i++){
156    checksum += array[i];
157   }
158   return(checksum);
159 }
160 void checkchecksum(){
161   byte checksum = 0; 
162   for (int i = 0; i < 128; i++){
163    checksum += edidarray[i];
164   }
165   Serial.print("\tChecksum (should = 0) \t");
166   Serial.println(checksum,HEX);
167    
168 }
169 void generatechecksum(){
170   Serial.println("Generating Checksum");
171   byte checksum = 0; 
172   for (int i = 0; i < 127; i++){
173    checksum += edidarray[i];
174   }
175   edidarray[127] = 256 - checksum;
176   checkchecksum();
177   
178 }
179 void edid_loop()
180 {
181   
182   if (Serial.available() > 0) {
183     // get incoming byte:
184     inByte = Serial.read();
185     switch (inByte){
186     
187       case 'r':  
188       count = 0; 
189         Serial.println("EEPROM:");
190         count = edidreadbytes(edidarray);
191         for (i = 0; i < count; i++){
192           Serial.print(edidarray[i],HEX);
193           Serial.print(',');
194           delay(1);
195         }
196         Serial.println();
197         Serial.print("Read ");
198         Serial.print(count);
199         Serial.println(" bytes");
200       break; 
201       case 'b': 
202       count = 0; 
203         Serial.println("EEPROM:");
204         count = edidreadbytes(edidarray);
205         for (i = 0; i < count; i++){
206           Serial.print(edidarray[i],BYTE);
207           delay(1);
208         }
209         Serial.println();
210         Serial.print("Read ");
211         Serial.print(count);
212         Serial.println(" bytes");
213        break;
214        case 'l':
215          count = 0; 
216          while (count < 128){
217              if (Serial.available() > 0) {
218                 // get incoming byte:
219                 edidarray[count] = Serial.read();
220                 count++;
221              }
222          }
223          Serial.println("Done Loading Bytes press W to write to eeprom ");
224        break; 
225        case 'w':
226          edidwritebytes(edidarray);
227          Serial.println("Wrote 128 Bytes to eeprom ");
228        break; 
229        case 'd': 
230          Serial.println("Contents of buffer");
231         for (i = 0; i < 128; i++){
232           Serial.print(edidarray[i]);
233           delay(1);
234         }
235        break; 
236        case 'e':
237          printDetailedinfo();
238        break;
239       case 'g':
240          generatechecksum();
241        break;
242        case 't':
243          checkchecksum();
244        break;
245       case 'c':
246         /******************************************************/
247         //Change custom bytes here  
248         edidarray[0] = 0;
249         //edidarray[49] = edidarray[49] | B11000000; //change aspect ratio to 16x9
250         edidarray[48] = 139;
251         edidarray[49] = 0 | B11000000; //change aspect ratio to 16x9 60hz resolution 
252         //Add on a few more custom resolutions here 
253         edidarray[50] = 129;
254         edidarray[51] = 12 | B11000000; //change aspect ratio to 16x9 72hz resolution 
255         edidarray[52] = 129; // change resolution to 1368
256         edidarray[53] = 0 | B11000000; //change aspect ratio to 16x9 60hz resolution         
257        break;
258       case 'h':
259 //      Serial.println("Help:\n\tr=read hex\n\tb=read binary\n\tl=load binary from serial(gets 128 bytes)\n\tw=write\n\td=displaybuffer(binary)\n\te=print detailed info\n\tg=generatechecksum\n\tt=check checksum\n\tc=change custom bytes (compiled in)");
260        break;
261     
262        default: 
263        break; 
264     }
265   }
266 }
267
268 int edid_read(char *a)
269 {
270   int ret = 0;
271
272   for(byte i = 0; i < 128; i++)
273     edidarray[i] = i;
274   int edid_count = edidreadbytes(edidarray);
275   byte edid_checksum = edidchecksum(edidarray);
276   sprintf(a, "EDID EEPROM:%d CRC=%02x %s\n",
277     edid_count,
278     edid_checksum,
279     edid_checksum == 0 ? "OK" : "FAIL"
280   );
281   if ( edid_checksum == 0 ) ret++;
282   return ret;
283 }