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

📄 tda988x.c

📁 mstar 776 开发的车载dvd
💻 C
字号:
#define TDA988X_C

/*
*******************************************************************************
**
** \file      tda988x.cpp
** \version   v1.0
** \date      01-12-2004
**
** (C) Copyright 2004 by Philips RF Solutions, Singapore
** All rights reserved.
**
**
** The code in this module may not be distributed or
** disclosed to third parties without written permission
** of the owner.
**
**		ver			date			by		Desc
**		==========================================================================
**		1							LD		Created
*******************************************************************************
*/

#include "DataType.h"
#include "msAPI_Memory.h"
#include "nxtcommon.h"
#include "tda988x.h"

/********************************************************************************/
/*                                typedef                                       */
/********************************************************************************/

/*IF Data*/
typedef struct{
	Data8	IIC_Addr;

	Data8	ifConfig[2];
	Data8	SMode;	// byte for switching mode
	Data8	AMode;	// byte for adjust mode
	Data8	DMode;	// byte for data mode
	Data8	Status_Byte;	// byte for read status

	void    *pInstance;	/* identifies the instance of this device */
	void    *pNext;		/* link tonext instance */
} IF_DC;

#define NULL_INSTANCE ((IF_DC *)(-1))


/********************************************************************************/
/*                                Local                                         */
/********************************************************************************/
static int GetAddr(void* Instance, Data8* pAddr);
static int SetData(void* Instance);
static IF_DC *GetDC(void *pContext);
static IF_DC *FindDC(void *pContext, IF_DC **ppPrevious);
static void	DeleteDC(void *pContext);


/* One Device Control instance is guaranteed --
 * this is the root of a list if multiple are needed.
 */
static IF_DC ins0 = {
	0x80, // IIC address of Tuner device
	0,0,  // ifConfig[2];
	0x17, // SMode Byte
	0x12, // AMode Byte
	0x45, // DMode Byte
	0x00, //Status_Byte;

	(void*)NULL_INSTANCE,	/* *pInstance */
	(void*)NULL_INSTANCE	/* *pNext */
};


/////////////////////////////////////////////////////////////////////////////
// IF_Init
//
// Inputs:
//		none
//
// Outputs:
//		none
//
// Returns:
//		0 - no error
//		TUN_IF_NOT_FOUND -- could not locate the tuner
//
// Description:
//		hunt for IF chip IIC address, then initializes IF chip
//
int IF_Init(void* Instance, Data8 addrGuess)
{
	Data8 u8Data = 0xFF;
	Data16 IiCError;
	IF_Error_t retValue=IF_NOT_FOUND;
	IF_DC *pDC;

	IiCError = NxtIicXfer(	(void *)NULL,
									NXT_IIC_READ,
									NXT_IIC_SPEED_SLOWEST,
									1,
									addrGuess,
									&u8Data);

	if( NXT_NO_ERROR == IiCError )
	{

		// read again to clear the power-up bit
		IiCError= NxtIicXfer(	(void *)NULL,
										NXT_IIC_READ,
										NXT_IIC_SPEED_SLOWEST,
										1,
										addrGuess,
										&u8Data);

		if( NXT_NO_ERROR == IiCError )
		{
			// check for power-up bit gone
			if(((~u8Data) & 0x01))
			{
				/* create the referenced DCB */
				pDC = GetDC((void*)(Instance));

				if (pDC == NULL_INSTANCE) return IF_MEMORY_PB;
				else {
					/* instance created ok */
					pDC->IIC_Addr=addrGuess;
					pDC->Status_Byte = u8Data;
					IiCError = SetData((void *)NULL);
					if(NXT_NO_ERROR == IiCError) retValue = IF_OK;
				}
			}
			else retValue= IF_NOT_FOUND;
		}
	}

	return retValue;
} // IF_Init


/////////////////////////////////////////////////////////////////////////////
// IF_GetBytes
//
// Inputs:
//		none
//
// Outputs:
//		Data8* - values returned in buffer passed as pointer
//
// Returns:
//		int - number of bytes returned
//
// Description:
//		returns tunerConfig[dividerbytes] + CB_Byte + BB_Byte + AB_Byte
//
int IF_GetBytes(void* Instance, Data8* pDataBytes)
{
	IF_DC *pDC, *pDummy;

	pDC = FindDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return IF_MEMORY_PB;

	pDataBytes[0]=pDC->SMode;
	pDataBytes[1]=pDC->AMode;
	pDataBytes[2]=pDC->DMode;

	return 3;
} //IF_GetBytes


/////////////////////////////////////////////////////////////////////////////
// IF_SetBytes
//
// Inputs:
//		Data8* - pointer to values stored in apps buffer
//		int - number of bytes
//
// Outputs:
//		none
//
// Returns:
//		int - 0 if success or NXT error
//
// Description:
//		reads tunerConfig[controlbytes] from control SW and reprograms tuner
//		with new data, according to the following steps:
//	1) Need to check and save data.
//	2) Program IF chip.
//
int IF_SetBytes(void* Instance, Data8* pDataBytes, Data8 DataLength)
{
	Data8	transferIFConfig[3], i;
	IF_DC *pDC, *pDummy;

	pDC = FindDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return IF_MEMORY_PB;


	// recover data
	for(i=0; i< DataLength; i++)
	{
		transferIFConfig[i]=pDataBytes[i];
	}

	// 1) check and save data
	pDC->SMode=transferIFConfig[0];
	pDC->AMode=transferIFConfig[1];
	pDC->DMode=transferIFConfig[2];

	// 2) program IF chip
	return SetData((void *)NULL);
} //IF_SetBytes


/////////////////////////////////////////////////////////////////////////////
// IF_ReadStatus
//
// Inputs:
//		none
//
// Outputs:
//		Data8* pstatus_byte: pointer to status data
//
// Returns:
//		none
//
// Description:
//		reads status byte of Tuner
//
int IF_ReadStatus(void* Instance, Data8* pstatus_byte)
{
	IF_DC *pDC, *pDummy;

	pDC = FindDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return IF_MEMORY_PB;


	NxtIicXfer((void *)NULL,
				NXT_IIC_READ,
				NXT_IIC_SPEED_SLOWEST,
				1,
				pDC->IIC_Addr,
				pstatus_byte);

	//now we can read the data
	NxtReadIicXferData((void *)NULL, 1, pstatus_byte);

	return IF_OK;
}//IF_ReadStatus


/////////////////////////////////////////////////////////////////////////////
// IF_IOCTL
//
// Inputs:
//		void *Instance, points to device instance
//		int function, selects which IO function is required
//		void *pInputs
//
// Outputs:
//		void *pOutputs
//
// Returns:
//		int - 0 if success, else non-zero
//
// Description:
//		performs special functionality
//
int IF_IOCTL(void* Instance, int function, void *pInputs, void *pOutputs)
{
	IF_Error_t  retVal=IF_OK;
	IF_DC *pDC, *pDummy;

	pDC = FindDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return IF_MEMORY_PB;

	switch (function) {
	case IF_GET_IIC_AD:
		GetAddr(Instance, (Data8*) pOutputs);
		break;
	default:
		retVal = IF_OTHER_ERROR;//IF_NOT_SUPPORTED;
		break;
	}

	return retVal;
} // IF_IOCTL


/*
**
** NON-EXPORTED FUNCTIONS NEXT
**
**
**
*/

/////////////////////////////////////////////////////////////////////////////
// GetAddr
//
// Inputs:
//		none
//
// Outputs:
//		Data8 *pAddr = I2C address of IF chip
//
// Returns:
//		0 = no error (always)
//
static int GetAddr(void* Instance, Data8 *pAddr)
{
	IF_DC *pDC, *pDummy;

	pDC = FindDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return IF_MEMORY_PB;

	*pAddr = pDC->IIC_Addr;
	return 0;

} // GetAddr


/////////////////////////////////////////////////////////////////////////////
// SetData
//
// Inputs:
//		none
//
// Outputs:
//		none
//
// Returns:
//		int - 0 if success or TUN_IF_NOT_FOUND error
//
// Description:
//		programs 3 control bytes in IF chip
//
static int SetData(void* Instance)
{
	Data16 IiCError;
	IF_DC *pDC, *pDummy;

	pDC = FindDC(Instance, &pDummy);

	if (pDC == NULL_INSTANCE) return IF_MEMORY_PB;


	IiCError=0;
	pDC->ifConfig[0]=0x00;	// switching byte
	pDC->ifConfig[1]=pDC->SMode;
	IiCError|= (Data16)NxtIicXfer((void *)NULL,
						NXT_IIC_WRITE,
						NXT_IIC_SPEED_SLOWEST,
						2,
						pDC->IIC_Addr,
						pDC->ifConfig);

	pDC->ifConfig[0]=0x01;	// adjust byte
	pDC->ifConfig[1]=pDC->AMode;
	IiCError|= (Data16)NxtIicXfer((void *)NULL,
						NXT_IIC_WRITE,
						NXT_IIC_SPEED_SLOWEST,
						2,
						pDC->IIC_Addr,
						pDC->ifConfig);

	pDC->ifConfig[0]=0x02;	// data byte
	pDC->ifConfig[1]=pDC->DMode;
	IiCError|= (Data16)NxtIicXfer((void *)NULL,
						NXT_IIC_WRITE,
						NXT_IIC_SPEED_SLOWEST,
						2,
						pDC->IIC_Addr,
						pDC->ifConfig);

	if( NXT_NO_ERROR == IiCError ){
		return 0;
	}
	else{
		return IF_NOT_FOUND;
	}
} // SetData






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

	/* attempt to find a matching DC */
	pDC = FindDC(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 = &ins0;
		}
		else {
			/* we're past the root, so allocate one */
			pDC = (IF_DC *)msAPI_Memory_Allocate(sizeof(IF_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;
}

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

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

	return pDC;
}

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

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

	/* did we find one? */
	if ( pDC != NULL_INSTANCE ) {
		/* fixup the links and free the DC */
		if ( pDC != &ins0 ) {   /* 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 = (IF_DC*)(pDC->pNext); /* temp to hold for free after copy */
				memcpy(pDC, pDC->pNext, sizeof(IF_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;
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
//
// IF_FreeInstances
//
// remove all DCs from list and returns memory
//
// Inputs:
//	void
//
// Returns:
//	void
//
void IF_FreeInstances(void)
{
	IF_DC *pDC=&ins0;

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


#undef TDA988X_C

⌨️ 快捷键说明

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