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

MTK

开发平台:

C++ Builder

  1.             MF_crystal_afc_ptr->REQ_Write_CAPID_To_NVRAM_Start();
  2.             is_suspend_cal = true;
  3.             this->Suspend();
  4.             is_suspend_cal = false;
  5.             E_METAAPP_RESULT_T state = MF_crystal_afc_ptr->Get_ConfirmState();
  6.             if (state != METAAPP_SUCCESS)
  7.             {
  8.                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  9.                 {
  10.                     CalErrorHandler(WM_MF_NVRAM_EF_L1_CRYSTAL_CAPDATA_LID_VERNO_FAIL);
  11.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  12.                               " FAIL : NVRAM_EF_L1_CRYSTAL_CAPDATA_LID version is not support, please update META to latest version. "
  13.                              );
  14.                 }
  15.                 else
  16.                 {
  17.                     CalErrorHandler(WM_MF_NVRAM_CRYSTAL_AFC_CAPID_WRITE_FAIL);
  18.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  19.                              " FAIL : Target< Write crystal AFC CAP ID to NVRAM fail "
  20.                              );
  21.                 }
  22.                 return false;
  23.             }
  24.         }
  25.         else
  26.         {
  27.             MetaResult = META_Rf_SetCrystalCapId_r( m_pCal->i_MainMETAHandle,  5000, &CrystalCfg_req );
  28.             if( MetaResult != META_SUCCESS )
  29.             {
  30.                 CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_SET_CAPID_FAIL );
  31.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  32.                           " FAIL : Target< Set crystal AFC CAP ID fail "
  33.                         );
  34.                 return false;
  35.             }
  36.         }
  37.         if ( this->Terminated )
  38.         {
  39.             this->OnTerminate = neByUser;
  40.             return false;
  41.         }
  42.         Sleep(50);
  43.         if (!MF_rf_stop.REQ_Start())
  44.         {
  45.             CalErrorHandler( WM_MF_RF_STOP_FAIL );
  46.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  47.                           " FAIL: Target < stop RF fail."
  48.                         );
  49.             return false;
  50.         }
  51.        // Sleep(50);
  52.         int i_CRYSTAL_AFC_PCL = Get_CRYSTAL_AFC_PCL( as_CRYSTAL_AFC_BAND );
  53.         MF_rf_tx_level_req->arfcn = m_sCRYSTAL_AFC_ARFCN;
  54.         MF_rf_tx_level_req->bsic  =  m_cTSC;
  55.         MF_rf_tx_level_req->power    = i_CRYSTAL_AFC_PCL;
  56.         MF_rf_tx_level_req->frames   = 2000;
  57.         MF_rf_tx_level_req->dacValue = m_sCRYSTAL_AFC_CAL_DAC;
  58.         MF_rf_tx_level_req->burstTypeNB = NB_TX_RANDOM_WITH_TSC; // NB burst
  59.         if ( META_Rf_NB_TX_r(  m_pCal->i_MainMETAHandle,
  60.                     MF_rf_tx_level_req,
  61.                     cb_MF_rf_tx_level_crystal,
  62.                     &MF_rf_tx_level_token,
  63. NULL
  64.                            ) != META_SUCCESS)
  65.         {
  66.             CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  67.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  68.                       " FAIL : Target< ARFCN = " + IntToStr(m_sCRYSTAL_AFC_ARFCN) +
  69.                       ", TSC = " + IntToStr( m_cTSC ) +
  70.                       ", power = " + i_CRYSTAL_AFC_PCL +
  71.                       ", frames = " + IntToStr(i_N_TX) +
  72.                       ", dac value = " + IntToStr( m_sDefault_value )
  73.                     );
  74.             return false;
  75.         }
  76.         is_suspend_cal = true;
  77.         this->Suspend();
  78.         is_suspend_cal = false;
  79.         if (! MF_rf_tx_level_cnf )
  80.         {
  81.             CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_TX_LEVEL_FAIL );
  82.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  83.                       " FAIL: TX level (! MF_rf_tx_level_cnf ) ."
  84.                     );
  85.             return false;
  86.         }
  87.         //*******************************************************
  88.         if( ! FetchAverageFreqErr( cal_CapIdFreqErr.FreqErr ) )
  89.         {
  90.             return false;
  91.         }
  92.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  93.                     " CAP ID = " + IntToStr( cal_CapIdFreqErr.cap_id ) + "," +
  94.                     " AFC DAC = " + IntToStr( m_sCRYSTAL_AFC_CAL_DAC ) + "," +
  95.                     " Freq error = " + Double_To_AnsiString(cal_CapIdFreqErr.FreqErr)
  96.                 );
  97.       //***************************************************************
  98.         if( Abs_double(cal_CapIdFreqErr.FreqErr) < min_CapIdFreqErr.FreqErr )
  99.         {
  100.             min_CapIdFreqErr.cap_id = cal_CapIdFreqErr.cap_id;
  101.             min_CapIdFreqErr.FreqErr = Abs_double(cal_CapIdFreqErr.FreqErr);
  102.         }
  103.         if( cal_CapIdFreqErr.FreqErr < 0 )
  104.         {
  105.             //isAnyNegativeFreqErr = true;
  106.             last_cap_id = cal_CapIdFreqErr.cap_id;
  107.         }
  108.         else if( cal_CapIdFreqErr.FreqErr >0 )
  109.         {
  110.             //isAnyPositiveFreqErr = true;
  111.             first_cap_id = cal_CapIdFreqErr.cap_id + 1;
  112.         }
  113.         if( first_cap_id >= last_cap_id || pre_cap_id == cal_CapIdFreqErr.cap_id)  break;
  114.         pre_cap_id = cal_CapIdFreqErr.cap_id;
  115.         cal_CapIdFreqErr.cap_id = (first_cap_id+last_cap_id) /2;
  116.     }
  117.     if (min_CapIdFreqErr.cap_id < CAP_ID_MIN)
  118.     {
  119.         min_CapIdFreqErr.cap_id = CAP_ID_MIN;
  120.     }
  121.     else if (min_CapIdFreqErr.cap_id > cap_id_max)
  122.     {
  123.         min_CapIdFreqErr.cap_id = cap_id_max;
  124.     }
  125.     MF_crystal_afc_ptr->Set_cap_id(min_CapIdFreqErr.cap_id);
  126.     CrystalCfg_req.cap_id = min_CapIdFreqErr.cap_id;
  127.     if (WRITE_NVARM == m_pCal->s_crystal_afc_option.crystal_cap_id_update)
  128.     {
  129.         MF_crystal_afc_ptr->ConfirmCallback = ::ccb_write_CrystalAFC_to_nvram;
  130.         MF_crystal_afc_ptr->REQ_Write_CAPID_To_NVRAM_Start();
  131.         is_suspend_cal = true;
  132.         this->Suspend();
  133.         is_suspend_cal = false;
  134.         E_METAAPP_RESULT_T state = MF_crystal_afc_ptr->Get_ConfirmState();
  135.         if (state != METAAPP_SUCCESS)
  136.         {
  137.             if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  138.             {
  139.                 CalErrorHandler(WM_MF_NVRAM_EF_L1_CRYSTAL_CAPDATA_LID_VERNO_FAIL);
  140.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  141.                          " FAIL : NVRAM_EF_L1_CRYSTAL_CAPDATA_LID version is not support, please update META to latest version. "
  142.                          );
  143.             }
  144.             else
  145.             {
  146.                 CalErrorHandler(WM_MF_NVRAM_CRYSTAL_AFC_CAPID_WRITE_FAIL);
  147.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  148.                              " FAIL : Target< Write crystal AFC CAP ID to NVRAM fail "
  149.                              );
  150.             }
  151.             return false;
  152.         }
  153.     }
  154.     else
  155.     {
  156.             MetaResult = META_Rf_SetCrystalCapId_r( m_pCal->i_MainMETAHandle, 5000, &CrystalCfg_req );
  157.             if( MetaResult != META_SUCCESS )
  158.             {
  159.                 CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_SET_CAPID_FAIL );
  160.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  161.                           " FAIL : Target< Set crystal AFC CAP ID fail "
  162.                         );
  163.                 return false;
  164.             }
  165.     }
  166.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  167.               " ================= Crystal CAPID calibration end ================== "
  168.            );
  169.      Sleep(50);
  170.      if (!MF_rf_stop.REQ_Start())
  171.      {
  172.         CalErrorHandler( WM_MF_RF_STOP_FAIL );
  173.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  174.                           " FAIL: Target < stop RF fail."
  175.                         );
  176.         return false;
  177.      }
  178.      return true;
  179. }
  180. //---------------------------------------------------------------------------
  181. bool __fastcall T_META_factory_calibration::CrystalAFCCapIDCalInc( void )
  182. {
  183.     int init_cap_id = MF_crystal_afc_ptr->Get_cap_id();
  184.     min_CapIdFreqErr.cap_id = init_cap_id;
  185.     min_CapIdFreqErr.FreqErr = 1000000000.0;
  186.     cal_CapIdFreqErr.cap_id = min_CapIdFreqErr.cap_id;
  187.     int pre_cap_id = -1;
  188.     int cap_id_max;
  189.     int first_cap_id = CAP_ID_MIN;
  190.     int last_cap_id = MF_crystal_afc_ptr->Get_max_cap_id(m_pCal->ui_rf_id);
  191.     cap_id_max  = last_cap_id;
  192.     int count=0;
  193.     while( min_CapIdFreqErr.FreqErr != 0.0       &&
  194.            first_cap_id < last_cap_id            &&
  195.            CAP_ID_MIN <= cal_CapIdFreqErr.cap_id &&
  196.            cal_CapIdFreqErr.cap_id <= cap_id_max &&
  197.            count < m_sCRYSTAL_AFC_RECURSIVE_TIMES
  198.          )
  199.     {
  200.         count++;
  201.         MF_crystal_afc_ptr->Set_cap_id(cal_CapIdFreqErr.cap_id);
  202.         CrystalCfg_req.cap_id = cal_CapIdFreqErr.cap_id;
  203.         if (WRITE_NVARM == m_pCal->s_crystal_afc_option.crystal_cap_id_update)
  204.         {
  205.             MF_crystal_afc_ptr->ConfirmCallback = ::ccb_write_CrystalAFC_to_nvram;
  206.             MF_crystal_afc_ptr->REQ_Write_CAPID_To_NVRAM_Start();
  207.             is_suspend_cal = true;
  208.             this->Suspend();
  209.             is_suspend_cal = false;
  210.             E_METAAPP_RESULT_T state = MF_crystal_afc_ptr->Get_ConfirmState();
  211.             if (state != METAAPP_SUCCESS)
  212.             {
  213.                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  214.                 {
  215.                     CalErrorHandler(WM_MF_NVRAM_EF_L1_CRYSTAL_CAPDATA_LID_VERNO_FAIL);
  216.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  217.                          " FAIL : NVRAM_EF_L1_CRYSTAL_CAPDATA_LID version is not support, please update META to latest version. "
  218.                          );
  219.                 }
  220.                 else
  221.                 {
  222.                     CalErrorHandler(WM_MF_NVRAM_CRYSTAL_AFC_CAPID_WRITE_FAIL);
  223.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  224.                              " FAIL : Target< Write crystal AFC CAP ID to NVRAM fail "
  225.                              );
  226.                 }
  227.                 return false;
  228.             }
  229.         }
  230.         else
  231.         {
  232.             MetaResult = META_Rf_SetCrystalCapId_r( m_pCal->i_MainMETAHandle, 5000, &CrystalCfg_req );
  233.             if( MetaResult != META_SUCCESS )
  234.             {
  235.                 CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_SET_CAPID_FAIL );
  236.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  237.                           " FAIL : Target< Set crystal AFC CAP ID fail "
  238.                         );
  239.                 return false;
  240.             }
  241.         }
  242.         if ( this->Terminated )
  243.         {
  244.             this->OnTerminate = neByUser;
  245.             return false;
  246.         }
  247.         Sleep(50);
  248.         if (!MF_rf_stop.REQ_Start())
  249.         {
  250.             CalErrorHandler( WM_MF_RF_STOP_FAIL );
  251.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  252.                           " FAIL: Target < stop RF fail."
  253.                         );
  254.             return false;
  255.         }
  256.        // Sleep(50);
  257.         int i_CRYSTAL_AFC_PCL = Get_CRYSTAL_AFC_PCL( as_CRYSTAL_AFC_BAND );
  258.         MF_rf_tx_level_req->arfcn = m_sCRYSTAL_AFC_ARFCN;
  259.         MF_rf_tx_level_req->bsic  =  m_cTSC;
  260.         MF_rf_tx_level_req->power    = i_CRYSTAL_AFC_PCL;
  261.         MF_rf_tx_level_req->frames   = 2000;
  262.         MF_rf_tx_level_req->dacValue = m_sCRYSTAL_AFC_CAL_DAC;
  263.         MF_rf_tx_level_req->burstTypeNB = NB_TX_RANDOM_WITH_TSC; // NB burst
  264.         if ( META_Rf_NB_TX_r(  m_pCal->i_MainMETAHandle,
  265.                     MF_rf_tx_level_req,
  266.                     cb_MF_rf_tx_level_crystal,
  267.                     &MF_rf_tx_level_token,
  268. NULL
  269.                            ) != META_SUCCESS)
  270.         {
  271.             CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  272.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  273.                       " FAIL : Target< ARFCN = " + IntToStr(m_sCRYSTAL_AFC_ARFCN) +
  274.                       ", TSC = " + IntToStr( m_cTSC ) +
  275.                       ", power = " + i_CRYSTAL_AFC_PCL +
  276.                       ", frames = " + IntToStr(i_N_TX) +
  277.                       ", dac value = " + IntToStr( m_sDefault_value )
  278.                     );
  279.             return false;
  280.         }
  281.         is_suspend_cal = true;
  282.         this->Suspend();
  283.         is_suspend_cal = false;
  284.         if (! MF_rf_tx_level_cnf )
  285.         {
  286.             CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_TX_LEVEL_FAIL );
  287.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  288.                       " FAIL: TX level (! MF_rf_tx_level_cnf ) ."
  289.                     );
  290.             return false;
  291.         }
  292.         //*******************************************************
  293.         if( ! FetchAverageFreqErr( cal_CapIdFreqErr.FreqErr ) )
  294.         {
  295.             return false;
  296.         }
  297.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  298.                     " CAP ID = " + IntToStr( cal_CapIdFreqErr.cap_id ) + "," +
  299.                     " AFC DAC = " + IntToStr(m_sCRYSTAL_AFC_CAL_DAC) + "," +
  300.                     " Freq error = " + Double_To_AnsiString(cal_CapIdFreqErr.FreqErr)
  301.                 );
  302.       //***************************************************************
  303.         if( Abs_double(cal_CapIdFreqErr.FreqErr) < min_CapIdFreqErr.FreqErr )
  304.         {
  305.             min_CapIdFreqErr.cap_id = cal_CapIdFreqErr.cap_id;
  306.             min_CapIdFreqErr.FreqErr = Abs_double(cal_CapIdFreqErr.FreqErr);
  307.         }
  308.         if( cal_CapIdFreqErr.FreqErr > 0 )
  309.         {
  310.             //isAnyNegativeFreqErr = true;
  311.             last_cap_id = cal_CapIdFreqErr.cap_id;
  312.         }
  313.         else if( cal_CapIdFreqErr.FreqErr < 0 )
  314.         {
  315.             //isAnyPositiveFreqErr = true;
  316.             first_cap_id = cal_CapIdFreqErr.cap_id + 1;
  317.         }
  318.         if( first_cap_id >= last_cap_id || pre_cap_id == cal_CapIdFreqErr.cap_id)  break;
  319.         pre_cap_id = cal_CapIdFreqErr.cap_id;
  320.         cal_CapIdFreqErr.cap_id = (first_cap_id+last_cap_id) /2;
  321.     }
  322.     if (min_CapIdFreqErr.cap_id < CAP_ID_MIN)
  323.     {
  324.         min_CapIdFreqErr.cap_id = CAP_ID_MIN;
  325.     }
  326.     else if (min_CapIdFreqErr.cap_id > cap_id_max)
  327.     {
  328.         min_CapIdFreqErr.cap_id = cap_id_max;
  329.     }
  330.     MF_crystal_afc_ptr->Set_cap_id(min_CapIdFreqErr.cap_id);
  331.     CrystalCfg_req.cap_id = min_CapIdFreqErr.cap_id;
  332.     if (WRITE_NVARM == m_pCal->s_crystal_afc_option.crystal_cap_id_update)
  333.     {
  334.         MF_crystal_afc_ptr->ConfirmCallback = ::ccb_write_CrystalAFC_to_nvram;
  335.         MF_crystal_afc_ptr->REQ_Write_CAPID_To_NVRAM_Start();
  336.         is_suspend_cal = true;
  337.         this->Suspend();
  338.         is_suspend_cal = false;
  339.         E_METAAPP_RESULT_T state = MF_crystal_afc_ptr->Get_ConfirmState();
  340.         if (state != METAAPP_SUCCESS)
  341.         {
  342.             if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  343.             {
  344.                 CalErrorHandler(WM_MF_NVRAM_EF_L1_CRYSTAL_CAPDATA_LID_VERNO_FAIL);
  345.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  346.                          " FAIL : NVRAM_EF_L1_CRYSTAL_CAPDATA_LID version is not support, please update META to latest version. "
  347.                          );
  348.             }
  349.             else
  350.             {
  351.                 CalErrorHandler(WM_MF_NVRAM_CRYSTAL_AFC_CAPID_WRITE_FAIL);
  352.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  353.                              " FAIL : Target< Write crystal AFC CAP ID to NVRAM fail "
  354.                              );
  355.             }
  356.             return false;
  357.         }
  358.     }
  359.     else
  360.     {
  361.             MetaResult = META_Rf_SetCrystalCapId_r( m_pCal->i_MainMETAHandle, 5000, &CrystalCfg_req );
  362.             if( MetaResult != META_SUCCESS )
  363.             {
  364.                 CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_SET_CAPID_FAIL );
  365.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  366.                           " FAIL : Target< Set crystal AFC CAP ID fail "
  367.                         );
  368.                 return false;
  369.             }
  370.     }
  371.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  372.               " ================= Crystal CAPID calibration end ================== "
  373.            );
  374.     Sleep(50);
  375.     if (!MF_rf_stop.REQ_Start())
  376.     {
  377.         CalErrorHandler( WM_MF_RF_STOP_FAIL );
  378.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  379.                           " FAIL: Target < stop RF fail."
  380.                         );
  381.         return false;
  382.     }
  383.     return true;
  384. }
  385. //----------------------------------------------------------------------------
  386. bool __fastcall T_META_factory_calibration::CrystalAFCCapIDCheck_BypassCapIDCal( void )
  387. {
  388.     char tempbuf[256];
  389.     bool isCrystalAfcCheckFinish[2];
  390.     ////////////////////////// check crystal AFC result ///////////////////////
  391.     log->Add( "n" +DateToStr(Date()) +  " " + CurrentTimeStr() +
  392.               " ================== Crystal CAPID check begin ===================== "
  393.            );
  394.     int i_CRYSTAL_AFC_PCL = Get_CRYSTAL_AFC_PCL( as_CRYSTAL_AFC_BAND );
  395.     for(int i=0; i<2; i++ )
  396.     {
  397.         Sleep(50);
  398.         if (!MF_rf_stop.REQ_Start())
  399.         {
  400.             CalErrorHandler( WM_MF_RF_STOP_FAIL );
  401.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  402.                           " FAIL: Target < stop RF fail."
  403.                         );
  404.             return false;
  405.         }
  406.         MF_rf_tx_level_req->arfcn = m_sCRYSTAL_AFC_ARFCN;
  407.         MF_rf_tx_level_req->bsic  =  m_cTSC;
  408.         MF_rf_tx_level_req->power    = i_CRYSTAL_AFC_PCL;
  409.         MF_rf_tx_level_req->frames   = 2000;
  410.         MF_rf_tx_level_req->dacValue = m_sCHECK_DAC[i];
  411.         MF_rf_tx_level_req->burstTypeNB = NB_TX_RANDOM_WITH_TSC;
  412.         if ( META_Rf_NB_TX_r(  m_pCal->i_MainMETAHandle,
  413.                     MF_rf_tx_level_req,
  414.                     cb_MF_rf_tx_level_crystal,
  415.                     &MF_rf_tx_level_token,
  416. NULL
  417.                           ) != META_SUCCESS)
  418.         {
  419.                 CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  420.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  421.                           " FAIL : Target< ARFCN = " + IntToStr(m_sCRYSTAL_AFC_ARFCN) +
  422.                           ", TSC = " + IntToStr( m_cTSC ) +
  423.                           ", power = " + i_CRYSTAL_AFC_PCL +
  424.                           ", frames = " + IntToStr(i_N_TX) +
  425.                           ", dac value = " + IntToStr( m_sDefault_value )
  426.                         );
  427.                 return false;
  428.         }
  429.         is_suspend_cal = true;
  430.         this->Suspend();
  431.         is_suspend_cal = false;
  432.         if (! MF_rf_tx_level_cnf )
  433.         {
  434.             CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_TX_LEVEL_FAIL );
  435.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  436.                       " FAIL: TX level (! MF_rf_tx_level_cnf ) ."
  437.                     );
  438.             return false;
  439.         }
  440.         if(! FetchAverageFreqErr(chk_CapIdFreqErr[i].FreqErr)  )
  441.         {
  442.             return false;
  443.         }
  444.         //********************************************************
  445.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  446.                     " CAP ID = " + IntToStr( MF_crystal_afc_ptr->Get_cap_id() ) + "," +
  447.                     " AFC DAC = " + IntToStr( m_sCHECK_DAC[i] ) + "," +
  448.                     " Freq error = " + Double_To_AnsiString(chk_CapIdFreqErr[i].FreqErr)
  449.                   );
  450.         FreqErrorPPM = chk_CapIdFreqErr[i].FreqErr / d_CAL_Center_Freq;
  451.         if( (FreqErrorPPM < d_CRYSTAL_AFC_MAX_FREQ_ERR_PPM && FreqErrorPPM>=0)  ||
  452.             (FreqErrorPPM > d_CRYSTAL_AFC_MIN_FREQ_ERR_PPM && FreqErrorPPM < 0) //||
  453.            // (chk_CapIdFreqErr[0].FreqErr > 0 && i==0 )                     ||
  454.            // (chk_CapIdFreqErr[1].FreqErr < 0 && i==1 )
  455.           )
  456.         {
  457.             //CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_CHECK_FAIL );
  458.                 isCrystalAfcCheckFinish[i] = false;
  459.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  460.                     " FAIL: Crystal AFC check:" +
  461.                     " CAP ID = " + IntToStr( MF_crystal_afc_ptr->Get_cap_id() ) + "," +
  462.                     " AFC DAC = " + IntToStr( m_sCHECK_DAC[i] ) + "," +
  463.                     " Freq error PPM = " + Double_To_AnsiString(FreqErrorPPM)+ "," +
  464.                     " min Freq error PPM = " +  Double_To_AnsiString( d_CRYSTAL_AFC_MIN_FREQ_ERR_PPM )+ "," +
  465.                     " max Freq error PPM = " +  Double_To_AnsiString( d_CRYSTAL_AFC_MAX_FREQ_ERR_PPM )+ "n"
  466.                   );
  467.                //FreqErrorPPMFail = FreqErrorPPM;
  468.            }
  469.            else
  470.            {
  471.                isCrystalAfcCheckFinish[i] = true;
  472.                log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  473.                     " PASS: Crystal AFC check:" +
  474.                     " CAP ID = " + IntToStr( MF_crystal_afc_ptr->Get_cap_id() ) + ", " +
  475.                     " AFC DAC = " + IntToStr( m_sCHECK_DAC[i] ) + ", " +
  476.                     " Freq error PPM = " + Double_To_AnsiString(FreqErrorPPM)+ ", " +
  477.                     " min Freq error PPM = " +  Double_To_AnsiString( d_CRYSTAL_AFC_MIN_FREQ_ERR_PPM ) + ", " +
  478.                     " max Freq error PPM = " +  Double_To_AnsiString( d_CRYSTAL_AFC_MAX_FREQ_ERR_PPM ) + "n"
  479.                   );
  480.            }
  481.     } // for
  482.     if( !isCrystalAfcCheckFinish[0] || !isCrystalAfcCheckFinish[1] )
  483.     {
  484.         CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_CHECK_FAIL );
  485.         return false;
  486.     }
  487.     PostMessage(
  488.                      ctrl.hPostMsgDestHandle,
  489.                      WM_MF_RF_CRYSTAL_AFC_CAPID_DONE,
  490.                      0,
  491.                      0
  492.                     );
  493.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  494.               " ================== Crystal CAPID check end ======================= "
  495.            );
  496.     Sleep(50);
  497.     if (!MF_rf_stop.REQ_Start())
  498.     {
  499.         CalErrorHandler(WM_MF_RF_STOP_FAIL);
  500.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  501.                           " FAIL: Target < stop RF fail."
  502.                         );
  503.         return false;
  504.     }
  505.     return true;
  506. }
  507. //----------------------------------------------------------------------------
  508. bool __fastcall T_META_factory_calibration::CrystalAFCCapIDCheck_Normal( void )
  509. {
  510.     char tempbuf[256];
  511.     ////////////////////////// check crystal AFC result ///////////////////////
  512.     log->Add( "n" +DateToStr(Date()) +  " " + CurrentTimeStr() +
  513.               " ================== Crystal CAPID check begin ===================== "
  514.            );
  515.    // if( ! isAnyPositiveFreqErr )
  516.    // {
  517.    //     CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_ALL_FREQ_ERR_POSITIVE_FAIL );
  518.    //     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  519.    //               " FAIL: crystal AFC calibration all frequency error are positive."
  520.    //             );
  521.    //     return false;
  522.    // }
  523.   //  if( ! isAnyNegativeFreqErr )
  524.   //  {
  525.   //      CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_ALL_FREQ_ERR_NEGATIVE_FAIL );
  526.   //      log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  527.   //                " FAIL: crystal AFC calibration all frequency error are negative."
  528.   //              );
  529.   //      return false;
  530.   //  }
  531.     bool isCrystalAfcCheckFinish[2];
  532.     isCrystalAfcCheckFinish[0] = false;
  533.     isCrystalAfcCheckFinish[1] = false;
  534.     double  FreqErrorPPMFail;
  535.     int cap_id_max;
  536.     cap_id_max = MF_crystal_afc_ptr->Get_max_cap_id(m_pCal->ui_rf_id);
  537.     // fine tune cap_id
  538.     int last_min_cap_id[2];
  539.     last_min_cap_id[0] = -10000;
  540.     last_min_cap_id[1] = -10000;
  541.     do
  542.     {
  543.         int i_CRYSTAL_AFC_PCL = Get_CRYSTAL_AFC_PCL( as_CRYSTAL_AFC_BAND );
  544.         int i=0;
  545.         for (i=0; i<2; i++)
  546.         {
  547.             Sleep(50);
  548.             if (!MF_rf_stop.REQ_Start())
  549.             {
  550.                 CalErrorHandler( WM_MF_RF_STOP_FAIL );
  551.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  552.                           " FAIL: Target < stop RF fail."
  553.                         );
  554.                 return false;
  555.             }
  556.             MF_rf_tx_level_req->arfcn = m_sCRYSTAL_AFC_ARFCN;
  557.             MF_rf_tx_level_req->bsic  =  m_cTSC;
  558.             MF_rf_tx_level_req->power    = i_CRYSTAL_AFC_PCL;
  559.             MF_rf_tx_level_req->frames   = 2000;
  560.             MF_rf_tx_level_req->dacValue = m_sCHECK_DAC[i];
  561.             MF_rf_tx_level_req->burstTypeNB = NB_TX_RANDOM_WITH_TSC;
  562.             if ( META_Rf_NB_TX_r( m_pCal->i_MainMETAHandle,
  563.                     MF_rf_tx_level_req,
  564.                     cb_MF_rf_tx_level_crystal,
  565.                     &MF_rf_tx_level_token,
  566. NULL
  567.                           ) != META_SUCCESS)
  568.             {
  569.                 CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  570.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  571.                           " FAIL : Target< ARFCN = " + IntToStr(m_sCRYSTAL_AFC_ARFCN) +
  572.                           ", TSC = " + IntToStr( m_cTSC ) +
  573.                           ", power = " + i_CRYSTAL_AFC_PCL +
  574.                           ", frames = " + IntToStr(i_N_TX) +
  575.                           ", dac value = " + IntToStr( m_sDefault_value )
  576.                         );
  577.                 return false;
  578.             }
  579.             is_suspend_cal = true;
  580.             this->Suspend();
  581.             is_suspend_cal = false;
  582.             if (! MF_rf_tx_level_cnf )
  583.             {
  584.                 CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_TX_LEVEL_FAIL );
  585.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  586.                       " FAIL: TX level (! MF_rf_tx_level_cnf ) ."
  587.                     );
  588.                 return false;
  589.             }
  590.             if(! FetchAverageFreqErr(chk_CapIdFreqErr[i].FreqErr)  )
  591.             {
  592.                 return false;
  593.             }
  594.             //********************************************************
  595.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  596.                     " CAP ID = " + IntToStr( MF_crystal_afc_ptr->Get_cap_id() ) + "," +
  597.                     " AFC DAC = " + IntToStr( m_sCHECK_DAC[i] ) + "," +
  598.                     " Freq error = " + Double_To_AnsiString(chk_CapIdFreqErr[i].FreqErr)
  599.                   );
  600.             FreqErrorPPM = chk_CapIdFreqErr[i].FreqErr / d_CAL_Center_Freq;
  601.             if( (FreqErrorPPM < d_CRYSTAL_AFC_MAX_FREQ_ERR_PPM && FreqErrorPPM>=0)  ||
  602.                 (FreqErrorPPM > d_CRYSTAL_AFC_MIN_FREQ_ERR_PPM && FreqErrorPPM < 0) //||
  603.                // (chk_CapIdFreqErr[0].FreqErr > 0 && i==0 )                     ||
  604.                // (chk_CapIdFreqErr[1].FreqErr < 0 && i==1 )
  605.               )
  606.             {
  607.             //CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_CHECK_FAIL );
  608.                 isCrystalAfcCheckFinish[i] = false;
  609.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  610.                     " FAIL: Crystal AFC check:" +
  611.                     " CAP ID = " + IntToStr( MF_crystal_afc_ptr->Get_cap_id() ) + "," +
  612.                     " AFC DAC = " + IntToStr( m_sCHECK_DAC[i] ) + "," +
  613.                     " Freq error PPM = " + Double_To_AnsiString(FreqErrorPPM)+ "," +
  614.                     " min Freq error PPM = " +  Double_To_AnsiString( d_CRYSTAL_AFC_MIN_FREQ_ERR_PPM )+ "," +
  615.                     " max Freq error PPM = " +  Double_To_AnsiString( d_CRYSTAL_AFC_MAX_FREQ_ERR_PPM )+ "n"
  616.                   );
  617.                FreqErrorPPMFail = FreqErrorPPM;
  618.            }
  619.            else
  620.            {
  621.                isCrystalAfcCheckFinish[i] = true;
  622.                log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  623.                     " PASS: Crystal AFC check:" +
  624.                     " CAP ID = " + IntToStr( MF_crystal_afc_ptr->Get_cap_id() ) + ", " +
  625.                     " AFC DAC = " + IntToStr( m_sCHECK_DAC[i] ) + ", " +
  626.                     " Freq error PPM = " + Double_To_AnsiString(FreqErrorPPM)+ ", " +
  627.                     " min Freq error PPM = " +  Double_To_AnsiString( d_CRYSTAL_AFC_MIN_FREQ_ERR_PPM ) + ", " +
  628.                     " max Freq error PPM = " +  Double_To_AnsiString( d_CRYSTAL_AFC_MAX_FREQ_ERR_PPM ) + "n"
  629.                   );
  630.            }
  631.         } // for
  632.         if( isCrystalAfcCheckFinish[0] ^ isCrystalAfcCheckFinish[1] )
  633.         {
  634.             last_min_cap_id[1] = last_min_cap_id[0];
  635.             last_min_cap_id[0] = min_CapIdFreqErr.cap_id;
  636.             if( FreqErrorPPMFail < d_CRYSTAL_AFC_MAX_FREQ_ERR_PPM && FreqErrorPPMFail>=0 )
  637.                {
  638.                    if( m_bFreqErrIncrease )
  639.                    {
  640.                         min_CapIdFreqErr.cap_id++;
  641.                    }
  642.                    else
  643.                    {
  644.                         min_CapIdFreqErr.cap_id--;
  645.                    }
  646.                }
  647.                else
  648.                {
  649.                    if( m_bFreqErrIncrease )
  650.                    {
  651.                         min_CapIdFreqErr.cap_id--;
  652.                    }
  653.                    else
  654.                    {
  655.                         min_CapIdFreqErr.cap_id++;
  656.                    }
  657.                }
  658.                MF_crystal_afc_ptr->Set_cap_id( min_CapIdFreqErr.cap_id );
  659.                CrystalCfg_req.cap_id = min_CapIdFreqErr.cap_id;
  660.                if (WRITE_NVARM == m_pCal->s_crystal_afc_option.crystal_cap_id_update)
  661.                {
  662.                     MF_crystal_afc_ptr->ConfirmCallback = ::ccb_write_CrystalAFC_to_nvram;
  663.                     MF_crystal_afc_ptr->REQ_Write_CAPID_To_NVRAM_Start();
  664.                     is_suspend_cal = true;
  665.                     this->Suspend();
  666.                     is_suspend_cal = false;
  667.                     E_METAAPP_RESULT_T state = MF_crystal_afc_ptr->Get_ConfirmState();
  668.                     if (state != METAAPP_SUCCESS)
  669.                     {
  670.                         if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  671.                         {
  672.                             CalErrorHandler(WM_MF_NVRAM_EF_L1_CRYSTAL_CAPDATA_LID_VERNO_FAIL);
  673.                             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  674.                                      " FAIL : NVRAM_EF_L1_CRYSTAL_CAPDATA_LID version is not support, please update META to latest version. "
  675.                                     );
  676.                         }
  677.                         else
  678.                         {
  679.                             CalErrorHandler(WM_MF_NVRAM_CRYSTAL_AFC_CAPID_WRITE_FAIL);
  680.                             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  681.                                       " FAIL : Target< Write crystal AFC CAP ID to NVRAM fail "
  682.                                     );
  683.                         }
  684.                         return false;
  685.                     }
  686.                 }
  687.                 else
  688.                 {
  689.                     MetaResult = META_Rf_SetCrystalCapId_r( m_pCal->i_MainMETAHandle, 5000, &CrystalCfg_req );
  690.                     if( MetaResult != META_SUCCESS )
  691.                     {
  692.                         CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_SET_CAPID_FAIL );
  693.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  694.                                   " FAIL : Target< Set crystal AFC CAP ID fail "
  695.                                 );
  696.                         return false;
  697.                     }
  698.                }
  699.         }
  700.     } // while
  701.     while( (isCrystalAfcCheckFinish[0] ^ isCrystalAfcCheckFinish[1]) &&
  702.            min_CapIdFreqErr.cap_id >=CAP_ID_MIN                   &&
  703.            min_CapIdFreqErr.cap_id <=cap_id_max                   &&
  704.            min_CapIdFreqErr.cap_id !=last_min_cap_id[0]           &&
  705.            min_CapIdFreqErr.cap_id !=last_min_cap_id[1]
  706.          );
  707.     if( !isCrystalAfcCheckFinish[0] || !isCrystalAfcCheckFinish[1] )
  708.     {
  709.         CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_CHECK_FAIL );
  710.         return false;
  711.     }
  712.     // save to result
  713.     for(int i=0; i<2; i++)
  714.     {
  715.         sprintf(tempbuf, "%8.3f", chk_CapIdFreqErr[i].FreqErr/d_CAL_Center_Freq);
  716.         m_pCal->as_ResultBuf = m_pCal->as_ResultBuf + tempbuf + ", ";
  717.     }
  718.     sprintf(tempbuf, "%8d", min_CapIdFreqErr.cap_id);
  719.     m_pCal->as_ResultBuf = m_pCal->as_ResultBuf + tempbuf + ", ";
  720.     WriteCrystalAFCCalResultToFile(as_ID+".cal",Application->ExeName, m_pCal->b_CalResultPath);
  721.     // write CAP ID to NVRAM
  722.     MF_crystal_afc_ptr->ConfirmCallback = ::ccb_write_CrystalAFC_to_nvram;
  723.     MF_crystal_afc_ptr->REQ_Write_CAPID_To_NVRAM_Start();
  724.     is_suspend_cal = true;
  725.     this->Suspend();
  726.     is_suspend_cal = false;
  727.     E_METAAPP_RESULT_T state = MF_crystal_afc_ptr->Get_ConfirmState();
  728.     if (state != METAAPP_SUCCESS)
  729.     {
  730.         if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  731.         {
  732.             CalErrorHandler(WM_MF_NVRAM_EF_L1_CRYSTAL_CAPDATA_LID_VERNO_FAIL);
  733.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  734.                          " FAIL : NVRAM_EF_L1_CRYSTAL_CAPDATA_LID version is not support, please update META to latest version. "
  735.                          );
  736.         }
  737.         else
  738.         {
  739.             CalErrorHandler(WM_MF_NVRAM_CRYSTAL_AFC_CAPID_WRITE_FAIL);
  740.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  741.                              " FAIL : Target< Write crystal AFC CAP ID to NVRAM fail "
  742.                              );
  743.         }
  744.         return false;
  745.     }
  746.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  747.                   " Target< Write crystal AFC CAP ID to NVRAM"
  748.                 );
  749.     if ( this->Terminated )
  750.     {
  751.         this->OnTerminate = neByUser;
  752.         return false;
  753.     }
  754.     PostMessage(
  755.                      ctrl.hPostMsgDestHandle,
  756.                      WM_MF_RF_CRYSTAL_AFC_CAPID_DONE,
  757.                      0,
  758.                      0
  759.                     );
  760.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  761.               " ================== Crystal CAPID check end ======================= "
  762.            );
  763.     Sleep(50);
  764.     if (!MF_rf_stop.REQ_Start())
  765.     {
  766.         CalErrorHandler( WM_MF_RF_STOP_FAIL );
  767.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  768.                           " FAIL: Target < stop RF fail."
  769.                         );
  770.         return false;
  771.     }
  772.     return true;
  773. }
  774. //---------------------------------------------------------------------------
  775. bool __fastcall T_META_factory_calibration::EquConfigPFER(void)
  776. {
  777.     RestartTimerCal(WM_MF_AGE8960_SET_MESA_FUNC_PFER_FAIL);
  778.     if (!m_rct_ctrl.RCT_confPFER( m_pRct, m_sCRYSTAL_AFC_FREQ_ERR_MEASUSE_COUNT))
  779.     {
  780.         CalErrorHandler(WM_MF_AGE8960_CONFIG_PFER_FAIL);
  781.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  782.                       " FAIL: " + m_pRct->as_RCT + " < config phase and frequency error."
  783.                     );
  784.         return false;
  785.     }
  786.     log->Add(DateToStr(Date()) + " " + CurrentTimeStr() + " " +
  787.              m_pRct->as_RCT + " < config phase and frequency error."
  788.              );
  789.     frmFatcory->DisableAllCalTimer();
  790.     return true;
  791. }
  792. //-----------------------------------------------------------------------------
  793. bool __fastcall T_META_factory_calibration::FetchAverageFreqErr(ViReal64& AvgFreqErr)
  794. {
  795.     RestartTimerCal(WM_MF_AGE8960_READ_PFER_FAIL);
  796.     if (!m_rct_ctrl.RCT_FetchAvgFreqErr(m_pRct, AvgFreqErr))
  797.     {
  798.         CalErrorHandler(WM_MF_AGE8960_READ_PFER_FAIL);
  799.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  800.                  " FAIL: " + m_pRct->as_RCT + " < fetch phase error."
  801.                  );
  802.         return false;
  803.     }
  804.     frmFatcory->DisableAllCalTimer();
  805.     return true;
  806. }
  807. //-----------------------------------------------------------------------------
  808. bool __fastcall T_META_factory_calibration::FetchSigAverageFreqErr(ViReal64& AvgFreqErr)
  809. {
  810.     RestartTimerCal(WM_MF_AGE8960_READ_PFER_FAIL);
  811.     if (!m_rct_ctrl.RCT_sig_FetchAvgFreqErr(m_pRct, AvgFreqErr))
  812.     {
  813.         CalErrorHandler(WM_MF_AGE8960_READ_PFER_FAIL);
  814.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  815.                             " FAIL: " + m_pRct->as_RCT + " < fetch phase error."
  816.                     );
  817.         return false;
  818.     }
  819.     frmFatcory->DisableAllCalTimer();
  820.     return true;
  821. }
  822. //----------------------------------------------------------------------------
  823. bool __fastcall T_META_factory_calibration::CrystalAFCMeasureRxFreqErr( void )
  824. {
  825.     char tempbuf[256];
  826.     
  827.     MF_rf_afc_req->arfcn    = m_sAFC_ARFCN;
  828.     MF_rf_afc_req->dacValue = (TCVCXO == m_pCal->e_AFC_type)?m_sDAC1:m_sCRYSTAL_DAC1;
  829.     MF_rf_afc_req->gain     = (MF_RF_GAIN_TO_BE_SUB - d_P_DL) * GAIN_REQUEST_PC_SIDE_MULTIPLY;
  830.     MF_rf_afc_req->testNumber = m_sN_AFC;
  831.     if ( META_Rf_AFC_r( m_pCal->i_MainMETAHandle,  MF_rf_afc_req, MF_rf_afc_cb, &MF_rf_afc_token,NULL ) != META_SUCCESS )
  832.     {
  833.         CalErrorHandler( WM_MF_RF_AFC_CONTROL_FAIL );
  834.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  835.                   " FAIL : Target < AFC: ARFCN = "+ IntToStr( m_sAFC_ARFCN ) +
  836.                   ", dac value = "+ IntToStr(MF_rf_afc_req->dacValue) +
  837.                   ", gain = " + Double_To_AnsiString(MF_rf_afc_req->gain/RF_RX_GAIN_SCALE) + " dB" +
  838.                   ", testing number = " + IntToStr( m_sN_AFC )
  839.                 );
  840.         return false;
  841.     }
  842.     if ( this->Terminated )
  843.     {
  844.         this->OnTerminate = neByUser;
  845.         return false;
  846.     }
  847.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  848.              " Target < ARFCN = "+ IntToStr( m_sAFC_ARFCN ) +
  849.              ", dac value = "+ IntToStr(MF_rf_afc_req->dacValue) +
  850.              ", gain = " + Double_To_AnsiString(MF_rf_afc_req->gain/RF_RX_GAIN_SCALE) + " dB" +
  851.              ", testing number = " + IntToStr( m_sN_AFC )
  852.            );
  853.     //log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  854.    //                         " TimerCal->Enabled 2= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  855.    //                       );
  856.     RestartTimerCal(WM_MF_RF_AFC_CONTROL_FAIL);
  857.     is_suspend_cal = true;
  858.     this->Suspend();
  859.     is_suspend_cal = false;
  860.     if (this->Terminated)
  861.     {
  862.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  863.                   " Terminate at line " + AnsiString(__FILE__) + " " + AnsiString(__LINE__)
  864.                 );
  865.         this->OnTerminate = neByUser;
  866.         return false;
  867.     }
  868.     if ( MF_rf_afc_cnf->fcb_ok_number <= 0 )
  869.     {
  870.         CalErrorHandler( WM_MF_RF_AFC_CONTROL_FAIL );
  871.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  872.                   " FAIL : Target< AFC FB detection fail "
  873.                 );
  874.         return false;
  875.     }
  876.     if ( this->Terminated )
  877.     {
  878.         this->OnTerminate = neByUser;
  879.         return false;
  880.     }
  881.     frmFatcory->DisableAllCalTimer();
  882.     sprintf(tempbuf, "%11d", MF_rf_afc_cnf->freqOffset);
  883.     m_pCal->as_ResultBuf = m_pCal->as_ResultBuf + tempbuf + ", ";
  884.     if (!WriteAFCCalResultToFile(as_ID+".cal",Application->ExeName, m_pCal->b_CalResultPath))
  885.     {
  886.         CalErrorHandler(WM_MF_RF_AFC_WRITE_CAL_RESULT_FAIL);
  887.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  888.                 " FAIL : write RX frequency error to cal file "
  889.                 );
  890.         return false;
  891.     }
  892.     frmFatcory->DisableAllCalTimer();
  893.     return true;
  894. }
  895. //----------------------------------------------------------------------------
  896. bool __fastcall T_META_factory_calibration::CrystalTXAfcOffsetCal( void )
  897. {
  898.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  899.               " ================== AFC TRX offset calibration begin ======================= "
  900.            );
  901.     PostMessage(
  902.                      ctrl.hPostMsgDestHandle,
  903.                      WM_MF_RF_CRYSTAL_AFC_TRX_OFFSET_BEGIN,
  904.                      0,
  905.                      0
  906.                     );
  907.     m_bFirstAfcTracking = true;
  908.     if( ! m_rct_ctrl.RCT_operatingMode(m_pRct, OPERATING_MODE_GSM_BCH_TCH))
  909.     {
  910.         CalErrorHandler(WM_MF_AGE8960_SET_OPERATION_MODE_FAIL);
  911.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  912.                               " FAIL :"+
  913.                               m_pRct->as_RCT + " < Operation mode = GSM BCH+TCH"
  914.                             );
  915.         return false;
  916.     }
  917.     char tempbuf[256];
  918.     double FreqErr;
  919.     int i_CRYSTAL_AFC_PCL;
  920.     short sARFCN;
  921.     int vi_Band;
  922.     FrequencyBand eFreqBand = FrequencyBand850;
  923.     while (eFreqBand < FrequencyBandCount)
  924.     {
  925.         switch (eFreqBand)
  926.         {
  927.             case FrequencyBand850:
  928.             {
  929.                 if (! IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  930.                 {
  931.                     eFreqBand++;
  932.                     continue;
  933.                 }
  934.                 vi_Band        = age1960_GSM850_BAND;
  935.                 sARFCN        = m_sARFCN_C0_GSM850;
  936.                 i_CRYSTAL_AFC_PCL = i_CRYSTAL_AFC_GSM850_PCL;
  937.             }
  938.             break;
  939.             case FrequencyBand900:
  940.             {
  941.                 if (! IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  942.                 {
  943.                     eFreqBand++;
  944.                     continue;
  945.                 }
  946.                 vi_Band        = age1960_EGSM_BAND;
  947.                 sARFCN        = m_sARFCN_C0_GSM;
  948.                 i_CRYSTAL_AFC_PCL = i_CRYSTAL_AFC_GSM900_PCL;
  949.             }
  950.             break;
  951.             case FrequencyBand1800:
  952.             {
  953.                 if (! IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  954.                 {
  955.                     eFreqBand++;
  956.                     continue;
  957.                 }
  958.                 vi_Band        = age1960_DCS1800_BAND;
  959.                 sARFCN        = m_sARFCN_C0_DCS;
  960.                 i_CRYSTAL_AFC_PCL = i_CRYSTAL_AFC_DCS1800_PCL;
  961.             }
  962.             break;
  963.             case FrequencyBand1900:
  964.             {
  965.                 if (! IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  966.                 {
  967.                     eFreqBand++;
  968.                     continue;
  969.                 }
  970.                 vi_Band        = age1960_PCS1900_BAND;
  971.                 sARFCN        = m_sARFCN_C0_PCS;
  972.                 i_CRYSTAL_AFC_PCL = i_CRYSTAL_AFC_PCS1900_PCL;
  973.             }
  974.             break;
  975.             default:
  976.                 assert(false);
  977.             break;
  978.         } // switch
  979.         
  980.        log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  981.                   " ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
  982.                 );
  983.         int  band_idx = FreqBand_To_BandIdx(eFreqBand);
  984.        // Sleep( 10000 ); // temp test
  985.         if (IsFreqBankSupported(m_asFreqBank, eFreqBand))
  986.         {
  987.             if(eFreqBand == FrequencyBand1900)
  988.             {
  989.                 if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle,  1, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  990.                 {
  991.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  992.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  993.                                   " FAIL: Target < set Band1900 flag = 1"
  994.                                 );
  995.                     return false;
  996.                 }
  997.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  998.                                   " Target < set Band1900 flag = 1"
  999.                                 );
  1000.             }
  1001.             else
  1002.             {
  1003.                 if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle, 0, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  1004.                 {
  1005.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1006.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1007.                                   " FAIL: Target < set Band1900 flag = 0"
  1008.                                 );
  1009.                     return false;
  1010.                 }
  1011.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1012.                                   " Target < set Band1900 flag = 0"
  1013.                                 );
  1014.             }
  1015.             is_suspend_cal = true;
  1016.             this->Suspend();
  1017.             is_suspend_cal = false;
  1018.         }
  1019.         if (AGILENT_8960 == m_pRct->device_type)
  1020.         {
  1021.             if (!m_rct_ctrl.RCT_cellBand(m_pRct, vi_Band))
  1022.             {
  1023.                 CalErrorHandler(WM_MF_AGE8960_SET_BAND_FAIL);
  1024.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1025.                          " FAIL:"+
  1026.                          " Agilent 8960 < Band = " + ViBand_To_Str(vi_Band)
  1027.                          );
  1028.                 return false;
  1029.             }
  1030.         }
  1031.         else
  1032.         {
  1033.             if( ! m_rct_ctrl.RCT_sig_cellBand( m_pRct, vi_Band ) )
  1034.             {
  1035.                 CalErrorHandler( WM_MF_AGE8960_SET_BAND_FAIL );
  1036.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1037.                               " FAIL :"+
  1038.                               " CMU 200 < Band = " + ViBand_To_Str( vi_Band )
  1039.                             );
  1040.                 return false;
  1041.             }
  1042.         }
  1043.         if ( this->Terminated )
  1044.         {
  1045.             this->OnTerminate = neByUser;
  1046.             return false;
  1047.         }
  1048.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1049.                       m_pRct->as_RCT + " < Band = " + ViBand_To_Str(vi_Band)
  1050.                     );
  1051. //        Sleep( 100 );  // wait Agilent 8960 ready!
  1052.                    // if Agilent 8960 not read, target will get error power!
  1053.         if ( this->Terminated )
  1054.         {
  1055.             this->OnTerminate = neByUser;
  1056.             return false;
  1057.         }
  1058.         if (AGILENT_8960 == m_pRct->device_type)
  1059.         {
  1060.             if( ! m_rct_ctrl.RCT_cellPower( m_pRct, d_P_DL ) )
  1061.             {
  1062.                 CalErrorHandler( WM_MF_AGE8960_SET_POWER_FAIL );
  1063.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: " + m_pRct->as_RCT + " < Power = " +
  1064.                           Double_To_AnsiString( d_P_DL) + " dBm" );
  1065.                 return false;
  1066.             }
  1067.         }
  1068.         else
  1069.         {
  1070.             if( ! m_rct_ctrl.RCT_sig_cellPower( m_pRct, d_P_DL ) )
  1071.             {
  1072.                 CalErrorHandler( WM_MF_AGE8960_SET_POWER_FAIL );
  1073.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: " + m_pRct->as_RCT + " < Power = " +
  1074.                           Double_To_AnsiString( d_P_DL) + " dBm");
  1075.                 return false;
  1076.             }
  1077.             if( ! m_rct_ctrl.RCT_sig_TCHLevel( m_pRct, d_CRYSTAL_AFC_TCH_P_DL ) )
  1078.             {
  1079.                 CalErrorHandler( WM_MF_AGE8960_SET_POWER_FAIL );
  1080.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: cmu200 < Power = " +
  1081.                           Double_To_AnsiString( d_CRYSTAL_AFC_TCH_P_DL) + " dBm");
  1082.                 return false;
  1083.             }
  1084.         }
  1085.         if ( this->Terminated )
  1086.         {
  1087.             this->OnTerminate = neByUser;
  1088.             return false;
  1089.         }
  1090.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1091.                   m_pRct->as_RCT + " < cell power = "+
  1092.                   Double_To_AnsiString(d_P_DL)
  1093.                  );
  1094.         if (AGILENT_8960 == m_pRct->device_type)
  1095.         {
  1096.             if( ! m_rct_ctrl.RCT_BCHARFCN( m_pRct, sARFCN) )
  1097.             {
  1098.                 CalErrorHandler( WM_MF_AGE8960_SET_BCH_ARFCN_FAIL );
  1099.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1100.                               " FAIL : "+
  1101.                               m_pRct->as_RCT + " < BCH ARFCN = " + IntToStr( sARFCN )
  1102.                     );
  1103.                 return false;
  1104.             }
  1105.         }
  1106.         else
  1107.         {
  1108.             if( ! m_rct_ctrl.RCT_sig_BCHARFCN( m_pRct, sARFCN ) )
  1109.             {
  1110.                 CalErrorHandler( WM_MF_AGE8960_SET_BCH_ARFCN_FAIL );
  1111.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1112.                               " FAIL :"+
  1113.                               m_pRct->as_RCT + " < BCH ARFCN = " + IntToStr( sARFCN )
  1114.                     );
  1115.                 return false;
  1116.             }
  1117.         }
  1118.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1119.                           m_pRct->as_RCT + " < BCH ARFCN = " + IntToStr(sARFCN)
  1120.                         );
  1121.         if( frmFatcory->rbAgilent8960->Checked )
  1122.         {
  1123.             if (!m_rct_ctrl.RCT_TCHARFCN(m_pRct, sARFCN))
  1124.             {
  1125.                 CalErrorHandler( WM_MF_AGE8960_SET_TCH_ARFCN_FAIL );
  1126.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1127.                               " FAIL :"+
  1128.                               m_pRct->as_RCT + " < TCH ARFCN = " + IntToStr(sARFCN)
  1129.                         );
  1130.                 return false;
  1131.             }
  1132.         }
  1133.         else
  1134.         {
  1135.             if (!m_rct_ctrl.RCT_sig_TCHARFCN(m_pRct, sARFCN))
  1136.             {
  1137.                 CalErrorHandler( WM_MF_AGE8960_SET_TCH_ARFCN_FAIL );
  1138.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1139.                               " FAIL :"+
  1140.                               m_pRct->as_RCT + " < TCH ARFCN = " + IntToStr(sARFCN)
  1141.                             );
  1142.                 return false;
  1143.             }
  1144.         }
  1145.         if ( this->Terminated )
  1146.         {
  1147.             this->OnTerminate = neByUser;
  1148.             return false;
  1149.         }
  1150.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1151.                           m_pRct->as_RCT + " < TCH ARFCN = " + IntToStr(sARFCN)
  1152.                         );
  1153.         if (frmFatcory->rbAgilent8960->Checked)
  1154.         {
  1155.             if( ! m_rct_ctrl.RCT_TCHTimeslot(m_pRct, TIMESLOT_3) )
  1156.             {
  1157.                 CalErrorHandler(WM_MF_AGE8960_SET_TCH_TIMESLOT_FAIL);
  1158.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1159.                               " FAIL :"+
  1160.                               m_pRct->as_RCT + " < timeslot = " + IntToStr( TIMESLOT_3 )
  1161.                             );
  1162.                 return false;
  1163.             }
  1164.         }
  1165.         else
  1166.         {
  1167.             if( ! m_rct_ctrl.RCT_sig_TCHTimeslot(m_pRct, TIMESLOT_3) )
  1168.             {
  1169.                 CalErrorHandler( WM_MF_AGE8960_SET_TCH_TIMESLOT_FAIL );
  1170.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1171.                               " FAIL :"+
  1172.                               m_pRct->as_RCT + " < timeslot = " + IntToStr(TIMESLOT_3)
  1173.                             );
  1174.                 return false;
  1175.             }
  1176.         }
  1177.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1178.                           m_pRct->as_RCT + " < TCH timeslot = " + IntToStr(TIMESLOT_3)
  1179.                         );
  1180.         if ( frmFatcory->rbAgilent8960->Checked )
  1181.         {
  1182.             if( ! m_rct_ctrl.RCT_ConfigTSC( m_pRct, m_cTSC ) )
  1183.             {
  1184.                 CalErrorHandler( WM_MF_AGE8960_SET_TSC_FAIL );
  1185.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1186.                               "FAIL : " + m_pRct->as_RCT + " < set TSC fail."
  1187.                             );
  1188.                 return false;
  1189.             }
  1190.         }
  1191.         else
  1192.         {
  1193.             if( ! m_rct_ctrl.RCT_sig_ConfigTSC( m_pRct, 0 ) )
  1194.             {
  1195.                 CalErrorHandler( WM_MF_AGE8960_SET_TSC_FAIL );
  1196.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1197.                           " FAIL : " + m_pRct->as_RCT + " < TSC = " + IntToStr( 0 )
  1198.                         );
  1199.                 return false;
  1200.             }
  1201.         }
  1202.         if ( this->Terminated )
  1203.         {
  1204.                 this->OnTerminate = neByUser;
  1205.                 return false;
  1206.         }
  1207.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1208.                           m_pRct->as_RCT + " < TSC = " + IntToStr( m_cTSC)
  1209.                         );
  1210.         if ( frmFatcory->rbAgilent8960->Checked )
  1211.         {
  1212.             if( ! m_rct_ctrl.RCT_MSTXLevel( m_pRct, i_CRYSTAL_AFC_PCL ) )
  1213.             {
  1214.                 CalErrorHandler( WM_MF_AGE8960_SET_MS_TX_LEVEL_FAIL );
  1215.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1216.                       " FAIL : " + m_pRct->as_RCT + " < MS TX level = " + IntToStr(i_CRYSTAL_AFC_PCL)
  1217.                     );
  1218.                 return false;
  1219.             }
  1220.         }
  1221.         else
  1222.         {
  1223.             if( ! m_rct_ctrl.RCT_sig_MSTXLevel( m_pRct, i_CRYSTAL_AFC_PCL ) )
  1224.             {
  1225.                 CalErrorHandler( WM_MF_AGE8960_SET_MS_TX_LEVEL_FAIL );
  1226.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1227.                       " FAIL : " + m_pRct->as_RCT + " < MS TX level = " + IntToStr( i_CRYSTAL_AFC_PCL )
  1228.                     );
  1229.                 return false;
  1230.             }
  1231.         }
  1232.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1233.                       m_pRct->as_RCT + " < MS TX level = " + IntToStr(i_CRYSTAL_AFC_PCL)
  1234.                     );
  1235.        // log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1236.        //                   " TimerCal->Enabled 8= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  1237.        //                 );
  1238.         RestartTimerCal( WM_MF_AGE8960_CONFIG_PFER_FAIL );
  1239.         if ( frmFatcory->rbAgilent8960->Checked )
  1240.         {
  1241.             if( ! m_rct_ctrl.RCT_confPFER( m_pRct, m_sCRYSTAL_AFC_FREQ_ERR_MEASUSE_COUNT ) )
  1242.             {
  1243.                 CalErrorHandler( WM_MF_AGE8960_CONFIG_PFER_FAIL );
  1244.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1245.                                   " FAIL : " + m_pRct->as_RCT + " < config PRER."
  1246.                                 );
  1247.                 return false;
  1248.             }
  1249.         }
  1250.         else
  1251.         {
  1252.             if( ! m_rct_ctrl.RCT_sig_confPFER( m_pRct, m_sCRYSTAL_AFC_FREQ_ERR_MEASUSE_COUNT ) )
  1253.             {
  1254.                 CalErrorHandler( WM_MF_AGE8960_CONFIG_PFER_FAIL );
  1255.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1256.                                   " FAIL : " + m_pRct->as_RCT + " < config PRER."
  1257.                                 );
  1258.                 return false;
  1259.             }
  1260.         }
  1261.        // Sleep(10000);
  1262.         float slope;
  1263.         unsigned short us_init_value;
  1264.         if(! AFCSlopeCal( vi_Band, sARFCN, slope, us_init_value  ) )
  1265.        {
  1266.            CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_CONTROL_FAIL );
  1267.            log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1268.                               " FAIL :"+
  1269.                               " TX AFC offset calibration "
  1270.                             );
  1271.             return false;
  1272.        }
  1273. #if 0
  1274.         int count=0;
  1275.         while( count<m_sCRYSTAL_AFC_RECURSIVE_TIMES )
  1276.         {
  1277.         count++;
  1278.            int  band_idx = FreqBand_To_BandIdx(eFreqBand);
  1279.             int band;
  1280.             short s_arfcn;
  1281.             int   tsc;
  1282.             if( frmFatcory->rbAgilent8960->Checked )
  1283.             {
  1284.                 tsc   = c_TSC;
  1285.             }
  1286.             else
  1287.             {
  1288.                 tsc   = 0;
  1289.             }
  1290.             if (!MF_rf_stop.REQ_Start())
  1291.             {
  1292.                 CalErrorHandler( WM_MF_RF_STOP_FAIL );
  1293.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1294.                           " FAIL: Target < stop RF fail."
  1295.                         );
  1296.                 return false;
  1297.             }
  1298.                 MF_rf_tx_level_req->arfcn = sARFCN;
  1299.                 MF_rf_tx_level_req->bsic  =  tsc;
  1300.                 MF_rf_tx_level_req->power    = i_CRYSTAL_AFC_PCL;
  1301.                 MF_rf_tx_level_req->frames   = -99;
  1302.                 MF_rf_tx_level_req->dacValue = MASK_AFC_TRACKING | us_init_value;
  1303.                 MF_rf_tx_level_req->burstTypeNB = NB_TX_RANDOM_WITH_TSC; // NB burst
  1304.                 if ( META_Rf_NB_TX_r( m_META_HANDLE_Obj.Get_MainHandle(),
  1305.                     MF_rf_tx_level_req,
  1306.                     cb_MF_rf_tx_level_crystal,
  1307.                     &MF_rf_tx_level_token,
  1308. NULL
  1309.                            ) != META_SUCCESS)
  1310.                 {
  1311.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1312.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1313.                       " FAIL : Target< ARFCN = " + IntToStr( sARFCN ) +
  1314.                       ", TSC = " + IntToStr( tsc ) +
  1315.                       ", power = " + IntToStr( i_CRYSTAL_AFC_PCL ) +
  1316.                       ", frames = " + IntToStr(i_N_TX) +
  1317.                       ", dac value = " + IntToStr( us_init_value )
  1318.                     );
  1319.                     return false;
  1320.                 }
  1321.                 is_suspend_cal = true;
  1322.                 this->Suspend();
  1323.                 is_suspend_cal = false;
  1324.                 if (! MF_rf_tx_level_cnf )
  1325.                 {
  1326.                     CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_TX_LEVEL_FAIL );
  1327.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1328.                       " FAIL: TX level (! MF_rf_tx_level_cnf ) ."
  1329.                     );
  1330.                     return false;
  1331.                 }
  1332.                 Sleep(100);
  1333.                 if( frmFatcory->rbAgilent8960->Checked )
  1334.                 {
  1335.                     if( ! FetchAverageFreqErr( FreqErr )  )
  1336.                     {
  1337.                         return false;
  1338.                     }
  1339.                 }
  1340.                 else
  1341.                 {
  1342.                     if( ! FetchSigAverageFreqErr( FreqErr )  )
  1343.                     {
  1344.                         return false;
  1345.                     }
  1346.                 }
  1347.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1348.                     " AFC DAC = " + IntToStr( us_init_value ) + "," +
  1349.                     " Freq error = " + Double_To_AnsiString(FreqErr)
  1350.                 );
  1351.          } // while()
  1352. #endif
  1353.        // if( frmFatcory->cbWriteTXAFCOffset->Checked )
  1354.         {
  1355.             double FreqErr_min;
  1356.             double FreqErr_norm;
  1357.             double FreqErr_norm_last = 0;
  1358.             double FreqErr_norm_min  = 10000000.0;
  1359.             int count = 0;
  1360.             //bool b_first_time = true;
  1361.             short  tx_afc_offset;
  1362.             short  tx_afc_offset_last;
  1363.             short  tx_afc_offset_min;
  1364.             //int band;
  1365.             //short arfcn;
  1366.             int   pcl[4];
  1367.             int   tsc;
  1368.             int   afc_dac;
  1369.             int   cs[4];
  1370.             int   ta;
  1371.             int   pattern;
  1372.             char TimeSlotMask;
  1373.             //band  = band_idx;
  1374.             //arfcn = sARFCN;
  1375.             if( frmFatcory->rbAgilent8960->Checked )
  1376.             {
  1377.                 tsc   = m_cTSC;
  1378.             }
  1379.             else
  1380.             {
  1381.                 tsc   = 0;
  1382.             }
  1383.             if (!MF_rf_stop.REQ_Start())
  1384.             {
  1385.                 CalErrorHandler( WM_MF_RF_STOP_FAIL );
  1386.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1387.                           " FAIL: Target < stop RF fail."
  1388.                         );
  1389.                 return false;
  1390.             }
  1391.                 MF_rf_tx_level_req->arfcn = sARFCN;
  1392.                 MF_rf_tx_level_req->bsic  =  tsc;
  1393.                 MF_rf_tx_level_req->power    = i_CRYSTAL_AFC_PCL;
  1394.                 MF_rf_tx_level_req->frames   = -99;
  1395.                 MF_rf_tx_level_req->dacValue = MASK_AFC_TRACKING | us_init_value;
  1396.                 MF_rf_tx_level_req->burstTypeNB = NB_TX_RANDOM_WITH_TSC; // NB burst
  1397.                 if ( META_Rf_NB_TX_r( m_pCal->i_MainMETAHandle, 
  1398.                     MF_rf_tx_level_req,
  1399.                     cb_MF_rf_tx_level_crystal,
  1400.                     &MF_rf_tx_level_token,
  1401. NULL
  1402.                            ) != META_SUCCESS)
  1403.                 {
  1404.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1405.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1406.                       " FAIL : Target< ARFCN = " + IntToStr( sARFCN ) +
  1407.                       ", TSC = " + IntToStr( tsc ) +
  1408.                       ", power = " + IntToStr( i_CRYSTAL_AFC_PCL ) +
  1409.                       ", frames = " + IntToStr(i_N_TX) +
  1410.                       ", dac value = " + IntToStr( us_init_value )
  1411.                     );
  1412.                     return false;
  1413.                 }
  1414.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1415.                       "  Target< ARFCN = " + IntToStr( sARFCN ) +
  1416.                       ", TSC = " + IntToStr( tsc ) +
  1417.                       ", power = " + IntToStr( i_CRYSTAL_AFC_PCL ) +
  1418.                       ", frames = " + IntToStr(i_N_TX) +
  1419.                       ", dac value = " + IntToStr( us_init_value )
  1420.                     );
  1421.                 is_suspend_cal = true;
  1422.                 this->Suspend();
  1423.                 is_suspend_cal = false;
  1424.                 if (! MF_rf_tx_level_cnf )
  1425.                 {
  1426.                     CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_TX_LEVEL_FAIL );
  1427.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1428.                       " FAIL: TX level (! MF_rf_tx_level_cnf ) ."
  1429.                     );
  1430.                     return false;
  1431.                 }
  1432.             while( FreqErr_norm_last * FreqErr_norm >=0 && count<m_sCRYSTAL_AFC_RECURSIVE_TIMES )
  1433.             {
  1434.                 Sleep(100);
  1435.                 if( frmFatcory->rbAgilent8960->Checked )
  1436.                 {
  1437.                     if( ! FetchAverageFreqErr( FreqErr )  )
  1438.                     {
  1439.                         return false;
  1440.                     }
  1441.                 }
  1442.                 else
  1443.                 {
  1444.                     if( ! FetchSigAverageFreqErr( FreqErr )  )
  1445.                     {
  1446.                         return false;
  1447.                     }
  1448.                 }
  1449.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1450.                     " Freq error = " + Double_To_AnsiString(FreqErr)
  1451.                 );
  1452.                 FreqErr_norm_last = FreqErr_norm;
  1453.                 FreqErr_norm = FreqErr * MF_rf_afc_ptr->Get_Afc_Slope_factor( band_idx );
  1454.                 if( count == 0 || count == 1 )
  1455.                 {
  1456.                     FreqErr_norm_last = 0;
  1457.                 }
  1458.                 if( count == 0 )
  1459.                 {
  1460.                     S_AFC_RESULT navram_afc;
  1461.                     MF_rf_afc_ptr->Get_AfcResult(navram_afc);
  1462.                     tx_afc_offset = (short) (0 - FreqErr_norm) / (1.0*RF_AFC_SLOPE_DENOMINATOR/navram_afc.i_AfcSlopeInv);
  1463.                     tx_afc_offset_last = tx_afc_offset;
  1464.                 }
  1465.                 else
  1466.                 {
  1467.                     tx_afc_offset_last = tx_afc_offset;
  1468.                     if( FreqErr_norm >= 0 )
  1469.                     {
  1470.                         tx_afc_offset--;
  1471.                     }
  1472.                     else
  1473.                     {
  1474.                         tx_afc_offset++;
  1475.                     }
  1476.                 }
  1477.                 if( FreqErr_norm_min > Abs_double( FreqErr_norm ) )
  1478.                 {
  1479.                      FreqErr_min       = FreqErr;
  1480.                      tx_afc_offset_min = tx_afc_offset_last;
  1481.                      FreqErr_norm_min  = Abs_double( FreqErr_norm );
  1482.                 }
  1483.                 MF_rf_apc_ptr->Set_TxAfcOffset( band_idx, tx_afc_offset );
  1484.                 //int band_index = FreqBand_To_BandIdx( eFreqBand );
  1485.                 MF_rf_apc_ptr->ConfirmCallback   = ::ccb_write_apc_to_nvram;
  1486.                 MF_rf_apc_ptr->REQ_RfSetRampTable_Start(m_pCal->ui_rf_id, m_pCal->b_TADOSupport, band_idx);
  1487.               //  MF_rf_apc_ptr->ConfirmCallback = ::ccb_write_apc_to_nvram;
  1488.               //  MF_rf_apc_ptr->REQ_Write_APC_To_NVRAM_Single_Band_Start( band_idx, p_cal->b_TADOSupport );
  1489.                 RestartTimerCal( WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL );
  1490.                 SUSPEND_CAL_THREAD
  1491.                 CHECK_TERMINATE_BY_USER
  1492.                 if (MF_rf_apc_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  1493.                 {
  1494.                     CalErrorHandler( WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL );
  1495.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1496.                               " FAIL: Target< set TX AFC offset "  +
  1497.                               " TX AFC offset = " + IntToStr( tx_afc_offset )
  1498.                             );
  1499.                     return false;
  1500.                 }
  1501.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1502.                               " Target< set TX AFC offset  "  +
  1503.                               " TX AFC offset = " + IntToStr( tx_afc_offset )
  1504.                             );
  1505.                 count++;
  1506.             }  //  while( norm )
  1507.             if( count > m_sCRYSTAL_AFC_RECURSIVE_TIMES )
  1508.             {
  1509.                 CalErrorHandler( WM_MF_RF_CRYSTAL_AFC_TX_LEVEL_FAIL );
  1510.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1511.                               " count reach limit"
  1512.                             );
  1513.                 return false;
  1514.             }
  1515.             if( tx_afc_offset != tx_afc_offset_min )
  1516.             {
  1517.                 MF_rf_apc_ptr->Set_TxAfcOffset( band_idx, tx_afc_offset_min );
  1518.                 //int band_index = FreqBand_To_BandIdx( eFreqBand );
  1519.                 MF_rf_apc_ptr->ConfirmCallback   = ::ccb_write_apc_to_nvram;
  1520.                 MF_rf_apc_ptr->REQ_RfSetRampTable_Start(m_pCal->ui_rf_id, m_pCal->b_TADOSupport, band_idx);
  1521.                 //MF_rf_apc_ptr->ConfirmCallback = ::ccb_write_apc_to_nvram;
  1522.                 //MF_rf_apc_ptr->REQ_Write_APC_To_NVRAM_Single_Band_Start( band_idx, m_pCal->b_TADOSupport );
  1523.                 RestartTimerCal( WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL );
  1524.                 SUSPEND_CAL_THREAD
  1525.                 CHECK_TERMINATE_BY_USER
  1526.                 if (MF_rf_apc_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  1527.                 {
  1528.                     CalErrorHandler( WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL );
  1529.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1530.                               " FAIL: Target< set TX AFC offset "  +
  1531.                               " TX AFC offset = " + IntToStr( tx_afc_offset_min )
  1532.                             );
  1533.                     return false;
  1534.                 }
  1535.                 else
  1536.                 {
  1537.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1538.                               " Target< set TX AFC offset "  +
  1539.                               " TX AFC offset = " + IntToStr( tx_afc_offset_min )
  1540.                             );
  1541.                 }
  1542.             }
  1543.             sprintf(tempbuf, "%8.3f", FreqErr_min);
  1544.             m_pCal->as_ResultBuf = m_pCal->as_ResultBuf + tempbuf + ", ";
  1545.             sprintf(tempbuf, "%8d", tx_afc_offset_min);
  1546.             m_pCal->as_ResultBuf = m_pCal->as_ResultBuf + tempbuf + ", ";
  1547.         }
  1548.         Sleep(50);
  1549.         if (!MF_rf_stop.REQ_Start())
  1550.         {
  1551.             CalErrorHandler( WM_MF_RF_STOP_FAIL );
  1552.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1553.                           " FAIL: Target < stop RF fail."
  1554.                         );
  1555.             return false;
  1556.         }
  1557.         
  1558.         WriteAPCCalResultToFile(as_ID+".cal",Application->ExeName, m_pCal->b_CalResultPath, FreqBand_To_BandIdx(eFreqBand));
  1559.         MF_rf_apc_ptr->ConfirmCallback = ::ccb_write_apc_to_nvram;
  1560.         MF_rf_apc_ptr->REQ_Write_APC_To_NVRAM_Single_Band_Start(m_pCal->ui_rf_id, band_idx, m_pCal->b_TADOSupport);
  1561.         RestartTimerCal( WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL );
  1562.         SUSPEND_CAL_THREAD
  1563.         CHECK_TERMINATE_BY_USER
  1564.         if (MF_rf_apc_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  1565.         {
  1566.             CalErrorHandler( WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL );
  1567.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1568.                               " FAIL: Target< write TX AFC offset to NVRAM "
  1569.                             );
  1570.             return false;
  1571.         }
  1572.         else
  1573.         {
  1574.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1575.                               " Target< write TX AFC offset to NVRAM. "
  1576.                             );
  1577.         }
  1578.         eFreqBand++;
  1579.     } // while
  1580.     Sleep(50);
  1581.     if (!(m_pCal->s_CalItem.b_bt_cap_id_cal && (BTMODULE_MT6611 == m_pCal->s_bt_bb_id.id)))
  1582.     {
  1583.         if (!MF_rf_stop.REQ_Start())
  1584.         {
  1585.             CalErrorHandler(WM_MF_RF_STOP_FAIL);
  1586.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1587.                      " FAIL: Target < stop RF fail."
  1588.                      );
  1589.             return false;
  1590.         }
  1591.     }
  1592.     PostMessage(
  1593.                      ctrl.hPostMsgDestHandle,
  1594.                      WM_MF_RF_CRYSTAL_AFC_TRX_OFFSET_CALIBRATION_DONE,
  1595.                      0,
  1596.                      0
  1597.                     );
  1598.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1599.               " ================== AFC TRX offset calibration end ======================= "
  1600.            );
  1601.     return true;
  1602. }
  1603. //===========================================================================
  1604. //----------------------------------------------------------------------------
  1605. int __fastcall T_META_factory_calibration::Get_CRYSTAL_AFC_PCL( AnsiString as_band  )
  1606. {
  1607.     int i_pcl;
  1608.     ViInt16  vi_band = Get_AgeBand( as_CRYSTAL_AFC_BAND );;
  1609.     switch( vi_band )
  1610.     {
  1611.         case age1960_PGSM_BAND:
  1612.         case age1960_EGSM_BAND:
  1613.              i_pcl = i_CRYSTAL_AFC_GSM900_PCL;
  1614.         break;
  1615.         case age1960_DCS1800_BAND:
  1616.              i_pcl = i_CRYSTAL_AFC_DCS1800_PCL;
  1617.         break;
  1618.         case age1960_PCS1900_BAND:
  1619.              i_pcl = i_CRYSTAL_AFC_PCS1900_PCL;
  1620.         break;
  1621.         case age1960_GSM850_BAND:
  1622.              i_pcl = i_CRYSTAL_AFC_GSM850_PCL;
  1623.         break;
  1624.     }
  1625.     return i_pcl;
  1626. }