bmt.c 94.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *	 bmt.c
 *
 * Project:
 * --------
 *	Maui_Software
 *
 * Description:
 * ------------
 *	This Module defines charging algorithm.
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *				 HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 *	 Rev 1.31	May 16 2005 23:42:02	BM_Trunk
 * removed!
 * removed!
 * removed!
 *	 Rev 1.30	Jan 18 2005 00:14:16	BM
 * removed!
 * removed!
 *	 Rev 1.29	Jan 14 2005 20:56:34	mtk00502
 * removed!
 * removed!
 * removed!
 *	 Rev 1.28	Dec 03 2004 10:32:24	mtk00502
 * removed!
 * removed!
 * removed!
 *	 Rev 1.27	Oct 27 2004 11:48:46	mtk00502
 * removed!
 * removed!
 * removed!
 *	 Rev 1.26.2.1	Oct 27 2004 11:48:36	mtk00502
 * removed!
 * removed!
 * removed!
 *	 Rev 1.26.1.1	Oct 27 2004 11:48:22	mtk00502
 * removed!
 * removed!
 * removed!
 *	 Rev 1.26.1.0	Oct 19 2004 22:08:48	BM
 * removed!
 * removed!
 *	 Rev 1.26	Sep 22 2004 17:25:00	mtk00479
 * removed!
 * removed!
 * removed!
 *	 Rev 1.25	Jul 20 2004 21:50:24	mtk00502
 * removed!
 * removed!
 * removed!
 *	 Rev 1.24	May 13 2004 15:30:06	mtk00502
 * removed!
 * removed!
 * removed!
 *	 Rev 1.23	Mar 31 2004 11:26:58	mtk00502
 * removed!
 * removed!
 * removed!
 *	 Rev 1.22	Feb 23 2004 14:56:52	mtk00502
 * removed!
 * removed!
 * removed!
 *	 Rev 1.21	Feb 06 2004 17:32:10	mtk00502
 * removed!
 * removed!
 * removed!
 *	 Rev 1.20	Feb 01 2004 17:28:36	mtk00502
 * removed!
 * removed!
 * removed!
 *	 Rev 1.13.3.7	Dec 30 2003 09:57:54	mtk00479
 * removed!
 * removed!
 * removed!
 *	 Rev 1.13.3.6	Dec 07 2003 14:31:28	mtk00479
 * removed!
 * removed!
 * removed!
 *	 Rev 1.13.3.5	Dec 07 2003 14:23:46	mtk00479
 * removed!
 * removed!
 * removed!
 *	 Rev 1.13.3.4	Nov 26 2003 17:32:50	mtk00479
 * removed!
 * removed!
 * removed!
 * removed!
 *	 Rev 1.13.3.3	Nov 21 2003 11:40:36	mtk00479
 * removed!
 * removed!
 * removed!
 *	 Rev 1.13.3.2	Nov 17 2003 13:47:40	mtk00479
 * removed!
 * removed!
 *	 Rev 1.13.3.1	Nov 16 2003 18:01:18	mtk00479
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 *	 Rev 1.13.3.0	Oct 08 2003 17:28:30	BM
 * removed!
 * removed!
 *	 Rev 1.13	Sep 29 2003 16:56:16	mtk00288
 * removed!
 * removed!
 * removed!
 *	 Rev 1.12	Sep 24 2003 17:50:06	mtk00479
 * removed!
 * removed!
 * removed!
 *	 Rev 1.11	Sep 17 2003 14:24:12	mtk00288
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/
#include	"drv_comm.h"
#include 	"stack_common.h"  
#include 	"stack_msgs.h"
#include 	"app_ltlcom.h"		/* Task message communiction */
#include	"bmt.h"
#include	"drvsignals.h"
#include	"bmt_trc.h"
// For RHR ADD Usage
#include 	"stack_config.h"
#include 	"kal_trace.h"
#include 	"stack_ltlcom.h"
#include    "bmt_sw.h"
#include    "kal_internal_api.h"
#include    "kal_general_types.h"
#include    "kal_public_api.h"
#include    "dcl.h"
#include	"dcl_pmu_sw.h"

#define CHARGING_TIME_UNIT KAL_TICKS_1_SEC

#if defined(__DRV_BMT_DISABLE_STOP_TIMER__)
#define STOPTIMER_TIMEOUT_TICK	 0
#else
#define STOPTIMER_TIMEOUT_TICK	 (KAL_TICKS_1_MIN*30)
#endif

#if defined(__DRV_BMT_ESTIMATIVE_TIMER_ON_TOPOFF__)
#define ESTIMATIVE_TIMEOUT_TICK	 (KAL_TICKS_1_MIN*70)
#endif //#if defined(__DRV_BMT_ESTIMATIVE_TIMER_ON_TOPOFF__)

#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
extern void bmt_vchg_compare_and_set_current(kal_int32 cur_VCHG, kal_bool Hysteresis_Enable);
#endif

extern void bmt_enable_sleepmode(kal_bool enable);

#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
kal_bool High_Vchg_Safety_Timer_Set = KAL_FALSE;
kal_bool First_Time_Charge_Enable = KAL_TRUE;
bmt_customized_high_vchg_struct *bmt_high_vchg_para;
kal_uint32 bmt_total_charge_time = 0;
kal_uint32 bmt_high_vchg_current = 0xFFFFFFF;
kal_int32 VCharge_Total = 0;
kal_int32 Pre_VCharge_AVG = 0;
kal_int32 Cur_VCharge_MAX = 0;
kal_bool bmt_safety_timer_config = KAL_FALSE;
#endif // End of #if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)

#if defined(__DRV_BMT_PRECHARGE_TO_FULL_DIRECTLY__)
DCL_HANDLE bmt_vbat_in_hisr_gpt_handle = 0xFF;
static kal_uint8  bmt_vbat_in_hisr_id;
kal_int32 g_battery_pre_voltage = 0;
extern volatile kal_uint8 bmt_event_sche_active;
#endif //defined(__DRV_BMT_PRECHARGE_TO_FULL_DIRECTLY__)

extern void bmt_set_stoptimer(kal_uint32 tick);
extern void bmt_charge_end(void);
extern void BMT_sendMes2UEM(BMT_CHR_STAT status);
extern void bmt_timer_config(kal_uint32 time);
extern kal_uint8  bmt_bmtid_adc_handle[BMT_ADC_MAX_CHANNEL_TOTAL];
extern stack_timer_struct		ChargeTimeout_timer;

#if defined(PMIC_6236_CV_TRIM_CALIBRATION)
extern pmu6236_chr_vbat_cv_enum mt6236_efuse_vbat_cv;
#endif // #if defined(PMIC_6236_CV_TRIM_CALIBRATION)

static kal_uint8 BATFULL_index = 0;
#if defined(DRV_BMT_PULSE_CHARGING)
static kal_bool batfull_msg_flag = KAL_FALSE;
static kal_uint8 BATNOTFULL_index = 0;
#if defined(DRV_BMT_HW_PRECC_WORKAROUND)
kal_uint8 SW_Low_Current_Cnts=0;
kal_bool SW_Workaround_Flag = KAL_TRUE;
kal_bool Manual_Disable_Charge_Flag = KAL_FALSE;
kal_uint8 HW_Plug_Status = bmt_chr_uninit;
#endif // End of #if defined(DRV_BMT_HW_PRECC_WORKAROUND)
#endif // #if defined(DRV_BMT_PULSE_CHARGING)
static CHRTStruct  BATTime;
BMTStruct BMT;			 /*Main BMT struct*/
#if defined(CHR_WITH_NI_MH_BATTERY) 
static kal_uint32 VBATtmp;
#endif
/*lint -e552*/  
//althought not access in bmt.c, but they are accessed in bmt_main.c
extern kal_uint32 VBAT_OFF;
extern double ADCBAT_OFF;
/*lint +e552*/
kal_uint8 low_charger_count = 0;
kal_uint8 over_charger_count = 0;
kal_uint8 low_temper_count = 0;
kal_uint8 over_temper_count = 0;
kal_uint8 over_current_count=0;
kal_uint8 low_current_count = 0;

static void BMT_CHRPRE_ON(void);
static void BMT_CHRPRE_OFF(void);
static void BMT_CHRFAST_ON(void);
static void BMT_CHRFAST_OFF(void);
static void BMT_CHRTOPOFF_ON(void);
static void BMT_CHRTOPOFF_OFF(void);
static void BMT_CHRBATFULL_ON(void);
static void BMT_CHRBATFULL_OFF(void);
static void BMT_CHRHOLD(void);
#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)
static void BMT_CHRERRORCHECK(void);
#endif //#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)
static void BMT_MeasureStop(void);
#if defined(DRV_BMT_PULSE_CHARGING)
static void BMT_CHRPOSTFULL_ON(void);
static void BMT_CHRPOSTFULL_OFF(void);
static kal_bool BMT_CHRPRE_TO_CHRBATFULL_CHECK(void);
#endif //#if defined(DRV_BMT_PULSE_CHARGING)

#if defined(DRV_BMT_PULSE_CV_CHARGING)
void BMT_CHRPRE_TO_CHRBATFULL(void);
#endif //#if defined(DRV_BMT_PULSE_CV_CHARGING)

#if defined(__DRV_BMT_PRECHARGE_TO_FULL_DIRECTLY__)
static void BMT_CHRPRE_FULLCHECK(void);
#endif // __DRV_BMT_PRECHARGE_TO_FULL_DIRECTLY__
static void BMT_CtrlCharge(kal_uint8 ON, kal_uint32 VBAT);
static kal_bool BMT_CHR_Is_Under_ExtremeTmp(BATPHYStruct *pBATPHYS);
static kal_bool BMT_CHR_Is_ExtremeTmp_Charge_Full(BATPHYStruct *pBATPHYS);
static kal_bool BMT_CHR_Is_ExtremeTmp_Charge_Recharge(BATPHYStruct *pBATPHYS);
void BMT_CHARSTOP(void);
kal_uint8 SaftyTimer_Flag = 0;

extern DCL_HANDLE bmt_PmuHandler;

#if defined(DRV_BMT_PULSE_CHARGING)
const CHR_FUNC	BMT_CHRFUNC[][2] = 
{
    {BMT_CHRPRE_OFF, BMT_CHRPRE_ON} //CHR_PRE
    ,{BMT_CHRFAST_OFF, BMT_CHRFAST_ON} //CHR_FAST
    ,{BMT_CHRTOPOFF_OFF, BMT_CHRTOPOFF_ON} //CHR_TOPOFF
    ,{BMT_CHRBATFULL_OFF, BMT_CHRBATFULL_ON} //CHR_BATFULL
    ,{BMT_MeasureStop,BMT_MeasureStop} //CHR_ERROR
    ,{BMT_CHRHOLD,BMT_CHRHOLD} //CHR_HOLD
    ,{BMT_CHRPOSTFULL_OFF, BMT_CHRPOSTFULL_ON} //CHR_POSTFULL
#ifdef __DRV_BMT_PRECHARGE_TO_FULL_DIRECTLY__
    ,{BMT_CHRPRE_FULLCHECK, BMT_CHRPRE_FULLCHECK} //CHR_PRE_FULL_CHECK
#endif
};
#else //#if defined(DRV_BMT_PULSE_CHARGING)
const CHR_FUNC	BMT_CHRFUNC[][2] = 
{
    {BMT_CHRPRE_OFF, BMT_CHRPRE_ON} //CHR_PRE
    ,{BMT_CHRFAST_OFF, BMT_CHRFAST_ON} //CHR_FAST
    ,{BMT_CHRTOPOFF_OFF, BMT_CHRTOPOFF_ON} //CHR_TOPOFF
    ,{BMT_CHRBATFULL_OFF, BMT_CHRBATFULL_ON} //CHR_BATFULL
    ,{BMT_MeasureStop,BMT_MeasureStop} //CHR_ERROR
    ,{BMT_CHRHOLD,BMT_CHRHOLD} //CHR_HOLD
#ifdef __DRV_BMT_PRECHARGE_TO_FULL_DIRECTLY__
    ,{BMT_MeasureStop,BMT_MeasureStop} //CHR_POSTFULL - actually non-pulse charging wnn't go here
    ,{BMT_CHRPRE_FULLCHECK, BMT_CHRPRE_FULLCHECK} //CHR_PRE_FULL_CHECK
#endif
};
#endif //#if defined(DRV_BMT_PULSE_CHARGING)

#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
void bmt_high_vchg_first_time_set(void)
{
	BATPHYStruct BATPHYS;
	if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	{
        bmt_vchg_compare_and_set_current(BATPHYS.VCHARGER, KAL_TRUE);
        Pre_VCharge_AVG = BATPHYS.VCHARGER;
        drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_FIRST_TIME_VCHG_TRC, Pre_VCharge_AVG);
	}
} 

void bmt_high_vchg_set_safetytimer(kal_int32 Cur_VCHG)   
{
    kal_int32 i;

    if((bmt_safety_timer_config == KAL_FALSE) && (BMT.pmictrl_state == PMIC_CHARGEON))
    {
        drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_CURRENT_VCHG_TRC, Cur_VCHG);
        // Cur_VCHG > S/W OVP
        if(Cur_VCHG >= bmt_high_vchg_para->HIGH_VCHG_TABLE[VCHG_VOL_LEVEL - 1][0])
        {
            bmt_total_charge_time = 0;
            drv_trace0(TRACE_GROUP_9, BMT_HIGH_VCHG_TOO_HIGH_TRC);
        }
        else if(Cur_VCHG < bmt_high_vchg_para->HIGH_VCHG_TABLE[0][0])
        {
            drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_LOWEST_THRESHOLD_TRC, bmt_high_vchg_para->HIGH_VCHG_TABLE[0][0]);
            bmt_total_charge_time = bmt_high_vchg_para->HIGH_VCHG_TABLE[0][2];
        }
        else
        {
            for(i = (VCHG_VOL_LEVEL - 1); i > 0; i--)
            {

                drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_UPPER_THRESHOLD_TRC, bmt_high_vchg_para->HIGH_VCHG_TABLE[i][0]);
                drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_LOWER_THRESHOLD_TRC, bmt_high_vchg_para->HIGH_VCHG_TABLE[i-1][0]);
                if((Cur_VCHG < bmt_high_vchg_para->HIGH_VCHG_TABLE[i][0]) && (Cur_VCHG >= bmt_high_vchg_para->HIGH_VCHG_TABLE[i-1][0]))
                {
                    bmt_total_charge_time = bmt_high_vchg_para->HIGH_VCHG_TABLE[i][2];  
                    drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_TABLE_INDEX_TRC, i);
                    break;
                }
            }
        }
        drv_trace1(TRACE_GROUP_9, BMT_CURRENT_PMIC_STATE_TRC, BMT.pmictrl_state);
        if(bmt_IsUSBorCharger() != BMT_USB_IN)
        {
            drv_trace0(TRACE_GROUP_9, BMT_AC_IN_TRC);
        }
        else
        {
            drv_trace0(TRACE_GROUP_9, BMT_USB_IN_TRC);
			bmt_total_charge_time = BMT_TOTAL_CHARGE_TIME;
        }
        stack_start_timer(&ChargeTimeout_timer, 0, KAL_TICKS_1_MIN*bmt_total_charge_time);
         drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_START_TRC, bmt_total_charge_time);                      

        bmt_safety_timer_config = KAL_TRUE;
        SaftyTimer_Flag = BMT_SAFETY_TIMER_ON; 
    }
    
}

void bmt_vchg_compare_and_set_current(kal_int32 Cur_VCHG, kal_bool Hysteresis_Enable)
{
    static kal_uint8 pre_level = 0xFF;

    kal_uint8 i, cur_level = 0;
  
    drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_CURRENT_VCHG_TRC, Cur_VCHG);    
    // Cur_VCHG > S/W OVP
    if(Cur_VCHG >= bmt_high_vchg_para->HIGH_VCHG_TABLE[VCHG_VOL_LEVEL - 1][0])
    {
        bmt_high_vchg_current = bmt_high_vchg_para->HIGH_VCHG_TABLE[VCHG_VOL_LEVEL - 1][1]; // Use Last Charge Current
        drv_trace0(TRACE_GROUP_9, BMT_HIGH_VCHG_TOO_HIGH_TRC);
        if(Hysteresis_Enable == KAL_TRUE)
        {
            cur_level = VCHG_VOL_LEVEL;
        }
    }
    else if(Cur_VCHG < bmt_high_vchg_para->HIGH_VCHG_TABLE[0][0])
    {
        drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_LOWEST_THRESHOLD_TRC, bmt_high_vchg_para->HIGH_VCHG_TABLE[0][0]);
        bmt_high_vchg_current = bmt_high_vchg_para->HIGH_VCHG_TABLE[0][1];
        if(Hysteresis_Enable == KAL_TRUE)
        {        
            cur_level = 0;
        }
    }
    else
    {
        for(i = (VCHG_VOL_LEVEL - 1); i > 0; i--)
        {
            drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_UPPER_THRESHOLD_TRC, bmt_high_vchg_para->HIGH_VCHG_TABLE[i][0]);
            drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_LOWER_THRESHOLD_TRC, bmt_high_vchg_para->HIGH_VCHG_TABLE[i-1][0]);
            if((Cur_VCHG < bmt_high_vchg_para->HIGH_VCHG_TABLE[i][0]) && (Cur_VCHG >= bmt_high_vchg_para->HIGH_VCHG_TABLE[i-1][0]))
            {
                bmt_high_vchg_current = bmt_high_vchg_para->HIGH_VCHG_TABLE[i][1];  
                if(Hysteresis_Enable == KAL_TRUE)
                {
                    cur_level = i;
                }
                break;
            }
        }
    }
    
    if(Hysteresis_Enable == KAL_TRUE)
    {
        drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_PRE_CURRENT_LEVEL_TRC, pre_level);       
        drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_CUR_CURRENT_LEVEL_TRC, cur_level);        
        if(cur_level != pre_level && ((cur_level - pre_level) == 1 || (pre_level - cur_level) == 1 ) )
        {
            drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_HYSTERESIS_UPPER_BOUND_TRC, bmt_high_vchg_para->HIGH_VCHG_TABLE[pre_level][0] + BMT_HIGH_VCHG_THRESHOLD);              
            drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_HYSTERESIS_LOWER_BOUND_TRC, bmt_high_vchg_para->HIGH_VCHG_TABLE[cur_level][0] - BMT_HIGH_VCHG_THRESHOLD);                          
            
            // Hysteresis
            if((cur_level > pre_level) && (Cur_VCHG < bmt_high_vchg_para->HIGH_VCHG_TABLE[pre_level][0] + BMT_HIGH_VCHG_THRESHOLD))
            {
                bmt_high_vchg_current = bmt_high_vchg_para->HIGH_VCHG_TABLE[pre_level][1];
            }
            else if((cur_level < pre_level) && (Cur_VCHG > bmt_high_vchg_para->HIGH_VCHG_TABLE[cur_level][0] - BMT_HIGH_VCHG_THRESHOLD))
            {
                bmt_high_vchg_current = bmt_high_vchg_para->HIGH_VCHG_TABLE[pre_level][1];
            }
            else
            {
                pre_level = cur_level;
            }
                
        }
        else
        {
            pre_level = cur_level;
        }
    }
    drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_CHARGE_CURRENT_TRC, bmt_high_vchg_current);                              


#if defined(__CHINA_CHARGER_STANDARD__)
	if(chr_usb_detect.chr_usb_present == CHARGER_PRESENT_NON)
	{
	    if(bmt_high_vchg_current > PMU_CHARGE_CURRENT_450_00_MA)
	    {
	        bmt_high_vchg_current = PMU_CHARGE_CURRENT_450_00_MA;
	        drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_SET_USB_CHARGE_CURRENT_TRC, CHR_USB_CHARGE_CURRENT);	
	    }
	}
#endif //#if defined(__CHINA_CHARGER_STANDARD__)

	
}
#endif // End of #if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)

void bmt_set_vbat_cv_calibration(DCL_INT32 vbat)
{
	PMU_CTRL_CHR_SET_VBAT_CV_CALIBRATION val;

	val.vbat = vbat;
	DclPMU_Control(bmt_PmuHandler,CHR_SET_VBAT_CV_CALIBRATION,(DCL_CTRL_DATA_T *)&val);
}


/*
* FUNCTION
*	   bmt_frequently_check_on_state
*
* DESCRIPTION                                                           
*   	This function is called to check whether "on state" need to be check frequently
*
* CALLS  
*
* PARAMETERS
*	   None
*	
* RETURNS
*	   KAL_TRUE: need check in this state
*	   KAL_FALSE: not need
*
* GLOBALS AFFECTED
*     None
*/
kal_bool bmt_frequently_check_on_state(void)
{
    if(BMT.pmictrl_state == PMIC_CHARGEON)
    {
        if((BMT.bat_state == CHR_PRE)  || 
            (BMT.bat_state == CHR_FAST) || 
      	    ((BMT.call_state != talk_mode) && (BMT.bat_state == CHR_TOPOFF)) ||
      	    ((BMT.call_state != talk_mode) && (BMT.bat_state == CHR_POSTFULL)) ||
      	    ((BMT.call_state != talk_mode) && (BMT.bat_state == CHR_BATFULL)) )
        {
            return KAL_TRUE;
        }
    }
    return KAL_FALSE;
}

#if defined(DRV_BMT_HW_PRECC_WORKAROUND)
/*
* FUNCTION
*	   bmt_frequently_check_off_state
*
* DESCRIPTION                                                           
*   	This function is called to check whether "off state" need to be check frequently
*
* CALLS  
*
* PARAMETERS
*	   None
*	
* RETURNS
*	   KAL_TRUE: need check in this state
*	   KAL_FALSE: not need
*
* GLOBALS AFFECTED
*     None
*/
kal_bool bmt_frequently_check_off_state(void)
{
    if(BMT.pmictrl_state == PMIC_CHARGEOFF)
    {
        if(BMT.bat_state == CHR_PRE)
        {
            return KAL_TRUE;
        }
    }
    return KAL_FALSE;
}
#endif // End of #if defined(DRV_BMT_HW_PRECC_WORKAROUND)

/*
* FUNCTION
*	   bmt_get_on_count
*
* DESCRIPTION                                                           
*   	This function is called to get the different count in each on state
*
* CALLS  
*
* PARAMETERS
*	   None
*	
* RETURNS
*	   kal_uint32: check count of current on state. 
*
* GLOBALS AFFECTED
*     None
*/
kal_uint32 bmt_get_on_count(void)
{
   kal_uint32 current_on_count = 0;
   switch(BMT.bat_state)
   {
      case 	CHR_PRE:
      	current_on_count = bmt_charging_para->PRE_TON*KAL_TICKS_1_SEC/45+1;
      	break;
      case 	CHR_FAST:
      	if(0 == BATTime.TON)
      		BATTime.TON = bmt_charging_para->TONOFFTABLE[0][CHRTON];
      	current_on_count = BATTime.TON*KAL_TICKS_1_SEC/45+1;
      	break;
      case 	CHR_TOPOFF:
      	current_on_count = bmt_charging_para->TOPOFF_TON*KAL_TICKS_1_SEC/45+1;
      	break;
#if defined(DRV_BMT_PULSE_CHARGING)
      case 	CHR_POSTFULL:
      	current_on_count = bmt_charging_para->BATPOSTFULL_TON_LI*KAL_TICKS_1_SEC/45+1;
		break;
#endif
      case 	CHR_BATFULL:
      	current_on_count = bmt_charging_para->BATFULL_TON_LI*KAL_TICKS_1_SEC/45+1;
      	break;
      default:
      	current_on_count = 1;
      	break;
   }
   drv_trace1(TRACE_GROUP_10, BMT_ERRORCHECK_ON_COUNT_TRC, current_on_count);   
   return current_on_count;
}

#if defined(DRV_BMT_HW_PRECC_WORKAROUND)
/*
* FUNCTION
*	   bmt_get_off_count
*
* DESCRIPTION                                                           
*   	This function is called to get the different count in each off state
*
* CALLS  
*
* PARAMETERS
*	   None
*	
* RETURNS
*	   kal_uint32: check count of current off state. 
*
* GLOBALS AFFECTED
*     None
*/
kal_uint32 bmt_get_off_count(void)
{
   kal_uint32 current_off_count = 0;
   switch(BMT.bat_state)
   {
      case 	CHR_PRE:
      	current_off_count = bmt_charging_para->PRE_TOFF*KAL_TICKS_1_SEC/45+1;
      	break;
      default:
      	current_off_count = 1;
      	break;
   }
   drv_trace1(TRACE_GROUP_10, BMT_ERRORCHECK_OFF_COUNT_TRC, current_off_count);
   return current_off_count;
}
#endif // End of #if defined(DRV_BMT_HW_PRECC_WORKAROUND)

/*
* FUNCTION
*	   bmt_finish_check_on_state
*
* DESCRIPTION                                                           
*   	This function is called to check whether "on state check" already finish
*
* CALLS  
*
* PARAMETERS
*	   None
*	
* RETURNS
*	   KAL_TRUE: finish
*	   KAL_FALSE: not finish
*
* GLOBALS AFFECTED
*     None
*/
kal_bool bmt_finish_check_on_state(void)
{
#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)
   static kal_uint8  pre_bat_state = 0xFF;
   static kal_uint32 state_counter = 0; //the counter during this ON state
   static kal_uint32 bmt_on_count  = 0; //the count of this ON state. (how many "200 ms")

   if(BMT.bat_state != pre_bat_state) //change state
   {
      bmt_on_count = bmt_get_on_count();
      pre_bat_state = BMT.bat_state;
      state_counter = 0;
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)      
      VCharge_Total = 0;
      drv_trace0(TRACE_GROUP_9, BMT_HIGH_VCHG_MAX_RESET_TO_ZERO_TRC); 
#endif      	  
   }
   
   if(state_counter == bmt_on_count)
   {
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT) 
      Pre_VCharge_AVG = VCharge_Total / bmt_on_count;
      bmt_vchg_compare_and_set_current(VCharge_Total / bmt_on_count, KAL_TRUE);
      drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_AVERAGE_TRC, VCharge_Total / bmt_on_count); 
      VCharge_Total = 0;
      Cur_VCharge_MAX = 0;
      drv_trace0(TRACE_GROUP_9, BMT_HIGH_VCHG_MAX_RESET_TO_ZERO_TRC);       
#endif    
   
      state_counter = 0;
      return KAL_TRUE;
   }
   else
   {
      state_counter++;
      return KAL_FALSE;
   }
#else
   return KAL_TRUE;
#endif //#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)
}

#if defined(DRV_BMT_HW_PRECC_WORKAROUND)
/*
* FUNCTION
*	   bmt_finish_check_on_state
*
* DESCRIPTION                                                           
*   	This function is called to check whether "on state check" already finish
*
* CALLS  
*
* PARAMETERS
*	   None
*	
* RETURNS
*	   KAL_TRUE: finish
*	   KAL_FALSE: not finish
*
* GLOBALS AFFECTED
*     None
*/
kal_bool bmt_finish_check_off_state(void)
{
#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)
   static kal_uint8  pre_bat_state = 0xFF;
   static kal_uint32 state_counter = 0; //the counter during this Off state
   static kal_uint32 bmt_off_count  = 0; //the count of this Off state. (how many "200 ms")

   if(BMT.bat_state != pre_bat_state) //change state
   {
      bmt_off_count = bmt_get_off_count();
      pre_bat_state = BMT.bat_state;
      state_counter = 0;
   }
   
   if(state_counter == bmt_off_count)
   {
      state_counter = 0;
      return KAL_TRUE;
   }
   else
   {
      state_counter++;
      return KAL_FALSE;
   }
#else
   return KAL_TRUE;
#endif //#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)
}
#endif // End of #if defined(DRV_BMT_HW_PRECC_WORKAROUND)

/*
* FUNCTION
*		bmt_measure_done
*
* DESCRIPTION																			  
*		This function is called when all parameters that charge algorithm needs
*  are all measured done.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
/*total channel measure done*/
void bmt_measure_done(void)
{
#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)
    if(bmt_frequently_check_on_state())
    {
        if(bmt_finish_check_on_state()) 
        {
            BMT_CHRFUNC[BMT.bat_state][BMT.pmictrl_state]();
        }
        else //check the error condition on CC
        {
            drv_trace0(TRACE_GROUP_10, BMT_ERRORCHECK_AT_CHARGE_ON_TRC);         
            BMT_CHRERRORCHECK();
        }
    }
#if defined(DRV_BMT_HW_PRECC_WORKAROUND)
    else if(bmt_frequently_check_off_state())
    {
        if(bmt_finish_check_off_state()) 
        {
            BMT_CHRFUNC[BMT.bat_state][BMT.pmictrl_state]();
        }
        else //check the error condition on CC
        {
            drv_trace0(TRACE_GROUP_10, BMT_ERRORCHECK_AT_CHARGE_OFF_TRC);   
            BMT_CHRERRORCHECK();
        }
    }
#endif // End of #if defined(DRV_BMT_HW_PRECC_WORKAROUND)
   else
#endif //#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)
	BMT_CHRFUNC[BMT.bat_state][BMT.pmictrl_state]();
}

/*
* FUNCTION
*		BMT_CallState
*
* DESCRIPTION																			  
*		This function is called by MMI to nodify talk/idle state to BMT task.
*
* CALLS  
*
* PARAMETERS
*		callState: 1, talk mode.
*					 0, idel mode.
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
void BMT_CallState(kal_uint8 callState)
{
	BMT.call_state = callState;	/*Talk time=1, otherwise = 0*/
	drv_trace1(TRACE_GROUP_10, BMT_CALL_STATE_TRC, callState);
}

/*
* FUNCTION
*		BMT_TONOFF
*
* DESCRIPTION																			  
*		This function is to obtain the TON/TOFF paramters in CHR_FAST mode.
*
* CALLS  
*
* PARAMETERS
*		PHY: measured physical parameters
*	  BATTime: the structure of TON/TOFF parameters
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
static void BMT_TONOFF(BATPHYStruct *PHY,CHRTStruct *pBATTime)////======
{
#ifndef __BMT_NO_ISENSE_RESISTOR__
	if(BMT.call_state == talk_mode)
	{
		if (PHY->ICHARGE > bmt_charging_para->FAST_ICHARGE_HIGHLEVEL)
		{
			pBATTime->TON=bmt_charging_para->TONOFFTABLE[3][CHRTON];
			pBATTime->TOFF=bmt_charging_para->TONOFFTABLE[3][CHRTOFF];
		}
		else
		{
			if (PHY->ICHARGE < bmt_charging_para->FAST_ICHARGE_LOWLEVEL)
			{
				pBATTime->TON=bmt_charging_para->TONOFFTABLE[5][CHRTON];
				pBATTime->TOFF=bmt_charging_para->TONOFFTABLE[5][CHRTOFF];
			}
			else
			{
				pBATTime->TON=bmt_charging_para->TONOFFTABLE[4][CHRTON];
				pBATTime->TOFF=bmt_charging_para->TONOFFTABLE[4][CHRTOFF];
			}
		}	
	}
	else
	{	 	
		if (PHY->ICHARGE > bmt_charging_para->FAST_ICHARGE_HIGHLEVEL)
		{
			pBATTime->TON=bmt_charging_para->TONOFFTABLE[0][CHRTON];
			pBATTime->TOFF=bmt_charging_para->TONOFFTABLE[0][CHRTOFF];
		}
		else
		{
			if (PHY->ICHARGE < bmt_charging_para->FAST_ICHARGE_LOWLEVEL)
			{
				pBATTime->TON=bmt_charging_para->TONOFFTABLE[2][CHRTON];
				pBATTime->TOFF=bmt_charging_para->TONOFFTABLE[2][CHRTOFF];
			}
			else
			{
				pBATTime->TON=bmt_charging_para->TONOFFTABLE[1][CHRTON];
				pBATTime->TOFF=bmt_charging_para->TONOFFTABLE[1][CHRTOFF];
			}
		}
	}	
#else // #ifndef __BMT_NO_ISENSE_RESISTOR__
	pBATTime->TON=bmt_charging_para->TONOFFTABLE[0][CHRTON];
	pBATTime->TOFF=bmt_charging_para->TONOFFTABLE[0][CHRTOFF];
#endif // #ifndef __BMT_NO_ISENSE_RESISTOR__
}


/*
* FUNCTION
*		BMT_CtrlUPMUCharge
*
* DESCRIPTION																			  
*		This function is called to control the SW of charge
*
* CALLS  
*
* PARAMETERS
*		ON: 1, charge on	  0, charge off
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
static void BMT_CtrlUPMUCharge(kal_bool ON)
{

	PMU_CTRL_CHR_SET_CSDAC_EN chr_csdac_en;
    PMU_CTRL_CHR_SET_CV_DETECTION_EN set_cv_detect_en;
    PMU_CTRL_CHR_SET_CV_DETECTION_VOLTAGE set_cv_detect_vol;
     
	if(ON == KAL_TRUE)
	{
   

        set_cv_detect_vol.voltage = PMU_VOLT_04_200000_V;// CHR_VBAT_CV_4_2000V
        DclPMU_Control(bmt_PmuHandler, CHR_SET_CV_DETECTION_VOLTAGE, (DCL_CTRL_DATA_T *)&set_cv_detect_vol);

    	set_cv_detect_en.enable = ON;
    	DclPMU_Control(bmt_PmuHandler, CHR_SET_CV_DETECTION_EN, (DCL_CTRL_DATA_T *)&set_cv_detect_en);
    	
	}


	DclPMU_Control(bmt_PmuHandler, CHR_SET_WDT_CLEAR, NULL);	

	chr_csdac_en.enable = ON;
	DclPMU_Control(bmt_PmuHandler, CHR_SET_CSDAC_EN, (DCL_CTRL_DATA_T *)&chr_csdac_en);


	BMT_Charge(ON);
}


/*
* FUNCTION
*		BMT_CtrlCharge
*
* DESCRIPTION																			  
*		This function is called to control the SW of charge
*
* CALLS  
*
* PARAMETERS
*		ON: 1, charge on	  0, charge off
*	  VBAT: the measured voltage of battery.
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
extern kal_bool bmt_sleepmode;
static void BMT_CtrlCharge(kal_uint8 ON, kal_uint32 VBAT)
{
#if defined(MT6236)

  	PMU_CTRL_CHR_SET_CHR_CURRENT set_chr_current;    
#endif

#if defined(__CHARGER_SOFT_START__)
	kal_uint32 curLevel = 0,i;
	PMU_CTRL_CHR_GET_CHR_CURRENT get_chr_current;
    PMU_CTRL_CHR_GET_CHR_CURRENT_LIST get_chr_current_list;

  	PMU_CTRL_CHR_SET_CHR_CURRENT set_chr_current;       
#endif //#if defined(__CHARGER_SOFT_START__)
	/*Prevent Charge Li Battery at CV mode*/
//	if ((((kal_bool)ON == KAL_TRUE) &&
//		(BMT.call_state == talk_mode) &&
//		(VBAT >= bmt_charging_para->V_PROTECT_HIGH_LI) &&
//		(BMT.BatType == LIBAT)) || (BMT.bat_state == CHR_ERROR)
//		)

#if defined(__DRV_BMT_SLEEPMODE_ENHANCE__)
	if (ON==KAL_TRUE && bmt_sleepmode==KAL_TRUE)
	{
		ASSERT(0);
	}
#endif //#if defined(__DRV_BMT_SLEEPMODE_ENHANCE__)

    drv_trace1(TRACE_GROUP_1, BMT_CTRL_CHARGE_TRC, ON);

	// If bat_state is HOLD, we do NOT enable chr_en
	if ( (BMT.bat_state == CHR_HOLD) || (BMT.bat_state == CHR_ERROR) )
	{
#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)
		bmt_chr_force_enable(KAL_FALSE);
#endif ////#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)
		BMT.pmictrl_state = PMIC_CHARGEOFF;
		BMT_CtrlUPMUCharge(KAL_FALSE);
		return;
	}

#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)
	bmt_chr_force_enable((kal_bool)ON);
#endif ////#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)


	if ((kal_bool)ON == KAL_TRUE)
	{
		if (BMT.pmictrl_state == PMIC_CHARGEOFF)
		{      
			BMT.pmictrl_state = PMIC_CHARGEON;
			// Fast plug in/out may cause HW disable current level, but SW do not receive interrupt.
			// Under such scenario the current level will incorrect.
			// Enforce set correct current level every time charge ON.


#if defined(DRV_BMT_PULSE_CHARGING) || defined(DRV_BMT_PULSE_CV_CHARGING)
            if(BMT.bat_state != CHR_PRE)
            {
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
			if(bmt_IsUSBorCharger() != BMT_USB_IN)	
			{
				if(First_Time_Charge_Enable == KAL_TRUE)
				{
				    drv_trace0(TRACE_GROUP_9, BMT_HIGH_VCHG_FIRST_TIME_CHARGER_ON_TRC);	
				    bmt_high_vchg_first_time_set();
				    First_Time_Charge_Enable = KAL_FALSE;
            }

#if defined(__CHINA_CHARGER_STANDARD__)
				if(chr_usb_detect.chr_usb_present == CHARGER_PRESENT_NON)
				{
				    if(bmt_high_vchg_current > PMU_CHARGE_CURRENT_450_00_MA)
            {
				        bmt_high_vchg_current = PMU_CHARGE_CURRENT_450_00_MA;
				        drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_SET_USB_CHARGE_CURRENT_TRC, CHR_USB_CHARGE_CURRENT);	
				    }
				}
#endif //#if defined(__CHINA_CHARGER_STANDARD__)
    	        
				drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_CHARGE_CURRENT_TRC, bmt_high_vchg_current);	

				bmt_find_and_set_the_nearest_current(bmt_high_vchg_current);
				
				Cur_VCharge_MAX = 0;
				drv_trace0(TRACE_GROUP_9, BMT_HIGH_VCHG_MAX_RESET_TO_ZERO_TRC);	
                              
            }
            else
            {
			    drv_trace0(TRACE_GROUP_9, BMT_HIGH_VCHG_USB_IN_USE_CUSTOM_CHARGE_CURRENT_TRC);	
				bmt_set_chr_current();
			}
    	        
#else // Else of #if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
			bmt_set_chr_current();
#endif // #if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)        
            }
		else
		{
#if defined(MT6236)		
			bmt_find_and_set_the_nearest_current(PMU_CHARGE_CURRENT_200_00_MA);
#else
			bmt_set_chr_current(); 
#endif // #if defined(MT6236)
		}
#endif //#if defined(DRV_BMT_PULSE_CHARGING)	


#if defined(__CHARGER_SOFT_START__)

	        
	        DclPMU_Control(bmt_PmuHandler, CHR_GET_CHR_CURRENT, (DCL_CTRL_DATA_T *)&get_chr_current);
            DclPMU_Control(bmt_PmuHandler, CHR_GET_CHR_CURRENT_LIST, (DCL_CTRL_DATA_T *)&get_chr_current_list);
	        
			// curLevel = get_chr_current.current;
            while(1)
            {
                if(get_chr_current_list.pCurrentList[curLevel] == get_chr_current.current)
                    break;
                curLevel++;
            }
            drv_trace2(TRACE_GROUP_5, BMT_CHARGE_CURRENT_AND_LEVEL_TRC, get_chr_current.current, curLevel);	
            drv_trace1(TRACE_GROUP_5, BMT_TOTAL_CHARGE_CURRENT_LEVEL_TRC, get_chr_current_list.number);
            drv_trace1(TRACE_GROUP_5, BMT_MIN_CHARGE_CURRENT_TRC, get_chr_current_list.pCurrentList[0]);	                        
            drv_trace1(TRACE_GROUP_5, BMT_MAX_CHARGE_CURRENT_TRC, get_chr_current_list.pCurrentList[get_chr_current_list.number - 1]);
            set_chr_current.current = (PMU_CHR_CURRENT_ENUM)get_chr_current_list.pCurrentList[1];
            DclPMU_Control(bmt_PmuHandler, CHR_SET_CHR_CURRENT, (DCL_CTRL_DATA_T *)&set_chr_current);
            
#endif //#if defined(__CHARGER_SOFT_START__)
			BMT_CtrlUPMUCharge(KAL_TRUE);
#if defined(__CHARGER_SOFT_START__)
  	        
			for(i=2;i<=curLevel;i++)
			{
				kal_sleep_task(1); // delay 1 frames
                set_chr_current.current = (PMU_CHR_CURRENT_ENUM)get_chr_current_list.pCurrentList[i];
                DclPMU_Control(bmt_PmuHandler, CHR_SET_CHR_CURRENT, (DCL_CTRL_DATA_T *)&set_chr_current);
				drv_trace1(TRACE_GROUP_5, BMT_SOFT_START_CURRENT_LEVEL, i);	
                drv_trace1(TRACE_GROUP_5, BMT_CURRENT_CHARGE_CURRENT_TRC, get_chr_current_list.pCurrentList[i]);	
			}
                				            
#endif //#if defined(__CHARGER_SOFT_START__)
		}
		else
		{
      	 		drv_trace0(TRACE_GROUP_6, BMT_CHARGING_ALREADY_ON_TRC);
#if defined(DRV_BMT_PULSE_CV_CHARGING)
			BMT_CtrlUPMUCharge(KAL_TRUE);
#endif //#if defined(DRV_BMT_PULSE_CV_CHARGING)				
      		}
	}
	else
	{
		BMT.pmictrl_state = PMIC_CHARGEOFF;
		BMT_CtrlUPMUCharge(KAL_FALSE);
	}
}

/*
* FUNCTION
*		BMT_PhyCheck
*
* DESCRIPTION																			  
*		This function is to check whether the measured physical
*	  parameters is over the physical limitation.
*
* CALLS  
*
* PARAMETERS
*		Data: None
*	
* RETURNS
*		BATPHYS: measured physical parameters
*
* GLOBALS AFFECTED
*	  None
*/


#define VCHARGER_LOW_CHECK_COUNT			6		// VCHARGER low check count
#define VCHARGER_OVER_CHECK_COUNT		  6		// VCHARGER high check count
#define OVER_CURRENT_CHECK_COUNT			3		// Over current check count
#define LOW_CURRENT_CHECK_COUNT			 5		// Low current check count
#define OVER_BATTEMP_CHECK_COUNT			5		// Over battery temperature check count
#define LOW_BATTEMP_CHECK_COUNT			 5		// Low battery temperature check count

// Return KAL_TRUE: Phys status is GOOD
// Return KAL_FALSE: Phys status is BAD, should go to chr error state
static kal_bool BMT_PhyCheck_VCharger(BATPHYStruct *BATPHYS)
{
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
	if ( (BATPHYS->VCHARGER > bmt_high_vchg_para->HIGH_VCHG_TABLE[VCHG_VOL_LEVEL - 1][0]) )
#else
	if ( (BATPHYS->VCHARGER > bmt_charging_para->VCHARGER_HIGH) )
#endif // End of #if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)   
	{
		over_charger_count ++;
		drv_trace1(TRACE_GROUP_6, BMT_PHY_CHECK_OVER_CHARGER_COUNT_TRC, over_charger_count);	
	}
	else
	{
		over_charger_count = 0;
	}
	if ( (BATPHYS->VCHARGER < bmt_charging_para->VCHARGER_LOW) )
	{
		low_charger_count++;
	}
	else
	{			
		low_charger_count = 0;
	}
	if ( (low_charger_count > VCHARGER_LOW_CHECK_COUNT) || (over_charger_count > VCHARGER_OVER_CHECK_COUNT) )
	{
		low_charger_count = 0;
		return KAL_FALSE;
	}
	return KAL_TRUE;
}

#ifndef __BMT_NO_ISENSE_RESISTOR__
// Return KAL_TRUE: Phys status is GOOD
// Return KAL_FALSE: Phys status is BAD, should go to chr error state
static kal_bool BMT_PhyCheck_OverCurrent(BATPHYStruct *BATPHYS)
{
	if (BMT.pmictrl_state == PMIC_CHARGEOFF)
	{
		#if defined(__DRV_BMT_ISENSE_OFFSET_COMPENSATION__)
		if(BATPHYS->ICHARGE > bmt_charging_para->ICHARGE_OFF_HIGH || BATPHYS->ISense_Offset > (bmt_charging_para->ICHARGE_OFF_HIGH / 5))	// x0.2 Ohm resistor
		#else
		if(BATPHYS->ICHARGE > bmt_charging_para->ICHARGE_OFF_HIGH)
		#endif	
		{
			over_current_count++;
			drv_trace1(TRACE_GROUP_6, BMT_PHY_CHECK_OVER_CURRENT_COUNT_TRC, over_current_count);	
		}
		else
		{				
			over_current_count = 0;	
		}
	}
	else
	{
		if(BATPHYS->ICHARGE > bmt_charging_para->ICHARGE_ON_HIGH)
		{
			over_current_count++;
			drv_trace1(TRACE_GROUP_6, BMT_PHY_CHECK_OVER_CURRENT_COUNT_TRC, over_current_count);	
		}
		else
		{				
			over_current_count = 0;	  
		}	
	}
	if (over_current_count > OVER_CURRENT_CHECK_COUNT)
	{
		over_current_count = 0;
		return KAL_FALSE;
		
	}
	return KAL_TRUE;
}

// Return KAL_TRUE: Phys status is GOOD
// Return KAL_FALSE: Phys status is BAD, should go to chr error state
static kal_bool BMT_PhyCheck_LowCurrent(BATPHYStruct *BATPHYS)
{
	if (BMT.pmictrl_state != PMIC_CHARGEOFF)
	{
		if ( (BMT.bat_state == CHR_FAST)) //|| (BMT.bat_state == CHR_PRE) )
		{
#if !defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__) && !defined(DRV_BMT_PULSE_CV_CHARGING)
			if(BATPHYS->ICHARGE < bmt_charging_para->ICHARGE_ON_LOW)
			{
				low_current_count++;
				drv_trace1(TRACE_GROUP_6, BMT_PHY_CHECK_LOW_CURRENT_COUNT_TRC, low_current_count);	
			}
			else
			{				
				low_current_count = 0;
			}
			if (low_current_count > LOW_CURRENT_CHECK_COUNT)
			{
				low_current_count = 0;
				return KAL_FALSE;
			}
#endif //#if !defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)
		}//if ( (BMT.bat_state == CHR_FAST) ) //|| (BMT.bat_state == CHR_PRE) )
	}//if (BMT.pmictrl_state != PMIC_CHARGEOFF)
	return KAL_TRUE;
}
#endif // End of #ifndef __BMT_NO_ISENSE_RESISTOR__

// Return KAL_TRUE: Phys status is GOOD
// Return KAL_FALSE: Phys status is BAD, should go to chr error state
static kal_bool BMT_PhyCheck_VBat(BATPHYStruct *BATPHYS)
{
#if defined(CHR_WITH_LI_ION_BATTERY)    
	if((BATPHYS->VBAT > bmt_charging_para->MAX_VBAT_LI) && (BMT.BatType == LIBAT) )
	{
		return KAL_FALSE;
	}
#endif 
   
#if defined(CHR_WITH_NI_MH_BATTERY)
	if ((BATPHYS->VBAT > bmt_charging_para->MAX_VBAT_NI) && (BMT.BatType == NIBAT) )
	{
		return KAL_FALSE;
	}
#endif    
	return KAL_TRUE;
}

// Return KAL_TRUE: Phys status is GOOD
// Return KAL_FALSE: Phys status is BAD, should go to chr error state
static kal_bool BMT_PhyCheck_LowBatTemp(BATPHYStruct *BATPHYS)
{
	if (BATPHYS->BATTMP > bmt_charging_para->BATTMP_0C && BATPHYS->BATTMP < bmt_charging_para->BATTMP_MINUS_40C)  /*battery temperature < 0C*/
	{
		low_temper_count ++;
		drv_trace1(TRACE_GROUP_6, BMT_PHY_CHECK_LOW_TRMPER_COUNT_TRC, low_temper_count);	
	}
	else
	{
		low_temper_count = 0;
	}
	
	if (low_temper_count > LOW_BATTEMP_CHECK_COUNT)
	{
		return KAL_FALSE;
	}
	
	return KAL_TRUE;
}

// Return KAL_TRUE: Phys status is GOOD
// Return KAL_FALSE: Phys status is BAD, should go to chr error state
static kal_bool BMT_PhyCheck_HighBatTemp(BATPHYStruct *BATPHYS)
{
	/*y=69.65-47.4x*/
	if (BATPHYS->BATTMP < bmt_charging_para->BATTMP_45C)	/*battery temperature > 45C*/
	{
		over_temper_count++;
		drv_trace1(TRACE_GROUP_6, BMT_PHY_CHECK_OVER_TEMPER_COUNT_TRC, over_temper_count);	
	}
	else
	{
		over_temper_count = 0;
	}
	
	if (over_temper_count > OVER_BATTEMP_CHECK_COUNT)
	{
		over_temper_count = 0;
		return KAL_FALSE;
	}
	
	return KAL_TRUE;
}

// Return KAL_TRUE: Phys status is GOOD
// Return KAL_FALSE: Phys status is BAD, should go to chr error state
static kal_bool BMT_PhyCheck_BadBattContact(BATPHYStruct *BATPHYS)
{
	if (BATPHYS->BATTMP > bmt_charging_para->BATTMP_MINUS_40C)
	{
		return KAL_FALSE;
	}
	
	return KAL_TRUE;
	
}

// KAL_TRUE: Should enter charge HOLD state
// KAL_FALSE: Should NOT enter charge HOLD state
static kal_bool BMT_PhyCheck_EnterChargeHold(BATPHYStruct *BATPHYS)
{
	if ((BMT.call_state == talk_mode) &&
		(BATPHYS->VBAT >= bmt_charging_para->V_PROTECT_HIGH_LI) &&
		(BMT.BatType == LIBAT) )
	{
		if (BMT.pmictrl_state == PMIC_CHARGEOFF ||
			(BMT.pmictrl_state == PMIC_CHARGEON) &&
			(BMT.bat_state == CHR_TOPOFF || BMT.bat_state == CHR_BATFULL || BMT.bat_state == CHR_POSTFULL))
		{
			return KAL_TRUE;
		}
	}
	
	return KAL_FALSE;
	
}

// KAL_TRUE:  Should turn off safety timer
// KAL_FALSE: Should turn on safety timer
static kal_bool BMT_PhyCheck_DisableSafetyTimer(void)
{
	//In talking mode, the power consumption may be large and cause long time to complete charging
	if (BMT.call_state == talk_mode)
	{
		return KAL_TRUE;
	}
	
	// When in hold state, the safety timer should be disabled
	// Because we do NOT perform charging
	if (BMT.bat_state == CHR_HOLD)
	{
		return KAL_TRUE;
	}
	
	return KAL_FALSE;
}

static void BMT_PhyCheck(BATPHYStruct *BATPHYS)
{
	if (bmt_charging_para->bmt_check_charger)
	{
		if (BMT_PhyCheck_VCharger(BATPHYS) == KAL_FALSE)
		{
			drv_trace0(TRACE_GROUP_10, BMT_INVALID_CHARGER_TRC);
			BMT_sendMes2UEM(BMT_INVALID_CHARGER);
			BMT.bat_state = CHR_ERROR;
			return;
		}
	}
	
#ifndef __BMT_NO_ISENSE_RESISTOR__
	if (BMT_PhyCheck_OverCurrent(BATPHYS) == KAL_FALSE)
	{
		drv_trace0(TRACE_GROUP_10, BMT_ICHARGE_OVER_TRC);
		BMT_sendMes2UEM(BMT_OVERCHARGECURRENT);
		BMT.bat_state = CHR_ERROR;
		return;
	}
	
	if (BMT_PhyCheck_LowCurrent(BATPHYS) == KAL_FALSE)
	{
		drv_trace0(TRACE_GROUP_10, BMT_ICHARGE_LOW_TRC);
		BMT_sendMes2UEM(BMT_LOWCHARGECURRENT);
		BMT.bat_state = CHR_ERROR;
		return;
	}
#endif // #ifndef __BMT_NO_ISENSE_RESISTOR__
	
	if (BMT_PhyCheck_VBat(BATPHYS) == KAL_FALSE)
	{
		drv_trace0(TRACE_GROUP_10, BMT_VBAT_OVER_TRC);
		BMT_sendMes2UEM(BMT_OVERVOLPROTECT);
		BMT.bat_state = CHR_ERROR;
		return;
	}
	
	if (bmt_charging_para->bmt_check_temp)
	{
		if (BMT_PhyCheck_HighBatTemp(BATPHYS) == KAL_FALSE)
		{
			drv_trace0(TRACE_GROUP_10, BMT_VTEMP_OVER_TRC);
			BMT_sendMes2UEM(BMT_OVERBATTEMP);
			BMT.bat_state = CHR_ERROR;
			return;
		}
		
		if (BMT_PhyCheck_LowBatTemp(BATPHYS) == KAL_FALSE)
		{
			drv_trace0(TRACE_GROUP_10, BMT_VTEMP_LOW_TRC);
			BMT_sendMes2UEM(BMT_LOWBATTEMP);
			BMT.bat_state = CHR_ERROR;
			return;
		}
		
		if (BMT_PhyCheck_BadBattContact(BATPHYS) == KAL_FALSE)
		{
			BMT_sendMes2UEM(BMT_BATTERY_BAD_CONTACT);
			BMT.bat_state = CHR_ERROR;
			return;
		}
	}		
	
	
	if (BMT_PhyCheck_DisableSafetyTimer())
	{
		// We should disable safety timer
		if (SaftyTimer_Flag == BMT_SAFETY_TIMER_ON)
		{	
			stack_stop_timer(&ChargeTimeout_timer);
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
            drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, bmt_total_charge_time);
#else
			drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, BMT_TOTAL_CHARGE_TIME);
#endif			
			SaftyTimer_Flag = BMT_SAFETY_TIMER_OFF; 
		}  
	}
	else
	{
		// We should enable safety timer
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
        if (SaftyTimer_Flag == BMT_SAFETY_TIMER_OFF && bmt_safety_timer_config == KAL_TRUE)
#else
		if (SaftyTimer_Flag == BMT_SAFETY_TIMER_OFF)
#endif         
		{	
#ifndef __BMT_CHARGE_GUARD_TIME__
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
            stack_start_timer(&ChargeTimeout_timer, 0, KAL_TICKS_1_MIN*bmt_total_charge_time);
            drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_START_TRC, bmt_total_charge_time);
#else
			stack_start_timer(&ChargeTimeout_timer, 0, KAL_TICKS_1_MIN*BMT_TOTAL_CHARGE_TIME);
			drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_START_TRC, BMT_TOTAL_CHARGE_TIME);
#endif
#endif
			drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_START_TRC, BMT_TOTAL_CHARGE_TIME);
			SaftyTimer_Flag = BMT_SAFETY_TIMER_ON; 
		}	
	}
	
	// Decide whether to enter HOLD state or NOT
	if (BMT_PhyCheck_EnterChargeHold(BATPHYS) == KAL_TRUE)
	{
		drv_trace0(TRACE_GROUP_10, BMT_CHR_HOLD_TRC);
		BMT.bat_state = CHR_HOLD;
		return;
	}
	
}

kal_bool BMT_Host_CHR_Is_Charge_Full(BATPHYStruct *pBATPHYS)
{
#ifndef __BMT_NO_ISENSE_RESISTOR__
	if (pBATPHYS->ICHARGE < bmt_charging_para->I_TOPOFF2FULL_THRES)
	{
		return KAL_TRUE;
	}
	else
	{
		return KAL_FALSE;
	}
#else	
	ASSERT(0); // When this function is called, we should have some operations to check charge full
	return KAL_FALSE;
#endif // #ifndef __BMT_NO_ISENSE_RESISTOR__
}

kal_bool BMT_CHR_Is_Charge_Full(BATPHYStruct *pBATPHYS)
{
	// Without external charger
#ifndef __GENERAL_EXTERNAL_CHARGER__
	return BMT_Host_CHR_Is_Charge_Full(pBATPHYS);
#endif // #ifndef __GENERAL_EXTERNAL_CHARGER__
	
	// With external charger
#if defined(__GENERAL_EXTERNAL_CHARGER__)
	if (BMT_CHR_FACTOR_CHECK_BY_CHARGER_DEV == default_get_chr_fac_check_support(BMT_CHR_FACTOR_CHARGE_FULL))
	{
		// Query external charger
		return ext_charger->get_charge_full(pBATPHYS->VBAT);
	}
	else
	{
		return BMT_Host_CHR_Is_Charge_Full(pBATPHYS);
	}
#endif // #if defined(__GENERAL_EXTERNAL_CHARGER__)
}

static kal_bool BMT_CHR_Is_Under_ExtremeTmp(BATPHYStruct *pBATPHYS)
{
	if (bmt_charging_para->bmt_check_temp == KAL_FALSE)
	{
		return KAL_FALSE;
	}
#if defined(__GENERAL_EXTERNAL_CHARGER__)
	// With external charger
	if (BMT_CHR_FACTOR_CHECK_BY_CHARGER_DEV == default_get_chr_fac_check_support(BMT_CHR_FACTOR_UNDER_EXTREMETMP))
	{
		kal_bool ret;
		
		// Query external charger
		ret = ext_charger->get_charge_under_extremetmp(pBATPHYS->BATTMP);
		if (KAL_TRUE == ret)
		{
			drv_trace0(TRACE_GROUP_10, BMT_CHR_UNDER_EXTREMETMP_TRC);
		}
		return ret;
	}
	else
	{
		return KAL_FALSE;
	}
#else // #if defined(__GENERAL_EXTERNAL_CHARGER__)
	// Without external charger
	return KAL_FALSE;
#endif // #if defined(__GENERAL_EXTERNAL_CHARGER__)
}

static kal_bool BMT_CHR_Is_ExtremeTmp_Charge_Full(BATPHYStruct *pBATPHYS)
{
	if (bmt_charging_para->bmt_check_temp == KAL_FALSE)
	{
		return KAL_FALSE;
	}
	
#if defined(__GENERAL_EXTERNAL_CHARGER__)
	// With external charger
	if (BMT_CHR_FACTOR_CHECK_BY_CHARGER_DEV == default_get_chr_fac_check_support(BMT_CHR_FACTOR_CHARGE_EXTREMETMP_FULL))
	{
		kal_bool ret;
		
		// Query external charger
		ret = ext_charger->get_charge_extremetmp_full(pBATPHYS->VBAT);
		if (KAL_TRUE == ret)
		{
			drv_trace0(TRACE_GROUP_10, BMT_CHR_EXTREMETMP_BATFULL_CHANGE_TRC);
#ifndef __DRV_BMT_CHARGING_COMPLETE_MSG__
			BMT_sendMes2UEM(BMT_CHARGE_COMPLETE);
#endif
			BMT.bat_state = CHR_BATFULL;
			bmt_stop_stoptimer();
			stack_stop_timer(&ChargeTimeout_timer);
#ifdef __BMT_CHARGE_GUARD_TIME__
			bmt_stop_guardtimer();
#endif // #ifdef __BMT_CHARGE_GUARD_TIME__
#if defined(__DRV_BMT_ESTIMATIVE_TIMER_ON_TOPOFF__)
			bmt_stop_estimativetimer();
#endif //#if defined(__DRV_BMT_ESTIMATIVE_TIMER_ON_TOPOFF__)
			BMT_CHARSTOP();
		}
		return ret;
	}
	else
	{
		return KAL_FALSE;
	}
#else // #if defined(__GENERAL_EXTERNAL_CHARGER__)
	// Without external charger
	return KAL_FALSE;
#endif // #if defined(__GENERAL_EXTERNAL_CHARGER__)
	
}

static kal_bool BMT_CHR_Is_ExtremeTmp_Charge_Recharge(BATPHYStruct *pBATPHYS)
{
	if (bmt_charging_para->bmt_check_temp == KAL_FALSE)
	{
		return KAL_FALSE;
	}
	
#if defined(__GENERAL_EXTERNAL_CHARGER__)
	// With external charger
	if (BMT_CHR_FACTOR_CHECK_BY_CHARGER_DEV == default_get_chr_fac_check_support(BMT_CHR_FACTOR_CHARGE_EXTREMETMP_RECHARGE))
	{
		kal_bool ret;
		
		// Query external charger
		ret = ext_charger->get_charge_extremetmp_recharge(pBATPHYS->VBAT);
		if (KAL_TRUE == ret)
		{
			drv_trace0(TRACE_GROUP_10, BMT_CHR_EXTREMETMP_CHARGE_RECHARGE_TRC);
		}
		return ret;
	}
	else
	{
		return KAL_FALSE;
	}
#else // #if defined(__GENERAL_EXTERNAL_CHARGER__)
	// Without external charger
	return KAL_FALSE;
#endif // #if defined(__GENERAL_EXTERNAL_CHARGER__)
	
}

/*
* FUNCTION
*		BMT_CHRPRE_ON
*
* DESCRIPTION																			  
*		This function is called when charger is on, and BMT charge module 
*	  is at CHR_PRE state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
static void BMT_CHRPRE_ON(void)
{
	BATPHYStruct BATPHYS;
	drv_trace0(TRACE_GROUP_10, BMT_CHRPRE_ON_STATE_TRC);
	if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	{
		BMT_PhyCheck(&BATPHYS);
		BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);	//Turn off Charge
	}
	bmt_timer_config(bmt_charging_para->PRE_TOFF*CHARGING_TIME_UNIT);
}

/*
* FUNCTION
*		BMT_CHRPRE_OFF
*
* DESCRIPTION																			  
*		This function is called when charger is off, and BMT charge module 
*	  is at CHR_PRE state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
static void BMT_CHRPRE_OFF(void)
{
	BATPHYStruct BATPHYS;
	#if defined(PMIC_PRESENT)
	
    #endif
	drv_trace0(TRACE_GROUP_10, BMT_CHRPRE_OFF_STATE_TRC);
	if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is off period
	{
		BMT_PhyCheck(&BATPHYS);
		if ((BMT.bat_state != CHR_ERROR) 
			&& (BMT_CHR_Is_Under_ExtremeTmp(&BATPHYS) == KAL_TRUE) 
			&& (BMT_CHR_Is_ExtremeTmp_Charge_Full(&BATPHYS) == KAL_TRUE))
		{
			return;
		}
		if( ((BATPHYS.VBAT) > bmt_charging_para->V_PRE2FAST_THRES) && (BMT.bat_state !=CHR_ERROR)&&(BMT.bat_state != CHR_HOLD))
		{
#if defined(DRV_BMT_PULSE_CHARGING)            
            if(BMT_CHRPRE_TO_CHRBATFULL_CHECK())
            {
				return;				
			}
#else

#if defined(DRV_BMT_PULSE_CV_CHARGING)
			if(BATPHYS.VBAT > CHR_V_PRE2FULL_THRES)
			{
				BMT_CHRPRE_TO_CHRBATFULL();
				return;				
			}
		
#endif //#if defined(DRV_BMT_PULSE_CV_CHARGING)

			BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);
#endif // #if defined(DRV_BMT_PULSE_CHARGING)

			BMT.bat_state = CHR_FAST;
			if(bmt_Get_PowerOn_Type()== (kal_uint8)PRECHRPWRON)//PRECHRPWRON
			{
				ilm_struct *BMT_ilm;				
				/*change power on type as charger power on*/						 
				bmt_Modify_PowerOn_Type((kal_uint8)CHRPWRON);
				#ifndef L4_NOT_PRESENT
				#if defined(PMIC_PRESENT)
            	
            	DclPW_Control(bmt_PmuHandler, PW_CMD_UPDATE_FLAGS, NULL);
            	               
		  
                #endif // #if defined(PMIC_PRESENT)
#endif // #ifndef L4_NOT_PRESENT
				DRV_BuildPrimitive(BMT_ilm, MOD_BMT, MOD_UEM, 
										MSG_ID_BMT_LEAVE_PRECHARGE_IND,NULL);
				msg_send_ext_queue(BMT_ilm);										
			}							
			drv_trace0(TRACE_GROUP_10, BMT_FAST_CHARGE_CHANGE_TRC);
		}
		else
		{
			BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);	//Turn on Charge
		}
	}
	bmt_timer_config(bmt_charging_para->PRE_TON*CHARGING_TIME_UNIT);
}

/*
* FUNCTION
*		BMT_CHRFAST_ON
*
* DESCRIPTION																			  
*		This function is called when charger is on, and BMT charge module 
*	  is at CHR_FAST state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
static void BMT_CHRFAST_ON(void)
{
	BATPHYStruct BATPHYS;
	drv_trace0(TRACE_GROUP_10, BMT_FAST_ON_STATE_TRC);
	if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	{
		BMT_PhyCheck(&BATPHYS);
		BMT_TONOFF(&BATPHYS,&BATTime);
		if(BMT.bat_state != CHR_ERROR)
		{
#if defined(CHR_WITH_NI_MH_BATTERY)            
			//Change state
			if (BMT.BatType == NIBAT)
			{
				if ( ((VBATtmp - BATPHYS.VBAT) > 100000) && (VBATtmp != 0) )
				{
					BMT.bat_state = CHR_BATFULL;
					bmt_stop_stoptimer();
					stack_stop_timer(&ChargeTimeout_timer);
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
					drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, bmt_total_charge_time);
#else
					drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, BMT_TOTAL_CHARGE_TIME);
#endif					/*30min*/
					bmt_set_stoptimer(STOPTIMER_TIMEOUT_TICK);
					VBATtmp = 0;
					BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);
					BMT_sendMes2UEM(BMT_CHARGE_COMPLETE);
					drv_trace0(TRACE_GROUP_10, BMT_CHR_BATFULL_CHANGE_TRC);
					bmt_timer_config(BATTime.TOFF*CHARGING_TIME_UNIT);
					return;
				}

				if (BATPHYS.BATTMP < bmt_charging_para->V_TEMP_FAST2FULL_THRES_NI)
				{
					BMT.bat_state = CHR_BATFULL;
					bmt_stop_stoptimer();
					stack_stop_timer(&ChargeTimeout_timer);
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
					drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, bmt_total_charge_time);
#else
					drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, BMT_TOTAL_CHARGE_TIME);
#endif					/*30min*/
					bmt_set_stoptimer(STOPTIMER_TIMEOUT_TICK);
					BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);
					BMT_sendMes2UEM(BMT_CHARGE_COMPLETE);
					bmt_timer_config(BATTime.TOFF*CHARGING_TIME_UNIT);
					drv_trace0(TRACE_GROUP_10, BMT_CHR_BATFULL_CHANGE_TRC);
					return;
				}
				VBATtmp = BATPHYS.VBAT;
			}
#endif // End of #if defined(CHR_WITH_NI_MH_BATTERY)
		}
		BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);	//Turn off Charge
	}
	bmt_timer_config(BATTime.TOFF*CHARGING_TIME_UNIT);
}

/*
* FUNCTION
*		BMT_CHRFAST_OFF
*
* DESCRIPTION																			  
*		This function is called when charger is off, and BMT charge module 
*	  is at CHR_FAST state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
static void BMT_CHRFAST_OFF(void)
{
	BATPHYStruct BATPHYS;
	drv_trace0(TRACE_GROUP_10, BMT_FAST_OFF_STATE_TRC);
	if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	{
		BMT_PhyCheck(&BATPHYS);
		if ((BMT.bat_state != CHR_ERROR) 
			&& (BMT_CHR_Is_Under_ExtremeTmp(&BATPHYS) == KAL_TRUE)
			&& (BMT_CHR_Is_ExtremeTmp_Charge_Full(&BATPHYS) == KAL_TRUE))
		{
			return;
		}
		if((BMT.bat_state != CHR_ERROR)&&(BMT.bat_state != CHR_HOLD))
		{
			if ((BMT.BatType == LIBAT) &&
				 (BATPHYS.VBAT > bmt_charging_para->V_FAST2TOPOFF_THRES)
				)
			{
				BMT.bat_state = CHR_TOPOFF;
				BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);
				drv_trace0(TRACE_GROUP_10, BMT_CHR_TOPOFF_CHANGE_TRC);
				bmt_timer_config(BATTime.TON*CHARGING_TIME_UNIT);
#if defined(__DRV_BMT_ESTIMATIVE_TIMER_ON_TOPOFF__) 
				bmt_set_estimativetimer(ESTIMATIVE_TIMEOUT_TICK);
#endif //#if defined(__DRV_BMT_ESTIMATIVE_TIMER_ON_TOPOFF__) 
				return;
			}
#if defined(CHR_WITH_NI_MH_BATTERY)
			if ((BMT.BatType == NIBAT) &&
				 (BATPHYS.BATTMP < bmt_charging_para->V_TEMP_FAST2FULL_THRES_NI)
				)
			{
				BMT.bat_state = CHR_BATFULL;
				BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);
				bmt_stop_stoptimer();
				stack_stop_timer(&ChargeTimeout_timer);
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
				drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, bmt_total_charge_time);
#else
				drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, BMT_TOTAL_CHARGE_TIME);
#endif          
				/*30min*/
				bmt_set_stoptimer(STOPTIMER_TIMEOUT_TICK);
				BMT_sendMes2UEM(BMT_CHARGE_COMPLETE);
				drv_trace0(TRACE_GROUP_10, BMT_CHR_BATFULL_CHANGE_TRC);
			}
#endif // End of #if defined(CHR_WITH_NI_MH_BATTERY)
			BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);	//Turn on Charge
		}
	}
	bmt_timer_config(BATTime.TON*CHARGING_TIME_UNIT);
}

/*
* FUNCTION
*		BMT_CHRTOPOFF_ON
*
* DESCRIPTION																			  
*		This function is called when charger is on, and BMT charge module 
*	  is at CHR_TOPOFF state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
static void BMT_CHRTOPOFF_ON(void)	  //CV
{
	BATPHYStruct BATPHYS;
	drv_trace0(TRACE_GROUP_10, BMT_CHRTOPOFF_ON_STATE_TRC);
	if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	{
		BMT_PhyCheck(&BATPHYS);
		if(BMT.bat_state != CHR_ERROR)
		{
#if defined(DRV_BMT_PULSE_CHARGING) || defined(__DRV_BMT_ALWAYS_PULSE_CHARGING__) 
			BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);
			bmt_timer_config(bmt_charging_para->TOPOFF_TOFF*CHARGING_TIME_UNIT);
			return;
#else // #if defined(DRV_BMT_PULSE_CHARGING)

			/*for efuse error with CV voltage on MT6235*/
			bmt_set_vbat_cv_calibration(BATPHYS.VBAT);
			
#if defined (WISDOM35B_DEMO_BB)
			if (BATPHYS.VBAT > 4235000)  // if reach 4.235V then TOPOFF => FULL
#else
				if (BMT_CHR_Is_Charge_Full(&BATPHYS))
#endif
			{
				BATFULL_index++;
				if (BATFULL_index == 6)
				{
					BATFULL_index = 0;	
					BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);
					BMT.bat_state = CHR_BATFULL;
					bmt_stop_stoptimer();
					stack_stop_timer(&ChargeTimeout_timer);
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
					drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, bmt_total_charge_time);
#else
						drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, BMT_TOTAL_CHARGE_TIME);
#endif          					/*30min*/
					bmt_set_stoptimer(STOPTIMER_TIMEOUT_TICK);
					BMT_sendMes2UEM(BMT_CHARGE_COMPLETE);
					drv_trace0(TRACE_GROUP_10, BMT_CHR_BATFULL_CHANGE_TRC);
				}
				else
				{
					BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);
				}
			}
			else
			{
				BATFULL_index = 0;
				BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);
			}
#endif //#if defined(DRV_BMT_PULSE_CHARGING)
		}//if (BMT.bat_state != CHR_ERROR)
	}//if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	bmt_timer_config(bmt_charging_para->TOPOFF_TON*CHARGING_TIME_UNIT);
}

/*
* FUNCTION
*		BMT_CHRTOPOFF_OFF
*
* DESCRIPTION																			  
*		This function is called when charger is off, and BMT charge module 
*	  is at CHR_TOPOFF state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
static void BMT_CHRTOPOFF_OFF(void)
{
#if defined(DRV_BMT_PULSE_CHARGING)
	BATPHYStruct BATPHYS;
	drv_trace0(TRACE_GROUP_10, BMT_CHRTOPOFF_OFF_STATE_TRC);
	if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	{
#if defined(__DRV_BMT_USE_ADC_DETECT_FULL__)
		if(bmt_get_chr_cv_det() || (BATPHYS.VBAT >= 4200000))
#else
		if(bmt_get_chr_cv_det())
#endif //#if defined(__DRV_BMT_USE_ADC_DETECT_FULL__)
		{
			BATFULL_index++;
		}
		else
		{
			BATNOTFULL_index++;
		}
		if(BATFULL_index == 6 )
		{
			// change state to post full
			BMT.bat_state = CHR_POSTFULL;
			drv_trace0(TRACE_GROUP_10, BMT_CHR_POSTFULL_CHANGE_TRC);
			/*30min*/
			bmt_set_stoptimer(KAL_TICKS_1_MIN*30);
			BMT_sendMes2UEM(BMT_CHARGE_COMPLETE);
			bmt_timer_config(bmt_charging_para->BATPOSTFULL_TWAIT_LI*KAL_TICKS_1_SEC); //wait 90 seconds, then check the vbat at BMT_CHRBATFULL_OFF
			BATFULL_index = BATNOTFULL_index = 0; //reset the index, recount the percentage again.
			return;
		}
		BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);
	}
	bmt_timer_config(bmt_charging_para->TOPOFF_TON*CHARGING_TIME_UNIT);
#elif defined(__DRV_BMT_ALWAYS_PULSE_CHARGING__)
	BATPHYStruct BATPHYS;
	drv_trace0(TRACE_GROUP_10, BMT_CHRTOPOFF_OFF_STATE_TRC);
	if(BMT.BatType == LIBAT)
	{
		if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
		{
			BMT_PhyCheck(&BATPHYS);
			if ((BMT.bat_state != CHR_ERROR) 
				&& (BMT_CHR_Is_Under_ExtremeTmp(&BATPHYS) == KAL_TRUE)
				&& (BMT_CHR_Is_ExtremeTmp_Charge_Full(&BATPHYS) == KAL_TRUE))
			{
				BATFULL_index = 0;
				return;
			}
			if ((BMT.bat_state != CHR_ERROR)&&(BMT.bat_state != CHR_HOLD))
			{
//already start a estimative timer in fast state, 
//not check VBAT full condition here
#if !defined(__DRV_BMT_ESTIMATIVE_TIMER_ON_TOPOFF__) 
				if (BMT_CHR_Is_Charge_Full(&BATPHYS))
				{
					BATFULL_index++;
					if (BATFULL_index == 1) //for always pulse charging only need once
					{
						BATFULL_index = 0;
						BMT.bat_state = CHR_BATFULL;
						bmt_stop_stoptimer();
						stack_stop_timer(&ChargeTimeout_timer);
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
						drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, bmt_total_charge_time);
#else
						drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, BMT_TOTAL_CHARGE_TIME);
#endif    
						/*30min*/
						bmt_set_stoptimer(STOPTIMER_TIMEOUT_TICK);
						BMT_sendMes2UEM(BMT_CHARGE_COMPLETE);
						drv_trace0(TRACE_GROUP_10, BMT_CHR_BATFULL_CHANGE_TRC);
					}
				}
				else
				{
					BATFULL_index = 0;
				}
#endif //#if defined(__DRV_BMT_ESTIMATIVE_TIMER_ON_TOPOFF__)
				BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);
			}
			else
			{
				BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);
			} // if(!error)
		} // if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	} //if(BMT.BatType == LIBAT)
	bmt_timer_config(bmt_charging_para->TOPOFF_TON*CHARGING_TIME_UNIT);
#else
	drv_trace0(TRACE_GROUP_10, BMT_CHRTOPOFF_OFF_STATE_TRC);
#endif //#if defined(DRV_BMT_PULSE_CHARGING)
}


#if defined(DRV_BMT_PULSE_CHARGING)
/*
* FUNCTION
*		BMT_CHRPOSTFULL_ON
*
* DESCRIPTION																			  
*		This function is called when charger is on, and BMT charge module 
*	  is at CHR_POSTFULL state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
static void BMT_CHRPOSTFULL_ON(void)
{
	BATPHYStruct BATPHYS;
	
	drv_trace0(TRACE_GROUP_10, BMT_CHRBATPOSTFULL_ON_STATE_TRC);
	if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	{
		BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);
		BMT_PhyCheck(&BATPHYS);
	}//if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	bmt_timer_config(bmt_charging_para->BATPOSTFULL_TOFF_LI*CHARGING_TIME_UNIT);
}

/*
* FUNCTION
*		BMT_CHRPOSTFULL_OFF
*
* DESCRIPTION																			  
*		This function is called when charger is off, and BMT charge module 
*	  is at CHR_POSTFULL state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
static void BMT_CHRPOSTFULL_OFF(void)
{
	BATPHYStruct BATPHYS;
	
	drv_trace0(TRACE_GROUP_10, BMT_CHRBATPOSTFULL_OFF_STATE_TRC);
	if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	{
		if(BMT.bat_state != CHR_ERROR)
		{
#if defined(__DRV_BMT_USE_ADC_DETECT_FULL__)
			if(bmt_get_chr_cv_det() || (BATPHYS.VBAT >= 4200000))
#else
			if(bmt_get_chr_cv_det())
#endif //#if defined(__DRV_BMT_USE_ADC_DETECT_FULL__)
			{
				BATFULL_index++;
			}
			else
			{
				BATNOTFULL_index++;
			}
			if(BATFULL_index == 6)
			{
				// change state to bat full
				BMT.bat_state = CHR_BATFULL;
				bmt_stop_stoptimer();
				stack_stop_timer(&ChargeTimeout_timer);
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
				drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, bmt_total_charge_time);
#else
				drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, BMT_TOTAL_CHARGE_TIME);
#endif    				
				drv_trace0(TRACE_GROUP_10, BMT_CHR_BATFULL_CHANGE_TRC);
				BMT.highfull = 1;
				BATFULL_index = BATNOTFULL_index = 0;
				batfull_msg_flag = KAL_TRUE;
				bmt_timer_config(bmt_charging_para->BATPOSTFULL_TON_LI*CHARGING_TIME_UNIT);
				return;
			}
			BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);
		}
	}
	bmt_timer_config(bmt_charging_para->BATPOSTFULL_TON_LI*CHARGING_TIME_UNIT);
}
#endif //#if defined(DRV_BMT_PULSE_CHARGING)

/*
* FUNCTION
*		BMT_CHRBATFULL_ON
*
* DESCRIPTION																			  
*		This function is called when charger is on, and BMT charge module 
*	  is at CHR_BATFULL state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
static void BMT_CHRBATFULL_ON(void)
{
	BATPHYStruct BATPHYS;
	drv_trace0(TRACE_GROUP_10, BMT_CHRBATFULL_ON_STATE_TRC);
	if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	{
		BMT_PhyCheck(&BATPHYS);
		if(BMT.bat_state != CHR_ERROR)
		{
			if (BMT.BatType == LIBAT)
			{
#if defined(__DRV_BMT_ALWAYS_PULSE_CHARGING__) || defined(DRV_BMT_PULSE_CHARGING)
				BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);
				bmt_timer_config(bmt_charging_para->BATFULL_TOFF_LI*CHARGING_TIME_UNIT);
#else
				if ((BATPHYS.VBAT) < bmt_charging_para->V_FULL2FAST_THRES)
				{
					BMT.bat_state = CHR_FAST;
					bmt_stop_stoptimer();
					drv_trace0(TRACE_GROUP_10, BMT_CHR_FAST_CHANGE_TRC);
				}
				BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);
				bmt_timer_config(bmt_charging_para->BATFULL_TON_LI*CHARGING_TIME_UNIT);
#endif // #if defined(__DRV_BMT_ALWAYS_PULSE_CHARGING__) || defined(DRV_BMT_PULSE_CHARGING)
			}
			
#if defined(CHR_WITH_NI_MH_BATTERY)			
			if (BMT.BatType == NIBAT)
			{
				if ((BATPHYS.VBAT) < bmt_charging_para->V_FULL2FAST_THRES_NI)
				{
					BMT.bat_state = CHR_FAST;
					bmt_stop_stoptimer();
					drv_trace0(TRACE_GROUP_10, BMT_CHR_FAST_CHANGE_TRC);
				}
				BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);
				bmt_timer_config(bmt_charging_para->BATFULL_TOFF_NI*CHARGING_TIME_UNIT);
			}
#endif // End of #if defined(CHR_WITH_NI_MH_BATTERY)
		}
		else
		{
			BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);
			bmt_timer_config(1*CHARGING_TIME_UNIT);			
		}//if (BMT.bat_state != CHR_ERROR)
	}//if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
}

/*
* FUNCTION
*		BMT_CHRBATFULL_OFF
*
* DESCRIPTION																			  
*		This function is called when charger is off, and BMT charge module 
*	  is at CHR_BATFULL state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
*/
static void BMT_CHRBATFULL_OFF(void) 
{
	BATPHYStruct BATPHYS;
	drv_trace0(TRACE_GROUP_10, BMT_CHRBATFULL_OFF_STATE_TRC);
	
	if (BMT.highfull)
	{
		drv_trace0(TRACE_GROUP_10, BMT_HIGH_BAT_FULL_TRC);
		if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
		{
			BMT_PhyCheck(&BATPHYS);
			if((BMT.bat_state != CHR_ERROR)&&(BMT.bat_state != CHR_HOLD))
			{
				if (BMT.BatType == LIBAT)
				{
					if (BMT_CHR_Is_Under_ExtremeTmp(&BATPHYS) == KAL_TRUE)
					{
						if (BMT_CHR_Is_ExtremeTmp_Charge_Recharge(&BATPHYS) == KAL_TRUE)
						{
							BMT.bat_state = CHR_FAST;
							bmt_stop_stoptimer();
							drv_trace0(TRACE_GROUP_10, BMT_CHR_FAST_CHANGE_TRC);
							BMT.highfull = 0;
#if defined(__DRV_BMT_SLEEPMODE_ENHANCE__)
							bmt_enable_sleepmode(KAL_FALSE);
#endif //#if defined(__DRV_BMT_SLEEPMODE_ENHANCE__)
							
#ifndef __BMT_CHARGE_GUARD_TIME__
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
	    					bmt_safety_timer_config = KAL_FALSE;						
#else
							stack_start_timer(&ChargeTimeout_timer, 0, KAL_TICKS_1_MIN*BMT_TOTAL_CHARGE_TIME);
#endif
#endif
#if defined(DRV_BMT_PULSE_CHARGING)
							/* For pulse charging, if the charging complete message has been sent, 
							we have to restart guard time charger. */
							if (batfull_msg_flag == KAL_FALSE)
							{
#endif //#if defined(DRV_BMT_PULSE_CHARGING)
#ifdef __BMT_CHARGE_GUARD_TIME__
								bmt_set_guardtimer(KAL_TICKS_1_MIN*BMT_CHARGE_GUARD_TIME_PERIOD);
#endif // #ifdef __BMT_CHARGE_GUARD_TIME__
#if defined(DRV_BMT_PULSE_CHARGING)
							}//if (batfull_msg_flag == KAL_FALSE)
							batfull_msg_flag = KAL_FALSE;
#endif //#if defined(DRV_BMT_PULSE_CHARGING)
						}//if (BMT_CHR_Is_ExtremeTmp_Charge_Recharge(&BATPHYS) == KAL_TRUE)
					}//if (BMT_CHR_Is_Under_ExtremeTmp(&BATPHYS) == KAL_TRUE)
					else //if (BMT_CHR_Is_Under_ExtremeTmp(&BATPHYS) == KAL_TRUE)
					{
						if ((BATPHYS.VBAT) < bmt_charging_para->V_FULL2FAST_THRES)
						{
							BMT.bat_state = CHR_FAST;
							bmt_stop_stoptimer();
							drv_trace0(TRACE_GROUP_10, BMT_CHR_FAST_CHANGE_TRC);
							BMT.highfull = 0;
#if defined(__DRV_BMT_SLEEPMODE_ENHANCE__)
							bmt_enable_sleepmode(KAL_FALSE);
#endif //#if defined(__DRV_BMT_SLEEPMODE_ENHANCE__)
							
#ifdef __BMT_CHARGE_GUARD_TIME__
							bmt_set_guardtimer(KAL_TICKS_1_MIN*BMT_CHARGE_GUARD_TIME_PERIOD);
#else
							//if we have guard time, we don't need safty time
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
	    					bmt_safety_timer_config = KAL_FALSE;						
#else
							stack_start_timer(&ChargeTimeout_timer, 0, KAL_TICKS_1_MIN*BMT_TOTAL_CHARGE_TIME); 
#endif
#endif
#if defined(DRV_BMT_PULSE_CHARGING)
							batfull_msg_flag = KAL_FALSE;
#endif //#if defined(DRV_BMT_PULSE_CHARGING)
						}//if ((BATPHYS.VBAT) < bmt_charging_para->V_FULL2FAST_THRES)
					}//if (BMT_CHR_Is_Under_ExtremeTmp(&BATPHYS) == KAL_TRUE)
				}//if (BMT.BatType == LIBAT)
#if defined(CHR_WITH_NI_MH_BATTERY)
				if (BMT.BatType == NIBAT)
				{
					if (BMT_CHR_Is_Under_ExtremeTmp(&BATPHYS) == KAL_TRUE)
					{
						if (BMT_CHR_Is_ExtremeTmp_Charge_Recharge(&BATPHYS) == KAL_TRUE)
						{
							BMT.bat_state = CHR_FAST;
							bmt_stop_stoptimer();
							drv_trace0(TRACE_GROUP_10, BMT_CHR_FAST_CHANGE_TRC);
							BMT.highfull = 0;
#ifdef __BMT_CHARGE_GUARD_TIME__
							bmt_set_guardtimer(KAL_TICKS_1_MIN*BMT_CHARGE_GUARD_TIME_PERIOD);
#else
							//if we have guard time, we don't need safty time
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
	    					bmt_safety_timer_config = KAL_FALSE;						
#else
							stack_start_timer(&ChargeTimeout_timer, 0, KAL_TICKS_1_MIN*BMT_TOTAL_CHARGE_TIME);
#endif
#endif
						}
					}
					else
					{
						if ((BATPHYS.VBAT) < bmt_charging_para->V_FULL2FAST_THRES_NI)
						{
							BMT.bat_state = CHR_FAST;
							bmt_stop_stoptimer();
							drv_trace0(TRACE_GROUP_10, BMT_CHR_FAST_CHANGE_TRC);
							BMT.highfull = 0;
#ifdef __BMT_CHARGE_GUARD_TIME__
							bmt_set_guardtimer(KAL_TICKS_1_MIN*BMT_CHARGE_GUARD_TIME_PERIOD);
#endif
						}//if ((BATPHYS.VBAT) < bmt_charging_para->V_FULL2FAST_THRES_NI)
					}//if (BMT_CHR_Is_Under_ExtremeTmp(&BATPHYS) == KAL_TRUE)
				}//else if (BMT.BatType == NIBAT)
#endif // End of #if defined(CHR_WITH_NI_MH_BATTERY)
			} //if ((BMT.bat_state != CHR_ERROR)&&(BMT.bat_state != CHR_HOLD))
			BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);
			bmt_timer_config(bmt_charging_para->BATFULL_TOFF*CHARGING_TIME_UNIT);
			return;
		}//if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	}//if (BMT.highfull)
#if defined(__DRV_BMT_ALWAYS_PULSE_CHARGING__) 
	else if(BMT.BatType == LIBAT)
	{
		if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
		{
			BMT_PhyCheck(&BATPHYS);
			if ((BMT.bat_state != CHR_ERROR) 
				&& (BMT_CHR_Is_Under_ExtremeTmp(&BATPHYS) == KAL_TRUE)
				&& (BMT_CHR_Is_ExtremeTmp_Charge_Full(&BATPHYS) == KAL_TRUE))
			{
				return;
			}
			if ((BMT.bat_state != CHR_ERROR)&&(BMT.bat_state != CHR_HOLD))
			{
				BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);
				bmt_timer_config(bmt_charging_para->BATFULL_TON_LI*CHARGING_TIME_UNIT);
			}
			else
			{
				BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);
				bmt_timer_config(bmt_charging_para->BATFULL_TOFF_LI*CHARGING_TIME_UNIT);
			}
			return;
		}//if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	}//else if(BMT.BatType == LIBAT)
#endif //#if defined(__DRV_BMT_ALWAYS_PULSE_CHARGING__)

#if defined(CHR_WITH_NI_MH_BATTERY)
	if (BMT.BatType == NIBAT)
	{
		if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
		{
			BMT_PhyCheck(&BATPHYS);
			if(BMT.bat_state != CHR_ERROR)
			{
				if ((BMT_CHR_Is_Under_ExtremeTmp(&BATPHYS) == KAL_TRUE) && (BMT_CHR_Is_ExtremeTmp_Charge_Full(&BATPHYS) == KAL_TRUE))
				{
					return;
				}
				if (BMT_CHR_Is_Under_ExtremeTmp(&BATPHYS) == KAL_TRUE)
				{
					if (BMT_CHR_Is_ExtremeTmp_Charge_Recharge(&BATPHYS) == KAL_TRUE)
					{
						//Guard timer should be alive and no need to stop it.
						BMT.bat_state = CHR_FAST;
						bmt_stop_stoptimer();
#ifndef __BMT_CHARGE_GUARD_TIME__
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
						bmt_safety_timer_config = KAL_FALSE;						
#else
						stack_start_timer(&ChargeTimeout_timer, 0, KAL_TICKS_1_MIN*BMT_TOTAL_CHARGE_TIME);
#endif
#endif
						drv_trace0(TRACE_GROUP_10, BMT_CHR_FAST_CHANGE_TRC);
					}
				}
				else
				{
					if ((BATPHYS.VBAT) < bmt_charging_para->V_FULL2FAST_THRES_NI)
					{
						BMT.bat_state = CHR_FAST;
						bmt_stop_stoptimer();
						drv_trace0(TRACE_GROUP_10, BMT_CHR_FAST_CHANGE_TRC);
					}
				}
			} //if (BMT.bat_state != CHR_ERROR)
		}//if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
		if(BMT.bat_state != CHR_ERROR)
		{
			BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);
		}
		bmt_timer_config(bmt_charging_para->BATFULL_TON_NI*CHARGING_TIME_UNIT);
	}//if (BMT.BatType == NIBAT)
#endif // End of #if defined(CHR_WITH_NI_MH_BATTERY)
}

/*
* FUNCTION
*		BMT_CHARSTOP
*
* DESCRIPTION																			  
*		This function is called if charge is complete and run after 30 min
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
*/
void BMT_CHARSTOP(void)  /*30 min*/
{
	DCL_STATUS adc_status;
	drv_trace0(TRACE_GROUP_10, BMT_MEASURE_STOP_TRC);
	BMT_CtrlCharge((kal_uint8)KAL_FALSE, 4200000);	//Stop Charge!!
	BMT.highfull = 1;

#if defined(__DRV_BMT_SLEEPMODE_ENHANCE__)
		bmt_enable_sleepmode(KAL_TRUE);
#endif //#if defined(__DRV_BMT_SLEEPMODE_ENHANCE__)
	
#ifdef __BMT_CHARGE_GUARD_TIME__ 
	drv_trace1(TRACE_GROUP_10, BMT_CHARGE_GUARD_TIMER_EXPIRE_TRC, BMT_CHARGE_GUARD_TIME_PERIOD);
	/* Force into FULL state, so that we can monitor it to recharge again. */
	BMT.bat_state = CHR_BATFULL;
#endif // __BMT_CHARGE_GUARD_TIME__

    
	adc_status = DclSADC_Control(bmt_adc_handle, ADC_CMD_STOP_MEASURE, NULL);
	if(adc_status != STATUS_OK)
	{
		ASSERT(0);
	}

	bmt_timer_config(bmt_charging_para->BATFULL_TOFF*CHARGING_TIME_UNIT);
}

/*
* FUNCTION
*		BMT_CHRHOLD
*
* DESCRIPTION																			  
*		This function is called when BMT charge module 
*	  is at CHR_HOLD state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
static void BMT_CHRHOLD(void)
{
	BATPHYStruct BATPHYS;
	drv_trace0(TRACE_GROUP_10, BMT_CHRHOLD_STATE_TRC);
	if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
	{
		BMT_PhyCheck(&BATPHYS);
		if(((BATPHYS.VBAT) < bmt_charging_para->V_PROTECT_LOW_LI) || (BMT.call_state == 0))
		{

#if defined(__DRV_BMT_SLEEPMODE_ENHANCE__)
			bmt_enable_sleepmode(KAL_FALSE);
#endif //#if defined(__DRV_BMT_SLEEPMODE_ENHANCE__)
		
			BMT.bat_state = CHR_FAST;
			bmt_stop_stoptimer();
			drv_trace0(TRACE_GROUP_10, BMT_FAST_CHARGE_CHANGE_TRC);
		}
	}
	BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT);
	bmt_timer_config(bmt_charging_para->BATHOLD_OFF*CHARGING_TIME_UNIT);
}

/*
* FUNCTION
*	   BMT_CHRERRORCHECK
*
* DESCRIPTION                                                           
*   	This function is called when BMT charge module 
*     is at CHR_HOLD state.
*
* CALLS  
*
* PARAMETERS
*	   None
*	
* RETURNS
*	   None
*
* GLOBALS AFFECTED
*     None
*/
#define VCHG_DIFF_THRESHOLD 300000 // 300mV
#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__) //only support if the platform can measurement current
static void BMT_CHRERRORCHECK(void)
{
   BATPHYStruct BATPHYS;
#if defined(DRV_BMT_HW_PRECC_WORKAROUND)
   DCL_HANDLE bmt_dcl_handle;
   BMT_CTRL_SET_CHR_STATUS_T ChrStatus;
#endif
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
    static kal_uint32 pre_vchg_current = 0;
#if defined(__CHINA_CHARGER_STANDARD__)
    kal_uint32 cust_usb_charger_current;
#endif
#endif



   drv_trace0(TRACE_GROUP_10, BMT_ERROR_CHECK_TRC);
   if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is on
   {
#if defined(DRV_BMT_HW_PRECC_WORKAROUND)
        if(SW_Workaround_Flag == KAL_TRUE) // VBAT < 3.4V
        {
            drv_trace0(TRACE_GROUP_10, BMT_SW_WORKAROUND_FLAG_TRUE_TRC);
            if(BATPHYS.VBAT > bmt_charging_para->V_PRE2FAST_THRES)
            {
                SW_Workaround_Flag = KAL_FALSE;
                drv_trace0(TRACE_GROUP_10, BMT_OUTSIDE_SW_WORKAROUND_RANGE_TRC);
                drv_trace0(TRACE_GROUP_10, BMT_SW_WORKAROUND_FLAG_FALSE_TRC);
                if(HW_Plug_Status == bmt_chr_out)
                {
                    drv_trace0(TRACE_GROUP_10, BMT_MISSING_HW_PLUG_OUT_TRC);
                    bmt_dcl_handle = DclBMT_Open(DCL_BMT, FLAGS_NONE);
                    ChrStatus.ChargerStat = DCL_BMT_CHR_OUT;
                    DclBMT_Control(bmt_dcl_handle, BMT_CMD_SET_CHR_STATUS, (DCL_CTRL_DATA_T *)&ChrStatus);
                    DclBMT_Close(bmt_dcl_handle);
                    return;
                }                
            }
            else
            {
                drv_trace0(TRACE_GROUP_10, BMT_INSIDE_SW_WORKAROUND_RANGE_TRC);
                SW_Workaround_Flag = KAL_TRUE;
				#if defined(__DRV_BMT_ISENSE_OFFSET_COMPENSATION__)
                if( BATPHYS.ICHARGE  < bmt_charging_para->ICHARGE_ON_LOW &&
				#else
                if( (BATPHYS.ICHARGE + bmt_charging_para->CurrOffset[BMT.call_state]) < bmt_charging_para->ICHARGE_ON_LOW &&
				#endif	
                (BATPHYS.VCHARGER < 2000000))
                {
                    SW_Workaround_Flag = KAL_FALSE;
                    drv_trace0(TRACE_GROUP_10, BMT_SW_WORKAROUND_FLAG_FALSE_TRC);    
                    drv_trace0(TRACE_GROUP_10, BMT_SW_PLUG_OUT_TRC);      
                    bmt_dcl_handle = DclBMT_Open(DCL_BMT, FLAGS_NONE);
                    ChrStatus.ChargerStat = DCL_BMT_CHR_OUT;
                    DclBMT_Control(bmt_dcl_handle, BMT_CMD_SET_CHR_STATUS, (DCL_CTRL_DATA_T *)&ChrStatus);
                    DclBMT_Close(bmt_dcl_handle);
                    return;
                }

		#if defined(__DRV_BMT_ISENSE_OFFSET_COMPENSATION__)
		if( (BMT.pmictrl_state == PMIC_CHARGEON)&&(BATPHYS.ICHARGE < bmt_charging_para->ICHARGE_ON_LOW))
		#else
		if( (BMT.pmictrl_state == PMIC_CHARGEON)&&((BATPHYS.ICHARGE + bmt_charging_para->CurrOffset[BMT.call_state]) < bmt_charging_para->ICHARGE_ON_LOW))
		#endif	
		{
			kal_uint8 threshold=7;

			if (threshold>bmt_get_on_count())
				threshold=bmt_get_on_count();
			
			SW_Low_Current_Cnts++;
			if (SW_Low_Current_Cnts>=threshold)
			{
			    SW_Workaround_Flag = KAL_FALSE;
			    drv_trace0(TRACE_GROUP_10, BMT_SW_WORKAROUND_FLAG_FALSE_TRC);    
			    drv_trace0(TRACE_GROUP_10, BMT_SW_PLUG_OUT_TRC);      
			    bmt_dcl_handle = DclBMT_Open(DCL_BMT, FLAGS_NONE);
			    ChrStatus.ChargerStat = DCL_BMT_CHR_OUT;
			    DclBMT_Control(bmt_dcl_handle, BMT_CMD_SET_CHR_STATUS, (DCL_CTRL_DATA_T *)&ChrStatus);
			    DclBMT_Close(bmt_dcl_handle);
			    return;
			}
		}
		else
		{		  
			SW_Low_Current_Cnts=0;
		}

	 


                if(Manual_Disable_Charge_Flag == KAL_TRUE)
                {
                    drv_trace0(TRACE_GROUP_10, BMT_PLUG_OUT_FALSE_ALARM_TRC);   
                    Manual_Disable_Charge_Flag = KAL_FALSE;
                    if(BMT.pmictrl_state == PMIC_CHARGEON)
                    {
                        drv_trace0(TRACE_GROUP_10, BMT_ENABLE_CHARGE_AT_CHARGE_ON_TRC);   
                        BMT_Charge(KAL_TRUE); // Enable Charge for next time measure
                    }
                    bmt_timer_config(45); //45 frames ~= 200ms
                    return;
                }

				#if defined(__DRV_BMT_ISENSE_OFFSET_COMPENSATION__)
                if(BATPHYS.ICHARGE  < bmt_charging_para->ICHARGE_ON_LOW)
				#else
                if((BATPHYS.ICHARGE + bmt_charging_para->CurrOffset[BMT.call_state]) < bmt_charging_para->ICHARGE_ON_LOW)
				#endif	
                {
                    if(BMT.pmictrl_state == PMIC_CHARGEON)
                    {
                        drv_trace0(TRACE_GROUP_10, BMT_POSSIBLE_PLUG_OUT_TRC);
                        Manual_Disable_Charge_Flag = KAL_TRUE;
                        BMT_Charge(KAL_FALSE); // Disable Charge for next time measure
                        drv_trace0(TRACE_GROUP_10, BMT_DISABLE_CHARGE_AT_CHARGE_ON_TRC); 
                        bmt_timer_config(45); //45 frames ~= 200ms
                        return;
                    }
                    drv_trace0(TRACE_GROUP_10, BMT_LOW_CHARGE_CURRENT_AT_CHARGE_OFF_TRC); 
                }
            }
        }
        else if( (BATPHYS.VCHARGER > bmt_charging_para->VCHARGER_HIGH) ||
            ((BMT.pmictrl_state == PMIC_CHARGEON) &&
        #if defined(__DRV_BMT_ISENSE_OFFSET_COMPENSATION__)
            (BATPHYS.ICHARGE  < bmt_charging_para->ICHARGE_ON_LOW)))
		#else
            ((BATPHYS.ICHARGE + bmt_charging_para->CurrOffset[BMT.call_state]) < bmt_charging_para->ICHARGE_ON_LOW)))
        #endif    
#elif defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT) // Else of #if defined(DRV_BMT_HW_PRECC_WORKAROUND)
      VCharge_Total += BATPHYS.VCHARGER;
      drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_CUR_VCHG_MAX_TRC, Cur_VCharge_MAX);
      drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_VCHG_DIFF_TRC, BATPHYS.VCHARGER - Pre_VCharge_AVG);
      if(((BATPHYS.VCHARGER - Pre_VCharge_AVG) > VCHG_DIFF_THRESHOLD) && BATPHYS.VCHARGER > Cur_VCharge_MAX && 
        bmt_IsUSBorCharger() != BMT_USB_IN && BMT.bat_state != CHR_PRE)
      {                  
          pre_vchg_current = bmt_high_vchg_current;
          bmt_vchg_compare_and_set_current(BATPHYS.VCHARGER, KAL_FALSE);
#if defined(__CHINA_CHARGER_STANDARD__)

            if(chr_usb_detect.chr_usb_present == CHARGER_PRESENT_NON)
            {
                cust_usb_charger_current = PMU_CHARGE_CURRENT_450_00_MA;
                if(bmt_high_vchg_current > cust_usb_charger_current)
                {
                    bmt_high_vchg_current = cust_usb_charger_current;
                    drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_SET_USB_CHARGE_CURRENT_TRC, cust_usb_charger_current);
                }
            }
#endif // End of #if defined(__CHINA_CHARGER_STANDARD__)

          if(pre_vchg_current != bmt_high_vchg_current)
          {
		  bmt_find_and_set_the_nearest_current(bmt_high_vchg_current);          
          /*
		  PMU_CTRL_CHR_SET_CHR_CURRENT set_chr_current;
		  set_chr_current.current = bmt_high_vchg_current; 
                DclPMU_Control(bmt_PmuHandler, CHR_SET_CHR_CURRENT, (DCL_CTRL_DATA_T *)&set_chr_current);
          */
                BMT_CtrlUPMUCharge(KAL_FALSE);
                BMT_CtrlUPMUCharge(KAL_TRUE);
          }
          drv_trace1(TRACE_GROUP_9, BMT_VCHARGER_TRC, BATPHYS.VCHARGER);
          Cur_VCharge_MAX = BATPHYS.VCHARGER;
          drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_RUNTIME_APPLY_CHARGE_CURRENT_TRC, bmt_high_vchg_current);  
          drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_CUR_VCHG_MAX_TRC, Cur_VCharge_MAX);  
      }

      drv_trace1(TRACE_GROUP_9, BMT_HIGH_VCHG_TOTAL_VCHG_TRC, VCharge_Total);  
      bmt_high_vchg_set_safetytimer(BATPHYS.VCHARGER);  
	  if ( (BATPHYS.VCHARGER > bmt_high_vchg_para->HIGH_VCHG_TABLE[VCHG_VOL_LEVEL - 1][0]) ||
      	  ((BMT.pmictrl_state == PMIC_CHARGEON) && 
      #if defined(__DRV_BMT_ISENSE_OFFSET_COMPENSATION__)
      	   (BATPHYS.ICHARGE  < bmt_charging_para->ICHARGE_ON_LOW)) )	  	
	  #else
      	   ((BATPHYS.ICHARGE + bmt_charging_para->CurrOffset[BMT.call_state]) < bmt_charging_para->ICHARGE_ON_LOW)) )	  	
      #endif	   
#else // Else of #if defined(DRV_BMT_HW_PRECC_WORKAROUND)
        if( (BATPHYS.VCHARGER > bmt_charging_para->VCHARGER_HIGH) ||
            ((BMT.pmictrl_state == PMIC_CHARGEON) && 
      #if defined(__DRV_BMT_ISENSE_OFFSET_COMPENSATION__)
            (BATPHYS.ICHARGE  < bmt_charging_para->ICHARGE_ON_LOW)) )
	  #else
            ((BATPHYS.ICHARGE + bmt_charging_para->CurrOffset[BMT.call_state]) < bmt_charging_para->ICHARGE_ON_LOW)) )
      #endif      
#endif // End of #if defined(DRV_BMT_HW_PRECC_WORKAROUND) 
        {
            bmt_chr_force_enable(KAL_FALSE);
            BMT_CtrlCharge((kal_uint8)KAL_FALSE, BATPHYS.VBAT); //clear the SW global variable
            kal_sleep_task(20); //delay 20 frames for HW issue plug-out interrupt
            bmt_chr_force_enable(KAL_TRUE);
            BMT_CtrlCharge((kal_uint8)KAL_TRUE, BATPHYS.VBAT);
        }
    }
    bmt_timer_config(45); //45 frames ~= 200ms
}
#endif //#if defined(__DRV_BMT_ULTRA_LOW_COST_CHARGER__)

/*
* FUNCTION
*		BMT_MeasureStop
*
* DESCRIPTION																			  
*		This function is used to stop the charge measure, and restart normal measure.
*	  Besides, this function is called when BMT charge module 
*	  is at CHR_ERROR state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
void BMT_MeasureStop(void)
{
	drv_trace0(TRACE_GROUP_10, BMT_MEASURE_STOP_TRC);
#if defined(CHR_WITH_NI_MH_BATTERY) 
	VBATtmp = 0;
#endif
	BMT.pmictrl_state = PMIC_CHARGEOFF;
	bmt_charge_end();
}

/*
* FUNCTION
*	   BMT_VBatVoltageIsFull
*
* DESCRIPTION                                                           
*   	This function is used to check the Battery voltage full or not.
*
* CALLS  
*
* PARAMETERS
*	   voltage: for compare with full threshold
*	
* RETURNS
*	   KAL_FALSE: still not full
*	   KAL_TRUE:  already full
*
* GLOBALS AFFECTED
*     None
*/
kal_bool BMT_VBatVoltageIsFull(kal_uint32 voltage)
{	
	#if defined(__GENERAL_EXTERNAL_CHARGER__)
	return ext_charger->get_vbat_specific_full_voltage_state(voltage);
	#else
	return KAL_FALSE;
	#endif
}

#if defined(__DRV_BMT_PRECHARGE_TO_FULL_DIRECTLY__)
void BMT_CHRPRE_FULLCHECK(void)
{
   if (g_battery_pre_voltage != 0)  //It means that the voltage measurement tiggered in CHRDET_HISR is done.
   {
      kal_bool pre_full;

      VBAT_OFF = g_battery_pre_voltage;

      pre_full = BMT_VBatVoltageIsFull(g_battery_pre_voltage);
      if (KAL_TRUE == pre_full)
      {
		   BATFULL_index = 0;
		   BMT.bat_state = CHR_BATFULL;
		   bmt_stop_stoptimer();
		   stack_stop_timer(&ChargeTimeout_timer);
		   drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, BMT_TOTAL_CHARGE_TIME);
		   /*30min*/
		   bmt_set_stoptimer(STOPTIMER_TIMEOUT_TICK);
		   BMT_sendMes2UEM(BMT_CHARGE_COMPLETE);
		   drv_trace0(TRACE_GROUP_10, BMT_CHR_BATFULL_CHANGE_TRC);
		   BMT_CtrlCharge((kal_uint8)KAL_TRUE, g_battery_pre_voltage);
      }
      else
      {
         BMT.bat_state = CHR_PRE; //Begin charging from pre-charge state
      }
      g_battery_pre_voltage = 1; //change it to a none-zero small value because it only needs to check once after cable plug-in
   }
   bmt_timer_config(5);
}

void BMT_VbatInHISR_Init(void)
{
   DCL_HANDLE adc_handle;
   ADC_CTRL_CREATE_OBJECT_T adc_create;
   ADC_CTRL_GET_PHYSICAL_CHANNEL_T adc_ch;
   DCL_STATUS adc_status;

   adc_handle = DclSADC_Open(DCL_ADC, FLAGS_NONE);
   if(adc_handle == DCL_HANDLE_INVALID)
   {
       ASSERT(0);   
   }
   adc_ch.u2AdcName = DCL_VBAT_ADC_CHANNEL;
   DclSADC_Control(adc_handle, ADC_CMD_GET_CHANNEL, (DCL_CTRL_DATA_T *)&adc_ch);

   bmt_vbat_in_hisr_gpt_handle = DclSGPT_Open(DCL_GPT_CB, 0);
   adc_create.u4Period = 10; // Measurement period (Uint is in Tick)
   adc_create.u1OwnerId = MOD_BMT; // Indicate the module to for ADC driver to notify the result
   adc_create.u1AdcChannel = adc_ch.u1AdcPhyCh; // To be measured physical ADC channel
   adc_create.u1EvaluateCount = 3; // Measurement count
   adc_create.fgSendPrimitive = KAL_FALSE; // Whether to send message to owner module or NOT
   adc_status = DclSADC_Control(adc_handle, ADC_CMD_CREATE_OBJECT, (DCL_CTRL_DATA_T *)&adc_create);
   if(adc_status != STATUS_OK)
   {
		ASSERT(0);   
   }
   bmt_vbat_in_hisr_id = adc_handle;
   DclSADC_Close(adc_handle);
}

static void check_vbat_full_at_fist(kal_int32 handle, kal_int32 volt_result, double adc_result)
{
	DCL_STATUS adc_status;
	adc_status = DclSADC_Control(bmt_vbat_in_hisr_id, ADC_CMD_STOP_MEASURE, NULL);
	if(adc_status != STATUS_OK)
	{
		ASSERT(0);
	}

	g_battery_pre_voltage = volt_result;
	VBAT_OFF = g_battery_pre_voltage;
}

static void bmt_bvat_in_hisr_adc_measurement_cb(void *parameter)
{
  DCL_STATUS adc_status;
  SGPT_CTRL_START_T start;  
  ADC_CTRL_REGISTER_COMPLETE_CB_T registerCB;

  if(KAL_TRUE == bmt_check_if_bat_on())
  {
  	if(bmt_event_sche_active == 1)
     {
        start.u2Tick=1;
        start.pfCallback=bmt_bvat_in_hisr_adc_measurement_cb;
        start.vPara=NULL;
         
        DclSGPT_Control(bmt_vbat_in_hisr_gpt_handle,SGPT_CMD_START,(DCL_CTRL_DATA_T*)&start);
  		return;
  	}
     else
     {
		DclSGPT_Control(bmt_vbat_in_hisr_gpt_handle,SGPT_CMD_STOP,0);
  		DclSGPT_Close(&bmt_vbat_in_hisr_gpt_handle);
  		bmt_vbat_in_hisr_gpt_handle = 0xFF;
		registerCB.pfComplete_cb = (PFN_DCLSADC_COMPLETE_CALLBACK)check_vbat_full_at_fist;
		adc_status = DclSADC_Control(bmt_vbat_in_hisr_id, ADC_CMD_REGISTER_COMPLETE_CB, (DCL_CTRL_DATA_T *)&registerCB);
		if(adc_status != STATUS_OK)
		{
			ASSERT(0);  		
		}
		adc_status = DclSADC_Control(bmt_vbat_in_hisr_id, ADC_CMD_START_MEASURE, NULL);
		if(adc_status != STATUS_OK)
		{
			ASSERT(0);  		
		}
  	}
  }
}


/*
* FUNCTION
*	   BMT_VbatInHISR
*
* DESCRIPTION                                                           
*   	This function is used to save the Battery voltage in HISR
*
* CALLS  
*
* PARAMETERS
*	   None
*	
* RETURNS
*	   None
*
* GLOBALS AFFECTED
*     None
*/
void BMT_VbatInHISR(void)
{
  DCL_STATUS adc_status;
  ADC_CTRL_REGISTER_COMPLETE_CB_T registerCB;	
  SGPT_CTRL_START_T start;

  if(KAL_TRUE == bmt_check_if_bat_on())
  {
    g_battery_pre_voltage = 0;
  	if (bmt_event_sche_active == 1)
     {
        if (0xFF == bmt_vbat_in_hisr_gpt_handle)
        {
			bmt_vbat_in_hisr_gpt_handle = DclSGPT_Open(DCL_GPT_CB, 0);
        }

	    start.u2Tick=1;
	    start.pfCallback=bmt_bvat_in_hisr_adc_measurement_cb;
	    start.vPara=NULL;

	    DclSGPT_Control(bmt_vbat_in_hisr_gpt_handle,SGPT_CMD_START,(DCL_CTRL_DATA_T*)&start);

  	}
    else
    {
  		// bmt task is NOT setting event scheduler, we can set directly
		registerCB.pfComplete_cb = (PFN_DCLSADC_COMPLETE_CALLBACK)check_vbat_full_at_fist;
		adc_status = DclSADC_Control(bmt_vbat_in_hisr_id, ADC_CMD_REGISTER_COMPLETE_CB, (DCL_CTRL_DATA_T *)&registerCB);
		if(adc_status != STATUS_OK)
		{
			ASSERT(0);  		
		}
		adc_status = DclSADC_Control(bmt_vbat_in_hisr_id, ADC_CMD_START_MEASURE, NULL);
		
		if(adc_status != STATUS_OK)
		{
			ASSERT(0);  		
		}
  	}
  }//if(KAL_TRUE == bmt_check_if_bat_on())
}

#endif //#if defined(__DRV_BMT_PRECHARGE_TO_FULL_DIRECTLY__)

#if defined(DRV_BMT_PULSE_CV_CHARGING)
void BMT_CHRPRE_TO_CHRBATFULL(void)
{
	BMT.bat_state = CHR_BATFULL;
	bmt_stop_stoptimer();	
	stack_stop_timer(&ChargeTimeout_timer);	
	
#if defined(__DRV_BMT_SLEEPMODE_ENHANCE__)
	bmt_enable_sleepmode(KAL_TRUE);
#endif //#if defined(__DRV_BMT_SLEEPMODE_ENHANCE__)
	
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
	drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, bmt_total_charge_time);
#else
	drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, BMT_TOTAL_CHARGE_TIME);
#endif 

	drv_trace0(TRACE_GROUP_10, BMT_CHR_BATFULL_CHANGE_TRC);
	BMT.highfull = 1;	
	BMT_sendMes2UEM(BMT_CHARGE_COMPLETE);
	bmt_timer_config(bmt_charging_para->PRE_TON*CHARGING_TIME_UNIT);
}
#endif //#if defined(DRV_BMT_PULSE_CV_CHARGING)


#if defined(DRV_BMT_PULSE_CHARGING)
void BMT_CHRPRE_TO_CHRBATFULL(void)
{
#if defined(MT6236)  

    PMU_CTRL_CHR_SET_CV_DETECTION_VOLTAGE chr;
#endif

    BMT.bat_state = CHR_BATFULL;
    stack_stop_timer(&ChargeTimeout_timer);
#if defined(DRV_BMT_HIGH_VCHG_ADAPTIVE_CHARGE_CURRENT_SUPPORT)
	drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, bmt_total_charge_time);
#else
    drv_trace1(TRACE_GROUP_10, BMT_SAFETY_TIMER_STOP_TRC, BMT_TOTAL_CHARGE_TIME);
#endif 
#if defined(MT6236)        

#if defined(PMIC_6236_CV_TRIM_CALIBRATION)

    chr.voltage = (PMU_VOLTAGE_ENUM)(PMU_VOLT_04_000000_V + mt6236_efuse_vbat_cv * 12500);
#else
    chr.voltage = PMU_VOLT_04_000000_V + CHR_VBAT_CV_4_2000V * 12500;
#endif
    DclPMU_Control(bmt_PmuHandler, CHR_SET_CV_DETECTION_VOLTAGE, (DCL_CTRL_DATA_T *)&chr);
          
#endif    
    drv_trace0(TRACE_GROUP_10, BMT_CHR_BATFULL_CHANGE_TRC);
    BMT.highfull = 1;
    batfull_msg_flag = KAL_TRUE;
    BMT_sendMes2UEM(BMT_CHARGE_COMPLETE);
    bmt_timer_config(bmt_charging_para->PRE_TON*CHARGING_TIME_UNIT);
}
/*
* FUNCTION
*		BMT_CHRPRE_TO_CHRBATFULL_CHECK
*
* DESCRIPTION																			  
*		This function is used to check if current battery voltage is larger than VBAT_CV
*	    Level - 2. We will transite to CHRBATFULL state.
*
* CALLS  
*
* PARAMETERS
*		None
*	
* RETURNS
*		None
*
* GLOBALS AFFECTED
*	  None
*/
kal_bool BMT_CHRPRE_TO_CHRBATFULL_CHECK(void)
{
#if defined(MT6236)
	DCL_HANDLE pmu_handle;
    PMU_CTRL_CHR_SET_CV_DETECTION_VOLTAGE chr;
#else    
    BATPHYStruct BATPHYS;
#endif
   
	if(bmt_get_chr_cv_det()) // For Custom check CV function
	{
        BMT_CHRPRE_TO_CHRBATFULL();
		return KAL_TRUE;				
	}
   
#if defined(MT6236)   
#if defined(PMIC_6236_CV_TRIM_CALIBRATION)   
    if(mt6236_efuse_vbat_cv >= 2)
    {
        
        chr.voltage = (PMU_VOLTAGE_ENUM)(PMU_VOLT_04_000000_V + (mt6236_efuse_vbat_cv - 2) * 12500);
        DclPMU_Control(bmt_PmuHandler, CHR_SET_CV_DETECTION_VOLTAGE, (DCL_CTRL_DATA_T *)&chr);
        
    }
#else // #if defined(PMIC_6236_CV_TRIM_CALIBRATION)
    
    chr.voltage = PMU_VOLT_04_000000_V + (CHR_VBAT_CV_4_1750V) * 12500;
    DclPMU_Control(bmt_PmuHandler, CHR_SET_CV_DETECTION_VOLTAGE, (DCL_CTRL_DATA_T *)&chr);
       
#endif // #if defined(PMIC_6236_CV_TRIM_CALIBRATION)

	if(bmt_get_chr_cv_det()) // For 4.175V CV Check
	{
        BMT_CHRPRE_TO_CHRBATFULL();
		return KAL_TRUE;				
	}
    else
	{
        
#if defined(PMIC_6236_CV_TRIM_CALIBRATION)
        chr.voltage = (PMU_VOLTAGE_ENUM)(PMU_VOLT_04_000000_V + mt6236_efuse_vbat_cv * 12500);
#else
        chr.voltage = PMU_VOLT_04_000000_V + CHR_VBAT_CV_4_2000V * 12500;
#endif
        DclPMU_Control(bmt_PmuHandler, CHR_SET_CV_DETECTION_VOLTAGE, (DCL_CTRL_DATA_T *)&chr);
                   
        return KAL_FALSE;
	}
#else // #if defined(MT6236)

	if (BMT_ObtainBMTPHYSTAT(&BATPHYS)) //The measure is off period
	{
		BMT_PhyCheck(&BATPHYS);
        if(BATPHYS.VBAT > 4175000)
        {
            BMT_CHRPRE_TO_CHRBATFULL();
		return KAL_TRUE;				
	}
        else
        {
    return KAL_FALSE;
}
	}
    else
    {
    return KAL_FALSE;
}
#endif // #if defined(MT6236)
}
#endif //#if defined(DRV_BMT_PULSE_CHARGING)