📄 tidtv_tvp5160.c
字号:
/*******************************************************************************
* @ModuleName :: TiDTV_TVP5160.c
*
* @Copyright :: Copyright 2005- Texas Instruments, Inc.
*
* @Description:: Analog Video Decoder (TVP5160) Control Module
*
* @History ::
*---------------------------------------
* 03-13-2005 W.Shi Created
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "ucos_ii.h"
#include "TiDTV_DataType.h"
#include "TiDTV_SysCtrl.h"
#include "TiDTV_UserApi.h"
#include "TVP9K_I2C_Def.h"
#include "TVP9K_NV_Def.h"
#include "TiDTV_Timer.h"
#include "TiDTV_UserGpio.h"
#include "TiDTV_TVP5160.h"
TI_DTV_TVP5160_CTRL TiDTV_VideoDecCtrl;
//----------------------------------------------------------
static void TiDTV_Tvp5160VdpFifoCtrl(UCHAR Flag);
static int TiDTV_Tvp5160SetVBUSAddr(UINT32 Addr);
#ifdef TVP5160_uCODE
static int TiDTV_Tvp5160VBUSWriteByte(int Addr,UCHAR Val);
static void TiDTV_Tvp5160DownloaduCode(void);
#endif
//----------------------------------------------------------
// TVP5160 Channel selections: I2C_0x00
//
static const UCHAR TVP5160_ChanSel[] = {
#ifdef TVP5160EVM
0x01, // CVBS CH1: TUNER
0x00, // CVBS CH2: CVBS-RCA
0x42, // S-VIDEO
0x95, // Component: YPbPr
// 0xC2, // SCART: RGB, CVBS-RCA
#else // for DTV RefDesign
0x00, // Tuner: CVBS CH1
0x06, // Video-1: CVBS CH2: CVBS-RCA
0x44, // S-VIDEO
0x92, // Component: YPbPr
#endif /* TVP5160EVM */
};
//----------------------------------------------------------
static const UCHAR TVP5160_InitData[][2] = {
// TVP5160 Direct I2C Registers
{0x02, 0x01}, // 0x00 = Auto, 0x01 = Force NTSC
{0x03, 0x00}, // Power Down: Normal Operation
// {0x04, 0x03}, // Auto Switch Mask: NTSC and PAL only
{0x30, 0x0F}, // Auto Switch including Progressive mode
{0x06, 0x40}, // Pedestal off
{0x07, 0x04}, // Luma peaking
{0x0e, 0x05}, // Chroma filter
{0x72, 0x04}, // Microvision setting
// Enhace analog video performace
{0x2e, 0x24}, // Enhance color transients
{0x74, 0x04}, // Kill Composite peak
{0x84, 0x40}, // Adjust 3DYC Luma Coring
{0x85, 0x20}, // Adjust 3DYC Chroma Coring
{0x59, 0x07}, // Enabled SDRAM and set SDRAM size
{0x0D, 0x80}, // 3DYC enabled, 3DNR disabled
{0x33, 0x40}, // Extended Coding Range, ITU-656 (10-bit)
{0x35, 0xEA}, // GLCO and FID Enabled
{0x36, 0xAF}, // HS and VS Enabled
{0x75, 0x16},
{0x7F, 0x01}, // enable Analog Video Output
{0xC0, 0x01}, // Allow host port access to the VBI FIFO data
};
//----------------------------------------------------------
// NTSC 601: Gemstar 1x/2x: 0x902040 - 0x90204F
//
static const UCHAR TVP5160_VdpCRamNtsc601Gemstar[] = { // 4 -- Gemstar 1x/2x
0xE1, // Data port
#if 1 // New C-RAM
0x80, // Run-in MSB SS 0x55 0x99
0x99, // Run-in LSB SS 0x99
0xFF, // Run-in Mask MSB
0xFF, // Run0in Mask LSB
0x05, // Frame Sync
0x51, // Mode/DTO
0x6E, // Quantizer/PCLK
0x04, // Slice/Words
0x63, // Video/Blank
0x19, // Increment LSB
0x13, // Increment MSB
0x80, // Filter Mux
0x00, // Slice Level
0x00, // TCLK/QCLK
0x60, // Destination
0x00,
#else // old C-RAM
0x99, // Run-in MSB SS 0x55
0x99, // Run-in LSB SS 0x99
0xFF, // Run-in Mask MSB
0xFF, // Run0in Mask LSB
0x05, // Frame Sync
0x51, // Mode/DTO
0x6E, // Quantizer/PCLK
0x05, // Slice/Words
0x63, // Video/Blank
0x19, // Increment LSB
0x13, // Increment MSB
0x80, // Filter Mux
0x00, // Slice Level
0x00, // TCLK/QCLK
0x60, // Destination
0x00,
#endif
};
#if 0
static void TiDTV_Tvp5160InitVDP(void);
//----------------------------------------------------------
static const UCHAR TVP5160_InitVdpNTSC[] = {
0xE1,
#if 0
0x15, 0xC1, // Line-21: CC field-1 (was 0xC4)
0x15, 0xC9, // Line-21: CC field-2 (was 0xCC)
#else
0x15, 0x45, // Line-21: User Define-1(Gemstar) field-1
0x15, 0x4D, // Line-21: User Define-1(Gemstar) field-2
#endif
// 0x0E, 0x03, // Line-14: VITC
// 0x14, 0x02, // Line-20: WSS
// 0x10, 0x04, // Line-16: Gemstar
};
#endif
//----------------------------------------------------------
static const UCHAR TVP5160_InitCC1x_resetNTSC[5] = {
0xE1,
0x15, 0xFF, // Line-21: CC field-1
0x15, 0xFF, // Line-21: CC field-2
};
//----------------------------------------------------------
static const UCHAR TVP5160_InitCC1xNTSC[5] = {
0xE1,
0x15, 0x41, // Line-21: CC field-1
0x15, 0x49, // Line-21: CC field-2
};
//----------------------------------------------------------
static const UCHAR TVP5160_InitCC2xNTSC[5] = {
0xE1,
0x15, 0x44, // Line-21: CC field-1
0x15, 0x4C, // Line-21: CC field-2
};
//---------------------------------------------------
// defined in "TiDTV_TVP516uCode.c"
//
extern const UCHAR TiDTV_TVP5160_uCode[3904 + 1];
//----------------------------------------------------------------
static const UCHAR TVP5160_NRSensitivityTable[4][4] = {
//0x5a 0x5b 0x5c 0x5d
{0x00, 0x00, 0x00, 0x00}, // Off
{0x37, 0x37, 0x53, 0x53}, // low
{0x80, 0x80, 0x80, 0x80}, // middle
{0xc0, 0xc0, 0x20, 0x20} // strong
};
extern int CC_slicing_mode;
extern int CC1x_error_rate, CC2x_error_rate;
extern int CC1x_max_error, CC2x_max_error;
extern int CC1x_errorcnt, CC2x_errorcnt;
extern int CC1x_capture, CC2x_capture;
/*******************************************************************************
* @RoutineName:: TiDTV_InitVideoDec
*
* @Description:: TVP5160 initializations
*
* @Input :: none
*
* @Output :: none
*
* @Return :: none
*******************************************************************************/
void TiDTV_InitVideoDec(void)
{
UCHAR (*pWorkI2cData)[2] = (UCHAR (*)[2]) TVP5160_InitData;
int i;
TiDTV_VideoDecCtrl.pMutexSem = OSSemCreate(1); // Mutual Exclusive Semaphore
TiDTV_VideoDecCtrl.LockStatus = 0; // Lock status
TiDTV_VideoDecCtrl.VideoFmt = // Input video format
TiDTV_VideoDecCtrl.NvVideoFmt = 0xff; // NV Video Format (according to VideoFmt)
TiDTV_VideoDecHwReset(); // Hardware Reset
#ifdef TVP5160_uCODE
TiDTV_Tvp5160DownloaduCode();
#endif
for (i = 0; i < sizeof(TVP5160_InitData) / sizeof(TVP5160_InitData[0]); i++, pWorkI2cData++)
TiDTV_Tvp5160I2cWrite(2, &((*pWorkI2cData)[0]), OS_TICKS_PER_SEC / 5);
// TVP5160 VDP (VBI Data Processor) Initializations
TiDTV_Tvp5160InitVDP();
}
/*******************************************************************************
* @RoutineName:: TiDTV_VideoDecHwReset
*
* @Description:: Video Decoder H/W Reset
*
* @Input :: none
*
* @Output :: none
*
* @Return :: none
*******************************************************************************/
void TiDTV_VideoDecHwReset(void)
{
// TVP5160 RESET = High
tvpGpioWrite(TI_DTV_GPIO_TVP5160_RESET, 1);
TiDTV_UserTimeDly(TI_DTV_SOFT_TIMER1, 30);
// TVP5160 RESET = Low
tvpGpioWrite(TI_DTV_GPIO_TVP5160_RESET, 0);
TiDTV_UserTimeDly(TI_DTV_SOFT_TIMER1, 20);
// TVP5160 RESET = High
tvpGpioWrite(TI_DTV_GPIO_TVP5160_RESET, 1);
TiDTV_UserTimeDly(TI_DTV_SOFT_TIMER1, 50);
}
/*******************************************************************************
* @RoutineName:: TiDTV_VideoDecInputSel
*
* @Description:: TVP5160 Inut Selection
*
* @Input :: none
*
* @Output :: none
*
* @Return :: none
*******************************************************************************/
void TiDTV_VideoDecInputSel(void)
{
INT8U uCosRetStatus;
UCHAR AddrData[2];
// Take the Mutex Semaphore
OSSemPend(TiDTV_VideoDecCtrl.pMutexSem, 0, &uCosRetStatus);
TiDTV_VideoDecCtrl.LockStatus = 0; // Lock status
TiDTV_VideoDecCtrl.VideoFmt = // Input video format
TiDTV_VideoDecCtrl.NvVideoFmt = 0xff; // NV Video Format (according to VideoFmt)
AddrData[0] = TVP5160_REG_INPUT_SEL;
switch (TiDTV_SysCtrl.AvInput) {
case TI_DTV_AV_INPUT_A_VIDEO1: // Analog TV Channels (Tuner)
AddrData[1] = TVP5160_ChanSel[0];
break;
case TI_DTV_AV_INPUT_A_VIDEO2: // Video (CVBS-1)
AddrData[1] = TVP5160_ChanSel[1];
break;
case TI_DTV_AV_INPUT_A_SVIDEO: // S-Video
AddrData[1] = TVP5160_ChanSel[2];
break;
case TI_DTV_AV_INPUT_YPBPR1: // Component-1
AddrData[1] = TVP5160_ChanSel[3];
break;
default:
break;
}
TiDTV_Tvp5160I2cWrite(2, AddrData, OS_TICKS_PER_SEC / 5);
// wait for a while
TiDTV_UserTimeDly(TI_DTV_SOFT_TIMER1, 50);
// select video standard
if (TiDTV_SysCtrl.AvInput == TI_DTV_AV_INPUT_A_VIDEO1) {
AddrData[0] = TVP5160_REG_SELECT_MODE;
AddrData[1] = 0x01; // Force NTSC
TiDTV_Tvp5160I2cWrite(2, AddrData, OS_TICKS_PER_SEC / 5);
AddrData[0] = 0x04;
AddrData[1] = 0x01; // Force NTSC
TiDTV_Tvp5160I2cWrite(2, AddrData, OS_TICKS_PER_SEC / 5);
AddrData[0] = 0x8d;
AddrData[1] = 0x03; // Enable IF compensation
TiDTV_Tvp5160I2cWrite(2, AddrData, OS_TICKS_PER_SEC / 5);
} else {
AddrData[0] = TVP5160_REG_SELECT_MODE;
AddrData[1] = 0x00; // Auto
TiDTV_Tvp5160I2cWrite(2, AddrData, OS_TICKS_PER_SEC / 5);
AddrData[0] = 0x04;
AddrData[1] = 0x03; // Force NTSC
TiDTV_Tvp5160I2cWrite(2, AddrData, OS_TICKS_PER_SEC / 5);
AddrData[0] = 0x8d;
AddrData[1] = 0x00; // Disable IF compensation
TiDTV_Tvp5160I2cWrite(2, AddrData, OS_TICKS_PER_SEC / 5);
}
// Set video decoder quality
if(TiDTV_SysCtrl.AvInput == TI_DTV_AV_INPUT_A_SVIDEO)
{
AddrData[0] = 0x07;
AddrData[1] = 0x84; // Luma comb/trap filter bypass
TiDTV_Tvp5160I2cWrite(2, AddrData, OS_TICKS_PER_SEC / 5);
}
else
{
AddrData[0] = 0x07;
AddrData[1] = 0x04; // Luma comb filter enable
TiDTV_Tvp5160I2cWrite(2, AddrData, OS_TICKS_PER_SEC / 5);
}
AddrData[0] = 0x2e;
AddrData[1] = 0x2a; // CTI
TiDTV_Tvp5160I2cWrite(2, AddrData, OS_TICKS_PER_SEC / 5);
// reset to 10-bit 656 output format: I2C_0x33 = 0x40
AddrData[0] = TVP5160_REG_OUTPUT_FMT_1;
AddrData[1] = 0x40;
TiDTV_Tvp5160I2cWrite(2, AddrData, OS_TICKS_PER_SEC / 5);
// Allow host port access to the VBI FIFO data
TiDTV_Tvp5160VdpFifoCtrl(1);
// Release the Mutex Semaphore
OSSemPost(TiDTV_VideoDecCtrl.pMutexSem);
}
/*******************************************************************************
* @RoutineName:: TiDTV_VideoDecCheckLockStatus
*
* @Description:: TVP5160 Check H/V lock status
*
* @Input :: none
*
* @Output :: none
*
* @Return ::
* int : H/V Lock status: 0 = not locked, non-0 = Locked
*******************************************************************************/
int TiDTV_VideoDecCheckLockStatus(void)
{
INT8U uCosRetStatus;
UCHAR HVLockStatus;
// Take the Mutex Semaphore
OSSemPend(TiDTV_VideoDecCtrl.pMutexSem, 0, &uCosRetStatus);
TiDTV_Tvp5160I2cRead(TVP5160_REG_STATUS_1, 1, &HVLockStatus, OS_TICKS_PER_SEC / 5);
if ((HVLockStatus & 0x06) == 0x06) {
if (TiDTV_VideoDecCtrl.LockStatus == 0) {
// Output the VBI FIFO data to 656 data port
TiDTV_Tvp5160VdpFifoCtrl(0);
}
TiDTV_VideoDecCtrl.LockStatus = 1;
} else {
TiDTV_VideoDecCtrl.LockStatus = 0;
// Allow host port access to the VBI FIFO data
TiDTV_Tvp5160VdpFifoCtrl(1);
}
// Release the Mutex Semaphore
OSSemPost(TiDTV_VideoDecCtrl.pMutexSem);
return TiDTV_VideoDecCtrl.LockStatus;
}
/*******************************************************************************
* @RoutineName:: TiDTV_VideoCCErorCheck
*
* @Description:: TVP5160 Check CC parity if error is higher than threshold, switch
* slicing method
*
* @Input :: none
*
* @Output :: none
*
* @Return ::
*
*******************************************************************************/
int CC_cnt=0;
UCHAR NoiseMSB;
UCHAR CC_Threshold;
void TiDTV_VideoCCErorCheck(void)
{
INT8U uCosRetStatus;
// Take the Mutex Semaphore
OSSemPend(TiDTV_VideoDecCtrl.pMutexSem, 0, &uCosRetStatus);
if ((CC1x_capture == 1) & (CC2x_capture==1)) { // no slicing
// try 2x slicing
CC_cnt++;
if (CC_cnt > 10) {
TiDTV_Tvp5160SetVBUSAddr(0x800600);
TiDTV_Tvp5160I2cWrite(sizeof(TVP5160_InitCC1xNTSC), (UCHAR *) &(TVP5160_InitCC2xNTSC[0]), 3 * OS_TICKS_PER_SEC);
CC_cnt=0;
}
}
else if ((CC1x_capture == 1) & (CC2x_capture==0)) { // 2x slicing done
CC_cnt++;
if (CC2x_error_rate > CC_Threshold) {
TiDTV_Tvp5160SetVBUSAddr(0x800600);
TiDTV_Tvp5160I2cWrite(sizeof(TVP5160_InitCC1xNTSC), (UCHAR *) &(TVP5160_InitCC1xNTSC[0]), 3 * OS_TICKS_PER_SEC);
// CC2x_capture =1;
CC_cnt=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -