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

📄 mode_detect.c

📁 Realtek 公司的RTD2523A芯片原厂source code,没有被修改过的。
💻 C
📖 第 1 页 / 共 5 页
字号:
    if (350 > usIPV_ACT_LEN)    return MODE_NOSUPPORT;

    // We only support 8N active width
    usIPH_ACT_WID   = (2 < (usIPH_ACT_WID & 0x0007)) ? ((usIPH_ACT_WID + 8) & 0xfff8) : (usIPH_ACT_WID & 0xfff8);

    // We only support active width not larger than 1280
    if (1280 < usIPH_ACT_WID)   usIPH_ACT_WID   = 1280;

    // Calculate maximum usIPV_ACT_LEN limitation
    ((unsigned int *)Data)[0]   = (unsigned long)(usVsync - 1) * DISP_LEN / MIN_DV_TOTAL;

    if (((unsigned int *)Data)[0] < usIPV_ACT_LEN)      usIPV_ACT_LEN   = ((unsigned int *)Data)[0];

    // First, we try user-mode to fit the undefined mode for full-screen display.
    Wait_Time_Cnt   = MODE_USER720x400;
    do
    {
        if (Mode_Preset[Wait_Time_Cnt][2] < CAP_WIN[Wait_Time_Cnt][4])   // V scale-down
        {
            // Larger scale-down ratio may need a larger DH_TOTAL. 
            // So usIPV_ACT_LEN should not be too larger than pre-set capture window length
            if (Mode_Preset[Wait_Time_Cnt][2] > usIPV_ACT_LEN || (CAP_WIN[Wait_Time_Cnt][4] + 24) < usIPV_ACT_LEN)   continue;
        }
        else
        {
            if (Mode_Preset[Wait_Time_Cnt][2] < usIPV_ACT_LEN)   continue;
        }

        ((unsigned int *)Data)[0]   = (unsigned long)(RTD_XTAL / 10) * Mode_Preset[Wait_Time_Cnt][0] 
                                    * Mode_Preset[Wait_Time_Cnt][2] / ((unsigned long)100 * usIPV_ACT_LEN * usHsync);

        // Return if we find out a suitable user-mode.
        if (MAX_DCLK > ((unsigned int *)Data)[0])   return Wait_Time_Cnt;
    }
    while (MODE_USER1600x1200 >= ++Wait_Time_Cnt);

    Wait_Time_Cnt   = MODE_NOSUPPORT;

#if (PARTIAL_DISP)
    // If failed, we try partial-screen display.
    Wait_Time_Cnt   = Partial_Display();
#endif

    return  Wait_Time_Cnt;

#else

    return  MODE_NOSIGNAL;

#endif
}

/////////////////////////////////////////////////////////
//------------------ Detect VIDEO Mode ----------------//
/////////////////////////////////////////////////////////
#if (VIDEO_CHIP != VDC_NONE)

void Detect_Video_Mode(void)
{
    I2CRead(ADDR_VIDEO, 0x1f, 0x01);

    //------ Check Horizontal Lock & Frequency ------
    if (0 == (Data[0] & 0x40))
    {   
        if ((Data[0] & 0x20) && (0x01 != (ucAV_Mode & 0x0f)))
        {
            // Field rate changed to 60Hz
            ucAV_Mode       = 0x01;
            ucMode_Found    = MODE_NOSUPPORT;
            ucMode_Times    = 0;

            Data[0] = 4;
            Data[1] = ADDR_VIDEO;
            Data[2] = 0x0e;
            Data[3] = 0x08;     // Start color search from NTSC-M
            I2CWrite(Data);
        }
        else if ((0 == (Data[0] & 0x20)) && (0x02 != (ucAV_Mode & 0x02)))
        {
            // Field rate changed to 50Hz
            ucAV_Mode       = 0x02; 
            ucMode_Found    = MODE_NOSUPPORT;
            ucMode_Times    = 0;
            
            Data[0] = 4;
            Data[1] = ADDR_VIDEO;
            Data[2] = 0x0e;
            Data[3] = 0x08;     // Start color search from PAL-BDGHI
            I2CWrite(Data);
        }
        else
        {    
            //------ Check Lock Color ------
            if (0x01 == (Data[0] & 0x01))
            {   
                // Lock Color
                if (ucAV_Mode & 0x01)
                {
                    if (MODE_VIDEO60HZ == ucMode_Found)
                    {
                        ucMode_Times ++;
                    }
                    else
                    {
                        ucMode_Found    = MODE_VIDEO60HZ;
                        ucMode_Times    = 0;
                    }
                }
                else
                {
                    if (MODE_VIDEO50HZ == ucMode_Found)
                    {
                        ucMode_Times ++;
                    }
                    else
                    {
                        ucMode_Found    = MODE_VIDEO50HZ;
                        ucMode_Times    = 0;
                    }
                }
            }
            else
            {
                // Non Lock Color
                if (MODE_NOSUPPORT != ucMode_Found)
                {
                    ucMode_Found    = MODE_NOSUPPORT;
                    ucMode_Times    = 0;
                }
                
                Data[0] = 4;
                Data[1] = ADDR_VIDEO;
                Data[2] = 0x0e;
                
                if (0x03 == (ucAV_Mode & 0x03))
                {
                    // SECAM -> PAL-BDGHI(50Hz)
                    Data[3] = 0x08;
                    I2CWrite(Data);                    
                    ucAV_Mode   = 0x02;
                }
                else
                {
                    if (ucMode_Times & 0x01)    // Every 2-times , change color
                    {
                        switch (ucAV_Mode & 0x70)
                        {
                        case 0x00:  
                            Data[3] = 0x18; // NTSC-4.43(50Hz)  // PAL-4.43(60Hz)
                            I2CWrite(Data);
                            ucAV_Mode   = (ucAV_Mode & 0x0f) | 0x10;
                            break;
                        case 0x10:
                            Data[3] = 0x28; // PAL-N(50Hz)      // NTSC-4.43(60Hz)
                            I2CWrite(Data);
                            ucAV_Mode   = (ucAV_Mode & 0x0f) | 0x20;
                            break;
                        case 0x20:
                            Data[3] = 0x38; // NTSC-N(50Hz)     // PAL-M(60Hz)
                            I2CWrite(Data);
                            ucAV_Mode   = (ucAV_Mode & 0x0f) | 0x30;
                            break;
                        case 0x30:
                            if (ucAV_Mode & 0x02)
                            {
                                // NTSC-N -> SECAM (50Hz)
                                Data[3]     = 0x5c;
                                I2CWrite(Data);
                                ucAV_Mode   = 0x03;
                            }
                            else
                            {	
                                // PAL-M -> NTSC_M (60Hz)
                                Data[3]     = 0x08;
                                I2CWrite(Data);
                                ucAV_Mode   = ucAV_Mode & 0x0f | 0x00;
                            }    
                        default:
                            break;
                        }//end of "switch (ucAV_Mode & 0x70)"
                    }//end of "if (ucMode_Times & 0x01)"	
                }//end of "if (0x03 == (ucAV_Mode & 0x03)) else"
            }//end of "Non Lock color
        }//end of "Check Lock Color"
    }//end of "if (0==(Data[0] & 0x40))"
    else
    {   
        // Non Horizontal Lock
        ucAV_Mode   = 0;
        
        if (ucMode_Found != MODE_NOSUPPORT)
        {
            ucMode_Found    = MODE_NOSUPPORT;
            ucMode_Times    = 0;
        }
    }
    
    //-------------Check result--------------
    if (MODE_NOSUPPORT != ucMode_Found)
    {   
        if (VIDEO_SEARCH_TIMES < ucMode_Times)
        {
            bLIGHT_PWR  = LIGHT_OFF;            // Turn off BackLight for reset display
            
            RTDSetByte(HOSTCTRL_02, 0x00);      // Wake RTD up
            OSD_Dispatcher(NOTIFY_RESET_OSD);   // Reset OSD
            
            if (SOURCE_YUV == (stGUD1.INPUT_SOURCE & 0x07))
                ucMode_Curr = (ucAV_Mode & 0x02) ? MODE_YUV50HZ : MODE_YUV60HZ;
            else
                ucMode_Curr = ucMode_Found;
                    
            ucMode_Times    = 0;
            bStable         = 0;            // bStable must be cleared when mode changed

            Display_Video_Set();            
        }
    }
    else
    {
        ucMode_Times ++;
        
        if (ucAV_Mode)
        {
            // Force to display although we cannot lock color
            if (VIDEO_SEARCH_TIMES < ucMode_Times 
                && (NO_COLOR_TIMES < ucMode_Times || SOURCE_YUV == (stGUD1.INPUT_SOURCE & 0x07)))
            {

                bLIGHT_PWR  = LIGHT_OFF;            // Turn off BackLight for reset display

                RTDSetByte(HOSTCTRL_02, 0x00);      // Wake RTD up
                OSD_Dispatcher(NOTIFY_CLEAR_OSD);   // Clear OSD
                
                ucAV_Mode       = (ucAV_Mode & 0x02) ? 0x82 : 0x81;

                if (SOURCE_YUV != (stGUD1.INPUT_SOURCE & 0x07))
                    ucMode_Curr = (ucAV_Mode & 0x02) ? MODE_VIDEO50HZ : MODE_VIDEO60HZ;
                else
                    ucMode_Curr = (ucAV_Mode & 0x02) ? MODE_YUV50HZ : MODE_YUV60HZ;

                ucMode_Times    = 0;
                bStable         = 0;    // bStable must be cleared when mode changed

                Display_Video_Set();
            }
        }
        else
        {
            if (NO_MODE_TIMES < ucMode_Times)
            {
                bStable         = 1;    // Set bStable to 1 when signal timing is stable.

                ucMode_Times    = NO_MODE_TIMES - 1;
            }
        }
    }
}


/////////////////////////////////////////////////////////
//------------------- Check VIDEO Mode ----------------//
/////////////////////////////////////////////////////////
void Check_Video_Mode(void)
{   
    I2CRead(ADDR_VIDEO, 0x1f, 0x01);
    
    // Check color
    if (0 == (ucAV_Mode & 0x80))
    {
        if (0 == (Data[0] & 0x01))      ucMode_Curr = MODE_NOSIGNAL;    // Color standard changed
    }
    else
    {
        if (0 == (Data[0] & 0x01))
        {
            ucMode_Times    = 0;
        }
        else
        {
            // When we cannot lock color, we set color standard to PAL-BDGHI if field rate is 50Hz, 
            // and to NTSC-M if it is 60Hz.
            // If we find color is locked, we should update ucAV_Mode to indicate we now get color.
            if (VIDEO_SEARCH_TIMES < ++ucMode_Times)    ucAV_Mode = ucAV_Mode & 0x0f;
        }
    }

    // Check field rate
    if (Data[0] & 0x40)
    {
        ucMode_Curr = MODE_NOSIGNAL;
    }
    else
    {
        if (ucAV_Mode & 0x02)       // 50Hz
        {   
            if (0x00 != (Data[0] & 0x20))   ucMode_Curr = MODE_NOSIGNAL;
        } 
        else                        // 60Hz
        {
            if (0x00 == (Data[0] & 0x20))   ucMode_Curr = MODE_NOSIGNAL;
        }
    }
   
    // Check result
    if (MODE_NOSIGNAL == ucMode_Curr)
        Reset_Mode();
    else
        bStable = 1;    // Set bStable to 1 when signal timing is stable.
}

#endif




void Calculate_IVS2DVS_Delay(unsigned char integer, unsigned char fraction, unsigned int DVStart)
{
    if ((usIPV_ACT_STA + integer) < (DVStart >> 6))
    {
        // You should never get into this code ....
        usIPV_ACT_STA   = ( DVStart >> 6) - integer;

        RTDSetByte(IPV_ACT_STA_0A, (unsigned char)usIPV_ACT_STA);
        RTDSetByte(IPV_ACT_STA_0A + 1, (unsigned char)(usIPV_ACT_STA >> 8));
    }

    ((unsigned int *)Data)[0] = ((usIPV_ACT_STA + integer) << 6) + ((unsigned int)fraction << 6) / 10 - DVStart;

    ucDV_Delay = (unsigned char)(((unsigned int*)Data)[0] >> 6);

    Data[6] = ((unsigned int*)Data)[0] - (ucDV_Delay << 6);
    Data[6] = (unsigned long)usADC_Clock * Data[6] / 1024;

    RTDSetByte(FS_FT_DELAY_1E, Data[6]);      
    RTDSetByte(IV_DV_LINES_38, ucDV_Delay);
}


void Initial_Mode(void)
{
    unsigned char   ucOption, ucDelay;
    unsigned int    usDispLen;


    // bit 7 of ucMode_Curr : partial-V display.
    // bit 6 of ucMode_Curr : select 720x350 or 720x400 for VGA-50Hz and VGA-60Hz
    ucOption    = ucMode_Curr & 0xc0;
    ucMode_Curr = ucMode_Curr & 0x3f;
    
	RTDSetBit(SD_CTRL_70, 0xf9, VGA_ICLK_DELAY);
    
    if (SOURCE_DVI == (stGUD1.INPUT_SOURCE & 0x07))
    {
        RTDSetBit(SD_CTRL_70, 0xf9, TMDS_ICLK_DELAY);

        usIPH_ACT_STA   = CAP_WIN[ucMode_Curr][1];

        ucDelay = CAPTURE_HDELAY;
    }
    else
    {
        if (ucOption & 0x40)
        {
            // Only VGA-50Hz and VGA-60Hz mode will set bit 6 of ucMode_Curr to indicate 720-pixel capture width
            // In these case, we should use 720 horizontal settings instead of 640 horizontal settings in table.
            usADC_Clock     = CAP_WIN[MODE_0720x0400x70HZ][0];
            usIPH_ACT_STA   = CAP_WIN[MODE_0720x0400x70HZ][1];
            usIPH_ACT_WID   = CAP_WIN[MODE_0720x0400x70HZ][2];
        }
        else
        {
            usADC_Clock     = CAP_WIN[ucMode_Curr][0];
            usIPH_ACT_STA   = CAP_WIN[ucMode_Curr][1];
            usIPH_ACT_WID   = CAP_WIN[ucMode_Curr][2];

            // Calculate pixel clock rate (round to MHz)
            usDispLen   = (unsigned long)RTD_XTAL * usADC_Clock / ((unsigned long)usHsync * 500);
            usDispLen   = (usDispLen >> 1) + (usDispLen & 0x01);

            // Use ADC to do H scale-down if pixel clock rate is over spec.
            if (MAX_ADC_FREQ < usDispLen && MODE_USER1152x864 <= ucMode_Curr && MODE_USER1600x1200 >= ucMode_Curr)
            {
                usADC_Clock     = ADC_SD_SET[ucMode_Curr - MODE_USER1152x864][0];
                usIPH_ACT_STA   = ADC_SD_SET[ucMode_Curr - MODE_USER1152x864][1];
                usIPH_ACT_WID   = ADC_SD_SET[ucMode_Curr - MODE_USER1152x864][2];
            }
        }

        // Calculate pixel clock rate (round to MHz)
        usDispLen   = (unsigned long)RTD_XTAL * usADC_Clock / ((unsigned long)usHsync * 500);
        usDispLen   = (usDispLen >> 1) + (usDispLen & 0x01);

        ucDelay = (60 > usDispLen) ? CAPTURE_HDELAY + 2 : (100 > usDispLen) ? CAPTURE_HDELAY + 1 : CAPTURE_HDELAY;

⌨️ 快捷键说明

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