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

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) 2001
  8. *
  9. *****************************************************************************/
  10. /*****************************************************************************
  11.  *
  12.  * Filename:
  13.  * ---------
  14.  *   T_IP2_MT6139.cpp
  15.  *
  16.  * Project:
  17.  * --------
  18.  *   Maui META APP
  19.  *
  20.  * Description:
  21.  * ------------
  22.  *  MT6139 IP2 calibration source
  23.  *
  24.  * Author:
  25.  * -------
  26.  *  Andy Ueng (mtk00490)
  27.  *
  28.  *============================================================================
  29.  *             HISTORY
  30.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  31.  *------------------------------------------------------------------------------
  32.  * $Revision$
  33.  * $Modtime$
  34.  * $Log$
  35.  *
  36.  *------------------------------------------------------------------------------
  37.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  38.  *============================================================================
  39. ****************************************************************************/
  40. #include <assert.h>
  41. #include <math.h>
  42. #pragma hdrstop
  43. #ifndef _T_META_FACTORY_CALIBRATION_H_
  44. #include "T_META_factory_calibration.H"
  45. #endif
  46. #ifndef _CAL_COMMON_H_
  47. #include "cal_common.h"
  48. #endif
  49. // form
  50. #ifndef _META_FACTORY_H_
  51. #include "META_Factory.h"
  52. #endif
  53. // call back
  54. #ifndef _META_FACTORY_NVRAM_CB_H_
  55. #include "meta_factory_nvram_cb.h"
  56. #endif
  57. #ifndef _META_FACTORY_RF_CB_H_
  58. #include "meta_factory_rf_cb.h"
  59. #endif
  60. // misc
  61. #ifndef  _FT_UTILS_H_
  62. #include "ft_utils.h"
  63. #endif
  64. #ifndef  _BAND_UTILS_H_
  65. #include "band_utils.h"
  66. #endif
  67. #ifndef  _TIME_UTILS_H_
  68. #include "time_utils.h"
  69. #endif
  70. // equipment
  71. #ifndef _AGECOMMON_H_
  72. #include "agecommon.h"
  73. #endif
  74. //----------------------------------------------------------------------------
  75. // IP2
  76. extern CRFPM*  MF_rf_pm_ptr;
  77. extern RfPm_Cnf  *MF_rf_pm_cnf;   // define in T_META_factory_calibration.cpp
  78. extern RfPm_Req  *MF_rf_pm_req;
  79. // Tthread
  80. extern T_META_factory_calibration* pt_calibration;
  81. extern bool  is_suspend_cal; // in T_META_factory_calibration.cpp
  82. //============================================================================
  83. ////////////////////////           Call back           ///////////////////////
  84. //============================================================================
  85. void ccb_SetMT6139CoefToReg( void )
  86. {
  87.      frmFatcory->DisableAllCalTimer();
  88.      if(is_suspend_cal == true && pt_calibration != NULL )
  89.      {
  90.                     is_suspend_cal = false;
  91.                     pt_calibration->Resume();
  92.      }
  93. }
  94. //----------------------------------------------------------------------------
  95. void ccb_GetAcodeFromReg( void )
  96. {
  97.     frmFatcory->DisableAllCalTimer();
  98.      if(is_suspend_cal == true && pt_calibration != NULL )
  99.      {
  100.                     is_suspend_cal = false;
  101.                     pt_calibration->Resume();
  102.      }
  103. }
  104. //----------------------------------------------------------------------------
  105. void ccb_GetBCcodeFromReg( void )
  106. {
  107.     frmFatcory->DisableAllCalTimer();
  108.      if(is_suspend_cal == true && pt_calibration != NULL )
  109.      {
  110.                     is_suspend_cal = false;
  111.                     pt_calibration->Resume();
  112.      }
  113. }
  114. //----------------------------------------------------------------------------
  115. void ccb_GetMT6139CW7FromReg( void )
  116. {
  117.     frmFatcory->DisableAllCalTimer();
  118.      if(is_suspend_cal == true && pt_calibration != NULL )
  119.      {
  120.                     is_suspend_cal = false;
  121.                     pt_calibration->Resume();
  122.      }
  123. }
  124. //----------------------------------------------------------------------------
  125. void ccb_SetMT6139CW2ToReg( void )
  126. {
  127.     frmFatcory->DisableAllCalTimer();
  128.      if(is_suspend_cal == true && pt_calibration != NULL )
  129.      {
  130.                     is_suspend_cal = false;
  131.                     pt_calibration->Resume();
  132.      }
  133. }
  134. //----------------------------------------------------------------------------
  135. void ccb_SetMT6139CW7ToReg( void )
  136. {
  137.     frmFatcory->DisableAllCalTimer();
  138.      if(is_suspend_cal == true && pt_calibration != NULL )
  139.      {
  140.                     is_suspend_cal = false;
  141.                     pt_calibration->Resume();
  142.      }
  143. }
  144. //----------------------------------------------------------------------------
  145. void ccb_SetBCcodeToReg( void )
  146. {
  147.     frmFatcory->DisableAllCalTimer();
  148.      if(is_suspend_cal == true && pt_calibration != NULL )
  149.      {
  150.                     is_suspend_cal = false;
  151.                     pt_calibration->Resume();
  152.      }
  153. }
  154. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  155. ///////////////////////     MT6139B IP2 calibration     ///////////////////////
  156. //---------------------------------------------------------------------------
  157. bool __fastcall T_META_factory_calibration::IP2Cal_mt6139b_cal(void)
  158. {
  159.     char tempbuf[256];
  160.     PostMessage(
  161.                      ctrl.hPostMsgDestHandle,
  162.                      WM_MF_RF_IP2_BEGIN,
  163.                      0,
  164.                      0
  165.                     );
  166.      log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  167.              " ================== IP2 calibration begin ==================="
  168.            );
  169. #if 0
  170.     // read IP2 register from NVRAM
  171.     MF_rf_pm_ptr->ConfirmCallback = ::ccb_read_ip2_from_nvram;
  172.     MF_rf_pm_ptr->REQ_Read_IP2Reg_From_NVRAM(final_rfid);
  173.     CHECK_TERMINATE_BY_USER
  174.     RestartTimerCal( WM_MF_NVRAM_IP2_READ_FAIL );
  175.     SUSPEND_CAL_THREAD
  176.     if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  177.     {
  178.         CalErrorHandler( WM_MF_NVRAM_IP2_READ_FAIL );
  179.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  180.                       " FAIL: Target > read IP2 from NVRAM fail."
  181.                     );
  182.             return false;
  183.     }
  184.     else
  185.     {
  186.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  187.                       " Target > read IP2 from NVRAM."
  188.                     );
  189.     }
  190.     MF_rf_pm_ptr->Get_ucMT6139Coef( uc_mt6139_coef );
  191. #endif
  192.     if( ! m_rct_ctrl.RCT_operatingMode( m_pRct, OPERATING_MODE_GSM_BCH ) )
  193.     {
  194.         CalErrorHandler( WM_MF_AGE8960_SET_OPERATION_MODE_FAIL );
  195.         if ( frmFatcory->rbAgilent8960->Checked )
  196.         {
  197.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  198.                               " FAIL :"+
  199.                               " Agilent 8960< Operation mode = GSM BCH"
  200.                             );
  201.         }
  202.         else
  203.         {
  204.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  205.                               " FAIL :"+
  206.                               " cmu200 < Band = Operation mode = GSM BCH"
  207.                             );
  208.         }
  209.         return false;
  210.     }
  211.     short s_ARFCN;
  212.     FrequencyBand eFreqBand = FrequencyBand850;
  213.     int vi_Band;
  214.     while (eFreqBand < FrequencyBandCount)
  215.     {
  216.         switch (eFreqBand)
  217.         {
  218.             case FrequencyBand850:
  219.             {
  220.                 if (! IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  221.                 {
  222.                    eFreqBand++;
  223.                    continue;
  224.                 }
  225.                 vi_Band = age1960_GSM850_BAND;
  226.                 s_ARFCN = m_sIP2_arfcn_C0_GSM850;
  227.             }
  228.             break;
  229.             case FrequencyBand900:
  230.             {
  231.                 if (! IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  232.                 {
  233.                     eFreqBand++;
  234.                     continue;
  235.                 }
  236.                 vi_Band = age1960_EGSM_BAND;
  237.                 s_ARFCN = m_sIP2_arfcn_C0_GSM;
  238.             }
  239.             break;
  240.             case FrequencyBand1800:
  241.             {
  242.                 if (! IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  243.                 {
  244.                     eFreqBand++;
  245.                     continue;
  246.                 }
  247.                 vi_Band = age1960_DCS1800_BAND;
  248.                 s_ARFCN = m_sIP2_arfcn_C0_DCS;
  249.             }
  250.             break;
  251.             case FrequencyBand1900:
  252.             {
  253.                 if (! IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  254.                 {
  255.                     eFreqBand++;
  256.                     continue;
  257.                 }
  258.                 vi_Band = age1960_PCS1900_BAND;
  259.                 s_ARFCN = m_sIP2_arfcn_C0_PCS;
  260.             }
  261.             break;
  262.             default:
  263.                 assert(false);
  264.             break;
  265.         } // switch
  266.         if( ! m_rct_ctrl.RCT_cellBand( m_pRct, vi_Band ) )
  267.         {
  268.             CalErrorHandler( WM_MF_AGE8960_SET_BAND_FAIL );
  269.             if ( frmFatcory->rbAgilent8960->Checked )
  270.             {
  271.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  272.                               " FAIL :"+
  273.                               " Agilent 8960< Band = " + ViBand_To_Str( vi_Band )
  274.                             );
  275.             }
  276.             else
  277.             {
  278.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  279.                               " FAIL :"+
  280.                               " cmu200 > Band = " + ViBand_To_Str( vi_Band )
  281.                             );
  282.             }
  283.             return false;
  284.         }
  285.         if ( this->Terminated )
  286.         {
  287.             this->OnTerminate = neByUser;
  288.             return false;
  289.         }
  290.         if ( frmFatcory->rbAgilent8960->Checked  )
  291.         {
  292.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< band = " +
  293.                       FreqBand_To_Str( eFreqBand) );
  294.         }
  295.         else
  296.         {
  297.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< band = " +
  298.                       FreqBand_To_Str( eFreqBand) );
  299.         }
  300.         if( ! m_rct_ctrl.RCT_ContModTran( m_pRct ) )
  301.         {
  302.             CalErrorHandler( WM_MF_AGE8960_SET_BAND_FAIL );
  303.             if ( frmFatcory->rbAgilent8960->Checked )
  304.             {
  305.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  306.                               " FAIL :"+
  307.                               " Agilent 8960< Set continuous tranmittion mode "
  308.                             );
  309.             }
  310.             else
  311.             {
  312.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  313.                               " FAIL :"+
  314.                               " cmu200< Set continuous tranmittion mode "
  315.                             );
  316.             }
  317.             return false;
  318.         }
  319.         if (IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  320.         {
  321.             if(eFreqBand == FrequencyBand1900)
  322.             {
  323.                 if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle,  1, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  324.                 {
  325.                     CalErrorHandler( WM_MF_RF_PM_SELECT_BAND_FAIL );
  326.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  327.                                               " FAIL:  Target < set Band1900 flag = 1 "
  328.                                              );
  329.                     return false;
  330.                 }
  331.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  332.                            " Target < set Band1900 flag = 1"
  333.                          );
  334.             }
  335.             else
  336.             {
  337.                 if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle,  0, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  338.                 {
  339.                     CalErrorHandler( WM_MF_RF_PM_SELECT_BAND_FAIL );
  340.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  341.                                            " FAIL: Target < set Band1900 flag = 0 "
  342.                                           );
  343.                     return false;
  344.                 }
  345.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  346.                             " Target < set Band1900 flag = 0"
  347.                          );
  348.             }
  349.             is_suspend_cal = true;
  350.             this->Suspend();
  351.             is_suspend_cal = false;
  352.         }
  353.         if( ! m_rct_ctrl.RCT_BCHARFCN( m_pRct, s_ARFCN ) )
  354.         {
  355.             CalErrorHandler( WM_MF_AGE8960_SET_BCH_ARFCN_FAIL );
  356.             if ( frmFatcory->rbAgilent8960->Checked )
  357.             {
  358.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: Agilent 8960< BCH ARFCN = " +
  359.                           AnsiString(s_ARFCN)
  360.                         );
  361.             }
  362.             else
  363.             {
  364.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: cmu200 < BCH ARFCN = " +
  365.                            AnsiString(s_ARFCN)
  366.                          );
  367.             }
  368.             return false;
  369.         }
  370.         if ( this->Terminated )
  371.         {
  372.             this->OnTerminate = neByUser;
  373.             return false;
  374.         }
  375.         if ( frmFatcory->rbAgilent8960->Checked )
  376.         {
  377.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< BCH ARFCN = " +
  378.                       AnsiString(s_ARFCN)
  379.                      );
  380.         }
  381.         else
  382.         {
  383.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< BCH ARFCN = " +
  384.                       AnsiString(s_ARFCN)
  385.                     );
  386.         }
  387.         i_IP2_AMCODEI[0]=i_IP2_AMCODEI1;
  388.         i_IP2_AMCODEI[1]=i_IP2_AMCODEI2;
  389.         i_IP2_AMCODEQ[0]=i_IP2_AMCODEQ1;
  390.         i_IP2_AMCODEQ[1]=i_IP2_AMCODEQ2;
  391.         for( int i=0; i<2; i++)
  392.         {
  393.             //===========================================================
  394.             // 1.0 SS power off
  395.             if( ! m_rct_ctrl.RCT_cellPower( m_pRct, d_IP2_CAL_P_DL_OFF_MT ) )
  396.             {
  397.                 CalErrorHandler( WM_MF_AGE8960_SET_POWER_FAIL );
  398.                 if( frmFatcory->rbAgilent8960->Checked )
  399.                 {
  400.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: Agilent 8960< Power = " +
  401.                           Double_To_AnsiString( d_IP2_CAL_P_DL_OFF_MT) ) + " dBm";
  402.                 }
  403.                 else
  404.                 {
  405.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: cmu200< Power = " +
  406.                           Double_To_AnsiString( d_IP2_CAL_P_DL_OFF_MT) ) + " dBm";
  407.                 }
  408.                 return false;
  409.             }
  410.             if ( this->Terminated )
  411.             {
  412.                 this->OnTerminate = neByUser;
  413.                 return false;
  414.             }
  415.             if( frmFatcory->rbAgilent8960->Checked )
  416.             {
  417.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< Power = " +
  418.                       Double_To_AnsiString( d_IP2_CAL_P_DL_OFF_MT) ) + " dBm";
  419.             }
  420.             else
  421.             {
  422.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< Power = " +
  423.                               Double_To_AnsiString( d_IP2_CAL_P_DL_OFF_MT) ) + " dBm";
  424.             }
  425.           //  if( frmFatcory->cbAFC->Checked || frmFatcory->cbRxPathLoss->Checked )
  426.           //  {
  427.            //     Sleep( 100 );  // wait Agilent 8960 ready!
  428.            // }
  429.            // else
  430.             {
  431.                 Sleep( 200 );  // wait Agilent 8960 ready!
  432.             }
  433.             //-------------------------------------------------------------
  434.             // 1.1 set AM, rxamcalmode=0
  435.             uc_mt6139_coef.rx.coef[eFreqBand].amcodeI = i_IP2_AMCODEI[i];
  436.             uc_mt6139_coef.rx.coef[eFreqBand].amcodeQ = i_IP2_AMCODEQ[i];
  437.             uc_mt6139_coef.rx.rxamcalmode = RXAMCALMODE_MT6139B_READ_ACODE;
  438.             MF_rf_pm_ptr->Set_ucMT6139Coef( uc_mt6139_coef );
  439.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  440.                           " Target < Set amcodeI = " + IntToStr( i_IP2_AMCODEI[i] ) +
  441.                           ", amcodeQ = " + IntToStr( i_IP2_AMCODEQ[i] ) +
  442.                           ", rxamcalmode=0"
  443.                         );
  444.             // write AM to NVRAM
  445.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_write_ip2_to_nvram;
  446.             MF_rf_pm_ptr->REQ_Write_IP2Reg_To_NVRAM_Start(m_pCal->ui_rf_id);
  447.             CHECK_TERMINATE_BY_USER
  448.             RestartTimerCal(WM_MF_NVRAM_IP2_WRITE_FAIL);
  449.             SUSPEND_CAL_THREAD
  450.             E_METAAPP_RESULT_T state = MF_rf_pm_ptr->Get_ConfirmState();
  451.             if (state != METAAPP_SUCCESS)
  452.             {
  453.                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  454.                 {
  455.                     CalErrorHandler(WM_MF_NVRAM_EF_L1_RFSPECIALCOEF_LID_VERNO_FAIL);
  456.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  457.                          " FAIL : NVRAM_EF_L1_RFSPECIALCOEF_LID version is not support, please update META to latest version. "
  458.                          );
  459.                 }
  460.                 else
  461.                 {
  462.                     CalErrorHandler(WM_MF_NVRAM_IP2_WRITE_FAIL);
  463.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  464.                           " FAIL: Target < write amcodeI, amcodeQ, rxamcalmode=0 to NVRAM fail."
  465.                         );
  466.                 }
  467.                 return false;
  468.             }
  469.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  470.                       " Target < write amcodeI, amcodeQ, rxamcalmode=0 to NVRAM."
  471.                     );
  472.             //------------------------------------------------------------
  473.             // 1.2 set gain
  474.            // PM
  475.            // if Agilent 8960 not read, target will get error power!
  476.            MF_rf_pm_cnf->deviation = 0;
  477.            MF_rf_pm_cnf->power = 0;
  478.            MF_rf_pm_cnf->usedGain = 0;
  479.            MF_rf_pm_cnf->extra_info.valid =0;
  480.            MF_rf_pm_cnf->extra_info.iOffset=0;
  481.    MF_rf_pm_cnf->extra_info.qOffset=0;
  482.    MF_rf_pm_cnf->extra_info.validSamples =0;
  483.            MF_rf_pm_req->arfcn = s_ARFCN-30;
  484.            MF_rf_pm_req->sampleNoPerFrame = c_IP2_M_PM;
  485.            MF_rf_pm_req->gain = d_IP2_GAIN *AVG_POWER_PC_SIDE_MULTIPLY ;
  486.            MF_rf_pm_req->frames = i_IP2_N_PM;
  487.            if( META_Rf_PM_r( m_pCal->i_MainMETAHandle, MF_rf_pm_req, MF_rf_pm_cnf_cb, &MF_rf_pm_token, NULL ) != META_SUCCESS )
  488.            {
  489.                 CalErrorHandler( WM_MF_RF_IP2_PM_FAIL );
  490.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  491.                                " FAIL: "+
  492.                               " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  493.                               " sample per frame = " + IntToStr( c_IP2_M_PM) +
  494.                               " gain = " + IntToStr( MF_rf_pm_req->gain ) +
  495.                               " frames = " + IntToStr( i_IP2_N_PM)
  496.                             );
  497.                         return false;
  498.             } // if
  499.             if ( this->Terminated )
  500.             {
  501.                 this->OnTerminate = neByUser;
  502.                 return false;
  503.             }
  504.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  505.                       " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  506.                       ", sample per frame = " + IntToStr( c_IP2_M_PM) +
  507.                       ", gain = " + IntToStr( MF_rf_pm_req->gain ) +
  508.                       ", frames = " + IntToStr( i_IP2_N_PM)
  509.                     );
  510.            //  Sleep(100);// delay for power measurement
  511.             RestartTimerCal( WM_MF_RF_IP2_PM_FAIL );
  512.             is_suspend_cal = true;
  513.             this->Suspend();
  514.             is_suspend_cal = false;
  515.             if ( this->Terminated )
  516.             {
  517.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  518.                       " Terminate at line " + AnsiString(__FILE__) + " " + AnsiString(__LINE__)
  519.                         );
  520.                 this->OnTerminate = neByUser;
  521.                 return false;
  522.             }
  523.             if( !MF_rf_pm_cnf->ok )
  524.             {
  525.                 CalErrorHandler( WM_MF_RF_IP2_FAIL );
  526.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  527.                           " Target < ok = " + IntToStr(MF_rf_pm_cnf->ok) +
  528.                           ", power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  529.                           ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  530.                           ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  531.                           ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  532.                           ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  533.                           ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  534.                           ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  535.                         );
  536.                 return false;
  537.             }
  538.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  539.                       " Target > power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  540.                       ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  541.                       ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  542.                       ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  543.                       ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  544.                       ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  545.                       ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  546.                    );
  547.            // EvalDC_I_sky[i] = MF_rf_pm_cnf->extra_info.iOffset;
  548.            // EvalDC_Q_sky[i] = MF_rf_pm_cnf->extra_info.qOffset;
  549.            //--------------------------------------------------------------
  550.            // 1.3 read A code
  551.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_GetAcodeFromReg;
  552.             MF_rf_pm_ptr->Get_AcodeFromReg_Start( eFreqBand, uc_mt6139_coef, m_pCal->ui_rf_id );
  553.             CHECK_TERMINATE_BY_USER
  554.             RestartTimerCal( WM_MF_RF_IP2_GET_ACODE_FAIL );
  555.             SUSPEND_CAL_THREAD
  556.             if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  557.             {
  558.                 CalErrorHandler( WM_MF_RF_IP2_GET_ACODE_FAIL );
  559.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  560.                           " FAIL: Target> get acode from register."
  561.                         );
  562.                 return false;
  563.             }
  564.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  565.                           " Target> get acode from register:" +
  566.                           " acodeI = " + IntToStr( uc_mt6139_coef.rx.coef[eFreqBand].acodeI ) +
  567.                           ", acodeQ = " + IntToStr( uc_mt6139_coef.rx.coef[eFreqBand].acodeQ )
  568.                         );
  569.             //------------------------------------------------------------
  570.             // 1.4 set stand-by mode(010)
  571.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_SetMT6139CW2ToReg;
  572.             MF_rf_pm_ptr->Set_MT6139CW2ToReg_Start( MODE_MT6139_CW2_STAND_BY, m_pCal->ui_rf_id );
  573.             CHECK_TERMINATE_BY_USER
  574.             RestartTimerCal( WM_MF_RF_IP2_SET_CW2_FAIL );
  575.             SUSPEND_CAL_THREAD
  576.             if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  577.             {
  578.                 CalErrorHandler( WM_MF_RF_IP2_SET_CW2_FAIL );
  579.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  580.                           " FAIL: Target < set CW2 to register."
  581.                         );
  582.                 return false;
  583.             }
  584.             //===============================================================
  585.             // 2.1 set rxamcalmode=1, set A code, gain
  586.             uc_mt6139_coef.rx.rxamcalmode = RXAMCALMODE_MT6139B_READ_BCCODE;
  587.             MF_rf_pm_ptr->Set_ucMT6139Coef( uc_mt6139_coef );
  588.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  589.                           " Target < Set acodeI = " + IntToStr( uc_mt6139_coef.rx.coef[eFreqBand].acodeI ) +
  590.                           ", acodeQ = " + IntToStr( uc_mt6139_coef.rx.coef[eFreqBand].acodeQ ) +
  591.                           ", rxamcalmode=1"
  592.                         );
  593.             // write rxamcalmode=1, A code
  594.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_write_ip2_to_nvram;
  595.             MF_rf_pm_ptr->REQ_Write_IP2Reg_To_NVRAM_Start(m_pCal->ui_rf_id);
  596.             CHECK_TERMINATE_BY_USER
  597.             RestartTimerCal(WM_MF_NVRAM_IP2_WRITE_FAIL);
  598.             SUSPEND_CAL_THREAD
  599.             state = MF_rf_pm_ptr->Get_ConfirmState();
  600.             if (state != METAAPP_SUCCESS)
  601.             {
  602.                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  603.                 {
  604.                     CalErrorHandler(WM_MF_NVRAM_EF_L1_RFSPECIALCOEF_LID_VERNO_FAIL);
  605.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  606.                          " FAIL : NVRAM_EF_L1_RFSPECIALCOEF_LID version is not support, please update META to latest version. "
  607.                          );
  608.                 }
  609.                 else
  610.                 {
  611.                     CalErrorHandler(WM_MF_NVRAM_IP2_WRITE_FAIL);
  612.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  613.                           " FAIL: Target < write acodeI, acodeQ, rxamcalmode=1 to NVRAM fail."
  614.                         );
  615.                 }
  616.                 return false;
  617.             }
  618.             else
  619.             {
  620.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  621.                       " Target < write acodeI, acodeQ, rxamcalmode=1 to NVRAM."
  622.                     );
  623.             }
  624.            // set gain, PM
  625.            // if Agilent 8960 not read, target will get error power!
  626.            MF_rf_pm_cnf->deviation = 0;
  627.            MF_rf_pm_cnf->power = 0;
  628.            MF_rf_pm_cnf->usedGain = 0;
  629.            MF_rf_pm_cnf->extra_info.valid =0;
  630.            MF_rf_pm_cnf->extra_info.iOffset=0;
  631.    MF_rf_pm_cnf->extra_info.qOffset=0;
  632.    MF_rf_pm_cnf->extra_info.validSamples =0;
  633.            MF_rf_pm_req->arfcn = s_ARFCN-30;
  634.            MF_rf_pm_req->sampleNoPerFrame = c_IP2_M_PM;
  635.            MF_rf_pm_req->gain = d_IP2_GAIN *AVG_POWER_PC_SIDE_MULTIPLY ;
  636.            MF_rf_pm_req->frames = i_IP2_N_PM;
  637.            if( META_Rf_PM_r( m_pCal->i_MainMETAHandle, MF_rf_pm_req, MF_rf_pm_cnf_cb, &MF_rf_pm_token, NULL ) != META_SUCCESS )
  638.            {
  639.                 CalErrorHandler( WM_MF_RF_IP2_PM_FAIL );
  640.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  641.                                " FAIL: "+
  642.                               " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  643.                               " sample per frame = " + IntToStr( c_IP2_M_PM) +
  644.                               " gain = " + IntToStr( MF_rf_pm_req->gain ) +
  645.                               " frames = " + IntToStr( i_IP2_N_PM)
  646.                             );
  647.                         return false;
  648.             } // if
  649.             if ( this->Terminated )
  650.             {
  651.                 this->OnTerminate = neByUser;
  652.                 return false;
  653.             }
  654.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  655.                       " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  656.                       ", sample per frame = " + IntToStr( c_IP2_M_PM) +
  657.                       ", gain = " + IntToStr( MF_rf_pm_req->gain ) +
  658.                       ", frames = " + IntToStr( i_IP2_N_PM)
  659.                     );
  660.            //  Sleep(100);// delay for power measurement
  661.             RestartTimerCal( WM_MF_RF_IP2_PM_FAIL );
  662.             is_suspend_cal = true;
  663.             this->Suspend();
  664.             is_suspend_cal = false;
  665.             if ( this->Terminated )
  666.             {
  667.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  668.                       " Terminate at line " + AnsiString(__FILE__) + " " + AnsiString(__LINE__)
  669.                         );
  670.                 this->OnTerminate = neByUser;
  671.                 return false;
  672.             }
  673.             if( !MF_rf_pm_cnf->ok )
  674.             {
  675.                 CalErrorHandler( WM_MF_RF_IP2_FAIL );
  676.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  677.                           " Target < ok = " + IntToStr(MF_rf_pm_cnf->ok) +
  678.                           ", power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  679.                           ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  680.                           ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  681.                           ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  682.                           ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  683.                           ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  684.                           ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  685.                         );
  686.                 return false;
  687.             }
  688.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  689.                       " Target > power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  690.                       ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  691.                       ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  692.                       ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  693.                       ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  694.                       ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  695.                       ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  696.                    );
  697.             //------------------------------------------------------------
  698.             // 2.2 read B & C code
  699.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_GetBCcodeFromReg;
  700.             MF_rf_pm_ptr->Get_BCcodeFromReg_Start( eFreqBand, uc_mt6139_coef, m_pCal->ui_rf_id );
  701.             CHECK_TERMINATE_BY_USER
  702.             RestartTimerCal( WM_MF_RF_IP2_GET_BCCODE_FAIL );
  703.             SUSPEND_CAL_THREAD
  704.             if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  705.             {
  706.                 CalErrorHandler( WM_MF_RF_IP2_GET_BCCODE_FAIL );
  707.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  708.                           " FAIL: Target> read IP2 B,C code from register fail."
  709.                         );
  710.                 return false;
  711.             }
  712.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  713.                           " Target> get B, C code from register:" +
  714.                           " bcodeI = " + IntToStr( uc_mt6139_coef.rx.coef[eFreqBand].bcodeI ) +
  715.                           ", bcodeQ = " + IntToStr( uc_mt6139_coef.rx.coef[eFreqBand].bcodeQ ) +
  716.                           " ccodeI = " + IntToStr( uc_mt6139_coef.rx.coef[eFreqBand].ccodeI ) +
  717.                           ", ccodeQ = " + IntToStr( uc_mt6139_coef.rx.coef[eFreqBand].ccodeQ )
  718.                         );
  719.             //--------------------------------------------------------------
  720.             // 2.3  set stand-by mode(010)
  721.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_SetMT6139CW2ToReg;
  722.             MF_rf_pm_ptr->Set_MT6139CW2ToReg_Start( MODE_MT6139_CW2_STAND_BY, m_pCal->ui_rf_id );
  723.             CHECK_TERMINATE_BY_USER
  724.             RestartTimerCal( WM_MF_RF_IP2_SET_CW2_FAIL );
  725.             SUSPEND_CAL_THREAD
  726.             if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  727.             {
  728.                 CalErrorHandler( WM_MF_RF_IP2_SET_CW2_FAIL );
  729.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  730.                           " FAIL: Target < set CW2 to register."
  731.                         );
  732.                 return false;
  733.             }
  734.             //---------------------------------------------------------------
  735.             // 2.4 set AUTO_CAL = 0 (CW7)
  736.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_GetMT6139CW7FromReg;
  737.             MF_rf_pm_ptr->Get_MT6139CW7FromReg_Start( m_mt6139_cw7, m_pCal->ui_rf_id );
  738.             CHECK_TERMINATE_BY_USER
  739.             RestartTimerCal( WM_MF_RF_IP2_SET_CW2_FAIL );
  740.             SUSPEND_CAL_THREAD
  741.             if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  742.             {
  743.                 CalErrorHandler( WM_MF_RF_IP2_GET_CW7_FAIL );
  744.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  745.                           " FAIL: Target> get CW7 from register."
  746.                         );
  747.                 return false;
  748.             }
  749.             m_mt6139_cw7.uc_auto_cal = 0;
  750.             m_mt6139_cw7.uc_man_cal  = 1;
  751.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_SetMT6139CW7ToReg;
  752.             MF_rf_pm_ptr->Set_MT6139CW7ToReg_Start( m_mt6139_cw7, m_pCal->ui_rf_id );
  753.             CHECK_TERMINATE_BY_USER
  754.             RestartTimerCal( WM_MF_RF_IP2_SET_CW7_FAIL );
  755.             SUSPEND_CAL_THREAD
  756.             if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  757.             {
  758.                 CalErrorHandler( WM_MF_RF_IP2_SET_CW7_FAIL );
  759.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  760.                           " FAIL: Target < set CW7 auto_cal=0 to register."
  761.                         );
  762.                 return false;
  763.             }
  764.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  765.                           " Target < set CW7 auto_cal=0 to register."
  766.                         );
  767.                         
  768.             //---------------------------------------------------------------
  769.             // 2.5 set I channel B & C code
  770.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_SetBCcodeToReg;
  771.             MF_rf_pm_ptr->Set_BCcodeToReg_Start( eFreqBand, uc_mt6139_coef, m_pCal->ui_rf_id );
  772.             CHECK_TERMINATE_BY_USER
  773.             RestartTimerCal( WM_MF_RF_IP2_SET_BCCODE_FAIL );
  774.             SUSPEND_CAL_THREAD
  775.             if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  776.             {
  777.                 CalErrorHandler( WM_MF_RF_IP2_SET_BCCODE_FAIL );
  778.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  779.                           " FAIL: Target < set B, C code to register."
  780.                         );
  781.                 return false;
  782.             }
  783.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  784.                           " Target < set B, C code to register."
  785.                         );
  786.             //===============================================================
  787.             // 3.1 set rxamcalmode=2, set gain
  788.             uc_mt6139_coef.rx.rxamcalmode = RXAMCALMODE_MT6139B_NORMAL;
  789.             MF_rf_pm_ptr->Set_ucMT6139Coef( uc_mt6139_coef );
  790.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  791.                           "Target < rxamcalmode=2"
  792.                         );
  793.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_write_ip2_to_nvram;
  794.             MF_rf_pm_ptr->REQ_Write_IP2Reg_To_NVRAM_Start(m_pCal->ui_rf_id);
  795.             CHECK_TERMINATE_BY_USER
  796.             RestartTimerCal(WM_MF_NVRAM_IP2_WRITE_FAIL);
  797.             SUSPEND_CAL_THREAD
  798.             state = MF_rf_pm_ptr->Get_ConfirmState();
  799.             if (state != METAAPP_SUCCESS)
  800.             {
  801.                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  802.                 {
  803.                     CalErrorHandler(WM_MF_NVRAM_EF_L1_RFSPECIALCOEF_LID_VERNO_FAIL);
  804.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  805.                          " FAIL : NVRAM_EF_L1_RFSPECIALCOEF_LID version is not support, please update META to latest version. "
  806.                          );
  807.                 }
  808.                 else
  809.                 {
  810.                     CalErrorHandler(WM_MF_NVRAM_IP2_WRITE_FAIL);
  811.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  812.                           " FAIL: Target < write rxamcalmode=2 to NVRAM fail."
  813.                         );
  814.                 }
  815.                 return false;
  816.             }
  817.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  818.                       " Target < write rxamcalmode=2 to NVRAM."
  819.                     );
  820.             // 3.2 get MDC1 from DSP
  821.             MF_rf_pm_cnf->deviation = 0;
  822.             MF_rf_pm_cnf->power = 0;
  823.             MF_rf_pm_cnf->usedGain = 0;
  824.             MF_rf_pm_cnf->extra_info.valid =0;
  825.             MF_rf_pm_cnf->extra_info.iOffset=0;
  826.     MF_rf_pm_cnf->extra_info.qOffset=0;
  827.     MF_rf_pm_cnf->extra_info.validSamples =0;
  828.             MF_rf_pm_req->arfcn = s_ARFCN-30;
  829.             MF_rf_pm_req->sampleNoPerFrame = c_IP2_M_PM;
  830.             MF_rf_pm_req->gain = d_IP2_GAIN *AVG_POWER_PC_SIDE_MULTIPLY ;
  831.             MF_rf_pm_req->frames = i_IP2_N_PM;
  832.             if( META_Rf_PM_r( m_pCal->i_MainMETAHandle, MF_rf_pm_req, MF_rf_pm_cnf_cb, &MF_rf_pm_token, NULL ) != META_SUCCESS )
  833.             {
  834.                 CalErrorHandler( WM_MF_RF_IP2_PM_FAIL );
  835.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  836.                                " FAIL: "+
  837.                               " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  838.                               " sample per frame = " + IntToStr( c_IP2_M_PM) +
  839.                               " gain = " + IntToStr( MF_rf_pm_req->gain ) +
  840.                               " frames = " + IntToStr( i_IP2_N_PM)
  841.                             );
  842.                         return false;
  843.             } // if
  844.             if ( this->Terminated )
  845.             {
  846.                 this->OnTerminate = neByUser;
  847.                 return false;
  848.             }
  849.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  850.                       " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  851.                       ", sample per frame = " + IntToStr( c_IP2_M_PM) +
  852.                       ", gain = " + IntToStr( MF_rf_pm_req->gain ) +
  853.                       ", frames = " + IntToStr( i_IP2_N_PM)
  854.                     );
  855.            //  Sleep(100);// delay for power measurement
  856.             RestartTimerCal( WM_MF_RF_IP2_PM_FAIL );
  857.             is_suspend_cal = true;
  858.             this->Suspend();
  859.             is_suspend_cal = false;
  860.             if ( this->Terminated )
  861.             {
  862.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  863.                       " Terminate at line " + AnsiString(__FILE__) + " " + AnsiString(__LINE__)
  864.                         );
  865.                 this->OnTerminate = neByUser;
  866.                 return false;
  867.             }
  868.             if( !MF_rf_pm_cnf->ok || !MF_rf_pm_cnf->extra_info.valid )
  869.             {
  870.                 CalErrorHandler( WM_MF_RF_IP2_FAIL );
  871.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  872.                           " Fail: Target < ok = " + IntToStr(MF_rf_pm_cnf->ok) +
  873.                           ", power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  874.                           ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  875.                           ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  876.                           ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  877.                           ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  878.                           ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  879.                           ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  880.                         );
  881.                 return false;
  882.             }
  883.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  884.                       " Target > power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  885.                       ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  886.                       ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  887.                       ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  888.                       ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  889.                       ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  890.                       ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  891.                    );
  892.             EvalMDC1_I_mt6139[i] = MF_rf_pm_cnf->extra_info.iOffset;
  893.             EvalMDC1_Q_mt6139[i] = MF_rf_pm_cnf->extra_info.qOffset;
  894.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  895.                       " Target > " +
  896.                       ", MDC1_I = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  897.                       ", MDC1_Q = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset)
  898.                    );
  899.             //================================================================
  900.             // 4.0 SS power on
  901.             if( ! m_rct_ctrl.RCT_cellPower( m_pRct, d_IP2_CAL_P_DL_ON_MT ) )
  902.             {
  903.                 CalErrorHandler( WM_MF_AGE8960_SET_POWER_FAIL );
  904.                 if( frmFatcory->rbAgilent8960->Checked )
  905.                 {
  906.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: Agilent 8960< Power = " +
  907.                           Double_To_AnsiString( d_IP2_CAL_P_DL_ON_MT) ) + " dBm";
  908.                 }
  909.                 else
  910.                 {
  911.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: cmu200< Power = " +
  912.                           Double_To_AnsiString( d_IP2_CAL_P_DL_ON_MT) ) + " dBm";
  913.                 }
  914.                 return false;
  915.             }
  916.             if ( this->Terminated )
  917.             {
  918.                 this->OnTerminate = neByUser;
  919.                 return false;
  920.             }
  921.             if( frmFatcory->rbAgilent8960->Checked )
  922.             {
  923.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< Power = " +
  924.                       Double_To_AnsiString( d_IP2_CAL_P_DL_ON_MT) ) + " dBm";
  925.             }
  926.             else
  927.             {
  928.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< Power = " +
  929.                               Double_To_AnsiString( d_IP2_CAL_P_DL_ON_MT) ) + " dBm";
  930.             }
  931.            // if( frmFatcory->cbAFC->Checked || frmFatcory->cbRxPathLoss->Checked )
  932.            // {
  933.            //     Sleep( 100 );  // wait Agilent 8960 ready!
  934.            // }
  935.            // else
  936.             {
  937.                 Sleep( 200 );  // wait Agilent 8960 ready!
  938.             }
  939.             //--------------------------------------------------------
  940.             // 4.1 get MDC2 from DSP
  941.             MF_rf_pm_cnf->deviation = 0;
  942.            MF_rf_pm_cnf->power = 0;
  943.            MF_rf_pm_cnf->usedGain = 0;
  944.            MF_rf_pm_cnf->extra_info.valid =0;
  945.            MF_rf_pm_cnf->extra_info.iOffset=0;
  946.    MF_rf_pm_cnf->extra_info.qOffset=0;
  947.    MF_rf_pm_cnf->extra_info.validSamples =0;
  948.            MF_rf_pm_req->arfcn = s_ARFCN-30;
  949.            MF_rf_pm_req->sampleNoPerFrame = c_IP2_M_PM;
  950.            MF_rf_pm_req->gain = d_IP2_GAIN *AVG_POWER_PC_SIDE_MULTIPLY ;
  951.            MF_rf_pm_req->frames = i_IP2_N_PM;
  952.            if( META_Rf_PM_r( m_pCal->i_MainMETAHandle, MF_rf_pm_req, MF_rf_pm_cnf_cb, &MF_rf_pm_token, NULL ) != META_SUCCESS )
  953.            {
  954.                 CalErrorHandler( WM_MF_RF_IP2_PM_FAIL );
  955.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  956.                                " FAIL: "+
  957.                               " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  958.                               " sample per frame = " + IntToStr( c_IP2_M_PM) +
  959.                               " gain = " + IntToStr( MF_rf_pm_req->gain ) +
  960.                               " frames = " + IntToStr( i_IP2_N_PM)
  961.                             );
  962.                         return false;
  963.             } // if
  964.             if ( this->Terminated )
  965.             {
  966.                 this->OnTerminate = neByUser;
  967.                 return false;
  968.             }
  969.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  970.                       " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  971.                       ", sample per frame = " + IntToStr( c_IP2_M_PM) +
  972.                       ", gain = " + IntToStr( MF_rf_pm_req->gain ) +
  973.                       ", frames = " + IntToStr( i_IP2_N_PM)
  974.                     );
  975.            //  Sleep(100);// delay for power measurement
  976.             RestartTimerCal( WM_MF_RF_IP2_PM_FAIL );
  977.             is_suspend_cal = true;
  978.             this->Suspend();
  979.             is_suspend_cal = false;
  980.             if ( this->Terminated )
  981.             {
  982.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  983.                       " Terminate at line " + AnsiString(__FILE__) + " " + AnsiString(__LINE__)
  984.                         );
  985.                 this->OnTerminate = neByUser;
  986.                 return false;
  987.             }
  988.             if( !MF_rf_pm_cnf->ok || !MF_rf_pm_cnf->extra_info.valid )
  989.             {
  990.                 CalErrorHandler( WM_MF_RF_IP2_FAIL );
  991.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  992.                           " Target < ok = " + IntToStr(MF_rf_pm_cnf->ok) +
  993.                           ", power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  994.                           ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  995.                           ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  996.                           ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  997.                           ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  998.                           ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  999.                           ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  1000.                         );
  1001.                 return false;
  1002.             }
  1003.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1004.                       " Target > power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  1005.                       ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  1006.                       ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  1007.                       ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  1008.                       ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  1009.                       ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  1010.                       ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  1011.                    );
  1012.             EvalMDC2_I_mt6139[i] = MF_rf_pm_cnf->extra_info.iOffset;
  1013.             EvalMDC2_Q_mt6139[i] = MF_rf_pm_cnf->extra_info.qOffset;
  1014.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1015.                       " Target > " +
  1016.                       ", MDC2_I = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  1017.                       ", MDC2_Q = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset)
  1018.                    );
  1019.             //---------------------------------------------------------------
  1020.             // 4.2 set AUTO_CAL = 1 (CW7)
  1021.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_GetMT6139CW7FromReg;
  1022.             MF_rf_pm_ptr->Get_MT6139CW7FromReg_Start( m_mt6139_cw7, m_pCal->ui_rf_id );
  1023.             CHECK_TERMINATE_BY_USER
  1024.             RestartTimerCal( WM_MF_RF_IP2_SET_CW2_FAIL );
  1025.             SUSPEND_CAL_THREAD
  1026.             if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  1027.             {
  1028.                 CalErrorHandler( WM_MF_RF_IP2_GET_CW7_FAIL );
  1029.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1030.                           " FAIL: Target> get CW7 from register."
  1031.                         );
  1032.                 return false;
  1033.             }
  1034.             m_mt6139_cw7.uc_auto_cal = 1;
  1035.             m_mt6139_cw7.uc_man_cal  = 0;
  1036.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_SetMT6139CW7ToReg;
  1037.             MF_rf_pm_ptr->Set_MT6139CW7ToReg_Start( m_mt6139_cw7, m_pCal->ui_rf_id );
  1038.             CHECK_TERMINATE_BY_USER
  1039.             RestartTimerCal( WM_MF_RF_IP2_SET_CW7_FAIL );
  1040.             SUSPEND_CAL_THREAD
  1041.             if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  1042.             {
  1043.                 CalErrorHandler( WM_MF_RF_IP2_SET_CW7_FAIL );
  1044.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1045.                           " FAIL: Target < set CW7 auto_cal=1 to register."
  1046.                         );
  1047.                 return false;
  1048.             }
  1049.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1050.                           " Target < set CW7 auto_cal=1 to register."
  1051.                         );
  1052.             
  1053.         } // for
  1054.         //==============================   Evaluate   =======================
  1055.         // 5.0 SS power off
  1056.             if( ! m_rct_ctrl.RCT_cellPower( m_pRct, d_IP2_CAL_P_DL_OFF_MT ) )
  1057.             {
  1058.                 CalErrorHandler( WM_MF_AGE8960_SET_POWER_FAIL );
  1059.                 if( frmFatcory->rbAgilent8960->Checked )
  1060.                 {
  1061.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: Agilent 8960< Power = " +
  1062.                           Double_To_AnsiString( d_IP2_CAL_P_DL_OFF_MT) ) + " dBm";
  1063.                 }
  1064.                 else
  1065.                 {
  1066.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: cmu200< Power = " +
  1067.                           Double_To_AnsiString( d_IP2_CAL_P_DL_OFF_MT) ) + " dBm";
  1068.                 }
  1069.                 return false;
  1070.             }
  1071.             if ( this->Terminated )
  1072.             {
  1073.                 this->OnTerminate = neByUser;
  1074.                 return false;
  1075.             }
  1076.             if( frmFatcory->rbAgilent8960->Checked )
  1077.             {
  1078.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< Power = " +
  1079.                       Double_To_AnsiString( d_IP2_CAL_P_DL_OFF_MT) ) + " dBm";
  1080.             }
  1081.             else
  1082.             {
  1083.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< Power = " +
  1084.                               Double_To_AnsiString( d_IP2_CAL_P_DL_OFF_MT) ) + " dBm";
  1085.             }
  1086.         int  c0_I = i_IP2_AMCODEI[0];
  1087.         int  c0_Q = i_IP2_AMCODEQ[0];
  1088.         int  c1_I = i_IP2_AMCODEI[1];
  1089.         int  c1_Q = i_IP2_AMCODEQ[1];
  1090.         int  f0_I = EvalMDC2_I_mt6139[0]-EvalMDC1_I_mt6139[0];
  1091.         int  f1_I = EvalMDC2_I_mt6139[1]-EvalMDC1_I_mt6139[1];
  1092.         int  f0_Q = EvalMDC2_Q_mt6139[0]-EvalMDC1_Q_mt6139[0];
  1093.         int  f1_Q = EvalMDC2_Q_mt6139[1]-EvalMDC1_Q_mt6139[1];
  1094.         unsigned char amcode_I;
  1095.         unsigned char amcode_Q;
  1096.         if( (c1_I - c0_I) == 0 && (c1_Q - c0_Q)==0 )  return false;
  1097.         // display evaluate error
  1098.         if( (c1_I - c0_I) != 0 )
  1099.         {
  1100.             float  slope_I = 1.0*(f1_I - f0_I) / (c1_I - c0_I);
  1101.             int  offset_I = f0_I - slope_I*c0_I;
  1102.             if( slope_I != 0 )
  1103.             {
  1104.                 amcode_I =  - offset_I/slope_I;
  1105.             }
  1106.         }
  1107.         if( (c1_Q - c0_Q) != 0 )
  1108.         {
  1109.             float  slope_Q = 1.0*(f1_Q - f0_Q) / (c1_Q - c0_Q);
  1110.             int  offset_Q = f0_Q - slope_Q*c0_Q;
  1111.             if( slope_Q != 0 )
  1112.             {
  1113.                 amcode_Q =  - offset_Q/slope_Q;
  1114.             }
  1115.         }
  1116.         uc_mt6139_coef.rx.coef[eFreqBand].amcodeI = amcode_I;
  1117.         uc_mt6139_coef.rx.coef[eFreqBand].amcodeQ = amcode_Q;
  1118.         uc_mt6139_coef.rx.rxamcalmode = RXAMCALMODE_MT6139C_READ_ACODE;
  1119.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1120.                       " Evaluate: amcodeI = " + IntToStr(amcode_I) +
  1121.                       ", amcodeQ = " + IntToStr(amcode_Q)
  1122.                    );
  1123.         sprintf(tempbuf, "%8d", amcode_I);
  1124.         m_pCal->as_ResultBuf = m_pCal->as_ResultBuf + tempbuf + ", ";
  1125.         sprintf(tempbuf, "%8d", amcode_Q);
  1126.         m_pCal->as_ResultBuf = m_pCal->as_ResultBuf + tempbuf + ", ";
  1127.         MF_rf_pm_ptr->Set_ucMT6139Coef( uc_mt6139_coef );
  1128.         // write IP2 register to NVRAM
  1129.         MF_rf_pm_ptr->ConfirmCallback = ccb_write_ip2_to_nvram;
  1130.         MF_rf_pm_ptr->REQ_Write_IP2Reg_To_NVRAM_Start(m_pCal->ui_rf_id);
  1131.         CHECK_TERMINATE_BY_USER
  1132.         RestartTimerCal(WM_MF_NVRAM_IP2_WRITE_FAIL);
  1133.         SUSPEND_CAL_THREAD
  1134.         E_METAAPP_RESULT_T state = MF_rf_pm_ptr->Get_ConfirmState();
  1135.         if (state != METAAPP_SUCCESS)
  1136.         {
  1137.             if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  1138.             {
  1139.                 CalErrorHandler(WM_MF_NVRAM_EF_L1_RFSPECIALCOEF_LID_VERNO_FAIL);
  1140.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1141.                          " FAIL : NVRAM_EF_L1_RFSPECIALCOEF_LID version is not support, please update META to latest version. "
  1142.                          );
  1143.             }
  1144.             else
  1145.             {
  1146.                 CalErrorHandler(WM_MF_NVRAM_IP2_WRITE_FAIL);
  1147.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1148.                   " FAIL: Target < write amcodeI, amcode_Q, rxamcalmode=0 to NVRAM fail."
  1149.                 );
  1150.             }
  1151.             return false;
  1152.         }
  1153.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1154.                       " Target < write amcodeI, amcode_Q, rxamcalmode=0 to NVRAM"
  1155.                 );
  1156.         
  1157.         //------------------------------------------------------------
  1158. #if 0
  1159.             // set rxamcalmode=0, gain
  1160.             if( !MF_rf_pm_ptr->Set_ucMT6139Coef( uc_mt6139_coef ) )
  1161.             {
  1162.                 CalErrorHandler( WM_MF_RF_IP2_SET_MT6139_COEF_FAIL );
  1163.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1164.                           " FAIL: IP2 Set MT6139 coefficient fail."
  1165.                         );
  1166.                 return false;
  1167.             }
  1168.             // write AM to NVRAM
  1169.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_write_ip2_to_nvram;
  1170.             MF_rf_pm_ptr->REQ_Write_IP2Reg_To_NVRAM(m_pCal->ui_rf_id);
  1171.             CHECK_TERMINATE_BY_USER
  1172.             RestartTimerCal( WM_MF_NVRAM_IP2_READ_FAIL );
  1173.             SUSPEND_CAL_THREAD
  1174.            // if (MF_rf_pm_ptr->ConfirmState != METAAPP_SUCCESS)
  1175.             if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  1176.             {
  1177.                 CalErrorHandler( WM_MF_NVRAM_IP2_READ_FAIL );
  1178.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1179.                           " FAIL: Target < write IP2 to NVRAM fail."
  1180.                         );
  1181.                 return false;
  1182.             }
  1183.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1184.                       " Target < write IP2 to NVRAM."
  1185.                     );
  1186. #endif
  1187.            // set gain, PM
  1188.            // if Agilent 8960 not read, target will get error power!
  1189.            MF_rf_pm_cnf->deviation = 0;
  1190.            MF_rf_pm_cnf->power = 0;
  1191.            MF_rf_pm_cnf->usedGain = 0;
  1192.            MF_rf_pm_cnf->extra_info.valid =0;
  1193.            MF_rf_pm_cnf->extra_info.iOffset=0;
  1194.    MF_rf_pm_cnf->extra_info.qOffset=0;
  1195.    MF_rf_pm_cnf->extra_info.validSamples =0;
  1196.            MF_rf_pm_req->arfcn = s_ARFCN-30;
  1197.            MF_rf_pm_req->sampleNoPerFrame = c_IP2_M_PM;
  1198.            MF_rf_pm_req->gain = d_IP2_GAIN *AVG_POWER_PC_SIDE_MULTIPLY ;
  1199.            MF_rf_pm_req->frames = i_IP2_N_PM;
  1200.            if( META_Rf_PM_r( m_pCal->i_MainMETAHandle, MF_rf_pm_req, MF_rf_pm_cnf_cb, &MF_rf_pm_token, NULL ) != META_SUCCESS )
  1201.            {
  1202.                 CalErrorHandler( WM_MF_RF_IP2_PM_FAIL );
  1203.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1204.                                " FAIL: "+
  1205.                               " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  1206.                               " sample per frame = " + IntToStr(c_IP2_M_PM) +
  1207.                               " gain = " + IntToStr( MF_rf_pm_req->gain ) +
  1208.                               " frames = " + IntToStr( i_IP2_N_PM)
  1209.                             );
  1210.                         return false;
  1211.             } // if
  1212.             if ( this->Terminated )
  1213.             {
  1214.                 this->OnTerminate = neByUser;
  1215.                 return false;
  1216.             }
  1217.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1218.                       " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  1219.                       ", sample per frame = " + IntToStr( c_IP2_M_PM) +
  1220.                       ", gain = " + IntToStr( MF_rf_pm_req->gain ) +
  1221.                       ", frames = " + IntToStr( i_IP2_N_PM)
  1222.                     );
  1223.            //  Sleep(100);// delay for power measurement
  1224.             RestartTimerCal( WM_MF_RF_IP2_PM_FAIL );
  1225.             is_suspend_cal = true;
  1226.             this->Suspend();
  1227.             is_suspend_cal = false;
  1228.             if ( this->Terminated )
  1229.             {
  1230.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1231.                       " Terminate at line " + AnsiString(__FILE__) + " " + AnsiString(__LINE__)
  1232.                         );
  1233.                 this->OnTerminate = neByUser;
  1234.                 return false;
  1235.             }
  1236.             if( !MF_rf_pm_cnf->ok )
  1237.             {
  1238.                 CalErrorHandler( WM_MF_RF_IP2_FAIL );
  1239.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1240.                           " Target < ok = " + IntToStr(MF_rf_pm_cnf->ok) +
  1241.                           ", power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  1242.                           ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  1243.                           ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  1244.                           ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  1245.                           ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  1246.                           ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  1247.                           ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  1248.                         );
  1249.                 return false;
  1250.             }
  1251.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1252.                       " Target > power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  1253.                       ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  1254.                       ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  1255.                       ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  1256.                       ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  1257.                       ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  1258.                       ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  1259.                    );
  1260.             //--------------------------------------------------------------
  1261.            // read A code
  1262.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_GetAcodeFromReg;
  1263.             MF_rf_pm_ptr->Get_AcodeFromReg_Start( eFreqBand, uc_mt6139_coef, m_pCal->ui_rf_id );
  1264.             CHECK_TERMINATE_BY_USER
  1265.             RestartTimerCal( WM_MF_RF_IP2_GET_ACODE_FAIL );
  1266.             SUSPEND_CAL_THREAD
  1267.             if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  1268.             {
  1269.                 CalErrorHandler( WM_MF_RF_IP2_GET_ACODE_FAIL );
  1270.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1271.                           " FAIL: Target> get acode from register."
  1272.                         );
  1273.                 return false;
  1274.             }
  1275.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1276.                           " Target> get acode from register:" +
  1277.                           " acodeI = " + IntToStr( uc_mt6139_coef.rx.coef[eFreqBand].acodeI ) +
  1278.                           ", acodeQ = " + IntToStr( uc_mt6139_coef.rx.coef[eFreqBand].acodeQ )
  1279.                         );
  1280.             sprintf( tempbuf, "%8d", uc_mt6139_coef.rx.coef[eFreqBand].acodeI );
  1281.             m_pCal->as_ResultBuf = m_pCal->as_ResultBuf + tempbuf + ", ";
  1282.             sprintf( tempbuf, "%8d", uc_mt6139_coef.rx.coef[eFreqBand].acodeQ );
  1283.             m_pCal->as_ResultBuf = m_pCal->as_ResultBuf + tempbuf + ", ";
  1284.             // write IP2 register to NVRAM
  1285.         uc_mt6139_coef.rx.rxamcalmode = RXAMCALMODE_MT6139B_NORMAL;
  1286.         MF_rf_pm_ptr->Set_ucMT6139Coef( uc_mt6139_coef );
  1287.         MF_rf_pm_ptr->ConfirmCallback = ::ccb_write_ip2_to_nvram;
  1288.         MF_rf_pm_ptr->REQ_Write_IP2Reg_To_NVRAM_Start(m_pCal->ui_rf_id);
  1289.         CHECK_TERMINATE_BY_USER
  1290.         RestartTimerCal(WM_MF_NVRAM_IP2_WRITE_FAIL);
  1291.         SUSPEND_CAL_THREAD
  1292.         state = MF_rf_pm_ptr->Get_ConfirmState();
  1293.         if (state != METAAPP_SUCCESS)
  1294.         {
  1295.             if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  1296.             {
  1297.                 CalErrorHandler(WM_MF_NVRAM_EF_L1_RFSPECIALCOEF_LID_VERNO_FAIL);
  1298.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1299.                          " FAIL : NVRAM_EF_L1_RFSPECIALCOEF_LID version is not support, please update META to latest version. "
  1300.                          );
  1301.             }
  1302.             else
  1303.             {
  1304.                 CalErrorHandler(WM_MF_NVRAM_IP2_WRITE_FAIL);
  1305.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1306.                          " FAIL: Target < write acodeI, acodeQ, rxamcalmode=2 to NVRAM fail."
  1307.                         );
  1308.             }
  1309.             return false;
  1310.         }
  1311.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1312.                       " Target < write acodeI, acodeQ, rxamcalmode=2 to NVRAM."
  1313.                 );
  1314.         if (!WriteIP2CalResultToFile(as_ID+".cal",Application->ExeName, m_pCal->b_CalResultPath, m_pCal->ui_rf_id, eFreqBand ) )
  1315.         {
  1316.             CalErrorHandler( WM_MF_RF_AFC_WRITE_CAL_RESULT_FAIL );
  1317.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1318.                 " FAIL : write IP2 parameter to cal file "
  1319.               );
  1320.             return false;
  1321.         }
  1322. #if 0
  1323.         //------------------------------------------------------------
  1324.             // set rxamcalmode=0, gain
  1325.            // uc_mt6139_coef.rx.rxamcalmode = RXAMCALMODE_READ_ACODE;
  1326.             MF_rf_pm_ptr->ConfirmCallback = ::ccb_SetMT6139CoefToReg;
  1327.             MF_rf_pm_ptr->Set_MT6139CoefToReg_Start(uc_mt6139_coef);
  1328.             CHECK_TERMINATE_BY_USER
  1329.             RestartTimerCal( WM_MF_RF_IP2_SET_AMCALMODE_FAIL );
  1330.             SUSPEND_CAL_THREAD
  1331.             if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  1332.             {
  1333.                 CalErrorHandler( WM_MF_RF_IP2_SET_AMCALMODE_FAIL );
  1334.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1335.                           " FAIL: Target < set rxamcalmode=2 to register."
  1336.                         );
  1337.                 return false;
  1338.             }
  1339.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1340.                           " Target < set rxamcalmode=2 to register."
  1341.                         );
  1342. #endif
  1343.         eFreqBand++;
  1344.     } // while
  1345.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1346.              " =================== IP2 calibration end ==================== n "
  1347.            );
  1348.     frmFatcory->DisableAllCalTimer();
  1349.     return true;
  1350. }
  1351. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1352. ///////////////////////     MT6139C IP2 calibration     ///////////////////////
  1353. //---------------------------------------------------------------------------
  1354. bool __fastcall T_META_factory_calibration::IP2Cal_mt6139c_cal( void )
  1355. {
  1356.     char tempbuf[256];
  1357.     PostMessage(
  1358.                      ctrl.hPostMsgDestHandle,
  1359.                      WM_MF_RF_IP2_BEGIN,
  1360.                      0,
  1361.                      0
  1362.                     );
  1363.      log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  1364.              " ================== IP2 calibration begin ==================="
  1365.            );
  1366. #if 0
  1367.     // read IP2 register from NVRAM
  1368.     MF_rf_pm_ptr->ConfirmCallback = ::ccb_read_ip2_from_nvram;
  1369.     MF_rf_pm_ptr->REQ_Read_IP2Reg_From_NVRAM(m_pCal->ui_rf_id);
  1370.     CHECK_TERMINATE_BY_USER
  1371.     RestartTimerCal( WM_MF_NVRAM_IP2_READ_FAIL );
  1372.     SUSPEND_CAL_THREAD
  1373.    // if (MF_rf_pm_ptr->ConfirmState != METAAPP_SUCCESS)
  1374.     if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  1375.     {
  1376.         CalErrorHandler( WM_MF_NVRAM_IP2_READ_FAIL );
  1377.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1378.                       " FAIL: Target > read IP2 from NVRAM fail."
  1379.                     );
  1380.             return false;
  1381.     }
  1382.     else
  1383.     {
  1384.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1385.                       " Target > read IP2 from NVRAM."
  1386.                     );
  1387.     }
  1388.     MF_rf_pm_ptr->Get_ucMT6139Coef( uc_mt6139_coef );
  1389. #endif
  1390.     if( ! m_rct_ctrl.RCT_operatingMode( m_pRct, OPERATING_MODE_GSM_BCH ) )
  1391.     {
  1392.         CalErrorHandler( WM_MF_AGE8960_SET_OPERATION_MODE_FAIL );
  1393.         if ( frmFatcory->rbAgilent8960->Checked )
  1394.         {
  1395.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1396.                               " FAIL :"+
  1397.                               " Agilent 8960< Operation mode = GSM BCH"
  1398.                             );
  1399.         }
  1400.         else
  1401.         {
  1402.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1403.                               " FAIL :"+
  1404.                               " cmu200 < Band = Operation mode = GSM BCH"
  1405.                             );
  1406.         }
  1407.         return false;
  1408.     }
  1409.     short s_ARFCN;
  1410.     short s_TCH_ARFCN;
  1411.     FrequencyBand eFreqBand = FrequencyBand850;
  1412.     int vi_Band;
  1413.     while ( eFreqBand < FrequencyBandCount )
  1414.     {
  1415.         switch (eFreqBand)
  1416.         {
  1417.             case FrequencyBand850:
  1418.             {
  1419.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  1420.                 {
  1421.                    eFreqBand++;
  1422.                    continue;
  1423.                 }