Initial.c
上传用户:xmyjxjd
上传日期:2013-05-04
资源大小:1517k
文件大小:15k
开发平台:

C/C++

  1. #define __INITIAL__
  2. #include "HeaderLcd_main.h"
  3. #include "reg52.h"
  4. #include "HeaderAccess.h"
  5. #include "Headerconfig.h"
  6. #include "intrins.h"
  7. #include "HeaderLcd_func.h"
  8. #include "HeaderInitial.h"
  9. #include "HeaderOSD.h"
  10. #include "HeaderSrc_Ctrl.h"
  11. #include "HeaderFrame_Sync.h"
  12. //#include "Power_Ctrl.h"
  13. void MCU_Init(void)
  14. {
  15. #if(MCU_TYPE == MCU_WINBOND)
  16.     IE      = 0x00;                 // Disable all interrupts
  17.     TMOD    = 0x11;                 // Program Timer0 & Timer1 to Mode#1
  18.     TR0     = 0;                    // Stop Timer0
  19.     TF0     = 0;                    // Clear Timer0 Overflow Bit
  20.     TL0     = TIME0_COUNTER_LBYTE;  // Load Timer0 low-byte   (for 20ms)
  21.     TH0     = TIME0_COUNTER_HBYTE;  // Load Timer0 high-byte  (for 20ms)
  22.     TR1     = 0;                    // Stop Timer1
  23.     TF1     = 0;                    // Clear Timer1 Overflow Bit
  24.     TL1     = TIME1_COUNTER_LBYTE;  // Load Timer1 low-byte   (for 1ms)
  25.     TH1     = TIME1_COUNTER_HBYTE;  // Load Timer1 hifh-byte  (for 1ms)
  26.     IE      = 0x8a;                 // Enable interrupts of Timer0 and Timer1
  27. //anson 050519    
  28.     M512_PadMode0=0x00; //
  29.     M512_PadMode1=0xff; //
  30.     M512_PadMode2=0xb0; //
  31.     M512_PadMode3=0xcf; //
  32.     M512_PadMode4=0xf2; //
  33.     M512_PadMode5=0x00; //
  34. //    M512_PadMode6=0xbf; //
  35.     M512_PadMode7=0x3f; //
  36.     M512_Option1=0x01;
  37. #else
  38.     IE      = 0x00;                 // Disable all interrupts
  39.     TMOD    = 0x11;             // Program Timer0 & Timer1 to Mode#1
  40.     TR0     = 0;                    // Stop Timer0
  41.     TF0     = 0;                    // Clear Timer0 Overflow Bit
  42.     TL0     = TIME0_COUNTER_LBYTE;  // Load Timer0 low-byte   (for 20ms)
  43.     TH0     = TIME0_COUNTER_HBYTE;  // Load Timer0 high-byte  (for 20ms)
  44.     TR1     = 0;                    // Stop Timer1
  45.     TF1     = 0;                    // Clear Timer1 Overflow Bit
  46.     TL1     = TIME1_COUNTER_LBYTE;  // Load Timer1 low-byte   (for 1ms)
  47.     TH1     = TIME1_COUNTER_HBYTE;  // Load Timer1 hifh-byte  (for 1ms)
  48.     
  49.     IE      = 0x86;                 // Enable interrupts of Timer0 and Timer1
  50.     TR0            = 1;
  51. //anson 050519    
  52.     M512_PadMode0=0x00; //
  53.     M512_PadMode1=0xff; //
  54.     M512_PadMode2=0xb0; //
  55.     M512_PadMode3=0xcf; //
  56.     M512_PadMode4=0xf2; //
  57.     M512_PadMode5=0x00; //
  58. //    M512_PadMode6=0xbf; //
  59.     M512_PadMode7=0x3f; //
  60.     M512_Option1=0x01;
  61. #endif
  62. }
  63. void Firmware_Init(void)
  64. {
  65. #if(MCU_TYPE == MCU_WINBOND)
  66.     bLIGHT_PWR  = LIGHT_OFF;
  67.     bPANEL_PWR  = PANEL_OFF;
  68.     bPanel_Status = _OFF;
  69. #else
  70.     MCU_WriteBacklightPower(LIGHT_OFF);
  71.     MCU_WritePanelPower(PANEL_OFF);
  72.     bPanel_Status = _OFF;
  73. #endif
  74.      MCU_Init();
  75.      RTDSetBit(HOSTCTRL_02,0xfb,0x00);    // RTD turn on
  76. #if(MCU_TYPE == MCU_WINBOND)
  77. #if(RESET_TYPE == RESET_IN)
  78.     bRTD_RST    = 0;
  79.     Delay_Xms(10);
  80.     do
  81.     {
  82.         // Wait for reset complete
  83.         bRTD_RST    = 1;
  84.         Delay_Xms(2);
  85.     }
  86.     while (!bRTD_RST);
  87. #endif
  88. #else
  89. #if(RESET_TYPE == RESET_IN)
  90.     MCU_WriteRtdReset(LOW);
  91.     Delay_Xms(10);
  92.     do
  93.     {
  94.         // Wait for reset complete
  95.         MCU_WriteRtdReset(HIGH);
  96.         Delay_Xms(10);
  97.     }
  98.     while (MCU_ReadRtdReset()!=HIGH);
  99. #endif
  100. #endif
  101.     RTDRead(ID_REG_00,1,N_INC);
  102. ucID_Code = Data[0];
  103.     RTDCodeW(RTD_PWUP_INI);
  104. #if(HDCP_ENABLE)
  105.     RTDCodeW(BKSV);
  106.     KEYCodeW(HDCP_KEY_A_Port);
  107. #endif
  108.     RTDSetByte(HOSTCTRL_02, 0x40);          // Wake RTD up
  109.     RTDSetByte(DPLL_CTRL_D0, 0x11);         // Enable DPLL
  110.     RTDSetByte(VDIS_SIGINV_21, 0x00 | DISP_EO_SWAP | DISP_RB_SWAP | DISP_ML_SWAP);       // DHS, DVS, DEN, DCLK MUST NOT be inverted.
  111.     RTDSetBit(VDIS_CTRL_20, 0xfd, 0x21);    // DHS, DVS, DEN, DCLK and data are clamped to 0
  112.     RTDSetBit(DIS_TIMING0_3A, 0xff, 0x20);  // Force display timing enable
  113.     RTDSetBit(DIS_TIMING0_3A, 0xdf, 0x00);  // Stop forcing
  114.     _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
  115.     RTDSetBit(VDIS_CTRL_20, 0xfe, 0x00);    // Stop display timing
  116.     RTDSetByte(HOSTCTRL_02, 0x42);          // Put RTD2020 back into power-saving mode
  117.     RTDSetByte(DPLL_CTRL_D0, 0x10);         // Disable DPLL
  118.     RTDCodeW(RTD_DDC_TABLE);
  119.     RTDCodeW(RTD_IO_INI);
  120. #if(!SOURCE_AUTO_SCAN)
  121.     PowerDown_VDC();
  122.     //Device_Power(VDC_POWER,OFF);
  123. #endif
  124. PowerDown_TMDS();
  125.     PowerDown_LVDS();
  126.     //Device_Power(TMDS_POWER,OFF);
  127.     //Device_Power(LVDS_POWER,OFF);
  128.     Delay_Xms(10);
  129.     // Check and initial EEPROM if necessary
  130.     Delay_Xms(10);
  131.     Check_EEPROM();
  132.     Delay_Xms(10);
  133.     // Turn timer0 on. Let's go ~~~
  134.     TR0     = 1;
  135. }
  136. void Power_Up_Init(void)    // Do once when power up
  137. {
  138.    
  139.      RTDSetBit(HOSTCTRL_02,0xfb,0x00);    // RTD turn on
  140.     Reset_RTD();            // Initial RTD3000 & OSD
  141. #if(ADC_DEFFERENTIAL)
  142.     RTDSetBit(ADC_DIFF_MODE_EC,0xff,0x40); //Differential mode
  143. #else
  144.     RTDSetBit(ADC_DIFF_MODE_EC,0xbf,0x00); //Single endded mode
  145. #endif
  146. #if(MCU_TYPE == MCU_WINBOND)
  147.     bLIGHT_PWR  = LIGHT_OFF;
  148.     bPANEL_PWR  = PANEL_OFF;
  149. bPanel_Status = _OFF;
  150. #else
  151.     MCU_WriteBacklightPower(LIGHT_OFF);
  152.     MCU_WritePanelPower(PANEL_OFF);
  153.     bPanel_Status = _OFF;
  154. #endif
  155. #if(TUNE_APLL)
  156. ucPE_Max = 0;
  157. RTDSetByte(DV_TOTAL_STATUS_3D,0x40); //clear PE Max value
  158. #endif
  159. #if(!SOURCE_AUTO_SCAN)
  160.     PowerDown_VDC();
  161. //Device_Power(VDC_POWER,OFF);
  162. #endif
  163.     PowerDown_LVDS();
  164. //Device_Power(LVDS_POWER,OFF);
  165. #if(TMDS_ENABLE)
  166. RTDSetByte(ANG_PERFORMANCE2_A3, 0x26); //Go about the TMDS impedence match
  167. RTDSetByte(ANG_PERFORMANCE2_A3, 0x36);
  168. #endif
  169.     //RTDSetBit(OVL_CTRL_6D, 0x3f, (stGUD1.FUNCTION & 0x10) ? 0x80 : 0x00);
  170.     RTDSetBit(OVL_CTRL_6D, 0xc7, (stGUD1.INPUT_SOURCE & 0xe0) >> 2);
  171. #if (SPREAD_SPECTRUM)
  172. //    RTDSetBit(SPREAD_SPECTRUM_99,0x03,0x00);
  173.     RTDSetBit(DPLL_N_D2,0xff,0x20); //DPLL spread spectrum Enable
  174. #endif
  175.     Init_Input_Source();    // Initial Mode & OSD
  176. }
  177. void Reset_RTD(void)
  178. {
  179. #if(MCU_TYPE == MCU_WINBOND)
  180. #if(RESET_TYPE == RESET_IN)
  181.     bRTD_RST    = 0;
  182. Delay_Xms(10);
  183.     do
  184.     {
  185.         // Wait for reset complete
  186.         bRTD_RST    = 1;
  187.         Delay_Xms(6);
  188.     }
  189.     while (!bRTD_RST);
  190. #endif
  191. #else
  192. #if(RESET_TYPE == RESET_IN)
  193.     MCU_WriteRtdReset(LOW);
  194.  Delay_Xms(10);
  195.     do
  196.     {
  197.         // Wait for reset complete
  198.         MCU_WriteRtdReset(HIGH);
  199.         Delay_Xms(6);
  200.     }
  201.     while (MCU_ReadRtdReset()!=HIGH);
  202. #endif
  203. #endif
  204.     
  205.     // Initial RTD3000 & free background display
  206.     RTDCodeW(RTD_PWUP_INI);
  207. #if(HDCP_ENABLE)
  208.     RTDCodeW(BKSV);
  209.     KEYCodeW(HDCP_KEY_A_Port);
  210. #endif
  211.     RTDCodeW(RTD_DDC_TABLE);
  212.     RTDCodeW(RTD_IO_INI);
  213.     RTDSetByte(HOSTCTRL_02, 0x40);          // Wake RTD up
  214.     RTDCodeW(FreeV);
  215.     RTDSetByte(VDIS_SIGINV_21, 0x00 | DISP_EO_SWAP | DISP_RB_SWAP | DISP_ML_SWAP);       // DHS, DVS, DEN, DCLK MUST NOT be inverted.
  216.     RTDSetBit(VDIS_CTRL_20, 0xfd, 0x01);    // DHS, DVS, DEN, DCLK and data are clamped to 0
  217.     // Initial RTD3000-OSD
  218.     RTDCodeW(OSD_PWUP_INI);
  219.     RTDOSDW(OSD_Reset);
  220.     // Initial OSD palette
  221.     RTDCodeW(Palette_Open);
  222.     RTDCodeW(Palette_7);
  223.     RTDCodeW(Palette_Close);
  224.     // Check and initial EEPROM if necessary
  225. //    Delay_Xms(10);
  226. //    Check_EEPROM();
  227. //    Delay_Xms(10);
  228.     Set_Gamma();
  229.     Set_Dithering();
  230.     Set_Bright_Contrast();
  231. }
  232. void Init_Input_Source(void)
  233. {       
  234.     ucMode_Found    = MODE_NOSUPPORT;
  235.     ucMode_Curr     = MODE_NOSIGNAL;
  236.     ucMode_Times    = 0;
  237.     ucAV_Mode       = 0;
  238.     bVpole_Curr     = 1;                // The initial set of polarity is positive
  239.     bHpole_Curr     = 1;                // The initial set of polarity is positive
  240.     // Unknown signal source
  241.     if (SOURCE_MAX < (stGUD1.INPUT_SOURCE & 0x07))
  242.         stGUD1.INPUT_SOURCE = (stGUD1.INPUT_SOURCE & 0xf8) | SOURCE_VGA;
  243. #if (TMDS_ENABLE == _FALSE)
  244.     if (SOURCE_DVI == (stGUD1.INPUT_SOURCE & 0x07))
  245.         stGUD1.INPUT_SOURCE = (stGUD1.INPUT_SOURCE & 0xf8) | SOURCE_VGA;
  246. #endif
  247. #if (VIDEO_CHIP == VDC_NONE)
  248.     if (SOURCE_AV == (stGUD1.INPUT_SOURCE & 0x07) || SOURCE_SV == (stGUD1.INPUT_SOURCE & 0x07))
  249.         stGUD1.INPUT_SOURCE = (stGUD1.INPUT_SOURCE & 0xf8) | SOURCE_VGA;
  250. #endif
  251. #if (VIDEO_CHIP != VDC_SAA7118)
  252.     if (SOURCE_YUV == (stGUD1.INPUT_SOURCE & 0x07))
  253.         stGUD1.INPUT_SOURCE = (stGUD1.INPUT_SOURCE & 0xf8) | SOURCE_VGA;
  254. #endif
  255. #if (TV_CHIP == TV_NONE)
  256.     if (SOURCE_TV == (stGUD1.INPUT_SOURCE & 0x07))
  257.         stGUD1.INPUT_SOURCE = (stGUD1.INPUT_SOURCE & 0xf8) | SOURCE_VGA;
  258. #endif
  259.     // Because internal ADC power state will not affect VGA mode detection,
  260.     // we always turn off ADC when source changed, and turn it on again only 
  261.     // when a valid VGA mode is going to display.
  262.     PowerDown_ADC();
  263.     //Device_Power(ADC_POWER,OFF);
  264. #if(SOURCE_AUTO_SCAN)
  265. PowerUp_VDC();
  266. Delay_Xms(10);
  267. #if (VIDEO_CHIP == VDC_SAA7114 || VIDEO_CHIP == VDC_SAA7115 || VIDEO_CHIP == VDC_SAA7118)
  268.         I2CWrite(V_ENABLE);         // VIDEO ENABLE
  269.         I2CWrite(VIDEO_ALL);
  270. #endif
  271.     bSourceScanEnable = _TRUE;
  272. #endif
  273.     switch (stGUD1.INPUT_SOURCE & 0x07)
  274.     {
  275.     case SOURCE_VGA:
  276. #if (VIDEO_CHIP == VDC_SAA7114 || VIDEO_CHIP == VDC_SAA7115 || VIDEO_CHIP == VDC_SAA7118)
  277.         I2CWrite(V_DISABLE);        // VIDEO DISABLE
  278. #endif
  279.         I2CWrite(V_NOWORK);         // VIDEO No Work
  280. #if(!SOURCE_AUTO_SCAN)
  281.     PowerDown_VDC();
  282. PowerDown_TMDS();
  283. //Device_Power(VDC_POWER,OFF);
  284. //Device_Power(TMDS_POWER,OFF);
  285. #else
  286.     PowerUp_VDC();
  287. PowerUp_TMDS();
  288. //Device_Power(VDC_POWER,ON);
  289. //Device_Power(TMDS_POWER,ON);
  290. #endif
  291. #if(TMDS_ENABLE)
  292.         RTDSetBit(TMDS_CORRECTION_FF,0xfc,0x00);
  293. #endif
  294.         // Set user's ADC gain and offset settings
  295.         SetADC_GainOffset();
  296.         ucInputSrc  = SOURCE_VGA;        
  297.         ucSync_Type = SYNC_SS;
  298.         RTDCodeW(VGA_INI_SS);
  299.         break;
  300.   case SOURCE_DVI:
  301. #if (VIDEO_CHIP == VDC_SAA7114 || VIDEO_CHIP == VDC_SAA7115 || VIDEO_CHIP == VDC_SAA7118)
  302.         I2CWrite(V_DISABLE);        // VIDEO DISABLE
  303. #endif
  304.         I2CWrite(V_NOWORK);         // VIDEO No Work
  305. #if(!SOURCE_AUTO_SCAN)
  306.     PowerDown_VDC();
  307. //Device_Power(VDC_POWER,OFF);
  308. #else
  309.     PowerUp_VDC();
  310. //Device_Power(VDC_POWER,ON);
  311. #endif
  312. #if(TMDS_ENABLE)
  313.         RTDSetBit(TMDS_CORRECTION_FF,0xfc,0x00);
  314. #endif
  315.         PowerUp_TMDS();
  316.         //Device_Power(TMDS_POWER,ON);
  317.         ucInputSrc  = SOURCE_DVI;        
  318.         RTDCodeW(DVI_INI);
  319.         break;      
  320.     case SOURCE_AV:
  321.     case SOURCE_TV:
  322. #if(SOURCE_AUTO_SCAN)
  323.         if((SOURCE_AV != MASTER_SOURCE) && (SOURCE_AV != SLAVE_SOURCE) && (SOURCE_AV != THIRD_SOURCE))
  324.    bSourceScanEnable = _FALSE;
  325. #endif  
  326.         PowerUp_VDC();
  327. #if(TMDS_ENABLE)
  328.         RTDSetBit(TMDS_CORRECTION_FF,0xfc,0x00);
  329. #endif
  330.         //Device_Power(VDC_POWER,ON);
  331.         // PCB rev.C
  332.         // For PCB rev.C, turning down TMDS will turning on video decoder.
  333.         // However, video decoder needs times to wake up.
  334.         Delay_Xms(10);
  335. #if (VIDEO_CHIP == VDC_SAA7114 || VIDEO_CHIP == VDC_SAA7115 || VIDEO_CHIP == VDC_SAA7118)
  336.         I2CWrite(V_ENABLE);         // VIDEO ENABLE
  337.         I2CWrite(VIDEO_ALL);
  338. #endif
  339.         I2CWrite(AV_DETECT);        // VIDEO Detect(AV)
  340.         if (SOURCE_AV == (stGUD1.INPUT_SOURCE & 0x07))
  341.         {
  342.             ucInputSrc  = SOURCE_AV;
  343.         }
  344.         else
  345.         {
  346.             ucInputSrc  = SOURCE_TV;
  347. #if (TV_CHIP != TV_NONE)
  348.             // Set TV channel
  349.             Set_TV_Channel();
  350. #endif
  351.             // Select TV signal input pin and disable AGC of video decoder
  352.             I2CWrite(TV_SOURCE_SEL);
  353.             I2CWrite(TV_SCAN_GAIN);
  354.         }
  355.         RTDCodeW(VIDEO_INI);
  356.         I2CRead(ADDR_VIDEO, 0x1f, 0x01);
  357.         break;
  358.         
  359.     case SOURCE_SV:
  360. #if(SOURCE_AUTO_SCAN)
  361.         if((SOURCE_SV != MASTER_SOURCE) && (SOURCE_SV != SLAVE_SOURCE) && (SOURCE_SV != THIRD_SOURCE))
  362.    bSourceScanEnable = _FALSE;
  363. #endif  
  364.  PowerUp_VDC();
  365.  //Device_Power(VDC_POWER,ON);
  366.        Delay_Xms(10);
  367. #if (VIDEO_CHIP == VDC_SAA7114 || VIDEO_CHIP == VDC_SAA7115 || VIDEO_CHIP == VDC_SAA7118)
  368.         I2CWrite(V_ENABLE);         // VIDEO ENABLE
  369.         I2CWrite(VIDEO_ALL);
  370. #endif
  371.         I2CWrite(SV_DETECT);        // VIDEO Detect(SV)
  372.         ucInputSrc  = SOURCE_SV;
  373.         RTDCodeW(VIDEO_INI);
  374.         I2CRead(ADDR_VIDEO, 0x1f, 0x01);
  375.         break;
  376.     case SOURCE_YUV:
  377. #if(SOURCE_AUTO_SCAN)
  378.         if((SOURCE_YUV != MASTER_SOURCE) && (SOURCE_YUV != SLAVE_SOURCE) && (SOURCE_YUV != THIRD_SOURCE))
  379.    bSourceScanEnable = _FALSE;
  380. #endif  
  381.     PowerUp_VDC();
  382. //Device_Power(VDC_POWER,ON);
  383.         
  384.         Delay_Xms(10);
  385.         I2CWrite(V_ENABLE);         // VIDEO ENABLE
  386.         I2CWrite(VIDEO_ALL);
  387.         I2CWrite(VIDEO_YUV);
  388.         I2CWrite(YUV_DETECT);       // VIDEO Detect(YUV)
  389.         ucInputSrc  = SOURCE_YUV;
  390.         RTDCodeW(VIDEO_INI);
  391.         I2CRead(ADDR_VIDEO, 0x1f, 0x01);
  392.         break;
  393.     }
  394. #if (AUDIO_TYPE != AUDIO_NONE)
  395.     SetVolume();
  396. #endif
  397. }
  398. void Power_Status_Init(void)
  399. {
  400. #if (POWER_KEY_TYPE == TYPE_ONE_TOUCH)
  401. #if (KEY_DISABLED == POWER_KEY_MASK)
  402.     // Force system to power up when no power-key
  403.     bPower_Status   = 1;
  404.     Power_Up_Init();
  405.     bStable     = 0;                    // Assume input signal is not stable when power up
  406.     bReload     = 1;                    // Always reload font when power up
  407.     bFrameSync  = 0;                    // Not sync yet
  408.     ucModeCnt   = MODE_DETECT_FREQ;     // Reset Input Mode Detector
  409.     
  410.     Measure_Mode();                     // Measure mode-timing
  411.     Set_Task(STATE_POWERUP);            // Notify Task State Machine
  412. #else
  413.     // Restore previous power status
  414.     if (0 == (stGUD1.FUNCTION & 0x08))
  415.     {
  416.         bPower_Status   = 1;
  417.           if(((stGUD3.TV_SETTING & 0x08)==0x08) || ((stGUD3.TV_SETTING & 0x20)==0x20)) //anson
  418.                 {
  419.                  stGUD3.TV_SETTING &= 0xF7 ;  
  420.                  Save_GUD3();
  421.                 }
  422.         Power_Up_Init();
  423.         
  424.         bStable     = 0;                    // Assume input signal is not stable when power up
  425.         bReload     = 1;                    // Always reload font when power up
  426.         bFrameSync  = 0;                    // Not sync yet
  427.         ucModeCnt   = MODE_DETECT_FREQ;     // Reset Input Mode Detector
  428.     
  429.         Measure_Mode();                     // Measure mode-timing
  430.         Set_Task(STATE_POWERUP);            // Notify Task State Machine
  431.     }
  432.     else
  433.     {
  434.         bPower_Status   = 0;
  435.     }
  436. #endif
  437. #else
  438.     // Do nothing when power-key is two-state type.
  439.     bPower_Status   = 0;
  440. #endif
  441. }