⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpu.c

📁 车载DVD osdIC TW8816原厂代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* CPU.c */

#include "config.h"

#include "typedefs.h"
#include "reg.h"
#include "regmap.h"
#include "cpu.h"
#include "main.h"
#include "I2C.h"
#include "TW88.h"
#include "Printf.h"
#include "KeyRemo.h"
#include "etc_eep.h"
#include "pc_eep.h"
#include "Audio.h"
#include "Measure.h"
#include "OSDBasic.h"
#include "OSDMenu.h"
#include "DispInfo.h"
#include "Monitor.h"
#ifdef SUPPORT_TV
#include "Tuner.h"
#endif

#define ClearRemoTimer()	TR2 = 0


extern		  BYTE	DebugLevel;
extern  IDATA BYTE	RemoDataReady,RemoSystemCode, RemoDataCode;
extern 	IDATA BYTE InputSelection;
extern		  BYTE  VInputStdDetectMode;
extern	IDATA BYTE	PcMode;
extern		  bit	AutoDetect;
extern 		  bit	NoInitAccess;

//===================== Timer =================================================
		DATA BYTE keytic=0;
		DATA BYTE Key=0;
		bit KeyReady=0, RepeatKey = 0;

		DATA BYTE tm001;
		DATA  WORD  tm01=0;
		DATA  BYTE  tic01=0;	//, tic_pc;
		DATA  WORD  tic_pc=0;

static  DATA DWORD	SystemClock;			//00:00
static  DATA DWORD	LastBlockedTime;		//00:00
//static  DATA WORD	WakeupTime;
//static  DATA BYTE	WakeupPR;
static  DATA WORD	OffTime;
static  DATA BYTE	SleepTimer;
static  DATA WORD	SleepTime;
static  DATA BYTE  cDelayCounter; // Hans


#ifdef SERIAL //=======================================================================

#define BUF_MAX 	4//6
static	DATA BYTE	RS_buf[BUF_MAX];
static	DATA BYTE   RS_in, RS_out;
		bit	        RS_Xbusy=0;			// bit RS_Xbusy=0;

#endif	// SERIAL ======================================================================
//================== Remocon ==================================================
#ifdef REMO_RC5

static	bit         RemoPhase1, RemoPhase2;
		IDATA BYTE	RemoDataReady=0;
		IDATA BYTE  RemoSystemCode, RemoDataCode;

#elif defined REMO_NEC

static  bit         RemoPhase=0;
static  DATA	BYTE  RemoStep=0;
static  DATA	BYTE  RemoHcnt, RemoLcnt;
		DATA	BYTE	 RemoData[4];
	    IDATA BYTE  RemoDataReady=0;
static  IDATA BYTE  RemoNum, RemoBit;

	    IDATA BYTE  RemoDataCode=0xff;
#endif	//================== Remocon ======================

CODE BYTE PanelInfoStr[]={
#if (defined UXGA)						// 1600 x 1200
	"---> Panel: UXGA-1600*1200"
#elif (defined SXGA)    					// 1280 x 1024
	"---> Panel: SXGA-1280*1024"
#elif (defined  XGA)  						// 1024 x 768
	"---> Panel: XGA-1024*768"
#elif (defined SVGA)						//  800 x 600
	"---> Panel: SVGA-800*600"
#elif (defined VGA)  						//  640 x 480
	"---> Panel: VGA-640*480"
#elif (defined WXGA)						// 1280 x 768 (15:9) // 1366 x 768 (16:9)
	"---> Panel: WXGA-1280*768(15:9) or 1366*768(16:9)"
#elif (defined WSVGA)						// 1024 x 600	*** only video mode
	"---> Panel: WSVGA-1024*600"
#elif (defined WVGA)						//  800 x 480
	#ifdef AU_10INCH
	"---> Panel: WVGA-800*480 -- TTL AU 10inch"
	#elif (defined SAMSUNG_10INCH)
	"---> Panel: WVGA-800*480 -- TTL SAMSUNG 10inch"
	#elif (defined SAMSUNG_TICONLESS_10INCH)
	"---> Panel: WVGA-800*480 -- TICONLESS SAMSUNG 10inch"
	#elif (defined SHARP_TICONLESS_7INCH)
	"---> Panel: WVGA-800*480 -- TICONLESS SHARP 7inch"
	#elif (defined AU_TICONLESS_7INCH)
	"---> Panel: WVGA-800*480 -- TICONLESS AU 7inch"
	#elif (defined AU_TICONLESS_10INCH)
	"---> Panel: WVGA-800*480 -- TICONLESS AU 10inch"
	#elif defined HITACHI_T01
	"---> Panel: WVGA-800*480 -- TICONLESS Hitachi TX18D24VM2BAA"
	#elif defined HITACHI_TX23D12
	"---> Panel: WVGA-800*480 -- TICONLESS Hitachi TX23D12"
	#elif defined HITACHI_TX18D24
	"---> Panel: WVGA-800*480 -- TICONLESS Hitachi TX18D24"
	#else
	"---> Panel: WVGA-800*480"
	#endif
#elif (defined HVGA)						//  480 x 320
	"---> Panel: HVGA-480*320"
#elif (defined QVGA)						//  320 x 240	*** only video mode
	"---> Panel: QVGA-320*240"
#elif (defined WQVGA)						//  480 x 234	*** only video mode
    #ifdef ANALOG_7INCH
	"---> Panel: WQVGA-480*234 -- ANALOG TICONLESS AU 7inch"
	#elif (defined SHARP_LQ043T3DX02)
	"---> Panel: WQVGA-480*234 -- TTL SHARP LED 4.3inch panel"
	#else
	"---> Panel: WQVGA-480*234 "
	#endif
#endif
};

#ifdef ADD_ANALOGPANEL		// Added Analog panel(AU7") from default set panel(expect WQVGA) with DIP #1 switch.(DIP#1=L:ANALOG, H:DEFAULT)
CODE BYTE AddedAnalogPanelStr[]={
		#ifdef ADD_ANALOG_7INCH
	"---> Added Analog Panel(AU 7inch) with DIP SW1=Enabled"
		#elif defined ADD_TMD_LTA05B352A
	"---> Added Analog Panel(TMD 4inch) with DIP SW1=Enabled"
		#endif
};
#endif




//=============================================================================
//                            CheckSpecialMode                                                   
//=============================================================================
void CheckSpecialMode(void)
{
	BYTE i;

	i = GetKey(0);

	switch( i ) {
	case MENUKEY:
		Puts("\r\n---- Menu Key is pressed -> Enter Special Mode( To Composite )");
		SaveInputSelectionEE(COMPOSITE);
		DebugLevel = 0;
///		PcDetectFlag = 0;
		break;
	}
}

//=============================================================================
//    Main Initialize Routine
//=============================================================================
void main_init (void)
{
	extern CODE BYTE *OutOfRangeStr[] ;

#ifdef SUPPORT_MSP_AUDIO
	BYTE	ikey;
#endif
	WORD	rev;
	#ifdef SUPPORT_PC
	BYTE	i;
	#endif

//	WriteTW88(0xff, 0x10);	// Disable I2C auto increase mode
	WriteTW88(0xe0, 0x10);	// Disable I2C auto increase mode

	PowerLED(ON);

	EnableRemoconInt();

#ifdef SUPPORT_MSP_AUDIO
	ikey = ResetMSP();
	if( ikey ) {
		#ifdef DEBUG
		dPrintf("\r\nError on resetting MSP:0x%02x", (WORD)ikey);
		#endif
	}
#else
	ResetMSP();
#endif

	rev = GetFWRevEE();
	Printf("\r\n New Firmware Base On:%04x", rev);

	if( GetFWRevEE()!=FWVER ) {					// exist EEPROM
		SaveFWRevEE(FWVER);
		Printf("\r\nCurr.FW:%04x", GetFWRevEE());
	}

	//---------- if FW version is not matched, initialize EEPROM data -----------

	if( GetFWRevEE()==FWVER ) {					// exist EEPROM
		if( rev != FWVER ) {					//
			SaveDebugLevelEE(DebugLevel);
			Printf("\r\nNew DebugLevel:%02x", (WORD)DebugLevel);

			SaveInputSelectionEE(COMPOSITE);
			ClearBasicEE();

			#ifdef SUPPORT_PC
			ResetPanelAttributeValue();
			SaveDefaultPCDataAllEE();
			#endif

			#ifdef SUPPORT_TV
			ResetTVValue();
			#endif
		}
		else {
			DebugLevel = GetDebugLevelEE();
			Printf("\r\nSaved DebugLevel:%02x", (WORD)DebugLevel);
		}

		#ifdef SUPPORT_PC
		if( DebugLevel > 0 ) {
			for(i=0; i<EE_PC_MAX+1; i++)
				WriteEEP( EE_PCDATA + LEN_PCDATA*i, 0 );
		}
		#endif
	}

	#ifdef SUPPORT_GAMMA
	DownLoadGamma();
	#endif

	VInputStdDetectMode = GetVInputStdDetectModeEE();
	if( VInputStdDetectMode != AUTO )
		SetVInputStd( VInputStdDetectMode );
	else
		SetVInputStd( NTSC );
	SetPossibleAutoDetectStdEE();

	//InputSelectionInx  = 0;
	InputSelection  = UNKNOWN;		//GetInputSelectionEE();

	#ifdef SUPPORT_TV
	InitTV();
	#endif

	ChangeInput( GetInputSelectionEE() );

	DownloadUDFont();
       DisplayLogo();

	LCDPowerON(1); // Power on but free run
	
	AutoDetect = 1;

	Puts("\n\rDone!!!"); 
	Prompt();


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

//	delay(100);
	WaitDelay(100); // 100 * 10ms  delay
	WriteTW88( STATUS0, 0xff);	// clear status
	WriteTW88( STATUS1, 0xff);	// clear status
	ClearLogo();

}



// ************************  ifdef INTERNAL_MCU *******************************
#ifdef INTERNAL_MCU  
	BYTE	xdata * data regTW88 = REG_START_ADDRESS;

//=============================================================================
//                            Initialize CPU                                                   
//=============================================================================
void InitCPU(void)
{

	SPICONTROL = 0x08;		// slow FLASH access mode
/* CLOCK_11M --------------------------------------------------------------------
	T0HIGH = 1;			// default is 144 but change it to 432 easily makes 0.01s
	T0LOW = 176;
	T1LOW = 144;		// default is 144 for 11.0592MHz makes 4800BPS
---------------------------------------------------------------------------------*/
/* CLOCK_22M ---------------------------------------------------------------------
	T0HIGH = 1;			// ddefault is 144 but change it to 432 easily makes 0.01s
	T0LOW = 176;
	T1LOW = 144;		// default is 144 for 22.1184MHz makes 9600BPS
---------------------------------------------------------------------------------*/
	T0HIGH = 1;			// default is 144 but change it to 432 easily makes 0.01s
	T0LOW = 176;
	T1LOW = 176;		// default is 144 for 11.0592MHz but 176 for 27MHz makes 9600BPS


	/*----- Initialize interrupt -------------*/

	TH1 = 0xff; 		//    SMOD = 0      SMOD =1						
						// 0ffh :57600 bps				
						// 0fdh : 9600 bps	0fdh :19200 bps				
						// 0fah : 4800 bps								
						// 0f4h : 2400 bps								
						// 0e8h : 1200 bps								

	SCON = 0x50;		// 0101 0000 mode 1 - 8 bit UART				
						// Enable serial reception						
//    TMOD = 0x22;		// 0010 0010 timer 0 - 8 bit auto reload		
    TMOD = 0x66;		// 0110 0110 timer 0 - 8 bit auto reload	//by yhbae for synopsys core	
						// timer 1 - baud rate generator				
    TCON = 0x55;		// 0101 0001 timer 0,1 run						
						// int 0,  edge triggered						
						// int 1,  edge triggered						
						// TF1 TR1 TF0 TR0	EI1 IT1 EI0 IT0				

//#if defined CLOCK_11M
//	TH0 = 128;
//#elif defined CLOCK_22M
//	TH0 = 128;		// count 128
//#elif defined CLOCK_27M
	TH0 = 131;		// count 256 - 125 = 131
//#endif

	PCON = PCON | 0x80;		// 1000 0000 SMOD(double baud rate bit) = 1		
	IP	 = 0x02;		// 0000 0000 interrupt priority					
						// -  - PT2 PS PT1 PX1 PT0 PX0	 		         

	#ifdef SERIAL
	IE	 = 0x92;		// 1001 0010 interrupt enable:Serial,TM0		
  						// EA - ET2 ES ET1 EX1 ET0 EX0					

	TI	 = 1;			// LJY000724 // For Starting Serial TX 
	ES   = 1;			// LJY000724
	#else
	IE   = 0x82;
	#endif // SERIAL
//	EIE = 0x0A;			// 0000 1010 : EX5=1, EX3=1;
//	EIE = 0x02;			// 0000 0010 : EX5=0, EX3=1;
	EIE = 0x00;			// disable all interrupts 

	//------------ Timer 2 for Remocon --------------------------------
	T2CON  = 0x00;				// Timer2 Clear
	TR2	   = 0;
	ET2    = 1;
	C_T2   = 1;					// timer source is external T2
	InitForRemo();
	//-----------------------------------------------------------------
}

//INTERRUPT(1, timer0_int)
void timer0_int(void) interrupt 1 using 1			// interrupt number1, bank register 2
{
	
	#ifdef DEBUG_TIMER0
	static BYTE test;
	P2 = test++;
	#endif
	tm001++;
	//---------- 0.01 sec timer ------------
	
//	#ifdef CLOCK_11M
//	if( tm001 > 2 ) {			// BYH 0.01s with T0H 128, T0 divider by 144*3 = 432
//	#elif defined CLOCK_22M
//	if( tm001 > 4 ) {		// BYH 0.01s with T0H 128, T0 divider by 432
//	#elif defined CLOCK_27M
	if( tm001 > 5 ) {		// BYH 0.01s with T0H 125, T0 divider by 432
//	#endif
		tm001 = 0;
		tic01++;
		
		if( tic01==100 ) {				// 1 sec
			SystemClock++;
			tic01 = 0;
		}
		if( tic_pc!=0xffff ) 
			tic_pc++;
		// Hans
		if (cDelayCounter > 0)
			cDelayCounter--;
	}

}

void	PowerDown( void )
{
//	LCDPowerOFF();
//	PowerLED(OFF);
//	WriteTW88(TW88_OSDLOCKUPSEL, (ReadTW88(TW88_OSDLOCKUPSEL)|0x10));	// OSD All Off (TW8806B)
	WriteTW88( MCU_STOP_MODE, (ReadTW88(MCU_STOP_MODE) | 0x80) );	// set bit7 to 1
	PCON = PCON | 2;	// enter stop mode
}

/*****************************************************************************/
/*      Ext Int 3_n Interrupt : implements interrup service                  */
/*****************************************************************************/
WORD	TW88IRQ = 0;

//INTERRUPT(9, ext3_int)
void ext3_int(void) interrupt 9 using 1
{
BYTE	page;

	EXIF = EXIF & 0xDF;		// clear interrupt 3;
	page = ReadTW88(0xff);
	WriteTW88(REG_PAGE, 0);
	TW88IRQ = ReadTW88( STATUS0 );
	TW88IRQ <<= 8;
	TW88IRQ += ReadTW88( STATUS1 );
	WriteTW88( STATUS0, 0xff );		// clear change bits
	WriteTW88( STATUS1, 0xff );		// clear change bits
	WriteTW88(REG_PAGE, page);
	//EIE &= 0xFD;			// disable interrupts
}

/*****************************************************************************/
/*      Ext Int 5_n Interrupt : Power Down sequence                          */
/*****************************************************************************/

//INTERRUPT(11, ext5_int)
void ext5_int(void) interrupt 11 using 1
{
WORD	i;
	EXIF = EXIF & 0x7F;		// clear interrupt 5;
//	EA = 0;				// disable all interrupts
//	EX5 = 0;

//	P2 = 0xf0;
	for ( i=0; i<0xFF0; i++ ) ;		// wait for some time...
	if ( POWER_DOWN == 1 ) {
		PowerDown();
		//PCON = PCON | 2;	// enter stop mode
    }
	else {
//		EA = 1;
	}
}

//------------------------------------------------------------------
// void	InitVars(void) - variable initialize
//------------------------------------------------------------------
void	InitVars(void)
{
	SystemClock=0;			//00:00
	LastBlockedTime=0xffffffff;		//00:00	//ljy010904...CC_FIX_CLEAR_ON_TIME..oops! previous vchip clear error 
	OffTime=0xffff;
	SleepTimer=0;
	SleepTime=0xffff;

	DebugLevel		= 0;
	RemoDataReady	= 0;
}

//------------------------------------------------------------------
// void	InitTechwell(void) - Techwell Decoder initialize
//------------------------------------------------------------------
void	InitTechwell( void )
{
	#ifdef NO_INITIALIZE
	if( ReadKey()==MENUKEY ) {// No Initialize
		NoInitAccess =1;
		Printf("\r\n No-initialize Test going on with MENU KEY pressed ..: %d\n", (WORD)P0_3);
		return;
	}
	#endif
	LCDPowerOFF();
       InitCCFL(); // Hans
       ConfigPanel(); // Hans
       ConfigTCON(); // Hans   
	Puts("\r\n\nDetect ");

	switch( ReadTW88(0) ) {
		case 0x21:	Puts("TW8816");		break;
		case 0x49:	Puts("TW8817");		break;
		default:	Printf("Nothing --0x%02x\r\n", (WORD)ReadDecoder(0)); 
			while(1) {
				#ifdef SERIAL
				if( RS_ready() ) break;
				#endif
				Printf("Check again --0x%02x\r\n", (WORD)ReadDecoder(0));
				delay(99);
				if( ReadTW88(0)==0 ) continue;
				if( ReadTW88(0)==0xff ) continue;
				break;
			}
			delay(99);
			delay(99);
			Printf("Last Check --0x%02x\r\n", (WORD)ReadDecoder(0));
		break;
	}

	#ifdef ADD_ANALOGPANEL
	if(IsAnalogOn())
		Printf("%s \n", AddedAnalogPanelStr);
	else
	#endif	
		Printf("%s \n", PanelInfoStr);

	CheckSpecialMode();
	DebugLevel = 0xff;
	main_init();

}
//=============================================================================
//                            Power LED ON / OFF                                                   
//=============================================================================
void PowerLED(BYTE flag)
{
#define GREEN_LED	P1_7
#define RED_LED		P1_6

	if( flag==ON ) {
		RED_LED = 0;		// ON RED 
		GREEN_LED = 0;		// ON GREEN
		dPuts("\r\n(PowerLED)-ON");
	}
	else {
		RED_LED = 0;		// ON RED
		GREEN_LED = 1;		// OFF GREEN
		dPuts("\r\n(PowerLED)-OFF");
	}
}
#else
//=============================================================================
//                            Initialize WINBOND CPU                                                   
//=============================================================================
void InitCPU(void)
{

	CHPENR = 0x87;		// Enable AUX RAM in Winbond(W78E516B)
	CHPENR = 0x59;		//
	CHPCON = 0x10;		//
	CHPENR = 0x00;		// Write Disable

	/*----- Initialize interrupt -------------*/

	TH1 = 0xff; 		//    SMOD = 0      SMOD =1						
						// 0ffh :57600 bps				
						// 0fdh : 9600 bps	0fdh :19200 bps				
						// 0fah : 4800 bps								
						// 0f4h : 2400 bps								
						// 0e8h : 1200 bps								

	SCON = 0x50;		// 0100 0000 mode 1 - 8 bit UART				
						// Enable serial reception						
    TMOD = 0x22;		// 0010 0010 timer 0 - 8 bit auto reload		
						// timer 1 - baud rate generator				
    TCON = 0x55;		// 0101 0001 timer 0,1 run						
						// int 0,  edge triggered						
						// int 1,  edge triggered						
						// TF1 TR1 TF0 TR0	EI1 IT1 EI0 IT0				
	TH0 = TL0 = 64;		// 64=4608 Hz at 11.0592MHz

	PCON = 0x80;		// 0000 0000 SMOD(double baud rate bit) = 1		
	IP	 = 0x02;		// 0000 0000 interrupt priority					
						// -  - PT2 PS PT1 PX1 PT0 PX0	 		         

	#ifdef SERIAL
	IE	 = 0x92;		// 1001 0010 interrupt enable:Serial,TM0		
  						// EA - ET2 ES ET1 EX1 ET0 EX0					

	TI	 = 1;			// LJY000724 // For Starting Serial TX 
	ES   = 1;			// LJY000724
	#else
	IE   = 0x82;
	#endif // SERIAL

	//------------ Timer 2 for Remocon --------------------------------
	T2CON  = 0x00;				// Timer2 Clear
	TR2	   = 0;
	ET2    = 1;
	//-----------------------------------------------------------------
	

}

#define _ReadKey()		( ((~P4>>2)& 0x01) | (~P1 & 0xfc) )

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

//INTERRUPT(1, timer0_int)
void timer0_int(void) interrupt 1 using 1			// interrupt number1, bank register 2
{

⌨️ 快捷键说明

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