T_BT_CAP_ID.cpp
上传用户:gelin96
上传日期:2017-01-08
资源大小:20993k
文件大小:29k
源码类别:

MTK

开发平台:

C++ Builder

  1. /*****************************************************************************
  2. *  Copyright Statement:
  3. *  --------------------
  4. *  This software is protected by Copyright and the information contained
  5. *  herein is confidential. The software may not be copied and the information
  6. *  contained herein may not be used or disclosed except with the written
  7. *  permission of MediaTek Inc. (C) 2005
  8. *
  9. *  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  10. *  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  11. *  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  12. *  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  13. *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  14. *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  15. *  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  16. *  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  17. *  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  18. *  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  19. *  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  20. *  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  21. *
  22. *  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  23. *  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  24. *  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  25. *  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  26. *  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  27. *
  28. *  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  29. *  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  30. *  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  31. *  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  32. *  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  33. *
  34. *****************************************************************************/
  35. /*****************************************************************************
  36.  *
  37.  * Filename:
  38.  * ---------
  39.  *   T_BT_CAP_ID.cpp
  40.  *
  41.  * Project:
  42.  * --------
  43.  *   Maui META APP
  44.  *
  45.  * Description:
  46.  * ------------
  47.  *  BT CAP ID calibration source
  48.  *
  49.  * Author:
  50.  * -------
  51.  *  Andy Ueng (mtk00490)
  52.  *
  53.  *============================================================================
  54.  *             HISTORY
  55.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  56.  *------------------------------------------------------------------------------
  57.  * $Revision$
  58.  * $Modtime$
  59.  * $Log$
  60.  *
  61.  *------------------------------------------------------------------------------
  62.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  63.  *============================================================================
  64. ****************************************************************************/
  65. #include <assert.h>
  66. #include <math.h>
  67. #pragma hdrstop
  68. #ifndef _T_META_FACTORY_CALIBRATION_H_
  69. #include "T_META_factory_calibration.H"
  70. #endif
  71. #ifndef _CAL_COMMON_H_
  72. #include "cal_common.h"
  73. #endif
  74. #ifndef _META_FACTORY_H_
  75. #include "meta_factory.h"
  76. #endif
  77. // BT
  78. #ifndef  _BT_POWER_H_
  79. #include "bt_power.h"
  80. #endif
  81. #ifndef  _BT_RF_H_
  82. #include "bt_rf.h"
  83. #endif
  84. #ifndef  _BT_CON_H_
  85. #include "bt_con.h"
  86. #endif
  87. #ifndef  _BT_INFO_MT6601_H_
  88. #include "bt_info_mt6601.h"
  89. #endif
  90. // misc
  91. #ifndef  _TIME_UTILS_H_
  92. #include "time_utils.h"
  93. #endif
  94. #ifndef  _MATH_UTILS_H_
  95. #include "math_utils.h"
  96. #endif
  97. #ifndef  _FT_UTILS_H_
  98. #include "ft_utils.h"
  99. #endif
  100. // callback
  101. #ifndef _MF_BT_CB_H_
  102. #include "mf_bt_cb.h"
  103. #endif
  104. #define TARGET_POLL_COUNT 400000
  105. extern bool  is_suspend_cal;
  106. //******************************************************************************
  107. /////////////////////////////  BT CAP ID calibration  //////////////////////////
  108. //******************************************************************************
  109. bool __fastcall T_META_factory_calibration::BTCrystalCapIDCal_MT6601(void)
  110. {
  111.     char tempbuf[256];
  112.     S_BT_CT sBTCT;
  113.     S_BTT_INQUIRY s_inquiry;
  114.     E_BTT_STATE_T btt_state;
  115.     S_BT_SCAN scan;
  116.     PostMessage(
  117.                      ctrl.hPostMsgDestHandle,
  118.                      WM_MF_BT_CRYSTAL_CAPID_BEGIN,
  119.                      0,
  120.                      0
  121.                     );
  122.     log->Add("n" +DateToStr(Date()) +  " " + CurrentTimeStr() +
  123.              " ================= BT Crystal CAP ID calibration begin ================ "
  124.            );
  125.     // read BT info from NVRAM
  126.     //m_cBT_INFO_Obj.ConfirmCallback  = ::CNF_MF_BT;
  127.     //m_cBT_INFO_Obj.REQ_Read_From_NVRAM_Start();
  128.     //RestartTimerCal(WM_MF_NVRAM_BT_INFO_READ_FAIL);
  129.     //SUSPEND_CAL_THREAD
  130.     //E_METAAPP_RESULT_T state = m_cBT_INFO_Obj.Get_ConfirmState();
  131.     //if (state != METAAPP_SUCCESS)
  132.     //{
  133.     //    CalErrorHandler( WM_MF_NVRAM_BT_INFO_READ_FAIL);
  134.     //    log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  135.     //              " FAIL : Target > read BT info. from NVRAM fail "
  136.     //            );
  137.     //    return false;
  138.     //}
  139.     RestartTimerCal(WM_MF_BTT_CONFIG_BD_ADDRESS_FAIL);
  140.     if (!m_btt_ctrl.BTT_Config_BD_Address(m_pBtt, m_asBD_Address))
  141.     {
  142.         CalErrorHandler(WM_MF_BTT_CONFIG_BD_ADDRESS_FAIL);
  143.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  144.                   " FAIL : BT tester < Config BD address fail "
  145.                 );
  146.         return false;
  147.     }
  148.     //m_cBT_POWER_Obj.REQ_Power_On_Start();
  149.     RestartTimerCal(WM_MF_BTT_CONFIG_SLAVE_PARAMETER_FAIL);
  150.     if (!m_btt_ctrl.BTT_Config_Slave_Parameter(m_pBtt, m_usBT_CRYSTAL_CHANNEL))
  151.     {
  152.         CalErrorHandler(WM_MF_BTT_CONFIG_SLAVE_PARAMETER_FAIL);
  153.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  154.                      " FAIL : BT tester < Config slave parameter fail "
  155.                     );
  156.         return false;
  157.     }
  158.     // inquiry enable
  159.     if (m_asInquiry_Enable.AnsiCompareIC("yes") == 0)
  160.     {
  161.         sBTCT.hPostMsgDestHandle = NULL;
  162.         sBTCT.log                = NULL;
  163.         // set inquiry scan interval
  164.         //if (!m_cBT_POWER_Obj.REQ_Query_Power_On_Start())
  165.         //{
  166.         //    m_cBT_POWER_Obj.REQ_Power_On_Start();
  167.         //}
  168.         //Sleep(1000);
  169.         
  170.         m_cBT_CT_Obj.ConfirmCallback = ::CNF_MF_BT;
  171.         m_cBT_CT_Obj.REQ_Set_Inquiry_Scan_Interval_Start(sBTCT);
  172.         RestartTimerCal(WM_MF_BT_INQUIRY_ENABLE_FAIL);
  173.         SUSPEND_CAL_THREAD
  174.         E_METAAPP_RESULT_T state = m_cBT_CT_Obj.Get_ConfirmState();
  175.         if (state != METAAPP_SUCCESS)
  176.         {
  177.             CalErrorHandler(WM_MF_BT_INQUIRY_ENABLE_FAIL);
  178.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  179.                      " FAIL : Target < set BT inquiry interval fail "
  180.                     );
  181.             return false;
  182.         }
  183.         // set scan type
  184.         scan.e_scan_type = SCAN_ENABLE_INQUIRY_ONLY;
  185.         scan.b_interlaced_inquiry_scan = true;
  186.         scan.b_interlaced_page_scan = false;
  187.         m_cBT_CT_Obj.Set_ScanParameter(scan);
  188.         m_cBT_CT_Obj.ConfirmCallback = ::CNF_MF_BT;
  189.         m_cBT_CT_Obj.REQ_Scan_Start(sBTCT);
  190.         RestartTimerCal(WM_MF_BTT_CONFIG_SCAN_FAIL);
  191.         SUSPEND_CAL_THREAD
  192.         state = m_cBT_CT_Obj.Get_ConfirmState();
  193.         if (state != METAAPP_SUCCESS)
  194.         {
  195.             CalErrorHandler(WM_MF_BTT_CONFIG_SCAN_FAIL);
  196.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  197.                      " FAIL : Target < BT config inquiry scan parameter fail "
  198.                     );
  199.             return false;
  200.         }
  201.         // trigger BT tester to inquiry
  202.         RestartTimerCal(WM_MF_BTT_CONFIG_INQUIRY_FAIL);
  203.         s_inquiry.i_timeout = 10;
  204.         s_inquiry.i_response_num = 1;
  205.         if (!m_btt_ctrl.BTT_Config_Inquiry(m_pBtt, s_inquiry))
  206.         {
  207.             CalErrorHandler(WM_MF_BTT_CONFIG_INQUIRY_FAIL);
  208.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  209.                      " FAIL : BT tester < Config inquiry fail "
  210.                     );
  211.             return false;
  212.         }
  213.         RestartTimerCal(WM_MF_BTT_INQUIRY_FAIL);
  214.         if (!m_btt_ctrl.BTT_Inquiry(m_pBtt))
  215.         {
  216.             CalErrorHandler(WM_MF_BTT_INQUIRY_FAIL);
  217.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  218.                      " FAIL : BT tester < Inquiry fail "
  219.                     );
  220.             return false;
  221.         }
  222.         RestartTimerCal(WM_MF_BTT_QUERY_STATE_FAIL);
  223.         do
  224.         {
  225.             CHECK_TERMINATE_BY_USER
  226.             Sleep(100);
  227.             if (!m_btt_ctrl.BTT_Query_State(m_pBtt, btt_state))
  228.             {
  229.                 CalErrorHandler(WM_MF_BTT_QUERY_STATE_FAIL);
  230.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  231.                          " FAIL : BT tester < query state fail "
  232.                         );
  233.                 return false;
  234.             }
  235.         }
  236.         while (BTT_STATUS_INQUIRY == btt_state);
  237.     }
  238.     unsigned char ucCAP_ID[2];
  239.     double d_freq_err[2];
  240.     ucCAP_ID[0] = m_ucBT_CRYSTAL_CAP_ID1;
  241.     ucCAP_ID[1] = m_ucBT_CRYSTAL_CAP_ID2;
  242.     for (int i=0; i<2; i++)
  243.     {
  244.         // set CAP_ID
  245.         m_pCal->s_cal_obj.pc_BT_INFO6601_Obj->Set_BT_CAP_ID(ucCAP_ID[i]);
  246.         // write BT info to NVRAM
  247.         m_pCal->s_cal_obj.pc_BT_INFO6601_Obj->ConfirmCallback  = ::CNF_MF_BT;
  248.         m_pCal->s_cal_obj.pc_BT_INFO6601_Obj->REQ_Write_To_NVRAM_Start();
  249.         RestartTimerCal(WM_MF_NVRAM_BT_INFO_WRITE_FAIL);
  250.         SUSPEND_CAL_THREAD
  251.         E_METAAPP_RESULT_T state = m_pCal->s_cal_obj.pc_BT_INFO6601_Obj->Get_ConfirmState();
  252.         if (state != METAAPP_SUCCESS)
  253.         {
  254.             if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  255.             {
  256.                 CalErrorHandler(WM_MF_NVRAM_EF_BTRADIO_MT6601_LID_VERNO_FAIL);
  257.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  258.                                " FAIL : NVRAM_EF_BTRADIO_MT6601_LID version is not support, please update META to latest version. "
  259.                                );
  260.             }
  261.             else
  262.             {
  263.                 CalErrorHandler(WM_MF_NVRAM_BT_INFO_WRITE_FAIL);
  264.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  265.                                  " FAIL:  Target < Write BT info. to flash "
  266.                                 );
  267.             }
  268.             return false;
  269.         }
  270.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  271.                                  " Target < Write BT CAP_ID = " + IntToStr(ucCAP_ID[i]) + " to flash "
  272.                                 );
  273.    //     m_cBT_POWER_Obj.REQ_Power_On_Start();
  274.         // enter test mode
  275.         //Sleep(1000);
  276.         S_BT_RF sBtRf;
  277.         sBtRf.hPostMsgDestHandle = NULL;
  278.         sBtRf.log                = NULL;
  279.         m_cBT_RF_Obj.ConfirmCallback  = ::CNF_MF_BT;
  280.         m_cBT_RF_Obj.REQ_Enter_Test_Mode_Start(sBtRf);
  281.         RestartTimerCal(WM_MF_BT_ENTER_TEST_MODE_FAIL);
  282.         SUSPEND_CAL_THREAD
  283.         state = m_cBT_RF_Obj.Get_ConfirmState();
  284.         if (state != METAAPP_SUCCESS)
  285.         {
  286.             CalErrorHandler(WM_MF_BT_ENTER_TEST_MODE_FAIL);
  287.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  288.                   " FAIL : Target < Enter BT test mode fail "
  289.                 );
  290.             return false;
  291.         }
  292.         // set page scan interval
  293.         m_cBT_CT_Obj.ConfirmCallback = ::CNF_MF_BT;
  294.         m_cBT_CT_Obj.REQ_Set_Page_Scan_Interval_Start(sBTCT);
  295.         RestartTimerCal(WM_MF_BT_INQUIRY_ENABLE_FAIL);
  296.         SUSPEND_CAL_THREAD
  297.         state = m_cBT_CT_Obj.Get_ConfirmState();
  298.         if (state != METAAPP_SUCCESS)
  299.         {
  300.             CalErrorHandler(WM_MF_BT_INQUIRY_ENABLE_FAIL);
  301.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  302.                      " FAIL : Target < set BT page interval fail "
  303.                     );
  304.             return false;
  305.         }
  306.         // set scan type
  307.         scan.e_scan_type = SCAN_ENABLE_PAGE_ONLY;
  308.         scan.b_interlaced_inquiry_scan = false;
  309.         scan.b_interlaced_page_scan = true;
  310.         m_cBT_CT_Obj.Set_ScanParameter(scan);
  311.         m_cBT_CT_Obj.ConfirmCallback = ::CNF_MF_BT;
  312.         m_cBT_CT_Obj.REQ_Scan_Start(sBTCT);
  313.         RestartTimerCal(WM_MF_BTT_CONFIG_SCAN_FAIL);
  314.         SUSPEND_CAL_THREAD
  315.         state = m_cBT_CT_Obj.Get_ConfirmState();
  316.         if (state != METAAPP_SUCCESS)
  317.         {
  318.             CalErrorHandler(WM_MF_BTT_CONFIG_SCAN_FAIL);
  319.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  320.                      " FAIL : Target < BT config page scan parameter fail "
  321.                     );
  322.             return false;
  323.         }
  324.         // trigger BT tester to make connection
  325.         RestartTimerCal(WM_MF_BTT_CONNECT_TEST_FAIL);
  326.         if (!m_btt_ctrl.BTT_Connect_Test(m_pBtt))
  327.         {
  328.             CalErrorHandler(WM_MF_BTT_CONNECT_TEST_FAIL);
  329.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  330.                      " FAIL : BT tester < Connect testmode fail "
  331.                     );
  332.             return false;
  333.         }
  334.         RestartTimerCal(WM_MF_BTT_QUERY_STATE_FAIL);
  335.         do
  336.         {
  337.             CHECK_TERMINATE_BY_USER
  338.             Sleep(100);
  339.             if (!m_btt_ctrl.BTT_Query_State(m_pBtt, btt_state))
  340.             {
  341.                 CalErrorHandler(WM_MF_BTT_QUERY_STATE_FAIL);
  342.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  343.                          " FAIL : BT tester < query state fail "
  344.                         );
  345.                 return false;
  346.             }
  347.         }
  348.         while (BTT_STATUS_TEST != btt_state);
  349.         // config TX modulation
  350.         S_BTT_MOD bt_mod;
  351.         bt_mod.i_burst_num = 100;
  352.         bt_mod.i_channel   = m_usBT_CRYSTAL_CHANNEL;
  353.         RestartTimerCal(WM_MF_BTT_CONNECT_TEST_FAIL);
  354.         if (!m_btt_ctrl.BTT_Config_MOD(m_pBtt, bt_mod))
  355.         {
  356.             CalErrorHandler(WM_MF_BTT_CONNECT_TEST_FAIL);
  357.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  358.                      " FAIL : BT tester < Connect testmode fail "
  359.                     );
  360.             return false;
  361.         }
  362.         // read TX frequency accuracy
  363.         RestartTimerCal(WM_MF_BTT_READ_FREQ_ACCURACY);
  364.         if (!m_btt_ctrl.BTT_Read_Average_Freq_Error(m_pBtt, d_freq_err[i]))
  365.         {
  366.             CalErrorHandler(WM_MF_BTT_READ_FREQ_ACCURACY);
  367.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  368.                      " FAIL : BT tester > read frequency accuracy fail "
  369.                     );
  370.             return false;
  371.         }
  372.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  373.                      " BT tester > read frequency accuracy = " + Double_To_AnsiString(d_freq_err[i])
  374.                     );
  375.         // release connection
  376.         RestartTimerCal(WM_MF_BTT_RELEASE_FAIL);
  377.         if (!m_btt_ctrl.BTT_Release(m_pBtt))
  378.         {
  379.             CalErrorHandler(WM_MF_BTT_RELEASE_FAIL);
  380.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  381.                      " FAIL : BT tester < release connection fail "
  382.                     );
  383.             return false;
  384.         }
  385.         RestartTimerCal(WM_MF_BTT_QUERY_STATE_FAIL);
  386.         do
  387.         {
  388.             CHECK_TERMINATE_BY_USER
  389.             Sleep(100);
  390.             if (!m_btt_ctrl.BTT_Query_State(m_pBtt, btt_state))
  391.             {
  392.                 CalErrorHandler(WM_MF_BTT_QUERY_STATE_FAIL);
  393.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  394.                          " FAIL : BT tester < query state fail "
  395.                         );
  396.                 return false;
  397.             }
  398.         }
  399.         while (BTT_STATUS_STANDBY != btt_state);
  400.     }
  401.     // Evaluate
  402.     double ddata;
  403.     unsigned char ucCAP_ID_min = BT_CAP_ID_MAX;
  404.     if ((d_freq_err[0] - d_freq_err[1]) != 0)
  405.     {
  406.         ddata = ucCAP_ID[0] - 1.0*d_freq_err[0]*(ucCAP_ID[0] - ucCAP_ID[1])/(d_freq_err[0] - d_freq_err[1]);
  407.         ddata += (ddata>=0) ? 0.5 : -0.5 ;
  408.         ucCAP_ID_min = (int)ddata;
  409.     }
  410.     if (ucCAP_ID_min > BT_CAP_ID_MAX)
  411.     {
  412.         ucCAP_ID_min = BT_CAP_ID_MAX;
  413.     }
  414.     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  415.                                " Evaluate : CAP_ID = " + IntToStr(ucCAP_ID_min)
  416.                                );
  417.     // set CAP_ID
  418.     m_pCal->s_cal_obj.pc_BT_INFO6611_Obj->Set_BT_CAP_ID(ucCAP_ID_min);
  419.     // write BT info to NVRAM
  420.     m_pCal->s_cal_obj.pc_BT_INFO6601_Obj->ConfirmCallback  = ::CNF_MF_BT;
  421.     m_pCal->s_cal_obj.pc_BT_INFO6601_Obj->REQ_Write_To_NVRAM_Start();
  422.     RestartTimerCal(WM_MF_NVRAM_BT_INFO_WRITE_FAIL);
  423.     SUSPEND_CAL_THREAD
  424.     E_METAAPP_RESULT_T state = m_pCal->s_cal_obj.pc_BT_INFO6601_Obj->Get_ConfirmState();
  425.     if (state != METAAPP_SUCCESS)
  426.     {
  427.         if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  428.         {
  429.             CalErrorHandler(WM_MF_NVRAM_EF_BTRADIO_MT6601_LID_VERNO_FAIL);
  430.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  431.                                " FAIL : NVRAM_EF_WNDRV_DAC_DC_OFFSET_LID version is not support, please update META to latest version. "
  432.                                );
  433.         }
  434.         else
  435.         {
  436.             CalErrorHandler(WM_MF_NVRAM_BT_INFO_WRITE_FAIL);
  437.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  438.                                  " FAIL:  Target < Write BT info. to flash "
  439.                                 );
  440.         }
  441.         return false;
  442.     }
  443.     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  444.                                  " Target < Write BT CAP_ID = " + IntToStr(ucCAP_ID_min) + " to flash "
  445.                                 );
  446.     if (!WriteBtCapIDCalResultToFile(as_ID+".cal", Application->ExeName, m_pCal->b_CalResultPath, m_pCal->s_bt_bb_id))
  447.     {
  448.         CalErrorHandler(WM_MF_BT_CRYSTAL_CAPID_WRITE_CAL_RESULT_FAIL);
  449.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  450.                 " FAIL : write AFC parameter to cal file "
  451.               );
  452.         return false;
  453.     }
  454.     
  455.     log->Add("n" +DateToStr(Date()) +  " " + CurrentTimeStr() +
  456.              " =================  BT Crystal CAP ID calibration end  ================ "
  457.            );
  458.     frmFatcory->DisableAllCalTimer();
  459.     return true;
  460. }
  461. //----------------------------------------------------------------------------
  462. bool __fastcall T_META_factory_calibration::BTCrystalCapIDCheck_MT6601(void)
  463. {
  464.     char tempbuf[256];
  465.     log->Add( "n" +DateToStr(Date()) +  " " + CurrentTimeStr() +
  466.               " ================= BT Crystal CAP ID check begin ================ "
  467.            );
  468.     // enter test mode
  469.     //Sleep(1000);
  470.     S_BT_RF sBtRf;
  471.     sBtRf.hPostMsgDestHandle = NULL;
  472.     sBtRf.log                = NULL;
  473.     m_cBT_RF_Obj.ConfirmCallback  = ::CNF_MF_BT;
  474.     m_cBT_RF_Obj.REQ_Enter_Test_Mode_Start(sBtRf);
  475.     RestartTimerCal(WM_MF_BT_ENTER_TEST_MODE_FAIL);
  476.     SUSPEND_CAL_THREAD
  477.     E_METAAPP_RESULT_T state = m_cBT_RF_Obj.Get_ConfirmState();
  478.     if (state != METAAPP_SUCCESS)
  479.     {
  480.         CalErrorHandler(WM_MF_BT_ENTER_TEST_MODE_FAIL);
  481.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  482.                   " FAIL : Target < Enter BT test mode fail "
  483.                 );
  484.         return false;
  485.     }
  486.     // set page scan interval
  487.     S_BT_CT sBTCT;
  488.     sBTCT.hPostMsgDestHandle = NULL;
  489.     sBTCT.log                = NULL;
  490.     m_cBT_CT_Obj.ConfirmCallback = ::CNF_MF_BT;
  491.     m_cBT_CT_Obj.REQ_Set_Page_Scan_Interval_Start(sBTCT);
  492.     RestartTimerCal(WM_MF_BT_INQUIRY_ENABLE_FAIL);
  493.     SUSPEND_CAL_THREAD
  494.     state = m_cBT_CT_Obj.Get_ConfirmState();
  495.     if (state != METAAPP_SUCCESS)
  496.     {
  497.         CalErrorHandler(WM_MF_BT_INQUIRY_ENABLE_FAIL);
  498.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  499.                      " FAIL : Target < set BT page interval fail "
  500.                     );
  501.         return false;
  502.     }
  503.     // set scan type
  504.     S_BT_SCAN scan;
  505.     scan.e_scan_type = SCAN_ENABLE_PAGE_ONLY;
  506.     scan.b_interlaced_inquiry_scan = false;
  507.     scan.b_interlaced_page_scan = true;
  508.     m_cBT_CT_Obj.Set_ScanParameter(scan);
  509.     m_cBT_CT_Obj.ConfirmCallback = ::CNF_MF_BT;
  510.     m_cBT_CT_Obj.REQ_Scan_Start(sBTCT);
  511.     RestartTimerCal(WM_MF_BTT_CONFIG_SCAN_FAIL);
  512.     SUSPEND_CAL_THREAD
  513.     state = m_cBT_CT_Obj.Get_ConfirmState();
  514.     if (state != METAAPP_SUCCESS)
  515.     {
  516.         CalErrorHandler(WM_MF_BTT_CONFIG_SCAN_FAIL);
  517.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  518.                      " FAIL : Target < BT config page scan parameter fail "
  519.                     );
  520.         return false;
  521.     }
  522.     // trigger BT tester to make connection
  523.     RestartTimerCal(WM_MF_BTT_CONNECT_TEST_FAIL);
  524.     if (!m_btt_ctrl.BTT_Connect_Test(m_pBtt))
  525.     {
  526.         CalErrorHandler(WM_MF_BTT_CONNECT_TEST_FAIL);
  527.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  528.                      " FAIL : BT tester < Connect testmode fail "
  529.                     );
  530.         return false;
  531.     }
  532.     RestartTimerCal(WM_MF_BTT_QUERY_STATE_FAIL);
  533.     E_BTT_STATE_T btt_state;
  534.     do
  535.     {
  536.         CHECK_TERMINATE_BY_USER
  537.         Sleep(100);
  538.         if (!m_btt_ctrl.BTT_Query_State(m_pBtt, btt_state))
  539.         {
  540.             CalErrorHandler(WM_MF_BTT_QUERY_STATE_FAIL);
  541.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  542.                          " FAIL : BT tester < query state fail "
  543.                         );
  544.             return false;
  545.         }
  546.     }
  547.     while (BTT_STATUS_TEST != btt_state);
  548.     // read TX frequency accuracy
  549.     double d_freq_err;
  550.     RestartTimerCal(WM_MF_BTT_READ_FREQ_ACCURACY);
  551.     if (!m_btt_ctrl.BTT_Read_Average_Freq_Error(m_pBtt, d_freq_err))
  552.     {
  553.         CalErrorHandler(WM_MF_BTT_READ_FREQ_ACCURACY);
  554.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  555.                      " FAIL : BT tester > read frequency accuracy fail "
  556.                     );
  557.         return false;
  558.     }
  559.     // release connection
  560.     RestartTimerCal(WM_MF_BTT_RELEASE_FAIL);
  561.     if (!m_btt_ctrl.BTT_Release(m_pBtt))
  562.     {
  563.         CalErrorHandler(WM_MF_BTT_RELEASE_FAIL);
  564.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  565.                      " FAIL : BT tester < release connection fail "
  566.                     );
  567.         return false;
  568.     }
  569.     RestartTimerCal(WM_MF_BTT_QUERY_STATE_FAIL);
  570.     do
  571.     {
  572.         CHECK_TERMINATE_BY_USER
  573.         Sleep(100);
  574.         if (!m_btt_ctrl.BTT_Query_State(m_pBtt, btt_state))
  575.         {
  576.             CalErrorHandler(WM_MF_BTT_QUERY_STATE_FAIL);
  577.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  578.                          " FAIL : BT tester < query state fail "
  579.                         );
  580.             return false;
  581.         }
  582.     }
  583.     while (BTT_STATUS_STANDBY != btt_state);
  584.     if (Abs_double(d_freq_err) > m_dBT_CRYSTAL_MAX_FREQ_ACCURACY)
  585.     {
  586.         CalErrorHandler(WM_MF_BT_CRYSTAL_CAPID_CHECK_FAIL);
  587.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  588.                   " Fail: BT CAP ID check ." + " frequency accuracy = " + Double_To_AnsiString(d_freq_err) +
  589.                   ", MAX frequency accuracy = " + Double_To_AnsiString(m_dBT_CRYSTAL_MAX_FREQ_ACCURACY)
  590.                 );
  591.         return false;
  592.     }
  593.     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  594.                   " Pass: BT CAP ID check ." + " frequency accuracy = " + Double_To_AnsiString(d_freq_err) +
  595.                   ", MAX frequency accuracy = " + Double_To_AnsiString(m_dBT_CRYSTAL_MAX_FREQ_ACCURACY)
  596.             );
  597.     PostMessage(ctrl.hPostMsgDestHandle,
  598.                 WM_MF_BT_CRYSTAL_CAPID_CALIBRATION_DONE,
  599.                 0,
  600.                 0
  601.                );
  602.     log->Add( "n" +DateToStr(Date()) +  " " + CurrentTimeStr() +
  603.               " =================  BT Crystal CAP ID check end  ================ "
  604.            );
  605.     frmFatcory->DisableAllCalTimer();
  606.     return true;
  607. }
  608. //===========================================================================
  609. bool __fastcall T_META_factory_calibration::BTCrystalCapIDCal_MT6611(void)
  610. {
  611.     PostMessage(ctrl.hPostMsgDestHandle,
  612.                 WM_MF_BT_CRYSTAL_CAPID_BEGIN,
  613.                 0,
  614.                 0
  615.                );
  616.     log->Add("n" +DateToStr(Date()) +  " " + CurrentTimeStr() +
  617.              " ================= BT Crystal CAP ID calibration begin ================ "
  618.            );
  619.     if (!m_pCal->s_cal_obj.pc_BT_INFO6611_Obj->REQ_Enable_PCM_Clock_Sync_Signal())
  620.     {
  621.         CalErrorHandler(WM_MF_BT_ENABLE_PCM_CLOCK_SYNC_SIGNAL_FAIL);
  622.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  623.                   " Fail: enable PCM clock sync. signal "
  624.                 );
  625.         return false;
  626.     }
  627.     unsigned char uc_pre_cap_id = BT_CAP_ID_MAX + 1;
  628.     unsigned int ui_min_poll_count;
  629.     unsigned int ui_poll_count_diff;
  630.     unsigned int ui_min_poll_count_diff = 2 * TARGET_POLL_COUNT;
  631.     unsigned char uc_min_cap_id;
  632.     unsigned char uc_cal_cap_id = m_pCal->s_cal_obj.pc_BT_INFO6611_Obj->Get_BT_CAP_ID();
  633.     unsigned char ucCAP_ID[2];
  634.     unsigned int ui_poll_count;
  635.     unsigned char uc_first_cap_id = BT_CAP_ID_MIN;
  636.     unsigned char uc_last_cap_id = BT_CAP_ID_MAX;
  637.     while ((ui_min_poll_count_diff != 0)      &&
  638.            (uc_first_cap_id < uc_last_cap_id) &&
  639.            (uc_cal_cap_id <= BT_CAP_ID_MAX)
  640.          )
  641.     {
  642.         if (!m_pCal->s_cal_obj.pc_BT_INFO6611_Obj->REQ_Set_CapId(uc_cal_cap_id))
  643.         {
  644.             CalErrorHandler(WM_MF_BT_SET_CRYSTAL_CAPID_FAIL);
  645.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  646.                   " Fail: set BT crystal CAP ID "
  647.                 );
  648.             return false;
  649.         }
  650.         if (!m_pCal->s_cal_obj.pc_BT_INFO6611_Obj->REQ_Start_Calibrate_Crystal_Clock())
  651.         {
  652.             CalErrorHandler(WM_MF_BT_START_CALIBRATE_CRYSTAL_CLOCK_FAIL);
  653.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  654.                      " Fail: BT start to calibrate crystal clock "
  655.                     );
  656.             return false;
  657.         }
  658.         
  659.         Sleep(100);
  660.         if (!m_pCal->s_cal_obj.pc_BT_INFO6611_Obj->REQ_Get_Result())
  661.         {
  662.             CalErrorHandler(WM_MF_BT_READ_CRYSTAL_CLOCK_RESULT_FAIL);
  663.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  664.                   " Fail: get BT crystal clock "
  665.                 );
  666.             return false;
  667.         }
  668.         ui_poll_count = m_pCal->s_cal_obj.pc_BT_INFO6611_Obj->Get_LpoPllCount();
  669.         ui_poll_count_diff = (unsigned int) abs(ui_poll_count - TARGET_POLL_COUNT);
  670.         if (ui_poll_count_diff < ui_min_poll_count_diff)
  671.         {
  672.             ui_min_poll_count_diff = ui_poll_count_diff;
  673.             ui_min_poll_count = ui_poll_count;
  674.             uc_min_cap_id = uc_cal_cap_id;
  675.         }
  676.         if (ui_poll_count < TARGET_POLL_COUNT)
  677.         {
  678.             uc_last_cap_id = uc_cal_cap_id - 1;
  679.         }
  680.         else if (ui_poll_count > TARGET_POLL_COUNT)
  681.         {
  682.             uc_first_cap_id = uc_cal_cap_id + 1;
  683.         }
  684.         if ((uc_first_cap_id >= uc_last_cap_id)  ||
  685.             (uc_pre_cap_id == uc_cal_cap_id)     ||
  686.             (TARGET_POLL_COUNT == ui_poll_count)
  687.            ) 
  688.         {
  689.             break;
  690.         }
  691.         uc_pre_cap_id = uc_cal_cap_id;
  692.         uc_cal_cap_id = (uc_first_cap_id + uc_last_cap_id) / 2;
  693.     }
  694.     m_pCal->s_cal_obj.pc_BT_INFO6611_Obj->Set_BT_CAP_ID(uc_min_cap_id);
  695.     m_pCal->s_cal_obj.pc_BT_INFO6611_Obj->ConfirmCallback = ::CNF_MF_BT;
  696.     m_pCal->s_cal_obj.pc_BT_INFO6611_Obj->REQ_Write_To_NVRAM_Start();
  697.     RestartTimerCal(WM_MF_NVRAM_BT_INFO_WRITE_FAIL);
  698.     SUSPEND_CAL_THREAD
  699.     CHECK_TERMINATE_BY_USER
  700.     E_METAAPP_RESULT_T state = m_pCal->s_cal_obj.pc_BT_INFO6611_Obj->Get_ConfirmState();
  701.     if (state != METAAPP_SUCCESS)
  702.     {
  703.         CalErrorHandler(WM_MF_NVRAM_BT_INFO_WRITE_FAIL);
  704.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  705.                  " FAIL: Target < write BT CAP ID to NVRAM fail."
  706.                  );
  707.         return false;
  708.     }
  709.     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  710.              " Target < write BT CAP ID = " + IntToStr(uc_min_cap_id) + " to NVRAM."
  711.              );
  712.     if (!WriteBtCapIDCalResultToFile(as_ID+".cal", Application->ExeName, m_pCal->b_CalResultPath, m_pCal->s_bt_bb_id))
  713.     {
  714.         CalErrorHandler(WM_MF_BT_CRYSTAL_CAPID_WRITE_CAL_RESULT_FAIL);
  715.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  716.                 " FAIL : write AFC parameter to cal file "
  717.               );
  718.         return false;
  719.     }
  720.     if ((ui_min_poll_count > m_uiMAX_POLL_COUNT) || (ui_min_poll_count < m_uiMIN_POLL_COUNT))
  721.     {
  722.         CalErrorHandler(WM_MF_BT_CRYSTAL_CAPID_CHECK_FAIL);
  723.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  724.                   " Fail: BT CAP ID check ." + " poll count = " + IntToStr(ui_min_poll_count) +
  725.                   ", min poll count = " + IntToStr(m_uiMIN_POLL_COUNT) +
  726.                   ", max poll count = " + IntToStr(m_uiMAX_POLL_COUNT)
  727.                 );
  728.         return false;
  729.     }
  730.     PostMessage(ctrl.hPostMsgDestHandle,
  731.                 WM_MF_BT_CRYSTAL_CAPID_CALIBRATION_DONE,
  732.                 0,
  733.                 0
  734.                );
  735.     return true;
  736. }