⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 saa7113.c

📁 用于TM1300/PNX1300系列DSP(主要用于视频处理)的设备库的源码
💻 C
📖 第 1 页 / 共 4 页
字号:
/*
 * Copyright (c) 1995 - 1999 by TriMedia Technologies. 
 *
 * +------------------------------------------------------------------+
 * | This software is furnished under a license and may only be used  |
 * | and copied in accordance with the terms and conditions of  such  |
 * | a license and with the inclusion of this copyright notice. This  |
 * | software or any other copies of this software may not be provided|
 * | or otherwise made available to any other person.  The ownership  |
 * | and title of this software is not transferred.                   |
 * |                                                                  |
 * | The information in this software is subject  to change without   |
 * | any  prior notice and should not be construed as a commitment by |
 * | TriMedia Technologies.                                           |
 * |                                                                  |
 * | this code and information is provided "as is" without any        |
 * | warranty of any kind, either expressed or implied, including but |
 * | not limited to the implied warranties of merchantability and/or  |
 * | fitness for any particular purpose.                              |
 * +------------------------------------------------------------------+
 *
 *
 *  Module name              : saa7113.c    1.9
 *
 *  Last update              : 11:58:38 - 99/06/23
 *
 *  Description              :
 *
 *  Initialize and control routines for saa7113
 *
 *
 *  Revision                 :
 *        Built for the TCS 2.0 release
 *
 *
 */


#include <stdio.h>
#include <tm1/tmIIC.h>
#include <tm1/tmAvFormats.h>
#include <tm1/tmVImmio.h>
#include <tm1/tmVIboard.h>
#include <tmlib/dprintf.h>
#include <saa7113.h>


/* OLDSB = 0, so the masks are slightly different and not compatible with */
/* old versions */

#define A_HLCK          0x40 /* OLDSB = 1. Horizontal freq locked */
#define A_HLVLN         0x40 /* OLDSB = 0  Horizontal/Vertical freq locked */
#define A_FIDT          0x20 /* LOW = 50Hz, HIGH = 60 Hz */
#define A_RDCAP         0x01 /* OLDSB = 0. Ready for capture */
#define A_CODE          0x01 /* OLDSB = 1. Colour Signal in accordance w/ selected standard */

/******************************************************************************/
/* Default structures for video decoder configuration (supports 1 default instance) */
#define NUM_OF_DEF_ADAPTER 2

static boardVIAdapterEntry_t defAdapters[NUM_OF_DEF_ADAPTER] = {
  { vaaCVBS,   0, 1 },
  { vaaSvideo, 0, 9 }
};

static boardVIDec_t defaultVD [1] = 
{
    { 0x4A,                /* I2C slave address of first decoder  */
      NUM_OF_DEF_ADAPTER,  /* Number of adapters                  */
      defAdapters }        /* mapping of adapters to inputs       */
};

/******************************************************************************/
/* Definitions and structures used by the video DecHAL implementation         */

typedef enum 
{
    ChipVersion=            0x00,   
    IncrementDelay=         0x01,   
    AnalogInputControl1=    0x02,   
    AnalogInputControl2=    0x03,   
    AnalogInputControl3=    0x04,   
    AnalogInputControl4=    0x05,   
    HorzSyncStart=          0x06,   
    HorzSyncStop=           0x07,   
    SyncControl=            0x08,   
    LuminanceControl=       0x09,   
    LuminanceBrightness=    0x0A,   
    LuminanceContrast=      0x0B,   
    ChromaSaturation=       0x0C,   
    ChromaHueControl=       0x0D,   
    ChromaControl=          0x0E,   
    ChromaGainControl=      0x0F,   

    FormatDelayControl=     0x10,   
    OutputControl1=         0x11,   
    OutputControl2=         0x12,   
    OutputControl3=         0x13,   

    VGateStart=             0x15,   
    VGateStop=              0x16,   
    VGateMSB=               0x17,   

    StatusByte=             0x1F,   

    AC1=                    0x40,   
    LCR2=                   0x41,   
    LCR3=                   0x42,   
    LCR4=                   0x43,   
    LCR5=                   0x44,   
    LCR6=                   0x45,   
    LCR7=                   0x46,   
    LCR8=                   0x47,   
    LCR9=                   0x48,   
    LCR10=                  0x49,   
    LCR11=                  0x4A,   
    LCR12=                  0x4B,   
    LCR13=                  0x4C,   
    LCR14=                  0x4D,   
    LCR15=                  0x4E,   
    LCR16=                  0x4F,   
    LCR17=                  0x50,   
    LCR18=                  0x51,   
    LCR19=                  0x52,   
    LCR20=                  0x53,   
    LCR21=                  0x54,   
    LCR22=                  0x55,   
    LCR23=                  0x56,   
    LCR24=                  0x57,   
    FC=                     0x58,   
    HOFF=                   0x59,   
    VOFF=                   0x5A,   
    HVOFF=                  0x5B,   

    DID=                    0x5D,   
    SDID=                   0x5E,   

    DR=                     0x60,   
    LN1=                    0x61,   
    LN2=                    0x62    
} decoderRegs_t;

typedef struct 
{
    Int     nSubAddr;
    Int     nValue;
} viRegValue;

static viRegValue saa7113_defRegs[] = {
    {IncrementDelay,        0x08},
    {AnalogInputControl1,   0xC1},
    {AnalogInputControl2,   0x13},
    {AnalogInputControl3,   0x00},
    {AnalogInputControl4,   0x00},
    {HorzSyncStart,         0xAB},
    {HorzSyncStop,          0xC0},
    {SyncControl,           0x98},
    {LuminanceControl,      0x01},
    {LuminanceBrightness,   0x80},
    {LuminanceContrast,     0x47},
    {ChromaSaturation,      0x40},
    {ChromaHueControl,      0x00},
    {ChromaControl,         0x01},
    {ChromaGainControl,     0x3f},

    {FormatDelayControl,    0x00},
    {OutputControl1,        0x0C},
    {OutputControl2,        0x11},
    {OutputControl3,        0x00},

    {VGateStart,            0x00},
    {VGateStop,             0x00},
    {VGateMSB,              0x00},

    {StatusByte,            0x00},

    {AC1,                   0x02},
    {LCR2,                  0xFF},
    {LCR3,                  0xFF},
    {LCR4,                  0xFF},
    {LCR5,                  0xFF},
    {LCR6,                  0xFF},
    {LCR7,                  0xFF},
    {LCR8,                  0xFF},
    {LCR9,                  0xFF},
    {LCR10,                 0xFF},
    {LCR11,                 0xFF},
    {LCR12,                 0xFF},
    {LCR13,                 0xFF},
    {LCR14,                 0xFF},
    {LCR15,                 0xFF},
    {LCR16,                 0xFF},
    {LCR17,                 0xFF},
    {LCR18,                 0xFF},
    {LCR19,                 0xFF},
    {LCR20,                 0xFF},
    {LCR21,                 0xFF},
    {LCR22,                 0xFF},
    {LCR23,                 0xFF},
    {LCR24,                 0xFF},
    {FC,                    0x00},
    {HOFF,                  0x54},
    {VOFF,                  0x0A},
    {HVOFF,                 0x03},

    {DID,                   0x00},
    {SDID,                  0x13},
    {DR,                    0x00},
    {LN1,                   0x0c},
    {LN2,                   0x2F},
    {-1,-1}                             /* End of array */
};


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

/* Static variables: */
static UInt m_nNumVBILines;
static UInt m_nStartVBILine;
static UInt lastI2cError = 0;

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

#define MAX_TT 14
                                                /* Teletext translation table */
                                                /* to convert bit masks into */
                                                /* teletext program nibbles */
                                                /* quickly. */
typedef struct 
{
    Int               nBitMask;
    Int               nNibble;
} viDataServiceMapping_t;

static viDataServiceMapping_t Convert7113TT[MAX_TT] =
{
    {vdsEuroTeleText,       0x0},
    {vdsEuroClosedCaption,  0x1},
    {vdsVPS,                0x2},
    {vdsWSS,                0x3},
    {vdsUSTeleText,         0x4},
    {vdsUSClosedCaption,    0x5},
    {vdsUpsampledRaw,       0x7},
    {vdsTeleText,           0x8},
    {vdsVITC_EBU,           0x9},
    {vdsVITC_SMPTE,         0xA},
    {vdsUSNABTS,            0xC},
    {vdsMoji,               0xD},
    {vdsJapFormatSwitch,    0xE},
    {vdsNone,               0xF},
};

/******************************************************************************/
static tmLibdevErr_t  
mapAdapterInput (pboardVIDec_t pVD, pboardVIParam_t param, UInt *mode)
{
    UInt i; 

    /* If this is vaaNone then use the default adapter */
    if (param->adapterType == vaaNone)
    {
        *mode = pVD->adapterTable[0].decInput;
        return TMLIBDEV_OK;
    }

    for (i = 0; i < pVD->numAdapters; i++)
    {
        /* Find the right instance of the adapter */
        if ((pVD->adapterTable[i].instNum == param->adapterInstance) &&
            (pVD->adapterTable[i].adapterType == param->adapterType))
        {
            *mode = pVD->adapterTable[i].decInput;
            return TMLIBDEV_OK;
        }
    }

    /* No adapter instance found. If it's unqual to zero */
    /*  -> try with instance zero... */
    if (param->adapterInstance != 0)
    {
        param->adapterInstance = 0;
        return mapAdapterInput (pVD, param, mode);
    }

    /* Right adapter was not found -> return error */
    return BOARD_ERR_UNSUPPORTED_ADAPTER;
}

/******************************************************************************/
tmLibdevErr_t 
saa7113InitM (pboardVIDec_t pVD, pboardVIParam_t param)
{
    UInt i, rval = 0;
    UInt inputMode;   

    viSetSELFCLOCKM(param->mmioBase, 0);

    /* Copy video standard to internal variable */
    pVD->curVideoStandard = param->videoStandard;

    if (param->videoStandard == vasNone)
        return TMLIBDEV_OK;

    if ( !(param->videoStandard & (vasNTSC | vasPAL)) )
        return BOARD_ERR_UNSUPPORTED_STANDARD;

    /* from here on : supported videoStandard is specified */ 

    /* Setup default setting in SAA7113 */
    for (i = 0; saa7113_defRegs[i].nSubAddr >= 0; i++) 
    {
        rval |= iicWriteReg (pVD->slaveAddr, saa7113_defRegs[i].nSubAddr, saa7113_defRegs[i].nValue);
    }

    if (! ( (param->adapterType == vaaNone)
         || (param->adapterType & (vaaCVBS|vaaSvideo))) )
        return BOARD_ERR_UNSUPPORTED_ADAPTER;

    /* Make sure the adapter's instance is valid on this board and we get a valid mode */
    if (mapAdapterInput (pVD, param, &inputMode) != TMLIBDEV_OK)
        return BOARD_ERR_INVALID_ADAPTER_INSTANCE;

    /* Set input of video accordingly */
    rval |= saa7113SetAnalogInput (pVD, inputMode);

    return (tmLibdevErr_t) rval;
}

tmLibdevErr_t 
saa7113Init (pboardVIParam_t param)
{
    return saa7113InitM(&defaultVD[0], param);
}

/******************************************************************************/
tmLibdevErr_t
saa7113GetStandardM(pboardVIDec_t pVD, tmVideoAnalogStandard_t * std)
{
    unsigned int        stat, chroma;
 
    int                 notDetected = True;
    int                 err = 0;
    int                 i, j;
    unsigned char       colorStandard[5] = {0x01, 0x11, 0x21, 0x31, 0x51};

    microsleep(40000);          /* This stupid routine need to sleep a lot. */
    for (j = 0; ( (j<2) && notDetected ); j++) { 
        for ( *std = vasNone, i = 0; ( (i<5) && notDetected); i++) {
            err |= iicWriteReg (pVD->slaveAddr, ChromaControl, colorStandard[i]);
            microsleep(100000);

            err |= iicReadReg (pVD->slaveAddr, ChromaControl, &chroma);
            err |= iicReadReg (pVD->slaveAddr, StatusByte, &stat);

            if (err) {
                /* iic read write err */
                return ( (tmLibdevErr_t) err); 
            }
            if (stat & A_HLCK) {
                microsleep(500000); /* not in sync err, give it a long nap */
            }
            if (stat & A_CODE) { /* color videoStandard detected */
                if (stat & A_FIDT) {  /* 60 Hz */
                    switch (i) {
                        case 0: /* NTSC M */
                            *std = vasNTSC;
                            break;
                        case 1: /* PAL 4.43 */
                            *std = vasPAL;
                            break;
                        case 2: /* NTSC 4.43 */
                            *std = vasNTSC;
                            break;
                        case 3: /* NTSC N */
                            *std = vasNTSC;
                            break;
                        case 4: /* PAL 4.43 */
                            *std = vasPAL;
                            break;
                        default:
                            *std = vasNone; /* should not come here */
                            break;
                    } /* end of switch */
                } else { /* 50Hz */
                    switch (i) {
                        case 0: /* PAL BGHI */
                            *std = vasPAL;
                            break;
                        case 1: /* NTSC 4.43 */
                            *std = vasNTSC;
                            break;
                        case 2: /* PAL N */
                            *std = vasPAL;
                            break;
                        case 3: /* PAL M */
                            *std = vasPAL;
                            break;
                        case 4: /* SECAM */
                            *std = vasSECAM;
                            break;
                        default:
                            *std = vasNone; /* should not come here */
                            break;
                    } /* end of switch */
                }
                notDetected = False;
            }
        } /* end of i for loop */

⌨️ 快捷键说明

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