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

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.  *   Crystal_Sweep_TX.cpp
  40.  *
  41.  * Project:
  42.  * --------
  43.  *   Maui META APP
  44.  *
  45.  * Description:
  46.  * ------------
  47.  *  Crystal sweep tx 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. //---------------------------------------------------------------------------
  66. #include <vcl.h>
  67. #pragma hdrstop
  68. #include "form_RFTool.h"
  69. #include "Crystal_Sweep_TX.h"
  70. #include "meta_utils.h"
  71. #include "Crystal_INIFileHandle.h"
  72. #include "man_dll.h"
  73. // equipment
  74. #include "ageCommon.h"
  75. #include "age1968A.h"
  76. #include "cmu200.h"
  77. #ifndef  _RCT_COMMON_H_
  78. #include "rct_common.h"
  79. #endif
  80. #ifndef  _CTS_CTRL_H_
  81. #include "cts_ctrl.h"
  82. #endif
  83. // misc
  84. #ifndef  _BAND_UTILS_H_
  85. #include "band_utils.h"
  86. #endif
  87. //---------------------------------------------------------------------------
  88. #define ONE_SECOND 1
  89. //------------------------------------------------------------------------------
  90. static CMU_VAR cmu_crystal;
  91. static ViInt16 vi_Band_Crystal;
  92. //==============================================================================
  93. static void cb_set_CAPID(void)
  94. {
  95.     if (frmRFTool->pt_Age8960_Crystal_Sweep_TX != NULL)
  96.     {
  97.         frmRFTool->pt_Age8960_Crystal_Sweep_TX->Resume();
  98.     }
  99. }
  100. //----------------------------------------------------------------------------
  101. static void cb_read_CrystalAFC_from_nvram_tx(void)
  102. {
  103.     if (frmRFTool->pt_Age8960_Crystal_Sweep_TX != NULL)
  104.     {
  105.         frmRFTool->pt_Age8960_Crystal_Sweep_TX->Resume();
  106.     }
  107. }
  108. //------------------------------------------------------------------------------
  109. static void cb_write_CrystalAFC_to_nvram_tx( void )
  110. {
  111.     if(frmRFTool->pt_Age8960_Crystal_Sweep_TX != NULL )
  112.     {
  113.         frmRFTool->pt_Age8960_Crystal_Sweep_TX->Resume();
  114.     }
  115. }
  116. //------------------------------------------------------------------------------
  117. void __stdcall cb_rf_tx_level_tx(const unsigned char cnf, const short token, void *usrData)
  118. {
  119.      if(frmRFTool->pt_Age8960_Crystal_Sweep_TX != NULL )
  120.      {
  121.         frmRFTool->pt_Age8960_Crystal_Sweep_TX->Resume();
  122.      }
  123. }
  124. //------------------------------------------------------------------------------
  125. static void cb_rf_stop_tx( void )
  126. {
  127.     if(frmRFTool->pt_Age8960_Crystal_Sweep_TX != NULL )
  128.     {
  129.         frmRFTool->pt_Age8960_Crystal_Sweep_TX->Resume();
  130.     }
  131. }
  132. //==============================================================================
  133. void _fastcall Crystal_Sweep_TX::ShowMsg()
  134. {
  135.     ShowMessage( as_Msg );
  136. }
  137. //------------------------------------------------------------------------------
  138. void _fastcall Crystal_Sweep_TX::SyncShowMsg(AnsiString as)
  139. {
  140.     as_Msg = as;
  141.     Synchronize( ShowMsg );
  142. }
  143. //=========================================================================================
  144. __fastcall Crystal_Sweep_TX::Crystal_Sweep_TX(bool CreateSuspended, S_CAS_T *cas): TThread(CreateSuspended)
  145. {
  146.     CAS = cas;
  147. }
  148. //---------------------------------------------------------------------------
  149. void __fastcall Crystal_Sweep_TX::Execute()
  150. {
  151.     ViInt16 vi_Band;
  152.     m_rct.device_type = CAS->e_device_type;
  153.     if (CAS->e_device_type == AGILENT_8960 )
  154.     {
  155.         if((! CAS->crystal_cfg->getGSM400_CableLoss( m_rct.age.d400 )) ||
  156.            (! CAS->crystal_cfg->getGSM850_CableLoss( m_rct.age.d850 )) ||
  157.            (! CAS->crystal_cfg->getGSM900_CableLoss( m_rct.age.d900 )) ||
  158.            (! CAS->crystal_cfg->getDCS1800_CableLoss( m_rct.age.d1800 )) ||
  159.            (! CAS->crystal_cfg->getPCS1900_CableLoss( m_rct.age.d1900 ))
  160.           )
  161.         {
  162.             SyncShowMsg( " Agilent 8960 read config file (cable loss) fail " );
  163.             return;
  164.         }
  165.         if(! CAS->crystal_cfg->getTimeOut( m_rct.age.TMO ) )
  166.         {
  167.             SyncShowMsg( "Get Time Out from file fail" );
  168.             return;
  169.         }
  170.         if(! CAS->crystal_cfg->getGPIB_Addr( m_rct.age.ADD ) )
  171.         {
  172.             SyncShowMsg( "Get GPIB address from file fail" );
  173.             return;
  174.         }
  175.     }
  176.     else // CMU 200
  177.     {
  178.         if(
  179.              (! CAS->crystal_cfg->getCMU200_INPUT_GSM850_CableLoss( m_rct.cmu.loss_gsm850_i )) ||
  180.              (! CAS->crystal_cfg->getCMU200_INPUT_GSM900_CableLoss( m_rct.cmu.loss_gsm_i    )) ||
  181.              (! CAS->crystal_cfg->getCMU200_INPUT_DCS1800_CableLoss( m_rct.cmu.loss_dcs_i   )) ||
  182.              (! CAS->crystal_cfg->getCMU200_INPUT_PCS1900_CableLoss( m_rct.cmu.loss_pcs_i   ))
  183.           )
  184.         {
  185.              SyncShowMsg(  " CMU200 read config file (input cable loss) fail " );
  186.              return;
  187.         }
  188.         //*************************************************************
  189.         //************************************************************
  190.         if(
  191.                (! CAS->crystal_cfg->getCMU200_OUTPUT_GSM850_CableLoss( m_rct.cmu.loss_gsm850_o )) ||
  192.                (! CAS->crystal_cfg->getCMU200_OUTPUT_GSM900_CableLoss( m_rct.cmu.loss_gsm_o ))    ||
  193.                (! CAS->crystal_cfg->getCMU200_OUTPUT_DCS1800_CableLoss( m_rct.cmu.loss_dcs_o ))   ||
  194.                (! CAS->crystal_cfg->getCMU200_OUTPUT_PCS1900_CableLoss( m_rct.cmu.loss_pcs_o ))
  195.                                                       )
  196.         {
  197.              SyncShowMsg(  " CMU200 read config file (output cable loss) fail " );
  198.              return;
  199.         }
  200.         if(! CAS->crystal_cfg->getCMU200_TimeOut( m_rct.cmu.TMO ) )
  201.         {
  202.             SyncShowMsg( "Get Time Out from file fail" );
  203.             return;
  204.         }
  205.         if(! CAS->crystal_cfg->getCMU200_GPIB_Addr( m_rct.cmu.ADD ) )
  206.         {
  207.             SyncShowMsg( "Get GPIB address from file fail" );
  208.             return;
  209.         }
  210.         m_rct.cmu.ADD = m_rct.cmu.ADD.SubString(0, m_rct.cmu.ADD.Pos("::INSTR")-1);
  211.     }
  212.     if( CAS->b_temp_sweep )
  213.     {
  214.         if( ! m_cts_ctrl.CTS_Init(CAS->com_port) )
  215.         {
  216.             SyncShowMsg( "Initialize CTS fail" );
  217.             return;
  218.         }
  219.   //  if( ! m_cts_ctrl.CTS_Close() )
  220.    // {
  221.    //     SyncShowMsg( "Close CTS fail" );
  222.    ///     return;
  223.    // }
  224.         CAS->hCOM = m_cts_ctrl.CTS_GetComHandle();
  225.         CAS->original_dcb = m_cts_ctrl.Get_OriginalDCB();
  226.         if( ! m_cts_ctrl.CTS_Start() )
  227.         {
  228.             SyncShowMsg( "Start CTS fail" );
  229.             return;
  230.         }
  231.     }
  232.     if( ! m_rct_ctrl.RCT_Init( &m_rct, BandIdx_To_FreqBandString(CAS->e_band) ) )
  233.     {
  234.         SyncShowMsg( "Initialize equipment fail" );
  235.         return;
  236.     }
  237.     if( ! m_rct_ctrl.RCT_operatingMode( &m_rct, OPERATING_MODE_GSM_BCH_TCH ) )
  238.     {
  239.         SyncShowMsg( "Set operation mode fail" );
  240.         return;
  241.     }
  242.     /////////////////////////////////////////////////////////////////////////////
  243.     switch ( CAS->e_band )
  244.     {
  245.          case BANDSEL_GSM900:
  246.          vi_Band = age1960_EGSM_BAND;
  247.             cmu_crystal.BAND="GSM";
  248.             vi_Band_Crystal=CMU_EGSM_BAND;
  249.          break;
  250.          case BANDSEL_DCS1800:
  251.             vi_Band = age1960_DCS1800_BAND;
  252.             cmu_crystal.BAND="DCS";
  253.             vi_Band_Crystal=CMU_DCS1800_BAND;
  254.          break;
  255.          case BANDSEL_PCS1900:
  256.             vi_Band = age1960_PCS1900_BAND;
  257.             cmu_crystal.BAND="PCS";
  258.             vi_Band_Crystal=CMU_PCS1900_BAND;
  259.          break;
  260.          case BANDSEL_GSM850:
  261.             vi_Band = age1960_GSM850_BAND;
  262.             cmu_crystal.BAND="GSM850";
  263.             vi_Band_Crystal=CMU_GSM850_BAND;
  264.          break;
  265.     } // switch
  266.     if( ! m_rct_ctrl.RCT_cellBand( &m_rct, vi_Band) )
  267.     {
  268.         if ( CAS->e_device_type == AGILENT_8960)
  269.         {
  270.             SyncShowMsg( " Agilent 8960 set cell band fail ");
  271.         }
  272.         else
  273.         {
  274.             SyncShowMsg( " CMU 200 set cell band fail ");
  275.         }
  276.         return;
  277.     }
  278.     /////////////////////////////////////////////////////////////////////////////
  279.     if( ! m_rct_ctrl.RCT_TCHARFCN( &m_rct, CAS->s_arfcn) )
  280.     {
  281.         if ( CAS->e_device_type == AGILENT_8960)
  282.         {
  283.             SyncShowMsg( " Agilent 8960 set TCH ARFCN fail " );
  284.         }
  285.         else
  286.         {
  287.             SyncShowMsg( " CMU 200 set TCH ARFCN fail " );
  288.         }
  289.     }
  290.     /////////////////////////////////////////////////////////////////////////////
  291.     if( ! m_rct_ctrl.RCT_ConfigTSC( &m_rct, CAS->c_TSC ) )
  292.     {
  293.         if ( CAS->e_device_type == AGILENT_8960)
  294.         {
  295.             SyncShowMsg( " Agilent 8960 set TSC fail ");
  296.         }
  297.         else
  298.         {
  299.             SyncShowMsg( " CMU 200 set TSC fail ");
  300.         }
  301.         return;
  302.     }
  303.     /////////////////////////////////////////////////////////////////////////////
  304.     if( ! m_rct_ctrl.RCT_MSTXLevel( &m_rct, CAS->pcl ) )
  305.     {
  306.         if ( CAS->e_device_type == AGILENT_8960)
  307.         {
  308.             SyncShowMsg( " Agilent 8960 set PCL fail ");
  309.         }
  310.         else
  311.         {
  312.             SyncShowMsg( " CMU 200 set PCL fail ");
  313.         }
  314.         return;
  315.     }
  316.     /////////////////////////////////////////////////////////////////////////////
  317.     //MeasFcnDone = -999; /* Set to unknown state to start       */
  318.     Sleep(50);
  319.    //_VI_PTR pvi = vi;
  320.     // read xo AFC from NVRAM
  321.     CRYSTAL_AFC_Object.ConfirmCallback = ::cb_read_CrystalAFC_from_nvram_tx;
  322.     CRYSTAL_AFC_Object.REQ_Read_CAPID_From_NVRAM_Start();
  323.     this->Suspend();
  324.     if(CRYSTAL_AFC_Object.Get_ConfirmState() !=  METAAPP_SUCCESS)
  325.     {
  326.         Application->MessageBox( "Execution Failure : read crystal AFC from NVRAM ", "FAILURE", MB_OK );
  327.         return;
  328.     }
  329.     // save header to log
  330.     buf[0]='';
  331.     for( int j=CAS->s_min_afc_dac; j<=CAS->s_max_afc_dac; j=j+CAS->s_afc_dac_step )
  332.     {
  333.         sprintf(buf1, "%15d", j );
  334.         strcat(buf, buf1);
  335.     }
  336.     CAS->st_Log->Add(buf);
  337.     frmRFTool->write_CAS_log();
  338.     if( ! CAS->b_temp_sweep )  CAS->max_temp = CAS->min_temp;
  339.     for(float temp=CAS->min_temp; temp<=CAS->max_temp; temp+=CAS->temp_step)
  340.     {
  341.         if( CAS->b_temp_sweep )
  342.         {
  343.             if( ! m_cts_ctrl.CTS_SetTemp( temp ) )
  344.             {
  345.                 SyncShowMsg( "CTS set temp fail" );
  346.                 return;
  347.             }
  348.             double  current_temp, setting_temp;
  349.             do
  350.             {
  351.                 if ( this->Terminated )
  352.                 {
  353.                     this->OnTerminate = CAS->ne_onTermByUser;
  354.                     return;
  355.                 }
  356.                 if( ! m_cts_ctrl.CTS_GetTemp( current_temp, setting_temp ) )
  357.                 {
  358.                    continue;
  359.                    // SyncShowMsg( "CTS get temp successful" );
  360.                    // break;
  361.                 }
  362.             }
  363.             while(temp != current_temp);    
  364.         }
  365.         for( int i=CAS->min_cap_id; i<=CAS->max_cap_id; i=i+CAS->cap_id_step )
  366.         {
  367.             buf[0]='';
  368.             if( CAS->b_temp_sweep )
  369.             {
  370.                 sprintf(buf1, "%3.1f ", temp );
  371.                 strcat(buf, buf1);
  372.             }
  373.             else
  374.             {
  375.                 sprintf(buf1, "    ");
  376.                 strcat(buf, buf1);
  377.             }
  378.            // CRYSTAL_AFC_Object.Set_cap_id(i);
  379.            // CRYSTAL_AFC_Object.ConfirmCallback = ::cb_write_CrystalAFC_to_nvram_tx;
  380.            // CRYSTAL_AFC_Object.REQ_Write_CAPID_To_NVRAM_Start();
  381.            
  382.             CRYSTAL_AFC_Object.ConfirmCallback = ::cb_set_CAPID;
  383.             CRYSTAL_AFC_Object.REQ_SetCapID_Start(i);
  384.             this->Suspend();
  385.             sprintf(buf0, "%3d", i);
  386.             strcat(buf, buf0);
  387.             if(CRYSTAL_AFC_Object.Get_ConfirmState() !=  METAAPP_SUCCESS)
  388.             {
  389.                 Application->MessageBox( "Execution Failure : write crystal AFC to NVRAM ", "FAILURE", MB_OK );
  390.                 return;
  391.             }
  392.             for( int j=CAS->s_min_afc_dac; j<=CAS->s_max_afc_dac; j=j+CAS->s_afc_dac_step )
  393.             {
  394.                 if ( this->Terminated )
  395.                 {
  396.                     this->OnTerminate = CAS->ne_onTermByUser;
  397.                     return;
  398.                 }
  399.             //sprintf(buf1, "AFC DAC = %d : ", j);
  400.                 Sleep(50);
  401.                 MF_rf_tx_level_req.arfcn = CAS->s_arfcn;
  402.                 MF_rf_tx_level_req.bsic  =  CAS->c_TSC;
  403.                 MF_rf_tx_level_req.power    = CAS->pcl;
  404.                 MF_rf_tx_level_req.frames   = 1000;
  405.                 MF_rf_tx_level_req.dacValue = j;
  406.                 MF_rf_tx_level_req.burstTypeNB = NB_TX_RANDOM_WITH_TSC; // NB burst
  407.                 if ( META_Rf_NB_TX_r( m_META_HANDLE_Obj.Get_MainHandle(),
  408.                     &MF_rf_tx_level_req,
  409.                     ::cb_rf_tx_level_tx,
  410.                     &MF_rf_tx_level_token,
  411. NULL
  412.                            ) != META_SUCCESS)
  413.                 {
  414.                     return;
  415.                 }
  416.                 this->Suspend();
  417.             //*******************************************
  418.             //------------- Read frequency Error--------
  419.             //**********************************************
  420.                 if ( this->Terminated )
  421.                 {
  422.                     this->OnTerminate = CAS->ne_onTermByUser;
  423.                     return;
  424.                 }
  425.               //  if( ! m_rct_ctrl.RCT_initMeasFcn_PFER( &m_rct ) )
  426.               //  {
  427.               //      if ( CAS->device_type == AGILENT_8960)
  428.               //      {
  429.               //          SyncShowMsg( " Agilent 8960 initialize PFER measurement fail ");
  430.              //       }
  431.              //       else
  432.              //       {
  433.              //           SyncShowMsg( " CMU 200 set initialize PFER measurement fail ");
  434.              //       }
  435.              //       return;
  436.              //   }
  437.                 Sleep(50);
  438.                 if( ! m_rct_ctrl.RCT_FetchAvgFreqErr( &m_rct, PFERFreqError ) )
  439.                 {
  440.                     if ( CAS->e_device_type == AGILENT_8960)
  441.                     {
  442.                         SyncShowMsg( " Agilent 8960 fetch average frequency error fail ");
  443.                     }
  444.                     else
  445.                     {
  446.                         SyncShowMsg( " CMU 200 fetch average frequency error fail ");
  447.                     }
  448.                     return;
  449.                 }
  450.                 //******************************************************************************
  451.                 FreqError = PFERFreqError;
  452.                 sprintf(buf1, "%12.4f", FreqError );
  453.                 strcat(buf, buf1);
  454.                 strcat(buf, "   ");
  455.         //strcat(buf, buf2);
  456.             //sprintf(buf, "   " );
  457.             //st_Log_CAS->Add(buf);
  458.                 Sleep(50);
  459.                 MF_rf_stop.REQ_Start();
  460.             }
  461.             CAS->st_Log->Add(buf);
  462.             frmRFTool->write_CAS_log();
  463.         }    
  464.     }
  465.     this->OnTerminate = CAS->ne_onTermSuccess;
  466. }