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

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_IP2_Skyworks74045.cpp
  40.  *
  41.  * Project:
  42.  * --------
  43.  *   Maui META APP
  44.  *
  45.  * Description:
  46.  * ------------
  47.  * Skyworks 74045  IP2 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. // form
  75. #ifndef _META_FACTORY_H_
  76. #include "META_Factory.h"
  77. #endif
  78. // call back
  79. #ifndef _META_FACTORY_NVRAM_CB_H_
  80. #include "meta_factory_nvram_cb.h"
  81. #endif
  82. #ifndef _META_FACTORY_RF_CB_H_
  83. #include "meta_factory_rf_cb.h"
  84. #endif
  85. // equipment
  86. #ifndef _AGECOMMON_H_
  87. #include "agecommon.h"
  88. #endif
  89. // misc
  90. #ifndef  _FT_UTILS_H_
  91. #include "ft_utils.h"
  92. #endif
  93. #ifndef  _BAND_UTILS_H_
  94. #include "band_utils.h"
  95. #endif
  96. #ifndef  _GSM_UTILS_H_
  97. #include "gsm_utils.h"
  98. #endif
  99. #ifndef  _TIME_UTILS_H_
  100. #include "time_utils.h"
  101. #endif
  102. static const  SKY74045_RCX_MIN =  -127;
  103. static const  SKY74045_RCX_MAX =   127;
  104. //----------------------------------------------------------------------------
  105. // IP2
  106. extern CRFPM*  MF_rf_pm_ptr;
  107. extern RfPm_Cnf  *MF_rf_pm_cnf;   // define in T_META_factory_calibration.cpp
  108. extern RfPm_Req  *MF_rf_pm_req;
  109. // Tthread
  110. extern T_META_factory_calibration* pt_calibration;
  111. extern bool  is_suspend_cal; // in T_META_factory_calibration.cpp
  112. //----------------------------------------------------------------------------
  113. bool __fastcall T_META_factory_calibration::IP2Cal_sky74045_cal( void )
  114. {
  115.     char tempbuf[256];
  116.     PostMessage(
  117.                      ctrl.hPostMsgDestHandle,
  118.                      WM_MF_RF_IP2_BEGIN,
  119.                      0,
  120.                      0
  121.                     );
  122.      log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  123.              " ================== IP2 calibration begin ==================="
  124.            );
  125.     
  126.     // read IP2 register from NVRAM
  127.     MF_rf_pm_ptr->ConfirmCallback = ccb_read_ip2_from_nvram;
  128.     MF_rf_pm_ptr->REQ_Read_IP2Reg_From_NVRAM_Start(m_pCal->ui_rf_id);
  129.     CHECK_TERMINATE_BY_USER
  130.     RestartTimerCal( WM_MF_NVRAM_IP2_READ_FAIL );
  131.     SUSPEND_CAL_THREAD
  132.     if (MF_rf_pm_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  133.     {
  134.         CalErrorHandler( WM_MF_NVRAM_IP2_READ_FAIL );
  135.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  136.                       " FAIL: Target > read IP2 from NVRAM fail."
  137.                     );
  138.             return false;
  139.     }
  140.     else
  141.     {
  142.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  143.                       " Target > read IP2 from NVRAM."
  144.                     );
  145.     }
  146.     MF_rf_pm_ptr->Get_cSky74045Coef( mc_sky74045_coef );
  147.     if( ! m_rct_ctrl.RCT_operatingMode( m_pRct, OPERATING_MODE_GSM_BCH ) )
  148.     {
  149.         CalErrorHandler( WM_MF_AGE8960_SET_OPERATION_MODE_FAIL );
  150.         if ( frmFatcory->rbAgilent8960->Checked )
  151.         {
  152.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  153.                               " FAIL :"+
  154.                               " Agilent 8960< Operation mode = GSM BCH"
  155.                             );
  156.         }
  157.         else
  158.         {
  159.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  160.                               " FAIL :"+
  161.                               " cmu200 < Band = Operation mode = GSM BCH"
  162.                             );
  163.         }
  164.         return false;
  165.     }
  166.     
  167.     int vi_Band;
  168.     short s_ARFCN;
  169.     //int i_TCH_ARFCN;
  170.     FrequencyBand eFreqBand = FrequencyBand850;
  171.     while (eFreqBand < FrequencyBandCount)
  172.     {
  173.         switch (eFreqBand)
  174.         {
  175.             case FrequencyBand850:
  176.             {
  177.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  178.                 {
  179.                    eFreqBand++;
  180.                    continue;
  181.                 }
  182.                 vi_Band = age1960_GSM850_BAND;
  183.                 s_ARFCN = m_sIP2_arfcn_C0_GSM850;
  184.             }
  185.             break;
  186.             case FrequencyBand900:
  187.             {
  188.                 if (! IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  189.                 {
  190.                     eFreqBand++;
  191.                     continue;
  192.                 }
  193.                 vi_Band = age1960_EGSM_BAND;
  194.                 s_ARFCN = m_sIP2_arfcn_C0_GSM;
  195.             }
  196.             break;
  197.             case FrequencyBand1800:
  198.             {
  199.                 if (! IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  200.                 {
  201.                     eFreqBand++;
  202.                     continue;
  203.                 }
  204.                 vi_Band = age1960_DCS1800_BAND;
  205.                 s_ARFCN = m_sIP2_arfcn_C0_DCS;
  206.             }
  207.             break;
  208.             case FrequencyBand1900:
  209.             {
  210.                 if (! IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  211.                 {
  212.                     eFreqBand++;
  213.                     continue;
  214.                 }
  215.                 vi_Band = age1960_PCS1900_BAND;
  216.                 s_ARFCN = m_sIP2_arfcn_C0_PCS;
  217.             }
  218.             break;
  219.             default:
  220.                 assert(false);
  221.             break;
  222.         } // switch
  223.         if( ! m_rct_ctrl.RCT_cellBand( m_pRct, vi_Band ) )
  224.         {
  225.             CalErrorHandler( WM_MF_AGE8960_SET_BAND_FAIL );
  226.             if ( frmFatcory->rbAgilent8960->Checked )
  227.             {
  228.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  229.                               " FAIL :"+
  230.                               " Agilent 8960< Band = " + ViBand_To_Str( vi_Band )
  231.                             );
  232.             }
  233.             else
  234.             {
  235.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  236.                               " FAIL :"+
  237.                               " cmu200 < Band = " + ViBand_To_Str( vi_Band )
  238.                             );
  239.             }
  240.             return false;
  241.         }
  242.         if( ! m_rct_ctrl.RCT_ContModTran( m_pRct ) )
  243.         {
  244.             CalErrorHandler( WM_MF_AGE8960_SET_BAND_FAIL );
  245.             if ( frmFatcory->rbAgilent8960->Checked )
  246.             {
  247.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  248.                               " FAIL :"+
  249.                               " Agilent 8960< set continous mode transmition " 
  250.                             );
  251.             }
  252.             else
  253.             {
  254.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  255.                               " FAIL : "+
  256.                               " cmu200 < set continous mode transmition " 
  257.                             );
  258.             }
  259.             return false;
  260.         }
  261.         if ( this->Terminated )
  262.         {
  263.             this->OnTerminate = neByUser;
  264.             return false;
  265.         }
  266.         if ( frmFatcory->rbAgilent8960->Checked  )
  267.         {
  268.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< band = " +
  269.                       FreqBand_To_Str( eFreqBand) );
  270.         }
  271.         else
  272.         {
  273.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< band = " +
  274.                       FreqBand_To_Str( eFreqBand) );
  275.         }
  276.         if (IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  277.         {
  278.             if(eFreqBand == FrequencyBand1900)
  279.             {
  280.                 if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle, 1, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  281.                 {
  282.                     CalErrorHandler( WM_MF_RF_PM_SELECT_BAND_FAIL );
  283.                     pt_calibration->log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  284.                                               " FAIL:  Target < set Band1900 flag = 1 "
  285.                                              );
  286.                     return false;
  287.                 }
  288.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  289.                            " Target < set Band1900 flag = 1"
  290.                          );
  291.             }
  292.             else
  293.             {
  294.                 if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle, 0, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  295.                 {
  296.                     CalErrorHandler( WM_MF_RF_PM_SELECT_BAND_FAIL );
  297.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  298.                                            " FAIL:  Target < set Band1900 flag = 0 "
  299.                                           );
  300.                     return false;
  301.                 }
  302.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  303.                             "  Target < set Band1900 flag = 0"
  304.                          );
  305.             }
  306.             is_suspend_cal = true;
  307.             this->Suspend();
  308.             is_suspend_cal = false;
  309.         }
  310.         if (!m_rct_ctrl.RCT_BCHARFCN(m_pRct, s_ARFCN))
  311.         {
  312.             CalErrorHandler(WM_MF_AGE8960_SET_BCH_ARFCN_FAIL);
  313.             if ( frmFatcory->rbAgilent8960->Checked )
  314.             {
  315.                  log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: Agilent 8960< BCH ARFCN = " +
  316.                            IntToStr(s_ARFCN)
  317.                          );
  318.             }
  319.             else
  320.             {
  321.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: cmu200 < BCH ARFCN = " +
  322.                            IntToStr(s_ARFCN)
  323.                          );
  324.             }
  325.             return false;
  326.         }
  327.         if ( this->Terminated )
  328.         {
  329.             this->OnTerminate = neByUser;
  330.             return false;
  331.         }
  332.         if ( frmFatcory->rbAgilent8960->Checked )
  333.         {
  334.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< BCH ARFCN = " +
  335.                       AnsiString(s_ARFCN)
  336.                      );
  337.         }
  338.         else
  339.         {
  340.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< BCH ARFCN = " +
  341.                       AnsiString(s_ARFCN)
  342.                     );
  343.         }
  344.         i_IP2_ICORRECTION[0]=i_IP2_ICORRECTION1_SKY74045;
  345.         i_IP2_ICORRECTION[1]=i_IP2_ICORRECTION2_SKY74045;
  346.         i_IP2_QCORRECTION[0]=i_IP2_QCORRECTION1_SKY74045;
  347.         i_IP2_QCORRECTION[1]=i_IP2_QCORRECTION2_SKY74045;
  348.         for( int i=0; i<2; i++)
  349.         {
  350.             if( ! m_rct_ctrl.RCT_cellPower( m_pRct, d_IP2_CAL_P_DL_OFF_SKY ) )
  351.         {
  352.             CalErrorHandler( WM_MF_AGE8960_SET_POWER_FAIL );
  353.             if( frmFatcory->rbAgilent8960->Checked )
  354.             {
  355.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: Agilent 8960< Power = " +
  356.                           Double_To_AnsiString( d_IP2_CAL_P_DL_OFF_SKY ) ) + " dBm";
  357.             }
  358.             else
  359.             {
  360.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: cmu200< Power = " +
  361.                           Double_To_AnsiString( d_IP2_CAL_P_DL_OFF_SKY ) ) + " dBm";
  362.             }
  363.             return false;
  364.         }
  365.         if ( this->Terminated )
  366.         {
  367.             this->OnTerminate = neByUser;
  368.             return false;
  369.         }
  370.         if( frmFatcory->rbAgilent8960->Checked )
  371.         {
  372.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< Power = " +
  373.                       Double_To_AnsiString( d_IP2_CAL_P_DL_OFF_SKY ) );
  374.         }
  375.         else
  376.         {
  377.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< Power = " +
  378.                               Double_To_AnsiString( d_IP2_CAL_P_DL_OFF_SKY ) );
  379.         }
  380.         {
  381.             Sleep( 200 );  // wait Agilent 8960 ready!
  382.         }
  383.             mc_sky74045_coef.rx.coef[eFreqBand].icorrection = i_IP2_ICORRECTION[i];
  384.             mc_sky74045_coef.rx.coef[eFreqBand].qcorrection = i_IP2_QCORRECTION[i];
  385.             EvalPmrcx_I_sky[i] = i_IP2_ICORRECTION[i];
  386.             EvalPmrcx_Q_sky[i] = i_IP2_QCORRECTION[i];
  387.             MF_rf_pm_ptr->Set_cSky74045Coef( mc_sky74045_coef );
  388.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  389.                           " Target < Set icorrection = " + IntToStr( EvalPmrcx_I_sky[i] ) +
  390.                           ", Qcorrection = " + IntToStr( EvalPmrcx_Q_sky[i] )
  391.                         );
  392.             // write IP2 register to NVRAM
  393.             MF_rf_pm_ptr->ConfirmCallback = ccb_write_ip2_to_nvram;
  394.             MF_rf_pm_ptr->REQ_Write_IP2Reg_To_NVRAM_Start(m_pCal->ui_rf_id);
  395.             CHECK_TERMINATE_BY_USER
  396.             RestartTimerCal(WM_MF_NVRAM_IP2_WRITE_FAIL);
  397.             SUSPEND_CAL_THREAD
  398.             E_METAAPP_RESULT_T state = MF_rf_pm_ptr->Get_ConfirmState();
  399.             if (state != METAAPP_SUCCESS)
  400.             {
  401.                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  402.                 {
  403.                     CalErrorHandler(WM_MF_NVRAM_EF_L1_RFSPECIALCOEF_LID_VERNO_FAIL);
  404.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  405.                          " FAIL : NVRAM_EF_L1_RFSPECIALCOEF_LID version is not support, please update META to latest version. "
  406.                          );
  407.                 }
  408.                 else
  409.                 {
  410.                     CalErrorHandler(WM_MF_NVRAM_IP2_WRITE_FAIL);
  411.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  412.                           " FAIL: Target < write IP2 to NVRAM fail."
  413.                         );
  414.                 }
  415.                 return false;
  416.             }
  417.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  418.                       " Target < write IP2 to NVRAM."
  419.                     );
  420.            // if Agilent 8960 not read, target will get error power!
  421.            MF_rf_pm_cnf->deviation = 0;
  422.            MF_rf_pm_cnf->power = 0;
  423.            MF_rf_pm_cnf->usedGain = 0;
  424.            MF_rf_pm_cnf->extra_info.valid =0;
  425.            MF_rf_pm_cnf->extra_info.iOffset=0;
  426.    MF_rf_pm_cnf->extra_info.qOffset=0;
  427.    MF_rf_pm_cnf->extra_info.validSamples =0;
  428.            MF_rf_pm_req->arfcn = s_ARFCN-30;
  429.            MF_rf_pm_req->sampleNoPerFrame = c_IP2_M_PM;
  430.            MF_rf_pm_req->gain = d_IP2_GAIN *AVG_POWER_PC_SIDE_MULTIPLY ;
  431.            MF_rf_pm_req->frames = i_IP2_N_PM;
  432.            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 )
  433.            {
  434.                 CalErrorHandler( WM_MF_RF_IP2_PM_FAIL );
  435.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  436.                                " FAIL: "+
  437.                               " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  438.                               " sample per frame = " + IntToStr( c_IP2_M_PM) +
  439.                               " gain = " + IntToStr( MF_rf_pm_req->gain ) +
  440.                               " frames = " + IntToStr( i_IP2_N_PM)
  441.                             );
  442.                         return false;
  443.             } // if
  444.             if ( this->Terminated )
  445.             {
  446.                 this->OnTerminate = neByUser;
  447.                 return false;
  448.             }
  449.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  450.                       " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  451.                       ", sample per frame = " + IntToStr( c_IP2_M_PM) +
  452.                       ", gain = " + IntToStr( MF_rf_pm_req->gain ) +
  453.                       ", frames = " + IntToStr( i_IP2_N_PM)
  454.                     );
  455.            //  Sleep(100);// delay for power measurement
  456.             RestartTimerCal( WM_MF_RF_IP2_PM_FAIL );
  457.             is_suspend_cal = true;
  458.             this->Suspend();
  459.             is_suspend_cal = false;
  460.             if ( this->Terminated )
  461.             {
  462.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  463.                       " Terminate at line " + AnsiString(__FILE__) + " " + AnsiString(__LINE__)
  464.                         );
  465.                 this->OnTerminate = neByUser;
  466.                 return false;
  467.             }
  468.             if( !MF_rf_pm_cnf->ok )
  469.             {
  470.                 CalErrorHandler( WM_MF_RF_IP2_FAIL );
  471.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  472.                           " Target < ok = " + IntToStr(MF_rf_pm_cnf->ok) +
  473.                           ", power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  474.                           ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  475.                           ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  476.                           ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  477.                           ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  478.                           ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  479.                           ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  480.                         );
  481.                 return false;
  482.             }
  483.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  484.                       " Target > power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  485.                       ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  486.                       ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  487.                       ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  488.                       ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  489.                       ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  490.                       ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  491.                    );
  492.             EvalMDC1_I_sky[i] = MF_rf_pm_cnf->extra_info.iOffset;
  493.             EvalMDC1_Q_sky[i] = MF_rf_pm_cnf->extra_info.qOffset;
  494.             //===========================================================
  495.             // 1.0 SS power on
  496.             if( ! m_rct_ctrl.RCT_cellPower( m_pRct, d_IP2_CAL_P_DL_ON_SKY ) )
  497.             {
  498.                 CalErrorHandler( WM_MF_AGE8960_SET_POWER_FAIL );
  499.                 if( frmFatcory->rbAgilent8960->Checked )
  500.                 {
  501.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: Agilent 8960< Power = " +
  502.                           Double_To_AnsiString( d_IP2_CAL_P_DL_ON_SKY ) ) + " dBm";
  503.                 }
  504.                 else
  505.                 {
  506.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: cmu200< Power = " +
  507.                           Double_To_AnsiString( d_IP2_CAL_P_DL_ON_SKY ) ) + " dBm";
  508.                 }
  509.                 return false;
  510.             }
  511.             if ( this->Terminated )
  512.             {
  513.                 this->OnTerminate = neByUser;
  514.                 return false;
  515.             }
  516.             if( frmFatcory->rbAgilent8960->Checked )
  517.             {
  518.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< Power = " +
  519.                       Double_To_AnsiString( d_IP2_CAL_P_DL_ON_SKY ) );
  520.             }
  521.             else
  522.             {
  523.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< Power = " +
  524.                               Double_To_AnsiString( d_IP2_CAL_P_DL_ON_SKY ) );
  525.             }
  526.             Sleep( 200 );  // wait Agilent 8960 ready!
  527.             mc_sky74045_coef.rx.coef[eFreqBand].icorrection = i_IP2_ICORRECTION[i];
  528.             mc_sky74045_coef.rx.coef[eFreqBand].qcorrection = i_IP2_QCORRECTION[i];
  529.             EvalPmrcx_I_sky[i] = i_IP2_ICORRECTION[i];
  530.             EvalPmrcx_Q_sky[i] = i_IP2_QCORRECTION[i];
  531.             MF_rf_pm_ptr->Set_cSky74045Coef( mc_sky74045_coef );
  532.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  533.                           " Target < Set icorrection = " + IntToStr( EvalPmrcx_I_sky[i] ) +
  534.                           ", Qcorrection = " + IntToStr( EvalPmrcx_Q_sky[i] )
  535.                         );
  536.             // write IP2 register to NVRAM
  537.             MF_rf_pm_ptr->ConfirmCallback = ccb_write_ip2_to_nvram;
  538.             MF_rf_pm_ptr->REQ_Write_IP2Reg_To_NVRAM_Start(m_pCal->ui_rf_id);
  539.             CHECK_TERMINATE_BY_USER
  540.             RestartTimerCal(WM_MF_NVRAM_IP2_WRITE_FAIL);
  541.             SUSPEND_CAL_THREAD
  542.             state = MF_rf_pm_ptr->Get_ConfirmState();
  543.             if (state != METAAPP_SUCCESS)
  544.             {
  545.                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  546.                 {
  547.                     CalErrorHandler(WM_MF_NVRAM_EF_L1_RFSPECIALCOEF_LID_VERNO_FAIL);
  548.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  549.                          " FAIL : NVRAM_EF_L1_RFSPECIALCOEF_LID version is not support, please update META to latest version. "
  550.                          );
  551.                 }
  552.                 else
  553.                 {
  554.                     CalErrorHandler(WM_MF_NVRAM_IP2_WRITE_FAIL);
  555.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  556.                           " FAIL: Target < write IP2 to NVRAM fail."
  557.                         );
  558.                 }
  559.                 return false;
  560.             }
  561.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  562.                       " Target < write IP2 to NVRAM."
  563.                     );
  564.            // if Agilent 8960 not read, target will get error power!
  565.            MF_rf_pm_cnf->deviation = 0;
  566.            MF_rf_pm_cnf->power = 0;
  567.            MF_rf_pm_cnf->usedGain = 0;
  568.            MF_rf_pm_cnf->extra_info.valid =0;
  569.            MF_rf_pm_cnf->extra_info.iOffset=0;
  570.    MF_rf_pm_cnf->extra_info.qOffset=0;
  571.    MF_rf_pm_cnf->extra_info.validSamples =0;
  572.            MF_rf_pm_req->arfcn = s_ARFCN-30;
  573.            MF_rf_pm_req->sampleNoPerFrame = c_IP2_M_PM;
  574.            MF_rf_pm_req->gain = d_IP2_GAIN *AVG_POWER_PC_SIDE_MULTIPLY ;
  575.            MF_rf_pm_req->frames = i_IP2_N_PM;
  576.            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 )
  577.            {
  578.                 CalErrorHandler( WM_MF_RF_IP2_PM_FAIL );
  579.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  580.                                " FAIL: "+
  581.                               " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  582.                               " sample per frame = " + IntToStr( c_IP2_M_PM) +
  583.                               " gain = " + IntToStr( MF_rf_pm_req->gain ) +
  584.                               " frames = " + IntToStr( i_IP2_N_PM)
  585.                             );
  586.                         return false;
  587.             } // if
  588.             if ( this->Terminated )
  589.             {
  590.                 this->OnTerminate = neByUser;
  591.                 return false;
  592.             }
  593.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  594.                       " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  595.                       ", sample per frame = " + IntToStr( c_IP2_M_PM) +
  596.                       ", gain = " + IntToStr( MF_rf_pm_req->gain ) +
  597.                       ", frames = " + IntToStr( i_IP2_N_PM)
  598.                     );
  599.            //  Sleep(100);// delay for power measurement
  600.             RestartTimerCal( WM_MF_RF_IP2_PM_FAIL );
  601.             is_suspend_cal = true;
  602.             this->Suspend();
  603.             is_suspend_cal = false;
  604.             if ( this->Terminated )
  605.             {
  606.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  607.                       " Terminate at line " + AnsiString(__FILE__) + " " + AnsiString(__LINE__)
  608.                         );
  609.                 this->OnTerminate = neByUser;
  610.                 return false;
  611.             }
  612.             if( !MF_rf_pm_cnf->ok )
  613.             {
  614.                 CalErrorHandler( WM_MF_RF_IP2_FAIL );
  615.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  616.                           " Target < ok = " + IntToStr(MF_rf_pm_cnf->ok) +
  617.                           ", power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  618.                           ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  619.                           ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  620.                           ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  621.                           ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  622.                           ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  623.                           ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  624.                         );
  625.                 return false;
  626.             }
  627.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  628.                       " Target > power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  629.                       ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  630.                       ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  631.                       ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  632.                       ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  633.                       ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  634.                       ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  635.                    );
  636.             EvalMDC2_I_sky[i] = MF_rf_pm_cnf->extra_info.iOffset;
  637.             EvalMDC2_Q_sky[i] = MF_rf_pm_cnf->extra_info.qOffset;
  638.         } // for
  639.         // Evaluate
  640.         unsigned char  c0_I = MF_rf_pm_ptr->Get_cxofrealcoef(EvalPmrcx_I_sky[0], m_pCal->ui_rf_id);
  641.         unsigned char  c1_I = MF_rf_pm_ptr->Get_cxofrealcoef(EvalPmrcx_I_sky[1], m_pCal->ui_rf_id);
  642.         unsigned char  c0_Q = MF_rf_pm_ptr->Get_cxofrealcoef(EvalPmrcx_Q_sky[0], m_pCal->ui_rf_id);
  643.         unsigned char  c1_Q = MF_rf_pm_ptr->Get_cxofrealcoef(EvalPmrcx_Q_sky[1], m_pCal->ui_rf_id);
  644.         int  f0_I = EvalMDC2_I_sky[0] - EvalMDC1_I_sky[0];
  645.         int  f1_I = EvalMDC2_I_sky[1] - EvalMDC1_I_sky[1];
  646.         int  f0_Q = EvalMDC2_Q_sky[0] - EvalMDC1_Q_sky[0];
  647.         int  f1_Q = EvalMDC2_Q_sky[1] - EvalMDC1_Q_sky[1];
  648.         signed char rcx_I = 0;
  649.         signed char rcx_Q = 0;
  650.         if( (c1_I - c0_I) == 0 && (c1_Q - c0_Q)==0 )  return false;
  651.         // display evaluate error
  652.         if( (c1_I - c0_I) != 0 )
  653.         {
  654.             float  slope_I = 1.0*(f1_I - f0_I) / (c1_I - c0_I);
  655.             int  offset_I = f0_I - slope_I*c0_I;
  656.             if( slope_I != 0 )
  657.             {
  658.                 unsigned char cx_I =  - offset_I/slope_I;
  659.                 rcx_I = MF_rf_pm_ptr->Get_realcoefofcx( cx_I, m_pCal->ui_rf_id );
  660.                 if( rcx_I < SKY74045_RCX_MIN ) rcx_I = SKY74045_RCX_MIN;
  661.                // else if( rcx_I > SKY74045_RCX_MAX ) rcx_I = SKY74045_RCX_MAX;
  662.             }
  663.         }
  664.         if( (c1_Q - c0_Q) != 0 )
  665.         {
  666.             float  slope_Q = 1.0*(f1_Q - f0_Q) / (c1_Q - c0_Q);
  667.             int  offset_Q = f0_Q - slope_Q*c0_Q;
  668.             if( slope_Q != 0 )
  669.             {
  670.                 unsigned char cx_Q =  - offset_Q/slope_Q;
  671.                 rcx_Q = MF_rf_pm_ptr->Get_realcoefofcx( cx_Q, m_pCal->ui_rf_id );
  672.                 if( rcx_Q < SKY74045_RCX_MIN ) rcx_Q = SKY74045_RCX_MIN;
  673.                 //else if( rcx_Q > SKY74045_RCX_MAX ) rcx_Q = SKY74045_RCX_MAX;
  674.             }
  675.         }
  676.         mc_sky74045_coef.rx.coef[eFreqBand].icorrection = rcx_I;
  677.         mc_sky74045_coef.rx.coef[eFreqBand].qcorrection = rcx_Q;
  678.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  679.                       " rcx_I = " + IntToStr(rcx_I) +
  680.                       " rcx_Q = " + IntToStr(rcx_Q) 
  681.                    );
  682.         sprintf( tempbuf, "%8d", rcx_I );
  683.         m_pCal->as_ResultBuf = m_pCal->as_ResultBuf + tempbuf + ", ";
  684.         sprintf( tempbuf, "%8d", rcx_Q );
  685.         m_pCal->as_ResultBuf = m_pCal->as_ResultBuf + tempbuf + ", ";
  686.         MF_rf_pm_ptr->Set_cSky74045Coef( mc_sky74045_coef );
  687.         if (!WriteIP2CalResultToFile(as_ID+".cal",Application->ExeName, m_pCal->b_CalResultPath, m_pCal->ui_rf_id, eFreqBand ) )
  688.         {
  689.             CalErrorHandler( WM_MF_RF_AFC_WRITE_CAL_RESULT_FAIL );
  690.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  691.                 " FAIL : write IP2 parameter to cal file "
  692.               );
  693.             return false;
  694.         }
  695.         eFreqBand++;
  696.     } // while
  697.    // if( ! frmFatcory->WriteIP2CalResultToFile(as_ID+".cal",Application->ExeName, p_cal->b_CalResultPath, m_pCal->ui_rf_id ) )
  698.    // {
  699.    //     CalErrorHandler( WM_MF_RF_AFC_WRITE_CAL_RESULT_FAIL );
  700.     //    log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  701.     //            " FAIL : write IP2 parameter to cal file "
  702.     //          );
  703.     //    return false;
  704.     //}
  705.     // write IP2 register to NVRAM
  706.     MF_rf_pm_ptr->ConfirmCallback = ccb_write_ip2_to_nvram;
  707.     MF_rf_pm_ptr->REQ_Write_IP2Reg_To_NVRAM_Start(m_pCal->ui_rf_id);
  708.     CHECK_TERMINATE_BY_USER
  709.     RestartTimerCal(WM_MF_NVRAM_IP2_WRITE_FAIL);
  710.     SUSPEND_CAL_THREAD
  711.     E_METAAPP_RESULT_T state = MF_rf_pm_ptr->Get_ConfirmState();
  712.     if (state != METAAPP_SUCCESS)
  713.     {
  714.         if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  715.         {
  716.             CalErrorHandler(WM_MF_NVRAM_EF_L1_RFSPECIALCOEF_LID_VERNO_FAIL);
  717.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  718.                          " FAIL : NVRAM_EF_L1_RFSPECIALCOEF_LID version is not support, please update META to latest version. "
  719.                          );
  720.         }
  721.         else
  722.         {
  723.             CalErrorHandler( WM_MF_NVRAM_IP2_READ_FAIL );
  724.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  725.                   " FAIL: Target < write IP2 to NVRAM fail."
  726.                 );
  727.         }
  728.         return false;
  729.     }
  730.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  731.                       " Target < write IP2 to NVRAM."
  732.                 );
  733.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  734.              " =================== IP2 calibration end ==================== n "
  735.            );
  736.     return true;
  737. }
  738. //===========================================================================
  739. bool __fastcall T_META_factory_calibration::IP2Cal_sky74045_check(void)
  740. {
  741.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  742.              " ===================   IP2 check begin   ==================== n "
  743.            );
  744.     
  745.     int MDCI[2];
  746.     int MDCQ[2];
  747.     int DCI;
  748.     int DCQ;
  749.     int vi_Band;
  750.     short s_ARFCN;
  751.     FrequencyBand eFreqBand = FrequencyBand850;
  752.     while (eFreqBand < FrequencyBandCount)
  753.     {
  754.         switch (eFreqBand)
  755.         {
  756.             case FrequencyBand850:
  757.             {
  758.                 if (! IsFreqBankSupported(m_asFreqBank, eFreqBand))
  759.                 {
  760.                    eFreqBand++;
  761.                    continue;
  762.                 }
  763.                 vi_Band = age1960_GSM850_BAND;
  764.                 s_ARFCN = m_sIP2_arfcn_C0_GSM850;
  765.             }
  766.             break;
  767.             case FrequencyBand900:
  768.             {
  769.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  770.                 {
  771.                     eFreqBand++;
  772.                     continue;
  773.                 }
  774.                 vi_Band = age1960_EGSM_BAND;
  775.                 s_ARFCN = m_sIP2_arfcn_C0_GSM;
  776.             }
  777.             break;
  778.             case FrequencyBand1800:
  779.             {
  780.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  781.                 {
  782.                     eFreqBand++;
  783.                     continue;
  784.                 }
  785.                 vi_Band = age1960_DCS1800_BAND;
  786.                 s_ARFCN = m_sIP2_arfcn_C0_DCS;
  787.             }
  788.             break;
  789.             case FrequencyBand1900:
  790.             {
  791.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  792.                 {
  793.                     eFreqBand++;
  794.                     continue;
  795.                 }
  796.                 vi_Band = age1960_PCS1900_BAND;
  797.                 s_ARFCN = m_sIP2_arfcn_C0_PCS;
  798.             }
  799.             break;
  800.             default:
  801.                 assert(false);
  802.             break;
  803.         } // switch
  804.         if( ! m_rct_ctrl.RCT_cellBand( m_pRct, vi_Band ) )
  805.         {
  806.             CalErrorHandler( WM_MF_AGE8960_SET_BAND_FAIL );
  807.             if ( frmFatcory->rbAgilent8960->Checked )
  808.             {
  809.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  810.                               " FAIL :"+
  811.                               " Agilent 8960< Band = " + ViBand_To_Str( vi_Band )
  812.                             );
  813.             }
  814.             else
  815.             {
  816.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  817.                               " FAIL :"+
  818.                               " cmu200 < Band = " + ViBand_To_Str( vi_Band )
  819.                             );
  820.             }
  821.             return false;
  822.         }
  823.         if( ! m_rct_ctrl.RCT_ContModTran(m_pRct))
  824.         {
  825.             CalErrorHandler( WM_MF_AGE8960_SET_BAND_FAIL );
  826.             if ( frmFatcory->rbAgilent8960->Checked )
  827.             {
  828.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  829.                               " FAIL :"+
  830.                               " Agilent 8960< set continous mode transmition " 
  831.                             );
  832.             }
  833.             else
  834.             {
  835.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  836.                               " FAIL : "+
  837.                               " cmu200 < set continous mode transmition " 
  838.                             );
  839.             }
  840.             return false;
  841.         }
  842.         if ( this->Terminated )
  843.         {
  844.             this->OnTerminate = neByUser;
  845.             return false;
  846.         }
  847.         if ( frmFatcory->rbAgilent8960->Checked  )
  848.         {
  849.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< band = " +
  850.                       FreqBand_To_Str( eFreqBand) );
  851.         }
  852.         else
  853.         {
  854.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< band = " +
  855.                       FreqBand_To_Str( eFreqBand) );
  856.         }
  857.         if (IsFreqBankSupported(m_asFreqBank, eFreqBand))
  858.         {
  859.             if(eFreqBand == FrequencyBand1900)
  860.             {
  861.                 if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle, 1, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  862.                 {
  863.                     CalErrorHandler( WM_MF_RF_PM_SELECT_BAND_FAIL );
  864.                     pt_calibration->log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  865.                                               " FAIL: Target < set Band1900 flag = 1 "
  866.                                              );
  867.                     return false;
  868.                 }
  869.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  870.                            " Target < set Band1900 flag = 1"
  871.                          );
  872.             }
  873.             else
  874.             {
  875.                 if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle, 0, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  876.                 {
  877.                     CalErrorHandler( WM_MF_RF_PM_SELECT_BAND_FAIL );
  878.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  879.                                            " FAIL: Target < set Band1900 flag = 0 "
  880.                                           );
  881.                     return false;
  882.                 }
  883.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  884.                             " Target < set Band1900 flag = 0"
  885.                          );
  886.             }
  887.             is_suspend_cal = true;
  888.             this->Suspend();
  889.             is_suspend_cal = false;
  890.         }
  891.         if( ! m_rct_ctrl.RCT_BCHARFCN( m_pRct, s_ARFCN ) )
  892.         {
  893.             CalErrorHandler( WM_MF_AGE8960_SET_BCH_ARFCN_FAIL );
  894.             if ( frmFatcory->rbAgilent8960->Checked )
  895.             {
  896.                  log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: Agilent 8960< BCH ARFCN = " +
  897.                            IntToStr(s_ARFCN)
  898.                          );
  899.             }
  900.             else
  901.             {
  902.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: cmu200 < BCH ARFCN = " +
  903.                            IntToStr(s_ARFCN)
  904.                          );
  905.             }
  906.             return false;
  907.         }
  908.         if ( this->Terminated )
  909.         {
  910.             this->OnTerminate = neByUser;
  911.             return false;
  912.         }
  913.         if ( frmFatcory->rbAgilent8960->Checked )
  914.         {
  915.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< BCH ARFCN = " +
  916.                       AnsiString(s_ARFCN)
  917.                      );
  918.         }
  919.         else
  920.         {
  921.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< BCH ARFCN = " +
  922.                       AnsiString(s_ARFCN)
  923.                     );
  924.         }
  925.         if( ! m_rct_ctrl.RCT_cellPower( m_pRct, d_IP2_CHECK_P_DL_OFF ) )
  926.         {
  927.             CalErrorHandler( WM_MF_AGE8960_SET_POWER_FAIL );
  928.             if( frmFatcory->rbAgilent8960->Checked )
  929.             {
  930.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: Agilent 8960< Power = " +
  931.                           Double_To_AnsiString( d_IP2_CHECK_P_DL_OFF) ) + " dBm";
  932.             }
  933.             else
  934.             {
  935.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: cmu200< Power = " +
  936.                           Double_To_AnsiString( d_IP2_CHECK_P_DL_OFF) ) + " dBm";
  937.             }
  938.             return false;
  939.         }
  940.         if ( this->Terminated )
  941.         {
  942.             this->OnTerminate = neByUser;
  943.             return false;
  944.         }
  945.         if( frmFatcory->rbAgilent8960->Checked )
  946.         {
  947.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< Power = " +
  948.                       Double_To_AnsiString( d_IP2_CHECK_P_DL_OFF) );
  949.         }
  950.         else
  951.         {
  952.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< Power = " +
  953.                               Double_To_AnsiString( d_IP2_CHECK_P_DL_OFF) );
  954.         }
  955.             Sleep( 200 );  // wait Agilent 8960 ready!
  956.         
  957.       MF_rf_pm_cnf->deviation = 0;
  958.       MF_rf_pm_cnf->power = 0;
  959.       MF_rf_pm_cnf->usedGain = 0;
  960.       MF_rf_pm_cnf->extra_info.valid =0;
  961.       MF_rf_pm_cnf->extra_info.iOffset=0;
  962.       MF_rf_pm_cnf->extra_info.qOffset=0;
  963.       MF_rf_pm_cnf->extra_info.validSamples =0;
  964.       MF_rf_pm_req->arfcn = s_ARFCN-30;
  965.       MF_rf_pm_req->sampleNoPerFrame = c_IP2_M_PM;
  966.       MF_rf_pm_req->gain = d_IP2_GAIN *AVG_POWER_PC_SIDE_MULTIPLY ;
  967.       MF_rf_pm_req->frames = i_IP2_N_PM;
  968.     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 )
  969.     {
  970.         CalErrorHandler( WM_MF_RF_IP2_PM_FAIL );
  971.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  972.                                " FAIL: "+
  973.                               " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  974.                               " sample per frame = " + IntToStr( c_IP2_M_PM) +
  975.                               " gain = " + IntToStr( MF_rf_pm_req->gain ) +
  976.                               " frames = " + IntToStr( i_IP2_N_PM)
  977.                             );
  978.                         return false;
  979.     } // if
  980.     if ( this->Terminated )
  981.     {
  982.        this->OnTerminate = neByUser;
  983.        return false;
  984.     }
  985.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  986.                       " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  987.                       ", sample per frame = " + IntToStr( c_IP2_M_PM) +
  988.                       ", gain = " + IntToStr( MF_rf_pm_req->gain ) +
  989.                       ", frames = " + IntToStr( i_IP2_N_PM)
  990.                     );
  991.            //  Sleep(100);// delay for power measurement
  992.     RestartTimerCal( WM_MF_RF_IP2_PM_FAIL );
  993.     is_suspend_cal = true;
  994.     this->Suspend();
  995.     is_suspend_cal = false;
  996.     if ( this->Terminated )
  997.     {
  998.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  999.                       " Terminate at line " + AnsiString(__FILE__) + " " + AnsiString(__LINE__)
  1000.                 );
  1001.         this->OnTerminate = neByUser;
  1002.         return false;
  1003.     }
  1004.     if( !MF_rf_pm_cnf->ok )
  1005.     {
  1006.         CalErrorHandler( WM_MF_RF_IP2_PM_FAIL );
  1007.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1008.                           " Target < ok = " + IntToStr(MF_rf_pm_cnf->ok) +
  1009.                           ", power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  1010.                           ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  1011.                           ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  1012.                           ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  1013.                           ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  1014.                           ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  1015.                           ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  1016.                         );
  1017.         return false;
  1018.     }
  1019.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1020.                       " Target > power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  1021.                       ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  1022.                       ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  1023.                       ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  1024.                       ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  1025.                       ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  1026.                       ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  1027.                    );
  1028.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1029.                   " I_MDC_OFF = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  1030.                   ", Q_MDC_OFF = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) 
  1031.                 );
  1032.         MDCI[0] = MF_rf_pm_cnf->extra_info.iOffset;
  1033.         MDCQ[0] = MF_rf_pm_cnf->extra_info.qOffset;
  1034.         //----------------------------------------------------------------
  1035.         // cell power on
  1036.         if( ! m_rct_ctrl.RCT_cellPower( m_pRct, d_IP2_CHECK_P_DL_ON ) )
  1037.         {
  1038.             CalErrorHandler( WM_MF_AGE8960_SET_POWER_FAIL );
  1039.             if( frmFatcory->rbAgilent8960->Checked )
  1040.             {
  1041.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: Agilent 8960< Power = " +
  1042.                           Double_To_AnsiString( d_IP2_CHECK_P_DL_ON) ) + " dBm";
  1043.             }
  1044.             else
  1045.             {
  1046.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: cmu200< Power = " +
  1047.                           Double_To_AnsiString( d_IP2_CHECK_P_DL_ON) ) + " dBm";
  1048.             }
  1049.             return false;
  1050.         }
  1051.         if ( this->Terminated )
  1052.         {
  1053.             this->OnTerminate = neByUser;
  1054.             return false;
  1055.         }
  1056.         if( frmFatcory->rbAgilent8960->Checked )
  1057.         {
  1058.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " Agilent 8960< Power = " +
  1059.                       Double_To_AnsiString( d_IP2_CHECK_P_DL_ON) );
  1060.         }
  1061.         else
  1062.         {
  1063.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " CMU 200< Power = " +
  1064.                               Double_To_AnsiString( d_IP2_CHECK_P_DL_ON) );
  1065.         }
  1066.             Sleep( 200 );  // wait Agilent 8960 ready!
  1067.         MF_rf_pm_cnf->deviation = 0;
  1068.         MF_rf_pm_cnf->power = 0;
  1069.         MF_rf_pm_cnf->usedGain = 0;
  1070.         MF_rf_pm_cnf->extra_info.valid =0;
  1071.         MF_rf_pm_cnf->extra_info.iOffset=0;
  1072.         MF_rf_pm_cnf->extra_info.qOffset=0;
  1073.         MF_rf_pm_cnf->extra_info.validSamples =0;
  1074.         MF_rf_pm_req->arfcn = s_ARFCN-30;
  1075.         MF_rf_pm_req->sampleNoPerFrame = c_IP2_M_PM;
  1076.         MF_rf_pm_req->gain = d_IP2_GAIN *AVG_POWER_PC_SIDE_MULTIPLY ;
  1077.         MF_rf_pm_req->frames = i_IP2_N_PM;
  1078.         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 )
  1079.         {
  1080.             CalErrorHandler( WM_MF_RF_IP2_PM_FAIL );
  1081.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1082.                                " FAIL: "+
  1083.                               " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  1084.                               " sample per frame = " + IntToStr( c_IP2_M_PM) +
  1085.                               " gain = " + IntToStr( MF_rf_pm_req->gain ) +
  1086.                               " frames = " + IntToStr( i_IP2_N_PM)
  1087.                             );
  1088.                         return false;
  1089.         } // if
  1090.         if ( this->Terminated )
  1091.         {
  1092.             this->OnTerminate = neByUser;
  1093.             return false;
  1094.         }
  1095.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1096.                       " Target < PM : ARFCN = " + IntToStr( s_ARFCN-30 ) +
  1097.                       ", sample per frame = " + IntToStr( c_IP2_M_PM) +
  1098.                       ", gain = " + IntToStr( MF_rf_pm_req->gain ) +
  1099.                       ", frames = " + IntToStr( i_IP2_N_PM)
  1100.                     );
  1101.            //  Sleep(100);// delay for power measurement
  1102.         RestartTimerCal( WM_MF_RF_IP2_PM_FAIL );
  1103.         is_suspend_cal = true;
  1104.         this->Suspend();
  1105.         is_suspend_cal = false;
  1106.         if ( this->Terminated )
  1107.         {
  1108.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1109.                       " Terminate at line " + AnsiString(__FILE__) + " " + AnsiString(__LINE__)
  1110.                 );
  1111.             this->OnTerminate = neByUser;
  1112.             return false;
  1113.         }
  1114.         if( !MF_rf_pm_cnf->ok )
  1115.         {
  1116.             CalErrorHandler( WM_MF_RF_IP2_PM_FAIL );
  1117.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1118.                           " Target < ok = " + IntToStr(MF_rf_pm_cnf->ok) +
  1119.                           ", power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  1120.                           ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  1121.                           ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  1122.                           ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  1123.                           ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  1124.                           ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  1125.                           ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  1126.                         );
  1127.             return false;
  1128.         }
  1129.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1130.                       " Target > power = " + Double_To_AnsiString(MF_rf_pm_cnf->power/8.0) +
  1131.                       ", deviation = " + Double_To_AnsiString(sqrt(MF_rf_pm_cnf->deviation)/8.0) +
  1132.                       ", usedGain = " + Double_To_AnsiString(MF_rf_pm_cnf->usedGain/8.0) +
  1133.                       ", valid = " + IntToStr(MF_rf_pm_cnf->extra_info.valid) +
  1134.                       ", I_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  1135.                       ", Q_DC = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  1136.                       ", valid sample = " + IntToStr(MF_rf_pm_cnf->extra_info.validSamples)
  1137.                    );
  1138. #if 0
  1139.         int abs_iOffset = abs(MF_rf_pm_cnf->extra_info.iOffset);
  1140.         int abs_qOffset = abs(MF_rf_pm_cnf->extra_info.qOffset);
  1141.         if( abs_iOffset > i_IP2_MAX_IDC || abs_qOffset > i_IP2_MAX_QDC)
  1142.         {
  1143.             CalErrorHandler( WM_MF_RF_IP2_CHECK_FAIL );
  1144.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1145.                   " Fail: IP2 check ." + " I_MDC_ON = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  1146.                   ", Q_MDC_ON = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  1147.                   ", IP2_MAX_IDC = " + IntToStr( i_IP2_MAX_IDC ) +
  1148.                   ", IP2_MAX_QDC = " + IntToStr( i_IP2_MAX_QDC )
  1149.                 );
  1150.             return false;
  1151.         }
  1152.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1153.                   " Pass: IP2 check ." + " I_MDC_ON = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  1154.                   ", Q_MDC_ON = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) +
  1155.                   ", IP2_MAX_IDC = " + IntToStr( i_IP2_MAX_IDC ) +
  1156.                   ", IP2_MAX_QDC = " + IntToStr( i_IP2_MAX_QDC )
  1157.                 );
  1158. #endif
  1159.        log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1160.                   " I_MDC_ON = " + IntToStr(MF_rf_pm_cnf->extra_info.iOffset) +
  1161.                   ", Q_MDC_ON = " + IntToStr(MF_rf_pm_cnf->extra_info.qOffset) 
  1162.                 );
  1163.         MDCI[1] = MF_rf_pm_cnf->extra_info.iOffset;
  1164.         MDCQ[1] = MF_rf_pm_cnf->extra_info.qOffset;
  1165.         DCI = MDCI[1] - MDCI[0];
  1166.         DCQ = MDCQ[1] - MDCQ[0];
  1167.         int ab_DCI = abs(DCI);
  1168.         int ab_QCQ = abs(DCQ);
  1169.         if( ab_DCI > m_iIP2_MAX_IDC_SKY74045 || ab_QCQ > m_iIP2_MAX_QDC_SKY74045 )
  1170.         {
  1171.             CalErrorHandler( WM_MF_RF_IP2_CHECK_FAIL );
  1172.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1173.                   " Fail: IP2 check ." + " I_DC = " + IntToStr(DCI) +
  1174.                   ", Q_DC = " + IntToStr(DCQ)  +
  1175.                   ", IP2_MAX_IDC = " + IntToStr( m_iIP2_MAX_IDC_SKY74045 ) +
  1176.                   ", IP2_MAX_QDC = " + IntToStr( m_iIP2_MAX_QDC_SKY74045 )
  1177.                 );
  1178.             return false;
  1179.         }
  1180.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1181.                   " Pass: IP2 check ." + " I_DC = " + IntToStr(DCI) +
  1182.                   ", Q_DC = " + IntToStr(DCQ) +
  1183.                   ", IP2_MAX_IDC = " + IntToStr( m_iIP2_MAX_IDC_SKY74045 ) +
  1184.                   ", IP2_MAX_QDC = " + IntToStr( m_iIP2_MAX_QDC_SKY74045 )
  1185.                 );
  1186.                 
  1187.         eFreqBand++;
  1188.     } // while            
  1189.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1190.              " ===================    IP2 check end    ==================== n "
  1191.            );
  1192.     PostMessage(
  1193.                      ctrl.hPostMsgDestHandle,
  1194.                      WM_MF_RF_IP2_CALIBRATION_DONE,
  1195.                      0,
  1196.                      0
  1197.                     );
  1198.     frmFatcory->DisableAllCalTimer();                
  1199.     return true;
  1200. }