summaryrefslogtreecommitdiff
path: root/edid_injector.c
blob: 454c42e050e626345b88e64c16db735e9e4df582 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
 *
 * \brief  EDID Injector firmware
 *
 * \author
 *      Wolfgang 'datenwolf' Draxinger
 *      Support email: projects+edid_injector@datenwolf.net
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. The name of ATMEL may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
#include "avr_compiler.h"

#include <string.h>

#include "eeprom_driver.h"

#include "twi_master_driver.h"
#include "twi_slave_driver.h"

/*! BAUDRATE 100kHz and Baudrate Register Settings */
#define BAUDRATE	100000
#define TWI_BAUDSETTING TWI_BAUD(F_CPU, BAUDRATE)

#define EDID_SLAVE_ADDRESS 0x50

/* TWIC is on the display side */
TWI_t * const twiDisplay = &TWIC;
TWI_Master_t twimDisplay;    /*!< TWI master module. Used for talking with display */
/*! TWIC Master Interrupt vector. */
ISR(TWIC_TWIM_vect)
{
	TWI_MasterInterruptHandler(&twimDisplay);
}


/* TWIE is on the host side */
TWI_t * const twiHost = &TWIE;
TWI_Slave_t twisHost;      /*!< TWI slave module.  Used for talking with host */
/*! TWIC Slave Interrupt vector. */
ISR(TWIE_TWIS_vect)
{
	TWI_SlaveInterruptHandler(&twisHost);
}

#define EDID_BLOCK_LENGTH 128

static uint8_t edid_data[EDID_BLOCK_LENGTH];
static uint8_t edid_offset;
static uint8_t recvbuf[1];

void TWIC_SlaveProcessData(TWI_Slave_t * const twi)
{
	if( twi->bytesReceived ) {
		edid_offset = twi->recvData[0];
	}
	twi->sendData = edid_data + edid_offset;
	twi->bytesToSend = sizeof(edid_data) - edid_offset;
}

void edid_initHostTWI(void)
{
	/* Initialize TWI slave. */
	TWI_SlaveInitializeDriver(&twisHost,
		twiHost,
		TWIC_SlaveProcessData);

	TWI_SlaveInitializeModule(&twisHost,
		EDID_SLAVE_ADDRESS,
		TWI_SLAVE_INTLVL_HI_gc,
		recvbuf,
		sizeof(recvbuf) );
}

void edid_initDisplayTWI(void)
{
	/* Initialize TWI master. */
	twimDisplay.status = TWIM_STATUS_READY;
	TWI_MasterInit(&twimDisplay,
	               twiDisplay,
	               TWI_MASTER_INTLVL_LO_gc,
	               TWI_BAUDSETTING);

	/* Enable internal pull-up on PC0, PC1. */
	/* If you don't have external pullups enable this */
#if 0
	/* Configure several PINxCTRL registers at the same time */
	PORTCFG.MPCMASK = 0x03; 

	/* Enable pull-up to get a defined level on the switches */
	PORTC.PIN0CTRL = (PORTC.PIN0CTRL & ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc;
#endif

}

uint8_t edid_checkData(uint8_t const *ediddata)
{
	uint8_t checksum = 0;
	for(uint8_t i = 0; i < EDID_BLOCK_LENGTH; i++) {
		checksum += ediddata[i];
	}
	if( 0 != checksum ) {
		return 1; /* checksum failed */
	}

	if( 0 != ediddata[0] ) {
		return 2; /* headerbyte[0] != 0 */
	}

	for(uint8_t i = 1; i < 7; i++) {
		if( 0xff != ediddata[i] ) {
			return 3; /* headerbyte[1..6] != 0xff */
		}
	}

	if( 0 != ediddata[7] ) {
		return 4; /* headerbyte[7] != 0 */
	}

	return 0; /* EDID data passed checks */
}

uint8_t edid_genChecksum(uint8_t *ediddata)
{
	uint8_t bytessum = 0;
	for(uint8_t i = 0; i < EDID_BLOCK_LENGTH-1; i++) {
		bytessum += ediddata[i];
	}

	return 0xff - bytessum + 1;
}

uint8_t edid_readFromDisplay(void)
{
	uint8_t const offset[1] = {0x00};
	uint8_t displayedid[EDID_BLOCK_LENGTH];
	memset(displayedid, 0, sizeof(displayedid));

	uint8_t error = 0;
	uint8_t retry = 20;
	while( retry-- ) {
		error = 0;

		TWI_MasterForceIdle(&twimDisplay);
		delay_ms(1);

		/* set EDID offset to 0 */
		if( !TWI_MasterWriteRead(&twimDisplay,
			EDID_SLAVE_ADDRESS,
			offset,
			sizeof(offset),
			displayedid,
			sizeof(displayedid) ) ) {
			PORTB.OUTCLR = (1<<3);
			PORTB.OUTSET = (1<<3);
		};

		while( !TWI_MasterReady(&twimDisplay) ) {
			delay_ms(1);
		}
		delay_ms(1);

		if( twimDisplay.result != TWIM_RESULT_OK ) {
			error = 1;
			continue;
		}

		if( edid_checkData(displayedid) ) {
			error = 2;
			continue;
		}

		break;
	}
	if( error ) {
		return error;
	}

	if( edid_checkData(displayedid) ) {
		return 3;
	}

	/* set EDID Extension Block count / flags to zero */
	displayedid[126] = 0;

	/* recalculate checksum */
	displayedid[127] = edid_genChecksum(displayedid);

	/* write EDID information to EEPROM */
	memcpy(edid_data, displayedid, EDID_BLOCK_LENGTH);

	return 0;
}

uint8_t edid_readFromEEPROM(void)
{
	uint8_t eepromedid[EDID_BLOCK_LENGTH];
	memset(eepromedid, 0, sizeof(EDID_BLOCK_LENGTH));

	EEPROM_WaitForNVM();
	EEPROM_FlushBuffer();
	EEPROM_DisableMapping();

	for(uint8_t i_page = 0;
	    i_page < EDID_BLOCK_LENGTH / EEPROM_PAGE_SIZE;
	    i_page++ ) {
		for(uint8_t i_b = 0; i_b < EEPROM_PAGE_SIZE; i_b++) {
			eepromedid[i_page * EEPROM_PAGE_SIZE + i_b] =
				EEPROM_ReadByte(i_page, i_b);
		}
	}

	if( edid_checkData(eepromedid) ) {
		/* EDID data read from EEPROM didn't pass sanity checks */
		// memset(edid_data, 0, EDID_BLOCK_LENGTH);
		return 1;
	}

	memcpy(edid_data, eepromedid, EDID_BLOCK_LENGTH);
	return 0;
}

void edid_writeToEEPROM(void)
{
	uint8_t pagebuf[EEPROM_PAGE_SIZE];

	EEPROM_WaitForNVM();
	EEPROM_FlushBuffer();
	EEPROM_DisableMapping();

	for(uint8_t i_page = 0;
	    i_page < EDID_BLOCK_LENGTH / EEPROM_PAGE_SIZE;
	    i_page++ ) {
		for(uint8_t i_b = 0; i_b < EEPROM_PAGE_SIZE; i_b++) {
			pagebuf[i_b] = EEPROM_ReadByte(i_page, i_b);
		}

		if( 0 == memcmp(pagebuf,
		                edid_data + i_page*EEPROM_PAGE_SIZE,
		                EEPROM_PAGE_SIZE )
		) {
			/* page unaltered skip writing it */
			continue;
		}

		memcpy(	pagebuf,
			edid_data + i_page*EEPROM_PAGE_SIZE,
			sizeof(pagebuf) );

		EEPROM_LoadPage( pagebuf );
		EEPROM_AtomicWritePage(i_page);
		EEPROM_WaitForNVM();
	}
}

int main(void)
{
	memset(edid_data, 0, sizeof(edid_data));

	edid_readFromEEPROM();

	edid_initHostTWI();
	edid_initDisplayTWI();

	/* Enable LO interrupt level. */
	PMIC.CTRL |=
		  PMIC_LOLVLEN_bm
		| PMIC_MEDLVLEN_bm
		| PMIC_HILVLEN_bm;
	
	PORTC_PIN2CTRL = PORT_OPC_WIREDANDPULL_gc;
	PORTC_DIRCLR   = _BV(2);
	PORTC_OUTSET   = _BV(2);

	sei();
	delay_ms(20); 

	/* EDID standard requires a host to wait for 20ms after switching +5V
	 * supply to display before performing the first readout attempt.
	 * Since uC supply == display +5V supply we're waiting 20ms here.
	 */
	for(;;) {
		/* Only read a new EDID setting if PC2 is pulled low */
		if( !(PORTC_IN & _BV(2)) ) {
			if( 0 == edid_readFromDisplay() ) {
				edid_writeToEEPROM();
				edid_readFromEEPROM();
			}
		}
		delay_ms(1000); 
	}

	return 0;
}