Initial.c
上传用户:hjhsjcl
上传日期:2020-09-25
资源大小:11378k
文件大小:32k
源码类别:

压缩解压

开发平台:

C++ Builder

  1. /*==========================================================================
  2. Copyright (c) 2004 ALi Corporation. All Rights Reserved
  3. File: Initial.c
  4. content: Default Program Module load by Boot Rom.
  5.  Process initialization of the device.
  6. History: Created by David Chiu 2004/3/19
  7. ==========================================================================*/
  8. //#define _REG_5661_H_
  9. #define _INITIAL_H_
  10. #include <DP8051XP.H>
  11. #include "TypeDef.h"
  12. #include "Const.h"
  13. #include "Reg5661.h"
  14. #include "Common.h"
  15. #include "Initial.h"
  16. #include "Key.h"
  17. #if(_C_MODE_)
  18. #include "OledString.h"
  19. #include "OLED.h"
  20. #include "IconDefine.h"
  21. #else
  22. #include "Lcd.h"
  23. #endif
  24. #include "FileSystem.h"
  25. #include "NandFlash.h"
  26. #include "Sd.h"
  27. #include "DDM.h"
  28. #include "SaveSetting.h"
  29. #include "ExtCodec.h"
  30. #include "IntCodec.h"
  31. #if(ENABLE_HOST_FUNCTION)
  32. #include "UsbHostCommon.h"
  33. #include "UsbHost.h"
  34. #endif
  35. /* //Walace060108 debug
  36. #define STORE_COUNT 256
  37. XBYTE gxbDebugData[STORE_COUNT];
  38. */
  39. void Init8051(void)
  40. {
  41.   CKCON &= 0xF8;
  42. CKCON |= 0x07; // STRECH = 111
  43. obREADYENL=SM_EN|USB_EN|SD_EN;
  44. obREADYENH=(SRAMRA_EN|RA_EN|SYSREG_EN)|0xF0;
  45. CKCON &= 0xF8; // STRECH = 000
  46. //CKCON &= 0xF9; // STRECH = 001 //David 050406
  47. IE = 0;
  48. EA = 1;
  49. IP = 0;
  50. PX0 = 1;
  51. TMOD = 0x10;
  52. TCON = 0;
  53. T2CON= 0;
  54. TA=0xAA;
  55. TA=0x55;
  56. WDCON =0;
  57. //Reset clock
  58. //Renshuo050528#D
  59. // obCLKMCUCTRL=0xD0; //MCU clock use XTALI 12MHz
  60. }
  61. void InitDisableClk(void)
  62. {
  63. ClkSdDis(); //Disable SD clk
  64. ClkSmDis(); //Disable SM clk
  65. ClkMsDis(); //Disable MS clk
  66. ClkDspDis(); //Disable DSP clk
  67. ClkCdcDis(); //Disable CODEC clk
  68. ClkI2S1Dis(); //Disable I2S_1 clk
  69. ClkI2S2Dis(); //Disable I2S_2 clk
  70. ClkI2CmDis(); //Disable I2C_M clk
  71. ClkLcdDis(); //Disable LCD clk
  72. }
  73. void InitPowerCtrl(void)
  74. {
  75. obSYSSOFTRSTCTRL |= 0x80; //Disable USB Reset
  76. obUSBOTGCTRL |= 0x02; //DPLL power down
  77. obMODSEL3=0x00; //0xFF09,not Bypass RA,RA path=SRAM Ra
  78. #if(DEBUG_M5638C) //Renshuo050528#D 
  79. //delay a while before DCV R/W
  80. EnableTimer0();
  81. gxbLcdCounter=0;
  82. while(gxbLcdCounter<6) {}
  83. DCV_TEST_EN();
  84. obDCVARRD=0x19;
  85. //obDCVWRDATA=0x01; //quick start
  86. obDCVWRDATA = obDCVRDDATA | 0x01; //quick start enable //Alan 060727#1
  87. DCV_TEST_DIS();
  88. #endif
  89. obDCVARRD=0x03; //DCV Addr 0x03
  90. obDCVWRDATA=(obDCVRDDATA&0xEF); //Set bit4=0
  91. obDCVARRD=0x08; //DCV Addr 0x08
  92. obDCVWRDATA=0x88; //Set 0x88 e LSADC reference source
  93. //David051205#2 start //not check register to avoid error detect
  94. //obDCVARRD=0x14;
  95. //if(obDCVRDDATA&0x80) //1: 1 BATT
  96. #if(ONE_BATT_MODE) //use compiler define
  97. //End 
  98. {
  99. #if(DEBUG_M5638C)  //vicky050608#3
  100.   //Renshuo050528#D //vicky050607#1     
  101. //obDCVARRD=0x09;
  102. // Jeff_061109 start
  103. #if(CIRCUIT_2_PW) //David050719
  104. //obDCVWRDATA=0x00; //2 power solution
  105. //SetDcvRegStep(SET_VDD,0x00,SET_VDD_DELAY); //Walace060718#1    // Jeff_061109
  106. obDCVARRD=0x0C; //DCV Addr 0x0C
  107. if(obDCVRDDATA==0x46)
  108. {
  109. StarUpPWMmodulation();  // Jeff_061031
  110. }  
  111. else
  112. {
  113. //SetDcvRegStep(SET_VDD,0x05,SET_VDD_DELAY);  // Set VDDA to 1.65V  // Jeff_061124 mark
  114. SetDcvRegStep(SET_VDD,0x00,SET_VDD_DELAY);  // Set VDDA to 1.65V    // Jeff_061124 
  115. //obDCVARRD=0x0C; //DCV Addr 0x0C // Jeff_061109
  116. //obDCVWRDATA=0x06;
  117. }
  118. #else
  119. //obDCVWRDATA=0x75; //3 power solution, Power saving for VDD_D
  120. SetDcvRegStep(SET_VDD,0x75,SET_VDD_DELAY); //Walace060718#1
  121. #endif
  122. obDCVARRD=0x0A; //DCV Addr 0x0A
  123. obDCVWRDATA=0x52; //Power saving for VDD_IO
  124. //obDCVARRD=0x0C; //DCV Addr 0x0C
  125. //obDCVWRDATA=0x06; //PWM Duty Cycle limited for un-know issue
  126. //SetDCVPWMCycle(PWM_NORMAL_CYCLE); //Walace060607#4
  127. SetDcvRegStep(SET_PWM,PWM_NORMAL_CYCLE,SET_PWM_DELAY); //Walace060718#1
  128. // Jeff_061109 end
  129. obDCVARRD=0x0D; //DCV Addr 0x0D
  130. obDCVWRDATA=0xF9; //For voltage drop recovering time 
  131. //Joseph, for SAR ADC
  132. obDCVARRD   = 0x00 ;
  133. obDCVWRDATA = 0x77 ; //Vtop: 1.00V
  134. obDCVARRD   = 0x08 ;
  135. obDCVWRDATA = 0x88 ;
  136. DCV_TEST_EN(); //vicky050607#1
  137. obDCVARRD   = 0x1B ;
  138. obDCVWRDATA = 0x0A ; //for one battery
  139. obDCVARRD   = 0x1C ;
  140. obDCVWRDATA = 0xA4 ;
  141. DCV_TEST_DIS();
  142. #else     
  143. //obDCVARRD=0x09; //Walace060718#1 //DCV Addr 0x09
  144. #if(CIRCUIT_2_PW) //David050719
  145. //obDCVWRDATA=0x55; //2 power solution  //Walace060718#1
  146. SetDcvRegStep(SET_VDD,0x55,SET_VDD_DELAY);
  147. #else
  148. //obDCVWRDATA=0x77; //3 power solution, Power saving for VDD_D
  149. SetDcvRegStep(SET_VDD,0x77,SET_VDD_DELAY);
  150. #endif
  151. obDCVARRD=0x0A; //DCV Addr 0x0A
  152. #if(CIRCUIT_2_PW) //David050719
  153. obDCVWRDATA=0x52; //2 power solution
  154. #else
  155. obDCVWRDATA=0x72; //3 power solution,Power saving for VDD_IO
  156. #endif
  157. //David050719, set after program load , suggest by Shiaw-Yi 
  158. //obDCVARRD=0x0C; //DCV Addr 0x0C
  159. //obDCVWRDATA=0x25; //PWM Duty Cycle limited for un-know issue
  160. obDCVARRD=0x0D; //DCV Addr 0x0D
  161. obDCVWRDATA=0xF9; //For voltage drop recovering time 
  162. #endif
  163. }
  164. #else //0: 2 BATT or Li-Ion //David051205#2 //use compiler define
  165. {
  166. obDCVARRD=0x0A; //DCV Addr 0x0A
  167. obDCVWRDATA=0x56; //Bit 2 e Li-ion0 = 1 
  168. ////Hsien051226#3 remove
  169. // obDCVARRD=0x0B; //DCV Addr 0x0B
  170. // obDCVWRDATA=0x08; //Bit 3 e must be 1 for DCV1 OFF 
  171. obDCVARRD=0x10; //DCV Addr 0x10
  172. obDCVWRDATA=0x45; //DCV2 min & max boundary
  173. obDCVARRD=0x11; //DCV Addr 0x0d
  174. obDCVWRDATA=0xF9; //freq. response
  175.    
  176. //Walace051212#1 power save 1v8->1v6
  177. //obDCVARRD=0x09; //DCV Addr 0x09 //David050719
  178. //obDCVWRDATA=0x00; //2 power solution
  179.     SetDcvRegStep(SET_VDD,0x00,SET_VDD_DELAY);  //Walace060718#1
  180.    
  181. #if(DEBUG_M5638C)  //Renshuo050528#D //vicky050607#1
  182. //Joseph, for SAR ADC
  183. #if(BATT_CHECK_AUXIN)    //Walace060613#1  //Hsin Hsien  apply
  184. obDCVARRD   = 0x00 ;
  185. obDCVWRDATA = 0xC7 ; //BAT_REF:0.725 //AUX_REF:0.675 
  186. obDCVARRD   = 0x08 ;
  187. obDCVWRDATA = 0xCA ; //
  188. #else
  189. obDCVARRD   = 0x08 ;
  190. obDCVWRDATA = 0xC8 ;
  191. obDCVARRD   = 0x00 ;
  192. obDCVWRDATA = 0xF7 ; //Vtop: 1.05V
  193. #endif
  194. DCV_TEST_EN(); //vicky050607#1
  195. obDCVARRD   = 0x1B ;
  196. obDCVWRDATA = 0x15 ;
  197. //obDCVWRDATA = 0x0A ; //for two battery
  198. obDCVARRD   = 0x1C ;
  199. obDCVWRDATA = 0x94 ;
  200. //obDCVWRDATA = 0xB4 ; //for two battery
  201. DCV_TEST_DIS();
  202.     #endif
  203. }
  204. #endif //David051205#2 // use compiler define
  205. #if(DEBUG_M5638C)  //Renshuo050528#D //vicky050607#1
  206. // DCV_TEST_EN();
  207.      //   obDCVARRD=0x1A;
  208. //       obDCVWRDATA=(obDCVRDDATA&(~0x01)); // 3.3V BandGap Power On
  209. //Walace060614#2  BandGap start
  210. #if(ONE_BATT_MODE)
  211. BANDGAP_1V8_SWITCH();
  212.        CommonDelay(BANDGAP_SWITCH_DELAY);
  213. BANDGAP_3V3_POWER_OFF();
  214. #else
  215. BANDGAP_3V3_POWER_ON();
  216.        CommonDelay(BANDGAP_SWITCH_DELAY);
  217. BANDGAP_3V3_SWITCH();
  218. #endif
  219. //Walace060614#2  BandGap end
  220. DCV_TEST_EN();
  221. obDCVARRD = 0x1B;
  222. obDCVWRDATA = obDCVRDDATA&(~0xe0) | 0x80;  //set REG_S5V_REF = 1.65V
  223. //obDCVARRD=0x1A; //Hsien051226#2
  224. //obDCVWRDATA=(obDCVRDDATA%(~0x02)); // Still use to 1.8V BandGap
  225. DCV_TEST_DIS();
  226. #endif
  227. obDCVARRD = 0x06;
  228. obDCVWRDATA = obDCVRDDATA | 0x08;
  229. obCLKLCDCTRL|=0x20; //Div2 Enable
  230. obDCVARRD=0x04; //DCV clock
  231. obDCVWRDATA=0xe1; //147 MHz(136)
  232. //USB Power Control
  233. //Renshuo050528#D
  234. if(VbusCheck(CHECK_VBUS_ON)) //Walace060901#2
  235. {
  236. //David051205#2 start // not check register to avoid error detect
  237. //obDCVARRD=0x14;
  238. //if(obDCVRDDATA&0x80) //1: 1 BATT
  239. #if(ONE_BATT_MODE) //use compiler define
  240. //End
  241. {
  242. //obDCVARRD=0x0C; //DCV Addr 0x0C
  243. //obDCVWRDATA=0x46; //PWM Duty Cycle limited for un-know issue
  244. //SetDCVPWMCycle(PWM_USB_CYCLE); //Walace060607#4
  245. SetDcvRegStep(SET_PWM,PWM_USB_CYCLE,SET_PWM_DELAY); //Walace060718#1
  246. obDCVARRD=0x14;
  247. obDCVWRDATA=0x01;
  248. }
  249. #else //0: 2 BATT or Li-Ion //David051205#2 //use compiler define
  250. {
  251. obDCVARRD=0x10; //DCV Addr 0x10
  252. obDCVWRDATA=0x45; //DCV2 min & max boundary
  253. obDCVARRD=0x0A; //DCV Addr 0x0A
  254. obDCVWRDATA=0x56; //Bit 2 e Li-ion0 = 1 
  255. //Hsien051226#3 remove
  256. // obDCVARRD=0x0B; //DCV Addr 0x0B
  257. // obDCVWRDATA=0x08; //Bit 3 e must be 1 for DCV1 OFF 
  258. obDCVARRD=0x14;
  259. obDCVWRDATA=0x01;
  260. }
  261. #endif //David051205#2  //use compiler define
  262. }
  263. else
  264. {
  265. /*============Del by Rojam  2007-03-28 09:22===============
  266. #if(!FIVEKEY)
  267.       obGPIOCDIR |= 0x04;//TONY060404
  268. obGPIOCDAT |= 0x04;
  269. #else
  270. obGPIOADIR  |=0x01;
  271. obGPIOADAT  |= 0x01;
  272. #endif
  273. =========================== end Del ========================*/
  274. }
  275.  //Walace060822#2 for power stable
  276. CommonDelay(0x1FFF);
  277. }
  278. void Init5661Ctrl(void)
  279. {
  280. //Mcu Clock
  281. obCLKMCUDIVF=0x08;
  282. obCLKMCUCTRL=0xC8; //73(67)/5=14.6(13.4)
  283. //Sram Clock
  284. obCLKSRAMDIVF=0x08;
  285. obCLKSRAMCTRL=0xC8; //73(67)/5=14.6(13.4)
  286. obSYSSOFTRSTCTRL |=0x02; //RA reset disable
  287. obSYSSOFTRSTCTRL|=DSP_RST_DIS;//DSP RST DIS
  288. obPMSIZE=0xB0; //Mem for Dsp
  289. obDMXSIZE=0x35;
  290. obDMYSIZE=0x13;
  291. obPMSIZE =0x20; //set pm size 6k 24bit mode //D1003
  292. obDMYSIZE=0x23; //set dmy size 4k 24bit and 6k 16bit //D1003
  293. obSYSSOFTRSTCTRL=SM_RST_DIS|RA_RST_DIS; //reset all, but not SM, RA
  294. obSYSSOFTRSTCTRL2=0x00;
  295. obMODSEL1=0x08;
  296. obMODSEL3=0x08;
  297. /* //tne 2006/01/17 #2 //Move to InitRtc()
  298. //enable RTC counter
  299. obDCVARRD=0x03;
  300. obDCVWRDATA=obDCVRDDATA|RTC_UPDATE_EN|RTC_INTERPT_1S|RTC_COUNTER_EN|RTC_CLK1K_EN; //D1003
  301. //enable DCV interrupt
  302. obSYSINT0MASK |= 0x02;
  303. //disable RTC & Alarm
  304. obDCV_INT_MASK =0;
  305. */
  306. /* //tne 2006/01/17 #2
  307. #if(DEBUG_M5638C) //vicky050607#1
  308. obDCVARRD = 0x07;
  309. obDCVWRDATA = obDCVRDDATA & (~0x80); //do not use pswitch delay
  310. #endif
  311. */
  312. }
  313. //tne 2006/01/17 #2
  314. void InitRtc(void)
  315. {
  316. //enable RTC counter
  317. obDCVARRD=0x03;
  318. obDCVWRDATA=obDCVRDDATA|RTC_UPDATE_EN|RTC_INTERPT_1S|RTC_COUNTER_EN|RTC_CLK1K_EN; //D1003
  319. //enable DCV interrupt
  320. obSYSINT0MASK |= 0x02;
  321. obDCV_INT_MASK = 0; //disable RTC & Alarm
  322. }
  323. void InitSpecialGpio(void)
  324. {
  325. #if(COLOR_MODE!=3)
  326. obGPIOEDAT|=0x40; //Power down FM //David 041106#1
  327. obGPIOEDIR|=0x40;
  328. #endif
  329. POWER_ON_3V3SD(); //Walace060329#1       
  330. // obGPIOHDAT|=0x18; //vicky050517
  331. // obGPIOHDIR|=0x18; // (Let the direction of  CS to be "output")
  332. NandGpioInit(); //allen060109#3
  333. }
  334. void InitSetPmi(void)
  335. {
  336. #if(SETTING_FOR_ICE_START|ICE_REAL_MODE)
  337. gxwAddrPMI=0;//256; //0924
  338. gxbDrmOptionMode=USB_MASS_STORAGE;
  339. //gxbDrmOptionMode=USB_MTP;
  340. #else
  341. //Re-allocate "gxwAddrPMI" address
  342. if(!CheckProgRestart()) //not restart by firmware (First time running here after power on)
  343. {
  344. gxwAddrPMI=*(XWORD*)(0xC000);
  345. SetProgRestart(); //set "Restart by firmware" flag
  346. }
  347. #endif
  348. }
  349. void GetDCVInDWORD(BYTE bDcvAddress,PDWORD pdwValue)
  350. {
  351. BYTE bIndex;
  352. for(bIndex=0;bIndex<4;bIndex++)
  353. {
  354. obDCVARRD=(bDcvAddress+3)-bIndex;
  355. *((PBYTE)pdwValue+bIndex)=obDCVRDDATA;
  356. }
  357. }
  358. //Walace060705#1 start
  359. #define DCV_RTCCTL_REG 0x03
  360. #define DCV_RTC0_REG 0x15
  361. #define DCV_ALARM0_REG 0x19
  362. void InitCheckValidAlarmTime() large
  363. {
  364. DWORD dwAlarmTime;
  365. DWORD dwNowTimeTemp;
  366. obDCVARRD=DCV_RTCCTL_REG;
  367. if(!(obDCVRDDATA&RTC_ALARM_EN))
  368. return;
  369. obDCV_INT_MASK &= RTC_COUNTER_DIS;
  370. GetDCVInDWORD(DCV_ALARM0_REG,&dwAlarmTime);
  371. do
  372. {
  373. #if(IC_5661_TYPE==IC_M5638C_B1B)
  374. obDCVARRD=DCV_RTCCTL_REG;
  375. obDCVWRDATA=(obDCVRDDATA&RTC_UPDATE_DIS);
  376. #endif
  377. GetDCVInDWORD(DCV_RTC0_REG,&dwNowTimeTemp);
  378. #if(IC_5661_TYPE==IC_M5638C_B1B)
  379. obDCVARRD=DCV_RTCCTL_REG;
  380. obDCVWRDATA=(obDCVRDDATA|RTC_UPDATE_EN);
  381. obDCVWRDATA=(obDCVRDDATA&RTC_UPDATE_DIS);
  382. #endif
  383. GetDCVInDWORD(DCV_RTC0_REG,&gxdwNowTime);
  384. #if(IC_5661_TYPE==IC_M5638C_B1B)
  385. obDCVARRD=DCV_RTCCTL_REG;
  386. obDCVWRDATA=(obDCVRDDATA|RTC_UPDATE_EN);
  387. #endif
  388. }while(dwNowTimeTemp!=gxdwNowTime);
  389. if(dwAlarmTime<gxdwNowTime)
  390. {
  391. obDCVARRD=DCV_RTCCTL_REG;
  392. obDCVWRDATA=(obDCVRDDATA&RTC_ALARM_DIS);
  393. obDCV_INT_MASK &= RTC_ALARM_DIS;
  394. }
  395. obDCV_INT_MASK |= RTC_COUNTER_EN;
  396. }
  397. //Walace060705#1 end
  398. void InitChkSetting(BOOL fForceRestore) large
  399. {
  400. BYTE bI;
  401. //by pass gxdwMaxSector check
  402. gxdwMaxSector=FORCE_REFLASH_SECTOR; //SAVE_SETTING_NAND_ADD; allen050809
  403. gxbStorType=STOR_NAND;
  404. gbSpecialRW=RW_PRIVATE;
  405. gxdwKeptDataSec=FORCE_REFLASH_SECTOR; //050223 allen
  406. gfKeptDataModified=0; //050223 allen
  407. FsRead(SAVE_SETTING_NAND_ADD,1,FS_DATA_0_0);
  408. //restore default setting
  409. if( (gxbFsData[SRAM_RECORD_ID1+gxwOffsetDataSec]!=0x56) ||
  410. (gxbFsData[SRAM_RECORD_ID2+gxwOffsetDataSec]!=0x61) ||
  411. (fForceRestore) )
  412. {
  413. RestoreDefaultSetting();
  414. NandFinishJob();
  415. }
  416. else
  417. { SetSettingToVariable(); }
  418. gxdwKeptDataSec=FORCE_REFLASH_SECTOR;
  419. gbSpecialRW=RW_NORMAL;
  420.        //David060412 start restore RTC from nandflash for assigning variables default value
  421.        obDCV_INT_MASK &= RTC_COUNTER_DIS;
  422. gxdwNowTime=*(XDWORD *)(gxbFsData+SRAM_LAST_RTC_TIME+gxwOffsetDataSec);
  423. gxdwSClockOffset=*(XDWORD *)(gxbFsData+SRAM_LAST_CK_OFFSET+gxwOffsetDataSec);
  424. obDCV_INT_MASK |= RTC_COUNTER_EN;
  425. //David060412 end
  426. if( (fForceRestore) || (!SramCheck()) )
  427. {
  428. // reset RTC clock and alarm~
  429. //Renshuo050216#B
  430. obDCV_INT_MASK &= RTC_COUNTER_DIS; //tne 2006/01/17 #2
  431. for(bI=0;bI<8;bI++)  //reset clock and alarm
  432. {
  433. obDCVARRD=bI+0x15;
  434. obDCVWRDATA=0;
  435. }
  436. obDCV_INT_MASK |= RTC_COUNTER_EN; //tne 2006/01/17 #2
  437. //gxdwNowTime=0;
  438. //gxdwSClockOffset=0;
  439. //gxbDrmOptionMode=(USB_MODE_DEFAULT|S_CLOCK_READY_DEFAULT); //tne 2005/04/26
  440. RestoreSramData();
  441. #if(INIT_USE_BTY_LOW_FLAG) //Renshuo050216#B remove to here
  442. bI=0;
  443. SramWrite(SRAM_BYT_LOW_FLAG,&bI);
  444. #endif
  445. #if (USE_FW_CHARGER) //Chris070521#1
  446. bI=0;
  447. SramWrite(SRAM_CHARGER_STOP_SADC,&bI);
  448. #endif
  449. // set check ID in SRAM
  450. #if(DEBUG_M5638C)  //vicky050608#3
  451. DCV_TEST_EN();
  452. obDCVARRD = 0x10;
  453. obDCVWRDATA = 0x56; //set ID
  454. obDCVARRD = 0x11;
  455. obDCVWRDATA = 0x61; //set ID
  456. DCV_TEST_DIS();
  457. #else
  458. bI=0x56;
  459. SramWrite(SRAM_RECORD_ID1,&bI);
  460. bI=0x61;
  461. SramWrite(SRAM_RECORD_ID2,&bI);
  462. #endif
  463. }
  464. else
  465. {
  466. SramRead(SRAM_DRM_OPTION_MODE,&gxbDrmOptionMode);
  467. ReadSClockFromSram();
  468. }
  469. InitCheckValidAlarmTime(); //Walace060705#1
  470. }
  471. //050811: BootRom set some gpio value, here restore to default.
  472. void BootGpioControl(void)
  473. {
  474. // obGPIOAPUHI=0x00; // BootRom Set GPIOA[4:0] pull-low
  475. // obGPIOAPUEN=0x1F; //
  476. obGPIOAPUEN=0x00;
  477. // obGPIOBPUHI=0xFE; // BootRom Set GPIOB[7:1] pull-high, GPIOB[0] pull-low
  478. // obGPIOBPUEN=0xFF;
  479. #if 1
  480. obGPIOBPUHI|=0xE0;
  481. obGPIOBPUEN|=0xE0;
  482. #else
  483. obGPIOBPUEN=0xE0;
  484. #endif
  485. // obGPIOCPUHI=0x00; // BootRom Set
  486. // obGPIOCPUEN=0x0F;
  487. #if 1
  488. obGPIOCPUHI |= 0x02;
  489. obGPIOCPUEN |= 0x02;
  490. #else
  491. obGPIOCPUEN=0x00;
  492. #endif
  493. // obGPIOEDAT=0xC0; // BootRom Set //0xFF38,disable IDE
  494. // obGPIOEDIR=0xC0; //0xFF39,disable IDE
  495. // obGPIOGPUHI=0x00; // BootRom Set
  496. // obGPIOGPUEN=0xFF;
  497. obGPIOGPUEN=0x00;
  498. // obGPIOHDAT = 0x14; // BootRom Set
  499. // obGPIOHDIR = 0x1C;
  500. // obGPIOHPUHI=0x00;
  501. // obGPIOHPUEN=0xE3;
  502. obGPIOHPUEN=0x00;
  503. // obGPIOIPUHI=0x00;
  504. // obGPIOIPUEN=0xFF;
  505. obGPIOIPUEN=0x00;
  506. // obGPIOKPUHI=0x00;
  507. // obGPIOKPUEN=0xFF;
  508. // obGPIOLPUHI=0x00;
  509. // obGPIOLPUEN=0xFF;
  510. // obGPIOMPUHI=0x00;
  511. // obGPIOMPUEN=0xFF;
  512. #if(COST_DOWN_CIRCUIT)
  513. obDCVARRD=0x14;
  514. obDCVWRDATA|=0x01;
  515. obGPIOCDIR|=0x04;
  516. obGPIOCDAT&=~0x04;
  517. obGPIOHPUEN|=0x01; // inorder to avoid usb key hold
  518. obGPIOHPUHI|=0x01;
  519. obGPIOCPUEN|=0x01; // reset IC need a pull hight register //Alan061026#1
  520. obGPIOCPUHI|=0x01; //Alan061026#1
  521. obGPIOBPUEN|=0xC0; // scan key pull high
  522. obGPIOBPUHI|=0xC0;
  523. #endif
  524. }
  525. bit InitRecInit(void) large
  526. {
  527. BYTE  btemp1,btemp2; //vicky050118
  528. BOOL fResult;//vicky050118
  529. LoadRecInfo(); //vicky050118
  530. fResult=TRUE; //David051014 start 
  531. if(!FsInfoCheck())
  532. {
  533. fResult=FsRecInit();
  534. }
  535. if(fResult)
  536. //if(FsRecInit()) //David051014 end
  537. {
  538. //gxdwFirstEmptyClu=2; //if no FsRecInit(), set this var.
  539. for(btemp2=0;btemp2<REC_DIR_NUM;btemp2++)
  540. {
  541. if(!FsFindDir(btemp2,gxbUiDirName[btemp2],1,ATTR_SUB_DIR)) //find record dir or create it. tne 2006/12/07 #3
  542. {
  543. gxbLastErrCode=ERR_INIT_REC_DIR_ERR;
  544. return FALSE;
  545. }
  546. }
  547. for(btemp2=0;btemp2<REC_DIR_NUM;btemp2++)
  548. {
  549. for(btemp1=0;btemp1<REC_PREFIX_LEN;btemp1++)
  550. gxbFileName[btemp1]=gxbUiFileName[btemp2][btemp1];
  551. if(btemp2==REC_DIR_NUM-1)
  552. fResult=FsOpenNewFile(btemp2,TRUE);
  553. else
  554. fResult=FsOpenNewFile(btemp2,FALSE);
  555. if(!fResult)
  556. {
  557. if(gxbErrCode==ERR_FS_NO_SPACE)
  558. gxbLastErrCode=ERR_INIT_NO_SPACE_ERR;
  559. else
  560. gxbLastErrCode=ERR_INIT_REC_ERR;
  561. return FALSE;
  562. }
  563. }
  564. }
  565. else
  566. {
  567. gxbLastErrCode=ERR_INIT_REC_ERR;
  568. return FALSE;
  569. }
  570. EA=0;//vicky050118
  571. obDCV_INT_MASK &= RTC_COUNTER_DIS;
  572. }
  573. void InitRunToUsb(BOOL fForceUsb) large
  574. {
  575. IE=0;
  576. #if(ENABLE_SD_FUNCTION)
  577. SD_POWER_OFF();
  578. #endif
  579. gxbDeviceMode=DEV_INIT;
  580. obSYSSOFTRSTCTRL|=DSP_RST_DIS;//DSP RST DIS
  581. obPMSIZE|=0x04;//MCU USE ALL
  582. //obDMXSIZE&=0xF8;
  583. //obDMYSIZE&=0xF8;
  584. //#if(DRM_MODE_EN) //Jeff070120#1
  585. //allen050601 start
  586. //obDCVARRD=0x0C; //DCV Addr 0x0C
  587. //obDCVWRDATA=0x46; //PWM Duty Cycle limited for un-know issue
  588. //SetDCVPWMCycle(PWM_USB_CYCLE); //Walace060607#4
  589. SetDcvRegStep(SET_PWM,PWM_USB_CYCLE,SET_PWM_DELAY);  //Walace060718#1
  590. obDCVARRD=0x0D; //DCV Addr 0x0d
  591. obDCVWRDATA=0xf9; //Set 0xf9
  592. //JeffDebug
  593. //obGPIOEDRV= 0x01; 
  594. //obGPIOBDRV=0x01;
  595. //SM Clock
  596. #if 0 //Walace070205#1
  597. obCLKSMCTRL=0xD0;
  598. #endif
  599. #if(!NEW_FLASH)
  600. gfNandInit=FALSE;
  601. #endif
  602. // gxbNandInitStage=NDINIT_INIT;//Allen060622: have not to re-init nandflash new_nf
  603. #if(DRM_MODE_EN) //Jeff070120#1
  604. if(fForceUsb)
  605. {
  606. //LoadProgram(PM_USB,NORMAL_CODE); //load USB program
  607. gxbFsDelId = FSDEL_INIT_FS | FSDEL_GO_USB;
  608. LoadProgram(PM_INIT_CHK,NORMAL_CODE); //File manipulation then go to specified PM
  609. }
  610. else
  611. {
  612. if((gxbDrmOptionMode&DRM_OPTION_MTP_MODE)==USB_MASS_STORAGE)
  613. {
  614. //LoadProgram(PM_USB,NORMAL_CODE); //load USB program
  615. gxbFsDelId = FSDEL_INIT_FS | FSDEL_GO_USB;
  616. LoadProgram(PM_INIT_CHK,NORMAL_CODE); //File manipulation then go to specified PM
  617. }
  618. else //if(gxbDrmOptionMode==1)
  619. {
  620. //LoadProgram(PM_MTP_INIT,NORMAL_CODE);  //load MTP program
  621. gxbFsDelId = FSDEL_INIT_FS | FSDEL_GO_MTP;
  622. LoadProgram(PM_INIT_CHK,NORMAL_CODE); //File manipulation then go to specified PM
  623. }
  624. }
  625. #else
  626. gxbFsDelId = FSDEL_INIT_FS | FSDEL_GO_USB;
  627. LoadProgram(PM_INIT_CHK,NORMAL_CODE); //File manipulation then go to specified PM
  628. //LoadProgram(PM_USB,NORMAL_CODE);
  629. #endif
  630. }
  631. ///////////////////////  Jeff051221 start
  632. #if(ONE_BATT_MODE)  // 1-BAT
  633. CBYTE cbVrefTable[] =  {0x71,0x61,0x51,0x01,0x11,0x21,0x31,0x41} ;  // Jeff051221
  634. void BandGapCalibrationCheck(void)
  635. {
  636. BYTE bVref = 0;    // Jeff051221
  637. BYTE bCalibrationOk = 0;  // Jeff051221
  638. //Hsiang 060209 start
  639. CKCON &= 0xF8;
  640. CKCON |= 0x02;
  641. obCLKMCUDIVF=0x02; //147/4=37
  642. obCLKSRAMDIVF=obCLKSMDIVF=0x02;
  643. //Hsiang 060209 end
  644. //tne 2006/01/17 #2 //by pass gxdwMaxSector check
  645. gxdwMaxSector=FORCE_REFLASH_SECTOR;
  646. NandPrivateEn(); //Walace060111#1
  647. FsRead(0, 1, FS_DATA_0_0);
  648. bCalibrationOk = gxbFsData[gxwOffsetDataSec+504];
  649. bVref = gxbFsData[gxwOffsetDataSec+505];
  650. obCLKMCUDIVF=0x08;   //Hsiang 060209 start
  651. obCLKSRAMDIVF=obCLKSMDIVF=0x08;    
  652. CKCON &= 0xF8;//Hsiang 060209 end
  653. if( bCalibrationOk!=0xA5 && bCalibrationOk!=0xB6)   //  first time Init
  654. {
  655. // Jeff060125 start
  656. //obDCVARRD=0x0C; //DCV Addr 0x0C
  657. //obDCVWRDATA=0x46; //PWM Duty Cycle limited for un-know issue
  658. //SetDCVPWMCycle(PWM_IC_DEFAULT_CYCLE); //Walace060607#4
  659. RefleshBattCheck(200);    // Jeff_061109 start
  660. if(BattCheck()<115)  // Modulate Duty cycle when Batt are lower than 1.4V
  661. {SetDcvRegStep(SET_PWM,PWM_CALIBRATION,SET_PWM_DELAY);}  //Walace060718#1  // Jeff_061109 end
  662. // Jeff060125 end
  663. //tne 2006/01/17 #2
  664. SetSysSpeed(SYS_SPEED_UP_CALIBRATION);
  665. gxbLcdCounter=0;
  666. while(gxbLcdCounter<15);
  667. bVref = SARADC_Calibration();
  668. SetDcvRegStep(SET_PWM,PWM_NORMAL_CYCLE,SET_PWM_DELAY);   //Walace060720#1 //Walace060718#1
  669. }
  670. else{;}
  671. DCV_TEST_EN();
  672. if((bVref<=5)&&(bVref>=1))// Jeff_060314 #2 //hsiang 060324
  673. {
  674. obDCVARRD = 0x19 ;
  675. obDCVWRDATA = cbVrefTable[bVref] ; 
  676. gxbFsData[504]=0xA5;
  677. gxbFsData[505]=bVref;
  678. }
  679. else
  680. {
  681. obDCVARRD = 0x19 ;
  682. obDCVWRDATA = cbVrefTable[3];
  683. gxbFsData[504]=0xB6;
  684. gxbFsData[505]=0x03;
  685. }
  686. DCV_TEST_DIS();
  687. obCLKMCUDIVF=0x08;    //test speed down
  688. obCLKSRAMDIVF=obCLKSMDIVF=0x08;  //test speed down
  689. //gxbLcdCounter=0; //hsiang 060511
  690. //while(gxbLcdCounter<15); //hsiang 060511
  691. FsWrite(0, 1 , FS_DATA_0_0);
  692. NandFinishJob(); //Walace060111#1
  693. NandPrivateDis();
  694. //Walace060607#4 start
  695. //SetDCVPWMCycle(PWM_NORMAL_CYCLE); //Walace060720#1
  696. // Jeff060125 start
  697. /* obDCVARRD=0x0C; //DCV Addr 0x0C
  698. if(obDCVRDDATA==0x46)
  699. {
  700. obDCVARRD=0x0C; //DCV Addr 0x0C
  701. obDCVWRDATA=0x26; //PWM Duty Cycle limited for un-know issue
  702. gxbLcdCounter=0;
  703. while(gxbLcdCounter<8);      // Delay
  704. obDCVARRD=0x0C; //DCV Addr 0x0C
  705. obDCVWRDATA=0x06; //PWM Duty Cycle limited for un-know issue
  706. }
  707. else {;}  */
  708. // Jeff060125 end
  709. //Walace060607#4 end
  710. }
  711. #endif // #if(ONE_BATT_MODE)  // 1-BAT
  712. /////////////////////////////////////// Jeff051221 end
  713. #if(USE_VDDIO_CALIBRATION) //hsiang VddioCali start
  714. #if(_PM_INIT_)  
  715. void InitialVddioCalibration(void)
  716. {
  717. XBYTE xbLevel = 0;   
  718. XBYTE xbCaliFinish = 0;
  719. NandInit();
  720. NandPrivateEn(); 
  721. FsRead(0, 1, FS_DATA_0_0);
  722. xbCaliFinish = gxbFsData[gxwOffsetDataSec+490];
  723. xbLevel = gxbFsData[gxwOffsetDataSec+491];
  724. if( xbCaliFinish!=0x5A && xbCaliFinish!=0x6B)   //  first time Init
  725. xbLevel = Vddio_Calibration();
  726. if((xbLevel==0x42)||(xbLevel==0x52)||(xbLevel==0x62)||(xbLevel==0x72))
  727. {
  728. obDCVARRD = 0x0A ;
  729. obDCVWRDATA = xbLevel ; 
  730. gxbFsData[490]=0x5A;
  731. gxbFsData[491]=xbLevel;
  732. }
  733. else
  734. {
  735. obDCVARRD=0x0A;
  736. obDCVWRDATA=0x52;
  737. gxbFsData[490]=0x6B;
  738. gxbFsData[491]=0x52;
  739. }
  740. //test speed down
  741. obCLKMCUDIVF=0x08;   
  742. obCLKSRAMDIVF=obCLKSMDIVF=0x08;  
  743. FsWrite(0, 1 , FS_DATA_0_0);
  744. }
  745. else
  746. {
  747. obDCVARRD = 0x0A ;
  748. obDCVWRDATA = xbLevel ;
  749. }
  750. NandFinishJob();
  751. NandPrivateDis();
  752. }
  753. #endif 
  754. #endif //hsiang VddioCali end
  755. #if 0 //Walace060108 debug //Chris change from #if(0) ->#if 0 for Source Insight view
  756. BOOL StoreSarADCValue(void)
  757. {
  758. BYTE bBtyTimeOut,bBtyNum,bStoreCount;
  759. WORD wCounter;
  760. BANDGAP_3V3_SWITCH();
  761. //OledInit();
  762. //OLED_CLEAR_ALL();
  763. gbTimeTick0=0;
  764. gxbLcdCounter=0;
  765. while(gxbLcdCounter<10){;}
  766. for(bStoreCount=0;bStoreCount<4;bStoreCount++)
  767. {
  768. obCLKMCUDIVF=0x01;    //test speed up
  769. obCLKSRAMDIVF=obCLKSMDIVF=0x01;  //test speed up
  770. gxbLcdCounter=0;
  771. while(gxbLcdCounter<3){;}
  772. for(wCounter=0;wCounter<STORE_COUNT;wCounter++)
  773. {
  774. bBtyTimeOut=0xFF;
  775. obCLKI2CSCTRL = 4; // Enable I2Cs Clock
  776. obLRRSOC |= 0x01;
  777. obLRRSOC &= 0xFE; // Give bit1 High->Low
  778. while((bBtyNum=obDCVBATLRR)>=0x80) // wait bit 7 go to 0
  779. {
  780. bBtyTimeOut--;
  781. if(bBtyTimeOut==0)
  782. {
  783. //bBtyNum=BATTERY_LV_ERR;
  784. bBtyNum=0xFF;
  785. break;
  786. }
  787. }
  788. obCLKI2CSCTRL = 0; // Disable I2Cs Clock
  789. gxbDebugData[wCounter]=bBtyNum;
  790. //OledShowNumber(bBtyNum,WORD0,LINE0,3);
  791. //OledShowNumber(bCounter,WORD0,LINE1,3);
  792. //OledShowNumber(gbTimeTick0,WORD0,LINE2,3);
  793. //gxbLcdCounter=0;
  794. //while(gxbLcdCounter<1){;}
  795. }
  796. if(bStoreCount==0)
  797. {
  798. NandInit();
  799. FsInit();
  800. //OledShowNumber(555,WORD3,LINE1,3);
  801. }
  802. /*
  803. gfKeptDataModified=0; //050223 allen
  804. gxbStorage=STOR_NAND;
  805. gxdwMaxSector=FORCE_REFLASH_SECTOR; //SAVE_SETTING_NAND_ADD; allen050809
  806. gxbStorType=STOR_NAND;
  807. gbSpecialRW=RW_PRIVATE;
  808. gxdwKeptDataSec=FORCE_REFLASH_SECTOR; //050223 allen
  809. */
  810. NandPrivateEn();
  811. FsRead(3+bStoreCount, 1, FS_DATA_0_0);
  812. //OledShowNumber(666,WORD3,LINE1,3);
  813. for(wCounter=0;wCounter<STORE_COUNT;wCounter++)
  814. gxbFsData[gxwOffsetDataSec+wCounter]=gxbDebugData[wCounter];
  815. //OledShowNumber(777,WORD3,LINE1,3);
  816. FsWrite(3+bStoreCount, 1 , FS_DATA_0_0);
  817. NandFinishJob();
  818. NandPrivateDis();
  819. //OledShowNumber(888,WORD3,LINE1,3);
  820. }
  821. obCLKMCUDIVF=0x08;    //test speed down
  822. obCLKSRAMDIVF=obCLKSMDIVF=0x08;  //test speed down
  823. return FALSE;
  824. }
  825. #endif
  826. //Ren060125 //Walace060125 //Walace060326#1 //Walace060730#1
  827. #if(DEBUG_M5638C)
  828. void BrownOutInit(void)
  829. {
  830. BYTE bTmp ;
  831. DCV_TEST_EN();
  832. /*
  833. obDCVARRD = 0x1C;
  834. obDCVWRDATA = obDCVRDDATA & (~0x18);//power on brown-out ckt, use batt as source 
  835. obDCVARRD = 0x1B;
  836. //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x04; //0.95V brown out
  837. #if(ONE_BATT_MODE)
  838. obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x00; // 0.85V   brown out //Walace060504#3
  839. #else
  840. obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x1F; // 1.75 // 1.71  // 2.65v  brown out //
  841. //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x1D; //1.7   // 1.66 //3.4    // 2.60
  842. //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x1B; //1.65 // 1.60 //3.3   // 2.53
  843. //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x19; //  1.6   //1.55   //  2.48
  844. //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x17; //1.55 //1.50  //  2.42
  845. //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x15; //1.50   // 1.44  //  2.34
  846. #endif                                                                         
  847. */
  848. #if(ONE_BATT_MODE)
  849. obDCVARRD = 0x1C;
  850. obDCVWRDATA = obDCVRDDATA & (~0x18);//power on brown-out ckt, use batt as source 
  851. obDCVARRD = 0x1B;
  852. //obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x00; // 0.85V   brown out //Walace060504#3
  853. obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x01; // 0.875V //tne 2006/11/29 #1
  854. #else
  855. // In Li-ion system,be care of that detemine the Brownout Voltage Level according the
  856. // battery work voltage(WV)
  857. #if(IC_5661_TYPE==IC_M5638C_B1B)
  858. obDCVARRD = 0x1C;
  859. obDCVWRDATA = obDCVRDDATA & (~0x08); //Power on brown-out ckt, use 1/2 batt as source 
  860. obDCVARRD = 0x1B;
  861. obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x00; //Brownout 2.65v  //Walace060504#3
  862. #else
  863. obDCVARRD = 0x1C;
  864. obDCVWRDATA = obDCVRDDATA & (~0x08);//Power on brown-out ckt, use 1/2 batt as source 
  865. obDCVARRD = 0x1B;
  866. obDCVWRDATA = (obDCVRDDATA & (~0x1F)) | 0x03;//Brownout 2.80v //Walace060504#3
  867. // Reg value : Brwonout Register Voltage = (Brownout Battery Voltage - 0.95)/2
  868. // 0.95   diode voltage drop
  869. // 0x00 : 0.850 = (2.65-0.95)/2
  870. // 0x01 : 0.875 = (2.70-0.95)/2
  871. // 0x02 : 0.900 = (2.75-0.95)/2
  872. // 0x03 : 0.925 = (2.80-0.95)/2
  873. // 0x04 : 0.950 = (2.85-0.95)/2
  874. // 0x05 : 0.975 = (2.90-0.95)/2
  875. // 0x06 : 1.000 = (2.95-0.95)/2
  876. // 0x07 : 1.025 = (3.00-0.95)/2
  877. // 0x08 : 1.050 = (3.05-0.95)/2
  878. // 0x09 : 1.075 = (3.10-0.95)/2
  879. // 0x0A : 1.100 = (3.15-0.95)/2
  880. // 0x0B : 1.125 = (3.20-0.95)/2
  881. #endif
  882. #endif
  883. DCV_TEST_DIS();
  884. bTmp = obDCV_INT_FLAG; //clear flag
  885. obDCV_INT_MASK |=0x02; //enable batt brown out
  886. obSYSINT0MASK |= 0x02; //enable DCV int
  887. }
  888. #endif
  889. //Ted 0203 start
  890. #define DIRECTLY_ASSIGN_INIT_ADDR   0 //current MPtool set 0
  891. void RestoreBlock0(void) large
  892. {
  893. BYTE bI;
  894. WORD   TempFinalAddr, CurrentAddr;
  895. WORD     TempRedu;
  896. BYTE  gxbTempRedu[4];
  897. SetSysSpeed(SYS_SPEED_NORMAL);
  898. NandReadPhySec(gxwAddrPMI,1,SRAM_DATA_ADDR);
  899. *((BYTE*)&TempFinalAddr) =gxbFsData[511];
  900. *((BYTE*)&TempFinalAddr+1) = gxbFsData[510];
  901. EraseBlock0();
  902. ////////////////////debug code/////////////////
  903. /*CurrentAddr=0;
  904. while(1)
  905. {
  906. //check the BLOCK0 for debug
  907. NandReadPhySec(CurrentAddr++,1,SRAM_DATA_ADDR);
  908. *((BYTE*)&TempRedu) =obSMREADREDU6; //Hi
  909. *((BYTE*)&TempRedu+1) = obSMREADREDU7; //Lo
  910. TempRedu>>=1;
  911. if(CurrentAddr>TempFinalAddr)
  912. break;
  913. }*/
  914. ///////////////////////////////////////
  915. CurrentAddr=gxwAddrPMI;
  916. while(1)
  917. {
  918. for(bI=0; bI<4; bI++)
  919. {
  920. NandReadPhySec(CurrentAddr+bI, 1,((WORD)bI<<9) + SRAM_DATA_ADDR);
  921. *((BYTE*)&TempRedu) =obSMREADREDU6; //Hi
  922. *((BYTE*)&TempRedu+1) = obSMREADREDU7; //Lo
  923. TempRedu>>=1;
  924. gxbTempRedu[bI]=*((BYTE *)&TempRedu +1 );
  925. }
  926. #if(DIRECTLY_ASSIGN_INIT_ADDR)
  927. if(CurrentAddr==gxwAddrPMI)
  928. {
  929. gxbFsData[4]=0x02;
  930. gxbFsData[5]=0x00;
  931. }
  932. #endif
  933. UpgradeBlock0(CurrentAddr-gxwAddrPMI, gxbTempRedu);
  934. CurrentAddr = CurrentAddr + 4;
  935. if(CurrentAddr-gxwAddrPMI>TempFinalAddr)
  936. break;
  937. }
  938. //gxwAddrPMI=0;
  939. /////////////////////////debug code//////////////////////
  940. /*CurrentAddr=0;
  941. while(1)
  942. {
  943. //check the BLOCK0 for debug
  944. NandReadPhySec(CurrentAddr++,1,SRAM_DATA_ADDR);
  945. *((BYTE*)&TempRedu) =obSMREADREDU6; //Hi
  946. *((BYTE*)&TempRedu+1) = obSMREADREDU7; //Lo
  947. TempRedu>>=1;
  948. if(CurrentAddr>TempFinalAddr)
  949. break;
  950. }
  951. LcdBackLightControl(LCD_BACKLIGHT_ON);
  952. while(1)
  953. {};
  954. */
  955. //////////////////////////////////////////////////////
  956. }
  957. //Ted 0203 end
  958. #if(1) //Patrick061016 //tne 2006/11/29 #1
  959. void LowBatteryDetection(BYTE bTestTime)
  960. {
  961. //USB PHY test  // 060803 Renshuo
  962. obMODSEL3 |= 0x40;
  963. gxbLcdCounter=0;
  964. gxbBtyLowNum=0;
  965. while(gxbLcdCounter<bTestTime)
  966. {
  967. if( (BattCheck()<=BATTERY_LV_LOW) && (gxbBtyLowNum<0xFF) )
  968. gxbBtyLowNum++;
  969. }
  970. obMODSEL3 &= (~0x40);
  971. }
  972. #endif
  973. // Jeff_061109 start
  974. #if(_PM_INIT_)
  975. void RefleshBattCheck(WORD wCounter) large
  976. {
  977. while(wCounter--) {BattCheck();}
  978. }
  979. // Jeff_061124 mark
  980. /*void StarUpPWMmodulation(void) large
  981. {
  982. RefleshBattCheck(0x5555);
  983. if(BattCheck()>90)   // Do not add Loading when batt is lower than 1.2V
  984. {obMODSEL3|=0x40;} // Enable USB clock to Add more loading
  985. CommonDelay(0x5555);
  986. obDCVARRD=0x0C; //DCV Addr 0x0C
  987. obDCVWRDATA=0x36;
  988. obMODSEL3&=(~0x40); // Disable USB clock to Save Power 
  989. CommonDelay(0x5555);    // suitable delay time
  990. obDCVARRD=0x0C; //DCV Addr 0x0C
  991. obDCVWRDATA=0x26;  
  992. CommonDelay(0xFFF);      // suitable delay time
  993. obDCVARRD=0x0C; //DCV Addr 0x0C
  994. obDCVWRDATA=0x16;   
  995. CommonDelay(0xFFF);      // suitable delay time
  996. obDCVARRD=0x0C; //DCV Addr 0x0C
  997. obDCVWRDATA=0x06;
  998. CommonDelay(0xFFF);
  999. if(BattCheck()>90)   // Do not add Loading when batt is lower than 1.2V
  1000. {obMODSEL3|=0x40; } // Enable USB clock to Add more loading
  1001. obDCVARRD=0x06;
  1002. obDCVWRDATA=obDCVRDDATA&(~0x08);      // over voltage detect disable
  1003. CommonDelay(0xFFF);
  1004. //if(BattCheck()>115)   // Do not modulate VDDD when batt is higher than 1.4V
  1005. {SetDcvRegStep(SET_VDD,0x05,SET_VDD_DELAY); }  // Set VDDA to 1.65V
  1006. //else
  1007. //{SetDcvRegStep(SET_VDD,0x00,SET_VDD_DELAY); }  // Set VDDD to 1.20V  
  1008. obMODSEL3&=(~0x40); // Disable USB clock to Save Power 
  1009. obDCVARRD=0x06;
  1010. obDCVWRDATA=obDCVRDDATA|0x08; // over voltage detect enable
  1011. }*/
  1012. // Jeff_061124 add
  1013. void StarUpPWMmodulation(void) large
  1014. {
  1015. obGPIODDAT|=0x01;
  1016. obGPIODDIR|=0x01;
  1017. //4 1v2 Modulation
  1018. SetDcvRegStep(SET_VDD,0x70,0xFFF);   // Set VDDD to 1.20V  // Jeff_061031
  1019. //4PWM Modulation
  1020. RefleshBattCheck(0x4000);
  1021. if(BattCheck()>90)   // Do not add Loading when batt is lower than 1.2V
  1022. {
  1023. obMODSEL3|=0x40; // Enable USB clock to Add more loading
  1024. CommonDelay(0xFFF);
  1025. SetDcvRegStep(0x0A,0x72,SET_VDD_DELAY);     // Modulate 3v3 to avoid drop
  1026. }
  1027. obGPIODDAT&=~0x01;
  1028. obMODSEL3&=(~0x40);         // Disable USB clock to Save Power
  1029. SetDcvRegStep(SET_PWM,0x36,0x6000);   // Modulate PWM to 0x36
  1030. CommonDelay(0x5000);
  1031. obGPIODDAT|=0x01;
  1032. //CommonDelay(0x5000);
  1033. //CommonDelay(0xFFF);
  1034. //4 1V8 Modulation
  1035. obDCVARRD=0x06;
  1036. obDCVWRDATA=obDCVRDDATA&(~0x08);      // over voltage detect disable
  1037. SetDcvRegStep(SET_VDD,0x00,0xFFF);   // Set VDDA to 1.65V  // Jeff_061031
  1038. CommonDelay(0x5000);
  1039. //4PWM Modulation
  1040. SetDcvRegStep(SET_PWM,0x26,0x5000); // Modulate PWM to 0x26
  1041. CommonDelay(0xFFF);
  1042. //4 3V3 Modulation
  1043. SetDcvRegStep(0x0A,0x52,0xFFF); 
  1044. CommonDelay(0xFFF);
  1045. //4PWM Modulation
  1046. obGPIODDAT&=~0x01;
  1047. SetDcvRegStep(SET_PWM,PWM_NORMAL_CYCLE,SET_PWM_DELAY); // Modulate PWM to 0x06
  1048. obMODSEL3&=(~0x40);         // Disable USB clock to Save Power
  1049. }
  1050. #endif
  1051. // Jeff_061109 end