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

📄 api_ctrl.c

📁 其乐达(Cheertek)LCD驱动芯片(CT675)的C51源代码
💻 C
字号:
#include "..\INC\SYS_DECLARE.H"
#include "..\INC\SYS_GLOBAL.H"
#include "..\INC\SYS_INITIAL.H"

#include "INC\API_REG.H"
#include "INC\API_IIC.H"
#include "INC\API_EEPROM.H"
#include "INC\API_CTRL.H"
#include "INC\API_PANEL.H"

/****************************************************************************
 * Function     : API_CTRL_GetVideoStd()
 ***************************************************************************/
BOOL API_CTRL_GetVideoStd(void)
{
    BYTE bval;

    bval = API_VD_ReadByte(REG_VD_STATUS0);

    gSysInfo.fVideoLock     = (bval & 0x80) ? 1 : 0;
    gSysInfo.fChromaLock    = (bval & 0x40) ? 1 : 0;
    gSysInfo.fNoColorBurst  = (bval & 0x20) ? 1 : 0;
    gSysInfo.fNoSignal      = (bval & 0x10) ? 1 : 0;
    gSysInfo.fAutoStable    = (bval & 0x08) ? 1 : 0;

    if(~(gSysInfo.fNoSignal) & gSysInfo.fVideoLock & gSysInfo.fAutoStable)
    {
        gSysInfo.bVideoStandard = bval & 0x07;

        bval = gSysInfo.bVideoStandard;

        if(gSysInfo.fChromaLock)
        {
            if((bval == EN_VIDEO_NTSC_50) || (bval == EN_VIDEO_NTSC_60) || (bval == EN_VIDEO_NTSC_M))
                gSysInfo.bVideoSystem = 0;
            else if((bval == EN_VIDEO_PAL_BG) || (bval == EN_VIDEO_PAL_N) || (bval == EN_VIDEO_PAL_60) || (bval == EN_VIDEO_PAL_M))
                gSysInfo.bVideoSystem = 1;
            else
                return 0;       // It's a incorrect state.
        }
        else
        {
            if(bval == EN_VIDEO_SECAM)
                gSysInfo.bVideoSystem = 2;
            else
                return 0;       // It's a incorrect state.
        }

        return 1;
    }
    else
        return 0;
}

/****************************************************************************
 * Function     : API_CTRL_ModeDetection()
 ***************************************************************************/
void API_CTRL_ModeDetection(void)
{
    BOOL fModeChange = 0;
    BYTE bval = 0, bVideoLns = gSysInfo.bVideoLines;

    if(gSysInfo.fStopPolling)   return;
    if(gbModeDetectTimer != 0)  return;

    if(gbModeDetectCounter == 0)    // Reset stable flag
    {
        gfModeDetectStable = 0;
    }

    if((gSysInfo.bInputSource != EN_INPUT_CCIR656) && (API_CTRL_GetVideoStd() == 0))
    {
        // No video signal is detected.
        if(bVideoLns != EN_VIDEO_FREERUN)
        {
            gbModeDetectCounter = 0;
            gSysInfo.bVideoLines = EN_VIDEO_FREERUN;
            fModeChange = 1;
        }
    }
    else
    {
        API_CTRL_GetVideoStd();

        bval = API_SC_ReadByte(REG_SC_IV_TOTAL);

        if(bval < 0x20)
        {
            if(bVideoLns != EN_VIDEO_LN525)
            {
                gbModeDetectCounter = 0;
                gSysInfo.bVideoLines = EN_VIDEO_LN525;
                fModeChange = 1;
            }
        }
        else
        {
            if(bVideoLns != EN_VIDEO_LN625)
            {
                gbModeDetectCounter = 0;
                gSysInfo.bVideoLines = EN_VIDEO_LN625;
                fModeChange = 1;
            }
        }
    }

    if(gSysInfo.bVideoLines == bVideoLns)
    {
        if(!gfModeDetectStable)
        {
            if(gbModeDetectCounter > 2)
            {
                gfModeDetectStable = 1;
            }
            else
            {
                gbModeDetectCounter++;
            }
        }
    }

    if(fModeChange)
    {
        SYS_BacklightPower(0);
        API_CTRL_DCDCEnable(0);

        API_PANEL_ChangeMode(gSysInfo.bInputSource, (gSysInfo.bVideoLines == EN_VIDEO_LN625) ? 1 : 0);

        if(gSysInfo.bVideoStandard == EN_VIDEO_SECAM)
        {
            bval = API_VD_ReadByte(REG_VD_AUTO_MODE_1);
            API_VD_WriteByte(REG_VD_AUTO_MODE_1, bval | 0x20);
        }

    }
    else
    {
        if(gfModeDetectStable)
        {
            API_CTRL_ScalerFreerun((gSysInfo.bVideoLines == EN_VIDEO_FREERUN) ? 1 : 0);
            API_CTRL_DCDCEnable(1);
            SYS_Delay_mS(50);
            SYS_BacklightPower(1);
        }
    }

    gbModeDetectTimer = 100;    // 100 ms
}

/****************************************************************************
 * Function     : API_CTRL_SwichSource()
 ***************************************************************************/
void API_CTRL_SwitchSource(BYTE bSrc)
{
    if(bSrc == gSysInfo.bInputSource)   return;

    SYS_BacklightPower(0);
    API_CTRL_DCDCEnable(0);

    API_EEPROM_StoreParameters(EE_SC_COLOR);

    gSysInfo.bInputSource = bSrc;

    API_PANEL_Initial();

    API_EEPROM_StoreParameters(EE_SYS_INFO);

    gSysInfo.bVideoLines = EN_VIDEO_LN525;

    // Force flow control routine initial.
    gfFlowControlInitial = 1;
    gbModeDetectTimer = 0;
}

/****************************************************************************
 * Function     : API_CTRL_ScalerFreerun()
 ***************************************************************************/
void API_CTRL_ScalerFreerun(BOOL fEnable)
{
    BYTE fFreerun  = API_SC_ReadByte(REG_SC_MISC_CTRL0);
    BYTE fForcebkg = API_SC_ReadByte(REG_SC_OUT_CTRL2);

    if(fEnable)
    {
        API_IIC_WriteTable(FBG_BLUE);
        API_SC_WriteByte(REG_SC_MISC_CTRL0, (fFreerun  & 0xFD));
        API_SC_WriteByte(REG_SC_OUT_CTRL2,  (fForcebkg | 0x10));
    }
    else
    {
        API_SC_WriteByte(REG_SC_OUT_CTRL2,  (fForcebkg & 0xEF));
        API_SC_WriteByte(REG_SC_MISC_CTRL0, (fFreerun  | 0x02));
        API_IIC_WriteTable(FBG_BLACK);
    }
}

/****************************************************************************
 * Function     : API_CTRL_DCDCEnable()
 ***************************************************************************/
void API_CTRL_DCDCEnable(BOOL fEnable)
{
    BYTE bval  = API_SC_ReadByte(REG_SC_MISC_CTRL2);

    if(fEnable) API_SC_WriteByte(REG_SC_MISC_CTRL2, bval  & 0xBF);
    else        API_SC_WriteByte(REG_SC_MISC_CTRL2, bval  | 0x40);
}

/****************************************************************************
 * Function     : API_CTRL_AdcAutoBalance()
 ***************************************************************************/
void API_CTRL_AdcAutoBalance(void)
{
    BYTE bStatus, it;

    API_AD_WriteByte(REG_AD_AB_CTRL, 0x0F);

    for(it = 0; it < 0xF0; it++)
    {
        bStatus = API_AD_ReadByte(REG_AD_AB_RESULT);

        if((bStatus & 0x03) != 0)   break;
    }

    API_AD_WriteByte(REG_AD_AB_CTRL, 0x00);
}

/****************************************************************************
 * Function     : API_CTRL_SetBacklightBrightness()
 ***************************************************************************/
void API_CTRL_SetBacklightBrightness(BYTE bVal)
{
    WORD wval;

#if(BACKLIGHT_CTRL_INVERSE)
    bVal = ~bVal;
#endif

    wval = (WORD)bVal * 0xa0 / 0xFF;

    API_SC_WriteByte(REG_SC_CLOCK_REG2,  0x04);
    API_SC_WriteByte(REG_SC_PWM_CTRL,    0x01);
    API_SC_WriteWord(REG_SC_PWM1_PERIOD, 0x00a0);
    API_SC_WriteWord(REG_SC_PWM1_DUTY,   wval);
}

/****************************************************************************
 * Function     : API_CTRL_GetSarAdcValue()
 ***************************************************************************/
BYTE API_CTRL_GetSarAdcValue(BOOL fChannel)
{
    BYTE bval = 0, it;

    // Enable SAR ADC clock
    bval = API_SC_ReadByte(REG_SC_CLOCK_OFF);
    API_SC_WriteByte(REG_SC_CLOCK_OFF, bval & 0xF7);

    // Set SAR ADC clock frquency -> XOSC /32
    bval = API_SC_ReadByte(REG_SC_CLOCK_REG2);
    API_SC_WriteByte(REG_SC_CLOCK_REG2, bval & 0xCF);

    // Select channel, then trigge SAR ADC to get the value.
    bval = 0x04 + (BYTE)fChannel;
    API_SC_WriteByte(REG_SC_SAR_CTRL, bval);
    SYS_Delay_mS(3);
    API_SC_WriteByte(REG_SC_SAR_CTRL, bval & 0x01);

    for(it = 0; it < 10; it++)
    {
        if(API_SC_ReadByte(REG_SC_SAR_CTRL) & 0x08)
        {
            bval = API_SC_ReadByte(REG_SC_SAR_DATA);
            break;
        }
        else
        {
            SYS_Delay_mS(3);
            continue;
        }
    }

    return bval;
}

/****************************************************************************
 * Function     : API_CTRL_AutoBacklightControl()
 ***************************************************************************/
void API_CTRL_AutoBacklightControl(BOOL fCh)
{
    BYTE bval;

    bval = API_CTRL_GetSarAdcValue(fCh);

    if(bval > 0x80)
    {
        if(gScalerPara.bBkliteBright < 0xF0)
        {
            gScalerPara.bBkliteBright += 1;
            API_CTRL_SetBacklightBrightness(gScalerPara.bBkliteBright);
        }
    }
    else
    {
        if(gScalerPara.bBkliteBright > 0x30)
        {
            gScalerPara.bBkliteBright -= 1;
            API_CTRL_SetBacklightBrightness(gScalerPara.bBkliteBright);
        }
    }
}

/****************************************************************************
 * Function     : API_CTRL_AutoBacklightControl()
 ***************************************************************************/
void API_CTRL_GammaCorrection(BOOL fEnable)
{
#if(GAMMA_CORRECTION_EN)
    BYTE bval = API_SC_ReadByte(REG_SC_OUT_CTRL2);

    if(fEnable)
    {
        API_IIC_WriteTable(GammaTable);
        API_SC_WriteByte(REG_SC_OUT_CTRL2, bval | 0x80);
    }
    else
    {
        API_SC_WriteByte(REG_SC_OUT_CTRL2, bval & 0x7F);
    }
#else
    fEnable = fEnable;
#endif
}

/****************************************************************************
 * Function     :
 * Description  :
 * Parameters   :
 ***************************************************************************/
WORD code sRGB_Matrix[][3] =
{
    { 0x0100, 0x0000, 0x0167 },
    { 0x0100, 0x8058, 0x80B6 },
    { 0x0100, 0x01C5, 0x0000 }
};

/* SineLUT[degree] = Sine(degree) * 256 */
BYTE code SineLUT[] =
{
  /*  0     1     2     3     4     5     6     7     8     9  */
    0x00, 0x04, 0x08, 0x0D, 0x11, 0x16, 0x1A, 0x1F, 0x23, 0x28,     // 0
    0x2C, 0x30, 0x35, 0x39, 0x3D, 0x42, 0x46, 0x4A, 0x4F, 0x53,     // 1
    0x57, 0x5B, 0x5F, 0x64, 0x68, 0x6C, 0x70, 0x74, 0x78, 0x7C,     // 2
    0x7F, 0x83, 0x87, 0x8B, 0x8F, 0x92, 0x96, 0x9A, 0x9D, 0xA1,     // 3
    0xA4, 0xA7, 0xAB, 0xAE, 0xB1, 0xB5, 0xB8, 0xBB, 0xBE, 0xC1,     // 4
    0xC4, 0xC6, 0xC9, 0xCC, 0xCF, 0xD1, 0xD4, 0xD6, 0xD9, 0xDB,     // 5
    0xDD, 0xDF, 0xE2, 0xE4, 0xE6, 0xE8, 0xE9, 0xEB, 0xED, 0xEE,     // 6
    0xF0, 0xF2, 0xF3, 0xF4, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB,     // 7
    0xFC, 0xFC, 0xFD, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF      // 8
};

void _SetMatrixCoeff(void)
{
    BOOL fg_hue_sign, fg_mx_sign;
    BYTE it, offset;

    WORD sinct, cosct, ucoeff, vcoeff, wval;

    API_SC_WriteByte(REG_SC_SRGB_CTRL, 0x80);

    if(gScalerPara.bHue >= 0x80)
    {
        fg_hue_sign = 1;
        it = 0xFF - gScalerPara.bHue + 1;
    }
    else
    {
        fg_hue_sign = 0;
        it = gScalerPara.bHue;
    }

    sinct = (it == 90) ? 0x100 : (WORD)(SineLUT[it]);
    cosct = (it ==  0) ? 0x100 : (WORD)(SineLUT[90 - it]);

    sinct = (sinct * gScalerPara.bSaturation) >> 7;
    cosct = (cosct * gScalerPara.bSaturation) >> 7;

    for(it = 0; it < 3; it++)
    {
        offset = it * 6;

        // Coeff Y
        wval = (sRGB_Matrix[it][0] * gScalerPara.bContrast) >> 7;
        API_SC_WriteWord(REG_SC_SRGB_RCOEFF1 + offset, wval);

        // Coeff U
        ucoeff = (((sRGB_Matrix[it][1] & 0x07FF) >> 2) * cosct) >> 7;
        vcoeff = (((sRGB_Matrix[it][2] & 0x07FF) >> 2) * sinct) >> 7;

        fg_mx_sign = 0;

        if(fg_hue_sign)
        {
            if(ucoeff > vcoeff)
            {
                wval = ucoeff - vcoeff;
            }
            else
            {
                if(vcoeff > 0)  fg_mx_sign = 1;

                wval = vcoeff - ucoeff;
            }
        }
        else
        {
            wval = ucoeff + vcoeff;
        }

        wval = wval << 1;
        if(it == 1)     fg_mx_sign = ~fg_mx_sign;
        if(fg_mx_sign)  wval = 0x0800 - wval;

        API_SC_WriteWord(REG_SC_SRGB_RCOEFF2 + offset, wval);

        // Coeff V
        ucoeff = (((sRGB_Matrix[it][1] & 0x07FF) >> 2) * sinct) >> 7;
        vcoeff = (((sRGB_Matrix[it][2] & 0x07FF) >> 2) * cosct) >> 7;

        fg_mx_sign = 0;

        if(fg_hue_sign)
        {
            wval = vcoeff + ucoeff;
        }
        else
        {
            if(ucoeff < vcoeff)
            {
                wval = vcoeff - ucoeff;
            }
            else
            {
                if(ucoeff > 0)  fg_mx_sign = 1;

                wval = ucoeff - vcoeff;
            }
        }

        wval = wval << 1;
        if(it == 1)     fg_mx_sign = ~fg_mx_sign;
        if(fg_mx_sign)  wval = 0x0800 - wval;

        API_SC_WriteWord(REG_SC_SRGB_RCOEFF3 + offset, wval);
    }
}

void API_CTRL_ScalerColorEnhance(void)
{
    // adjust brightness
    API_SC_WriteByte(REG_SC_SRGB_YOFFSET, gScalerPara.bBrightness);

    // adjust contrast, hue, saturation
    _SetMatrixCoeff();

    // adjust sharpness, cti, nr HERE!
    API_IIC_WriteTable(LtiTable[gScalerPara.bSharpness]);
}

⌨️ 快捷键说明

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