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

📄 mxd_porting_maxim2165.c

📁 mxd_tuner_ref_Ax.x.xxx :为了您使用方便
💻 C
字号:
/*!
 *
 * \file    mxd_porting_maxim2165.c
 *
 * \brief	API porting for maxim tuner .
 *
 * C files to define the basic functions for maxscend SDK, 
 * which will be used in the custom application.
 *
 * \par    Include files
 *    - MxdPorting_Maxim2165.h
 *
 * \par    Functions
 *    - MxdPorting_Maxim2165InitTuner
 *    - MxdPorting_Maxim2165SetFreq
 *    - MxdPorting_Maxim2165GetTunerStatus
 * \par    Copyright (c) 2007 Maxscend Technologies Inc. All rights reserved
 *
 * PROPRIETARY RIGHTS of Maxscend Technologies Inc. are involved in
 * the subject matter of this material.  All manufacturing, reproduction, 
 * use, and sales rights pertaining to this subject matter are governed 
 * by the license agreement.  The recipient of this software implicitly 
 * accepts the terms of the license.
 * 
 * \version
 * Revision of last commit: $Rev:: 169                        $
 * Author of last commit  : $Author:: maxscend\lei.wei        $
 * Date of last commit    : $Date:: 2007-10-18 17:07:19 +0800#$
 *
 *
 * \par    Modification History 
 * [ Revision  data  author Bugzilla ID ] 
 * [ Description ]
 *
 */

#include "mxd_porting_maxim2165.h"

/*!
 * Init for tuner, including demod for tuner and tuner itself
 *
 * \param hDevice    [ in ] Device handle
 *
 * \return Return code by MXD_RTN_CODE_E enumeration
 *
 * \remarks 
 *
 */
MXD_RTN_CODE_E MXD_API MxdPorting_Maxim2165InitTuner (IN HMXDDEV hDevice)
{
    MXD_RTN_CODE_E eRtnCode = MXD_RTN_OK;

    /* Setup tuner interface */	
    DDS_WriteReg( hDevice,  COMMON_TIC_CONTROL_REG, MAXIM_MAX2165_CTRL_REG_VAL );
    DDS_WriteReg( hDevice,  COMMON_TIC_TUNER_SLAVE_ADDR_REG, MAXIM_MAX2165_SALVE_ADDR_VAL );
    DDS_WriteReg( hDevice,  COMMON_TIC_I2C_BIT_RATE_REG, MAXIM_MAX2165_I2CBRATE_CFIG_VAL );		

	/* PWM setting */
	DDS_WriteReg( hDevice,  DTMB_TDP_PWM_INI_REG, 0x30);/*PWM init value*/ 
	DDS_WriteReg( hDevice,  COMMON_ADC_MODE_CTRL_REG, 0xdf );
	DDS_WriteReg( hDevice,  DTMB_TDP_PWM_CONTROL_REG, 0x44);
	DDS_WriteReg( hDevice,  DTMB_TDP_PWM_CLK_STEP_REG, 0x64);

	return eRtnCode;
}/* enf of MxdPorting_Maxim2165InitTuner( )*/
 
/*!
 * Tune the tuner to the frequency, this function should be used 
 * after MXD_TIF_InitTuner having been called.
 *
 * \param hDevice    [ in ] Device handle
 * \param freqHz     [ in ] frequency to be tuned, unit: Hz
 *
 * \return Return code by MXD_RTN_CODE_E enumeration
 *
 * \remarks 
 *
 */
MXD_RTN_CODE_E MXD_API MxdPorting_Maxim2165SetFreq (
							IN HMXDDEV hDevice,
							IN MXD_U32 freqHz )
{
    /* MXD_S32 pllDiv; */
    /* DOUBLE xtrefFreq = 20.0; */ /* actual reference crystal frequency in MHz */
    MXD_U32 xtrefFreqKHz = 20*1000;   /* 20 MHz= 20kkHz*/
    MXD_U8 frac2 =  0x18;	/* (FRAC=Fractional Mode, LSB is set in Max2165set_pll */
    MXD_U8 lna = 0x01;	    /* (LnaSw=on)  */ 
    MXD_U8 pll = 0xa;       /* (Rdiv=1)(ICP=600)(CPS=ICP)(ADLY=1)(LF_DIV=2) */
    MXD_U8 shutDown	= 0x2;  /*  All shutdown bits off	*/
    MXD_U8 vco = 0x54;	    /* (VCO=1)(BS=1)(VAS=on)	*/
    MXD_U8 baseBand	= 0xc3; /* (BB_BW set in 2165set_bb_filter)(BB_BIA=low)(PD_TH=3)*/
    MXD_U8 dcOffset = 0x75; /* (DC_MO=normal)(DC_SP=slow)(DC_TH=1)			*/
    MXD_U8 tfn, tfb;
    MXD_U8 nta,ntb;
    MXD_U8 romVal = 0;
    MXD_U32 txFreq = 0;
    MXD_U8  balL,balH;
    MXD_U8 tfCaps = 0;   
    MXD_U8 bb8,tfFilter;
    MXD_U8 registers[16], i;
    MXD_U32 freqKHz;
    MXD_U32 quotientN=0;
    MXD_U32 quatientFrac=0;   	

    /* set the register defaults */
	registers[MAX2165_FRAC2]	= frac2;	/* (FRAC=Fractional Mode, LSB is set in Max2165set_pll */
	registers[MAX2165_LNA]		= lna;	/* (LnaSw=on)  */ 
	registers[MAX2165_PLL]		= pll;  /* (Rdiv=1)(ICP=600)(CPS=ICP)(ADLY=1)(LF_DIV=2) */
	registers[MAX2165_TEST]		= 0x8;  /* (CP_TST=Normal)(TURBO=on)(LD_MUX=Normal)	 */
	registers[MAX2165_SHUTDOWN]	= shutDown;  /*  All shutdown bits off	*/
	registers[MAX2165_VCO]		= 0x54;	/* (VCO=1)(BS=1)(VAS=on)	*/
	registers[MAX2165_BASEBAND]	= 0xc3; /* (BB_BW set in 2165set_bb_filter)(BB_BIA=low)(PD_TH=3)*/
	registers[MAX2165_DCOFFSET]	= dcOffset; /* (DC_MO=normal)(DC_SP=slow)(DC_TH=1)			*/
	registers[MAX2165_DCOFFSET_DAC] = 0x0;
	registers[MAX2165_ROM_ADDR]	= 0x00;

    freqKHz = (freqHz/1000);
	/* set Max2165 PLL divider ratio's for a given frequency*/
	/* pllDiv =(MXD_S32)(  ( 1 << 20 ) * freq / xtrefFreq); */
    UTIL_DivUint32(freqKHz,xtrefFreqKHz,&quotientN,&quatientFrac);    
    /* 20bits quatientFrac */
    quatientFrac = quatientFrac>>10;
    quatientFrac += 1;
    quatientFrac = quatientFrac>>1;
    
    registers[MAX2165_NDIV]= (MXD_U8) quotientN;

    registers[MAX2165_FRAC2]= (MXD_U8) ( (registers[MAX2165_FRAC2]&0xf0) | ( (quatientFrac>>16) &0x0f ));
    registers[MAX2165_FRAC1]= (MXD_U8) (quatientFrac >> 8) & 0xff;
    registers[MAX2165_FRAC0]= (MXD_U8) (quatientFrac & 0xff);

    /* RF tracking filter. Note different interpolation algorithms
      * used for VHF III vs. UHF band. No range checking done. 
      */
    romVal = ReadMax2165RomTable( hDevice, MAX2165_ROMTFNTCH);
    nta = romVal & 0xf; 
    ntb = romVal >>4; 
    
    if( txFreq >0 )
    {
        if(freqKHz < 725*1000)
        {
            tfn = ntb;
        }
        else
        {
            tfn = nta;
        }
    }
    else
    {
        tfn = 15;
    }

    romVal = ReadMax2165RomTable( hDevice, MAX2165_ROMTF_BAL);
    balL = romVal & 0xf;
    balH = (romVal >>4) & 0xf;

    tfb = (MXD_U8)( balL +(balH - balL)*(freqKHz - 470*1000)/(780*1000 -470*1000));
    /* just in case, limit to 4bits to prevent rounding problems near 470MHz*/
    if( tfb> 15)
    {
        tfb = 15;
    }
    tfCaps = (MXD_U8)(tfn <<4) | tfb;
	registers[MAX2165_TF_CAPS] = (MXD_U8)( tfn << 4 ) | tfb ;
	  
    romVal = ReadMax2165RomTable( hDevice, MAX2165_ROMBBBW );
    bb8 = romVal>>4;
	tfFilter=bb8+3;

    if (tfFilter >15)
	{
	    tfFilter=15;
	}

    baseBand = (baseBand & 0xf)|(MXD_U8)(tfFilter<<4);
    registers[MAX2165_BASEBAND]= (registers[MAX2165_BASEBAND] & 0xf) | (MXD_U8)(tfFilter<<4);

	for(i = 0; i < 15; i++)
    {
        TIF_WriteTunerReg( hDevice, i, registers[i] );	    
    }

    /* set fraction divider of vco*/
    TIF_WriteTunerReg( hDevice, MAX2165_FRAC2, registers[MAX2165_FRAC2] );
    TIF_WriteTunerReg( hDevice, MAX2165_FRAC1, registers[MAX2165_FRAC1] );
    TIF_WriteTunerReg( hDevice, MAX2165_FRAC0, registers[MAX2165_FRAC0]  );    
    /* set integer divider of vco */
    TIF_WriteTunerReg( hDevice, MAX2165_NDIV,   registers[MAX2165_NDIV] );
	TIF_WriteTunerReg( hDevice, MAX2165_TF_CAPS, tfCaps);
    TIF_WriteTunerReg( hDevice, MAX2165_BASEBAND, baseBand );
    TIF_WriteTunerReg( hDevice, MAX2165_VCO, vco);

    return MXD_RTN_OK;

}/* end of MxdPorting_Maxim2165SetFreq( )*/

/*!
 * Get tuner status. If locked, then return ok, if un-locked, then return failed.
 *
 * \param hDevice    [ in ] Device handle
 *
 * \return Return code by MXD_RTN_CODE_E enumeration
 *
 * \remarks 
 *
 */
MXD_RTN_CODE_E MXD_API MxdPorting_Maxim2165GetTunerStatus (IN HMXDDEV hDevice)
{
    MXD_RTN_CODE_E eRtnCode = MXD_RTN_FAIL;
    MXD_U8 regVal;
    MXD_U8 i;

    /* Read status byte*/
    for(i=0; i<200; i++)
    {
        TIF_ReadTunerReg( hDevice, MAX2165_STATUS, &regVal );
        if( (regVal&0x10) == 0x10 )
        {
            return MXD_RTN_OK; 
        }
    }
    return eRtnCode;
}/* end of MxdPorting_Maxim2165GetTunerStatus( )*/

/*!
 * Read Maxim Rom Table from MAX2165, get register value to be set to the maxim register
 *
 * \param tableIndex: [ in ] index of the Rom Table
 *
 * \return register value read back
 *
 */
MXD_U8 MXD_API ReadMax2165RomTable( IN HMXDDEV hDevice, IN MXD_U8 tableIndex )
{
    MXD_U8 romVal = 0;
    /* Set Index in ROM in table to read back Bias */
    TIF_WriteTunerReg( hDevice, MAX2165_ROM_ADDR, tableIndex );
    /* Read back from table */
    TIF_ReadTunerReg( hDevice, MAX2165_ROM_READ, &romVal );
    TIF_WriteTunerReg( hDevice, MAX2165_ROM_ADDR, 0 );
    
    return romVal; 
} /* end of ReadMax2165RomTable( ) */

/*!
 *  MAX2165 control
 *
 * \param hDevice: [ in ] the handle of device
 *
 * \return NULL
 *
 */
void MXD_API MxdPorting_Maxim2165LnaCtrl( HMXDDEV hDevice )
{
    MXD_U8 pwmVal;
    MXD_U8 uTunerRegValue;

    /* read the lna value */
	DDS_ReadReg(hDevice, DTMB_TDP_PWM_THD_REG, &pwmVal);
	TIF_ReadTunerReg( hDevice, MAX2165_LNA, &uTunerRegValue );
	if( (0x01 == (uTunerRegValue&0x01)) && pwmVal < PWM_MIN_THRESHOLD)
	{
	    /* lna off */
		OAL_DebugPrint(1, "lna off!, the 0x27: 0x%x", pwmVal);
        TIF_ReadTunerReg( hDevice, MAX2165_LNA, &uTunerRegValue );
        uTunerRegValue &= 0xfe;
        TIF_WriteTunerReg(hDevice, MAX2165_LNA, uTunerRegValue );
        return ;
	}
	else if( (0x00 == (uTunerRegValue&0x01)) && pwmVal >= PWM_MAX_THRESHOLD) 
	{	
		/*lna on */
        OAL_DebugPrint(1, "lna on !, the 0x27: 0x%x", pwmVal);
        TIF_ReadTunerReg( hDevice, MAX2165_LNA, &uTunerRegValue );
        uTunerRegValue |= 0x01;
		TIF_WriteTunerReg(hDevice, MAX2165_LNA, uTunerRegValue );
		return;
	}
	return;
}
/*!
 * set data with certain bit.
 *
 * \param inDividend		[ in ]		dividend.
 * \param inDivisor			[ in ]		divisor.
 * \param outQuotientN		[ out ]		integer value of the quotient.
 * \param outQuatientFrac	[ out ]		fractional value of the quotient. 
 *
 * \return Return code MXD_U8: 1: OK;0: the divisor is 0   
 *
 * \remark: (float)quatientN + (float)(quatientFrac)/(float)(1<<fracPosi)== (float)devidend/(float)divisor
 *
 */
MXD_U8 UTIL_DivUint32( MXD_U32 inDividend,
                 MXD_U32 inDivisor,
                 MXD_U32 * outQuotientN,
                 MXD_U32 * outQuatientFrac
                )
{
    
    MXD_U32 remainder;
    MXD_U32 quotientN;
    MXD_U32 quatientFrac;
    /*MXD_U8  fracPosi;*/
    MXD_U8 i;
    /*MXD_U8 j;*/
    MXD_U32 divisor  = inDivisor;
    MXD_U32 dividend = inDividend;       

    if (0 == divisor)
    {
        return 0;
    } 

    quotientN = dividend/divisor;
    
    remainder = dividend - quotientN*divisor;

    /* @here remainder < divisor */
    if (remainder>=0x80000000)
    {
        remainder = remainder >> 1;
        divisor   = divisor >> 1;
    }

    /* @here remainder <= divisor */
    quatientFrac = 0;

    if (remainder != divisor)
    {
        for (i=0;i<31;i++)
        {
            remainder = remainder<<1;    /* remainder *= 2; */

            if ( remainder >= divisor )
            {
                quatientFrac += 1;       /* BS_ (quatientFrac,bit0) */
                remainder -= divisor;
            }

            quatientFrac = quatientFrac<<1;
        } 

        if(0xffffffff==quatientFrac )
        {
            quotientN++;
            quatientFrac = 0;
        }
        else
        {
            quatientFrac = (quatientFrac+1)>>1;
        }
    }
    
    else   /* remainder == divisor */
    {
        quotientN++;
        quatientFrac = 0;
    }

    *outQuotientN = quotientN;
    *outQuatientFrac = quatientFrac;
    
    return 1;
}

/*end of _MXD_PORTING_MAXIM2165_C_ */

⌨️ 快捷键说明

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