📄 lgs_8gl5.c
字号:
/***************************************************************************************
*
* (c) copyright 2007,legendsilicon,beijing,China
*
* All Rights Reserved
*
*
* File Name : LGS8GL5.c
*
* Programmer(s) : wangying
*
* Date Created : 2007.07.25
*
* Date Updated :
*
* Current Revision : V1.0.0
*
* Modification History :
*
* Description :
*
***************************************************************************************/
/***************************************************************************************
* INCLUDE FILES
***************************************************************************************/
#include <intrins.h>
#include <stdarg.h>
#include <stdio.h>
#include "lgs_8GL5.h"
#include "lgs_debug.h"
LGS_HANDLE g_lgsHandleI2c;
LGS_HANDLE g_lgsBaseAddress = 0;
LGS_HANDLE g_tunerHandleI2c = 0;
void LGS_RegisterI2C(LGS_OPEN_I2C popen,
LGS_READ_I2C pread,
LGS_WRITE_I2C pwrite,
LGS_CLOSE_I2C pclose)
{
LGS_OpenI2C = popen;
LGS_ReadI2C = pread;
LGS_WriteI2C = pwrite;
LGS_CloseI2C = pclose;
}
void LGS_RegisterWait(LGS_WAIT wait)
{
LGS_Wait = wait;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UINT8 LGS_ComputeSectionAddress(UINT8 regAddr, UINT8 *psecAddr)
{
//Sect 1: 0x00 ~ 0xBF; Sect 2: 0xC0 ~ 0xFF , Jay
if (regAddr >= 0x00 && regAddr <= 0xBF)
{
*psecAddr = LGS8gL5ADDR ;
}
else if (regAddr >= 0xC0 && regAddr <= 0xFF)
{
*psecAddr = LGS8gL5ADDR + 4;
}
else
return LGS_REGISTER_ERROR; //illegal register address
return LGS_NO_ERROR;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
INT8 LGS_WriteRegister(UINT8 regAddr, UINT8 regVal)
{
UINT8 secAddr;
UINT8 buffer[4];
INT8 err = LGS_NO_ERROR;
err = LGS_ComputeSectionAddress(regAddr, &secAddr);
if ( err != LGS_NO_ERROR )
{
LGS_Debug(("\t LGS_ComputeSectionAddress is failed!\n"));
goto failed;
}
buffer[0] = regAddr;
buffer[1] = regVal;
err = LGS_OpenI2C(&g_lgsHandleI2c, secAddr, 1);
if (err != LGS_NO_ERROR)
{
LGS_Debug(("\t LGS_OpenI2C is failed!\n"));
goto failed;
}
err = LGS_WriteI2C(g_lgsHandleI2c, buffer, 2, 100);
if(err != LGS_NO_ERROR)
{
LGS_Debug(("\t LGS_WriteI2C is failed, setting sectiong(%02x) register(%02x) to %02x!\n",
secAddr, buffer[0], buffer[1]));
goto failed;
}
err = LGS_CloseI2C(g_lgsHandleI2c);
if (err != LGS_NO_ERROR)
{
LGS_Debug(("\t LGS_CloseI2C is failed!\n"));
goto failed;
}
failed:
return err;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
INT8 LGS_ReadRegister(UINT8 regAddr, UINT8 *pregVal)
{
UINT8 secAddr;
UINT8 buffer[4];
INT8 err = LGS_NO_ERROR;
LGS_Debug(("\t Calling LGS_ReadRegister for register_address(%02x)\n", regAddr));
err = LGS_ComputeSectionAddress(regAddr, &secAddr);
if ( err != LGS_NO_ERROR )
{
LGS_Debug(("\t LGS_ComputeSectionAddress is failed!\n"));
goto failed;
}
buffer[0] = regAddr;
err = LGS_OpenI2C(&g_lgsHandleI2c, secAddr, 1);
if (err != LGS_NO_ERROR)
{
LGS_Debug(("\t LGS_OpenI2C is failed!\n"));
goto failed;
}
err = LGS_WriteI2C(g_lgsHandleI2c, buffer, 1, 100);
if(err != LGS_NO_ERROR)
{
LGS_Debug(("\t LGS_WriteI2C is failed!\n"));
goto failed;
}
err = LGS_ReadI2C(g_lgsHandleI2c, pregVal, 1, 100);
if(err != LGS_NO_ERROR)
{
LGS_Debug(("\t LGS_ReadI2C is failed, sectiong(%02x) register(%02x)!\n",
secAddr, regAddr));
goto failed;
}
err = LGS_CloseI2C(g_lgsHandleI2c);
if (err != LGS_NO_ERROR)
{
LGS_Debug(("\t LGS_CloseI2C is failed!\n"));
goto failed;
}
failed:
return err;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UINT8 LGS_SoftReset(void)
{
INT8 err = LGS_NO_ERROR;
UINT8 dat;
err = LGS_ReadRegister(0x02, &dat);
if (err != LGS_NO_ERROR)
return err;
dat = dat & 0xFE;
err = LGS_WriteRegister (0x02, dat);
if (err != LGS_NO_ERROR)
return err;
dat = dat | 0x01;
err = LGS_WriteRegister (0x02, dat);
if (err != LGS_NO_ERROR)
return err;
LGS_Wait(5); // wait 5 ms, allows SNR, AGC, AFC and feedback loops to stabilize
return LGS_NO_ERROR;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
INT8 LGS_SelectADMode(UINT8 mode)
{
UINT8 datax;
if (LGS_ReadRegister(0x07, &datax)) goto failed;
if (mode == INTERNAL_AD)
{
datax &= 0x7C;
}
else
if (mode == EXTERNAL_AD)
{
datax |= 0x83;
}
return LGS_WriteRegister(0x07, datax);
failed:
return LGS_CHANGE_MODE_ERROR;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
INT8 LGS_SetMpegMode(UINT8 serial, UINT8 clkPolarity, UINT8 clkGated)
{
INT8 err = LGS_NO_ERROR;
UINT8 dat;
err = LGS_ReadRegister (0xC2, &dat);
if (err != LGS_NO_ERROR)
return err;
dat = dat & 0xF8;
dat = dat | (serial & 0x01) | (clkPolarity & 0x02) | (clkGated & 0x04);
err = LGS_WriteRegister (0xC2, dat);
if (err != LGS_NO_ERROR)
return err;
err = LGS_SoftReset();
if (err != LGS_NO_ERROR)
return err;
return LGS_NO_ERROR;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UINT8 LGS_SetManualParameters(UINT8 datax)
{
if(LGS_WriteRegister(0x7D, datax)) return LGS_SET_MANUAL_PARAMETERS_ERROR;
LGS_SoftReset();
return LGS_NO_ERROR;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
INT8 LGS_CheckLocked(UINT8 *result)
{
/* returns true if demod CA locked and AFC Locked, false otherwise */
INT8 err = LGS_NO_ERROR;
err = LGS_ReadRegister (0x4B, result);
if (err != LGS_NO_ERROR)
return err;
*result = *result & 0xC0; /* Check Flag CA_Locked and AFC_Locked, Jay */
if (*result == 0xC0)
*result = 1;
else
*result = 0;
return LGS_NO_ERROR;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
UINT8 LGS_SetManualMode(void)
{
UINT8 datax;
if(LGS_ReadRegister(0x7E, &datax)) goto failed;
datax &= 0xFE;
if(LGS_WriteRegister(0x7E,datax )) goto failed;
//FEC self reset activated
if(LGS_ReadRegister(0xC5, &datax)) goto failed;
datax = (datax & 0xE0) | 0x06;
if(LGS_WriteRegister(0xC5, datax)) goto failed;
LGS_SoftReset();
return LGS_NO_ERROR;
failed:
return LGS_DETECT_ERROR;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// reg: 寄存器
// mask:掩码
// val:期望的正确值
// interval:每次循环之间的间隔
UINT8 WaitForLock( UINT8 reg, UINT8 mask, UINT8 val, UINT8 interval,UINT8 times ,UINT8 *real_times)
{
UINT8 i = 0;
UINT8 tmp;
for( i=0; i<times; i++ )
{
LGS_Wait(interval);
LGS_ReadRegister(reg, &tmp);
if( (tmp & mask) == val )
{
*real_times = i;
return true;
}
}
*real_times = i;
return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
UINT8 LGS_AutoDetect(void)
{
UINT8 datax;
UINT8 datay;
INT8 i;
UINT8 j;
UINT8 k;
UINT8 retryNum;
j = 0;
k = 0;
i = -1;
retryNum = 0;
do
{
i++;
if(LGS_ReadRegister( 0x04, &datax )) goto failed;
if(LGS_ReadRegister( 0x37, &datay )) goto failed;
datax &= 0xFC;
switch( i%5 )
{
case 0: datay &= 0x7F; datax |= 0x02; break; // 945 VPN
case 1: datay |= 0x80; datax |= 0x02; break; // 945 CPN
case 2: datay &= 0x7F; datax |= 0x00; break; // 420 VPN
case 3: datay |= 0x80; datax |= 0x00; break; // 420 CPN
case 4: datay |= 0x80; datax |= 0x01; break; // 595 CPN
default: datay &= 0x7F; datax |= 0x02;
}
if(LGS_WriteRegister(0x04, datax )) goto failed;
if(LGS_WriteRegister(0x37, datay )) goto failed;
LGS_SoftReset();
if( false == WaitForLock( 0x4B, 0x80, 0x80, 1,10,&k)) continue;
if( false == WaitForLock( 0xA4, 0x03, 0x01, 5,20,&j))
{
if(LGS_ReadRegister( 0x7C, &datax )) goto failed; //最高位为0:多载波 1:单载波
if((datax & 0x80) == 0x80)
{
datax &= 0x7F;
}
else
{
datax |= 0x80;
}
if(LGS_WriteRegister(0x7C, datax )) goto failed;
retryNum++;
if (retryNum > 2)
{
retryNum = 0;
continue;
}
i--;
continue;
}
if(LGS_ReadRegister(0xA2, &datax)) goto failed;
if(LGS_SetManualParameters(datax) != LGS_NO_ERROR)
continue;
break;
} while( i<(5*2) );
if(i == 5*2 )
{
goto failed;
}
return LGS_NO_ERROR;
failed:
return LGS_DETECT_ERROR;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
UINT8 LGS_SetAutoMode(void)
{
// set automode
UINT8 datax;
if(LGS_ReadRegister(0x03, &datax)) goto failed;
datax &= 0xFE;
if(LGS_WriteRegister(0x03, datax)) goto failed;
if(LGS_ReadRegister(0x7E, &datax)) goto failed;
datax |= 0x01;
if(LGS_WriteRegister(0x7E, datax)) goto failed;
//FEC self reset in never activated
if(LGS_ReadRegister(0xC5, &datax)) goto failed;
datax &= 0xE0;
if(LGS_WriteRegister(0xC5, datax)) goto failed;
//LGS_SoftReset();
return LGS_NO_ERROR;
failed:
return LGS_CHANGE_MODE_ERROR;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
UINT8 LGS_SelectTunerType(UINT8 type)
{
UINT8 datax;
UINT8 tmp[4];
if (LGS_ReadRegister(0x07, &datax)) goto failed;
if (type == SILICON_TUNER)
{
datax |= 0x0C;
tmp[0] = 0x00;tmp[1] = 0x00;tmp[2] = 0x00;tmp[3] = 0x00;
}
else if (type == CAN_TUNER)
{
datax &= 0xF3;
tmp[0] = 0xCB;tmp[1] = 0x6B;tmp[2] = 0x28;tmp[3] = 0x2F;
}
if (LGS_WriteRegister(0x07, datax ))goto failed;
if (LGS_WriteRegister(0x09, tmp[0]))goto failed;
if (LGS_WriteRegister(0x0A, tmp[1]))goto failed;
if (LGS_WriteRegister(0x0B, tmp[2]))goto failed;
if (LGS_WriteRegister(0x0C, tmp[3]))goto failed;
return LGS_NO_ERROR;
failed:
return LGS_CHANGE_MODE_ERROR;
}
/* end of lgs8g42.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -