Initial.c
资源名称:SDK_M5661.rar [点击查看]
上传用户:hjhsjcl
上传日期:2020-09-25
资源大小:11378k
文件大小:32k
源码类别:
压缩解压
开发平台:
C++ Builder
- /*==========================================================================
- Copyright (c) 2004 ALi Corporation. All Rights Reserved
- File: Initial.c
- content: Default Program Module load by Boot Rom.
- Process initialization of the device.
- History: Created by David Chiu 2004/3/19
- ==========================================================================*/
- //#define _REG_5661_H_
- #define _INITIAL_H_
- #include <DP8051XP.H>
- #include "TypeDef.h"
- #include "Const.h"
- #include "Reg5661.h"
- #include "Common.h"
- #include "Initial.h"
- #include "Key.h"
- #if(_C_MODE_)
- #include "OledString.h"
- #include "OLED.h"
- #include "IconDefine.h"
- #else
- #include "Lcd.h"
- #endif
- #include "FileSystem.h"
- #include "NandFlash.h"
- #include "Sd.h"
- #include "DDM.h"
- #include "SaveSetting.h"
- #include "ExtCodec.h"
- #include "IntCodec.h"
- #if(ENABLE_HOST_FUNCTION)
- #include "UsbHostCommon.h"
- #include "UsbHost.h"
- #endif
- /* //Walace060108 debug
- #define STORE_COUNT 256
- XBYTE gxbDebugData[STORE_COUNT];
- */
- void Init8051(void)
- {
- CKCON &= 0xF8;
- CKCON |= 0x07; // STRECH = 111
- obREADYENL=SM_EN|USB_EN|SD_EN;
- obREADYENH=(SRAMRA_EN|RA_EN|SYSREG_EN)|0xF0;
- CKCON &= 0xF8; // STRECH = 000
- //CKCON &= 0xF9; // STRECH = 001 //David 050406
- IE = 0;
- EA = 1;
- IP = 0;
- PX0 = 1;
- TMOD = 0x10;
- TCON = 0;
- T2CON= 0;
- TA=0xAA;
- TA=0x55;
- WDCON =0;
- //Reset clock
- //Renshuo050528#D
- // obCLKMCUCTRL=0xD0; //MCU clock use XTALI 12MHz
- }
- void InitDisableClk(void)
- {
- ClkSdDis(); //Disable SD clk
- ClkSmDis(); //Disable SM clk
- ClkMsDis(); //Disable MS clk
- ClkDspDis(); //Disable DSP clk
- ClkCdcDis(); //Disable CODEC clk
- ClkI2S1Dis(); //Disable I2S_1 clk
- ClkI2S2Dis(); //Disable I2S_2 clk
- ClkI2CmDis(); //Disable I2C_M clk
- ClkLcdDis(); //Disable LCD clk
- }
- void InitPowerCtrl(void)
- {
- obSYSSOFTRSTCTRL |= 0x80; //Disable USB Reset
- obUSBOTGCTRL |= 0x02; //DPLL power down
- obMODSEL3=0x00; //0xFF09,not Bypass RA,RA path=SRAM Ra
- #if(DEBUG_M5638C) //Renshuo050528#D
- //delay a while before DCV R/W
- EnableTimer0();
- gxbLcdCounter=0;
- while(gxbLcdCounter<6) {}
- DCV_TEST_EN();
- obDCVARRD=0x19;
- //obDCVWRDATA=0x01; //quick start
- obDCVWRDATA = obDCVRDDATA | 0x01; //quick start enable //Alan 060727#1
- DCV_TEST_DIS();
- #endif
- obDCVARRD=0x03; //DCV Addr 0x03
- obDCVWRDATA=(obDCVRDDATA&0xEF); //Set bit4=0
- obDCVARRD=0x08; //DCV Addr 0x08
- obDCVWRDATA=0x88; //Set 0x88 e LSADC reference source
- //David051205#2 start //not check register to avoid error detect
- //obDCVARRD=0x14;
- //if(obDCVRDDATA&0x80) //1: 1 BATT
- #if(ONE_BATT_MODE) //use compiler define
- //End
- {
- #if(DEBUG_M5638C) //vicky050608#3
- //Renshuo050528#D //vicky050607#1
- //obDCVARRD=0x09;
- // Jeff_061109 start
- #if(CIRCUIT_2_PW) //David050719
- //obDCVWRDATA=0x00; //2 power solution
- //SetDcvRegStep(SET_VDD,0x00,SET_VDD_DELAY); //Walace060718#1 // Jeff_061109
- obDCVARRD=0x0C; //DCV Addr 0x0C
- if(obDCVRDDATA==0x46)
- {
- StarUpPWMmodulation(); // Jeff_061031
- }
- else
- {
- //SetDcvRegStep(SET_VDD,0x05,SET_VDD_DELAY); // Set VDDA to 1.65V // Jeff_061124 mark
- SetDcvRegStep(SET_VDD,0x00,SET_VDD_DELAY); // Set VDDA to 1.65V // Jeff_061124
- //obDCVARRD=0x0C; //DCV Addr 0x0C // Jeff_061109
- //obDCVWRDATA=0x06;
- }
- #else
- //obDCVWRDATA=0x75; //3 power solution, Power saving for VDD_D
- SetDcvRegStep(SET_VDD,0x75,SET_VDD_DELAY); //Walace060718#1
- #endif
- obDCVARRD=0x0A; //DCV Addr 0x0A
- obDCVWRDATA=0x52; //Power saving for VDD_IO
- //obDCVARRD=0x0C; //DCV Addr 0x0C
- //obDCVWRDATA=0x06; //PWM Duty Cycle limited for un-know issue
- //SetDCVPWMCycle(PWM_NORMAL_CYCLE); //Walace060607#4
- SetDcvRegStep(SET_PWM,PWM_NORMAL_CYCLE,SET_PWM_DELAY); //Walace060718#1
- // Jeff_061109 end
- obDCVARRD=0x0D; //DCV Addr 0x0D
- obDCVWRDATA=0xF9; //For voltage drop recovering time
- //Joseph, for SAR ADC
- obDCVARRD = 0x00 ;
- obDCVWRDATA = 0x77 ; //Vtop: 1.00V
- obDCVARRD = 0x08 ;
- obDCVWRDATA = 0x88 ;
- DCV_TEST_EN(); //vicky050607#1
- obDCVARRD = 0x1B ;
- obDCVWRDATA = 0x0A ; //for one battery
- obDCVARRD = 0x1C ;
- obDCVWRDATA = 0xA4 ;
- DCV_TEST_DIS();
- #else
- //obDCVARRD=0x09; //Walace060718#1 //DCV Addr 0x09
- #if(CIRCUIT_2_PW) //David050719
- //obDCVWRDATA=0x55; //2 power solution //Walace060718#1
- SetDcvRegStep(SET_VDD,0x55,SET_VDD_DELAY);
- #else
- //obDCVWRDATA=0x77; //3 power solution, Power saving for VDD_D
- SetDcvRegStep(SET_VDD,0x77,SET_VDD_DELAY);
- #endif
- obDCVARRD=0x0A; //DCV Addr 0x0A
- #if(CIRCUIT_2_PW) //David050719
- obDCVWRDATA=0x52; //2 power solution
- #else
- obDCVWRDATA=0x72; //3 power solution,Power saving for VDD_IO
- #endif
- //David050719, set after program load , suggest by Shiaw-Yi
- //obDCVARRD=0x0C; //DCV Addr 0x0C
- //obDCVWRDATA=0x25; //PWM Duty Cycle limited for un-know issue
- obDCVARRD=0x0D; //DCV Addr 0x0D
- obDCVWRDATA=0xF9; //For voltage drop recovering time
- #endif
- }
- #else //0: 2 BATT or Li-Ion //David051205#2 //use compiler define
- {
- obDCVARRD=0x0A; //DCV Addr 0x0A
- obDCVWRDATA=0x56; //Bit 2 e Li-ion0 = 1
- ////Hsien051226#3 remove
- // obDCVARRD=0x0B; //DCV Addr 0x0B
- // obDCVWRDATA=0x08; //Bit 3 e must be 1 for DCV1 OFF
- obDCVARRD=0x10; //DCV Addr 0x10
- obDCVWRDATA=0x45; //DCV2 min & max boundary
- obDCVARRD=0x11; //DCV Addr 0x0d
- obDCVWRDATA=0xF9; //freq. response
- //Walace051212#1 power save 1v8->1v6
- //obDCVARRD=0x09; //DCV Addr 0x09 //David050719
- //obDCVWRDATA=0x00; //2 power solution
- SetDcvRegStep(SET_VDD,0x00,SET_VDD_DELAY); //Walace060718#1
- #if(DEBUG_M5638C) //Renshuo050528#D //vicky050607#1
- //Joseph, for SAR ADC
- #if(BATT_CHECK_AUXIN) //Walace060613#1 //Hsin Hsien apply
- obDCVARRD = 0x00 ;
- obDCVWRDATA = 0xC7 ; //BAT_REF:0.725 //AUX_REF:0.675
- obDCVARRD = 0x08 ;
- obDCVWRDATA = 0xCA ; //
- #else
- obDCVARRD = 0x08 ;
- obDCVWRDATA = 0xC8 ;
- obDCVARRD = 0x00 ;
- obDCVWRDATA = 0xF7 ; //Vtop: 1.05V
- #endif
- DCV_TEST_EN(); //vicky050607#1
- obDCVARRD = 0x1B ;
- obDCVWRDATA = 0x15 ;
- //obDCVWRDATA = 0x0A ; //for two battery
- obDCVARRD = 0x1C ;
- obDCVWRDATA = 0x94 ;
- //obDCVWRDATA = 0xB4 ; //for two battery
- DCV_TEST_DIS();
- #endif
- }
- #endif //David051205#2 // use compiler define
- #if(DEBUG_M5638C) //Renshuo050528#D //vicky050607#1
- // DCV_TEST_EN();
- // obDCVARRD=0x1A;
- // obDCVWRDATA=(obDCVRDDATA&(~0x01)); // 3.3V BandGap Power On
- //Walace060614#2 BandGap start
- #if(ONE_BATT_MODE)
- BANDGAP_1V8_SWITCH();
- CommonDelay(BANDGAP_SWITCH_DELAY);
- BANDGAP_3V3_POWER_OFF();
- #else
- BANDGAP_3V3_POWER_ON();
- CommonDelay(BANDGAP_SWITCH_DELAY);
- BANDGAP_3V3_SWITCH();
- #endif
- //Walace060614#2 BandGap end
- DCV_TEST_EN();
- obDCVARRD = 0x1B;
- obDCVWRDATA = obDCVRDDATA&(~0xe0) | 0x80; //set REG_S5V_REF = 1.65V
- //obDCVARRD=0x1A; //Hsien051226#2
- //obDCVWRDATA=(obDCVRDDATA%(~0x02)); // Still use to 1.8V BandGap
- DCV_TEST_DIS();
- #endif
- obDCVARRD = 0x06;
- obDCVWRDATA = obDCVRDDATA | 0x08;
- obCLKLCDCTRL|=0x20; //Div2 Enable
- obDCVARRD=0x04; //DCV clock
- obDCVWRDATA=0xe1; //147 MHz(136)
- //USB Power Control
- //Renshuo050528#D
- if(VbusCheck(CHECK_VBUS_ON)) //Walace060901#2
- {
- //David051205#2 start // not check register to avoid error detect
- //obDCVARRD=0x14;
- //if(obDCVRDDATA&0x80) //1: 1 BATT
- #if(ONE_BATT_MODE) //use compiler define
- //End
- {
- //obDCVARRD=0x0C; //DCV Addr 0x0C
- //obDCVWRDATA=0x46; //PWM Duty Cycle limited for un-know issue
- //SetDCVPWMCycle(PWM_USB_CYCLE); //Walace060607#4
- SetDcvRegStep(SET_PWM,PWM_USB_CYCLE,SET_PWM_DELAY); //Walace060718#1
- obDCVARRD=0x14;
- obDCVWRDATA=0x01;
- }
- #else //0: 2 BATT or Li-Ion //David051205#2 //use compiler define
- {
- obDCVARRD=0x10; //DCV Addr 0x10
- obDCVWRDATA=0x45; //DCV2 min & max boundary
- obDCVARRD=0x0A; //DCV Addr 0x0A
- obDCVWRDATA=0x56; //Bit 2 e Li-ion0 = 1
- //Hsien051226#3 remove
- // obDCVARRD=0x0B; //DCV Addr 0x0B
- // obDCVWRDATA=0x08; //Bit 3 e must be 1 for DCV1 OFF
- obDCVARRD=0x14;
- obDCVWRDATA=0x01;
- }
- #endif //David051205#2 //use compiler define
- }
- else
- {
- /*============Del by Rojam 2007-03-28 09:22===============
- #if(!FIVEKEY)
- obGPIOCDIR |= 0x04;//TONY060404
- obGPIOCDAT |= 0x04;
- #else
- obGPIOADIR |=0x01;
- obGPIOADAT |= 0x01;
- #endif
- =========================== end Del ========================*/
- }
- //Walace060822#2 for power stable
- CommonDelay(0x1FFF);
- }
- void Init5661Ctrl(void)
- {
- //Mcu Clock
- obCLKMCUDIVF=0x08;
- obCLKMCUCTRL=0xC8; //73(67)/5=14.6(13.4)
- //Sram Clock
- obCLKSRAMDIVF=0x08;
- obCLKSRAMCTRL=0xC8; //73(67)/5=14.6(13.4)
- obSYSSOFTRSTCTRL |=0x02; //RA reset disable
- obSYSSOFTRSTCTRL|=DSP_RST_DIS;//DSP RST DIS
- obPMSIZE=0xB0; //Mem for Dsp
- obDMXSIZE=0x35;
- obDMYSIZE=0x13;
- obPMSIZE =0x20; //set pm size 6k 24bit mode //D1003
- obDMYSIZE=0x23; //set dmy size 4k 24bit and 6k 16bit //D1003
- obSYSSOFTRSTCTRL=SM_RST_DIS|RA_RST_DIS; //reset all, but not SM, RA
- obSYSSOFTRSTCTRL2=0x00;
- obMODSEL1=0x08;
- obMODSEL3=0x08;
- /* //tne 2006/01/17 #2 //Move to InitRtc()
- //enable RTC counter
- obDCVARRD=0x03;
- obDCVWRDATA=obDCVRDDATA|RTC_UPDATE_EN|RTC_INTERPT_1S|RTC_COUNTER_EN|RTC_CLK1K_EN; //D1003
- //enable DCV interrupt
- obSYSINT0MASK |= 0x02;
- //disable RTC & Alarm
- obDCV_INT_MASK =0;
- */
- /* //tne 2006/01/17 #2
- #if(DEBUG_M5638C) //vicky050607#1
- obDCVARRD = 0x07;
- obDCVWRDATA = obDCVRDDATA & (~0x80); //do not use pswitch delay
- #endif
- */
- }
- //tne 2006/01/17 #2
- void InitRtc(void)
- {
- //enable RTC counter
- obDCVARRD=0x03;
- obDCVWRDATA=obDCVRDDATA|RTC_UPDATE_EN|RTC_INTERPT_1S|RTC_COUNTER_EN|RTC_CLK1K_EN; //D1003
- //enable DCV interrupt
- obSYSINT0MASK |= 0x02;
- obDCV_INT_MASK = 0; //disable RTC & Alarm
- }
- void InitSpecialGpio(void)
- {
- #if(COLOR_MODE!=3)
- obGPIOEDAT|=0x40; //Power down FM //David 041106#1
- obGPIOEDIR|=0x40;
- #endif
- POWER_ON_3V3SD(); //Walace060329#1
- // obGPIOHDAT|=0x18; //vicky050517
- // obGPIOHDIR|=0x18; // (Let the direction of CS to be "output")
- NandGpioInit(); //allen060109#3
- }
- void InitSetPmi(void)
- {
- #if(SETTING_FOR_ICE_START|ICE_REAL_MODE)
- gxwAddrPMI=0;//256; //0924
- gxbDrmOptionMode=USB_MASS_STORAGE;
- //gxbDrmOptionMode=USB_MTP;
- #else
- //Re-allocate "gxwAddrPMI" address
- if(!CheckProgRestart()) //not restart by firmware (First time running here after power on)
- {
- gxwAddrPMI=*(XWORD*)(0xC000);
- SetProgRestart(); //set "Restart by firmware" flag
- }
- #endif
- }
- void GetDCVInDWORD(BYTE bDcvAddress,PDWORD pdwValue)
- {
- BYTE bIndex;
- for(bIndex=0;bIndex<4;bIndex++)
- {
- obDCVARRD=(bDcvAddress+3)-bIndex;
- *((PBYTE)pdwValue+bIndex)=obDCVRDDATA;
- }
- }
- //Walace060705#1 start
- #define DCV_RTCCTL_REG 0x03
- #define DCV_RTC0_REG 0x15
- #define DCV_ALARM0_REG 0x19
- void InitCheckValidAlarmTime() large
- {
- DWORD dwAlarmTime;
- DWORD dwNowTimeTemp;
- obDCVARRD=DCV_RTCCTL_REG;
- if(!(obDCVRDDATA&RTC_ALARM_EN))
- return;
- obDCV_INT_MASK &= RTC_COUNTER_DIS;
- GetDCVInDWORD(DCV_ALARM0_REG,&dwAlarmTime);
- do
- {
- #if(IC_5661_TYPE==IC_M5638C_B1B)
- obDCVARRD=DCV_RTCCTL_REG;
- obDCVWRDATA=(obDCVRDDATA&RTC_UPDATE_DIS);
- #endif
- GetDCVInDWORD(DCV_RTC0_REG,&dwNowTimeTemp);
- #if(IC_5661_TYPE==IC_M5638C_B1B)
- obDCVARRD=DCV_RTCCTL_REG;
- obDCVWRDATA=(obDCVRDDATA|RTC_UPDATE_EN);
- obDCVWRDATA=(obDCVRDDATA&RTC_UPDATE_DIS);
- #endif
- GetDCVInDWORD(DCV_RTC0_REG,&gxdwNowTime);
- #if(IC_5661_TYPE==IC_M5638C_B1B)
- obDCVARRD=DCV_RTCCTL_REG;
- obDCVWRDATA=(obDCVRDDATA|RTC_UPDATE_EN);
- #endif
- }while(dwNowTimeTemp!=gxdwNowTime);
- if(dwAlarmTime<gxdwNowTime)
- {
- obDCVARRD=DCV_RTCCTL_REG;
- obDCVWRDATA=(obDCVRDDATA&RTC_ALARM_DIS);
- obDCV_INT_MASK &= RTC_ALARM_DIS;
- }
- obDCV_INT_MASK |= RTC_COUNTER_EN;
- }
- //Walace060705#1 end
- void InitChkSetting(BOOL fForceRestore) large
- {
- BYTE bI;
- //by pass gxdwMaxSector check
- gxdwMaxSector=FORCE_REFLASH_SECTOR; //SAVE_SETTING_NAND_ADD; allen050809
- gxbStorType=STOR_NAND;
- gbSpecialRW=RW_PRIVATE;
- gxdwKeptDataSec=FORCE_REFLASH_SECTOR; //050223 allen
- gfKeptDataModified=0; //050223 allen
- FsRead(SAVE_SETTING_NAND_ADD,1,FS_DATA_0_0);
- //restore default setting
- if( (gxbFsData[SRAM_RECORD_ID1+gxwOffsetDataSec]!=0x56) ||
- (gxbFsData[SRAM_RECORD_ID2+gxwOffsetDataSec]!=0x61) ||
- (fForceRestore) )
- {
- RestoreDefaultSetting();
- NandFinishJob();
- }
- else
- { SetSettingToVariable(); }
- gxdwKeptDataSec=FORCE_REFLASH_SECTOR;
- gbSpecialRW=RW_NORMAL;
- //David060412 start restore RTC from nandflash for assigning variables default value
- obDCV_INT_MASK &= RTC_COUNTER_DIS;
- gxdwNowTime=*(XDWORD *)(gxbFsData+SRAM_LAST_RTC_TIME+gxwOffsetDataSec);
- gxdwSClockOffset=*(XDWORD *)(gxbFsData+SRAM_LAST_CK_OFFSET+gxwOffsetDataSec);
- obDCV_INT_MASK |= RTC_COUNTER_EN;
- //David060412 end
- if( (fForceRestore) || (!SramCheck()) )
- {
- // reset RTC clock and alarm~
- //Renshuo050216#B
- obDCV_INT_MASK &= RTC_COUNTER_DIS; //tne 2006/01/17 #2
- for(bI=0;bI<8;bI++) //reset clock and alarm
- {
- obDCVARRD=bI+0x15;
- obDCVWRDATA=0;
- }
- obDCV_INT_MASK |= RTC_COUNTER_EN; //tne 2006/01/17 #2
- //gxdwNowTime=0;
- //gxdwSClockOffset=0;
- //gxbDrmOptionMode=(USB_MODE_DEFAULT|S_CLOCK_READY_DEFAULT); //tne 2005/04/26
- RestoreSramData();
- #if(INIT_USE_BTY_LOW_FLAG) //Renshuo050216#B remove to here
- bI=0;
- SramWrite(SRAM_BYT_LOW_FLAG,&bI);
- #endif
- #if (USE_FW_CHARGER) //Chris070521#1
- bI=0;
- SramWrite(SRAM_CHARGER_STOP_SADC,&bI);
- #endif
- // set check ID in SRAM
- #if(DEBUG_M5638C) //vicky050608#3
- DCV_TEST_EN();
- obDCVARRD = 0x10;
- obDCVWRDATA = 0x56; //set ID
- obDCVARRD = 0x11;
- obDCVWRDATA = 0x61; //set ID
- DCV_TEST_DIS();
- #else
- bI=0x56;
- SramWrite(SRAM_RECORD_ID1,&bI);
- bI=0x61;
- SramWrite(SRAM_RECORD_ID2,&bI);
- #endif
- }
- else
- {
- SramRead(SRAM_DRM_OPTION_MODE,&gxbDrmOptionMode);
- ReadSClockFromSram();
- }
- InitCheckValidAlarmTime(); //Walace060705#1
- }
- //050811: BootRom set some gpio value, here restore to default.
- void BootGpioControl(void)
- {
- // obGPIOAPUHI=0x00; // BootRom Set GPIOA[4:0] pull-low
- // obGPIOAPUEN=0x1F; //
- obGPIOAPUEN=0x00;
- // obGPIOBPUHI=0xFE; // BootRom Set GPIOB[7:1] pull-high, GPIOB[0] pull-low
- // obGPIOBPUEN=0xFF;
- #if 1
- obGPIOBPUHI|=0xE0;
- obGPIOBPUEN|=0xE0;
- #else
- obGPIOBPUEN=0xE0;
- #endif
- // obGPIOCPUHI=0x00; // BootRom Set
- // obGPIOCPUEN=0x0F;
- #if 1
- obGPIOCPUHI |= 0x02;
- obGPIOCPUEN |= 0x02;
- #else
- obGPIOCPUEN=0x00;
- #endif
- // obGPIOEDAT=0xC0; // BootRom Set //0xFF38,disable IDE
- // obGPIOEDIR=0xC0; //0xFF39,disable IDE
- // obGPIOGPUHI=0x00; // BootRom Set
- // obGPIOGPUEN=0xFF;
- obGPIOGPUEN=0x00;
- // obGPIOHDAT = 0x14; // BootRom Set
- // obGPIOHDIR = 0x1C;
- // obGPIOHPUHI=0x00;
- // obGPIOHPUEN=0xE3;
- obGPIOHPUEN=0x00;
- // obGPIOIPUHI=0x00;
- // obGPIOIPUEN=0xFF;
- obGPIOIPUEN=0x00;
- // obGPIOKPUHI=0x00;
- // obGPIOKPUEN=0xFF;
- // obGPIOLPUHI=0x00;
- // obGPIOLPUEN=0xFF;
- // obGPIOMPUHI=0x00;
- // obGPIOMPUEN=0xFF;
- #if(COST_DOWN_CIRCUIT)
- obDCVARRD=0x14;
- obDCVWRDATA|=0x01;
- obGPIOCDIR|=0x04;
- obGPIOCDAT&=~0x04;
- obGPIOHPUEN|=0x01; // inorder to avoid usb key hold
- obGPIOHPUHI|=0x01;
- obGPIOCPUEN|=0x01; // reset IC need a pull hight register //Alan061026#1
- obGPIOCPUHI|=0x01; //Alan061026#1
- obGPIOBPUEN|=0xC0; // scan key pull high
- obGPIOBPUHI|=0xC0;
- #endif
- }
- bit InitRecInit(void) large
- {
- BYTE btemp1,btemp2; //vicky050118
- BOOL fResult;//vicky050118
- LoadRecInfo(); //vicky050118
- fResult=TRUE; //David051014 start
- if(!FsInfoCheck())
- {
- fResult=FsRecInit();
- }
- if(fResult)
- //if(FsRecInit()) //David051014 end
- {
- //gxdwFirstEmptyClu=2; //if no FsRecInit(), set this var.
- for(btemp2=0;btemp2<REC_DIR_NUM;btemp2++)
- {
- if(!FsFindDir(btemp2,gxbUiDirName[btemp2],1,ATTR_SUB_DIR)) //find record dir or create it. tne 2006/12/07 #3
- {
- gxbLastErrCode=ERR_INIT_REC_DIR_ERR;
- return FALSE;
- }
- }
- for(btemp2=0;btemp2<REC_DIR_NUM;btemp2++)
- {
- for(btemp1=0;btemp1<REC_PREFIX_LEN;btemp1++)
- gxbFileName[btemp1]=gxbUiFileName[btemp2][btemp1];
- if(btemp2==REC_DIR_NUM-1)
- fResult=FsOpenNewFile(btemp2,TRUE);
- else
- fResult=FsOpenNewFile(btemp2,FALSE);
- if(!fResult)
- {
- if(gxbErrCode==ERR_FS_NO_SPACE)
- gxbLastErrCode=ERR_INIT_NO_SPACE_ERR;
- else
- gxbLastErrCode=ERR_INIT_REC_ERR;
- return FALSE;
- }
- }
- }
- else
- {
- gxbLastErrCode=ERR_INIT_REC_ERR;
- return FALSE;
- }
- EA=0;//vicky050118
- obDCV_INT_MASK &= RTC_COUNTER_DIS;
- }
- void InitRunToUsb(BOOL fForceUsb) large
- {
- IE=0;
- #if(ENABLE_SD_FUNCTION)
- SD_POWER_OFF();
- #endif
- gxbDeviceMode=DEV_INIT;
- obSYSSOFTRSTCTRL|=DSP_RST_DIS;//DSP RST DIS
- obPMSIZE|=0x04;//MCU USE ALL
- //obDMXSIZE&=0xF8;
- //obDMYSIZE&=0xF8;
- //#if(DRM_MODE_EN) //Jeff070120#1
- //allen050601 start
- //obDCVARRD=0x0C; //DCV Addr 0x0C
- //obDCVWRDATA=0x46; //PWM Duty Cycle limited for un-know issue
- //SetDCVPWMCycle(PWM_USB_CYCLE); //Walace060607#4
- SetDcvRegStep(SET_PWM,PWM_USB_CYCLE,SET_PWM_DELAY); //Walace060718#1
- obDCVARRD=0x0D; //DCV Addr 0x0d
- obDCVWRDATA=0xf9; //Set 0xf9
- //JeffDebug
- //obGPIOEDRV= 0x01;
- //obGPIOBDRV=0x01;
- //SM Clock
- #if 0 //Walace070205#1
- obCLKSMCTRL=0xD0;
- #endif
- #if(!NEW_FLASH)
- gfNandInit=FALSE;
- #endif
- // gxbNandInitStage=NDINIT_INIT;//Allen060622: have not to re-init nandflash new_nf
- #if(DRM_MODE_EN) //Jeff070120#1
- if(fForceUsb)
- {
- //LoadProgram(PM_USB,NORMAL_CODE); //load USB program
- gxbFsDelId = FSDEL_INIT_FS | FSDEL_GO_USB;
- LoadProgram(PM_INIT_CHK,NORMAL_CODE); //File manipulation then go to specified PM
- }
- else
- {
- if((gxbDrmOptionMode&DRM_OPTION_MTP_MODE)==USB_MASS_STORAGE)
- {
- //LoadProgram(PM_USB,NORMAL_CODE); //load USB program
- gxbFsDelId = FSDEL_INIT_FS | FSDEL_GO_USB;
- LoadProgram(PM_INIT_CHK,NORMAL_CODE); //File manipulation then go to specified PM
- }
- else //if(gxbDrmOptionMode==1)
- {
- //LoadProgram(PM_MTP_INIT,NORMAL_CODE); //load MTP program
- gxbFsDelId = FSDEL_INIT_FS | FSDEL_GO_MTP;
- LoadProgram(PM_INIT_CHK,NORMAL_CODE); //File manipulation then go to specified PM
- }
- }
- #else
- gxbFsDelId = FSDEL_INIT_FS | FSDEL_GO_USB;
- LoadProgram(PM_INIT_CHK,NORMAL_CODE); //File manipulation then go to specified PM
- //LoadProgram(PM_USB,NORMAL_CODE);
- #endif
- }
- /////////////////////// Jeff051221 start
- #if(ONE_BATT_MODE) // 1-BAT
- CBYTE cbVrefTable[] = {0x71,0x61,0x51,0x01,0x11,0x21,0x31,0x41} ; // Jeff051221
- void BandGapCalibrationCheck(void)
- {
- BYTE bVref = 0; // Jeff051221
- BYTE bCalibrationOk = 0; // Jeff051221
- //Hsiang 060209 start
- CKCON &= 0xF8;
- CKCON |= 0x02;
- obCLKMCUDIVF=0x02; //147/4=37
- obCLKSRAMDIVF=obCLKSMDIVF=0x02;
- //Hsiang 060209 end
- //tne 2006/01/17 #2 //by pass gxdwMaxSector check
- gxdwMaxSector=FORCE_REFLASH_SECTOR;
- NandPrivateEn(); //Walace060111#1
- FsRead(0, 1, FS_DATA_0_0);
- bCalibrationOk = gxbFsData[gxwOffsetDataSec+504];
- bVref = gxbFsData[gxwOffsetDataSec+505];
- obCLKMCUDIVF=0x08; //Hsiang 060209 start
- obCLKSRAMDIVF=obCLKSMDIVF=0x08;
- CKCON &= 0xF8;//Hsiang 060209 end
- if( bCalibrationOk!=0xA5 && bCalibrationOk!=0xB6) // first time Init
- {
- // Jeff060125 start
- //obDCVARRD=0x0C; //DCV Addr 0x0C
- //obDCVWRDATA=0x46; //PWM Duty Cycle limited for un-know issue
- //SetDCVPWMCycle(PWM_IC_DEFAULT_CYCLE); //Walace060607#4
- RefleshBattCheck(200); // Jeff_061109 start
- if(BattCheck()<115) // Modulate Duty cycle when Batt are lower than 1.4V
- {SetDcvRegStep(SET_PWM,PWM_CALIBRATION,SET_PWM_DELAY);} //Walace060718#1 // Jeff_061109 end
- // Jeff060125 end
- //tne 2006/01/17 #2
- SetSysSpeed(SYS_SPEED_UP_CALIBRATION);
- gxbLcdCounter=0;
- while(gxbLcdCounter<15);
- bVref = SARADC_Calibration();
- SetDcvRegStep(SET_PWM,PWM_NORMAL_CYCLE,SET_PWM_DELAY); //Walace060720#1 //Walace060718#1
- }
- else{;}
- DCV_TEST_EN();
- if((bVref<=5)&&(bVref>=1))// Jeff_060314 #2 //hsiang 060324
- {
- obDCVARRD = 0x19 ;
- obDCVWRDATA = cbVrefTable[bVref] ;
- gxbFsData[504]=0xA5;
- gxbFsData[505]=bVref;
- }
- else
- {
- obDCVARRD = 0x19 ;
- obDCVWRDATA = cbVrefTable[3];
- gxbFsData[504]=0xB6;
- gxbFsData[505]=0x03;
- }
- DCV_TEST_DIS();
- obCLKMCUDIVF=0x08; //test speed down
- obCLKSRAMDIVF=obCLKSMDIVF=0x08; //test speed down
- //gxbLcdCounter=0; //hsiang 060511
- //while(gxbLcdCounter<15); //hsiang 060511
- FsWrite(0, 1 , FS_DATA_0_0);
- NandFinishJob(); //Walace060111#1
- NandPrivateDis();
- //Walace060607#4 start
- //SetDCVPWMCycle(PWM_NORMAL_CYCLE); //Walace060720#1
- // Jeff060125 start
- /* obDCVARRD=0x0C; //DCV Addr 0x0C
- if(obDCVRDDATA==0x46)
- {
- obDCVARRD=0x0C; //DCV Addr 0x0C
- obDCVWRDATA=0x26; //PWM Duty Cycle limited for un-know issue
- gxbLcdCounter=0;
- while(gxbLcdCounter<8); // Delay
- obDCVARRD=0x0C; //DCV Addr 0x0C
- obDCVWRDATA=0x06; //PWM Duty Cycle limited for un-know issue
- }
- else {;} */
- // Jeff060125 end
- //Walace060607#4 end
- }
- #endif // #if(ONE_BATT_MODE) // 1-BAT
- /////////////////////////////////////// Jeff051221 end
- #if(USE_VDDIO_CALIBRATION) //hsiang VddioCali start
- #if(_PM_INIT_)
- void InitialVddioCalibration(void)
- {
- XBYTE xbLevel = 0;
- XBYTE xbCaliFinish = 0;
- NandInit();
- NandPrivateEn();
- FsRead(0, 1, FS_DATA_0_0);
- xbCaliFinish = gxbFsData[gxwOffsetDataSec+490];
- xbLevel = gxbFsData[gxwOffsetDataSec+491];
- if( xbCaliFinish!=0x5A && xbCaliFinish!=0x6B) // first time Init
- {
- xbLevel = Vddio_Calibration();
- if((xbLevel==0x42)||(xbLevel==0x52)||(xbLevel==0x62)||(xbLevel==0x72))
- {
- obDCVARRD = 0x0A ;
- obDCVWRDATA = xbLevel ;
- gxbFsData[490]=0x5A;
- gxbFsData[491]=xbLevel;
- }
- else
- {
- obDCVARRD=0x0A;
- obDCVWRDATA=0x52;
- gxbFsData[490]=0x6B;
- gxbFsData[491]=0x52;
- }
- //test speed down
- obCLKMCUDIVF=0x08;
- obCLKSRAMDIVF=obCLKSMDIVF=0x08;
- FsWrite(0, 1 , FS_DATA_0_0);
- }
- else
- {
- obDCVARRD = 0x0A ;
- obDCVWRDATA = xbLevel ;
- }
- NandFinishJob();
- NandPrivateDis();
- }
- #endif
- #endif //hsiang VddioCali end
- #if 0 //Walace060108 debug //Chris change from #if(0) ->#if 0 for Source Insight view
- BOOL StoreSarADCValue(void)
- {
- BYTE bBtyTimeOut,bBtyNum,bStoreCount;
- WORD wCounter;
- BANDGAP_3V3_SWITCH();
- //OledInit();
- //OLED_CLEAR_ALL();
- gbTimeTick0=0;
- gxbLcdCounter=0;
- while(gxbLcdCounter<10){;}
- for(bStoreCount=0;bStoreCount<4;bStoreCount++)
- {
- obCLKMCUDIVF=0x01; //test speed up
- obCLKSRAMDIVF=obCLKSMDIVF=0x01; //test speed up
- gxbLcdCounter=0;
- while(gxbLcdCounter<3){;}
- for(wCounter=0;wCounter<STORE_COUNT;wCounter++)
- {
- bBtyTimeOut=0xFF;
- obCLKI2CSCTRL = 4; // Enable I2Cs Clock
- obLRRSOC |= 0x01;
- obLRRSOC &= 0xFE; // Give bit1 High->Low
- while((bBtyNum=obDCVBATLRR)>=0x80) // wait bit 7 go to 0
- {
- bBtyTimeOut--;
- if(bBtyTimeOut==0)
- {
- //bBtyNum=BATTERY_LV_ERR;
- bBtyNum=0xFF;
- break;
- }
- }
- obCLKI2CSCTRL = 0; // Disable I2Cs Clock
- gxbDebugData[wCounter]=bBtyNum;
- //OledShowNumber(bBtyNum,WORD0,LINE0,3);
- //OledShowNumber(bCounter,WORD0,LINE1,3);
- //OledShowNumber(gbTimeTick0,WORD0,LINE2,3);
- //gxbLcdCounter=0;
- //while(gxbLcdCounter<1){;}
- }
- if(bStoreCount==0)
- {
- NandInit();
- FsInit();
- //OledShowNumber(555,WORD3,LINE1,3);
- }
- /*
- gfKeptDataModified=0; //050223 allen
- gxbStorage=STOR_NAND;
- gxdwMaxSector=FORCE_REFLASH_SECTOR; //SAVE_SETTING_NAND_ADD; allen050809
- gxbStorType=STOR_NAND;
- gbSpecialRW=RW_PRIVATE;
- gxdwKeptDataSec=FORCE_REFLASH_SECTOR; //050223 allen
- */
- NandPrivateEn();
- FsRead(3+bStoreCount, 1, FS_DATA_0_0);
- //OledShowNumber(666,WORD3,LINE1,3);
- for(wCounter=0;wCounter<STORE_COUNT;wCounter++)
- gxbFsData[gxwOffsetDataSec+wCounter]=gxbDebugData[wCounter];
- //OledShowNumber(777,WORD3,LINE1,3);
- FsWrite(3+bStoreCount, 1 , FS_DATA_0_0);
- NandFinishJob();
- NandPrivateDis();
- //OledShowNumber(888,WORD3,LINE1,3);
- }
- obCLKMCUDIVF=0x08; //test speed down
- obCLKSRAMDIVF=obCLKSMDIVF=0x08; //test speed down
- return FALSE;
- }
- #endif
- //Ren060125 //Walace060125 //Walace060326#1 //Walace060730#1
- #if(DEBUG_M5638C)
- void BrownOutInit(void)
- {
- BYTE bTmp ;
- DCV_TEST_EN();
- /*
- obDCVARRD = 0x1C;
- obDCVWRDATA = obDCVRDDATA & (~0x18);//power on brown-out ckt, use batt as source
- obDCVARRD = 0x1B;
- //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x04; //0.95V brown out
- #if(ONE_BATT_MODE)
- obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x00; // 0.85V brown out //Walace060504#3
- #else
- obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x1F; // 1.75 // 1.71 // 2.65v brown out //
- //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x1D; //1.7 // 1.66 //3.4 // 2.60
- //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x1B; //1.65 // 1.60 //3.3 // 2.53
- //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x19; // 1.6 //1.55 // 2.48
- //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x17; //1.55 //1.50 // 2.42
- //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x15; //1.50 // 1.44 // 2.34
- #endif
- */
- #if(ONE_BATT_MODE)
- obDCVARRD = 0x1C;
- obDCVWRDATA = obDCVRDDATA & (~0x18);//power on brown-out ckt, use batt as source
- obDCVARRD = 0x1B;
- //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x00; // 0.85V brown out //Walace060504#3
- obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x01; // 0.875V //tne 2006/11/29 #1
- #else
- // In Li-ion system,be care of that detemine the Brownout Voltage Level according the
- // battery work voltage(WV)
- #if(IC_5661_TYPE==IC_M5638C_B1B)
- obDCVARRD = 0x1C;
- obDCVWRDATA = obDCVRDDATA & (~0x08); //Power on brown-out ckt, use 1/2 batt as source
- obDCVARRD = 0x1B;
- obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x00; //Brownout 2.65v //Walace060504#3
- #else
- obDCVARRD = 0x1C;
- obDCVWRDATA = obDCVRDDATA & (~0x08);//Power on brown-out ckt, use 1/2 batt as source
- obDCVARRD = 0x1B;
- obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x03;//Brownout 2.80v //Walace060504#3
- // Reg value : Brwonout Register Voltage = (Brownout Battery Voltage - 0.95)/2
- // 0.95 diode voltage drop
- // 0x00 : 0.850 = (2.65-0.95)/2
- // 0x01 : 0.875 = (2.70-0.95)/2
- // 0x02 : 0.900 = (2.75-0.95)/2
- // 0x03 : 0.925 = (2.80-0.95)/2
- // 0x04 : 0.950 = (2.85-0.95)/2
- // 0x05 : 0.975 = (2.90-0.95)/2
- // 0x06 : 1.000 = (2.95-0.95)/2
- // 0x07 : 1.025 = (3.00-0.95)/2
- // 0x08 : 1.050 = (3.05-0.95)/2
- // 0x09 : 1.075 = (3.10-0.95)/2
- // 0x0A : 1.100 = (3.15-0.95)/2
- // 0x0B : 1.125 = (3.20-0.95)/2
- #endif
- #endif
- DCV_TEST_DIS();
- bTmp = obDCV_INT_FLAG; //clear flag
- obDCV_INT_MASK |=0x02; //enable batt brown out
- obSYSINT0MASK |= 0x02; //enable DCV int
- }
- #endif
- //Ted 0203 start
- #define DIRECTLY_ASSIGN_INIT_ADDR 0 //current MPtool set 0
- void RestoreBlock0(void) large
- {
- BYTE bI;
- WORD TempFinalAddr, CurrentAddr;
- WORD TempRedu;
- BYTE gxbTempRedu[4];
- SetSysSpeed(SYS_SPEED_NORMAL);
- NandReadPhySec(gxwAddrPMI,1,SRAM_DATA_ADDR);
- *((BYTE*)&TempFinalAddr) =gxbFsData[511];
- *((BYTE*)&TempFinalAddr+1) = gxbFsData[510];
- EraseBlock0();
- ////////////////////debug code/////////////////
- /*CurrentAddr=0;
- while(1)
- {
- //check the BLOCK0 for debug
- NandReadPhySec(CurrentAddr++,1,SRAM_DATA_ADDR);
- *((BYTE*)&TempRedu) =obSMREADREDU6; //Hi
- *((BYTE*)&TempRedu+1) = obSMREADREDU7; //Lo
- TempRedu>>=1;
- if(CurrentAddr>TempFinalAddr)
- break;
- }*/
- ///////////////////////////////////////
- CurrentAddr=gxwAddrPMI;
- while(1)
- {
- for(bI=0; bI<4; bI++)
- {
- NandReadPhySec(CurrentAddr+bI, 1,((WORD)bI<<9) + SRAM_DATA_ADDR);
- *((BYTE*)&TempRedu) =obSMREADREDU6; //Hi
- *((BYTE*)&TempRedu+1) = obSMREADREDU7; //Lo
- TempRedu>>=1;
- gxbTempRedu[bI]=*((BYTE *)&TempRedu +1 );
- }
- #if(DIRECTLY_ASSIGN_INIT_ADDR)
- if(CurrentAddr==gxwAddrPMI)
- {
- gxbFsData[4]=0x02;
- gxbFsData[5]=0x00;
- }
- #endif
- UpgradeBlock0(CurrentAddr-gxwAddrPMI, gxbTempRedu);
- CurrentAddr = CurrentAddr + 4;
- if(CurrentAddr-gxwAddrPMI>TempFinalAddr)
- break;
- }
- //gxwAddrPMI=0;
- /////////////////////////debug code//////////////////////
- /*CurrentAddr=0;
- while(1)
- {
- //check the BLOCK0 for debug
- NandReadPhySec(CurrentAddr++,1,SRAM_DATA_ADDR);
- *((BYTE*)&TempRedu) =obSMREADREDU6; //Hi
- *((BYTE*)&TempRedu+1) = obSMREADREDU7; //Lo
- TempRedu>>=1;
- if(CurrentAddr>TempFinalAddr)
- break;
- }
- LcdBackLightControl(LCD_BACKLIGHT_ON);
- while(1)
- {};
- */
- //////////////////////////////////////////////////////
- }
- //Ted 0203 end
- #if(1) //Patrick061016 //tne 2006/11/29 #1
- void LowBatteryDetection(BYTE bTestTime)
- {
- //USB PHY test // 060803 Renshuo
- obMODSEL3 |= 0x40;
- gxbLcdCounter=0;
- gxbBtyLowNum=0;
- while(gxbLcdCounter<bTestTime)
- {
- if( (BattCheck()<=BATTERY_LV_LOW) && (gxbBtyLowNum<0xFF) )
- gxbBtyLowNum++;
- }
- obMODSEL3 &= (~0x40);
- }
- #endif
- // Jeff_061109 start
- #if(_PM_INIT_)
- void RefleshBattCheck(WORD wCounter) large
- {
- while(wCounter--) {BattCheck();}
- }
- // Jeff_061124 mark
- /*void StarUpPWMmodulation(void) large
- {
- RefleshBattCheck(0x5555);
- if(BattCheck()>90) // Do not add Loading when batt is lower than 1.2V
- {obMODSEL3|=0x40;} // Enable USB clock to Add more loading
- CommonDelay(0x5555);
- obDCVARRD=0x0C; //DCV Addr 0x0C
- obDCVWRDATA=0x36;
- obMODSEL3&=(~0x40); // Disable USB clock to Save Power
- CommonDelay(0x5555); // suitable delay time
- obDCVARRD=0x0C; //DCV Addr 0x0C
- obDCVWRDATA=0x26;
- CommonDelay(0xFFF); // suitable delay time
- obDCVARRD=0x0C; //DCV Addr 0x0C
- obDCVWRDATA=0x16;
- CommonDelay(0xFFF); // suitable delay time
- obDCVARRD=0x0C; //DCV Addr 0x0C
- obDCVWRDATA=0x06;
- CommonDelay(0xFFF);
- if(BattCheck()>90) // Do not add Loading when batt is lower than 1.2V
- {obMODSEL3|=0x40; } // Enable USB clock to Add more loading
- obDCVARRD=0x06;
- obDCVWRDATA=obDCVRDDATA&(~0x08); // over voltage detect disable
- CommonDelay(0xFFF);
- //if(BattCheck()>115) // Do not modulate VDDD when batt is higher than 1.4V
- {SetDcvRegStep(SET_VDD,0x05,SET_VDD_DELAY); } // Set VDDA to 1.65V
- //else
- //{SetDcvRegStep(SET_VDD,0x00,SET_VDD_DELAY); } // Set VDDD to 1.20V
- obMODSEL3&=(~0x40); // Disable USB clock to Save Power
- obDCVARRD=0x06;
- obDCVWRDATA=obDCVRDDATA|0x08; // over voltage detect enable
- }*/
- // Jeff_061124 add
- void StarUpPWMmodulation(void) large
- {
- obGPIODDAT|=0x01;
- obGPIODDIR|=0x01;
- //4 1v2 Modulation
- SetDcvRegStep(SET_VDD,0x70,0xFFF); // Set VDDD to 1.20V // Jeff_061031
- //4PWM Modulation
- RefleshBattCheck(0x4000);
- if(BattCheck()>90) // Do not add Loading when batt is lower than 1.2V
- {
- obMODSEL3|=0x40; // Enable USB clock to Add more loading
- CommonDelay(0xFFF);
- SetDcvRegStep(0x0A,0x72,SET_VDD_DELAY); // Modulate 3v3 to avoid drop
- }
- obGPIODDAT&=~0x01;
- obMODSEL3&=(~0x40); // Disable USB clock to Save Power
- SetDcvRegStep(SET_PWM,0x36,0x6000); // Modulate PWM to 0x36
- CommonDelay(0x5000);
- obGPIODDAT|=0x01;
- //CommonDelay(0x5000);
- //CommonDelay(0xFFF);
- //4 1V8 Modulation
- obDCVARRD=0x06;
- obDCVWRDATA=obDCVRDDATA&(~0x08); // over voltage detect disable
- SetDcvRegStep(SET_VDD,0x00,0xFFF); // Set VDDA to 1.65V // Jeff_061031
- CommonDelay(0x5000);
- //4PWM Modulation
- SetDcvRegStep(SET_PWM,0x26,0x5000); // Modulate PWM to 0x26
- CommonDelay(0xFFF);
- //4 3V3 Modulation
- SetDcvRegStep(0x0A,0x52,0xFFF);
- CommonDelay(0xFFF);
- //4PWM Modulation
- obGPIODDAT&=~0x01;
- SetDcvRegStep(SET_PWM,PWM_NORMAL_CYCLE,SET_PWM_DELAY); // Modulate PWM to 0x06
- obMODSEL3&=(~0x40); // Disable USB clock to Save Power
- }
- #endif
- // Jeff_061109 end
English
