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

MTK

开发平台:

C++ Builder

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