main.c

来自「显示屏驱动源代码」· C语言 代码 · 共 667 行

C
667
字号
/*****************************************************************************/
/*                                                                           */
/*                    TW880x Demo Board   Techwell,Inc                       */
/*                                                                           */
/* CPU        : Winbond 78E62BP-40                                           */
/* LANGUAGE   : Tasking C / Keil C                                           */
/* PROGRAMMER : Jooyeon Lee / Harry Han                                      */
/*                                                                           */
/*****************************************************************************/
/* See 'Release.txt' for firmware revision history                           */

#include "Config.h"

#include "reg.h"
#include "typedefs.h"
#include "main.h"
#include "tw88.h"
#include "i2c.h"
#include "adc.h"
#include "osdbasic.h"
#include "dispinfo.h"
#include "etc_eep.h"
#include "measure.h"
#include "Printf.h"
#include "audio.h"
#include "osdmenu.h"
#include "pc_eep.h"
#include "KeyRemo.h"
#include "Monitor.h"
#include "CPU.h"

#include "rgbmix.h"			
#include "panel.h"
#ifdef SUPPORT_TV
#include "Tuner.h"
#endif
#include "measure.h" // Hans


void Loader(BYTE);

#ifdef SUPPORT_GAMMA
extern CODE BYTE GammaRed[];
extern CODE BYTE GammaBlue[];
extern CODE BYTE GammaGreen[];
#endif

extern  DATA  WORD  tic_pc;

extern  IDATA BYTE    cSourceState; // Hans

		IDATA BYTE  InputSelection;
//		IDATA BYTE	InputSelectionInx = 0;
///	    bit	  PcDetectFlag = 1;
//		bit	  PowerMode = ON;
		IDATA WORD  IVF;
		IDATA DWORD	IHF;
IDATA BYTE  VInputStd;

		bit   Range4Coarse=0, AutoDetect=1;
		BYTE  VInputStdDetectMode;
		BYTE  CheckBuf;

CODE struct struct_IdName	struct_InputSelection[]={
	{ UNKNOWN			,	"" },				//don't remove or change this.
	{ COMPOSITE			,	"CVBS" },			//don't remove or change this.

												// you can change the order of the followings
	#ifdef SUPPORT_SVIDEO
	{ SVIDEO			,	"S-Video" },
	#endif

	#ifdef SUPPORT_COMPONENT
	{ COMPONENT	,			"Component" },
	#endif

	#ifdef SUPPORT_DTV
	{ DTV	,				"DTV-SOG" },
	#endif

	#ifdef SUPPORT_TV
	{ TV				,	"TV       " },
	#endif

	#ifdef SUPPORT_PC
	{ PC				,	"PC-HV"},
	#endif

	#ifdef SUPPORT_DIGITALVGA
	{ DIGITALVGA		,	"DVI" },
	#endif

	{0					,	""},				//don't remove or change this.
};


CODE struct struct_IdName struct_VInputStd[]={
	{ UNKNOWN			,	"" },				//don't remove or change this.
	{ NTSC,					"NTSC"},			//don't remove or change this.

	#ifdef SUPPORT_PAL
	{ PAL,					"PAL"},
	#endif

	#ifdef SUPPORT_SECAM
	{ SECAM,				"SECAM"},
	#endif

	#ifdef SUPPORT_PALM
	{ PALM,					"PALM"},
	#endif

	#ifdef SUPPORT_PALN
	{ PALN,					"PALN"},
	#endif

	#ifdef SUPPORT_PAL60
	{ PAL60,				"PAL60"},
	#endif

	#ifdef SUPPORT_NTSC4
	{ NTSC4,				"NTSC4.43"},
	#endif

	{0					,	""},				//don't remove or change this.
};



//===================== OSD ===================================================
	    WORD  OSDLastKeyInTime;		// in ms
//===================== Button Key ============================================

//================= Etc. ======================================================
#ifdef WIDE_SCREEN
		BYTE  WideScreenMode;
#endif
		BYTE	DebugLevel;

		bit   Flag4AutoPanelRegs = 0;
		bit   I2CAutoIncFlagOn = 0;
#ifdef SUPPORT_COMPONENT
	    IDATA BYTE  ComponentMode;
#endif
//extern  bit		    OnChangingValue;
extern  BYTE	TVInputSel;
#ifdef SUPPORT_DEBUG
extern  bit Debug_On;
#endif

#ifdef NO_INITIALIZE
		bit		NoInitAccess=0;
#endif
#ifdef INTERNAL_MCU
extern WORD	TW88IRQ;
#endif

	    bit   DisplayInputHold = 0;


//////////////////////////////////////////////////////////////////////////////

//=============================================================================
//		Prompt				                                               
//=============================================================================
/*
void CheckDipSwitch(void)
{
	static bit BW_STATUS=0, EDGE_STATUS=0;
			//----- Check DIP Switch ---------------

			if( EDGE_STATUS != DIP_EDGE_ENHANCE ) {
				if( DIP_EDGE_ENHANCE==0 ) {
					EDGE_STATUS = 0;
					WriteTW88(0xdd, ReadTW88(0xdd) | 0x80);
					Printf("\r\nEnable Color Enhancement");
				}
				else {
					EDGE_STATUS = 1;
					WriteTW88(0xdd, ReadTW88(0xdd) & 0x7f);
					Printf("\r\nDisable Color Enhancement");
				}
			}
			
			if( BW_STATUS != DIP_BW_STRETCH ) {
				if( DIP_BW_STRETCH==0 ) {
					BW_STATUS = 0;
					#ifdef WQVGA
					WriteTW88(0x86, 0x0c);
					WriteTW88(0x87, 0xff);
					#else
					WriteTW88(0x86, 0x20);
					WriteTW88(0x87, 0xe0);
					#endif
					Printf("\r\nEnable Black/White Stretch");
				}
				else {
					BW_STATUS = 1;
					WriteTW88(0x86, 0x00);
					WriteTW88(0x87, 0xff);
					Printf("\r\nDisable Black/White Stretch");
				}
			}

}*/

#ifdef SUPPORT_GAMMA

#include "Gamma.c"

void DownLoadGamma(void)
{
		// Red
		WriteI2C(TW88I2CAddress, 0xf0, 0xe8|1 ); 
		WriteI2C(TW88I2CAddress, 0xf1, 0x00); //  Start Address
		WriteI2C(TW88I2CAddress, 0xf2, GammaRed[0]); //  
		WriteI2Cn(TW88I2CAddress, 0xf2, &GammaRed[1], 255);
		// Blue
		WriteI2C(TW88I2CAddress, 0xf0, 0xe8|2 ); 
		WriteI2C(TW88I2CAddress, 0xf1, 0x00); //  Start Address
		WriteI2C(TW88I2CAddress, 0xf2, GammaGreen[0]); //  
		WriteI2Cn(TW88I2CAddress, 0xf2, &GammaGreen[1], 255);
		// Red
		WriteI2C(TW88I2CAddress, 0xf0, 0xe8|3 ); 
		WriteI2C(TW88I2CAddress, 0xf1, 0x00); //  Start Address
		WriteI2C(TW88I2CAddress, 0xf2, GammaBlue[0]); //  
		WriteI2Cn(TW88I2CAddress, 0xf2, &GammaBlue[1], 255);

}
#endif

void I2CDeviceInitialize( CODE_P BYTE *RegSet)
{
	int	cnt=0;
	BYTE addr, index, val;

	addr = *RegSet;
	#ifdef DEBUG_TW88
	dPrintf("\r\nI2C address : %02x", (WORD)addr);
	#endif
	cnt = *(RegSet+1);
	RegSet+=2;

	while (( RegSet[0] != 0xFF ) || ( RegSet[1]!= 0xFF )) {			// 0xff, 0xff is end of data
		index = *RegSet;
		val = *(RegSet+1);
		WriteI2C(addr, index, val);

		#ifdef DEBUG_TW88
		dPrintf("\r\n    addr=%02x  index=%02x   val=%02x", (WORD)addr, (WORD)index, (WORD)val );
		#endif

		RegSet+=2;
	}
	WriteTW88(0xff, 0x00);		// set page 0
}

extern CODE struct RegisterInfo UserRange;
extern CODE struct RegisterInfo VideoContrastRange;
extern CODE struct RegisterInfo VideoBrightnessRange;
extern CODE struct RegisterInfo VideoSaturationRange;
extern CODE struct RegisterInfo VideoHueRange;
extern CODE struct RegisterInfo VideoSharpnessRange;
extern CODE struct RegisterInfo DigitalVideoSaturationRange;

#ifdef ADD_ANALOGPANEL
BYTE IsAnalogOn(void)
{
	if(DIP_PANEL_SWITCH==0) return 1;
	else return 0;
}
#endif

/*********************************/
BYTE IsNoInput(void)
{
	if( ReadDecoder(CSTATUS) & 0x80 ) return 1;
	return 0;
}

#ifdef ID_CHECK_BY_FW
BYTE Is50Hz(void)
{
	if( ReadDecoder(CSTATUS) & 0x01 ) return 1;
	return 0;
}

BYTE Slock(void)
{
	if( ReadDecoder(CSTATUS) & 0x20 ) return 1;
	return 0;
}
#endif	// ID_CHECK_BY_FW

BYTE ReadVInputSTD(void)
{
	BYTE std;
	
	if( IsNoInput() ) return 1; // Noinput!!
	
	std = ReadDecoder(0x1c) & 0xf0;
	if( std & 0x80 ) return 1; // Detection in progress..
	else 
		return ((( std & 0x70 ) >> 4 ) + 1 );
}

BYTE GetVInputStdInx(void)
{
	BYTE i, std;

	std = ReadVInputSTD();

	switch( std ) {

	case NTSC4:
	case PALM:
	case PAL60:
	case NTSC:		IVF = 60;		IHF = 15723;	break;	// 15734

	case SECAM:
	case PALN:
	case PAL:		IVF = 50;		IHF = 15723;	break;  // 15625
	default:		IVF = 0;		IHF = 0;		break;
	}

	for(i=0; ; i++) {
		if( struct_VInputStd[i].Id ==std )
			return i;
		if( struct_VInputStd[i].Id ==0 )
			break;
	}
	return 0;
}

void SetVInputStd(BYTE newd)
{
	VInputStd = newd;

	#ifdef DEBUG
	ePrintf("\r\n(SetVInputStd) VInputStd:(%s)", struct_VInputStd[GetVInputStdInx()].Name);
	#endif

	switch( VInputStd ) {

	#ifdef SUPPORT_NTSC4
	case NTSC4:
	#endif
	#ifdef SUPPORT_PALM
	case PALM:
	#endif
	#ifdef SUPPORT_PAL60
	case PAL60:
	#endif
	case NTSC:		IVF = 60;		IHF = 15723;	break;	// 15734

	#ifdef SUPPORT_SECAM
	case SECAM:
	#endif
	#ifdef SUPPORT_PALN
	case PALN:
	#endif
	#ifdef SUPPORT_PAL
	case PAL:		IVF = 50;		IHF = 15723;	break;  // 15625
	#endif

	default:		IVF = 0;		IHF = 0;		break;
	}
}

BYTE GetInputSelection(void)
{
	return InputSelection;
}

BYTE GetNextInputSelection(void)
{
	BYTE i;

	for (i=1; ;i++)
		if( struct_InputSelection[i].Id==InputSelection )  break;
	i++;
	if( struct_InputSelection[i].Id == 0 ) i=1;

	return struct_InputSelection[i].Id;
}

void PowerDown_XTAL(bit flag)
{
	if( flag ) P3_7 = 1;
	else      P3_7 = 0;
}

void ChangeInput(BYTE newsel)
{
	#ifdef DEBUG
	BYTE i;
	#endif

	if( InputSelection==newsel ) return;

	DisplayInputHold = 0; // Refresh Input info.
	ClearOSDInfo();

	//PanelMute(1);

	InputSelection = newsel;

       cSourceState = ssInputSetup; // start input setup

	#ifdef DEBUG
	for (i=1; ;i++)
		if( struct_InputSelection[i].Id==InputSelection )  break;
	Printf("\r\n ->->->->->->->->->->-> Changed Input: %s(%02x)", struct_InputSelection[i].Name, (WORD)InputSelection);
	#endif

}

void PowerOff(void)
{
	dPuts("\r\n++(PowerOff)__");
//	PowerMode = OFF;

	AudioOff();
	
	ClearOSDInfo();
	LCDPowerOFF();
	PowerLED(OFF);
}

void WaitPowerOn(void)
{

	BYTE ikey;
	BYTE AutoKey, _RemoDataCode;

	while(1) {
		if( (ikey = GetKey(0)) ) {
			#ifdef POWERKEY
			if( ikey==POWERKEY ) 
				break;		//poweron
			#endif
		}
		if( IsRemoDataReady(&_RemoDataCode, &AutoKey) )	{
			if( _RemoDataCode==REMO_STANDBY && !AutoKey )		//POWERON
				break;		//poweron
		}

	}

	#ifdef DEBUG_MAIN
	dPuts("\r\n++(PowerOn)__");
	#endif
//	PowerMode = ON;
	PowerLED(ON);

	AudioOn();			//SetMSPVol();
	LCDPowerON(0);

}

void SetOSDLastKeyInTime(void)
{
	OSDLastKeyInTime = GetTime_ms();
}

WORD GetOSDLastKeyInTime(void)
{
	return OSDLastKeyInTime ;
}

//=============================================================================
//
//=============================================================================
#ifdef SUPPORT_PATTERN
CODE BYTE PATTERN[7][] = {
	{TW88I2CAddress,7, 0x61,0x5f, 0x64,0xff, 0x65,0x00, 0x66,0x00, 0x67,0xff, 0x68,0x00, 0x69,0x00, 0xff,0xff},	// Red
	{TW88I2CAddress,7, 0x61,0x5f, 0x64,0x00, 0x65,0xff, 0x66,0x00, 0x67,0x00, 0x68,0xff, 0x69,0x00, 0xff,0xff},	// Green
	{TW88I2CAddress,7, 0x61,0x5f, 0x64,0x00, 0x65,0x00, 0x66,0xff, 0x67,0x00, 0x68,0x00, 0x69,0xff, 0xff,0xff},	// Blue
	{TW88I2CAddress,7, 0x61,0x5f, 0x64,0xff, 0x65,0xff, 0x66,0xff, 0x67,0xff, 0x68,0xff, 0x69,0xff, 0xff,0xff},	// White
	{TW88I2CAddress,7, 0x61,0x5f, 0x64,0x00, 0x65,0x00, 0x66,0x00, 0x67,0xff, 0x68,0xff, 0x69,0xff, 0xff,0xff},	// Gray
	{TW88I2CAddress,7, 0x61,0x5f, 0x64,0x00, 0x65,0x00, 0x66,0x00, 0x67,0x00, 0x68,0x00, 0x69,0x00, 0xff,0xff},	// Black
	{TW88I2CAddress,7, 0x61,0x00, 0x64,0xc0, 0x65,0xc0, 0x66,0xc0, 0x67,0x88, 0x68,0x88, 0x69,0x88, 0xff,0xff}	// Gray scale
};
#endif // SUPPORT_PATTERN


#ifdef SUPPORT_KRS_OSDDEMO
#include "udfont_Krs.c"
#include "OsdInitTable_KRS.c"

#define MaxFontUdFont	174
#define COLORFONT_START	0x3a

void DownloadUDFont_Only(void)
{
	BYTE i;

	WriteTW88(TW88_MULTISTART, COLORFONT_START); // Set Color Font Start Address. 

	for(i=0; i< NO_UDFONT && i<= MaxFontUdFont ; i++) {
		DownloadFont(i, RAMFONTDATA_KRS[i]);
	}
}

void Change_OSDColorLookup_KRS(void)
{
	BYTE i,j,dt;
	for(j=0; j<2; j++) {   
		for(i=0; i<8; i++) {
			dt = ReadTW88(0x9c);
			dt &= 0xf0;
			WriteTW88(0x9c, i+j*8);
			WriteTW88(0x9d, OSD_Color_LookUp_Data_Table_KRS[j][i]);
		}
	}
}

#endif
		
void main_loop(void)
{
	#ifdef INTERNAL_MCU
	WORD	i;
	#endif

	//---------------------------------------------------------------
	//			             Main Loop
	//---------------------------------------------------------------
	while(1) {
		
	  #ifdef INTERNAL_MCU
		#ifdef DEBUG_MCU
		WriteTW88(0x16, resetCount);
		#endif
		if (POWER_DOWN == 1) {
			Puts("\r\nGot signal to Power Down!!! wait for some times.....");
			for ( i=0; i<0xFF0; i++ ) ;
			if ( POWER_DOWN == 1 ) {
				Puts("\r\n Entering Stop mode.....");
				PowerDown();
			}
			else
				Puts("\r\nGlitch on Power Down signal");
		}
		
		if ( TW88IRQ ) {		// it means EXT3_INT, TW88 IRQ has occured
			Printf("\r\nTW88 IRQ occur.....: 0x%4x", TW88IRQ);
			TW88IRQ = 0;
		}
	  #else
		//while(TestStop ) ; // Test By RYU.
		if( (P4 & 0x02)==0 ) {
			Printf("\r\nDisable main Micom I2C");
			while((P4 & 0x02)==0);
			Printf("\r\nEnable Main Micom I2C");
		}
	  #endif

		//-------------- Check Serial Port -----------------
		#ifdef SERIAL
		while( RS_ready() ) 
			Monitor();				// for new monitor functions
			//DebugKeyIn( RS_rx() );	// Input - Serial Port

			#ifndef INTERNAL_MCU
			//-------------- Check No Initial text mode -----------------
			#ifdef NO_INITIALIZE
			if( NoInitAccess )	 // No Initialize
			{
				if( P0_3 == 1 ) 
				{	
					NoInitAccess =0;
					Printf("\r\n Recovered normal mode from no-initialize Test mode ..\n");
				}
				else
					continue;
			}
			#endif	// NO_INITIALIZE
			#endif //  INTERNAL_MCU
		#endif // SERIAL

		//-------------- Check Key in --------------------------
		if( !CheckKeyIn() ) break;			// POWEROFF

		//-------------- Check Remote Controller ---------------
		if( !CheckRemo() ) break;			// POWEROFF
			
		//-------------- Check OSD timer -----------------------
		#ifdef DEBUG_OSD				//ljy012604
		if(DebugLevel>=DEBUG_INFO)
			;
		else
		#endif
		CheckAndClearOSD();

		//-------------- Check OFF timer -----------------------
		if( OKSleepTime() ) break;

              SourceProcess(); // Hans

	}

}

void main(void)
{
	TW88HWReset = 0;			//TW88 HW reset by port pin, internal MCU no meaning
	//P4=0x0f;

	InitCPU();

	SetMonAddress(TW88I2CAddress);

	InitVars();

	InitTechwell();

	while(1) {
		main_loop();		//exit when power off
		
		PowerOff();
		WaitPowerOn();
		ChangeInput( GetInputSelectionEE() );
	}
}

#if defined SUPPORT_TV
BYTE WantToStopTVScan(void)
{
	BYTE AutoKey=0, ret=0, _RemoDataCode=0;

	ret = GetKey(1); 

	if( ret == MENUKEY ) {
		//Printf("\r\n(WantToStopTVScan) Pushed Menu Key!!");
		return 1;
	}

	if( !ret )
		ret = IsRemoDataReady(&_RemoDataCode, &AutoKey);

	if( _RemoDataCode==REMO_MENU  )	ret = 1;
	else	ret = 0;

	#if defined DEBUG_KEYREMO || defined DEBUG_TV
	dPrintf("\r\n(WantToStopTVScan) ret:0x%x, RemoDataCode:0x%x", (WORD)ret, (WORD)_RemoDataCode);
	#endif

	return ret;
}

void ChangeVInputStdDetectMode(BYTE val)
{
	VInputStdDetectMode = val;
	SaveVInputStdDetectModeEE(val);
	
	if( VInputStdDetectMode ==AUTO )	
		SetAutoDetectStd();
	else {
		SetVInputStd(val);
		ClearAutoDetectStd(val-1);
//		InitVideoData( val );			
	}	
}
#endif		//SUPPORT_TV

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?