mt313drv.c

来自「QPSK Tuner details, for conexant chipset」· C语言 代码 · 共 2,160 行 · 第 1/5 页

C
2,160
字号
/*************************************************************************

* Stv0299drv.C                                                        *

* Copyright ? Shenzhen Coship Electronics Co.#,LTD.  2002.3             *

* All rights reserved.                                                  *

*                                                                       *

* Author: Zhou Cheng                                                    *

* Date:  2002/3/13                                                      *

* Compiler:					                                             *

*                                                                       *

* Description:	ZL1031X QPSK Link IC Driver								*

* Notes:                                                                *

* Update:   2002/3/6  Zhou Cheng										*

*************************************************************************/
#include "cs_typedef.h"
#include "CSHDI_typedef.h"
#include "CSQpskCFG.h"
#include "cs_qpsk.h"
#include "zl10039.h"
#include "cs_frnpublic.h"
#include "ZL1031XDRV.h"
#include "cs_os.h"
#include "CS_I2c.h"
#include "Allcommand.h"
#include "cs_gpio.h"
#include "stv0299drv.h"

//#define ZL1031X_DEBUG



#define DiSEqCMODE_MASK		0x03
#define PORTCTRL_MASK			0x07
#define TONEBURST_MASK			0x04
#define MODULATED_BURST		0x04
#define UNMODULATED_BURST		0x00
#define FIFO_FULL_MASK			0x01

#define CFOUND_FLAG			0x80
#define TPLOCK_FLAG				0x08
#define VSTATUS_MASK			0x88


#define ZL1031X_ADDR   			0x1c
#define ZL10039_ADDR   			0xC0
#define XTALK10111KHZ     		1  
#define ZL1031X_XTAL     			XTALK10111KHZ  
#define USE_MAX_BANDWIDTH  	1
//#define ZL1031X_XTAL     			XTALK10111KHZ  
#define ZL1031X_MIN_FREQ		925000
#define ZL1031X_MAX_FREQ		2175000


#define ZLX_TEI_IN_MEG       	1  /* set (default) to enable SNIM_TPS error indicator in mpeg packet header*/
#define ZLX_MOCLK_INV        	1  /* set (default) to invert the MPEG output clock*/
#define ZLX_BKERR_INV        	1  /* invert the \BKERR output pin*/
#define ZLX_MPEGIRQCLK      	0  /* External MPEG clock is on IRQ, not DiSEqC2 pin (ZL10313 only)*/
#define ZLX_BKERR_NOSIG     	1  /* flag missing packets on BKERR line*/
/* ZL1031X Register VENRATE (0x09) Puncture rate enable */
#define ZL1031X_VENRATE12  (0x01)  /* Enable Basic Puncture Rate 1/2 */
#define ZL1031X_VENRATE23  (0x02)  /* Enable Puncture Rate 2/3 */
#define ZL1031X_VENRATE34  (0x04)  /* Enable Puncture Rate 3/4 */
#define ZL1031X_VENRATE56  (0x08)  /* Enable Puncture Rate 5/6 */
#define ZL1031X_VENRATE67  (0x10)  /* Enable Puncture Rate 6/7 Mode B or 7/8 Mode A */
#define ZL1031X_VENRATE78  (0x20)                                         


static int	g_nASIInSelect[MAX_TUNER_NUM];
static int g_nASIStatusPin[MAX_TUNER_NUM];
static int g_nTSOutputMode[MAX_TUNER_NUM];
static HCSHANDLE g_hZL1031XI2C[MAX_TUNER_NUM];

extern BYTE g_bPLLICType[MAX_TUNER_NUM];


static void vDelay_time(int x)
{
	while(x--);
}
static CSQPSK_Error_t ConfigTSOutMode (CSHDITunerIndex bTunerIndex, int nTSOutMode)
{
	if ((TS_PARALLEL == nTSOutMode) || (TS_SERIAL == nTSOutMode))
	{
		g_nTSOutputMode[bTunerIndex] = nTSOutMode;
                
		return CSQPSK_SUCCESS;
	}
	else
		return CSQPSK_INVALID_PARAM;
}

CSQPSK_Error_t ZL1031XInit(CSHDITunerIndex bTunerIndex)
{
	CSI2C_Error_t ErrorCode;	
	
	g_hZL1031XI2C[bTunerIndex] = 0;
	ErrorCode = CSI2COpen(bTunerIndex,ZL1031X_ADDR,&g_hZL1031XI2C[bTunerIndex]);
	CSASSERT(ErrorCode==CSI2C_SUCCESS);
	if ( ErrorCode != CSI2C_SUCCESS)
	{
		return CSQPSK_I2C_ERROR;
	}

	return CSQPSK_SUCCESS;
}
/* +==========================================================================+ */
/* | Function:	OrZL1031XP4J}>]												  |	*/		
/* | Input:		FpJ<<D4fFwKwR}:E											  |	*/
/* | 			V8OrJ}>]5DV8Uk												  |	*/
/* | 			4}P4HkJ}>]8vJ}												  |	*/
/* | Output:	N^															  |	*/
/* | Return:	P4Hk3I9&1jV>												  |	*/
/* +==========================================================================+ */
CSQPSK_Error_t ZL1031XWrtReg ( CSHDITunerIndex bTunerIndex, BYTE StartReg, BYTE *pbValue, DWORD dwLength )
{
	DWORD ii;
	int ErrorCode;
	BYTE bValue[64];
	
	CSASSERT(g_hZL1031XI2C[bTunerIndex] != 0);
	if(dwLength>=64)
		return CSQPSK_INVALID_PARAM;

	bValue[0] = StartReg;

	for ( ii = 1; ii < dwLength+1; ii++ )
		bValue[ii] = *pbValue++;

	if ( CSI2CRequestBus_inner(bTunerIndex, CSHDI_TIMEOUT_INFINITY) == CSI2C_SUCCESS )
	{
		ErrorCode = CSI2CWriteWithStop ( g_hZL1031XI2C[bTunerIndex], bValue, dwLength+1) ;  
		CSI2CReleaseBus_inner(bTunerIndex);
		if (ErrorCode == CSI2C_SUCCESS)
		{
			return CSQPSK_SUCCESS;
		}
	}

	return CSQPSK_I2C_ERROR;
}	


CSQPSK_Error_t ZL1031XWriteReg ( CSHDITunerIndex bTunerIndex, BYTE StartReg, BYTE *pbValue, DWORD dwLength )
{
	DWORD ii;
	int ErrorCode;
	BYTE bValue[64];

	CSASSERT(g_hZL1031XI2C[bTunerIndex]!= 0);
	if(dwLength>=64)
		return CSQPSK_INVALID_PARAM;

	bValue[0] = StartReg;

	for ( ii = 1; ii < dwLength+1; ii++ )
		bValue[ii] = *pbValue++;

	ErrorCode = CSI2CWriteWithStop ( g_hZL1031XI2C[bTunerIndex], bValue, dwLength+1) ;  
	if (ErrorCode!=CSI2C_SUCCESS)
	{
		return CSQPSK_I2C_ERROR;
	}

	return CSQPSK_SUCCESS;
}	


/* +==========================================================================+ */
/* | Function:	4SZL1031X6AR;4.J}>]											  |	*/		
/* | Input:		FpJ<<D4fFwKwR}:E											  |	*/
/* | 			V8OrJ}>]5DV8Uk												  |	*/
/* | 			4}6AHkJ}>]8vJ}												  |	*/
/* | Output:	6A3v5DJ}>]													  |	*/
/* | Return:	P4Hk3I9&1jV>												  |	*/
/* +==========================================================================+ */

CSQPSK_Error_t ZL1031XRdReg(CSHDITunerIndex bTunerIndex, BYTE bStartReg, BYTE *pbValue, DWORD dwLength )
{
	CSQPSK_Error_t  nResult= CSQPSK_SUCCESS;
	BYTE bValue;
	
	CSASSERT(g_hZL1031XI2C[bTunerIndex] != 0);
	if ( CSI2CRequestBus_inner(bTunerIndex, CSHDI_TIMEOUT_INFINITY) == CSI2C_SUCCESS )
	{
		bValue = bStartReg;

		if ( CSI2CWriteWithoutStop ( g_hZL1031XI2C[bTunerIndex], &bValue, 1 ) != CSI2C_SUCCESS ) 
			nResult = CSQPSK_I2C_ERROR;

		if ( CSI2CReadWithStop (g_hZL1031XI2C[bTunerIndex], pbValue, dwLength ) !=CSI2C_SUCCESS )
			nResult = CSQPSK_I2C_ERROR;

		CSI2CReleaseBus_inner(bTunerIndex);
	}
	else
		nResult = CSQPSK_I2C_ERROR;

	return nResult;
}	
// end ZL1031XRdReg()
CSQPSK_Error_t ZL1031XReadReg(CSHDITunerIndex bTunerIndex, BYTE bStartReg, BYTE *pbValue, DWORD dwLength )
{
	int nResult= CSQPSK_SUCCESS ;
	BYTE bValue;

	CSASSERT(g_hZL1031XI2C[bTunerIndex]!= 0);
	bValue = bStartReg;
	if ( CSI2CWriteWithStop ( g_hZL1031XI2C[bTunerIndex], &bValue, 1 ) != CSI2C_SUCCESS ) 
		nResult = CSQPSK_I2C_ERROR;

	if ( CSI2CReadWithStop (g_hZL1031XI2C[bTunerIndex], pbValue, dwLength ) !=CSI2C_SUCCESS )
		nResult = CSQPSK_I2C_ERROR;

	return nResult;
}	


CSQPSK_Error_t  ZL1031x_ReadExtra(CSHDITunerIndex bTunerIndex,BYTE ucSelect,BYTE *ucData )
{
    BYTE ucTemp;

    if (ZL1031XRdReg(bTunerIndex,ZL1031X_HW_CTRL,&ucTemp,1)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;
      /* set the selection value*/
        ucTemp &= 0x1F;
        ucTemp |= (BYTE) (ucSelect<<5);
        if (ZL1031XWrtReg ( bTunerIndex,ZL1031X_HW_CTRL,&ucTemp,1)==CSQPSK_SUCCESS)
        { /* read the selected register*/
            return ZL1031XRdReg(bTunerIndex,ZL1031X_MPEG_CTRL,ucData,1);
        }
    
    return  CSQPSK_SUCCESS;
}

CSQPSK_Error_t ZL1031x_WriteExtra(CSHDITunerIndex bTunerIndex,BYTE ucSelect,BYTE *ucData )
{
     BYTE ucTemp;

   
	if (ZL1031XRdReg(bTunerIndex,ZL1031X_HW_CTRL,&ucTemp,1)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;
      /* set the selection value*/
        ucTemp &= 0x1F;
        ucTemp |= (BYTE) (ucSelect<<5);
        if (ZL1031XWrtReg ( bTunerIndex,ZL1031X_HW_CTRL,&ucTemp,1)==CSQPSK_SUCCESS)
        { /* read the selected register*/
            return ZL1031XWrtReg(bTunerIndex,ZL1031X_MPEG_CTRL,ucData,1);
        }
    
     return CSQPSK_FAILURE;
}

/* +==========================================================================+ */
/* | Function:	?XVFF22R}=EJd3v22KV\FZPE:E									  |	*/		
/* | Input:		?XVFJdHk ( F22KON / F22KOFF )								  |	*/
/* | Output:	N^															  |	*/
/* | Return:	P4Hk3I9&1jV>												  |	*/
/* +==========================================================================+ */

CSQPSK_Error_t ZL1031X_F22K_Switch ( CSHDITunerIndex bTunerIndex, CSHDISwitch nF22KCtrl )
{
	BYTE bValue;

   	if(ZL1031XRdReg(bTunerIndex, ZL1031X_DISEQC_MODE,  &bValue, 1)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;

	if ( nF22KCtrl == CSHDI_ON)
	{
		bValue &= (~0x07); 
		bValue |=  0x01 ;
	}
	else 
	{
		bValue &= (~0x07); 
	}
	return ( ZL1031XWrtReg ( bTunerIndex,ZL1031X_DISEQC_MODE, &bValue, 1 ) );
}
CSQPSK_Error_t ZL1031X_HV_Switch ( CSHDITunerIndex bTunerIndex, Channel_Polarisation HorV )
{
	BYTE bValue;

   	if(ZL1031XRdReg(bTunerIndex, ZL1031X_DISEQC_MODE,  &bValue, 1)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;
	// CSTRACE( INFO_LEVEL, "\n[F22KSwitch] nF22KCtrl = %d",nF22KCtrl);
	
	if ( HorV == POLARISATION_H)
		bValue |=  0x40 ;
	else 
		bValue &= 0xbf  ;
	return ( ZL1031XWrtReg ( bTunerIndex,ZL1031X_DISEQC_MODE, &bValue, 1 ) );
}
CSQPSK_Error_t ZL1031x_I2CPassThrough(CSHDITunerIndex bTunerIndex,BOOL bOpen)
{

    BYTE ucBuffer;
    CSQPSK_Error_t error=CSQPSK_SUCCESS;
	
  //  error |=ZL1031XRdReg(bTunerIndex, ZL1031X_GPP,  &ucBuffer, 1);
    
    if(bOpen)
    {
    	ucBuffer =  0xc0;
    }
    else
    {
    	ucBuffer =  0x80;
    }
    	
    error |= ZL1031XWrtReg(bTunerIndex, ZL1031X_GPP,  &ucBuffer, 1);
	CSASSERT(error==CSQPSK_SUCCESS);
	return error;
    

}
/* +==========================================================================+ */
/* | Function:	ZL1031XCheckChipID									  |	*/		
/* | Input:		bTunerIndex													      |	*/
/* | Output:	No															  |	*/
/* | Return:	SUCCESS/FAILURE												  |	*/
/* +==========================================================================+ */
CSQPSK_Error_t ZL1031XCheckChipID ( CSHDITunerIndex bTunerIndex )
{
	BYTE bValue ;
	CSQPSK_Error_t  ErrorCode;
	int i=0;
	if( ZL1031XInit( bTunerIndex)!=CSQPSK_SUCCESS)
	{
		CSTRACE(ERROR_LEVEL,"[qpsk]ZL1031XInit error \n");
		return CSQPSK_FAILURE;
	}


	while (i++<10)
	{
		 ErrorCode = ZL1031XRdReg ( bTunerIndex,ZL1031X_ID, &bValue, 1);
		 CSTRACE(ERROR_LEVEL,"[qpsk]ZL1031x chip id = %d\n",bValue);
		 if ( bValue == ZL1031X_ID_ZL10313 ) 
		 {
		 	break;
		 }
		 	CSSleep(10);
	}

	if ( ErrorCode != CSQPSK_SUCCESS )
	{
		CSTRACE(ERROR_LEVEL,"[qpsk]ZL1031X read  error =%d\n",ErrorCode);
		return ErrorCode;
	}
	CSASSERT(bValue == ZL1031X_ID_ZL10313);
	if ( bValue == ZL1031X_ID_ZL10313 )
	{
		CSTRACE(ERROR_LEVEL,"[qpsk]ZL1031X read chip ID success! \n");
		#ifdef ZL10039_SUPPORT	
			if ( ZL1003zInit(bTunerIndex) == CSQPSK_SUCCESS )
			{
				g_bPLLICType[bTunerIndex] = ZL10039;	
				if (FTDCheckTuner (bTunerIndex) == CSQPSK_SUCCESS )
				{
					 Zl10039_Initialise(bTunerIndex);
					CSTRACE( INFO_LEVEL, "\n[HDI][CS_QPSK][FTDInitlize]:Tuner is zl10039\n");
					printf("\n[HDI][CS_QPSK][FTDInitlize]:Tuner is zl10039\r\n");
					return CSQPSK_SUCCESS;
				}
				else
				{
					ZL1003zTerm(bTunerIndex);
				}
			}
		#endif

		#ifdef NEW_PLL1
		
		#endif
		
		#ifdef NEW_PLL2
		
		#endif
		CSI2CClose(g_hZL1031XI2C[bTunerIndex]);/*关掉HANDLE*/
		return CSQPSK_FAILURE;	/*遍历所有MT313所支持的TUNER都没有找到合适的TUNER*/
	}
	else
	{
		ErrorCode = CSI2CClose(g_hZL1031XI2C[bTunerIndex]);
		return CSQPSK_FAILURE;
	}		
}
/* +==========================================================================+ */
/* | Function:	ZL1031XKySP<D4fFw3uJ<;/										  |	*/		
/* | Input:		N^														      |	*/
/* | Output:	N^															  |	*/
/* | Return:	P4Hk3I9&1jV>												  |	*/
/* +==========================================================================+ */

CSQPSK_Error_t ZL1031XRegInit ( CSHDITunerIndex bTunerIndex,int nTSOutMode)
{
	
	BYTE bValue[0x14];
	int ErrorCode = CSQPSK_SUCCESS;
	
	 ConfigTSOutMode ( bTunerIndex,  nTSOutMode);
	
	/* write the config register*/
	bValue[0] = 0x8c; 
	if (ZL1031XWrtReg ( bTunerIndex,ZL1031X_CONFIG,bValue,1)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;
	/* FULL RESET HERE */
       bValue[0] = MT312_RESET_FULL; 
	if (ZL1031XWrtReg ( bTunerIndex,ZL1031X_RESET,bValue,1)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;
	
	//vDelay_time(1000);
	CSSleep(2);
	
	bValue[0] = 0x00; 
	if (ZL1031XWrtReg ( bTunerIndex,ZL1031X_HW_CTRL,bValue,1)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;
	/* reset Viterbi and QPSK */
	bValue[0] = MT312_RESET_PART; 
	if (ZL1031XWrtReg ( bTunerIndex,ZL1031X_RESET,bValue,1)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;

⌨️ 快捷键说明

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