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

📄 measure.c

📁 此程序为twell8806驱动程序
💻 C
📖 第 1 页 / 共 3 页
字号:

	//------ Set Test Range, Divide Range more detail

	low  = 10;
	high = ConvertBasedOnInput( GetHPN() );
	SetMeasureWindowH(low, high);			//

	low  = 1;
	high = GetVPN() - 1;
	SetMeasureWindowV(low, high);			//

	low  = PCMDATA[PcMode].low;
	high = PCMDATA[PcMode].high;

	i = IVF;
	num = GetHpnVpn(1);								// Get vpn, IVF
	if( num==0 ) {
		#ifdef DEBUG_PC2
		dPrintf("\r\n  --> Error at GetHpnVpn() line:%d", __LINE__);
		#endif
		return 0;
	}

	#ifdef DEBUG_PC3
	if( i != IVF ) dPrintf("\r\nIVF changed %d -> %d ****", i, IVF);
	#endif

	SetVCORange( (DWORD)IVF * num * PCMDATA[PcMode].CLOCK );

	#ifdef DEBUG_PC2
	dPrintf("\r\nRange:%04x to %04x\r\n", low, high);
	#endif

	//#ifdef SUPPORT_DTV
	if( IsDTVInput() ) {
		num = PCMDATA[PcMode].CLOCK;
		SetCoarse( num );
		//SetPhase(0);
		return num;
	}
	//#endif

	//===== For the PLL div range, seek the Phase Peak =====
	// if PLL div value is correct, phase RGB values are peak.
	
	RGBPeak=0;
	WriteADC(0xc1, 0x30);

	num = low;	// plldiv of new peak
	for(i=low; i <= high; ) {

		SetCoarse(i);

		for(j=0; j<8; j++) {
			phase = (7-j)*4;					// HHY 1.10
			SetPhase(phase);					// if NO_INPUT stop
			if( !GetPhaseRGB() ) return 0;		//

			if(RGBPeak < PhaseRGB) {
				RGBPeak = PhaseRGB;
				phasepeak = phase;
				num = i;
				dPuts(" -*"); 
			} 
			else {
				dPuts("   ");
			}
		}

		if( Range4Coarse ) i++;
		else if(PcMode>=EE_SXGA_60 || PcMode==EE_VGA_60)
			i += 4;
		else	
			i += 8;
	}

	if( high!=low ) SetCoarse(num);		// set value of peak
	SetPhase(phasepeak);
	WriteADC(0xc1, 0x20);
	return num;

}
//-----------------------------------------------------------------------------
//		Final Set ADC [PLL div], [Phase]
//-----------------------------------------------------------------------------
void AutoTunePhase(void)
{
	BYTE	i, j, peak_j;

	dPuts("\r\n-------------Tune-Fine");

	if( IsDTVInput() ) return;

	j = GetPhaseCurrent() - 8; //- 0x20;

	RGBPeak=0;
	for(i=0; i<9; i++, j+=0x02) {

		j=j%0x1f;
		SetPhase(j);						// if NO_INPUT stop
		if( !GetPhaseRGB() ) return;		//

		if(RGBPeak < PhaseRGB) {
			RGBPeak = PhaseRGB;
			peak_j = j;
			dPuts("--peak");
		} 
		else {
			dPuts("   ");
		}
	}
	SetPhase(peak_j);
}

//-----------------------------------------------------------------------------
//			Measure    VPN, HPN
//			Calculate  IVF, IHF
//-----------------------------------------------------------------------------
WORD GetHpnVpn(BYTE en)
{
	WORD	vpn, hpn, sync;	//hsync, vsync;

	if( !en && DebugLevel ) {
		en = DebugLevel;
		DebugLevel = 0;
	}
	else
		en = DebugLevel;

	#ifdef DEBUG_PC
	dPuts("\r\n------ GetHpnVpn()");
	dPrintf("\r\n PPF = %ld", GetPPF() );
	#endif
	//----- Measurement Command -------------

	if( !MeasureAndWait(3) ) {
		#ifdef DEBUG_PC
		dPrintf(" Error at GetHpnVpn ");
		#endif
		DebugLevel = en;
		return 0;
	}

	//----- Get VPN -------------------------
	vpn = GetVPN();						// VPN = V Period Num
	sync = GetVSYNCPulse();			// Vsync Pulse Width = VSYNC interval

	#ifdef DEBUG_PC
	dPrintf("\r\nVPN:%04x(%d)  VSYNC:%04x(%d)", vpn, vpn, sync, sync);
	#endif

	// Polarity check
	if( sync > (vpn/3) ) {
		InvertVSYNCPolarity();
		MeasureAndWait(3);
		sync = GetVSYNCPulse();
		#ifdef DEBUG_PC
		dPrintf(" --->New VSYNC:%04x(%d)", sync, sync);
		#endif
	}

	//----- Get HPN ---------------------
	hpn = GetHPN();						// HPN = H Period Num
	IHF = (GetPPF()+hpn/2) / hpn;		// IHF = PPF / HPN 
	sync = GetHSYNCPulse();			// H sync Pulse Width = HSYNC interval

	#ifdef DEBUG_PC
	dPrintf("\r\nIHF:%08lx(%ld)", IHF, IHF);
	dPrintf("\r\nHPN:%04x(%d)  HSYNC:%04x(%d)", hpn, hpn, sync, sync);
	#endif

	//#ifdef SUPPORT_DTV	
	//if(	IsDTVInput() ) {	// Don't check h polarity. 
	//	if(vpn<250) { vpn = IHF/
	//#endif

	if( sync >= (hpn/3) ) {
		InvertHSYNCPolarity();
		MeasureAndWait(3);
	}

	//----- Get New HPN -------------
	sync = GetHSYNCPulse();			// H sync Pulse Width = HSYNC interval
	hpn = GetHPN();						// HPN = H Period Num
	IHF = (GetPPF()+hpn/2) / hpn;		// IHF = PPF / HPN 

	#ifdef DEBUG_PC
	dPrintf("\r\nIHF(New):%08lx(%ld)", IHF, IHF);
	dPrintf("\r\nHPN(New):%04x(%d)  HSYNC(New):%04x(%d)", hpn, hpn, sync, sync);
	#endif

	//----- Get New VPN -------------
	vpn = GetVPN();
	IVF = (WORD)((IHF+vpn/2)/vpn);		// IVF = IHF / VPN 

	#ifdef DEBUG_PC
	dPrintf("\r\nVPN(New):%04x(%d)", vpn, vpn);
	dPrintf("\r\nIVF(New):%04x(%d)", IVF, IVF);
	dPuts("\r\n-------");
	#endif

	DebugLevel = en;
	return vpn;
}

//=============================================================================
//			Return V active Start
//=============================================================================
WORD GetVerticalActiveArea(void)
{
	WORD  vstart, vstart1, vend;
	char  off=0;

	dPuts("\r\n----- Get V Active Area");

	//----- if current mode is DTV, use default value----------------

	//#ifdef SUPPORT_DTV
	if( IsDTVInput() ) {

		#ifdef DEBUG_DTV
		dPrintf("  ---> Use fixed data Vstart=%d  VAN=%d", PCMDATA[PcMode].Vstart, PCMDATA[PcMode].VAN);
		#endif

		SetVactiveStart( PCMDATA[PcMode].Vstart );
		
		switch (PcMode) {
		case EE_YPbPr_480I:		off = 4;	break;
		case EE_YPbPr_1080I:	off = 2;	break;
		case EE_RGB_1080I:		off = 4;	break;
		default:				off = 0;	break;
		}
		SetVactiveLen( PCMDATA[PcMode].VAN+off+20 );
		
		return PCMDATA[PcMode].Vstart;
	}
	//#endif

	//======================== Get the Bottom End ========================

	vend   = GetVend();
	if( vend >= GetVPN() ) {
		#ifdef DEBUG_PC
		Printf("\r\nToo big vend [%04x]", vend);
		#endif
		vend = GetVPN()-1;
	}
	vstart  = vend - PCMDATA[PcMode].VAN + 1;
	vstart1 = GetVstart();
//	vstart  = GetVstart();

	#ifdef DEBUG_PC
	dPrintf("\r\nFind Vend   --- %04x %04x", vstart, vend);
	#endif

	//================================================= HHY 2.00
	if( PcMode >= EE_1152_60 && PcMode <=EE_1152_75 ) {
		if( vstart1 > vstart && vstart1 <= vstart+3 ) {
			vstart = vstart1;
			vend   = vstart + PCMDATA[PcMode].VAN - 1;
			#ifdef DEBUG_PC
			dPuts("    -------> Use Vstart");
			#endif
		}
	}
	//=================================================

	#ifdef DEBUG_PC
	dPrintf("\r\nAuto Measure Vstart=%04x(%d)  Vend=%04x(%d)", vstart, vstart, vend, vend);
	#endif

	if( ( vend < GetVSYNCPulse() + PCMDATA[PcMode].VAN + 1 ) ||		// HHY 1.63 

	    (vstart > PCMDATA[PcMode].Vstart + 30) || ((int)vstart < ((int)PCMDATA[PcMode].Vstart - 30) ) ) {

		#ifdef DEBUG_PC
		ePrintf("    ==> Out Of Range V Active");
		#endif

		vstart = GetVactiveStartEE(PcMode);
		vend   = vstart + PCMDATA[PcMode].VAN - 1;
	}

	//----- Compensation mode by mode -------------------------------

	#ifdef VGA
	if( PcMode>=EE_VGA_60 || PcMode<=EE_VGA_85 ) {
		vstart -= 5;
		Printf("\r\n      ----------- VGA Bypass !!!");
	}
	#endif

	if( PcMode==EE_SP2 ) vstart = PCMDATA[EE_SP2].Vstart;	// HHY 1.62	640x350

	//----- set the register values ( Vstart, VAN ) -----------------

	SetVactiveStart(vstart);

/* //Vtest
	if( PCMDATA[PcMode].VAN== GetPVR() || Flag4Bypass ) {
		SetVactiveStart(vstart);				// bypass mode
	}
	else {
		SetVactiveStart( GetVSYNCPulse() );		// non-bypass mode
	}
*/
	SetVactiveLen(PCMDATA[PcMode].VAN);		// with VAN
	
	#ifdef DEBUG_PC
	ePrintf("\r\n@@@@@ Vstart=%04x[%d]  Vend=%04x[%d]", vstart, vstart, vend, vend);
	#endif

	return vstart;
}
//=============================================================================
//
//=============================================================================
BYTE GetHorizontalActiveArea(void)
{
	WORD	hstart, hend;

	dPuts("\r\n----- Get H Active Area ");

	//#ifdef SUPPORT_DTV
	if( IsDTVInput() ) {
		hstart = PCMDATA[PcMode].Hstart;
		hend   = hstart + PCMDATA[PcMode].HAN + 1;
		
		#ifdef DEBUG_PC
		dPrintf("  ---> Use fixed data Hstart=%d  HAN=%d  Hend=%d", hstart, PCMDATA[PcMode].HAN, hend);
		#endif
		
		SetHactiveStart( hstart );
		SetHactiveEnd( hend+3 );				// with HAN, Hstart
		return TRUE;
	}
	//#endif

	//======================== Get the Right End ========================

	hstart = GetHstart();

	hend   = hstart + PCMDATA[PcMode].HAN + 1;

	#ifdef DEBUG_PC
	dPrintf("\r\nAuto Measure Hstart=%04x(%d)  Hend=%04x(%d)", hstart, hstart, hend, hend);
	#endif
	
	if( (hstart > PCMDATA[PcMode].Hstart + 100) || ((int)hstart < (int)PCMDATA[PcMode].Hstart - 100) ) {

		#ifdef DEBUG_PC
		ePuts("    ==> Out Of Range H Active");
		#endif
		
		hstart = GetHactiveStartEE(PcMode);
		hend   = hstart + PCMDATA[PcMode].HAN + 1;

		SetHactiveStart(hstart);			// with Hstart
		SetHactiveEnd( hend );				// with HAN, Hstart

		return TRUE;
	}

	//----- Compensation mode by mode -------------------------------

	if( PcMode < EE_XGA_60 ) hend++;

	if( !IsBypassmode() ) {
		hstart--;
		hend--;
	}


	#ifdef XGA
	if( PcMode>=EE_1152_60 ) {
		hstart+=3;
		hend+=3;
	}
	#endif

	#ifdef SXGA
	if( PcMode>=EE_1152_60 && PcMode<=EE_1152_75 ) {
		hstart+=2;
		hend+=2;
	}
	#endif

	//----- set the register values ( Hstart, Hend ) ----------------

	SetHactiveStart(hstart);			// with Hstart
	SetHactiveEnd( hend );				// with HAN, Hstart

	#ifdef DEBUG_PC
	ePrintf("\r\n@@@@@ Hstart=%04x[%d]  Hend=%04x[%d]", hstart, hstart, hend, hend);
	#endif
	
	return TRUE;
}

//-----------------------------------------------------------------------------
//			Calcurate and Save VOback(0xb9) & PVP(0xb7,0xbb)
//             - Refer to additional document
//-----------------------------------------------------------------------------
void SetVValueForPanel(WORD VIstart)
{
	DWORD	dtmp;				
	WORD	PVR, VAN;
	BYTE	VIsync, VOsync, VOback;

	VIstart = VIstart;
	VIsync  = GetVSYNCPulse();
	PVR     = GetPVR();
	VAN     = PCMDATA[PcMode].VAN;
	VOsync  = ReadTW88(0xb8);

	dtmp = VIstart + 3 - VIsync - 1;
	dtmp = (dtmp * PVR * 10) / VAN;		// +5 means round-up.
	VOback = (dtmp+5-5)/10 - VOsync;

	#ifdef DEBUG_PC
	Printf("\r\n************************");
	Printf("\r\nVIstart=%d VIsync=%d PVR=%d VAN=%d ", (WORD)VIstart, (WORD)VIsync, (WORD)PVR, (WORD)VAN);
	Printf("VOsync=%d", (WORD)VOsync);
	Printf("==> VOback=%02bx(%bd)", VOback, VOback);
	Printf("\r\n************************");
	#endif // DEBUG_PC

	//----- Compensation mode by mode -------------------------------
	#ifdef XGA
	if     ( PcMode==EE_DOS ) VOback -= 2;	// HHY 3.00 
	else if( PcMode==EE_SP1 ) VOback -= 2;	// HHY 3.00 
	#endif

	#if defined VGA || defined WVGA
	if     ( PcMode==EE_DOS ) VOback += 1;
	else if( (PcMode>=EE_SVGA_56) && (PcMode<=EE_SVGA_85) ) VOback += 1;
	#endif

	//-----------------------------------------------------------

	SetVBackPorch( (BYTE)VOback );
	SetPVP(VOsync + VOback + PVR + 10);
}
//-----------------------------------------------------------------------------
//			Calcurate [Panel H. Cycle] = PHP(Panel H Period)
//-----------------------------------------------------------------------------
#ifndef AUTOCALC_PC
BYTE SetHValueForPanel(void)
{
	WORD	sum=0;
	WORD	php;

	sum = ReadTW88(0xb3) + ReadTW88(0xb4) + GetPHR();	// sum = AA+AB+AC,AD = From Hsync to Active region
	
	MeasureAndWait(3);

	php = (DWORD)(GetHPN()) * PCMDATA[PcMode].VAN / GetPVR();		// PHP = HPN * (VAN/PVR)

	#ifdef DEBUG_PC
	dPrintf("\r\nHPN:%04x(%d)", GetHPN(), GetHPN());
	dPrintf("\r\nVAN:%04x(%d)", PCMDATA[PcMode].VAN, PCMDATA[PcMode].VAN);
	dPrintf("\r\nPVR:%04x(%d)", GetPVR(), GetPVR());
	dPrintf("\r\nPanel H. Cycle:%04x(%d),  aa+ab+ac:%04x(%d)", php, php, sum, sum); 
	#endif

	if( php <= sum ) {
		ePuts("--not enough");

		#ifdef DEBUG_PC
		return FALSE;
		#endif
	}

	//=================================

	#ifdef SXGA
	switch( PcMode ) {
	case EE_RGB_576I:		case EE_YPbPr_576I:		php = 0x70f;	break;
	case EE_RGB_720P:		case EE_YPbPr_720P:		php = 0x623;	break;
	case EE_RGB_1080I:		case EE_YPbPr_1080I:	php = 0x618;	break;
	case EE_RGB_1080I50A:	case EE_YPbPr_1080I50A:	php -= 6;		break;
	}
	#endif

	//=================================
	
	//------ Set PHP ----------
	SetPHP(php);

	return TRUE;
}
#else
BYTE SetHValueForPanel(void)
{
	return TRUE;
}
#endif
//-----------------------------------------------------------------------------
//		VAN = V Active Number
//		HAN = H active Number
//-----------------------------------------------------------------------------
BYTE GetActiveRegion(void)
{
	WORD	vstart;
	DWORD   pres, scale;
	DWORD	ppf;

	WORD	period, sync;

	//----- Set Measurement Wondow Size -----

	SetMeasureWindowV(1, 0x400);
	MeasureAndWait(3);

	// Set H window
	if( (PCMDATA[PcMode].VAN != GetPVR()) || !Flag4Bypass ) {	// --- Change hpulse & hpn ---
		period = ConvertBasedOnInput( GetHPN() );
		sync   = ConvertBasedOnInput( GetHSYNCPulse() );
	}
	else {
		period = GetCoarse();
		sync   = GetHSYNCPulse();
	}
	SetMeasureWindowH(sync, period);

	// Set V window
	period = GetVPN();
	sync   = GetVSYNCPulse();
	SetMeasureWindowV(1, period);

	//----- Do Measurement ---------------------------
	MeasureAndWait(3);

	//----- Get VAN = Vertical Active Area -----------

	vstart = GetVerticalActiveArea();  // Measure Input Vstart, Vactive

	SetMeasureWindowV(1, period);		// HHY 1.61 for stable h sync region

	//----- Calculate Y-scale Factor, and save -------

	#if (defined WIDE_SCREEN)
	Set4WideScreen(GetWideModeEE());
	#else
	{ //NORNAL SCREEN
		pres = (DWORD)PVR_;
		scale = (PCMDATA[PcMode].VAN * 0x10000L) / pres;
	
		#ifdef DEBUG_PC
		dPrintf("\r\n VAN=%04x  PVR=%04x", PCMDATA[PcMode].VAN, pres);
		dPrintf("\r\nYscaleFactor:%04x(%d)", scale, scale);
		#endif
	
		#ifndef WXGA
		if( scale==0x10000 && Flag4Bypass ) {		// Bypass
			BypassZoom();
		}
		else
		#endif
		{
			Clear_bypass();
			YScale2(scale);						// save V Scale Up Factor
		}
	} // WIDE
	#endif
//	vstart = GetVactiveStart();
	SetVValueForPanel(vstart);		// with VAN, Vstart

	//----- Calcurate Panel H. Cycle(A9,AD) ----------

	ppf = GetPPF();

/****/
	#ifdef DEBUG_PC
	while( !SetHValueForPanel() )	{	// we need to use higher PPF

		ppf += 2700000;
		if( ppf > MAX_PPF ) {
			ePuts("\r\n\n--------------Too big IPF\r\n");
			return FALSE;
		}
		ChangeInternPLL(ppf);			// internal PLL
		#ifdef DEBUG_PC
		dPrintf("--> Increased PPF:%8ld", ppf);
		#endif
		delay(10);
	}
	#endif
/****/

	//----- Get HAN = Horizontal Active Area ---------

	GetHorizontalActiveArea();  

	if( (PCMDATA[PcMode].VAN != GetPVR()) || !Flag4Bypass) {	// non-bypass
		pres = (DWORD)GetPHR();
		scale = (PCMDATA[PcMode].HAN * 0x10000L ) / pres;	// 256*han/phr

		#ifdef DEBUG_PC
		dPrintf("\r\nXscaleFactor:%04x(%d)", scale, scale);
		#endif

		//#if (defined SXGA) || (defined WXGA) || (defined WSGA)
		if( PCMDATA[PcMode].HAN > 1024 ) {//1100
			scale = (PCMDATA[PcMode].HAN * 0x80L ) / 1024 + 1;		// (HAN/1024)*256
			XscaleD(scale);
			#ifdef DEBUG_PC
			Printf("\r\nXscaleFactorD:%04x(%d)", scale, scale);
			#endif
			scale = (1024 * 0x10000L ) / 1280;						// (1024/PHR)*256
			XscaleU(scale);
			#ifdef DEBUG_PC
			Printf("\r\nXscaleFactorU:%04x(%d)", scale, scale);
			#endif
		}
		else
		//#endif
		{
	#ifdef WIDE_SCREEN
		if(WideScreenMode == WIDESCREEN_WIDE)
	#endif // WIDE_SCREEN
			XScale2(scale);
		}
	}

	return TRUE;
}
/*===========================================================================*/
/*					  Display The Result for Debugging			             */
/*===========================================================================*/
void DisplayResultAndSetActiveRange(void)
{

⌨️ 快捷键说明

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