ct_nim.c

来自「DVB软件,基于CT216软件的开发源程序.」· C语言 代码 · 共 1,736 行 · 第 1/3 页

C
1,736
字号
#include <stdio.h>
#include "ct_type.h"
#include "ct_gpio.h"
#include "ct_iic.h"

#include "ct_os.h"

#include "io_config.h"
#include "nim_config.h"

#include "ct_nim.h"
#include "ct_demux.h"
#include "ct_decsa.h"
#include "ct_adc.h"

#include "ct221.h"

#include "ct_video.h"                    //(GFG-002)
//#include "dvb_type.h"                  //(GFG-002)

#define MS_PER_TICKS 		CTOS_MS_PER_TICKS

#define COUNT_50_MSEC (50	/ MS_PER_TICKS)
#define COUNT_1_SEC (1000 / MS_PER_TICKS)
#define COUNT_5_SEC (5000 / MS_PER_TICKS)
#define COUNT_10_SEC (10000 / MS_PER_TICKS)
#define NIM_CFG_BASE					0x80010004

#define MAX_SNR	3200//32.00

//#define CT_NIM_VOID
#ifdef CT_NIM_VOID
bool8 b8NIMDebugMode = FALSE;
#endif

/*******************************************************************************************/       

#if 0
#define CTNIM_MSG(p) 		printf p
#else
#define CTNIM_MSG(p)
#endif

#if 0
#define CTNIM_DBG(p) 		printf p
#else
#define CTNIM_DBG(p)
#endif

#define abs(a)	(((a)>0) ? (a) : (-(a))) /*!< Define ABS(a,b) */

/*******************************************************************************************/       

/* Global variables */
static u8 u8chswitch = 0;
static u16 _u16KeyInterrupt;
static bool8 b8InitSuccess=FALSE;
static u32 u32TempFreq = 0;
static u16 u16TempBW = 0;

static u32 u32OffSetFreq = 0;
static u8 u8SDRstCnt = 0;

extern TUNER_InitParams Tuner_Init;
static bool8 b8InitTunerType=FALSE;

#ifdef TWIN_TUNER
static EN_CT_GPIO_PORT enNim1_Reset_Port, enNim1_Polar_Port, enNim1_Power_Port;
static EN_CT_GPIO_BIT enNim1_Reset_Bit, enNim1_Polar_Bit, enNim1_Power_Bit;
#endif
//static EN_CT_GPIO_PORT enNim0_Reset_Port, enNim0_Polar_Port;
//static EN_CT_GPIO_BIT enNim0_Reset_Bit, enNim0_Polar_Bit; 

static EN_CT_GPIO_PORT enNim0_Power_Port = EN_CT_GPIO_PORT_INVALID;
static EN_CT_GPIO_BIT enNim0_Power_Bit = EN_CT_GPIO_BIT_INVALID;

#ifdef CONFIG_2
static EN_CT_NIM_BUS_ID stBusID = EN_CT_NIM_BUS_1;
#else
static EN_CT_NIM_BUS_ID stBusID = EN_CT_NIM_BUS_0;
#endif

static EN_CT_ADC_PORT enADCPort = EN_CT_ADC_PORT_INVALID;

/*
 * Using 2WS bus read NIM module register value
 */
EN_DRV_RESULT CT_NIM_ReadReg(u16 u16RegIndex, u8 *pu8Buf, u16 u16Len)
{
	#ifdef CT_OS_CHECK_STACKSIZE
	   CT_OS_RecordTaskStack();
    #endif
    
	RegRead(DevID, u16RegIndex, pu8Buf, u16Len);
	return DRV_OK;
}

/*
 * Using 2WS Bus write NIM module register value
 */
EN_DRV_RESULT CT_NIM_WriteReg(u16 u16RegIndex, u8 *pu8Buf, u16 u16Len)
{
	#ifdef CT_OS_CHECK_STACKSIZE
	   CT_OS_RecordTaskStack();
    #endif
	
	RegWrite(DevID, u16RegIndex, pu8Buf, u16Len);    
	return DRV_OK;
}

/*
 * DiSEqC response read
 */
EN_DRV_RESULT CT_NIM_ReadDiSEqC(CT_DiSEqC_PARAMETER *pstDiSEqCMsg)
{
	// In DVB_T system, NULL function.
	return DRV_OK;
}

/*
 * DiSEqC command write
 */
EN_DRV_RESULT CT_NIM_WriteDiSEqC(CT_DiSEqC_PARAMETER *stDiSEqCMsg)
{
	// In DVB_T system, NULL function.
	return DRV_OK;
}

/*
 * NIM hardware reset
 */
EN_DRV_RESULT CT_NIM_HwReset(void)
{
	/*
#ifdef CT_OS_CHECK_STACKSIZE
	   CT_OS_RecordTaskStack();
    #endif
#ifdef TWIN_TUNER
	if (CT_NIM_GetBus() == EN_CT_NIM_BUS_0)
	{    
		// NIM hardware reset.
		CT_GPIO_BitSet(enNim0_Reset_Port, enNim0_Reset_Bit, 0);
		//CT_OS_Delay(10);    //250ms
		CT_OS_Delay(25);    //250ms
		CT_GPIO_BitSet(enNim0_Reset_Port, enNim0_Reset_Bit, 1);
	}
	else
	{
		// NIM hardware reset.
		CT_GPIO_BitSet(enNim1_Reset_Port, enNim1_Reset_Bit, 0);
		//CT_OS_Delay(10);    //250ms
		CT_OS_Delay(25);    //250ms
		CT_GPIO_BitSet(enNim1_Reset_Port, enNim1_Reset_Bit, 1);		
	}
#else
	// NIM hardware reset.
	CT_GPIO_BitSet(enNim0_Reset_Port, enNim0_Reset_Bit, 0);
	//CT_OS_Delay(10);    //250ms
	CT_OS_Delay(25);    //250ms
	CT_GPIO_BitSet(enNim0_Reset_Port, enNim0_Reset_Bit, 1);
#endif
*/
	return DRV_OK;
}

/*
 * NIM hardware reset
 */
EN_DRV_RESULT CT_NIM_SwReset(void)
{
	#ifdef CT_OS_CHECK_STACKSIZE
	   CT_OS_RecordTaskStack();
    #endif

	#ifdef CT_NIM_VOID
	if(b8NIMDebugMode)
	{
		return DRV_OK;
	}
	#endif
    
    if(b8InitSuccess == FALSE)
    {
		if(CT221_soft_reset())
		{
			if (Tuner_Init.enADCFrequency == EN_ADC_DQPLL)
				CTNIM_MSG(("DQPLL version\n"));
			else if (Tuner_Init.enADCFrequency == EN_ADC_FREQ5625)
				CTNIM_MSG(("FREQ5625 version\n"));
			else if (Tuner_Init.enADCFrequency == EN_ADC_FREQ555)
				CTNIM_MSG(("FREQ555 version\n"));
			else if (Tuner_Init.enADCFrequency == EN_ADC_FREQ5528)
				CTNIM_MSG(("FREQ5528 version\n"));
			else if (Tuner_Init.enADCFrequency == EN_ADC_FREQ4995)
				CTNIM_MSG(("FREQ4995 version\n"));			
			CTNIM_MSG(("\n>> CT_NIM_Initialise : Success"));
			b8InitSuccess=TRUE;        
	        return DRV_OK;
	    }
		else
		{
			CTNIM_MSG(("\n>> CT_NIM_Initialise : Failed"));
			return DRVERR_NOTOK;
		}
	}
	return DRV_OK;
}

/*
 * NIM initialise
 */
EN_DRV_RESULT CT_NIM_Initialise(void)
{
	#ifdef CT_OS_CHECK_STACKSIZE
	   CT_OS_RecordTaskStack();
    #endif
#ifdef TWIN_TUNER
	    if( IOCFG_GetIOLocation(EN_LNB_0_POLAR, &enNim0_Polar_Port, &enNim0_Polar_Bit)==DRVERR_NOTOK )
	    {
	        CTNIM_DBG(("Init Nim0 Polar Pin Fail.\n"));
	        return DRVERR_NOTOK;
	    }

	    if( IOCFG_GetIOLocation(EN_LNB_0_POWER, &enNim0_Power_Port, &enNim1_Power_Bit)==DRVERR_NOTOK )
	    {
	        CTNIM_DBG(("Init Nim0 Power Pin Fail.\n"));
	        return DRVERR_NOTOK;
	    }
	    
	    if( IOCFG_GetIOLocation(EN_LNB_1_POLAR, &enNim1_Polar_Port, &enNim1_Polar_Bit)==DRVERR_NOTOK )
	    {
	        CTNIM_DBG(("Init Nim1 Polar Pin Fail.\n"));
	        return DRVERR_NOTOK;
	    }

	    if( IOCFG_GetIOLocation(EN_LNB_1_POWER, &enNim1_Power_Port, &enNim1_Power_Bit)==DRVERR_NOTOK )
	    {
	        CTNIM_DBG(("Init Nim1 Power Pin Fail.\n"));
	        return DRVERR_NOTOK;
	    }
#else	
	if (CT_NIM_GetBus() == EN_CT_NIM_BUS_0)
	{	/*
	    if( IOCFG_GetIOLocation(EN_LNB_0_POLAR, &enNim0_Polar_Port, &enNim0_Polar_Bit)==DRVERR_NOTOK )
	    {
	        CTNIM_DBG(("Init Nim0 Polar Pin Fail.\n"));
	        return DRVERR_NOTOK;
	    }
		*/
	    if( IOCFG_GetIOLocation(EN_LNB_0_POWER, &enNim0_Power_Port, &enNim0_Power_Bit)==DRVERR_NOTOK )
	    {
	        CTNIM_DBG(("Init Nim0 Power Pin Fail.\n"));
	        //return DRVERR_NOTOK;
	    }
    }
    else
    {	/*
	    if( IOCFG_GetIOLocation(EN_LNB_1_POLAR, &enNim0_Polar_Port, &enNim0_Polar_Bit)==DRVERR_NOTOK )
	    {
	        CTNIM_DBG(("Init Nim1 Polar Pin Fail.\n"));
	        return DRVERR_NOTOK;
	    }
		*/
	    if( IOCFG_GetIOLocation(EN_LNB_1_POWER, &enNim0_Power_Port, &enNim0_Power_Bit)==DRVERR_NOTOK )
	    {
	        CTNIM_DBG(("Init Nim1 Power Pin Fail.\n"));
	        //return DRVERR_NOTOK;
	    }
    }
#endif    

	if( IOCFG_GetADCLocation(EN_ADC_NIM_RFAGC, &enADCPort) != DRV_OK )
	{
	    CTNIM_DBG(("No NIM_RFAGC assign.\n"));
	}

	if(b8InitTunerType == FALSE)
	{
    	Tuner_Init.enTunerType = DEFAULT_TUNER;
    	Tuner_Init.enCountry = DEFAULT_COUNTRY;
    	Tuner_Init.enAISLSetting = DEFAULT_AISL;
    
   		CT221_InitValueByTuner(Tuner_Init.enTunerType);
   	}
     
    return DRV_OK;
}
/*
 * Get NIM signal ber current
 */
EN_DRV_RESULT CT_NIM_GetBer(CT_INT *pBer)
{
	return DRV_OK;
}
/*
 * Get NIM signal quality current
 */
EN_DRV_RESULT CT_NIM_GetQuality(u16 *pu16Quality)
{
	#ifdef CT_OS_CHECK_STACKSIZE
	   CT_OS_RecordTaskStack();
    #endif
  
    u8 u8Quality;
    u32 u32SNR;
    SYS_INFO sys_info;
    u8 u8Mode, u8Code_rate;
    u32 u32CN_Limit = 0;
    u32 temp;
    u16 pu16LockStatus;

	#ifdef CT_NIM_VOID
	if(b8NIMDebugMode)
	{
		*pu16Quality = 50;
		return DRV_OK;
	}
	#endif
       
	if ( b8InitSuccess == FALSE )
	{
		*pu16Quality = 0;
		CTNIM_DBG(("\nb8Init Fail"));
		return DRVERR_NOTOK;
	}       
       
	CT221_GetSysInfo(&sys_info);
	   
	switch( sys_info.mod )
	{  
		case QPSK:      u8Mode=0;       break;
		case QAM_16:    u8Mode=1;       break;
		case QAM_64:    u8Mode=2;       break;
		default:
			*pu16Quality = 0;
			return DRVERR_NOTOK;
	}	 
	   
	switch( sys_info.hpcr )
	{  
		case CR_1over2:     u8Code_rate=0;      break;
		case CR_2over3:     u8Code_rate=1;      break;
		case CR_3over4:     u8Code_rate=2;      break;
		case CR_5over6:     u8Code_rate=3;      break;
		case CR_7over8:     u8Code_rate=4;      break;
		default:
			*pu16Quality = 0; 
			return DRVERR_NOTOK;
	}  
       
    switch(u8Mode)
    {  
        case 0:     //QPSK
            switch(u8Code_rate)
            {
                case 0:      u32CN_Limit=4;      break;    //1/2
                case 1:      u32CN_Limit=5;      break;    //2/3
                case 2:      u32CN_Limit=6;      break;    //3/4
                case 3:      u32CN_Limit=7;      break;    //5/6
                case 4:      u32CN_Limit=7;      break;    //7/8
				default:
					*pu16Quality = 0;
					return DRVERR_NOTOK;
            }
            break;
        case 1:     //16QAM
            switch(u8Code_rate)
            {
                case 0:      u32CN_Limit=10;      break;    //1/2
                case 1:      u32CN_Limit=11;      break;    //2/3
                case 2:      u32CN_Limit=12;      break;    //3/4
                case 3:      u32CN_Limit=13;      break;    //5/6
                case 4:      u32CN_Limit=13;      break;    //7/8
				default:
					*pu16Quality = 0;
					return DRVERR_NOTOK;
            }
            break;
        case 2:     //64QAM
            switch(u8Code_rate)
            {
                case 0:      u32CN_Limit=17;      break;    //1/2
                case 1:      u32CN_Limit=17;      break;    //2/3
                case 2:      u32CN_Limit=18;      break;    //3/4
                case 3:      u32CN_Limit=19;      break;    //5/6
                case 4:      u32CN_Limit=20;      break;    //7/8
				default:
					*pu16Quality = 0;
					return DRVERR_NOTOK;
            }            
            break;
		default:
			*pu16Quality = 0;
			return DRVERR_NOTOK;
    }  
    u32CN_Limit*=100;
       
    u32SNR = CT221_Get_SNR();
	CTNIM_MSG(("\nSNR= %d u32CN_Limit = %d", u32SNR, u32CN_Limit));
    CT_NIM_CheckLock(&pu16LockStatus);   
    
	if(CT221_TPS_Lock_Status() == TRUE)
	{
//		CTNIM_DBG(("\nSNR = %d",u8SNR));
		if (u32SNR > MAX_SNR)
		{  
			 u32SNR = MAX_SNR;
		}  
		   
	    if (u32SNR > u32CN_Limit)    //CN is good enough for no error receive, quality will be normalized to 40up.
		{  
		    if(u32SNR < MAX_SNR) 
		    {     
	            temp = 60 * (u32SNR - u32CN_Limit);
	            temp /= (MAX_SNR - u32CN_Limit);
	            temp += 40;
	        }
	        else
	        {
	            temp = 100;
	        }    
	    }  
	    else                    //CN is not good enough for no error receive, quality will be normalized from 0 to 40.
		{  
		    if(u32SNR >0)
		    {        
	            temp = 35 * (u32SNR - 0);
	            temp /= (u32CN_Limit - 0);
	            temp +=5;
	        }
	        else
	        {
	            temp = 0;
	        }      
		}  
		u8Quality = (u8) temp;
	}
	else
	{
		u8Quality = 0;
	}
		
	CTNIM_MSG(("\nFrontend u8Quality = %d", u8Quality)); 
    
	*pu16Quality = u8Quality;

	return DRV_OK;
}

/*
 * Get NIM signal level current
 */
EN_DRV_RESULT CT_NIM_GetStrength(u16 *pu16Strength)
{
	#ifdef CT_OS_CHECK_STACKSIZE
	   CT_OS_RecordTaskStack();
    #endif

	#ifdef CT_NIM_VOID
	if(b8NIMDebugMode)
	{
		*pu16Strength = 50;
		return DRV_OK;
	}
	#endif

	u32 u32ADCCode=0;
	u8 u8AGCLevel; 
    u8 u8Strength=0;
    u16 pu16LockStatus;
    
    if ( b8InitSuccess==FALSE )
	{
		*pu16Strength = 0;
		return DRVERR_NOTOK;
	}

	u8AGCLevel = CT221_AGCLevel();
	if( CT_ADC_GetCode(enADCPort, &u32ADCCode) != DRV_OK)
	{
	   //printf("CT_ADC_GetCode fail\n");
	   u32ADCCode = 0xff;
	}

	//printf("u8AGCLevel:%hx  ", u8AGCLevel);
	//printf("u32ADCCode:%hx\n", u32ADCCode); 
    
    CT_NIM_CheckLock(&pu16LockStatus);   
	if(pu16LockStatus == TRUE)
	{
		if  (Tuner_Init.enTunerType == EN_TUNER_DTT74530)
		{
			if(u8AGCLevel >= 0xE0)			//0xE0
			{//0
				u8Strength = 0;
			}	
			else if(u8AGCLevel >= 0xB0)		//0x60
			{//0~40
				u8Strength = (39 * (0xE0 - u8AGCLevel)) / (0xE0 - 0xB0);			
			}			
			else if(u8AGCLevel >= 0xAD)		//0x40
			{//40~60
				u8Strength = (19 * (0xB0 - u8AGCLevel)) / (0xB0 - 0xAD);
				u8Strength += 40;
			}
			else if(u8AGCLevel > 0xA1)		//0x2A
			{//60~75
				u8Strength = (14 * (0xAD - u8AGCLevel)) / (0xAD - 0xA1);
				u8Strength += 60;
			}
			else
			{
				if(u32ADCCode >= 0xE5)		//0xCF
				{//75
					u8Strength = 75;	
				}
				else if(u32ADCCode >= 0xC0)	//0xA0
				{//75~80
					u8Strength = (14 * (0xE5 - u32ADCCode)) / (0xE5 - 0xC0);
					u8Strength += 75; 
				}
				else if(u32ADCCode >= 0x40)	//0x5D
				{//80~100
					u8Strength = (19 * (0xC0 - u32ADCCode)) / (0xC0 - 0x40);
					u8Strength += 80;			
				}
				else
				{
					u8Strength = 99;
				}
			}
			
			u8Strength += ((u8AGCLevel & 0x01) + (u32ADCCode & 0x01)); 					
		}
		else if (Tuner_Init.enTunerType == EN_TUNER_DTT2004)
		{	
			if(u8AGCLevel >= 0xE0)
			{//0
				u8Strength = 0;
			}
			else if(u8AGCLevel >= 0x72)
			{//0~40
				u8Strength = (39 * (0xE0 - u8AGCLevel)) / (0xE0 - 0x72);			
			}			
			else if(u8AGCLevel >= 0x60)
			{//40~60
				u8Strength = (19 * (0x72 - u8AGCLevel)) / (0x72 - 0x60);
				u8Strength += 40;
			}
			else if(u8AGCLevel > 0x53)
			{//60~75
				u8Strength = (14 * (0x60 - u8AGCLevel)) / (0x60 - 0x53);
				u8Strength += 60;
			}
			else
			{
				if(u32ADCCode >= 0x90)
				{//75
					u8Strength = 75;	
				}
				else if(u32ADCCode >= 0x6F)
				{//75~80
					u8Strength = (14 * (0x90 - u32ADCCode)) / (0x90 - 0x6F);
					u8Strength += 75; 
				}
				else if(u32ADCCode >= 0x3A)
				{//80~100
					u8Strength = (19 * (0x6F - u32ADCCode)) / (0x6F - 0x3A);
					u8Strength += 80;			
				}
				else
				{
					u8Strength = 99;
				}
			}
			
			u8Strength += ((u8AGCLevel & 0x01) + (u32ADCCode & 0x01)); 
		}
		else if  (Tuner_Init.enTunerType == EN_TUNER_TD1611)

⌨️ 快捷键说明

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