hamaro_cx24128.c

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

C
1,877
字号
   if(HAMARO_TunerRegisterWrite(p_nim, (unsigned short)(0x1B), (unsigned long)reg_value, HAMARO_IO_UNKNOWN) != True) 
   {
      return(False);
   }
   
   /* F[2:0]=0x1C[7:5] */
   reg_vlue_temp = (fvalue <<5) & 0xE0;
   if(HAMARO_TunerRegisterRead(p_nim, (unsigned short)(0x1C), (unsigned long*)&reg_value, HAMARO_IO_UNKNOWN)!= True)
   {
      return(False);
   }
   reg_value = reg_value & 0x1F;
   reg_value |= reg_vlue_temp;
   if(HAMARO_TunerRegisterWrite(p_nim, (unsigned short)(0x1C), (unsigned long)reg_value, HAMARO_IO_UNKNOWN) != True) 
   {
      return(False);
   }
   
   
   reg_value = (unsigned long)(rvalue - 1);
   
   if (HAMARO_TUNER_CX24128_SetReferenceFreq(p_nim, (HAMARO_RFREQVAL)reg_value) == False)
   {
      return (False);
   }
   
   /* multiplication factor of 10000 is used to improve precision */
   Nfrac = ( (fvalue * 10000L) / HAMARO_CX24128_DIVIDER) + (nvalue * 10000UL) + (32UL * 10000UL);
   
   HAMARO_BCD_set(&bcd, (p_nim->tuner_crystal_freq / p_nim->tuner.cx24128.R));
   
   HAMARO_BCD_mult(&bcd, Nfrac);
   HAMARO_BCD_mult(&bcd, 2);
   HAMARO_BCD_div(&bcd, 10000);
   
   HAMARO_BCD_div(&bcd, p_nim->tuner.cx24128.vcodiv);
   
   if ((p_nim->tuner_crystal_freq) / HAMARO_MM >= 20)
   {
      HAMARO_BCD_mult(&bcd, 2);
   }
   pllfreq = HAMARO_BCD_out(&bcd); 
   
   /* Now tuner is set to a new frequency. */
   p_nim->pll_frequency = pllfreq;
   
   return(True);
}  /* HAMARO_TUNER_CX24128_set_NFRregisters() */

/*******************************************************************************************************
 * HAMARO_TUNER_CX24128_SetFrequency() 
 * Set specified tuner pll frequency 
 ******************************************************************************************************/
BOOL           
HAMARO_TUNER_CX24128_SetFrequency(HAMARO_NIM      *p_nim, /* pointer to nim */
                                 unsigned long  frequency) /* pointer to unsigned long  */
{
   unsigned short nvalue;
   int fvalue;
   unsigned char  reg_value;
   
   HAMARO_TUNER_CX24128_VALIDATE(p_nim);
   
   HAMARO_CX24128_refresh_tuner_pll_lock = True;
   
   p_nim->pll_frequency = frequency;
   
   /* Switch to normal mode: set 0x14[7:6] to 0x00 */
   /* Read 0x14, and clear 0x14[7:6] */
   if (HAMARO_TunerRegisterRead(p_nim, (unsigned short)(0x14), (unsigned long*)&reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   reg_value &= 0x3F; 
   
   if (HAMARO_TunerRegisterWrite(p_nim, (unsigned short)(0x14), (unsigned long)reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False); 
   }
   
   /* Disable ps_test: set 0x10[6] to 0x00 */
   /* Read 0x10, and clear 0x10[6] */
   if (HAMARO_TunerRegisterRead(p_nim, (unsigned short)(0x10), (unsigned long*)&reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   reg_value &= 0xBF; 
   
   if (HAMARO_TunerRegisterWrite(p_nim, (unsigned short)(0x10), (unsigned long)reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   
   /* Calculate N,F,R by specified frequency value. */
   if (HAMARO_TUNER_CX24128_calc_pllNF(p_nim,&nvalue,&fvalue) != True) 
      return(False); 
   /* Set N,F,R to tuner registers. */
   if (HAMARO_TUNER_CX24128_SetNFRRegisters(p_nim,nvalue,fvalue,p_nim->tuner.cx24128.R) != True) 
      return(False); 


   /* set the vco divider to the tuner */
   /* Read 0x18, and clear 0x18[6] */
   if (HAMARO_TunerRegisterRead(p_nim, (unsigned short)(0x18), (unsigned long*)&reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   reg_value &= 0xBF; 
   
   if (p_nim->tuner.cx24128.vcodiv == HAMARO_VCODIV2)
   {
      reg_value |= 0x00;
   }
   else
   {
      reg_value |= 0x40;
   }

   if (HAMARO_TunerRegisterWrite(p_nim, (unsigned short)(0x18), (unsigned long)reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   
   
   /* Start tuning. */
   /* Read 0x1C, and set 0x1C[4] to 1 */
   if (HAMARO_TunerRegisterRead(p_nim, (unsigned short)(0x1C), (unsigned long*)&reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   reg_value |= 0x10; 
   if (HAMARO_TunerRegisterWrite(p_nim, (unsigned short)(0x1C), (unsigned long)reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }

   if (HAMARO_TUNER_CX24128_TuningMachineFailSafe(p_nim) == False)
   {
      return (False);
   }
   //trace("CX24128_SetFreq is called\n");
   return(True);
}  /* HAMARO_TUNER_CX24128_SetFrequency() */

/*******************************************************************************************************
 * HAMARO_TUNER_CX24128_GetFilterBandwidth() 
 * function to get the filter bandwidth for tuner's anti-alias filter 
 *******************************************************************************************************/
BOOL           
HAMARO_TUNER_CX24128_GetFilterBandwidth(HAMARO_NIM            *p_nim,             /* pointer to nim */
                                 HAMARO_FILTERBW       *p_filterbandwidth, /* filter bandwidth */
                                 unsigned short *p_gmcbandwidth)    /* gmc bandwidth */
{ 
  unsigned short gmcbw;

  HAMARO_TUNER_CX24128_VALIDATE(p_nim);
  if (p_gmcbandwidth == 0 || p_filterbandwidth == 0) return(False);

  *p_filterbandwidth = (HAMARO_FILTERBW)((p_nim->antialias_bandwidthkhz & 0xFC0) >> 6);

  gmcbw = (unsigned short)(p_nim->antialias_bandwidthkhz & 0x3F);
  /* convert gmc bandwidth to be in KHz. */
	gmcbw = (unsigned short)(gmcbw * HAMARO_CX24128_GMCBW_STEP_SIZE + HAMARO_CX24128_MIN_GMCBW);
  *p_gmcbandwidth = gmcbw;

  return(True);
} /* HAMARO_TUNER_CX24128_GetFilterBandwidth */

/*******************************************************************************************************
 * HAMARO_TUNER_CX24128_SetFilterBandwidth() 
 * function to set the filter bandwidth for tuner's anti-alias filter 
 *******************************************************************************************************/
BOOL           
HAMARO_TUNER_CX24128_SetFilterBandwidth(HAMARO_NIM            *p_nim,          /* pointer to nim */
                                 unsigned long  bandwidthkhz)    /* bandwidth in khz */
{ 
   unsigned char  reg_value;
   HAMARO_FILTERBW  filterbandwidth = HAMARO_FILTERBW_35MHZ;
   
   HAMARO_TUNER_CX24128_VALIDATE(p_nim);
   
   /* gmcbandwidth is in KHz. */
   if ((bandwidthkhz > HAMARO_CX24128_MAX_GMCBW) || (bandwidthkhz < HAMARO_CX24128_MIN_GMCBW))
   {
      return(False);
   }
   
   if (bandwidthkhz <= 19000UL) /* 19 MHz */
   {
      filterbandwidth = HAMARO_FILTERBW_35MHZ;
   }
   else if (bandwidthkhz > 19000UL && bandwidthkhz <= 25000UL) /* 19 MHz to 25 MHz */
   {
      filterbandwidth = HAMARO_FILTERBW_40MHZ;
   }
   else /* > 25 MHz */
   {
      filterbandwidth = HAMARO_FILTERBW_65MHZ;
   }
   /* 0x1E[5:0] */
   reg_value = (unsigned long)((bandwidthkhz - HAMARO_CX24128_MIN_GMCBW) / HAMARO_CX24128_GMCBW_STEP_SIZE);
   /* 0x1E[7:6] */
   reg_value |= ((unsigned char)filterbandwidth << 6); 
   
   if (HAMARO_TunerRegisterWrite(p_nim, (unsigned short)(0x1E), (unsigned long)reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   
   p_nim->antialias_bandwidthkhz = ((unsigned long)filterbandwidth << 6UL) | reg_value;
   
   return(True);
} /* HAMARO_TUNER_CX24128_SetFilterBandwidth */

/*******************************************************************************************************
 * HAMARO_TUNER_CX24128_GetGainSettings() 
 * function to get the tuner gain settings 
 *******************************************************************************************************/
BOOL           
HAMARO_TUNER_CX24128_GetGainSettings(HAMARO_NIM           *p_nim,             /* pointer to nim */
                              HAMARO_AMPOUT        *p_ampout,          /* Discrete gain control*/
                              HAMARO_VGA1OFFSET    *p_vga1offset,      /* VGA1 offset control*/
                              HAMARO_VGA2OFFSET    *p_vga2offset,      /* VGA2 offset control*/
                              HAMARO_RFVGAOFFSET   *p_rfvgaoffset)     /* RF VGA offset control*/
                                 
{ 
	HAMARO_TUNER_CX24128_VALIDATE(p_nim);
	if (p_ampout == 0 || p_vga1offset == 0 || p_vga2offset == 0 || p_rfvgaoffset == 0) 
	{
		return(False);
	}

	*p_ampout      = p_nim->tuner.cx24128.ampout;
	*p_vga1offset  = p_nim->tuner.cx24128.vga1offset;
	*p_vga2offset  = p_nim->tuner.cx24128.vga2offset;
	*p_rfvgaoffset = p_nim->tuner.cx24128.rfvgaoffset;

  return(True);
} /* HAMARO_TUNER_CX24128_GetGainSettings */


/*******************************************************************************************************
 * HAMARO_TUNER_CX24128_SetGainSettigns() 
 * function to set the gain settings to tuner and nim  
 *******************************************************************************************************/
BOOL           
HAMARO_TUNER_CX24128_SetGainSettings(HAMARO_NIM           *p_nim,          /* pointer to nim */
                              unsigned long  symbolrateksps)/* symbol rate determines the VCA, VGA settings */
{   
   unsigned long reg_value;
   signed char  power_estimated = 0;
   HAMARO_AMPOUT        ampout;
   HAMARO_VGA1OFFSET    vga1offset;
   HAMARO_VGA2OFFSET    vga2offset;
   HAMARO_RFVGAOFFSET   rfvgaoffset;
   
   HAMARO_TUNER_CX24128_VALIDATE(p_nim);
   
   
   
   symbolrateksps = 0; /* Unused. However, the prototype should match HAMARO_TUNER_SetGainSettings function pointer */
   
   if (HAMARO_GetPowerEstimation(p_nim, &power_estimated) == False)
   {
      return (False);
   }

   /* Read 0x1D, and clear 0x1D[3:0] */
   if (HAMARO_TunerRegisterRead(p_nim, (unsigned short)(0x1D), (unsigned long*)&reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   reg_value &= 0xF0; 
   
   if (power_estimated > p_nim->tuner.cx24128.tuner_gain_thresh) /* in dBm */
   { /* SET2 (Optimized for IIP3) high signal level*/
      p_nim->tuner.cx24128.gain_setting_selection = HAMARO_SIGNAL_LEVEL_HIGH;
      rfvgaoffset = HAMARO_RFVGAOFFSET_0; 
      vga1offset  = HAMARO_VGA1OFFSET_7;   
      vga2offset  = HAMARO_VGA2OFFSET_7;  
      ampout      = HAMARO_AMPOUT_25DB; 
   }
   else /* SET1 (Optimized for NF) - low signal level*/
   {
      p_nim->tuner.cx24128.gain_setting_selection = HAMARO_SIGNAL_LEVEL_LOW; 
      rfvgaoffset = HAMARO_RFVGAOFFSET_2; 
      vga1offset  = HAMARO_VGA1OFFSET_2;   
      vga2offset  = HAMARO_VGA2OFFSET_6;  
      ampout      = HAMARO_AMPOUT_25DB; 
   }
   
   reg_value |= ((unsigned char)ampout & 0x0F);
   
   if(HAMARO_TunerRegisterWrite(p_nim, (unsigned short)(0x1D), (unsigned long)reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   
   p_nim->tuner.cx24128.ampout = ampout;

   /* Read 0x1F, and clear 0x1F[5:0] */
   if (HAMARO_TunerRegisterRead(p_nim, (unsigned short)(0x1F), (unsigned long*)&reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   reg_value &= 0xC0; 
   
   reg_value |= ((unsigned char)vga1offset & 0x07); /* 0x1F[2:0] */ 
   
   p_nim->tuner.cx24128.vga1offset = vga1offset;
   
   
   reg_value |= (((unsigned char)vga2offset << 3) & 0x38); /* 0x1F[5:3] */

   if(HAMARO_TunerRegisterWrite(p_nim, (unsigned short)(0x1F), (unsigned long)reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   p_nim->tuner.cx24128.vga2offset = vga2offset;

   /* Read 0x20, and clear 0x20[3:2] */
   if (HAMARO_TunerRegisterRead(p_nim, (unsigned short)(0x20), (unsigned long*)&reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   reg_value &= 0xF3; 
   
   reg_value |= (((unsigned char)rfvgaoffset << 2) & 0x0C);
   
   if(HAMARO_TunerRegisterWrite(p_nim, (unsigned short)(0x20), (unsigned long)reg_value, HAMARO_IO_UNKNOWN) != True)  
   {
      return(False);
   }
   
   p_nim->tuner.cx24128.rfvgaoffset = rfvgaoffset;
   
   return(True);
} /* HAMARO_TUNER_CX24128_SetGainSettings */


/*******************************************************************************************************
 * HAMARO_TUNER_CX24128_GetClkInversion() 
 * gets the current clock inversion status, returns value to caller 
 *******************************************************************************************************/
BOOL     
HAMARO_TUNER_CX24128_GetClkInversion(HAMARO_NIM      *p_nim,          /* pointer to nim */
                              BOOL     *p_clkinversion) /* clock inversion value */
{
  HAMARO_TUNER_CX24128_VALIDATE(p_nim);

  *p_clkinversion = p_nim->tuner.cx24128.clkinversion;

  return(True);

}  /* HAMARO_TUNER_CX24128_GetClkInversion() */

/*******************************************************************************************************
 * HAMARO_TUNER_CX24128_GetEnableRegister() 
 * read the enable bits from tuner 
 *******************************************************************************************************/
BOOL     
HAMARO_TUNER_CX24128_GetEnableRegister(HAMARO_NIM     *p_nim,       /* pointer to nim */
                                unsigned char *p_enable) /* enable bits */
{
  unsigned char reg_value;

  HAMARO_TUNER_CX24128_VALIDATE(p_nim);

  /* read the enable bits from the tuner. read 0x21[5:0] */
  if(HAMARO_TunerRegisterRead(p_nim, (unsigned short)(0x21), (unsigned long*)&reg_value, HAMARO_IO_UNKNOWN) != True)  
     return(False);

  *p_enable = reg_value & 0x3F;

  return(True);
}  /* HAMARO_TUNER_CX24128_GetEnableRegister() */

⌨️ 快捷键说明

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