//----------------------------------------------------------------------------------------------------
// ID Code      : Mcu.c No.0002
// Update Note  :
//
//----------------------------------------------------------------------------------------------------

#define __MCU__

#include  "Core\Header\Include.h"

/**
 * CMainSystemInitial
 * Initial MCU for once when power up
 * @param <none>
 * @return {none}
 *
 */

#if(_MCU_TYPE == _REALTEK_RTD2662_EMCU)
/**
* CMcuInitial
* Initialize the MCU _REALTEK_RTD2662_EMCU
* @param <none>
* @return {none}
*
*/
void CMcuInitial(void)
{
    /*
    	IE		= 0x00;                         ///! Disable all interrupts
        TMOD	= 0x11;                     	///! Timer0 & Timer1 as timer in Mode1
        TR0    	= 0;                            ///! Stop Timer0
       	TF0    	= 0;                            ///! Clear Timer0 Overflow Bit
        TL0     = _TIMER0_COUNT_LBYTE;    		///! Load Timer0 low-byte   (for 250us)
        TH0    	= _TIMER0_COUNT_HBYTE;        	///! Load Timer0 high-byte  (for 250us)
        TR1 	= 0;							///! Stop Timer1
        TF1 	= 0;							///! Clear Timer1 Overflow Bit
        TL1 	= _TIMER1_COUNT_LBYTE;  		///! Load Timer1 low-byte   (for 11ms)
        TH1	 	= _TIMER1_COUNT_HBYTE;  		///! Load Timer1 hifh-byte  (for 11ms)
        IE      = 0x8A;                         ///! Enable interrupts of Timer0 and Timer1
        IT1 	= 0;							///! INT1 trigger when low electronical level
        PT0 	= 1;							///! Timer0 interrupt as prior
        PT1 	= 0;
    */

    IE      = 0x00;                         // Disable all interrupts
    TMOD    = 0x11;                         // Program Timer0 & Timer1 to Mode#1
    TR0     = 0;                            // Stop Timer0
    TF0     = 0;                            // Clear Timer0 Overflow Bit
    TL0     = _TIMER0_COUNT_LBYTE;          // Load Timer0 low-byte   (for 1ms)
    TH0     = _TIMER0_COUNT_HBYTE;          // Load Timer0 high-byte  (for 1ms)
    TR1     = 0;                            // Stop Timer1
    TF1     = 0;                            // Clear Timer1 Overflow Bit
    //IE      = 0x8a;                         // Enable interrupts of Timer0
    IE      = 0x82;                         // Enable interrupts of Timer0

    //20080425, GPIO pull high
#if  0//_PCB_TYPE==_PCB_PT2662C1B		//change 20090915
    MCU_PIN_PULLUP_CTRL20_FFA8 =0xff;//MCU_PIN_PULLUP_CTRL21_FFA9 |= _BIT7;	//Pin98-103 pull high, active only in GPIO    //change 20090915
#endif
    //20080425, GPIO pull high

//#if(_PCB_TYPE == _RTD2662_QA_PCB)
    MCU_WATCHDOG_TIMER_FFEA = 0x00; //dis wdt, wdt default enable

    //eric 20070627 sync v006
    MCU_WATCHDOG_TIMER_FFEA |= 0x40;//clr wdt.//eric 20070627
    MCU_CTRL_FFED          = 0x80; //FlashDIV=1


    MCU_HDMI_DDC_ENA_FF2C = 0x00;  //disable HDMI DDC channel

#if _HDMI_EDID_RW

//20080421, DDC support for digital port 0. start...
//hdmi port left
    MCU_DVI_DDC_ENA_FF1E = 0x07;//open ddc2
    MCU_HDMI_DDC_ENA_FF2C = 0x00;//close ddc3
    MCU_DDCRAM_PART_FF21 = 0x2b;//addcram_st=3,dddcram_st=3,hddcram_st=3 (Xram=640 Byte)
//hdmi port right ???
//	MCU_DVI_DDC_ENA_FF1E = 0x00;//open ddc2
//	MCU_HDMI_DDC_ENA_FF2C = 0x02;//close ddc3
//	MCU_DDCRAM_PART_FF21 = 0x3a;//addcram_st=2(0xFd00),dddcram_st=2(0xfd80),hddcram_st=3(0xfd80)
//20080421, DDC support for digital port 0. end...

#else	//#if _HDMI_EDID_RW

//Gary for Interior HDMI  DDC 	20070711
#if ((_HDMI_EDID==_ON)&&(_HDMI_DDC_CHANNEL_SELECT==_DDC2))

    MCU_DVI_DDC_ENA_FF1E = 0x02;
    MCU_HDMI_DDC_ENA_FF2C = 0x00;  //disable HDMI DDC channel
    MCU_DDCRAM_PART_FF21 = 0x2b;//addcram_st=2(0xfd00),dddcram_st=20(0xfd80),hddcram_st=3

#elif((_HDMI_EDID==_ON)&&(_HDMI_DDC_CHANNEL_SELECT==_DDC3))

    MCU_DVI_DDC_ENA_FF1E = 0x00;//disable DVI DDC channel
    MCU_HDMI_DDC_ENA_FF2C = 0x02;
    MCU_DDCRAM_PART_FF21 = 0x3a;//addcram_st=3(0xFd80),dddcram_st=2(0xfd80),hddcram_st=2(0xfe00)

#else

    //MCU_ADC_DDC_ENA_FF1B = 0x00;  //Disable ADC DDC channel
    MCU_DVI_DDC_ENA_FF1E = 0x00;//Disable DVI DDC channel
    MCU_HDMI_DDC_ENA_FF2C = 0x00;  //disable HDMI DDC channel
    MCU_DDCRAM_PART_FF21 = 0x3f;//addcram_st=3,dddcram_st=3,hddcram_st=3 (Xram=640 Byte)
#endif

#endif	//#if _HDMI_EDID_RW



//20080421, DDC support for VGA. start...
    MCU_ADC_DDC_ENA_FF1B = 0x07;
//20080421, DDC support for VGA. end...

    MCU_PIN_SHARE_CTRL04_FF9A |= 0x05; 		//P6.6/P6.7 opendrain output for I2C master
    MCU_PORT_READ_CTRL_FFC0 = 0xfd; 		//read All GPIO value from bus,except p3

    //MCU_BANK_SWITCH_CTRL_FFFC = 0x08; 	// enable global XFR

    //eric 20070627 sync v006
    //MCU_CTRL_FFED = 0x85; //mcu clk out

    //MCU_BANK_SWITCH_CTRL_FFFC = 0x09; 		// enable global XFR & enable bank switching func(P3.5)
    MCU_BANK_SWITCH_CTRL_FFFC = 0x0B; 		// enable global XFR & enable bank switching func(Pbank)
    MCU_BANK_START_FFFD = 0x01;
    MCU_BANK_SEL_FFFE = 0x00;


    MCU_CLOCK_CTRL_FFEE |= 0x40;		//keep mcu peripheral running while mcu stopped by spi flash access

    //MCU_BANK_START_FFFD = 0x01; 		//Xdata_Bstart
    //MCU_BANK_SEL_FFFE = 0x00; 			//Xdata_bank_sel, Bank0

    MCU_PWML_FF46 |= 0x80;					//PWM_W_DB_EN
    //MCU_PWML_FF46 |= _BIT2;				//PWM2 is positive palse
    //MCU_PWM_EN_FF48 |= 0x02;				//PWM1 output enable

#ifdef _BACKLIGHT_SUPPORT_USE_PWM4
    MCU_PWM_EN_FF48 |= 0x10;				//PWM2/PWM1 output enable
    MCU_PWM_EN_FF48 |= 0x08;				//PWM4 output enable
#else
    MCU_PWM_EN_FF48 |= 0x10;				//PWM2/PWM1 output enable
#endif



    //20080418
    MCU_PWM_CK_SEL_FF3A = 0x3F;			//sel pwm clk as PLL output, so pwm is 60KHz

    //PT2660A1/_PCB_PT2662F1A PWM5 for 4052's negtive voltage
#if (_PCB_PT2660A1A==_PCB_TYPE ||_PCB_PT2662F1A ==_PCB_TYPE ||_PCB_PT2662F1C==_PCB_TYPE)
    MCU_PWML_FF46 |= _BIT5;				//PWM5 is positive palse
    SET_NEG_PWM(128);
    MCU_PWM_EN_FF48 |= _BIT5;				//PWM5 output enable
#endif	//#if (_PCB_PT2660A1A==_PCB_TYPE)


    //PT2662B1C PWM5 for 4052's negtive voltage
#if (_PCB_PT2662B1C==_PCB_TYPE)
    MCU_PWML_FF46 |= _BIT5;				//PWM5 is positive palse
    SET_NEG_PWM(128);
    MCU_PWM_EN_FF48 |= _BIT5;				//PWM5 output enable
#endif	//#if (_PCB_PT2660C==_PCB_TYPE)


    //RTD2662 PWM1 for volume (Pin71)
#if _VOLUME_REVERSE
    MCU_PWM4H_DUT_FF50 = 0xff;			//minimum audio volume
    MCU_PWM45L_DUT_FF52 |= 0xf0;			//
#else
    MCU_PWM1H_DUT_FF4B = 0x00;			//minimum audio volume
    MCU_PWM01L_DUT_FF4C &= 0x0f;			//
#endif

#if _BACKLIGHT_SUPPORT
#ifdef _BACKLIGHT_SUPPORT_USE_PWM4
    MCU_PWM3H_DUT_FF4E=0XFF;
    MCU_PWM23L_DUT_FF4F&=0XF0;
#endif
    //RTD2662 PWM2 for backlight (Pin98)
    //MCU_PWM1H_DUT_FF4B = 0xff;			//minimum backlight
    //MCU_PWM01L_DUT_FF4C &= 0xf0;			//keep low bit to get paulse output
#endif

#if(_FAST_READ == _ON)
    CMCUSetFlashClk();
    /*    //   M2PLL Enable
        MCU_SCA_INF_CTRL_FFF3 = 0x20; //BWR data enable, Non_INC
        MCU_SCA_INF_ADDR_FFF4 = 0x9f; //DDC_addr
        MCU_SCA_INF_DATA_FFF5 = 0x01; //DDC_data, page-1
        MCU_SCA_INF_ADDR_FFF4 = 0xE4; //M2-PLL Reg Ctrl Address
        MCU_SCA_INF_DATA_FFF5 = 0x00; //Enable M2PLL

        //  Power-saving mode Enable ?
        MCU_SCA_INF_ADDR_FFF4 = 0x01; //DDC_addr- Host Ctrl
        MCU_SCA_INF_DATA_FFF5 = 0x00; //DDC_data - Disable power-saving mode

        //  M2PLL output enable
        MCU_SCA_INF_ADDR_FFF4 = 0xE0; //M2-PLL-CTRL0
        MCU_SCA_INF_DATA_FFF5 = 0x82; //M2PLL output enable
     #if 1
        //  Enable FastRead mode
        MCU_CEN_CTRL_FF76 = ( ((0x3)<<4) | ((0x1)<<2) | ((0x1)<<0) );  //ChipEnable Timing Setup

    #if (_TURBO_MODE==_ON)
        MCU_FAST_READ_OP_CODE_FF6B = 0x3B;  //Fast Dual data read for winbond flash only
        MCU_READ_INSTRUCTION_FF6C |= ( ((0x2)<<6) | ((0x1)<<5) | ((0x0)<<4) | ((0x0)<<2) | ((0x0)<<0) ); //Fast Read mode, DI@half cycle, Dout@one cycle
    #else
        MCU_FAST_READ_OP_CODE_FF6B = 0x0B;  //Fast Read for all kind of flash
        MCU_READ_INSTRUCTION_FF6C |= ( ((0x1)<<6) | ((0x1)<<5) | ((0x0)<<4) | ((0x0)<<2) | ((0x0)<<0) ); //Fast Read mode, DI@half cycle, Dout@one cycle
    #endif // End of #if (_TURBO_MODE==_ON)

        CMCUFlashClkDiv(_FLASH_CLK_DIV);    //Flash CLK
        CMCUClkDiv(_MCU_CLK_DIV);      //MCU=FlashClk/2
        MCU_CTRL_FFED |= 0x02; //Switch to PLL
    #endif*/
#endif // End of #if(_FAST_READ == _ON)

//20071214, x.hu
//update for: code in flash is changed while power up (AC line insert in operation)
#if(_FAST_READ != _ON)
    MCU_CEN_CTRL_FF76 = ( ((0x3)<<4) | ((0x1)<<2) | ((0x1)<<0) );  //ChipEnable Timing Setup
    MCU_FAST_READ_OP_CODE_FF6B = 0x0B;  //Fast Read for all kind of flash
    MCU_READ_INSTRUCTION_FF6C |= ( ((0x1)<<6) | ((0x1)<<5) | ((0x0)<<4) | ((0x0)<<2) | ((0x0)<<0) ); //Fast Read mode, DI@half cycle, Dout@one cycle
#endif // End of #if(_FAST_READ != _ON)

#if _FLASH_PROTECT_SUPPORT		//090427
    if ((CFlashReadStatusByte()&(7<<2))!=(7<<2))
        CFlashWriteStatusByte(0x80|(7<<2));
#else
    MCU_COMMON_INST_EN_FF60 = 0x68;		//Write after WREN & Write_No=1
    MCU_COMMON_OP_CODE_FF61 = 0x01;		//Instruction Code(WRSR:0x01)
    MCU_FLASH_PROG_ISP0_FF64 = 0x80;		//write byte(SRWD=1)
    MCU_COMMON_INST_EN_FF60 |= 0x01;		//com_inst_en
    //MCU_COMMON_INST_EN_FF60 = 0x42;		//Read & Read_No=1
    //MCU_COMMON_OP_CODE_FF61 = 0x05;		//Instruction Code(JEDEC_ID:0x9F / RDSR:0x05)
    //MCU_COMMON_INST_EN_FF60 |= 0x01;		//com_inst_en
    //pData[0] = MCU_COMMON_INST_RD_PT0_FF67;
    //MCU_THRESHOLD_VALUE_FF0F = pData[0];
#endif

//#endif
    CLR_TUNER_POWER();
    //20080823 ccy modify
#if _PCB_TYPE == _PCB_TEST
    CLR_DVD_POWERONOFF();
#endif

}

#if(_FAST_READ == _ON)
void CMCUSetFlashClk(void)
{
    //   M2PLL Enable
    MCU_SCA_INF_CTRL_FFF3 = 0x20; //BWR data enable, Non_INC
    MCU_SCA_INF_ADDR_FFF4 = 0x9f; //DDC_addr
    MCU_SCA_INF_DATA_FFF5 = 0x01; //DDC_data, page-1
    MCU_SCA_INF_ADDR_FFF4 = 0xE4; //M2-PLL Reg Ctrl Address
    MCU_SCA_INF_DATA_FFF5 = 0x00; //Enable M2PLL

    //  Power-saving mode Enable ?
    MCU_SCA_INF_ADDR_FFF4 = 0x01; //DDC_addr- Host Ctrl
    MCU_SCA_INF_DATA_FFF5 = 0x00; //DDC_data - Disable power-saving mode

    //  M2PLL output enable
    MCU_SCA_INF_ADDR_FFF4 = 0xE0; //M2-PLL-CTRL0
    MCU_SCA_INF_DATA_FFF5 = 0x82; //M2PLL output enable

    //  Enable FastRead mode
    MCU_CEN_CTRL_FF76 = ( ((0x3)<<4) | ((0x1)<<2) | ((0x1)<<0) );  //ChipEnable Timing Setup

#if (_TURBO_MODE==_ON)
    MCU_FAST_READ_OP_CODE_FF6B = 0x3B;  //Fast Dual data read for winbond flash only
    MCU_READ_INSTRUCTION_FF6C |= ( ((0x2)<<6) | ((0x1)<<5) | ((0x0)<<4) | ((0x0)<<2) | ((0x0)<<0) ); //Fast Read mode, DI@half cycle, Dout@one cycle
#else
    MCU_FAST_READ_OP_CODE_FF6B = 0x0B;  //Fast Read for all kind of flash
    MCU_READ_INSTRUCTION_FF6C |= ( ((0x1)<<6) | ((0x1)<<5) | ((0x0)<<4) | ((0x0)<<2) | ((0x0)<<0) ); //Fast Read mode, DI@half cycle, Dout@one cycle
#endif

    CMCUFlashClkDiv(_FLASH_CLK_DIV);    //Flash CLK
    CMCUClkDiv(_MCU_CLK_DIV);      //MCU=FlashClk/2
    MCU_CTRL_FFED |= 0x02; //Switch to PLL

}


void CMCUFlashClkDiv(BYTE clkdiv) // _FlashClkDiv(x=0-15): SPI Flash Clock Generator (OSC/PLL°£¥HDIV:from 0~15)
{
    clkdiv &= 0x0F;
    clkdiv = (clkdiv << 2);
    clkdiv |= (MCU_CTRL_FFED & 0xC3);
    MCU_CTRL_FFED = clkdiv;
}

void CMCUClkDiv(BYTE clkdiv)  // _MCUClkDiv(x=0-15): 8051 Clock Generator (Flash_Clk/DIV:from 0~15)
{
    clkdiv &= 0x0F;
    clkdiv = (clkdiv << 2);
    clkdiv |= (MCU_CLOCK_CTRL_FFEE & 0xC3);
    MCU_CLOCK_CTRL_FFEE = clkdiv;
}
#endif //End of #if(_FAST_READ == _ON)

#endif


