mxl5005_ct221.c

来自「DVB软件,基于CT216软件的开发源程序.」· C语言 代码 · 共 324 行

C
324
字号
/*******************************************************************************
 * Program MXL5005 tuner
 *******************************************************************************/
#include <string.h>
#include "ct_sys.h"
#include "ct_iic.h"
#include "ct_nim.h"
#include "ct_os.h"

//#include "Data_Type.h"
#include "ct_type.h"
//#include <iostream.h>
#include <stdio.h>
#include "MXL5005_ct221.h"

//#include "IIC_Connector.h"

Tuner_struct TunerTmp;
Tuner_struct *Tuner = &TunerTmp;
/*u8	AGC_MASTER_BYTE ; */

//#include "stdafx.h"
//#include "MXL5005_c.h"

//#include <stdio.h>
//#include <windows.h>

//#using <mscorlib.dll>

//using namespace System;

u8	AGC_MASTER_BYTE ;
u8	Reg_Addr[104] ;
u8	Reg_Data[104] ;
u8	Temp_Data[10] ;
int		count ;

AGC_Mode			agc_mode ;
Tuner_Mode			mode ;
Tuner_IF_Mode		if_mode ;	
u32				bandwidth ;
u32				if_out ;
u32				fxtal ;
u16				top ;
u16				if_out_load ;
Tuner_Clock_Out		clock_out ;
Tuner_Div_Out		div_out ;
Tuner_Cap_Select	capselect ;
Tuner_RSSI			en_rssi ;
u8			mod_type ;
u8			tf_type ;
u32			rf_in ;

bool8 RegWrite(u8 deviceId, u8 address, u8 *buffer, u8 bytenum);

bool8 CT221_PD_MAX5003(void)
{
	unsigned int status = 0;
	
	status += MXL_GetMasterControl(Reg_Data, MC_POWER_DOWN) ;
	Reg_Addr[0] = MASTER_CONTROL_ADDR ;
	Reg_Data[0] |= AGC_MASTER_BYTE ;
	status += Write_I2C(DeviceID, Reg_Addr, Reg_Data, 1) ;
	CT_OS_MS_Delay(3);
}
bool8 CT221_ProgramTuner_MAX5003(u32 rfFreqKhz, u8 dwBWandFreqkHz)
{
	unsigned int status = 0;
	//u8 WriteBytes[5];
//	Tuner_struct *Tuner = malloc(sizeof(Tuner_struct));
	u32	if_divval_temp ;

	//printf("\n Acquire MXL5003 %d  %d",rfFreqKhz,dwBWandFreqkHz);

//	Tuner = malloc(sizeof(Tuner_struct));

	mode = MXL_DIGITAL_MODE ;         // Options - MXL_ANALOG_MODE= 0 
									  //           MXL_DIGITAL_MODE = 1
	if_mode = MXL_ZERO_IF ;           // Options - MXL_ZERO_IF = 0
									  //           MXL_LOW_IF = 1
	bandwidth = dwBWandFreqkHz * 1000000;
	//if_out = 36125000;
	//if_out = 36150000;
	if_out = 4570000;
	//if_out = 5200000;
	fxtal = 16000000;
	agc_mode    = MXL_SINGLE_AGC ;    // Options - MXL_DUAL_AGC = 0 
	//           MXL_SINGLE_AGC = 1
	top         = 252 ;
	if_out_load = 200 ;
	clock_out   = MXL_CLOCK_OUT_DISABLE ;	// Options - MXL_CLOCK_OUT_DISABLE = 0
											//           MXL_CLOCK_OUT_ENABLE = 1
	div_out     = MXL_DIV_OUT_4 ;			// Options - MXL_DIV_OUT_1 = 0
											//           MXL_DIV_OUT_4 = 1
	capselect   = MXL_CAP_SEL_ENABLE ;		// Options - MXL_CAP_SEL_DISABLE = 0
											//           MXL_CAP_SEL_ENABLE = 1
	en_rssi     = MXL_RSSI_ENABLE ;			// Options - MXL_RSSI_DISABLE = 0 
											//           MXL_RSSI_ENABLE = 1
	mod_type    = MXL_DVBT ;				// Options - MXL_DEFAULT_MODULATION = 0  
											//           MXL_DVBT = 1
											//           MXL_ATSC = 2 
											//           MXL_QAM = 3 
											//           MXL_ANALOG_CABLE = 4
	tf_type     = MXL_TF_C_H ;            // Options - MXL_TF_DEFAULT = 0 
										  //           MXL_TF_OFF = 1 
										  //           MXL_TF_C = 2 
										  //           MXL_TF_C_H = 3
										  //           MXL_TF_D = 4
  										  //		   MXL_TF_D_L = 5
  										  //           MXL_TF_E = 6
										  //           MXL_TF_F = 7
	rf_in       = rfFreqKhz*1000 ;


	if (agc_mode == MXL_DUAL_AGC) // DUAL AGC
		AGC_MASTER_BYTE = 0x04 ;
	else
		AGC_MASTER_BYTE = 0x00 ;

	status += MXL_GetMasterControl(Reg_Data, MC_SYNTH_RESET) ;
	Reg_Addr[0] = MASTER_CONTROL_ADDR ;
	Reg_Data[0] |= AGC_MASTER_BYTE ;
	status += Write_I2C(DeviceID, Reg_Addr, Reg_Data, 1) ;
	CT_OS_MS_Delay(3);
	status += MXL5005_TunerConfig( Tuner, mode, if_mode, bandwidth, if_out, fxtal, agc_mode, top, if_out_load, 
			clock_out, div_out, capselect, en_rssi, mod_type, tf_type) ;

	//
	// retrieve the init registers to write to tuner
	//
	status += MXL_GetInitRegister(Tuner, Reg_Addr, Reg_Data, &count);

	//
	// End Init
	// Write Init Registers to Tuner
	//
	status += Write_I2C(DeviceID, Reg_Addr, Reg_Data, count) ;

	// 
	// Determine if the tuner is in Zero-IF mode or Analog Low-IF mode
	//
	if (if_mode == MXL_ZERO_IF) // Analog or Digital Zero IF mode
	{
		//
		// Set MasterControl = Synth_Reset
		//

		status += MXL_GetMasterControl(Reg_Data, MC_SYNTH_RESET) ;
		Reg_Addr[0] = MASTER_CONTROL_ADDR ;
		Reg_Data[0] |= AGC_MASTER_BYTE ;
		status += Write_I2C(DeviceID, Reg_Addr, Reg_Data, 1) ;

		// RF Tune
		status += MXL_TuneRF(Tuner, rf_in) ;

		//
		// Store the calculated IF_DIVVAL value into IF_Divval
		//
		status += MXL_ControlRead(Tuner, IF_DIVVAL, &if_divval_temp) ;
		status += MXL_ControlWrite(Tuner, SEQ_FSM_PULSE, 0) ;
		status += MXL_ControlWrite(Tuner, SEQ_EXTPOWERUP, 1) ;
		status += MXL_ControlWrite(Tuner, IF_DIVVAL, 8) ;
		status += MXL_GetCHRegister(Tuner, Reg_Addr, Reg_Data, &count) ;

		//
		// Set MasterControl = Load_Start
		//
		status += MXL_GetMasterControl(Temp_Data, MC_LOAD_START) ;
		Reg_Addr[count] = MASTER_CONTROL_ADDR ;
		Temp_Data[0] |= AGC_MASTER_BYTE ;
		Reg_Data[count] = Temp_Data[0] ;
		status += Write_I2C(DeviceID, Reg_Addr, Reg_Data, count+1) ;

		// Wait 30 ms for calibration to complete
		CT_OS_MS_Delay(30) ;

		status += MXL_ControlWrite(Tuner, SEQ_FSM_PULSE, 1) ;
		status += MXL_ControlWrite(Tuner, IF_DIVVAL, if_divval_temp) ;
		status += MXL_GetCHRegister_ZeroIF(Tuner, Reg_Addr, Reg_Data, &count) ;

		//
		// Set MasterControl = Load_Start
		//
		status += MXL_GetMasterControl(Temp_Data, MC_LOAD_START) ;
		Reg_Addr[count] = MASTER_CONTROL_ADDR ;
		Reg_Data[count] = Temp_Data[0] | AGC_MASTER_BYTE ;

		//
		// Set MasterControl = Load_Start
		// Write all the data into chip and Latch
		// 
		status += Write_I2C(DeviceID, Reg_Addr, Reg_Data, count+1) ;
	}
	else // Analog Low IF mode
	{
		// RF Tune; 
		status += MXL_TuneRF(Tuner, rf_in) ;

		status += MXL_ControlWrite(Tuner, SEQ_EXTPOWERUP, 1) ;
		status += MXL_ControlWrite(Tuner, SEQ_FSM_PULSE, 0) ;
		status += MXL_GetCHRegister(Tuner, Reg_Addr, Reg_Data, &count) ;

		// Write Chan Change Registers to Tuner
		//		status += Write_I2C(DeviceID, Reg_Addr, Reg_Data, count) ;
		//
		// Set MasterControl = Load_Start
		//
		status += MXL_GetMasterControl(Reg_Data, MC_LOAD_START) ;
		Reg_Addr[0] = MASTER_CONTROL_ADDR ;
		Reg_Data[0] |= AGC_MASTER_BYTE ;
		//		status += Write_I2C(DeviceID, Reg_Addr, Reg_Data, 1) ;

		//CT_OS_MS_Delay(100);

		//
		// Set MasterControl = Seq_Off
		//
		status += MXL_GetMasterControl(Reg_Data, MC_SEQ_OFF) ;
		Reg_Addr[0] = MASTER_CONTROL_ADDR ;
		Reg_Data[0] |= AGC_MASTER_BYTE ;

		//		status += Write_I2C(DeviceID, Reg_Addr, Reg_Data, 1) ;

		status += MXL_ControlWrite(Tuner, SEQ_EXTPOWERUP, 0) ;
		status += MXL_GetCHRegister_LowIF(Tuner, Reg_Addr, Reg_Data, &count) ;

		// Write Chan Change Registers to Tuner for Seq 3
		//		status += Write_I2C(DeviceID, Reg_Addr, Reg_Data, count) ;
	}

//	free(Tuner);

	// ==========================================================
	// Turn on TP6101 ADC
//	WriteBytes[0] = 0x01;
//	WriteBytes[1] = 0xf0;
//	WriteBytes[2] = 0x07;
//	WriteBytes[3] = 0xff;
//	WriteBytes[4] = 0x08;

//	status = RegWrite(0xf0, WriteBytes[0], &WriteBytes[1], 4);
	// ----------------------------------------------------------


	return TRUE ;
} 

bool8 Write_I2C(u8 Device_ID, u8 *Reg_Addr,  u8 *Reg_Data, u8 count)
{
	int i;
	u8	u8TempBuffer[3];
	u8 noOfBytes;

	for(i = 0; i < count ; i++)
	{
		u8TempBuffer[0] = Reg_Addr[i];
		u8TempBuffer[1] = Reg_Data[i];
		
		//printf("\n\r[Count = %d] [0x%02x] = [0x%02x]",count, u8TempBuffer[0], u8TempBuffer[1]);
		
		//u8TempBuffer[2] = 254;
		noOfBytes = 2;
		//CT221_Repeat(DEMOD_ADDR);
				
		if (!Tuner_Write(&u8TempBuffer[0], noOfBytes))
		{
			printf("\n\r Write I2C Fail (01)");
			return FALSE;
		}	
	}

	u8TempBuffer[0] = 254;
	noOfBytes = 2;
	//printf("\n\r [0x%02x] = [0x%02x]\n\n", u8TempBuffer[0], u8TempBuffer[1]);
	//printf("\n\r Send Latch");
	
	//CT221_Repeat(DEMOD_ADDR);
		
	//if (!RegisterWrite2wb(DeviceID, u8TempBuffer[0], &u8TempBuffer[1], noOfBytes))
	if(!Tuner_Write(&u8TempBuffer[0], noOfBytes))
	{
		printf("\n\r Write I2C Fail (01)");
		return FALSE;
	}		
	
	return TRUE;
}
/*
bool8 Tuner_Write(u8 *pu8Buffer , u8 u8Length)
{
    u32 u32Handle;	
    
	if (CT_NIM_GetBus() == EN_CT_NIM_BUS_0)
	{
		if( CT_IIC_Open(EN_CT_IIC_BUS_0, TUNER_Addr, &u32Handle)!= DRV_OK)
		{		
			printf("\nCT_IIC_Open 0 Error");
	        return FALSE;
	    }
	}
	else
	{
		if( CT_IIC_Open(EN_CT_IIC_BUS_1, TUNER_Addr, &u32Handle)!= DRV_OK)
		{		
			printf("\nCT_IIC_Open 1 Error");
	        return FALSE;
	    }		
	}
#ifdef NIM_SW_IIC
	if(CT_SW_IIC_WriteData(u32Handle, pu8Buffer, u8Length) != DRV_OK )
#else
	if(CT_IIC_WriteData(u32Handle, pu8Buffer, u8Length) != DRV_OK )
#endif
	{		
		printf("\nCT_IIC_WriteData Error");
		CT_IIC_Close(u32Handle);
		return(FALSE);
	}
	
	CT_IIC_Close(u32Handle);
	return (TRUE);     
}
*/

⌨️ 快捷键说明

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