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

📄 tua6034.c

📁 mstar 776 开发的车载dvd
💻 C
📖 第 1 页 / 共 3 页
字号:
		else if (dFMHz > 450.0) {
			pDC->BB_Byte |= HIGH_BAND;
		}
		else {
			pDC->BB_Byte |= MID_BAND;
		}
	}
	return TUN_OK;
} // SetTunerBand


/////////////////////////////////////////////////////////////////////////////
// SetInput
//
// Inputs:
//		void *Instance, points to device instance
//		bool Input - input 0 or 1 of NIM TUV1236D
//
// Outputs:
//		none
//
// Returns:
//		none
//
// Description:
//		set antenna-input selection
//
static int	 SetInput(void* Instance, bool bInput)
{
	TUN_DC *pDC, *pDummy;

	pDC = FindTunDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return TUN_MEMORY_PB;

	if(!bInput)
	{
		if(pDC->AB_Byte != 0xFF)
			pDC->BB_Byte &= ~INPUT_SEL;	// P3 = 0
	}
	else
	{
		if(pDC->AB_Byte != 0xFF)
			pDC->BB_Byte |= INPUT_SEL;	// P3 = 1
	}
	SetFrequency(Instance, pDC->frequency);
	return TUN_OK;
} // SetInput


/////////////////////////////////////////////////////////////////////////////
// SetATC
//
// Inputs:
//		void *Instance, points to device instance
//		bool AtcBit
//
// Outputs:
//		none
//
// Returns:
//		none
//
// Description:
//		set AGC Time constant bit
//
static int	 SetATC(void* Instance, bool AtcBit)
{
	TUN_DC *pDC, *pDummy;

	pDC = FindTunDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return TUN_MEMORY_PB;

	if(pDC->AB_Byte == 0xFF) return TUN_OK; // TDA6508

	if(!AtcBit)	pDC->AB_Byte &= ~ATC_LOW;
	else	pDC->AB_Byte |= ATC_LOW;
	return TUN_OK;
} // SetATC


/////////////////////////////////////////////////////////////////////////////
// SetAgcTakeOver
//
// Inputs:
//		void *Instance, points to device instance
//		Data8 value - AGC value
//
// Outputs:
//		none
//
// Returns:
//		none
//
// Description:
//		set AGC take-over point
//
static int	 SetAgcTakeOver(void* Instance, Data8 value)
{
	TUN_DC *pDC, *pDummy;

	pDC = FindTunDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return TUN_MEMORY_PB;

	if((0<=value) && (value<=7))
	{
		pDC->AB_Byte &= 0x8F; // mask 3 bits for AGC
		pDC->AB_Byte |= (value<<4);
	}
	return TUN_OK;
} // SetAgcTakeOver


/////////////////////////////////////////////////////////////////////////////
// SetFrequency
//
// Inputs:
//		void *Instance, points to device instance
//		double dFMHz -- frequency to tune
//
// Outputs:
//		none
//
// Returns:
//		0 - no error
//		TUN_OTHER_ERROR -- tuner PLL never locked
//
// Description (Phillips 1256):
//		calculate the divider ratio based on frequency
//
//		setup config bytes:
//		PLL using divider ratio
//		Tuner Band using frequency
//
//		initially set charge pump high
//		configure tuner
//
//		wait for tuner lock status or timeout
//		once locked set charge pump low
//
//		return locked, or other error
//
static int SetFrequency(void* Instance, double dFMHz)
{
	int   DividerRatio;
	Bool  bLocked     = FALSE;
	Data8 waitCount   = 5; //Laurent 1000;
	TUN_Error_t returnValue = TUN_OTHER_ERROR;
	Data16 drvErr;
	TUN_DC *pDC, *pDummy;

	pDC = FindTunDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return TUN_MEMORY_PB;

	if(!pDC->Digital_Mode)
		DividerRatio = (int) floor(0.5 + (dFMHz*1.0e6 + F_IF_AN)/(8.0*pDC->F_ref)); //analogue mode
	else
		DividerRatio = (int) floor(0.5 + (dFMHz*1.0e6 + F_IF_DI)/(8.0*pDC->F_ref)); //digital mode

	SetPLLDivider(Instance, DividerRatio);
	SetTunerBand(Instance, /*(int)*/ dFMHz);

	/* Coarse Tune */
	SetChargePump(Instance, TRUE);

	PrepareControlBytes(Instance);

	if(!pDC->DirectAccess)
	{
		// access through channel decoder
		do{
			drvErr |= (Data16)NxtIicXfer((void *)NULL,
								NXT_IIC_WRITE,
								NXT_IIC_SPEED_SLOWEST,
								4,
								pDC->IIC_Addr,
								&pDC->tunerConfig[0]);
		}while(waitCount-- && (NXT_NO_ERROR != drvErr));

		if (NXT_NO_ERROR == drvErr) {

			waitCount = 5;
			while (!bLocked && waitCount--)
			{
				//Sleep(1); //CABLE_SYSTEM

				drvErr = (Data16)NxtIicXfer((void *)NULL,
											NXT_IIC_READ,
											NXT_IIC_SPEED_SLOWEST,
											1,
											pDC->IIC_Addr,
											&pDC->Status_Byte);

				//now we can read the data
				drvErr |= NxtReadIicXferData((void *)NULL, 1, &pDC->Status_Byte);

				bLocked = ((NXT_NO_ERROR == drvErr) &&
							(((pDC->Status_Byte) & TUNER_LOCK_MASK) == TUNER_LOCK_MASK)
						  );
			}

			if (bLocked)
			{
				/* Fine Tune */
				SetChargePump(Instance, FALSE);

				PrepareControlBytes(Instance);

				waitCount = 5;

				do
				{
					drvErr = (Data16)NxtIicXfer((void *)NULL,
												NXT_IIC_WRITE,
												NXT_IIC_SPEED_SLOWEST,
												4,
												pDC->IIC_Addr,
												&pDC->tunerConfig[0]);

				}while(waitCount-- && (NXT_NO_ERROR != drvErr));

				if (NXT_NO_ERROR == drvErr) {
					returnValue = 0;
				}
			}
		}
	}

	return returnValue;
} // SetFrequency


/////////////////////////////////////////////////////////////////////////////
// SetMode
//
// Inputs:
//		void *Instance, points to device instance
//		bool mode - analogue (FALSE) or digital (TRUE) mode of NIM
//
// Outputs:
//		none
//
// Returns:
//		none
//
// Description:
//		set Digital_Mode value according to transmit mode, analogue or digital
//
static int SetMode(void* Instance, bool mode)
{
	TUN_DC *pDC, *pDummy;

	pDC = FindTunDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return TUN_MEMORY_PB;

	pDC->Digital_Mode=mode;
	return TUN_OK;
} // SetMode


/////////////////////////////////////////////////////////////////////////////
// PrepareControlBytes
//
// Inputs:
//		void *Instance, points to device instance
//
// Outputs:
//		none
//
// Returns:
//		bool - TRUE if AB_Byte to be sent; FALSE otherwise
//
// Description:
//		sets up tunerConfig[controlbytes]
//
static bool PrepareControlBytes(void* Instance)
{
	TUN_DC *pDC, *pDummy;
	bool returnVal=FALSE;
	Data8 temp = (pDC->CB_Byte & TEST_BITS)>>3;

	pDC = FindTunDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return FALSE;

	/*commit CB_Byte to tunerConfig[CONTROL1]*/
	pDC->tunerConfig[CONTROL1] = pDC->CB_Byte;

	if((temp==AB_MODE) && (pDC->AB_Byte != 0xFF))
	{ //t2t1t0 ==011 send AB byte && TUA6034 only
		pDC->tunerConfig[CONTROL2] = pDC->AB_Byte;	/*commit AB_Byte to tunerConfig[CONTROL2]*/
		returnVal=TRUE;
	}

	return returnVal;
} //PrepareControlBytes


/////////////////////////////////////////////////////////////////////////////
// GetADCLevel
//
// Inputs:
//		void *Instance, points to device instance
//
// Outputs:
//		none
//
// Returns:
//		int
//
// Description:
//		returns digital output of 5-level ADC
//
static int GetADCLevel(void* Instance)
{
	Data8	AdcLevel;
	TUN_DC *pDC, *pDummy;

	pDC = FindTunDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return TUN_MEMORY_PB;

	TUN_ReadStatus(Instance, &pDC->Status_Byte);
	AdcLevel=pDC->Status_Byte & TUNER_ADC_OP;
	return AdcLevel;
} //GetADCLevel


/////////////////////////////////////////////////////////////////////////////
// GetAgcBit
//
// Inputs:
//		void *Instance, points to device instance
//
// Outputs:
//		none
//
// Returns:
//		bool
//
// Description:
//		returns AGC active
//
static bool GetAgcBit(void* Instance)
{
	bool	AgcActive;
	TUN_DC *pDC, *pDummy;

	pDC = FindTunDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return FALSE;

	TUN_ReadStatus(Instance, &pDC->Status_Byte);
	AgcActive=(!(pDC->Status_Byte & TUNER_AGC_MASK))? FALSE:TRUE;
	return AgcActive;
} //GetAgcBit


/////////////////////////////////////////////////////////////////////////////
// GetPorFlag
//
// Inputs:
//		void *Instance, points to device instance
//
// Outputs:
//		none
//
// Returns:
//		bool
//
// Description:
//		returns Power-On Reset flag
//
static bool GetPorFlag(void* Instance)
{
	bool	PorFlag;
	TUN_DC *pDC, *pDummy;

	pDC = FindTunDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return FALSE;

	TUN_ReadStatus(Instance, &pDC->Status_Byte);
	PorFlag=(!(pDC->Status_Byte & TUNER_POR_MASK))? FALSE:TRUE;
	return PorFlag;
} //GetPorFlag




/////////////////////////////////////////////////////////////////////////////
//
// GetTunDC
//
// Finds a matching DC, or allocates a new one for future match.
//
// Inputs:
//	void *pInstance - NULL, or user-defined ID for multiple tuners
//
// Returns:
//	TUN_DC * - pointer to a DC, or NULL_INSTANCE if memory allocation failed.
//
static TUN_DC *GetTunDC(void *pInstance)
{
	TUN_DC *pDC;
	TUN_DC *pLastDC;

	/* attempt to find a matching DC */
	pDC = FindTunDC(pInstance, &pLastDC);

	/* did we find one? */
	if ( pDC == NULL_INSTANCE ) {
		/* no match found -- is the root in use yet? */
		if ( pLastDC->pInstance == NULL_INSTANCE ) {
			/* no -- give back the root */
			pDC = &inst0;
		}
		else {
			/* we're past the root, so allocate one */
			pDC = (TUN_DC *)msAPI_Memory_Allocate(sizeof(TUN_DC));
			/* link it on the list */
			pLastDC->pNext = pDC;
		}
		/* mark this DC in use */
		pDC->pInstance = pInstance;
		/* make it the end of the list */
		pDC->pNext = NULL_INSTANCE;
	}
	return pDC;
}

/////////////////////////////////////////////////////////////////////////////
//
// FindTunDC
//
// Finds a matching DC among initialized contexts
//
// Inputs:
//	void *pInstance - NULL, or user-defined ID for multiple tuners
//
// Outputs:
//	TUN_DC **ppLastDC - pointer to previous DC in single linked list
//
// Returns:
//	TUN_DC * - pointer to matching DC
//
static TUN_DC *FindTunDC(void *pInstance, TUN_DC **ppLastDC)
{
	TUN_DC *pDC = &inst0;

	*ppLastDC = NULL_INSTANCE;
    /* iterate until we match or hit the end of the list */
	do {
		if ( pDC->pInstance == pInstance ) {
			break;
		}
		else {
			*ppLastDC = pDC;
			pDC = (TUN_DC*)(pDC->pNext);
		}
	} while(pDC != NULL_INSTANCE);

	return pDC;
}

/////////////////////////////////////////////////////////////////////////////
//
// DeleteTunDC
//
// Unlinks DC from list and returns memory
//
// Inputs:
//	void *pInstance - NULL, or user-defined ID for multiple tuners
//
// Returns:
//	void
//
static void DeleteTunDC(void *pInstance)
{
	TUN_DC *pDC;
	TUN_DC *pPreviousDC;

	/* attempt to find a matching DC */
	pDC = FindTunDC(pInstance, &pPreviousDC);

	/* did we find one? */
	if ( pDC != NULL_INSTANCE ) {
		/* fixup the links and free the DC */
		if ( pDC != &inst0 ) {   /* not the root */
			pPreviousDC->pNext = pDC->pNext;
			/* clean up data in case this one gets re-allocated */
			pDC->pNext = NULL_INSTANCE;
			pDC->pInstance = NULL_INSTANCE;
			MSAPI_MEMORY_FREE(pDC);
		}
		else { /* delete the root by compressing the list */
			if ( pDC->pNext != NULL_INSTANCE ) { /* copy the 1st dynamic DC to the root */
				pPreviousDC = (TUN_DC*)(pDC->pNext); /* temp to hold for free after copy */
				memcpy(pDC, pDC->pNext, sizeof(TUN_DC));
				pPreviousDC->pNext = NULL_INSTANCE;
				pPreviousDC->pInstance = NULL_INSTANCE;
				MSAPI_MEMORY_FREE(pPreviousDC);
			}
			else { /* the list is now empty */
				pDC->pNext = NULL_INSTANCE;
				pDC->pInstance = NULL_INSTANCE;
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
//
// TUN_FreeInstances
//
// remove all DCs from list and returns memory
//
// Inputs:
//	void
//
// Returns:
//	void
//
void TUN_FreeInstances(void)
{
	TUN_DC *pDC=&inst0;

	do
	{
		DeleteTunDC((void*)pDC->pInstance);
	}
	while (pDC->pInstance != NULL_INSTANCE);
}


#undef TUA6034_C

⌨️ 快捷键说明

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