zl10039.c

来自「QPSK Tuner details, for conexant chipset」· C语言 代码 · 共 387 行

C
387
字号
//#include <stdio.h>
#include "cs_typedef.h"
#include "CSHDI_typedef.h"
#include "CSQpskCFG.h"
#include "cs_qpsk.h"
#include "ZL1031XDRV.h"
#include "zl10039.h"
#include "cs_os.h"
#include "CS_I2c.h"
#include "Allcommand.h"
//#include "cs_frnpublic.h"
#include "cs_gpio.h"
#include "cs_assert.h"



BYTE  ucPLLByte5 = 0;
BYTE  ucZl39_AgcRef;
BYTE  ucZl39_AgcMin;
BYTE  ucZl39_AgcMax; 
BYTE  ucZl39_AgcCtl;   


#define ZL1003Z_BW_MIN      8
#define ZL1003Z_BW_MAX      40

static HCSHANDLE hZL1003zI2C[2];
CSQPSK_Error_t  ZL1003z_TunerRegRead(CSHDITunerIndex bTunerIndex, BYTE RegInd, BYTE *buffer,BYTE noOfBytes);
 CSQPSK_Error_t ZL1003z_TunerRegWrite(CSHDITunerIndex bTunerIndex, BYTE RegInd, BYTE *buffer,BYTE noOfBytes);
CSQPSK_Error_t ZL1003zInit(CSHDITunerIndex bTunerIndex)
{
	int ErrorCode;
	
	hZL1003zI2C[bTunerIndex] = 0;
	ErrorCode = CSI2COpen(bTunerIndex,ZL1003Z_ADDR,&hZL1003zI2C[bTunerIndex]);
	if ( ErrorCode != CSI2C_SUCCESS)
	{
		return CSQPSK_I2C_ERROR;
	}
	
	{
		BYTE ucData[16];
		
		ucData[5] = 0xc8;
		ZL1003z_TunerRegWrite(0, 0, ucData, 6);//baseband flilter
		CSSleep(100);
		ucData[5] = 0xff;
		ZL1003z_TunerRegRead(0, 0, ucData, 6);
		//printf("@@@@@@@@@@@@@@@@@ 5 ucData = 0x%x\r\n", ucData[5]);
		ucData[6] = 0x03;
		ZL1003z_TunerRegWrite(0, 0, ucData, 7);//br
		CSSleep(100);
		ucData[6] = 0xff;
		ZL1003z_TunerRegRead(0, 0, ucData, 7);
		//printf("@@@@@@@@@@@@@@@@@ 6 ucData = 0x%x\r\n", ucData[6]);
	}
	
	return CSQPSK_SUCCESS;
}
CSQPSK_Error_t ZL1003zTerm( CSHDITunerIndex bTunerIndex )
{
	int ErrorCode;
	
	ErrorCode = CSI2CClose(hZL1003zI2C[bTunerIndex]);
	if ( ErrorCode != CSI2C_SUCCESS)
	{
		return CSQPSK_I2C_ERROR;
	}
	return CSQPSK_SUCCESS;

}
 CSQPSK_Error_t ZL1003z_TunerRegWrite(CSHDITunerIndex bTunerIndex, BYTE RegInd, BYTE *buffer,BYTE noOfBytes)
{
  BYTE szBuffer[18];
  BYTE i;
  int ErrorCode =CSQPSK_SUCCESS;
  
  if(noOfBytes > 32)
  	return CSQPSK_FAILURE;
  if(ZL1031x_I2CPassThrough(bTunerIndex,1)!=CSQPSK_SUCCESS)
  	{
  		CSTRACE(ERROR_LEVEL, "[qpsk]ZL1031x_I2CPassThrough error 1 ?\n");
		return CSQPSK_FAILURE;
  	}
  //CSSleep(5);
  szBuffer[0] = RegInd;
  for (i=0; i<noOfBytes ; i++ )
  	szBuffer[i+1] = buffer[i];
  if ( CSI2CRequestBus_inner(bTunerIndex, CSHDI_TIMEOUT_INFINITY) == CSI2C_SUCCESS )
  {
		ErrorCode = CSI2CWriteWithStop ( hZL1003zI2C[bTunerIndex], szBuffer, noOfBytes+1);  
		CSI2CReleaseBus_inner(bTunerIndex);

		if (ErrorCode != CSI2C_SUCCESS)
			CSTRACE(ERROR_LEVEL,"[ZL1003z_TunerRegWrite]CSI2CWriteWithStop failed\r\n");
			//printf("MMMMMMMMMMMMMMMMMVVVVVVVVVVVVV\n");
			
		//CSTRACE(ERROR_LEVEL, "[qpsk]CSI2CWriteWithStop error= %d\n",ErrorCode);
		//if (ErrorCode == CSI2C_SUCCESS)
		//{
		//	return CSQPSK_SUCCESS;
		//}
 }
  else
  {
  	CSTRACE(ERROR_LEVEL,"[ZL1003z_TunerRegWrite]CSI2CRequestBus failed\r\n");
  }
  if(ZL1031x_I2CPassThrough(bTunerIndex,0)!=CSQPSK_SUCCESS)
 {
  		CSTRACE(ERROR_LEVEL, "[qpsk]ZL1031x_I2CPassThrough error 2 ?\n");
		return CSQPSK_FAILURE;
 }    
 return ErrorCode;

}

CSQPSK_Error_t  ZL1003z_TunerRegRead(CSHDITunerIndex bTunerIndex, BYTE RegInd, BYTE *buffer,BYTE noOfBytes)
{
   BYTE nResult;
   BYTE bValue;
   BYTE szData[20];
   nResult = CSQPSK_SUCCESS;
    szData[0] = 0; 
   if(ZL1031x_I2CPassThrough(bTunerIndex,1)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;
   {
   	BYTE ucValue;
	ZL1031XRdReg ( 0, 20, &ucValue, 1 );
	//printf("ZL1031X_GPP = 0x%x\r\n", ucValue);
   }
  if ( CSI2CRequestBus_inner(bTunerIndex, CSHDI_TIMEOUT_INFINITY) == CSI2C_SUCCESS )
	{
		bValue = RegInd;

		if ( CSI2CWriteWithStop ( hZL1003zI2C[bTunerIndex], &bValue, 1 ) != CSI2C_SUCCESS ) 
		{
			CSTRACE(ERROR_LEVEL,"[ZL1003z_TunerRegRead]CSI2CWriteWithStop failed\r\n");
			nResult = CSQPSK_I2C_ERROR;
		}

		if ( CSI2CReadWithStop (hZL1003zI2C[bTunerIndex], buffer, noOfBytes ) !=CSI2C_SUCCESS )
		{
			CSTRACE(ERROR_LEVEL,"[ZL1003z_TunerRegRead]CSI2CReadWithStop failed\r\n");
			nResult = CSQPSK_I2C_ERROR;
		}

		CSI2CReleaseBus_inner(bTunerIndex);
	}
	else
	{
		CSTRACE(ERROR_LEVEL,"[ZL1003z_TunerRegRead]CSI2CRequestBus failed\r\n");
		nResult = CSQPSK_I2C_ERROR;
	}
   if(ZL1031x_I2CPassThrough(bTunerIndex,0)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;      
   return nResult;

}
CSQPSK_Error_t  ZL1003zSetFrequency(CSHDITunerIndex bTunerIndex,DWORD dwFreqkHz,DWORD dwRs)
{
    	CSQPSK_Error_t bOk;
	WORD divRatio;
	BYTE ucDiv[2];
						/*晶体的预分频系数是10*/
	divRatio = (WORD)((dwFreqkHz*10) / ZL1003Z_XTALKHZ );

	ucDiv[0] = (BYTE)(divRatio >> 8);	    /* MSB */
	ucDiv[1] = (BYTE)(divRatio & 0x00FF);	/* LSB */
	bOk = ZL1003z_TunerRegWrite( bTunerIndex, ZL1003Z_R0, ucDiv, 0x02);
 
	ucPLLByte5 = 0x00; /* Ensure if Freq reprogrammed Bandwidth reprogrammed */

    return (bOk);

}

/******************************************************************************
*   Name:_Zl1003xSetBandwidth
*   Purpose:Sets the bandwidth of the baseband filters 
* 
*   Inputs:Desired bandwidth in MHz
*
*   Outputs:status
*
********************************************************************************/
CSQPSK_Error_t ZL1003zSetBandwidth(CSHDITunerIndex bTunerIndex,BYTE ucBWMHz)
{
    DWORD ucBF;
	BYTE  ucBuffer;
    int  bRet ;
	BYTE ucTmp;

	/* limit absolute bandwidth to 8 to 40 MHz */
    if (ZL1003Z_BW_MIN > ucBWMHz)ucBWMHz = ZL1003Z_BW_MIN;
    if (ZL1003Z_BW_MAX < ucBWMHz)ucBWMHz = ZL1003Z_BW_MAX;

	ucBF = 50280; 
	ucBF = ucBWMHz * ucBF;
	ucBF = (ucBF + 5000)/10000;
	ucBF = ucBF  - 1;
    //	printf("[ZL1003zSetBandwidth]ucBWMHz = 0x%x, ucBF = 0x%x\r\n", ucBWMHz, ucBF);
	if(ucBF != ucPLLByte5)
	{
		ucBuffer = 0x0a;
		bRet = ZL1003z_TunerRegWrite( bTunerIndex, ZL1003Z_R6, &ucBuffer, 0x01);

		//printf("[ZL1003zSetBandwidth] 1 ucBWMHz = 0x%x, ucBF = 0x%x\r\n", ucBWMHz, ucBF);
		ucTmp = (BYTE)ucBF;
		bRet = ZL1003z_TunerRegWrite( bTunerIndex, ZL1003Z_R5, &ucTmp, 0x01);
		//printf("[ZL1003zSetBandwidth]bRet = %d\r\n", bRet);
		ucPLLByte5 = (BYTE)ucBF;
		#if 0
		{
		BYTE ucData[16];
		
		ucData[5] = 0xc8;
		//ZL1003z_TunerRegWrite(0, 0, ucData, 6);
		CSSleep(100);
		ucData[5] = 0xff;
		ZL1003z_TunerRegRead(0, 0, ucData, 6);
		//printf("@@@@@@@@@@@@@@@@@ 5 ucData = 0x%x\r\n", ucData[5]);
		ucData[6] = 0x03;
		//ZL1003z_TunerRegWrite(0, 0, ucData, 7);
		CSSleep(100);
		ucData[6] = 0xff;
		ZL1003z_TunerRegRead(0, 0, ucData, 7);
		//printf("@@@@@@@@@@@@@@@@@ 6 ucData = 0x%x\r\n", ucData[6]);
		}
		#endif
		ucBuffer = 0x2a;
		bRet = ZL1003z_TunerRegWrite( bTunerIndex, ZL1003Z_R6, &ucBuffer, 0x01);

	}

    return bRet;

}

CSQPSK_Error_t ZL1031XSetAGC(CSHDITunerIndex bTunerIndex)
{
/* Setup anything else, such as AGC */
	BYTE ucTemp;
	CSQPSK_Error_t bOk;
    /* Demod  AGC slope*/
     bOk = ZL1031XRdReg(bTunerIndex,ZL1031X_AGC_CTRL,&ucTemp,1);
    ucTemp &= 0xFE; /* keep all except AGC_SL */
    if (bOk==CSQPSK_SUCCESS) 
		bOk = ZL1031XWrtReg(bTunerIndex,ZL1031X_AGC_CTRL,&ucTemp,1);

    /* Demod set up AGC target*/
    ucTemp = ZL1003Z_AGCREF;
    if (bOk==CSQPSK_SUCCESS) 
		bOk = ZL1031XWrtReg(bTunerIndex,ZL1031X_FE_AGC_REF,&ucTemp,1);

    /* Demod  AGC limits*/
    ucTemp = ZL1003Z_AGCMIN;
    if (bOk==CSQPSK_SUCCESS) 
		bOk = ZL1031XWrtReg(bTunerIndex,ZL1031X_FE_AGC_MIN,&ucTemp,1);
    ucTemp = ZL1003Z_AGCMAX;
    if (bOk==CSQPSK_SUCCESS) 
		bOk = ZL1031XWrtReg(bTunerIndex,ZL1031X_FE_AGC_MAX,&ucTemp,1);

    /* Demod AGC control*/
    ucTemp =  ZL1003Z_AGC_CTL;/*new value */
    if (bOk==CSQPSK_SUCCESS) 
		bOk = ZL1031XWrtReg(bTunerIndex,ZL1031X_AGC_CTRL,&ucTemp,1);
	return bOk;
}

CSQPSK_Error_t ZL1003zReset(CSHDITunerIndex bTunerIndex)
{
    	BYTE ucTemp;
	BYTE ucDiv[2];
   	CSQPSK_Error_t bOk=CSQPSK_SUCCESS;
	
	ucTemp = ZL1003Z_RF_CLR ;
	ZL1003z_TunerRegWrite( bTunerIndex, ZL1003Z_RF, &ucTemp, 0x01);
	CSSleep(20);
	//CSTRACE(ERROR_LEVEL, "[qpsk]ZL1003z reset %d \n",bOk);
	ucTemp = 0x00;
	bOk |= ZL1003z_TunerRegWrite( bTunerIndex, ZL1003Z_RF, &ucTemp, 0x01);

	/* Need Delay here about 1ms - 4ms to allow for power up  */
	CSSleep(5);
  
	ucTemp = (ZL1003Z_R2_C_750 << 4) | 0x0A;
	bOk |= ZL1003z_TunerRegWrite( bTunerIndex, ZL1003Z_R2, &ucTemp, 0x01);

	ucTemp = 0x0a;
	bOk |=ZL1003z_TunerRegWrite( bTunerIndex, ZL1003Z_R6, &ucTemp, 0x01);
	
	ucTemp = 0xee;
	bOk |= ZL1003z_TunerRegWrite( bTunerIndex, ZL1003Z_R4, &ucTemp, 0x01);

	ucTemp = (ZL1003Z_R7_BG_14_5 << 3);
	bOk |=ZL1003z_TunerRegWrite( bTunerIndex, ZL1003Z_R7, &ucTemp, 0x01);

	ucDiv[0] = 0x05;
	ucDiv[1] = 0xFD;
	bOk |= ZL1003z_TunerRegWrite( bTunerIndex,ZL1003Z_R0, ucDiv, 0x02);

 	bOk |= ZL1003z_TunerRegWrite( bTunerIndex,ZL1003Z_R8, &ucTemp, 0x01); /* for delay */

	ucPLLByte5 = 0x00;
	bOk |=ZL1031XSetAGC( bTunerIndex);

    /* Demod  IQ swap */
// sfg   ucTemp = 0x40;
// sfg   if (bOk) bOk = ZL1031XWrtReg(bTunerIndex,ZL1031X_QPSK_CTRL,&ucTemp,1);
    
	return bOk;
}


CSQPSK_Error_t ZL1003zGetTuneLock(CSHDITunerIndex bTunerIndex , int *nlock)
{
	BYTE ucBuffer[2];

	ucBuffer[0] = 0x30;
	if(ZL1003z_TunerRegRead(bTunerIndex,  ZL1003Z_R0, ucBuffer, 2)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;
	
	*nlock = FALSE;
	CSTRACE(ERROR_LEVEL,"[ZL1003zGetTuneLock]ucBuffer[0]  = 0x%x\r\n", ucBuffer[0]);
   	 if(ucBuffer[0] & 0x80) 
   	 {
   		*nlock = TRUE;
		//CSTRACE(ERROR_LEVEL, "[qpsk] tuner type is zl10039 \n");
   	 }
    			
	return CSQPSK_SUCCESS;

}

CSQPSK_Error_t ZL1003zGetTuneValue(CSHDITunerIndex bTunerIndex )
{
	BYTE ucBuffer[16];
	int i;
	if(ZL1003z_TunerRegRead(bTunerIndex,  ZL1003Z_R0, ucBuffer, 16)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;
	CSTRACE(ERROR_LEVEL, "[qpsk] Zl10039 Register value :\n ");
	for(i=0;i<16;i++)
		CSTRACE(ERROR_LEVEL, "[%02x]%02x   ",i,ucBuffer[i]);
 	 CSTRACE(ERROR_LEVEL, "\n ");    			
	return CSQPSK_SUCCESS;

}
CSQPSK_Error_t Zl10039_Initialise(CSHDITunerIndex bTunerIndex)
{
 // ucZl39_AgcRef  =  ZL1003Z_AGCREF;		  
 // ucZl39_AgcMin  =  ZL1003Z_AGCMIN	;
 // ucZl39_AgcMax =  ZL1003Z_AGCMAX	;
 // ucZl39_AgcCtl  =  ZL1003Z_AGC_CTL;
  CSASSERT((hZL1003zI2C[bTunerIndex] != 0));
  if (hZL1003zI2C[bTunerIndex] == 0)
  {
  	if(ZL1003zInit( bTunerIndex)!=CSQPSK_SUCCESS)
		return CSQPSK_FAILURE;
  }
  else
  	CSTRACE(ERROR_LEVEL, "[qpsk]Zl10039_Initialise has init \n");
  if(ZL1003zReset(bTunerIndex)!=CSQPSK_SUCCESS) 
  	return CSQPSK_FAILURE;

  return CSQPSK_SUCCESS;	
}

CSQPSK_Error_t  ZL1003zSetFreq_Ex(CSHDITunerIndex bTunerIndex,DWORD dwFreqkHz,DWORD dwRs)
{
	//CSTRACE(ERROR_LEVEL, "[qpsk]ZL1003z setup freq \n");
	 if(ZL1003zReset(bTunerIndex)!=CSQPSK_SUCCESS) 
	 {
		CSTRACE(ERROR_LEVEL, "[qpsk]ZL1003zReset error \n");
	 	return CSQPSK_FAILURE;
	 }
	 //CSTRACE(ERROR_LEVEL, "[qpsk]ZL1003z reset over \n");
	 if (ZL1003zSetFrequency( bTunerIndex, dwFreqkHz, dwRs)!=CSQPSK_SUCCESS)
	 {
		CSTRACE(ERROR_LEVEL, "[qpsk]ZL1003zSetFrequency error \n");
	 	return CSQPSK_FAILURE;
	 }	
	// CSTRACE(ERROR_LEVEL, "[qpsk]ZL1003z setup freq over \n");
	  return CSQPSK_SUCCESS;
}


⌨️ 快捷键说明

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