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

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_TXPCL_Skyworks77328.cpp
  40.  *
  41.  * Project:
  42.  * --------
  43.  *   Maui META APP
  44.  *
  45.  * Description:
  46.  * ------------
  47.  *  Skyworks 77328 TX PCL source
  48.  *
  49.  * Author:
  50.  * -------
  51.  *  Andy Ueng (mtk00490)
  52.  *
  53.  *============================================================================
  54.  *             HISTORY
  55.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  56.  *------------------------------------------------------------------------------
  57.  * $Revision$
  58.  * $Modtime$
  59.  * $Log$
  60.  *
  61.  *------------------------------------------------------------------------------
  62.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  63.  *============================================================================
  64. ****************************************************************************/
  65. #include <assert.h>
  66. #include <math.h>
  67. #pragma hdrstop
  68. #ifndef _T_META_FACTORY_CALIBRATION_H_
  69. #include "T_META_factory_calibration.H"
  70. #endif
  71. #ifndef _CAL_COMMON_H_
  72. #include "cal_common.h"
  73. #endif
  74. #ifndef _APCCAL_COMMON_H_
  75. #include "apccal_common.h"
  76. #endif
  77. #ifndef  _RF_APC_H_
  78. #include "rf_apc.h"
  79. #endif
  80. // callback
  81. #ifndef _META_FACTORY_RF_CB_H_
  82. #include "meta_factory_rf_cb.h"
  83. #endif
  84. #ifndef _META_FACTORY_NVRAM_CB_H_
  85. #include "meta_factory_nvram_cb.h"
  86. #endif
  87. // form
  88. #ifndef _META_FACTORY_H_
  89. #include "META_Factory.h"
  90. #endif
  91. // equipment
  92. #ifndef _AGECOMMON_H_
  93. #include "agecommon.h"
  94. #endif
  95. #ifndef  _AGE_MISC_H_
  96. #include "age_misc.h"
  97. #endif
  98. // misc
  99. #ifndef  _FT_UTILS_H_
  100. #include "ft_utils.h"
  101. #endif
  102. #ifndef  _BAND_UTILS_H_
  103. #include "band_utils.h"
  104. #endif
  105. #ifndef  _GSM_UTILS_H_
  106. #include "gsm_utils.h"
  107. #endif
  108. #ifndef  _TIME_UTILS_H_
  109. #include "time_utils.h"
  110. #endif
  111. //const double SKYWORKS_77328_CONST               = 3.1303;
  112. //const double SKYWORKS_77328_GSM850_SWITCH_LOSS  = 1.2;
  113. //const double SKYWORKS_77328_GSM_SWITCH_LOSS     = 1.2;
  114. //const double SKYWORKS_77328_DCS1800_SWITCH_LOSS = 1.3;
  115. //const double SKYWORKS_77328_PCS1900_SWITCH_LOSS = 1.3;
  116. //const double SKYWORKS_77328_GSM850_M            = 16.8;
  117. //const double SKYWORKS_77328_GSM_M               = 16.8;
  118. //const double SKYWORKS_77328_DCS1800_M           = 17.78;
  119. //const double SKYWORKS_77328_PCS1900_M           = 17.78;
  120. //----------------------------------------------------------------------------
  121. extern CRFAPC*  MF_rf_apc_ptr;
  122. extern RfNbtx_Req *MF_rf_tx_level_req;
  123. extern bool MF_rf_tx_level_cnf;
  124. extern bool  is_suspend_cal; // in T_META_factory_calibration.cpp
  125. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  126. /////////////////////////////     Skyworks 77328      ///////////////////////
  127. //---------------------------------------------------------------------------
  128. bool __fastcall T_META_factory_calibration::TXPCLCal_CalApcDcOffset_Skyworks77328( void )
  129. {
  130.     //AnsiString as_cal_flag = read_Sky77328_APC_DC_Offset_cal("MF_setup.txt", Application->ExeName);
  131.     //if( as_cal_flag.AnsiCompareIC("yes") != 0 )   return true;
  132.     short s_FIRST_PCL=0;
  133.     short s_LAST_PCL=0;
  134.     short s_first_pcl_idx;
  135.     short s_last_pcl_idx;
  136.     double   M;
  137.     unsigned short target_pcl;
  138.     unsigned short us_original_first_pcl_apc_dac;
  139.     unsigned short us_original_last_pcl_apc_dac;
  140.     int i_original_first_pcl_apc_dc_offset;
  141.     int i_original_last_pcl_apc_dc_offset;
  142.     int i_min_nominal_tx_power;
  143.     double  d_max_low_apc_dc_offset_p;
  144.     double  d_max_high_apc_dc_offset_p;
  145.     double  d_wanted_low_apc_dc_offset_p;
  146.     double  d_wanted_high_apc_dc_offset_p;
  147.     double  d_min_low_apc_dc_offset_p;
  148.     double  d_min_high_apc_dc_offset_p;
  149.     AnsiString   as_c;
  150.     AnsiString   as_sub_c;
  151.     log->Add( "n" +DateToStr(Date()) +  " " + CurrentTimeStr() +
  152.                   " =============== Calibrate APC DC offset value begin ============== "
  153.                 );
  154.     if( TX_SLOT_SINGLE == m_pCal->e_tx_slot )
  155.     {
  156.         if( ! m_rct_ctrl.RCT_operatingMode( m_pRct, OPERATING_MODE_GSM_BCH_TCH ) )
  157.         {
  158.             CalErrorHandler( WM_MF_AGE8960_SET_OPERATION_MODE_FAIL );
  159.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  160.                               " FAIL: " +
  161.                               m_pRct->as_RCT + " < Operation mode = GSM BCH+TCH"
  162.                             );
  163.             return false;
  164.         }
  165.     }
  166.     else
  167.     {
  168.         if( ! m_rct_ctrl.RCT_operatingMode( m_pRct, OPERATING_MODE_GPRS_BCH_PDTCH) )
  169.         {
  170.             CalErrorHandler( WM_MF_AGE8960_SET_OPERATION_MODE_FAIL );
  171.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  172.                               " FAIL: " +
  173.                               m_pRct->as_RCT + " < Operation mode = GPRS BCH+PDTCH"
  174.                             );
  175.             return false;
  176.         }
  177.     }
  178.     short s_ARFCN;
  179.     i_N_TX = -99; // TX frames
  180.     FrequencyBand eFreqBand = FrequencyBand850;
  181.     E_BANDSEL band_index;
  182.     int vi_Band;
  183.     while (eFreqBand < FrequencyBandCount)
  184.     {
  185.         switch (eFreqBand)
  186.         {
  187.             case FrequencyBand850:
  188.             {
  189.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  190.                 {
  191.                     eFreqBand++;
  192.                     continue;
  193.                 }
  194.                 vi_Band                  = age1960_GSM850_BAND;
  195.                 s_ARFCN                  = m_sARFCN_C0_GSM850;
  196.                 band_index               = BANDSEL_GSM850;
  197.                 s_FIRST_PCL              = GSM850_FIRST_PCL;
  198.                 s_LAST_PCL               = GSM850_LAST_PCL;
  199.                 as_c                     = m_as_gsm850_c;
  200.                 i_min_nominal_tx_power   = GSM850_MIN_NOMINAL_TX_POWER;
  201.                 d_max_low_apc_dc_offset_p = m_dSKY77328_GSM850_MAX_LOW_APC_DC_OFFSET_P;
  202.                 d_wanted_low_apc_dc_offset_p = m_dSKY77328_GSM850_WANTED_LOW_APC_DC_OFFSET_P;
  203.                 d_min_low_apc_dc_offset_p = m_dSKY77328_GSM850_MIN_LOW_APC_DC_OFFSET_P;
  204.                 d_max_high_apc_dc_offset_p = m_dSKY77328_GSM850_MAX_HIGH_APC_DC_OFFSET_P;
  205.                 d_wanted_high_apc_dc_offset_p = m_dSKY77328_GSM850_WANTED_HIGH_APC_DC_OFFSET_P;
  206.                 d_min_high_apc_dc_offset_p = m_dSKY77328_GSM850_MIN_HIGH_APC_DC_OFFSET_P;
  207.             }
  208.             break;
  209.             case FrequencyBand900:
  210.             {
  211.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  212.                 {
  213.                     eFreqBand++;
  214.                     continue;
  215.                 }
  216.                 vi_Band                  = age1960_EGSM_BAND;
  217.                 s_ARFCN                  = m_sARFCN_C0_GSM;
  218.                 band_index               = BANDSEL_GSM900;
  219.                 s_FIRST_PCL              = GSM900_FIRST_PCL;
  220.                 s_LAST_PCL               = GSM900_LAST_PCL;
  221.                 as_c                     = m_as_gsm900_c;
  222.                 i_min_nominal_tx_power   = GSM900_MIN_NOMINAL_TX_POWER;
  223.                 d_max_low_apc_dc_offset_p = m_dSKY77328_GSM900_MAX_LOW_APC_DC_OFFSET_P;
  224.                 d_wanted_low_apc_dc_offset_p = m_dSKY77328_GSM900_WANTED_LOW_APC_DC_OFFSET_P;
  225.                 d_min_low_apc_dc_offset_p = m_dSKY77328_GSM900_MIN_LOW_APC_DC_OFFSET_P;
  226.                 d_max_high_apc_dc_offset_p = m_dSKY77328_GSM900_MAX_HIGH_APC_DC_OFFSET_P;
  227.                 d_wanted_high_apc_dc_offset_p = m_dSKY77328_GSM900_WANTED_HIGH_APC_DC_OFFSET_P;
  228.                 d_min_high_apc_dc_offset_p = m_dSKY77328_GSM900_MIN_HIGH_APC_DC_OFFSET_P;
  229.             }
  230.             break;
  231.             case FrequencyBand1800:
  232.             {
  233.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  234.                 {
  235.                     eFreqBand++;
  236.                     continue;
  237.                 }
  238.                 vi_Band                  = age1960_DCS1800_BAND;
  239.                 s_ARFCN                  = m_sARFCN_C0_DCS;
  240.                 band_index               = BANDSEL_DCS1800;
  241.                 s_FIRST_PCL              = DCS1800_FIRST_PCL;
  242.                 s_LAST_PCL               = DCS1800_LAST_PCL;
  243.                 as_c                     = m_as_dcs1800_c;
  244.                 i_min_nominal_tx_power   = DCS1800_MIN_NOMINAL_TX_POWER;
  245.                 d_max_low_apc_dc_offset_p = m_dSKY77328_DCS1800_MAX_LOW_APC_DC_OFFSET_P;
  246.                 d_wanted_low_apc_dc_offset_p = m_dSKY77328_DCS1800_WANTED_LOW_APC_DC_OFFSET_P;
  247.                 d_min_low_apc_dc_offset_p = m_dSKY77328_DCS1800_MIN_LOW_APC_DC_OFFSET_P;
  248.                 d_max_high_apc_dc_offset_p = m_dSKY77328_DCS1800_MAX_HIGH_APC_DC_OFFSET_P;
  249.                 d_wanted_high_apc_dc_offset_p = m_dSKY77328_DCS1800_WANTED_HIGH_APC_DC_OFFSET_P;
  250.                 d_min_high_apc_dc_offset_p = m_dSKY77328_DCS1800_MIN_HIGH_APC_DC_OFFSET_P;
  251.             }
  252.             break;
  253.             case FrequencyBand1900:
  254.             {
  255.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  256.                 {
  257.                     eFreqBand++;
  258.                     continue;
  259.                 }
  260.                 vi_Band                  = age1960_PCS1900_BAND;
  261.                 s_ARFCN                  = m_sARFCN_C0_PCS;
  262.                 band_index               = BANDSEL_PCS1900;
  263.                 s_FIRST_PCL              = PCS1900_FIRST_PCL;
  264.                 s_LAST_PCL               = PCS1900_LAST_PCL;
  265.                 as_c                     = m_as_pcs1900_c;
  266.                 i_min_nominal_tx_power   = PCS1900_MIN_NOMINAL_TX_POWER;
  267.                 d_max_low_apc_dc_offset_p = m_dSKY77328_PCS1900_MAX_LOW_APC_DC_OFFSET_P;
  268.                 d_wanted_low_apc_dc_offset_p = m_dSKY77328_PCS1900_WANTED_LOW_APC_DC_OFFSET_P;
  269.                 d_min_low_apc_dc_offset_p = m_dSKY77328_PCS1900_MIN_LOW_APC_DC_OFFSET_P;
  270.                 d_max_high_apc_dc_offset_p = m_dSKY77328_PCS1900_MAX_HIGH_APC_DC_OFFSET_P;
  271.                 d_wanted_high_apc_dc_offset_p = m_dSKY77328_PCS1900_WANTED_HIGH_APC_DC_OFFSET_P;
  272.                 d_min_high_apc_dc_offset_p = m_dSKY77328_PCS1900_MIN_HIGH_APC_DC_OFFSET_P;
  273.             }
  274.             break;
  275.             default:
  276.             {
  277.                 assert(false);
  278.             }
  279.             break;
  280.         } // switch
  281.         if (! getAnsiStrSubItem( as_c, 1, DEFAULT_SEP_CHAR, as_sub_c) )
  282.         {
  283.              CalErrorHandler( WM_MF_CFG_READ_FAIL );
  284.              log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  285.                               " FAIL: Read config file fail(c)."
  286.                      );
  287.              return false;
  288.         }
  289.         if (IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  290.         {
  291.             unsigned char selectPCS1900;
  292.             selectPCS1900 = (FrequencyBand1900 == eFreqBand) ? 1 : 0;
  293.             log->Add("n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  294.                               " Target < set Band1900 flag = " + IntToStr(selectPCS1900)
  295.                         );
  296.             if (META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle, selectPCS1900, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  297.             {
  298.                 CalErrorHandler(WM_MF_RF_SELECTBAND_CNF_FAIL);
  299.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  300.                          " FAIL: Target < set Band1900 flag = " + IntToStr(selectPCS1900)
  301.                          );
  302.                 return false;
  303.             }
  304.             is_suspend_cal = true;
  305.             this->Suspend();
  306.             is_suspend_cal = false;
  307.         }
  308.         if( ! m_rct_ctrl.RCT_cellBand( m_pRct, vi_Band ) )
  309.         {
  310.             CalErrorHandler(WM_MF_AGE8960_SET_BAND_FAIL);
  311.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  312.                               " FAIL : " +
  313.                               m_pRct->as_RCT + " < Band = " + ViBand_To_Str(vi_Band)
  314.                             );
  315.             return false;
  316.         }
  317.      //   Sleep(100);
  318.         if ( this->Terminated )
  319.         {
  320.             this->OnTerminate = neByUser;
  321.             return false;
  322.         }
  323.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  324.                           m_pRct->as_RCT + " < Band = " + ViBand_To_Str( vi_Band )
  325.                         );
  326.         short bch_arfcn = ::Get_SeperateChannel(vi_Band, s_ARFCN);
  327.         if( ! m_rct_ctrl.RCT_BCHARFCN(m_pRct, bch_arfcn))
  328.         {
  329.             CalErrorHandler(WM_MF_AGE8960_SET_BCH_ARFCN_FAIL);
  330.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  331.                               " FAIL : "+
  332.                               m_pRct->as_RCT + " < BCH ARFCN = " + IntToStr(bch_arfcn)
  333.                     );
  334.             return false;
  335.         }
  336.         if (!m_rct_ctrl.RCT_TCHARFCN(m_pRct, s_ARFCN))
  337.         {
  338.             CalErrorHandler(WM_MF_AGE8960_SET_TCH_ARFCN_FAIL);
  339.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  340.                               " FAIL : "+
  341.                               m_pRct->as_RCT + " < TCH ARFCN = " + IntToStr( s_ARFCN )
  342.                             );
  343.             return false;
  344.         }
  345.         if (this->Terminated)
  346.         {
  347.             this->OnTerminate = neByUser;
  348.             return false;
  349.         }
  350.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  351.                           m_pRct->as_RCT + " < TCH ARFCN = " + IntToStr(s_ARFCN)
  352.                         );
  353.         if (!m_rct_ctrl.RCT_ConfigTSC(m_pRct, m_cTSC))
  354.         {
  355.             CalErrorHandler(WM_MF_AGE8960_SET_TSC_FAIL);
  356.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  357.                               "FAIL : " + m_pRct->as_RCT + " < set TSC fail."
  358.                             );
  359.             return false;
  360.         }
  361.         if (this->Terminated)
  362.         {
  363.             this->OnTerminate = neByUser;
  364.             return false;
  365.         }
  366.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  367.                  m_pRct->as_RCT + " < TSC = " + IntToStr(m_cTSC)
  368.                  );
  369.        RestartTimerCal(WM_MF_AGE8960_CONFIG_TX_POWER_FAIL);
  370.        if (!m_rct_ctrl.RCT_confTXPower(m_pRct))
  371.        {
  372.             CalErrorHandler(WM_MF_AGE8960_CONFIG_TX_POWER_FAIL);
  373.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  374.                                   " FAIL : " + m_pRct->as_RCT + " < config TX power 1."
  375.                                 );
  376.             return false;
  377.        }
  378.         PCL_To_PCLIndex(band_index, s_FIRST_PCL, s_first_pcl_idx);
  379.         PCL_To_PCLIndex(band_index, s_LAST_PCL, s_last_pcl_idx);
  380.         us_original_first_pcl_apc_dac = MF_rf_apc_ptr->Get_ApcProfilePower(band_index, s_first_pcl_idx);
  381.         us_original_last_pcl_apc_dac  = MF_rf_apc_ptr->Get_ApcProfilePower(band_index, s_last_pcl_idx);
  382.         MF_rf_apc_ptr->Get_RealApcDcOffset(m_pCal->b_TADOSupport, band_index, s_FIRST_PCL, i_original_first_pcl_apc_dc_offset);
  383.         MF_rf_apc_ptr->Get_RealApcDcOffset(m_pCal->b_TADOSupport, band_index, s_LAST_PCL, i_original_last_pcl_apc_dc_offset);
  384.         MF_rf_apc_ptr->Set_ApcProfilePower(band_index, s_first_pcl_idx, 0);
  385.         MF_rf_apc_ptr->Set_ApcProfilePower(band_index, s_last_pcl_idx, 0);
  386.        // MF_rf_apc_ptr->Req_Rf_SetRampApcLevel(eFreqBand, s_FIRST_PCL, 0);
  387.        // MF_rf_apc_ptr->Req_Rf_SetRampApcLevel(eFreqBand, s_LAST_PCL, 0);
  388.         int i_apc_lowest_power = MF_rf_apc_ptr->Get_ApcLowestPower(band_index);
  389.         int i_apc_dc_offset_count;
  390.         if (m_pCal->b_TADOSupport && (i_apc_lowest_power > i_min_nominal_tx_power))
  391.         {
  392.             i_apc_dc_offset_count = 2;
  393.         }
  394.         else
  395.         {
  396.             i_apc_dc_offset_count = 1;
  397.         }
  398.         unsigned short us_original_pcl_apc_dac[2];
  399.         int i_original_pcl_apc_dc_offset[2];
  400.         double d_max_apc_dc_offset_p[2];
  401.         double d_wanted_apc_dc_offset_p[2];
  402.         double d_min_apc_dc_offset_p[2];
  403.         us_original_pcl_apc_dac[0] = us_original_last_pcl_apc_dac;
  404.         us_original_pcl_apc_dac[1] = us_original_first_pcl_apc_dac;
  405.         i_original_pcl_apc_dc_offset[0] = i_original_last_pcl_apc_dc_offset;
  406.         i_original_pcl_apc_dc_offset[1] = i_original_first_pcl_apc_dc_offset;
  407.         d_max_apc_dc_offset_p[0]    = d_max_high_apc_dc_offset_p;
  408.         d_max_apc_dc_offset_p[1]    = d_max_low_apc_dc_offset_p;
  409.         d_wanted_apc_dc_offset_p[0] = d_wanted_high_apc_dc_offset_p;
  410.         d_wanted_apc_dc_offset_p[1] = d_wanted_low_apc_dc_offset_p;
  411.         d_min_apc_dc_offset_p[0]    = d_min_high_apc_dc_offset_p;
  412.         d_min_apc_dc_offset_p[1]    = d_min_low_apc_dc_offset_p;
  413.         double d_c = atof(as_sub_c.c_str());
  414.         AnsiString as_apc_dc_offset[2] = {"high", "low"};
  415.         int i_pcl[2] = {s_LAST_PCL, s_FIRST_PCL};
  416.         for (int apc_dc_offset_idx=0; apc_dc_offset_idx<i_apc_dc_offset_count; apc_dc_offset_idx++)
  417.         {
  418.             log->Add("n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  419.                        " Wanted " + as_apc_dc_offset[apc_dc_offset_idx] + " APC DC offset = " + Double_To_AnsiString(d_wanted_apc_dc_offset_p[apc_dc_offset_idx]) + " dB"
  420.                      );
  421.             if (!m_rct_ctrl.RCT_Config_ExpectedTxPower(m_pRct, false, d_wanted_apc_dc_offset_p[apc_dc_offset_idx]))
  422.             {
  423.                 RestartTimerCal(WM_MF_AGE8960_CONFIG_TX_POWER_FAIL);
  424.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  425.                                   " FAIL : " + m_pRct->as_RCT + " < config expected TX power = " + Double_To_AnsiString(d_wanted_apc_dc_offset_p[apc_dc_offset_idx]) + " fail"
  426.                                 );
  427.                 return false;
  428.             }
  429.            // if (!m_rct_ctrl.RCT_MSTXLevel(m_pRct, i_pcl[apc_dc_offset_idx]))
  430.            // {
  431.            //     CalErrorHandler(WM_MF_AGE8960_SET_MS_TX_LEVEL_FAIL);
  432.            //     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  433.            //                       " FAIL : "
  434.            //                        + m_pRct->as_RCT + " < MS TX level = " + IntToStr(i_pcl[apc_dc_offset_idx])
  435.            //                     );
  436.            //     return false;
  437.            // }
  438.            // if ( this->Terminated )
  439.            // {
  440.            //     this->OnTerminate = neByUser;
  441.            //     return false;
  442.            // }
  443.            // log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  444.            //                   m_pRct->as_RCT + " < MS TX level = " + IntToStr(i_pcl[apc_dc_offset_idx])
  445.            //                 );
  446.            //
  447.             m_iApcDcOffset = us_original_first_pcl_apc_dac + i_original_pcl_apc_dc_offset[apc_dc_offset_idx];
  448.             if (!MF_rf_apc_ptr->Set_RealApcDcOffset(m_pCal->b_TADOSupport, band_index, i_pcl[apc_dc_offset_idx], m_iApcDcOffset))
  449.             {
  450.                 CalErrorHandler(WM_MF_RF_TX_LEVEL_FAIL);
  451.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  452.                               " FAIL: Set APC DC offset " +
  453.                               " TADO support = " + IntToStr(m_pCal->b_TADOSupport) +
  454.                               " eFreqBand = " + IntToStr(eFreqBand) +
  455.                               " pcl = " + IntToStr(i_pcl[apc_dc_offset_idx])
  456.                                );
  457.                             return false;
  458.             }
  459.             MF_rf_apc_ptr->ConfirmCallback   = ::ccb_write_apc_to_nvram;
  460.             MF_rf_apc_ptr->REQ_RfSetRampTable_Start(m_pCal->ui_rf_id, m_pCal->b_TADOSupport, band_index);
  461.             RestartTimerCal(WM_MF_SET_APC_DC_OFFSET_FAIL);
  462.             SUSPEND_CAL_THREAD
  463.             CHECK_TERMINATE_BY_USER
  464.             E_METAAPP_RESULT_T state = MF_rf_apc_ptr->Get_ConfirmState();
  465.             if (state != METAAPP_SUCCESS)
  466.             {
  467.                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  468.                 {
  469.                     CalErrorHandler(WM_MF_NVRAM_EF_L1_RAMPTABLE_xxx_LID_VERNO_FAIL);
  470.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  471.                                           " FAIL : NVRAM_EF_L1_RAMPTABLE_xxx_LID version is not support, please update META to latest version. "
  472.                                          );
  473.                 }
  474.                 else
  475.                 {
  476.                     CalErrorHandler(WM_MF_SET_APC_DAC_FAIL);
  477.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  478.                                       " FAIL: Target < set APC DC offset, band_index = " + IntToStr(band_index) +
  479.                                       " ,PCL = "+ i_pcl[apc_dc_offset_idx] +
  480.                                       " ,APC DC offset = " +  IntToStr(m_iApcDcOffset)
  481.                                     );
  482.                     return false;
  483.                 }
  484.             }
  485.             for (int iRealRecursiveTimes=0; iRealRecursiveTimes<i_TXlevelRecusiveTimes; iRealRecursiveTimes++)
  486.             {
  487.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  488.                        " RecursiveTimes = " + IntToStr(iRealRecursiveTimes)
  489.                      );
  490.                 RestartTimerCal(WM_MF_RF_STOP_FAIL);
  491.                 if (!MF_rf_stop.REQ_Start())
  492.                 {
  493.                     CalErrorHandler(WM_MF_RF_STOP_FAIL);
  494.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  495.                              " FAIL: Target < stop RF fail."
  496.                             );
  497.                     return false;
  498.                 }
  499.                 RestartTimerCal(WM_MF_RF_TX_LEVEL_FAIL);
  500.                 MF_rf_tx_level_req->arfcn       = s_ARFCN;
  501.                 MF_rf_tx_level_req->bsic        = m_cTSC;
  502.                 MF_rf_tx_level_req->power       = i_pcl[apc_dc_offset_idx];
  503.                 MF_rf_tx_level_req->frames      = i_N_TX;
  504.                 MF_rf_tx_level_req->dacValue    = m_sDefault_value;
  505.                 MF_rf_tx_level_req->burstTypeNB = NB_TX_RANDOM_WITH_TSC;
  506.                 if (META_Rf_NB_TX_r(m_pCal->i_MainMETAHandle,
  507.                                     MF_rf_tx_level_req,
  508.                                     MF_rf_tx_level_cb,
  509.                                     &MF_rf_tx_level_token,
  510.                                     NULL
  511.                                    ) != META_SUCCESS)
  512.                 {
  513.                     CalErrorHandler(WM_MF_RF_TX_LEVEL_FAIL);
  514.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  515.                               " FAIL : Target < ARFCN = " + IntToStr(s_ARFCN) +
  516.                               ", TSC = " + IntToStr(m_cTSC) +
  517.                               ", power = " + IntToStr(i_pcl[apc_dc_offset_idx]) +
  518.                               ", frames = " + IntToStr(i_N_TX) +
  519.                               ", dac value = " + IntToStr(m_sDefault_value)
  520.                             );
  521.                     return false;
  522.                }
  523.                if ( this->Terminated )
  524.                {
  525.                     this->OnTerminate = neByUser;
  526.                     return false;
  527.                }
  528.                log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  529.                          " Target < TX: ARFCN = " + IntToStr( s_ARFCN ) +
  530.                          ", TSC = " + IntToStr( m_cTSC ) +
  531.                          ", power = " + IntToStr(i_pcl[apc_dc_offset_idx]) +
  532.                          ", frames = " + IntToStr(i_N_TX) +
  533.                          ", dac value = " + IntToStr( m_sDefault_value )
  534.                        );
  535.               // log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  536.               //            " TimerCal->Enabled 7= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  537.               //          );
  538.                 RestartTimerCal( WM_MF_RF_TX_LEVEL_FAIL );
  539.                 is_suspend_cal = true;
  540.                 this->Suspend();
  541.                 is_suspend_cal = false;
  542.                 if (!MF_rf_tx_level_cnf)
  543.                 {
  544.                     CalErrorHandler(WM_MF_RF_TX_LEVEL_FAIL);
  545.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  546.                               " FAIL : ! MF_rf_tx_level_cnf"
  547.                             );
  548.                     return false;
  549.                 }
  550.                 if (this->Terminated)
  551.                 {
  552.                     this->OnTerminate = neByUser;
  553.                     return false;
  554.                 }
  555.                 RestartTimerCal( WM_MF_AGE8960_READ_TX_POWER_FAIL );
  556.                 if (!m_rct_ctrl.RCT_ReadTxPower(m_pRct, m_virTX))
  557.                 {
  558.                     CalErrorHandler(WM_MF_AGE8960_READ_TX_POWER_FAIL);
  559.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  560.                                   " FAIL : " + m_pRct->as_RCT + " < read TX power 1."
  561.                                 );
  562.                     return false;
  563.                 }
  564.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +  " " +
  565.                               m_pRct->as_RCT + " < read TX power = "+ Double_To_AnsiString(m_virTX)
  566.                             );
  567.                 signed short  ss1;
  568.                 double d1;
  569.                 if (iRealRecursiveTimes != i_TXlevelRecusiveTimes-1) //iteration
  570.                 {
  571.                     if (m_virTX > (d_wanted_apc_dc_offset_p[apc_dc_offset_idx] + m_dAPC_Delta) ||
  572.                         m_virTX < (d_wanted_apc_dc_offset_p[apc_dc_offset_idx] - m_dAPC_Delta)
  573.                        )
  574.                     {
  575.                         d1 = d_c * (d_wanted_apc_dc_offset_p[apc_dc_offset_idx]  - m_virTX );
  576.                         ss1 = (signed short) (d1>0) ? floor(d1+1.0): ceil(d1-1.0);
  577.                         if( ! MF_rf_apc_ptr->Get_RealApcDcOffset( m_pCal->b_TADOSupport, band_index, i_pcl[apc_dc_offset_idx], m_iApcDcOffset ) )
  578.                         {
  579.                             CalErrorHandler(WM_MF_RF_TX_LEVEL_FAIL);
  580.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  581.                               " FAIL: Get APC DC offset " +
  582.                               " TADO support = " + IntToStr( m_pCal->b_TADOSupport ) +
  583.                               " eFreqBand = " + IntToStr( eFreqBand ) +
  584.                               " pcl = " + IntToStr(i_pcl[apc_dc_offset_idx])
  585.                                );
  586.                             return false;
  587.                         }
  588.                         m_iApcDcOffset += ss1;
  589.                         if (!MF_rf_apc_ptr->Set_RealApcDcOffset(m_pCal->b_TADOSupport, band_index, i_pcl[apc_dc_offset_idx], m_iApcDcOffset))
  590.                         {
  591.                             CalErrorHandler(WM_MF_RF_TX_LEVEL_FAIL);
  592.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  593.                               " FAIL: Set APC DC offset " +
  594.                               " TADO support = " + IntToStr(m_pCal->b_TADOSupport) +
  595.                               " eFreqBand = " + IntToStr(eFreqBand) +
  596.                               " pcl = " + IntToStr(i_pcl[apc_dc_offset_idx])
  597.                                );
  598.                             return false;
  599.                         }
  600.                         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  601.                                   " delta s = "+
  602.                                   AnsiString(ss1)
  603.                                 );
  604.                         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  605.                                   " new APC DC offset DAC = "+
  606.                                   IntToStr(m_iApcDcOffset)
  607.                                 );
  608.                         MF_rf_apc_ptr->ConfirmCallback   = ::ccb_write_apc_to_nvram;
  609.                         MF_rf_apc_ptr->REQ_RfSetRampTable_Start(m_pCal->ui_rf_id, m_pCal->b_TADOSupport, band_index);
  610.                         RestartTimerCal(WM_MF_SET_APC_DC_OFFSET_FAIL);
  611.                         SUSPEND_CAL_THREAD
  612.                         CHECK_TERMINATE_BY_USER
  613.                         E_METAAPP_RESULT_T state = MF_rf_apc_ptr->Get_ConfirmState();
  614.                         if (state != METAAPP_SUCCESS)
  615.                         {
  616.                             if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  617.                             {
  618.                                 CalErrorHandler(WM_MF_NVRAM_EF_L1_RAMPTABLE_xxx_LID_VERNO_FAIL);
  619.                                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  620.                                           " FAIL : NVRAM_EF_L1_RAMPTABLE_xxx_LID version is not support, please update META to latest version. "
  621.                                          );
  622.                             }
  623.                             else
  624.                             {
  625.                                 CalErrorHandler(WM_MF_SET_APC_DAC_FAIL);
  626.                                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  627.                                       " FAIL: Target < set APC DC offset, band_index = " + IntToStr(band_index) +
  628.                                       " ,PCL = "+ i_pcl[apc_dc_offset_idx] +
  629.                                       " ,APC DC offset = " +  IntToStr(m_iApcDcOffset)
  630.                                     );
  631.                                 return false;
  632.                             }
  633.                         }
  634.                         CHECK_TERMINATE_BY_USER
  635.                         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  636.                                       " Target < set APC DC offset, band_index = " + IntToStr(band_index) +
  637.                                       " ,PCL = "+ i_pcl[apc_dc_offset_idx] +
  638.                                       " ,APC DC offset = " +  IntToStr(m_iApcDcOffset)
  639.                                     );
  640.                     }
  641.                     else
  642.                     {
  643.                          break; // for iRealRecursiveTimes
  644.                     }
  645.                 } // if no iteration
  646.                 else if (iRealRecursiveTimes == i_TXlevelRecusiveTimes-1 )
  647.                 {
  648.                     if ((m_virTX > d_max_apc_dc_offset_p[apc_dc_offset_idx]) ||
  649.                         (m_virTX < d_min_apc_dc_offset_p[apc_dc_offset_idx])
  650.                        )
  651.                     {
  652.                         CalErrorHandler(WM_MF_RF_TX_LEVEL_CHECK_FAIL);
  653.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  654.                                       m_pRct->as_RCT + " > read TX power = "+ Double_To_AnsiString(m_virTX) +
  655.                                       " ( Out of Range ), min TX power = " + Double_To_AnsiString(d_min_apc_dc_offset_p[apc_dc_offset_idx]) + ", max TX power = " + Double_To_AnsiString(d_max_apc_dc_offset_p[apc_dc_offset_idx])
  656.                                     );
  657.                         return false;
  658.                     }
  659.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  660.                              m_pRct->as_RCT + " > read TX power = "+ Double_To_AnsiString(m_virTX) +
  661.                              " ( Pass ), min TX power = " + Double_To_AnsiString(d_min_apc_dc_offset_p[apc_dc_offset_idx]) + ", max TX power = " + Double_To_AnsiString(d_max_apc_dc_offset_p[apc_dc_offset_idx])
  662.                              );
  663.                 }
  664.             } // for iRealRecursiveTimes
  665.         } // for apc_dc_offset_idx
  666.         MF_rf_apc_ptr->Set_ApcProfilePower(band_index, s_first_pcl_idx, us_original_first_pcl_apc_dac);
  667.         MF_rf_apc_ptr->Set_ApcProfilePower(band_index, s_last_pcl_idx, us_original_last_pcl_apc_dac);
  668.         MF_rf_apc_ptr->ConfirmCallback = ::ccb_write_apc_to_nvram;
  669.         MF_rf_apc_ptr->REQ_Write_APC_To_NVRAM_Single_Band_Start(m_pCal->ui_rf_id, band_index, m_pCal->b_TADOSupport );
  670.         eFreqBand++;
  671.     }
  672.     log->Add( "n" +DateToStr(Date()) +  " " + CurrentTimeStr() +
  673.                   " ===============  Calibrate APC DC offset value end  ============== "
  674.                 );
  675.     RestartTimerCal(WM_MF_RF_STOP_FAIL);
  676.     if (!MF_rf_stop.REQ_Start())
  677.     {
  678.         CalErrorHandler(WM_MF_RF_STOP_FAIL);
  679.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  680.                           " FAIL: Target < stop RF fail."
  681.                         );
  682.         return false;
  683.     }
  684.     frmFatcory->DisableAllCalTimer();
  685.     return true;
  686. }
  687. //----------------------------------------------------------------------------
  688. bool __fastcall T_META_factory_calibration::TXPCLCal_EvalDac_Skyworks77328( void )
  689. {
  690.      AnsiString   as_tx_pcl;
  691.      AnsiString   as_tx_2cal_pcl;
  692.      AnsiString   as_wanted_p;
  693.      AnsiString   as_correction;
  694.      AnsiString   as_sub_tx_pcl;
  695.      AnsiString   as_sub_tx_2cal_pcl;
  696.      AnsiString   as_sub_wanted_p;
  697.      AnsiString   as_sub_correction;
  698.      // Evaluate a and b
  699.     // double   switch_loss;
  700.      double   M;
  701.      double   Mnew;
  702.      unsigned short weight;
  703.      double d_weight;
  704.      unsigned short ramp_value;
  705.      unsigned short battery_compensate;
  706.      double  d_battery_compensate;
  707.      unsigned short DACth;
  708.      unsigned short DACthnew;
  709.      unsigned short DACwant;
  710.      unsigned short DACd;
  711.      double   Poff;
  712.      double   Poffnew;
  713.     // double   Pmeas;
  714.      unsigned short i_PCL_Cal[MAX_SUPPORT_BAND_NUM][CALIBRATIE_NUM_2PCL];
  715.      unsigned short target_pcl;
  716.      unsigned short EffPCLDac_Cal[MAX_SUPPORT_BAND_NUM][CALIBRATIE_NUM_2PCL];
  717.     // double PoutV_Cal[MAX_SUPPORT_BAND_NUM][CALIBRATIE_NUM_2PCL];
  718.     // double Vramp_Cal[MAX_SUPPORT_BAND_NUM][CALIBRATIE_NUM_2PCL];
  719.    //  double PoutV_Eval[MAX_SUPPORT_BAND_NUM][RF_RAMP_TABLE_TX_LEVEL_COUNT];
  720.    //  double Vramp_Eval[MAX_SUPPORT_BAND_NUM][RF_RAMP_TABLE_TX_LEVEL_COUNT];
  721.      unsigned short EffPclDac_Eval[MAX_SUPPORT_BAND_NUM][RF_RAMP_TABLE_TX_LEVEL_COUNT];
  722.     log->Add( "n" +DateToStr(Date()) +  " " + CurrentTimeStr() +
  723.                   " =================== Evaluate PCL DAC value begin =================== "
  724.                 );
  725.     int FIRST_PCL=0;
  726.     short s_ARFCN;
  727.     FrequencyBand eFreqBand = FrequencyBand850;
  728.     E_BANDSEL band_index;
  729.     int vi_Band;
  730.     while (eFreqBand < FrequencyBandCount)
  731.     {
  732.         switch (eFreqBand)
  733.         {
  734.             case FrequencyBand850:
  735.             {
  736.                 if (! IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  737.                 {
  738.                     eFreqBand++;
  739.                     continue;
  740.                 }
  741.                 vi_Band        = age1960_GSM850_BAND;
  742.                 s_ARFCN        = m_sARFCN_C0_GSM850;
  743.                 band_index     = BANDSEL_GSM850;
  744.                 FIRST_PCL      = GSM850_FIRST_PCL;
  745.                 as_tx_pcl      = m_as_gsm850_tx_pcl;
  746.                 as_tx_2cal_pcl = m_as_gsm850_tx_2cal_pcl;
  747.                 as_wanted_p    = m_as_gsm850_wanted_p;
  748.                 as_correction  = m_as_gsm850_correction;
  749.                 M              = d_SKY77328_GSM850_M;
  750.                 target_pcl     = i_SKY77328_GSM850_TARGET_PCL;
  751.             }
  752.             break;
  753.             case FrequencyBand900:
  754.             {
  755.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  756.                 {
  757.                     eFreqBand++;
  758.                     continue;
  759.                 }
  760.                 vi_Band        = age1960_EGSM_BAND;
  761.                 s_ARFCN        = m_sARFCN_C0_GSM;
  762.                 band_index     = BANDSEL_GSM900;
  763.                 FIRST_PCL      = GSM900_FIRST_PCL;
  764.                 as_tx_pcl      = m_as_gsm900_tx_pcl;
  765.                 as_tx_2cal_pcl = m_as_gsm900_tx_2cal_pcl;
  766.                 as_wanted_p    = m_as_gsm900_wanted_p;
  767.                 as_correction  = m_as_gsm900_correction;
  768.                 M              = d_SKY77328_GSM900_M;
  769.                 target_pcl     = i_SKY77328_GSM900_TARGET_PCL;
  770.             }
  771.             break;
  772.             case FrequencyBand1800:
  773.             {
  774.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  775.                 {
  776.                     eFreqBand++;
  777.                     continue;
  778.                 }
  779.                 vi_Band        = age1960_DCS1800_BAND;
  780.                 s_ARFCN        = m_sARFCN_C0_DCS;
  781.                 band_index     = BANDSEL_DCS1800;
  782.                 FIRST_PCL      = DCS1800_FIRST_PCL;
  783.                 as_tx_pcl      = m_as_dcs1800_tx_pcl;
  784.                 as_tx_2cal_pcl = m_as_dcs1800_tx_2cal_pcl;
  785.                 as_wanted_p    = m_as_dcs1800_wanted_p;
  786.                 as_correction  = m_as_dcs1800_correction;
  787.                 M              = d_SKY77328_DCS1800_M;
  788.                 target_pcl     = i_SKY77328_DCS1800_TARGET_PCL;
  789.             }
  790.             break;
  791.             case FrequencyBand1900:
  792.             {
  793.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  794.                 {
  795.                     eFreqBand++;
  796.                     continue;
  797.                 }
  798.                 vi_Band        = age1960_PCS1900_BAND;
  799.                 s_ARFCN        = m_sARFCN_C0_PCS;
  800.                 band_index     = BANDSEL_PCS1900;
  801.                 FIRST_PCL      = PCS1900_FIRST_PCL;
  802.                 as_tx_pcl      = m_as_pcs1900_tx_pcl;
  803.                 as_tx_2cal_pcl = m_as_pcs1900_tx_2cal_pcl;
  804.                 as_wanted_p    = m_as_pcs1900_wanted_p;
  805.                 as_correction  = m_as_pcs1900_correction;
  806.                 M              = d_SKY77328_PCS1900_M;
  807.                 target_pcl     = i_SKY77328_PCS1900_TARGET_PCL;
  808.             }
  809.             break;
  810.             default:
  811.                 assert(false);
  812.             break;
  813.         } // switch
  814.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  815.                               "---------------------------------------------"
  816.                         );
  817.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  818.                               BandIdx_To_String(band_index)
  819.                         );
  820.         if (IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  821.         {
  822.             if(eFreqBand == FrequencyBand1900)
  823.             {
  824.                 if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle, 1, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  825.                 {
  826.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  827.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  828.                                   " FAIL: Target < set Band1900 flag = 1"
  829.                                 );
  830.                     return false;
  831.                 }
  832.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  833.                               " Target < set Band1900 flag = 1"
  834.                         );
  835.             }
  836.             else
  837.             {
  838.                 if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle, 0, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  839.                 {
  840.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  841.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  842.                                   " FAIL: Target < set Band1900 flag = 0"
  843.                                 );
  844.                     return false;
  845.                 }
  846.                 
  847.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  848.                               " Target < set Band1900 flag = 0"
  849.                             );
  850.             }
  851.             is_suspend_cal = true;
  852.             this->Suspend();
  853.             is_suspend_cal = false;
  854.         }
  855.                 
  856.         if( ! m_rct_ctrl.RCT_cellBand( m_pRct, vi_Band ) )
  857.         {
  858.             CalErrorHandler( WM_MF_AGE8960_SET_BAND_FAIL );
  859.             if ( frmFatcory->rbAgilent8960->Checked )
  860.             {
  861.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  862.                               " FAIL :"+
  863.                               " Agilent 8960< Band = " + ViBand_To_Str( vi_Band )
  864.                             );
  865.             }
  866.             else
  867.             {
  868.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  869.                               " FAIL :"+
  870.                               " cmu200 > Band = " + ViBand_To_Str( vi_Band )
  871.                             );
  872.             }
  873.             return false;
  874.         }
  875.         if ( this->Terminated )
  876.         {
  877.             this->OnTerminate = neByUser;
  878.             return false;
  879.         }
  880.         log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  881.                   " ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
  882.                 );
  883.         if ( frmFatcory->rbAgilent8960->Checked )
  884.         {
  885.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  886.                       " Agilent 8960< Band = " + ViBand_To_Str( vi_Band )
  887.                     );
  888.         }
  889.         else
  890.         {
  891.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  892.                       " CMU 200< Band = " + ViBand_To_Str( vi_Band )
  893.                     );
  894.         }
  895.         Sleep( 100 );  // wait Agilent 8960 ready!
  896.                    // if Agilent 8960 not read, target will get error power!
  897.         short bch_arfcn = ::Get_SeperateChannel( vi_Band, s_ARFCN );
  898.         if( ! m_rct_ctrl.RCT_BCHARFCN( m_pRct, bch_arfcn) )
  899.         {
  900.             CalErrorHandler( WM_MF_AGE8960_SET_TCH_ARFCN_FAIL );
  901.             if ( frmFatcory->rbAgilent8960->Checked )
  902.             {
  903.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  904.                               " FAIL :"+
  905.                               " Agilent 8960< BCH ARFCN = " + IntToStr( bch_arfcn )
  906.                     );
  907.             }
  908.             else
  909.             {
  910.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  911.                               " FAIL :"+
  912.                               " CMU 200< BCH ARFCN = " + IntToStr( bch_arfcn )
  913.                     );
  914.             }
  915.             return false;
  916.         }
  917.         if( ! m_rct_ctrl.RCT_TCHARFCN( m_pRct, s_ARFCN ) )
  918.         {
  919.             CalErrorHandler( WM_MF_AGE8960_SET_TCH_ARFCN_FAIL );
  920.             if ( frmFatcory->rbAgilent8960->Checked )
  921.             {
  922.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  923.                               " FAIL :"+
  924.                               " Agilent 8960< TCH ARFCN = " + IntToStr( s_ARFCN )
  925.                             );
  926.             }
  927.             else
  928.             {
  929.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  930.                               " FAIL :"+
  931.                               " CMU 200< TCH ARFCN = " + IntToStr( s_ARFCN )
  932.                             );
  933.             }
  934.             return false;
  935.         }
  936.         if ( this->Terminated )
  937.         {
  938.             this->OnTerminate = neByUser;
  939.             return false;
  940.         }
  941.         if ( frmFatcory->rbAgilent8960->Checked )
  942.         {
  943.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  944.                           " Agilent 8960< TCH ARFCN = " + IntToStr( s_ARFCN )
  945.                         );
  946.         }
  947.         else
  948.         {
  949.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  950.                           " CMU 200< TCH ARFCN = " + IntToStr( s_ARFCN )
  951.                         );
  952.         }
  953.         // ----------------------- set Agilent 8960 < TSC -------------------------
  954.         if( ! m_rct_ctrl.RCT_ConfigTSC( m_pRct, m_cTSC ) )
  955.         {
  956.             CalErrorHandler( WM_MF_AGE8960_SET_TSC_FAIL );
  957.             if( frmFatcory->rbAgilent8960->Checked )
  958.             {
  959.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  960.                               "FAIL : Agilent 8960 set TSC fail."
  961.                             );
  962.             }
  963.             else
  964.             {
  965.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  966.                           " FAIL : cmu200 > TSC = " + IntToStr( m_cTSC)
  967.                         );
  968.             }
  969.             return false;
  970.         }
  971.         if ( this->Terminated )
  972.         {
  973.                 this->OnTerminate = neByUser;
  974.                 return false;
  975.         }
  976.         if ( frmFatcory->rbAgilent8960->Checked )
  977.         {
  978.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  979.                           " Agilent 8960< TSC = " + IntToStr( m_cTSC)
  980.                         );
  981.         }
  982.         else
  983.         {
  984.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  985.                           " CMU 200< TSC = " + IntToStr( m_cTSC)
  986.                         );
  987.         }
  988.        RestartTimerCal( WM_MF_AGE8960_CONFIG_TX_POWER_FAIL );
  989.        if (!m_rct_ctrl.RCT_Config_ExpectedTxPower(m_pRct, true, 0))
  990.        {
  991.             RestartTimerCal(WM_MF_AGE8960_CONFIG_TX_POWER_FAIL);
  992.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  993.                                   " FAIL : " + m_pRct->as_RCT + " < config expected TX power to auto fail"
  994.                                 );
  995.             return false;
  996.        }
  997.        if( ! m_rct_ctrl.RCT_confTXPower( m_pRct ) )
  998.        {
  999.                     CalErrorHandler( WM_MF_AGE8960_CONFIG_TX_POWER_FAIL );
  1000.                     if ( frmFatcory->rbAgilent8960->Checked )
  1001.                     {
  1002.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1003.                                   " FAIL : Agilent 8960< config TX power 1."
  1004.                                 );
  1005.                     }
  1006.                     else
  1007.                     {
  1008.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1009.                                   " FAIL : CMU 200< config TX power 1."
  1010.                                 );
  1011.                     }
  1012.                     return false;
  1013.        }
  1014.        // int i=0;
  1015.         for(int i=0; i<CALIBRATIE_NUM_2PCL; i++)
  1016.         {
  1017.            // PoutV_Cal[band_index][i] = sqrt(50.0*pow(10, (m_dPCL_dBm[band_index][i]+switch_loss)/10.0)/1000.0);
  1018.            // log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1019.            //                " PoutV_Cal[" + IntToStr(band_index) + "][" + IntToStr(i) + "] = " +
  1020.            //                  Double_To_AnsiString(PoutV_Cal[band_index][i]) + "n"
  1021.            //             );
  1022.             if (! getAnsiStrSubItem( as_tx_2cal_pcl, i+1, DEFAULT_SEP_CHAR, as_sub_tx_2cal_pcl) )
  1023.             {
  1024.                 CalErrorHandler( WM_MF_CFG_READ_FAIL );
  1025.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1026.                               " FAIL: Read config file fail(sub_tx_cal_pcl)."
  1027.                         );
  1028.                 return false;
  1029.             }
  1030.             i_PCL_Cal[band_index][i] = as_sub_tx_2cal_pcl.ToInt();
  1031.             l1cal_rampTable_T* p_ramp_table = MF_rf_apc_ptr->Get_ApcProfile(band_index);
  1032.             if(! MF_rf_apc_ptr->Get_SubBandWeighting( *p_ramp_table, s_ARFCN, as_sub_tx_2cal_pcl.ToInt(), weight) )
  1033.             {
  1034.                 CalErrorHandler( WM_MF_RF_TX_LEVEL_EVALDAC_FAIL );
  1035.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1036.                               " FAIL: get sub-band weighting."
  1037.                         );
  1038.                 return false;
  1039.             }
  1040.             d_weight = 1.0 * weight / WEIGHT_SCALE;
  1041.             if(! MF_rf_apc_ptr->Get_RightTopRampUpValue(*p_ramp_table, band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt(), ramp_value) )
  1042.             {
  1043.                 CalErrorHandler( WM_MF_RF_TX_LEVEL_EVALDAC_FAIL );
  1044.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1045.                               " FAIL: get ramp up value."
  1046.                         );
  1047.                 return false;
  1048.             }
  1049.             if(! MF_rf_apc_ptr->Get_BatteryCompensate(*p_ramp_table, MID_VOLTAGE_INDEX, MID_TEMPERATURE_INDEX, battery_compensate) )
  1050.             {
  1051.                 CalErrorHandler( WM_MF_RF_TX_LEVEL_EVALDAC_FAIL );
  1052.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1053.                               " FAIL: get battery_compensate value."
  1054.                         );
  1055.                 return false;
  1056.             }
  1057.             d_battery_compensate = 1.0* battery_compensate / BATTERY_COMPENSATE_SCALE;
  1058.             EffPCLDac_Cal[band_index][i] = m_usPCLDacValue[band_index][i] * d_weight * d_battery_compensate * ramp_value / MAX_RAMP_VALUE;
  1059.            // Vramp_Cal[band_index][i] = (MF_rf_apc_ptr->ApcDcOffset[band_index]+EffPCLDac_Cal[band_index][i])/pow(2.0, 10.0) * 2.8;
  1060.            // log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1061.            //                " Vramp_Cal[" + IntToStr(band_index) + "][" + IntToStr(i) + "] = " +
  1062.            //                  Double_To_AnsiString(Vramp_Cal[band_index][i]) + "n"
  1063.            //             );
  1064.         }  // for
  1065.         // 2. Calculate DACth
  1066.         int i_apc_dc_offset[2];
  1067.         double factor = pow(10.0, (m_dPCL_dBm[band_index][1]-m_dPCL_dBm[band_index][0])/M);
  1068.        // DACth = ( (EffPCLDac_Cal[band_index][0]+MF_rf_apc_ptr->ApcDcOffset[band_index]) * factor - (EffPCLDac_Cal[band_index][1]+MF_rf_apc_ptr->ApcDcOffset[band_index]) ) / ( factor - 1 );
  1069.        // DACth = ( (EffPCLDac_Cal[band_index][0]+MF_rf_apc_ptr->Get_HighApcDcOffset(band_index)) * factor - (EffPCLDac_Cal[band_index][1]+MF_rf_apc_ptr->Get_HighApcDcOffset(band_index)) ) / ( factor - 1 );
  1070.         if( ! MF_rf_apc_ptr->Get_RealApcDcOffset( m_pCal->b_TADOSupport, band_index, target_pcl, m_iApcDcOffset ) )
  1071.         {
  1072.             CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1073.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1074.                               " FAIL: Get APC DC offset " +
  1075.                               " TADO support = " + IntToStr( m_pCal->b_TADOSupport ) +
  1076.                               " eFreqBand = " + IntToStr( eFreqBand ) +
  1077.                               " pcl = " + IntToStr( i_PCL_Cal[band_index][0] )
  1078.                                );
  1079.             return false;
  1080.         }
  1081.         if( ! MF_rf_apc_ptr->Get_RealApcDcOffset( m_pCal->b_TADOSupport, band_index, i_PCL_Cal[band_index][0], i_apc_dc_offset[0] ) )
  1082.         {
  1083.             CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1084.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1085.                               " FAIL: Get APC DC offset " +
  1086.                               " TADO support = " + IntToStr( m_pCal->b_TADOSupport ) +
  1087.                               " eFreqBand = " + IntToStr( eFreqBand ) +
  1088.                               " pcl = " + IntToStr( i_PCL_Cal[band_index][0] )
  1089.                                );
  1090.             return false;
  1091.         }
  1092.         if( ! MF_rf_apc_ptr->Get_RealApcDcOffset( m_pCal->b_TADOSupport, band_index, i_PCL_Cal[band_index][1], i_apc_dc_offset[1] ) )
  1093.         {
  1094.             CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1095.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1096.                               " FAIL: Get APC DC offset " +
  1097.                               " TADO support = " + IntToStr( m_pCal->b_TADOSupport ) +
  1098.                               " eFreqBand = " + IntToStr( eFreqBand ) +
  1099.                               " pcl = " + IntToStr( i_PCL_Cal[band_index][1] )
  1100.                                );
  1101.             return false;
  1102.         }
  1103.         DACth = ( (EffPCLDac_Cal[band_index][0]+i_apc_dc_offset[0]) * factor - (EffPCLDac_Cal[band_index][1]+i_apc_dc_offset[1]) ) / ( factor - 1 );
  1104.         // 3. Calculate Poff
  1105.        // Poff = m_dPCL_dBm[band_index][0] - M * log10(1.0*(EffPCLDac_Cal[band_index][0]+MF_rf_apc_ptr->ApcDcOffset[band_index] - DACth));
  1106.        // Poff = m_dPCL_dBm[band_index][0] - M * log10(1.0*(EffPCLDac_Cal[band_index][0]+MF_rf_apc_ptr->Get_HighApcDcOffset(band_index) - DACth));
  1107.          Poff = m_dPCL_dBm[band_index][0] - M * log10(1.0*(EffPCLDac_Cal[band_index][0]+i_apc_dc_offset[0] - DACth));
  1108.         int pcl_index = cfg->getTargetPclIndex(as_tx_pcl, target_pcl);
  1109.         if (! getAnsiStrSubItem( as_wanted_p, pcl_index, DEFAULT_SEP_CHAR, as_sub_wanted_p) )
  1110.         {
  1111.                 CalErrorHandler( WM_MF_CFG_READ_FAIL );
  1112.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1113.                               " FAIL: Read config file fail(sub_wanted_p)."
  1114.                             );
  1115.                 return false;
  1116.         }
  1117.         int iRealRecursiveTimes = 0;
  1118.         do
  1119.         {
  1120.             // 4. Calculate DACwant
  1121.             DACwant = DACth + pow(10.0, (atof(as_sub_wanted_p.c_str())-Poff)/M);
  1122.             MF_rf_apc_ptr->Set_ApcProfilePower(band_index, pcl_index-1, DACwant-m_iApcDcOffset );
  1123.             MF_rf_apc_ptr->ConfirmCallback = ccb_set_apc_level;
  1124.             MF_rf_apc_ptr->Req_Rf_SetRampApcLevel(eFreqBand, target_pcl, MF_rf_apc_ptr->Get_ApcProfilePower(band_index, pcl_index-1) );
  1125.             if ( this->Terminated )
  1126.             {
  1127.                 this->OnTerminate = neByUser;
  1128.                 return false;
  1129.             }
  1130.             RestartTimerCal( WM_MF_SET_APC_DAC_FAIL );
  1131.            // log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1132.            //                       " TimerCal->Enabled 9= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  1133.            //                     );
  1134.             is_suspend_cal = true;
  1135.             this->Suspend();
  1136.             is_suspend_cal = false;
  1137.             E_METAAPP_RESULT_T state = MF_rf_apc_ptr->Get_ConfirmState();
  1138.             if (state != METAAPP_SUCCESS)
  1139.             {
  1140.                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  1141.                 {
  1142.                     CalErrorHandler(WM_MF_NVRAM_EF_L1_RAMPTABLE_xxx_LID_VERNO_FAIL);
  1143.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1144.                              " FAIL : NVRAM_EF_L1_RAMPTABLE_xxx_LID version is not support, please update META to latest version. "
  1145.                              );
  1146.                 }
  1147.                 else
  1148.                 {
  1149.                     CalErrorHandler(WM_MF_SET_APC_DAC_FAIL);
  1150.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1151.                                       " FAIL: Target < set APC level fail: PCL = "+ IntToStr(FIRST_PCL) +
  1152.                                      " APC DAC = " +  IntToStr( MF_rf_apc_ptr->Get_ApcProfilePower(band_index, 0) )  + "n"
  1153.                                     );
  1154.                 }
  1155.                 return false;
  1156.             }
  1157.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1158.                           " Target < set APC level ok: PCL = "+ IntToStr(FIRST_PCL) + "," +
  1159.                          " APC DAC = " +  IntToStr( MF_rf_apc_ptr->Get_ApcProfilePower(band_index, 0) )  + "n"
  1160.                          );
  1161.             // 5. Get Pwantmeas from equipment
  1162.             if( ! m_rct_ctrl.RCT_MSTXLevel(m_pRct, target_pcl))
  1163.             {
  1164.                 CalErrorHandler( WM_MF_AGE8960_SET_MS_TX_LEVEL_FAIL );
  1165.                 if ( frmFatcory->rbAgilent8960->Checked )
  1166.                 {
  1167.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1168.                                   " FAIL : Agilent 8960< MS TX level = " + IntToStr( target_pcl )
  1169.                                 );
  1170.                 }
  1171.                 else
  1172.                 {
  1173.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1174.                                   " FAIL : cmu200 > MS TX level = " + IntToStr( target_pcl )
  1175.                                 );
  1176.                 }
  1177.                 return false;
  1178.             }
  1179.             if ( this->Terminated )
  1180.             {
  1181.                     this->OnTerminate = neByUser;
  1182.                     return false;
  1183.             }
  1184.             if ( frmFatcory->rbAgilent8960->Checked )
  1185.             {
  1186.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1187.                               " Agilent 8960< MS TX level = " + IntToStr( target_pcl )
  1188.                             );
  1189.             }
  1190.             else
  1191.             {
  1192.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1193.                               " CMU200 < MS TX level = " + IntToStr( target_pcl )
  1194.                             );
  1195.             }
  1196.             RestartTimerCal(WM_MF_RF_TX_LEVEL_FAIL);
  1197.             if (!MF_rf_stop.REQ_Start())
  1198.             {
  1199.                 CalErrorHandler( WM_MF_RF_STOP_FAIL );
  1200.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1201.                           " FAIL: Target < stop RF fail."
  1202.                         );
  1203.                 return false;
  1204.             }
  1205.             MF_rf_tx_level_req->arfcn = s_ARFCN;
  1206.             MF_rf_tx_level_req->bsic  =  m_cTSC;
  1207.             MF_rf_tx_level_req->power    = target_pcl;
  1208.             MF_rf_tx_level_req->frames   = i_N_TX;
  1209.             MF_rf_tx_level_req->dacValue = m_sDefault_value;
  1210.             MF_rf_tx_level_req->burstTypeNB = NB_TX_RANDOM_WITH_TSC;
  1211.             if ( META_Rf_NB_TX_r(    m_pCal->i_MainMETAHandle,
  1212.                      MF_rf_tx_level_req,
  1213.                      MF_rf_tx_level_cb,
  1214.                      &MF_rf_tx_level_token,
  1215.                                      NULL
  1216.                                   ) != META_SUCCESS)
  1217.             {
  1218.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1219.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1220.                               " FAIL : Target < ARFCN = " + IntToStr( s_ARFCN ) +
  1221.                               ", TSC = " + IntToStr( m_cTSC ) +
  1222.                               ", power = " + IntToStr( target_pcl ) +
  1223.                               ", frames = " + IntToStr(i_N_TX) +
  1224.                               ", dac value = " + IntToStr( m_sDefault_value )
  1225.                             );
  1226.                     return false;
  1227.             }
  1228.             if ( this->Terminated )
  1229.             {
  1230.                     this->OnTerminate = neByUser;
  1231.                     return false;
  1232.             }
  1233.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1234.                          " Target < TX: ARFCN = " + IntToStr( s_ARFCN ) +
  1235.                          ", TSC = " + IntToStr( m_cTSC ) +
  1236.                          ", power = " + IntToStr( target_pcl ) +
  1237.                          ", frames = " + IntToStr(i_N_TX) +
  1238.                          ", dac value = " + IntToStr( m_sDefault_value )
  1239.                     );
  1240.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1241.                           " TimerCal->Enabled 7= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  1242.                         );
  1243.             RestartTimerCal( WM_MF_RF_TX_LEVEL_FAIL );
  1244.             is_suspend_cal = true;
  1245.             this->Suspend();
  1246.             is_suspend_cal = false;
  1247.             if (! MF_rf_tx_level_cnf )
  1248.             {
  1249.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1250.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1251.                               " FAIL : ! MF_rf_tx_level_cnf"
  1252.                             );
  1253.                     return false;
  1254.             }
  1255.             if ( this->Terminated )
  1256.             {
  1257.                     this->OnTerminate = neByUser;
  1258.                     return false;
  1259.             }
  1260.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1261.                           " TimerCal->Enabled 8= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  1262.                         );
  1263.             RestartTimerCal( WM_MF_AGE8960_CONFIG_TX_POWER_FAIL );
  1264.             if (!m_rct_ctrl.RCT_Config_ExpectedTxPower(m_pRct, true, 0))
  1265.             {
  1266.                 RestartTimerCal(WM_MF_AGE8960_CONFIG_TX_POWER_FAIL);
  1267.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1268.                                   " FAIL : " + m_pRct->as_RCT + " < config expected TX power to auto fail"
  1269.                                 );
  1270.                 return false;
  1271.             }
  1272.             if( ! m_rct_ctrl.RCT_confTXPower( m_pRct ) )
  1273.             {
  1274.                     CalErrorHandler( WM_MF_AGE8960_CONFIG_TX_POWER_FAIL );
  1275.                     if ( frmFatcory->rbAgilent8960->Checked )
  1276.                     {
  1277.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1278.                                   " FAIL : Agilent 8960< config TX power 1."
  1279.                                 );
  1280.                     }
  1281.                     else
  1282.                     {
  1283.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1284.                                   " FAIL : CMU 200< config TX power 1."
  1285.                                 );
  1286.                     }
  1287.                     return false;
  1288.             }
  1289.             RestartTimerCal( WM_MF_AGE8960_READ_TX_POWER_FAIL );
  1290.             if( ! m_rct_ctrl.RCT_ReadTxPower( m_pRct, m_virTX ) )
  1291.             {
  1292.                     CalErrorHandler( WM_MF_AGE8960_READ_TX_POWER_FAIL );
  1293.                     if ( frmFatcory->rbAgilent8960->Checked )
  1294.                     {
  1295.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1296.                                   " FAIL : Agilent 8960< read TX power 1."
  1297.                                 );
  1298.                     }
  1299.                     else
  1300.                     {
  1301.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1302.                                   " FAIL : CMU 200< read TX power 1."
  1303.                                 );
  1304.                     }
  1305.                     return false;
  1306.             }
  1307.             if ( frmFatcory->rbAgilent8960->Checked )
  1308.             {
  1309.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1310.                               " Agilent 8960< read TX power = "+ Double_To_AnsiString( m_virTX )
  1311.                             );
  1312.             }
  1313.             else
  1314.             {
  1315.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1316.                               " CMU 200< read TX power = "+ Double_To_AnsiString( m_virTX )
  1317.                             );
  1318.             }
  1319.             int iRealRecursiveTimes_DAC = 0;
  1320.             do
  1321.             {
  1322.                 // 6. Calculate DACthnew
  1323.                 DACthnew = DACwant - pow(10.0, (m_virTX-Poff)/M);
  1324.                 // 7. Calculate Mnew
  1325.                // Mnew = ( m_dPCL_dBm[band_index][1]-m_dPCL_dBm[band_index][0]) / ( log10(EffPCLDac_Cal[band_index][1]+MF_rf_apc_ptr->ApcDcOffset[band_index]-DACthnew) - log10(EffPCLDac_Cal[band_index][0]+MF_rf_apc_ptr->ApcDcOffset[band_index]-DACthnew) );
  1326.               //  Mnew = ( m_dPCL_dBm[band_index][1]-m_dPCL_dBm[band_index][0]) / ( log10(EffPCLDac_Cal[band_index][1]+MF_rf_apc_ptr->Get_HighApcDcOffset(band_index)-DACthnew) - log10(EffPCLDac_Cal[band_index][0]+MF_rf_apc_ptr->Get_HighApcDcOffset(band_index)-DACthnew) );
  1327.                 Mnew = ( m_dPCL_dBm[band_index][1]-m_dPCL_dBm[band_index][0]) / ( log10(EffPCLDac_Cal[band_index][1]+i_apc_dc_offset[1]-DACthnew) - log10(EffPCLDac_Cal[band_index][0]+i_apc_dc_offset[0]-DACthnew) );
  1328.                // Poffnew = m_dPCL_dBm[band_index][0] - Mnew * log10(EffPCLDac_Cal[band_index][0]+MF_rf_apc_ptr->ApcDcOffset[band_index] - DACthnew);
  1329.                // Poffnew = m_dPCL_dBm[band_index][0] - Mnew * log10(EffPCLDac_Cal[band_index][0]+MF_rf_apc_ptr->Get_HighApcDcOffset(band_index) - DACthnew);
  1330.                 Poffnew = m_dPCL_dBm[band_index][0] - Mnew * log10(EffPCLDac_Cal[band_index][0]+i_apc_dc_offset[0] - DACthnew);
  1331.                 // 8. Calculate DACdelta
  1332.                 DACd     = abs(DACthnew - DACth);
  1333.                 DACth    = DACthnew;
  1334.                 Poff     = Poffnew;
  1335.                 M        = Mnew;
  1336.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1337.                               " DAC delta = " + IntToStr( DACd ) +
  1338.                               ", DACth = " + IntToStr( DACth ) +
  1339.                               ", Poff = " + Double_To_AnsiString( Poff ) +
  1340.                               ", M = " + Double_To_AnsiString( M )
  1341.                             );
  1342.                 iRealRecursiveTimes_DAC++;
  1343.             }
  1344.             while( (DACd > i_SKY77328_DAC_Delta) && (iRealRecursiveTimes_DAC < i_TXlevelRecusiveTimes) );
  1345.             iRealRecursiveTimes++;
  1346.         }
  1347.         while( ( m_virTX > (atof(as_sub_wanted_p.c_str())+ m_dAPC_Delta)  ||
  1348.                  m_virTX < (atof(as_sub_wanted_p.c_str())- m_dAPC_Delta)) &&
  1349.                  (iRealRecursiveTimes < i_TXlevelRecusiveTimes) );
  1350.         // Evaluate DAC except highest and lowest PCL
  1351.         for(int i=0; i<RF_RAMP_TABLE_TX_LEVEL_COUNT; i++)
  1352.         {
  1353.             if( i==RF_RAMP_TABLE_TX_LEVEL_COUNT-1 &&
  1354.                 (eFreqBand==FrequencyBand850 || eFreqBand==FrequencyBand900 )
  1355.               )
  1356.             {
  1357.                 break;
  1358.             }
  1359.             if (! getAnsiStrSubItem( as_tx_pcl, i+1, DEFAULT_SEP_CHAR, as_sub_tx_pcl) )
  1360.             {
  1361.                 CalErrorHandler( WM_MF_CFG_READ_FAIL );
  1362.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1363.                               " FAIL: Read config file fail(sub_tx_pcl)."
  1364.                         );
  1365.                 return false;
  1366.             }
  1367.             if (! getAnsiStrSubItem( as_wanted_p, i+1, DEFAULT_SEP_CHAR, as_sub_wanted_p) )
  1368.             {
  1369.                 CalErrorHandler( WM_MF_CFG_READ_FAIL );
  1370.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1371.                               " FAIL: Read config file fail(sub_wanted_p)."
  1372.                             );
  1373.                 return false;
  1374.             }
  1375.             if (! getAnsiStrSubItem( as_correction, i+1, DEFAULT_SEP_CHAR, as_sub_correction) )
  1376.             {
  1377.                 CalErrorHandler( WM_MF_CFG_READ_FAIL );
  1378.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1379.                               " FAIL: Read config file fail(as_sub_correction)."
  1380.                             );
  1381.                 return false;
  1382.             }
  1383.             if( as_sub_tx_pcl.ToInt() != i_PCL_Cal[band_index][PCL_LOW_INDEX] &&
  1384.                 as_sub_tx_pcl.ToInt() != i_PCL_Cal[band_index][PCL_MID_INDEX]
  1385.               )
  1386.             {
  1387.                // EffPclDac_Eval[band_index][i] = DACth + pow(10.0, (atof(as_sub_wanted_p.c_str()) + atof(as_sub_correction.c_str())- Poff)/M) - MF_rf_apc_ptr->ApcDcOffset[band_index];
  1388.                // EffPclDac_Eval[band_index][i] = DACth + pow(10.0, (atof(as_sub_wanted_p.c_str()) + atof(as_sub_correction.c_str())- Poff)/M) - MF_rf_apc_ptr->Get_HighApcDcOffset(band_index);
  1389.                 if( ! MF_rf_apc_ptr->Get_RealApcDcOffset( m_pCal->b_TADOSupport, band_index, as_sub_tx_pcl.ToInt(), m_iApcDcOffset ) )
  1390.                 {
  1391.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1392.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1393.                               " FAIL: PCL to dBm " +
  1394.                               " TADO support = " + IntToStr( m_pCal->b_TADOSupport ) +
  1395.                               " eFreqBand = " + IntToStr( eFreqBand ) +
  1396.                               " pcl = " + as_sub_tx_pcl
  1397.                                );
  1398.                     return false;
  1399.                 }
  1400.                 EffPclDac_Eval[band_index][i] = DACth + pow(10.0, (atof(as_sub_wanted_p.c_str()) + atof(as_sub_correction.c_str())- Poff)/M) - m_iApcDcOffset;
  1401.                 if( d_weight*d_battery_compensate*ramp_value == 0 )
  1402.                 {
  1403.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_EVALDAC_FAIL );
  1404.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1405.                               " FAIL: weight*battery_compensate*ramp_value = 0."
  1406.                         );
  1407.                     return false;
  1408.                 }
  1409.                 m_usPCLDacValue_ideal[band_index][i] = EffPclDac_Eval[band_index][i]*MAX_RAMP_VALUE/(d_weight*d_battery_compensate*ramp_value);
  1410.                // MF_rf_apc_ptr->ApcProfile[band_index].rampData.power[i] = m_usPCLDacValue_ideal[band_index][i];
  1411.                 MF_rf_apc_ptr->Set_ApcProfilePower(band_index, i, m_usPCLDacValue_ideal[band_index][i]);
  1412.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1413.                           // "                         " +
  1414.                            " m_usPCLDacValue_ideal[" + IntToStr(band_index) + "][" + IntToStr(i) + "] = " +
  1415.                              Double_To_AnsiString(m_usPCLDacValue_ideal[band_index][i]) + "n"
  1416.                         );
  1417.             }
  1418.         }
  1419.         MF_rf_apc_ptr->ConfirmCallback = ::ccb_write_apc_to_nvram;
  1420.         MF_rf_apc_ptr->REQ_Write_APC_To_NVRAM_Single_Band_Start(m_pCal->ui_rf_id, band_index, m_pCal->b_TADOSupport );
  1421.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1422.                       " Write to NVRAM band_index = " + IntToStr(band_index)
  1423.                     );
  1424. #ifdef _MYDEBUG_
  1425.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1426.                      " TimerCal->Enabled 15= " + IntToStr((int)Form_META_Factory->TimerCal->Enabled) + "n"
  1427.                    );
  1428. #endif
  1429.         RestartTimerCal(WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL);
  1430.         CHECK_TERMINATE_BY_USER
  1431.         SUSPEND_CAL_THREAD
  1432.         WriteAPCCalResultToFile(as_ID+".cal",Application->ExeName, m_pCal->b_CalResultPath, FreqBand_To_BandIdx(eFreqBand) );
  1433.         eFreqBand++;
  1434.     } // while
  1435.     RestartTimerCal(WM_MF_RF_STOP_FAIL);
  1436.     if (!MF_rf_stop.REQ_Start())
  1437.     {
  1438.         CalErrorHandler(WM_MF_RF_STOP_FAIL);
  1439.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1440.                           " FAIL: Target < stop RF fail."
  1441.                         );
  1442.         return false;
  1443.     }
  1444.     
  1445.     frmFatcory->DisableAllCalTimer();
  1446.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1447.                  " ===================== Evaluate other PCL DAC value end =========== n "
  1448.                 );
  1449.     return true;
  1450.    
  1451. }