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

MTK

开发平台:

C++ Builder

  1. /*****************************************************************************
  2. *  Copyright Statement:
  3. *  --------------------
  4. *  This software is protected by Copyright and the information contained
  5. *  herein is confidential. The software may not be copied and the information
  6. *  contained herein may not be used or disclosed except with the written
  7. *  permission of MediaTek Inc. (C) 2005
  8. *
  9. *  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  10. *  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  11. *  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  12. *  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  13. *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  14. *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  15. *  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  16. *  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  17. *  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  18. *  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  19. *  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  20. *  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  21. *
  22. *  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  23. *  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  24. *  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  25. *  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  26. *  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  27. *
  28. *  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  29. *  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  30. *  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  31. *  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  32. *  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  33. *
  34. *****************************************************************************/
  35. /*****************************************************************************
  36.  *
  37.  * Filename:
  38.  * ---------
  39.  *   T_META_factory_calibration.cpp
  40.  *
  41.  * Project:
  42.  * --------
  43.  *   Maui META APP
  44.  *
  45.  * Description:
  46.  * ------------
  47.  *  Factory calibration thread source
  48.  *
  49.  * Author:
  50.  * -------
  51.  *  Andy Ueng (mtk00490)
  52.  *
  53.  *============================================================================
  54.  *             HISTORY
  55.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  56.  *------------------------------------------------------------------------------
  57.  * $Revision$
  58.  * $Modtime$
  59.  * $Log$
  60.  * 
  61.  *------------------------------------------------------------------------------
  62.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  63.  *============================================================================
  64. ****************************************************************************/
  65. #include <Classes.hpp>
  66. #include <assert.h>
  67. #include <math.h>
  68. #include <stdlib.h>
  69. #include "age1960.h"
  70. #include "hp661x.h"
  71. #include "hp663x2.h"
  72. #pragma hdrstop
  73. // common
  74. #ifndef  _METAAPP_COMMON_H_
  75. #include "metaapp_common.h"
  76. #endif
  77. // equipment
  78. #ifndef  _RCT_COMMON_H_
  79. #include "rct_common.h"
  80. #endif
  81. #include "age1968a.h"
  82. #include "cmu200.h"
  83. #ifndef  _AGE_MISC_H_
  84. #include "age_misc.h"
  85. #endif
  86. // misc
  87. #include "math_utils.h"
  88. #include "meta_utils.h"
  89. #include "meta_msg.h"
  90. #include "META_const.h"
  91. #include "GSM_utils.h"
  92. #ifndef  _TIME_UTILS_H_
  93. #include "time_utils.h"
  94. #endif
  95. #ifndef  _MISC_WIFI_H_
  96. #include "misc_wifi.h"
  97. #endif
  98. #include "META_factory_type.h"
  99. #include "META_factory_cfg.h"
  100. // call back
  101. #include "META_factory_RF_cb.h"
  102. #include "META_factory_crystal_cb.h"
  103. #include "META_factory_NVRAM_cb.h"
  104. #ifndef _META_FACTORY_BB_CB_H_
  105. #include "META_factory_bb_cb.h"
  106. #endif
  107. // form
  108. #include "META_Factory.h"
  109. #include "form_Main.h"
  110. #include "T_META_factory_calibration.h"
  111. //////
  112. #include "rf_apc.h"
  113. #include "rf_agc.h"
  114. #include "rf_afc.h"
  115. #include "rf_stop.h"
  116. #include "bb_adc.h"
  117. // file
  118. #include "META_Factory_Result_Handle.h"
  119. #include "META_Factory_CFG_Handle.h"
  120. #ifndef _CAL_COMMON_H_
  121. #include "cal_common.h"
  122. #endif
  123. #ifndef _APCCAL_COMMON_H_
  124. #include "apccal_common.h"
  125. #endif
  126. // BT
  127. #ifndef  _BT_POWER_H_
  128. #include "bt_power.h"
  129. #endif
  130. //------------------------------------------------------------------------------
  131. // phase error calibration
  132. #define BB_TX_NOT_CALIBRATED 0
  133. #define BB_TX_CALIBRATED     1
  134. // timeout value
  135. #define TREE_SENCONDS 3000
  136. #define FIVE_SENCONDS 5000
  137. #define TEN_SENCONDS 10000
  138. #define FIFTEEN_SENCONDS 15000
  139. //---------------------------------------------------------------------------
  140. //   Important: Methods and properties of objects in VCL can only be
  141. //   used in a method called using Synchronize, for example:
  142. //
  143. //      Synchronize(UpdateCaption);
  144. //
  145. //   where UpdateCaption could look like:
  146. //
  147. //      void __fastcall Unit1::UpdateCaption()
  148. //      {
  149. //        Form1->Caption = "Updated in a thread";
  150. //      }
  151. //---------------------------------------------------------------------------
  152. //                {GSM, DCS, PCS}
  153. int PCL_high[3] = { 6,  1,  1};
  154. int PCL_mid[3]  = {12,  8, 13};
  155. int PCL_low[3]  = {17, 13, 13};
  156. //               {low, mid, high}
  157. int PCL_GSM[3] = {17, 12,  6};
  158. int PCL_DCS[3] = {13,  8,  1};
  159. int PCL_PCS[3] = {13,  8,  1};
  160. bool  is_suspend_cal = true;
  161. extern int TimerCalTimeoutID;
  162. int TimeoutInterval = FIFTEEN_SENCONDS;
  163. int ADC_TimeoutInterval = FIVE_SENCONDS;
  164. extern int TimerADCCalTimeoutID;
  165. //------------------------------------------------------------------------------
  166. // common
  167. extern bool is_calibration_start;
  168. // AFC
  169. extern CRFAFC*  MF_rf_afc_ptr;
  170. // crystal AFC
  171. extern CCRYSTALAFC* MF_crystal_afc_ptr;
  172. // RX path loss
  173. extern CRFAGC*  MF_rf_agc_ptr;
  174. // IP2
  175. extern CRFPM*  MF_rf_pm_ptr;
  176. // APC
  177. extern CRFAPC*  MF_rf_apc_ptr;
  178. extern RfNbtx_Req *MF_rf_tx_level_req;
  179. extern BOOL MF_rf_tx_level_cnf;
  180. extern CRFAPC8PSK*  MF_rf_apc_8psk_ptr;
  181. // TX phase error
  182. extern CRFTXIQ*  MF_rf_txiq_ptr;
  183. // ADC
  184. extern CBBADC* MF_BB_ADC_ptr; 
  185. // Theread
  186. //extern T_META_factory_calibration* pt_calibration;
  187. extern bool IS_AgilentPowerSupply_INITIALISE;  // T_Init_Age8960.cpp
  188. //CMU
  189. extern char cmd[1024], buf[1024];
  190. extern unsigned long num;
  191. //------------------------------------------------------------------------------
  192. __fastcall T_META_factory_calibration::T_META_factory_calibration(
  193.                                          bool CreateSuspended,
  194.                                          TYPE_META_FACTORY_CONTROL MF_ctrl,
  195.                                          TYPE_META_FACTORY_ADC_CONTROL MF_ADC_ctrl, // add by Andy Ueng for ADC calibration
  196.                                          TMETA_factory_cfg *MF_cfg,
  197.                                          TStringList *st_Log,
  198.                                          TNotifyEvent ne_onTermSuccess,
  199.                                          TNotifyEvent ne_onTermByUser,
  200.                                          AnsiString as_ID_par,
  201.                                          AnsiString as_TaModel,
  202.                                          S_RCT *p_Rct,
  203.                                          S_BTT *p_Btt,
  204.                                          S_WFT *p_Wft,
  205.                                          S_DCS *p_Dcs,
  206.                                          S_CAL *p_Cal)
  207.         : TThread(CreateSuspended)
  208. {
  209.    ctrl = MF_ctrl;
  210.    ADC_ctrl = MF_ADC_ctrl; // ADC calibration
  211.    cfg = MF_cfg;
  212.    log = st_Log;
  213.    neSuccess = ne_onTermSuccess;
  214.    neByUser = ne_onTermByUser;
  215.    as_ID = as_ID_par;
  216.    as_AgeTaModel = as_TaModel;
  217.    is_calibration_start = true;
  218.    m_pRct = p_Rct;
  219.    m_pBtt = p_Btt;
  220.    m_pWft = p_Wft;
  221.   // rct.age.vi = ctrl.vi;
  222.    m_pDcs = p_Dcs;
  223.    m_pCal = p_Cal;
  224. }
  225. //==============================================================================
  226. void __fastcall T_META_factory_calibration::Execute()
  227. {
  228.     Read_Common_CFG();
  229.     if( m_pCal->s_CalItem.b_wifi_tx_power_cal )
  230.     {
  231.         Read_WiFi_Common_CFG();
  232.     }
  233.     if (!WriteInitValueToNVRAM())
  234.     {
  235.         return;
  236.     }
  237.     if( m_pCal->s_CalItem.b_ip2_cal                     ||
  238.         m_pCal->s_CalItem.b_afc_cal                     ||
  239.         m_pCal->s_CalItem.b_rx_path_loss_cal            ||
  240.         m_pCal->s_CalItem.b_tx_iq_cal                   ||
  241.         m_pCal->s_CalItem.b_tx_pcl_cal
  242.       )
  243.     {
  244.         if( ! m_rct_ctrl.RCT_Init( m_pRct, m_asFreqBank ) )
  245.         {
  246.             CalErrorHandler( WM_MF_AGE8960_INIT_FAIL );
  247.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  248.                       " FAIL: " + m_pRct->as_RCT+ " < Initialize"
  249.                     );
  250.             return;
  251.         }
  252.     }
  253.      // IP2 calibration
  254.     if( m_pCal->s_CalItem.b_ip2_cal )
  255.     {
  256.         Read_IP2_CFG();
  257.         if( ! IP2Cal() )
  258.         {
  259.             return;
  260.         }
  261.     }
  262.     // AFC calibration
  263.     m_sDefault_value = 4100;
  264.     if( m_pCal->s_CalItem.b_afc_cal )
  265.     {
  266.         Read_AFC_CFG();
  267.         if( TCVCXO == m_pCal->e_AFC_type )
  268.         {
  269.             if( ! AFCCal() )
  270.             {
  271.                 return;
  272.             }
  273.         }
  274.         else // crystal
  275.         {
  276.             Read_CrystalAFC_CFG();
  277.             if( ! CrystalAFCCapIDCommon() )
  278.             {
  279.                 return;
  280.             }
  281.             if( ! CrystalAFCCapIDPreCheck() )
  282.             {
  283.                 return;
  284.             }
  285.             if( ! m_bBypassCapIDCal )
  286.             {
  287.                 if( m_bFreqErrIncrease )
  288.                 {
  289.                     if( ! CrystalAFCCapIDCalInc() )
  290.                     {
  291.                         return;
  292.                     }
  293.                 }
  294.                 else
  295.                 {
  296.                     if( ! CrystalAFCCapIDCalDec() )
  297.                     {
  298.                         return;
  299.                     }
  300.                 }
  301.             }
  302.             if( ! m_bBypassCapIDCal )
  303.             {
  304.                 if( ! CrystalAFCCapIDCheck_Normal() )
  305.                 {
  306.                     return;
  307.                 }
  308.             }
  309.             else
  310.             {
  311.                 if( ! CrystalAFCCapIDCheck_BypassCapIDCal() )
  312.                 {
  313.                     return;
  314.                 }
  315.             }
  316.             if (!AFCCal())
  317.             {
  318.                 return;
  319.             }
  320.         }
  321.     }
  322.     // RX path loss calibration
  323.     if( m_pCal->s_CalItem.b_rx_path_loss_cal)
  324.     {
  325.         Read_RxPathLoss_CFG();
  326.             
  327.         if (!RxPathLossCal())
  328.         {
  329.             return;
  330.         }
  331.     }
  332.     //TX DC offset calibration
  333.     if( m_pCal->s_CalItem.b_tx_iq_cal )
  334.     {
  335.         if (!m_pCal->s_CalItem.b_afc_cal             &&
  336.            (AGILENT_8960 == m_pRct->device_type) 
  337.            )
  338.         {
  339.             PostMessage(
  340.                           ctrl.hPostMsgDestHandle,
  341.                           WM_MF_AFC_NOT_CHECK,
  342.                           0,
  343.                           0
  344.                         );
  345.             return;            
  346.         }
  347.         Read_TxIq_CFG();
  348.         if( TX_IQ_GMSK == m_pCal->e_TX_IQ_type )
  349.         {
  350.             if( ! TXIqCal() )
  351.             {
  352.                 return;
  353.             }
  354.         }
  355.         else
  356.         {
  357.             if( ! TXIqCal_EPSK() )
  358.             {
  359.                 return;
  360.             }
  361.         }
  362.     }
  363.     
  364.     // TX PCL calibration
  365.     if (m_pCal->s_CalItem.b_tx_pcl_cal)
  366.     {
  367.         if (!TXPCLCal())
  368.         {
  369.             return;
  370.         }
  371.     }
  372.     // TRX offset calibration
  373.     if (m_pCal->s_CalItem.b_trx_offset_cal)
  374.     {
  375.         if (!m_pCal->s_CalItem.b_afc_cal)
  376.         {
  377.             PostMessage(
  378.                           ctrl.hPostMsgDestHandle,
  379.                           WM_MF_AFC_NOT_CHECK,
  380.                           0,
  381.                           0
  382.                         );
  383.             return;
  384.         }
  385.         if (CMU_200 == m_pRct->device_type)
  386.         {
  387.             if (!m_rct_ctrl.RCT_sig_Init(m_pRct, m_asFreqBank))
  388.             {
  389.                 CalErrorHandler(WM_MF_AGE8960_INIT_FAIL);
  390.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() + " FAIL: CMU 200 < Initialize"
  391.                         );
  392.                 return;
  393.             }
  394.         }
  395.         if (!CrystalTXAfcOffsetCal())
  396.         {
  397.             return;
  398.         }
  399.        // if (!m_rct_ctrl.RCT_Init(m_pRct, m_asFreqBank))
  400.        // {
  401.        //     CalErrorHandler(WM_MF_AGE8960_INIT_FAIL);
  402.        //     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  403.        //              " FAIL: " + m_pRct->as_RCT+ " < Initialize"
  404.        //              );
  405.        //     return;
  406.        // }
  407.     }
  408.     //BT
  409.     if (m_pCal->s_CalItem.b_bt_cap_id_cal)
  410.     {
  411.         if (!m_cBT_POWER_Obj.REQ_Query_Power_On_Start())
  412.         {
  413.             m_cBT_POWER_Obj.REQ_Power_On_Main_BT_Start();
  414.         }
  415.         Read_BT_CMU200_CFG();
  416.         Read_BT_CBT_CFG();
  417.         Read_BT_Common_CFG();
  418.         Read_BT_CrystalCapID_CFG();
  419.         if (BTMODULE_MT6601 == m_pCal->s_bt_bb_id.id)
  420.         {
  421.             if (!m_btt_ctrl.BTT_Init(m_pBtt))
  422.             {
  423.                 PostMessage(ctrl.hPostMsgDestHandle,
  424.                             WM_MF_BTT_INIT_FAIL,
  425.                             0,
  426.                             0
  427.                            );
  428.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  429.                          " BT tester initialize fail "
  430.                         );
  431.                 return;
  432.             }
  433.             if (!m_btt_ctrl.BTT_Config_Downlink_Power(m_pBtt,  m_dBT_DOWNLINK_POWER))
  434.             {
  435.                 PostMessage(ctrl.hPostMsgDestHandle,
  436.                             WM_MF_BTT_CONFIG_DOWNLINK_POWER_FAIL,
  437.                             0,
  438.                             0
  439.                            );
  440.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  441.                          " BT tester < config downlink power fail "
  442.                         );
  443.                 return;
  444.             }
  445.             if (!BTCrystalCapIDCal_MT6601())
  446.             {
  447.                 return;
  448.             }
  449.             if (!BTCrystalCapIDCheck_MT6601())
  450.             {
  451.                 return;
  452.             }
  453.         }
  454.         else if(BTMODULE_MT6611 == m_pCal->s_bt_bb_id.id)
  455.         {
  456.             if (!BTCrystalCapIDCal_MT6611())
  457.             {
  458.                 return;
  459.             }
  460.         }
  461.         if (!MF_rf_stop.REQ_Start())
  462.         {
  463.             CalErrorHandler(WM_MF_RF_STOP_FAIL);
  464.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  465.                          " FAIL: Target < stop RF fail."
  466.                         );
  467.             return;
  468.         }
  469.     }
  470.     // ADC calibration
  471.     if (m_pCal->s_CalItem.b_adc_cal)
  472.     {
  473.         Read_ADC_CFG();
  474.         if( ! ADCCal() )
  475.         {
  476.             return;
  477.         }
  478.     }
  479.     // CV calibration
  480.     if (m_pCal->s_CalItem.b_cv_cal)
  481.     {
  482.         Read_ADC_CFG();
  483.         Read_CV_CFG();
  484.         if (!CVCal_cal())
  485.         {
  486.             return;
  487.         }
  488.         if (!CVCal_check())
  489.         {
  490.             return;
  491.         }
  492.     }
  493.     // WiFi TX DC offset
  494.     if( m_pCal->s_CalItem.b_wifi_tx_dc_offset_cal )
  495.     {
  496.         if( ! WiFiTxDcOffsetCal() )
  497.         {
  498.             return;
  499.         }    
  500.     }
  501.     if( m_pCal->s_CalItem.b_wifi_tx_power_cal )
  502.     {
  503.        // Read_WiFi_Common_CFG();
  504.         Read_WiFi_IQviewInit_CFG();
  505.         Read_WiFi_AgeN4010aInit_CFG();
  506.         Read_WiFi_TxPcl_CFG();
  507.        // if( NULL == m_pWft->iqv.p_handle )
  508.        // {
  509.             if( ! m_wft_ctrl.WFT_Init( m_pWft ) )
  510.             {
  511.                 PostMessage(
  512.                           ctrl.hPostMsgDestHandle,
  513.                           WM_MF_WIFI_INIT_FAIL,
  514.                           0,
  515.                           0
  516.                         );
  517.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  518.                    " WiFi tester initialize fail "
  519.                 );
  520.                 return;
  521.             }
  522.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  523.                    " WiFi tester initialize successfully "
  524.                 );
  525.             if( ! m_wft_ctrl.WFT_ConOpen( m_pWft ) )
  526.             {
  527.                 PostMessage(
  528.                          ctrl.hPostMsgDestHandle,
  529.                          WM_MF_WIFI_CONN_OPEN_FAIL,
  530.                          0,
  531.                          0
  532.                         );
  533.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  534.                       " WiFi tester conncetion open fail "
  535.                     );
  536.                 return;
  537.             }
  538.             if( ! m_wft_ctrl.WFT_ConCalUpdate(  m_pWft  )  )
  539.             {
  540.                 PostMessage(
  541.                          ctrl.hPostMsgDestHandle,
  542.                          WM_MF_WIFI_CONN_UPDATE_CALDATA_FAIL,
  543.                          0,
  544.                          0
  545.                         );
  546.                log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  547.                       " WiFi tester update calibration data fail "
  548.                     );
  549.                return;
  550.            }
  551.            if( ! m_wft_ctrl.WFT_SetDefault( m_pWft )  )
  552.            {
  553.                PostMessage(
  554.                          ctrl.hPostMsgDestHandle,
  555.                          WM_MF_WIFI_SET_DEFAULT_FAIL,
  556.                          0,
  557.                          0
  558.                        );
  559.                log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  560.                       " WiFi tester sets default value fail "
  561.                     );
  562.                return;
  563.             }
  564.             if( IQVIEW == m_pWft->device_type )
  565.             {
  566.                 m_pWft->iqv.p_handle->adc.sampling_time = 0.005;
  567.             m_pWft->iqv.p_handle->adc.sampling_trig_pre_time = 1.0e-6;
  568.             m_pWft->iqv.p_handle->adc.sampling_trig_type = IQV_TRIG_TYPE_IF_NO_CAL;
  569.             m_pWft->iqv.p_handle->select_input_mod = IQV_INPUT_MOD_DAC_RF; //1:IQV_INPUT_MOD_DAC_BB
  570.     m_pWft->iqv.p_handle->select_input_adc = IQV_INPUT_ADC_RF;
  571.         }
  572.         if( ! m_wft_ctrl.WFT_SetTriggerStyle( m_pWft )  )
  573.         {
  574.                PostMessage(
  575.                          ctrl.hPostMsgDestHandle,
  576.                          WM_MF_WIFI_SET_TRIGGER_STYLE_FAIL,
  577.                          0,
  578.                          0
  579.                        );
  580.                log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  581.                       " WiFi tester sets trigger style fail "
  582.                     );
  583.                return;
  584.         }
  585.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  586.                       " WiFi tester sets trigger style successfully "
  587.                     );
  588.         if( ! m_wft_ctrl.WFT_SetPacketLength( m_pWft, 0.0001 )  ) // 100 us
  589.         {
  590.                PostMessage(
  591.                          ctrl.hPostMsgDestHandle,
  592.                          WM_MF_WIFI_SET_PACKET_LEN_FAIL,
  593.                          0,
  594.                          0
  595.                        );
  596.                log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  597.                       " WiFi tester sets default value fail "
  598.                     );
  599.                return;
  600.         }
  601.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  602.                       " WiFi tester sets packet length = 100us "
  603.                     );
  604.         if( ! WiFiTxPclCal_cal() )
  605.         {
  606.             return;
  607.         }
  608.         if( ! WiFiTxPclCal_check() )
  609.         {
  610.             return;
  611.         }
  612.       //  if( ! m_wft_ctrl.WFT_ConClose( m_pWft )  )
  613.       //  {
  614.       //      PostMessage(
  615.       //               ctrl.hPostMsgDestHandle,
  616.       //               WM_MF_WIFI_CONN_CLOSE_FAIL,
  617.       //               0,
  618.       //               0
  619.       //              );
  620.       //      log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  621.       //                " WiFi tester closes connection fail "
  622.       //              );
  623.       //  }
  624.       //  if( ! m_wft_ctrl.WFT_Terminate( m_pWft )  )
  625.        // {
  626.       //      PostMessage(
  627.       //               ctrl.hPostMsgDestHandle,
  628.       //               WM_MF_WIFI_TERMINATE_FAIL,
  629.       //               0,
  630.       //               0
  631.       //              );
  632.       //      log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  633.       //                " WiFi tester terminates fail "
  634.       //              );
  635.       //  }
  636.     }
  637.     this->OnTerminate = neSuccess;
  638. }
  639. //==============================================================================
  640. bool __fastcall T_META_factory_calibration::WriteInitValueToNVRAM(void)
  641. {
  642.     char tempbuf[256];
  643.     log->Add("n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  644.               " =============== Write initial value to flash begin ==============="
  645.             );
  646.     //== write AFC value to NVRAM !! ===========================================
  647.     if (m_pCal->s_CalItem.b_afc_cal)
  648.     {
  649.         MF_rf_afc_ptr->ConfirmCallback = ::ccb_write_afc_to_nvram;
  650.         MF_rf_afc_ptr->REQ_Write_To_NVRAM_Start();
  651.         CHECK_TERMINATE_BY_USER
  652.         RestartTimerCal (WM_MF_NVRAM_AFC_WRITE_FAIL);
  653.         SUSPEND_CAL_THREAD
  654.         E_METAAPP_RESULT_T state = MF_rf_afc_ptr->Get_ConfirmState();
  655.         if (state != METAAPP_SUCCESS)
  656.         {
  657.             if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  658.             {
  659.                 CalErrorHandler(WM_MF_NVRAM_EF_L1_AFCDATA_LID_VERNO_FAIL);
  660.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  661.                              " FAIL : NVRAM_EF_L1_AFCDATA_LID version is not support, please update META to latest version. "
  662.                              );
  663.             }
  664.             else
  665.             {
  666.                 CalErrorHandler(WM_MF_NVRAM_AFC_WRITE_FAIL);
  667.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  668.                                       " FAIL:  Target < Write AFC slope and offset to NVRAM fail "
  669.                                      );
  670.            }
  671.            return false;
  672.        }
  673.        log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  674.                       " Target < Write AFC slope and offset to NVRAM"
  675.                     );
  676.         if (CRYSTAL == m_pCal->e_AFC_type )
  677.         {
  678.             //== write crystal AFC value to NVRAM !! ===============================
  679.             MF_crystal_afc_ptr->ConfirmCallback = ::ccb_write_crystal_afc_to_nvram;
  680.             MF_crystal_afc_ptr->REQ_Write_CAPID_To_NVRAM_Start();
  681.             CHECK_TERMINATE_BY_USER
  682.             RestartTimerCal (WM_MF_NVRAM_CRYSTAL_AFC_CAPID_WRITE_FAIL);
  683.             SUSPEND_CAL_THREAD
  684.             E_METAAPP_RESULT_T state = MF_crystal_afc_ptr->Get_ConfirmState();
  685.             if (state != METAAPP_SUCCESS)
  686.             {
  687.                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  688.                 {
  689.                     CalErrorHandler(WM_MF_NVRAM_EF_L1_CRYSTAL_CAPDATA_LID_VERNO_FAIL);
  690.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  691.                              " FAIL : NVRAM_EF_L1_CRYSTAL_CAPDATA_LID version is not support, please update META to latest version. "
  692.                             );
  693.                 }
  694.                 else
  695.                 {
  696.                     CalErrorHandler(WM_MF_NVRAM_CRYSTAL_AFC_CAPID_WRITE_FAIL);
  697.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  698.                                       " FAIL:  Target < Write crystal AFC CAP ID value to NVRAM fail "
  699.                                      );
  700.                 }
  701.                 return false;
  702.             }
  703.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  704.                       " Target < Write crystal AFC CAP ID value to NVRAM"
  705.                     );
  706.             if (!m_pCal->s_CalItem.b_tx_pcl_cal)
  707.             {
  708.                 // AFC TRX offset
  709.                 MF_rf_apc_ptr->ConfirmCallback = ::ccb_read_apc_from_nvram;
  710.                 MF_rf_apc_ptr->REQ_Read_APC_From_NVRAM_Start(m_pCal->ui_rf_id, m_pCal->b_TADOSupport);
  711.                 RestartTimerCal( WM_MF_NVRAM_LEVEL_RAMP_READ_FAIL );
  712.                 SUSPEND_CAL_THREAD
  713.                 CHECK_TERMINATE_BY_USER
  714.                 if (MF_rf_apc_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  715.                 {
  716.                     CalErrorHandler(WM_MF_NVRAM_LEVEL_RAMP_READ_FAIL);
  717.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  718.                               " FAIL: Target > read GMSK level ramp from NVRAM fail."
  719.                             );
  720.                     return false;
  721.                 }
  722.                 else
  723.                 {
  724.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  725.                              " Target > read GMSK level ramp from NVRAM."
  726.                     );
  727.                 }
  728.                 if (!MF_rf_apc_ptr->REQ_Read_TX_AFC_DAC_offset_From_File(frmFatcory->stIniFile->Caption.c_str()))
  729.                     return false;
  730.                 if ((CRYSTAL == m_pCal->e_AFC_type)    &&
  731.                     m_pCal->s_CalItem.b_trx_offset_cal
  732.                   )
  733.                 {
  734.                     int i=0;
  735.                     for (i=0; i<MAX_SUPPORT_BAND_NUM; i++)
  736.                     {
  737.                         MF_rf_apc_ptr->Set_TxAfcOffset(i, 0);
  738.                     }
  739.                 }
  740.                 MF_rf_apc_ptr->ConfirmCallback = ::ccb_write_apc_to_nvram;
  741.                 MF_rf_apc_ptr->REQ_Write_APC_To_NVRAM_Start(m_pCal->ui_rf_id, m_pCal->b_TADOSupport);
  742.                 RestartTimerCal(WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL);
  743.                 SUSPEND_CAL_THREAD
  744.                 CHECK_TERMINATE_BY_USER
  745.                 E_METAAPP_RESULT_T state = MF_rf_apc_ptr->Get_ConfirmState();
  746.                 if (state != METAAPP_SUCCESS)
  747.                 {
  748.                     if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  749.                     {
  750.                         CalErrorHandler(WM_MF_NVRAM_EF_L1_RAMPTABLE_xxx_LID_VERNO_FAIL);
  751.                         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  752.                              " FAIL : NVRAM_EF_L1_RAMPTABLE_xxx_LID version is not support, please update META to latest version. "
  753.                              );
  754.                     }
  755.                     else
  756.                     {
  757.                         CalErrorHandler(WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL);
  758.                         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  759.                               " FAIL: Target < write GMSK level ramp to NVRAM fail."
  760.                             );
  761.                     }
  762.                     return false;
  763.                 }
  764.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  765.                       " Target < write GMSK level ramp to NVRAM."
  766.                     );
  767.             }
  768.         }
  769.     }
  770.     //== write path loss to NVRAM !! ============================================
  771.     if (frmFatcory->cbRxPathLoss->Checked)
  772.     {
  773.         MF_rf_agc_ptr->ConfirmCallback = ::ccb_write_agc_to_nvram;
  774.         MF_rf_agc_ptr->REQ_Write_To_NVRAM_Start();
  775.         CHECK_TERMINATE_BY_USER
  776.         RestartTimerCal(WM_MF_NVRAM_PATH_LOSS_WRITE_FAIL);
  777.         SUSPEND_CAL_THREAD
  778.         E_METAAPP_RESULT_T state = MF_rf_agc_ptr->Get_ConfirmState();
  779.         if (state != METAAPP_SUCCESS)
  780.         {
  781.             if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  782.             {
  783.                 CalErrorHandler(WM_MF_NVRAM_EF_L1_AGCPATHLOSS_LID_VERNO_FAIL);
  784.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  785.                          " FAIL : NVRAM_EF_L1_AGCPATHLOSS_LID version is not support, please update META to latest version. "
  786.                          );
  787.             }
  788.             else
  789.             {
  790.                 CalErrorHandler( WM_MF_NVRAM_PATH_LOSS_WRITE_FAIL );
  791.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  792.                                   " FAIL:  Target < Write path loss to NVRAM "
  793.                                 );
  794.             }
  795.             return false;
  796.         }
  797.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  798.                       " Target < Write path loss to NVRAM "
  799.                     );
  800.     }
  801.     //======================================================================
  802.     // write IP2 to NVRAM
  803.   //  MF_rf_pm_ptr->ConfirmCallback = ccb_write_ip2_to_nvram;
  804.   //  MF_rf_pm_ptr->REQ_Write_IP2Reg_To_NVRAM(final_rfid);
  805.   //  if ( this->Terminated )
  806.   //  {
  807.   //      this->OnTerminate = neByUser;
  808.   //      return false;
  809.   //  }
  810.   //  RestartTimerCal( WM_MF_NVRAM_IP2_WRITE_FAIL );
  811.   //  is_suspend_cal = true;
  812.   //  this->Suspend();
  813.   //  is_suspend_cal = false;
  814.   //  if (MF_rf_pm_ptr->ConfirmState != METAAPP_SUCCESS)
  815.   //  {
  816.   //      CalErrorHandler( WM_MF_NVRAM_IP2_WRITE_FAIL );
  817.   //      pt_calibration->log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  818.   //                                " FAIL:  Target < Write IP2 to NVRAM "
  819.   //                              );
  820.   //      return false;
  821.   //  }
  822.   //  else
  823.   //  {
  824.   //      log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  825.   //                " Target < Write IP2 to NVRAM "
  826.   //               );
  827.   //  }
  828.     //===================== write ramp table  to NVRAM =========================
  829.     if (m_pCal->s_CalItem.b_tx_pcl_cal)
  830.     {
  831.         if (m_pCal->s_CalItem.b_afc_cal && (CRYSTAL == m_pCal->e_AFC_type))
  832.         {
  833.             if (!MF_rf_apc_ptr->REQ_Read_APC_From_File(frmFatcory->stIniFile->Caption.c_str(), m_pCal->ui_rf_id, m_pCal->b_TADOSupport))
  834.             {
  835.                 return false;
  836.             }    
  837.         }
  838.         if (//m_pCal->s_CalItem.b_afc_cal                      &&
  839.             (CRYSTAL == m_pCal->e_AFC_type)                  &&
  840.             m_pCal->s_CalItem.b_trx_offset_cal
  841.            )
  842.         {
  843.             int i=0;
  844.             for (i=0; i<MAX_SUPPORT_BAND_NUM; i++)
  845.             {
  846.                 MF_rf_apc_ptr->Set_TxAfcOffset(i, 0);
  847.             }
  848.         }
  849.         if (!m_pCal->s_CalItem.b_afc_cal)
  850.         {
  851.             MF_rf_apc_ptr->ConfirmCallback = ::ccb_read_apc_from_nvram;
  852.             MF_rf_apc_ptr->REQ_Read_APC_From_NVRAM_Start(m_pCal->ui_rf_id, m_pCal->b_TADOSupport);
  853.             RestartTimerCal(WM_MF_NVRAM_LEVEL_RAMP_READ_FAIL);
  854.             SUSPEND_CAL_THREAD
  855.             CHECK_TERMINATE_BY_USER
  856.             if (MF_rf_apc_ptr->Get_ConfirmState() != METAAPP_SUCCESS)
  857.             {
  858.                 CalErrorHandler(WM_MF_NVRAM_LEVEL_RAMP_READ_FAIL);
  859.                 log->Add(DateToStr(Date()) +  " " + TimeToStr(Time()) +
  860.                          " FAIL: Target > read GMSK level ramp from NVRAM fail."
  861.                         );
  862.                 return false;
  863.             }
  864.             log->Add(DateToStr(Date()) +  " " + TimeToStr(Time()) +
  865.                       " Target > read GMSK level ramp from NVRAM."
  866.                     );
  867.             short tx_afc_offset[4];
  868.             for (int i=0; i<4; i++)
  869.             {
  870.                 tx_afc_offset[i] = MF_rf_apc_ptr->Get_TxAfcOffset(i);
  871.             }
  872.             if (!MF_rf_apc_ptr->REQ_Read_APC_From_File(m_pCal->as_IniFile.c_str(),  m_pCal->ui_rf_id, m_pCal->b_TADOSupport))
  873.             {
  874.                 return false;
  875.             }
  876.             for (int i=0; i<4; i++)
  877.             {
  878.                 MF_rf_apc_ptr->Set_TxAfcOffset(i, tx_afc_offset[i]);
  879.             }
  880.         }
  881.         if (m_pCal->i_APC_mod & APC_MOD_GMSK)
  882.         {
  883.             MF_rf_apc_ptr->ConfirmCallback = ::ccb_write_apc_to_nvram;
  884.             MF_rf_apc_ptr->REQ_Write_APC_To_NVRAM_Start(m_pCal->b_TADOSupport, m_pCal->ui_rf_id);
  885.             RestartTimerCal(WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL);
  886.             SUSPEND_CAL_THREAD
  887.             CHECK_TERMINATE_BY_USER
  888.             E_METAAPP_RESULT_T state = MF_rf_apc_ptr->Get_ConfirmState();
  889.             if (state != METAAPP_SUCCESS)
  890.             {
  891.                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  892.                 {
  893.                     CalErrorHandler(WM_MF_NVRAM_EF_L1_RAMPTABLE_xxx_LID_VERNO_FAIL);
  894.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  895.                              " FAIL : NVRAM_EF_L1_RAMPTABLE_xxx_LID version is not support, please update META to latest version. "
  896.                              );
  897.                 }
  898.                 else
  899.                 {
  900.                     CalErrorHandler(WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL);
  901.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  902.                              " FAIL: Target < write GMSK level ramp to NVRAM fail."
  903.                             );
  904.                 }
  905.                 return false;
  906.             }
  907.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  908.                       " Target < write GMSK level ramp to NVRAM."
  909.                     );
  910.          }
  911.     }
  912.     if (m_pCal->s_CalItem.b_tx_pcl_cal                                    &&
  913.        ((RFMD_3159 == m_pCal->e_PA_type) || (SKY_77316 == m_pCal->e_PA_type) || (SKY_77340 == m_pCal->e_PA_type)) &&
  914.        (APC_MOD_EPSK & m_pCal->i_APC_mod)
  915.        )
  916.     {
  917.         MF_rf_apc_8psk_ptr->ConfirmCallback = ::ccb_write_apc_to_nvram;
  918.         MF_rf_apc_8psk_ptr->REQ_Write_APC_To_NVRAM_Start(m_pCal->ui_rf_id);
  919.         RestartTimerCal(WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL);
  920.         SUSPEND_CAL_THREAD
  921.         CHECK_TERMINATE_BY_USER
  922.         E_METAAPP_RESULT_T state = MF_rf_apc_8psk_ptr->Get_ConfirmState();
  923.         if (state != METAAPP_SUCCESS)
  924.         {
  925.             if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  926.             {
  927.                 CalErrorHandler(WM_MF_NVRAM_EF_L1_EPSK_RAMPTABLE_xxx_LID_VERNO_FAIL);
  928.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  929.                                               " FAIL : NVRAM_EF_L1_EPSK_RAMPTABLE_xxx_LID version is not support, please update META to latest version. "
  930.                                              );
  931.             }
  932.             else
  933.             {
  934.                 CalErrorHandler(WM_MF_NVRAM_LEVEL_RAMP_WRITE_FAIL);
  935.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  936.                       " FAIL: Target < write EPSK level ramp to NVRAM fail."
  937.                     );
  938.             }
  939.             return false;
  940.         }
  941.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  942.                       " Target < write EPSK level ramp to NVRAM."
  943.                     );
  944.     }
  945. #if 0
  946.     //===================== write TXIQ to NVRAM =========================
  947.     if( frmFatcory->cbTxPhaseError->Checked )
  948.     {
  949.         MF_rf_txiq_ptr->ConfirmCallback = ::ccb_write_txiq_to_nvram;
  950.         MF_rf_txiq_ptr->REQ_Write_To_NVRAM_Start();
  951.         //log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  952.         //    " TimerCal->Enabled 5= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  953.         //   );
  954.         RestartTimerCal( WM_MF_FDM_TXIQ_WRITE_FAIL );
  955.         SUSPEND_CAL_THREAD
  956.         CHECK_TERMINATE_BY_USER
  957.         if (MF_rf_txiq_ptr->Get_ConfirmState() != STATE_TXIQ_OK)
  958.         {
  959.             CalErrorHandler( WM_MF_FDM_TXIQ_WRITE_FAIL );
  960.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  961.                       " FAIL: Target < write TXIQ to NVRAM fail."
  962.                     );
  963.             return false;
  964.         }
  965.         else
  966.         {
  967.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  968.                       " Target < write TXIQ to NVRAM."
  969.                     );
  970.         }
  971.     }
  972. #endif
  973.     //======================    write ADC  to NVRAM   =========================
  974.     if (m_pCal->s_CalItem.b_adc_cal)
  975.     {
  976.         MF_BB_ADC_ptr->ConfirmCallback = ccb_write_ADC_to_nvram;
  977.         MF_BB_ADC_ptr->REQ_Write_To_NVRAM_Start();
  978.         RestartTimerCal(WM_MF_NVRAM_ADC_WRITE_FAIL);
  979.         SUSPEND_CAL_THREAD
  980.         CHECK_TERMINATE_BY_USER
  981.         E_METAAPP_RESULT_T state = MF_BB_ADC_ptr->Get_ConfirmState();
  982.         if (state != METAAPP_SUCCESS)
  983.         {
  984.             if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  985.             {
  986.                 CalErrorHandler(WM_MF_NVRAM_EF_ADC_LID_VERNO_FAIL);
  987.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  988.                                " FAIL : NVRAM_EF_ADC_LID version is not support, please update META to latest version. "
  989.                                );
  990.             }
  991.             else
  992.             {
  993.                 CalErrorHandler(WM_MF_NVRAM_ADC_WRITE_FAIL);
  994.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  995.                       " FAIL: Target < write ADC to NVRAM fail."
  996.                     );
  997.             }
  998.             return false;
  999.         }
  1000.         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1001.                  " Target < write ADC to NVRAM."
  1002.                  );
  1003.     }
  1004. #if 0
  1005.     //---------------------------------------------------------------------
  1006.     // write WiFi APC to NVRAM
  1007.     if( m_pCal->s_CalItem.wifi_tx_power_cal )
  1008.     {
  1009.         if( IsWiFiSupported( m_asWiFi_Support, WIFI_802_11B_IDX ) ||
  1010.             IsWiFiSupported( m_asWiFi_Support, WIFI_802_11G_IDX )
  1011.           )
  1012.         {
  1013.             m_pCal->cal_data.MF_wifi_apc_ptr->ConfirmCallback = ::CNF_MF_write_WiFi_APC_to_nvram;
  1014.             m_pCal->cal_data.MF_wifi_apc_ptr->REQ_Write_TxPower2400M_To_NVRAM_Start();
  1015.             RestartTimerCal( WM_MF_NVRAM_WIFI_APC_WRITE_FAIL );
  1016.             SUSPEND_CAL_THREAD
  1017.             CHECK_TERMINATE_BY_USER
  1018.             if( m_pCal->cal_data.MF_wifi_apc_ptr->Get_ConfirmState() != STATE_WIFI_APC_OK )
  1019.             {
  1020.                 CalErrorHandler( WM_MF_NVRAM_WIFI_APC_WRITE_FAIL );
  1021.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1022.                       " FAIL: Target < write WiFi 2.4G TX power to NVRAM fail."
  1023.                     );
  1024.                 return false;
  1025.             }
  1026.             else
  1027.             {
  1028.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1029.                       " Target < write WiFi 2.4G TX power to NVRAM."
  1030.                     );
  1031.             }
  1032.         }
  1033.         if( IsWiFiSupported( m_asWiFi_Support, WIFI_802_11A_IDX ) )
  1034.         {
  1035.             m_pCal->cal_data.MF_wifi_apc_ptr->ConfirmCallback = ::CNF_MF_write_WiFi_APC_to_nvram;
  1036.             m_pCal->cal_data.MF_wifi_apc_ptr->REQ_Write_TxPower5000M_To_NVRAM_Start();
  1037.             RestartTimerCal( WM_MF_NVRAM_WIFI_APC_WRITE_FAIL );
  1038.             SUSPEND_CAL_THREAD
  1039.             CHECK_TERMINATE_BY_USER
  1040.             if( m_pCal->cal_data.MF_wifi_apc_ptr->Get_ConfirmState() != STATE_WIFI_APC_OK )
  1041.             {
  1042.                 CalErrorHandler( WM_MF_NVRAM_WIFI_APC_WRITE_FAIL );
  1043.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1044.                       " FAIL: Target < write WiFi 5G TX power to NVRAM fail."
  1045.                     );
  1046.                 return false;
  1047.             }
  1048.             else
  1049.             {
  1050.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1051.                       " Target < write WiFi 5G TX power to NVRAM."
  1052.                     );
  1053.             }
  1054.         }
  1055.     }
  1056. #endif
  1057.     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1058.               " ================ Write initial value to flash end ================ n "
  1059.             );
  1060.     //== write initail value to flash end  =======================================
  1061.     return  true;
  1062. }
  1063. //******************************************************************************
  1064. //                         IP2 calibration
  1065. //******************************************************************************
  1066. bool __fastcall T_META_factory_calibration::IP2Cal(void)
  1067. {
  1068.     switch (m_pCal->ui_rf_id)
  1069.     {
  1070.         case RF_ID_SKY74045:
  1071.         {
  1072.             if (!IP2Cal_sky74045_cal())
  1073.             {
  1074.                 return false;
  1075.             }
  1076.             if (!IP2Cal_sky74045_check())
  1077.             {
  1078.                 return false;
  1079.             }
  1080.         }
  1081.         break;
  1082.         case RF_ID_SKY74117:
  1083.         case RF_ID_SKY74400:
  1084.         {
  1085.             if (!IP2Cal_sky74117_cal())
  1086.             {
  1087.                 return false;
  1088.             }
  1089.             if (!IP2Cal_sky74117_check())
  1090.             {
  1091.                 return false;
  1092.             }
  1093.         }
  1094.         break;
  1095.        // case RF_ID_MT6139B:
  1096.        // {
  1097.        //      if( ! IP2Cal_mt6139b_cal() )     return false;
  1098.        //      if( ! IP2Cal_mt6139_check() )   return false;
  1099.        // }
  1100.        // break;
  1101.        // case RF_ID_MT6139C:
  1102. //        case RF_ID_MT6139E:
  1103.        // case RF_ID_MT6140A:
  1104.        // case RF_ID_MT6140B:
  1105.       //  {
  1106.       //       if( ! IP2Cal_mt6139c_cal() )     return false;
  1107.       //       if( ! IP2Cal_mt6139_check() )   return false;
  1108.        // }
  1109.        // break;
  1110.     }
  1111.     return true;
  1112. }
  1113. //***********************************************************************************
  1114. //                                TX level calibration
  1115. //***********************************************************************************
  1116. bool __fastcall T_META_factory_calibration::TXPCLCal(void)
  1117. {
  1118.     PostMessage(
  1119.                      ctrl.hPostMsgDestHandle,
  1120.                      WM_MF_RF_TX_LEVEL_BEGIN,
  1121.                      0,
  1122.                      0
  1123.                     );
  1124.     Read_TXPCL_CFG();
  1125.     if (((SKY_77316 == m_pCal->e_PA_type) || (SKY_77340 == m_pCal->e_PA_type) || (RFMD_3159 == m_pCal->e_PA_type))  &&
  1126.         (m_pCal->i_APC_mod & APC_MOD_EPSK)
  1127.        )
  1128.     {
  1129.         Read_EPSK_TXPCL_CFG();
  1130.     }
  1131.     switch (m_pCal->e_PA_type)
  1132.     {
  1133.         case RFMD_3140: // RFMD 3140
  1134.         {
  1135.             if (THREE_PCL == m_pCal->e_GMSK_APC_type)
  1136.             {
  1137.                 if (!TXPCLCal_3cal())
  1138.                 {
  1139.                     return false;
  1140.                 }
  1141.                 if (!TXPCLCal_RFMD3140_3PCLEvalDac())
  1142.                 {
  1143.                     return false;
  1144.                 }
  1145.             }
  1146.             else if (FULL_PCL == m_pCal->e_GMSK_APC_type)
  1147.             {
  1148.                 if (!TXPCLCal_FullCal())
  1149.                 {
  1150.                     return false;
  1151.                 }
  1152.             }
  1153.         }
  1154.         break;
  1155.         case RFMD_3159:
  1156.         case SKY_77340:
  1157.         {
  1158.             if (m_pCal->i_APC_mod & APC_MOD_GMSK)
  1159.             {
  1160.                 if (THREE_PCL == m_pCal->e_GMSK_APC_type)
  1161.                 {
  1162.                     if (! TXPCLCal_3cal())
  1163.                     {
  1164.                         return false;
  1165.                     }
  1166.                     if (!TXPCLCal_RFMD3140_3PCLEvalDac())
  1167.                     {
  1168.                         return false;
  1169.                     }
  1170.                 }
  1171.                 else if (FULL_PCL == m_pCal->e_GMSK_APC_type)
  1172.                 {
  1173.                     if (!TXPCLCal_FullCal())
  1174.                     {
  1175.                         return false;
  1176.                     }
  1177.                 }
  1178.             }
  1179.             if (m_pCal->i_APC_mod & APC_MOD_EPSK)
  1180.             {
  1181.                 if (RF_ID_A60111A == m_pCal->ui_rf_id)
  1182.                 {
  1183.                     if (!m_RF_A60111A_REG_Obj.Enter_Factory_Cal())
  1184.                     {
  1185.                         CalErrorHandler(WM_MF_RF_A60111A_ENTER_FACTORY_MODE_FAIL);
  1186.                         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1187.                                  " FAIL : Target < A60111A enter test mode "
  1188.                                 );
  1189.                         return false;
  1190.                     }
  1191.                 }
  1192.                 switch (m_pCal->e_EPSK_APC_type)
  1193.                 {
  1194.                     case ONE_PCL:
  1195.                     {
  1196.                         if (!TXPCLCal_EPSK_1cal())
  1197.                         {
  1198.                             return false;
  1199.                         }
  1200.                         if (!TXPCLCal_EvalDac_A60111A_EPSK())
  1201.                         {
  1202.                             return false;
  1203.                         }
  1204.                     }
  1205.                     break;
  1206.                     case FOUR_PCL:
  1207.                     {
  1208.                         if (!TXPCLCal_EPSK_4cal())
  1209.                         {
  1210.                             return false;
  1211.                         }
  1212.                         if (!TXPCLCal_EvalDac_RFMD3159_FixedPaBias())
  1213.                         {
  1214.                             return false;
  1215.                         }
  1216.                     }
  1217.                     break;
  1218.                     case FULL_PCL:
  1219.                     {
  1220.                         if (RF_ID_A60111A == m_pCal->ui_rf_id)
  1221.                         {
  1222.                             if (!TXPCLCal_EPSK_FullCal_A60111A())
  1223.                             {
  1224.                                 return false;
  1225.                             }
  1226.                         }
  1227.                         else
  1228.                         {
  1229.                             if (!TXPCLCal_EPSK_FullCal())
  1230.                             {
  1231.                                 return false;
  1232.                             }
  1233.                         }
  1234.                     }
  1235.                     break;
  1236.                 }
  1237.                 if (RF_ID_A60111A == m_pCal->ui_rf_id)
  1238.                 {
  1239.                     if (!m_RF_A60111A_REG_Obj.Read_CW26())
  1240.                     {
  1241.                         CalErrorHandler(WM_MF_RF_A60111A_READ_REF_TEMP_FAIL);
  1242.                         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1243.                                  " FAIL : Target > read ref_temp from register "
  1244.                                 );
  1245.                         return false;
  1246.                     }
  1247.                     m_RF_A60111A_REG_Obj.ConfirmCallback = ::CNF_MF;
  1248.                     m_RF_A60111A_REG_Obj.REQ_Write_To_NVRAM_Start();
  1249.                     RestartTimerCal(WM_MF_NVRAM_REF_TEMP_WRITE_FAIL);
  1250.                     SUSPEND_CAL_THREAD
  1251.                     CHECK_TERMINATE_BY_USER
  1252.                     if (m_RF_A60111A_REG_Obj.Get_ConfirmState() != METAAPP_SUCCESS)
  1253.                     {
  1254.                         CalErrorHandler(WM_MF_NVRAM_REF_TEMP_WRITE_FAIL);
  1255.                         log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1256.                                  " FAIL: Target < write ref_temp to NVRAM fail."
  1257.                                 );
  1258.                         return false;
  1259.                     }
  1260.                     unsigned char us_reftemp = m_RF_A60111A_REG_Obj.Get_RefTemp();
  1261.                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1262.                                  " Target < write ref_temp = " + IntToStr(us_reftemp) + " to NVRAM"
  1263.                                 );
  1264.                 }
  1265.             }
  1266.         }
  1267.         break;
  1268.         case SKY_77328:
  1269.         case SKY_77506:
  1270.         {
  1271.             if (TWO_PCL == m_pCal->e_GMSK_APC_type)
  1272.             {
  1273.                 if (!TXPCLCal_CalApcDcOffset_Skyworks77328())
  1274.                 {
  1275.                     return false;
  1276.                 }
  1277.                 if (!TXPCLCal_2cal())
  1278.                 {
  1279.                     return false;
  1280.                 }
  1281.                 if (!TXPCLCal_EvalDac_Skyworks77328())
  1282.                 {
  1283.                     return false;
  1284.                 }
  1285.             }
  1286.             else if (FULL_PCL == m_pCal->e_GMSK_APC_type)
  1287.             {
  1288.                 if (!TXPCLCal_CalApcDcOffset_Skyworks77328())
  1289.                 {
  1290.                     return false;
  1291.                 }
  1292.                 if (!TXPCLCal_FullCal())
  1293.                 {
  1294.                     return false;
  1295.                 }
  1296.             }
  1297.         }
  1298.         break;
  1299.         case SKY_77316:
  1300.         {
  1301.             if (m_pCal->i_APC_mod & APC_MOD_GMSK)
  1302.             {
  1303.                 if (TWO_PCL == m_pCal->e_GMSK_APC_type)
  1304.                 {
  1305.                     if (!TXPCLCal_2cal())
  1306.                     {
  1307.                         return false;
  1308.                     }
  1309.                     if (!TXPCLCal_EvalDac_Skyworks77316_GMSK())
  1310.                     {
  1311.                         return false;
  1312.                     }
  1313.                 }
  1314.                 else if (FULL_PCL == m_pCal->e_GMSK_APC_type)
  1315.                 {
  1316.                     if (!TXPCLCal_FullCal())
  1317.                     {
  1318.                         return false;
  1319.                     }
  1320.                 }
  1321.             }
  1322.             if (m_pCal->i_APC_mod & APC_MOD_EPSK)
  1323.             {
  1324.                 if (TWO_PCL == m_pCal->e_EPSK_APC_type)
  1325.                 {
  1326.                     if (!TXPCLCal_EPSK_2cal())
  1327.                     {
  1328.                         return false;
  1329.                     }
  1330.                     if (!TXPCLCal_EvalDac_Skyworks77316_EPSK())
  1331.                     {
  1332.                         return false;
  1333.                     }
  1334.                 }
  1335.                 else if (FULL_PCL == m_pCal->e_EPSK_APC_type)
  1336.                 {
  1337.                     if (!TXPCLCal_EPSK_FullCal())
  1338.                     {
  1339.                         return false;
  1340.                     }
  1341.                 }
  1342.             }
  1343.         }
  1344.         break;
  1345.         case RENESAS_PF08155B:
  1346.         {
  1347.             if (THREE_PCL == m_pCal->e_GMSK_APC_type)
  1348.             {
  1349.                 if (!TXPCLCal_3cal())
  1350.                 {
  1351.                     return false;
  1352.                 }
  1353.                 if (!TXPCLCal_EvalDac_RenesasPF08155B())
  1354.                 {
  1355.                     return false;
  1356.                 }
  1357.             }
  1358.             else if (FULL_PCL == m_pCal->e_GMSK_APC_type)
  1359.             {
  1360.                 if (!TXPCLCal_FullCal())
  1361.                 {
  1362.                     return false;
  1363.                 }
  1364.             }
  1365.         }
  1366.         break;
  1367.         // case RENESAS_PF09016B:
  1368.         // {
  1369.         //     if( THREE_PCL == m_pCal->e_AFC_type )
  1370.         //     {
  1371.         //         if( ! TXPCLCal_3cal() )
  1372.         //         {
  1373.         //             return false;
  1374.         //         }
  1375.         //         if(! TXPCLCal_EvalDac_RenesasPF09016B() )
  1376.         //         {
  1377.         //             return false;
  1378.         //         }
  1379.         //     }
  1380.         // }
  1381.         // break;
  1382.         case AXIOM_AX502:
  1383.         {
  1384.             if (SIX_DAC == m_pCal->e_GMSK_APC_type)
  1385.             {
  1386.                 if (!TXPCLCal_6cal_DAC())
  1387.                 {
  1388.                     return false;
  1389.                 }
  1390.                 if (!TXPCLCal_Axiom502_3PCLEvalDac())
  1391.                 {
  1392.                     return false;
  1393.                 }
  1394.             }
  1395.             else if (FULL_PCL == m_pCal->e_GMSK_APC_type)
  1396.             {
  1397.                 if (!TXPCLCal_FullCal())
  1398.                 {
  1399.                     return false;
  1400.                 }
  1401.             }
  1402.         }
  1403.         break;
  1404.         case TRIQUINT_TQM6M5001:
  1405.         {
  1406.             if (THREE_PCL == m_pCal->e_GMSK_APC_type)
  1407.             {
  1408.                 if (!TXPCLCal_3cal())
  1409.                 {
  1410.                     return false;
  1411.                 }
  1412.                 if (!TXPCLCal_RFMD3140_3PCLEvalDac())
  1413.                 {
  1414.                     return false;
  1415.                 }
  1416.             }
  1417.             else if (FULL_PCL == m_pCal->e_GMSK_APC_type)
  1418.             {
  1419.                 if (!TXPCLCal_FullCal())
  1420.                 {
  1421.                     return false;
  1422.                 }
  1423.             }
  1424.             if (m_pCal->i_APC_mod & APC_MOD_EPSK)
  1425.             {
  1426.                 if (TWO_PCL == m_pCal->e_EPSK_APC_type)
  1427.                 {
  1428.                     if (!TXPCLCal_EPSK_2cal())
  1429.                     {
  1430.                         return false;
  1431.                     }
  1432.                     if (!TXPCLCal_EvalDac_Skyworks77316_EPSK())
  1433.                     {
  1434.                         return false;
  1435.                     }
  1436.                 }
  1437.                 else if (FULL_PCL == m_pCal->e_EPSK_APC_type)
  1438.                 {
  1439.                     if (!TXPCLCal_EPSK_FullCal())
  1440.                     {
  1441.                         return false;
  1442.                     }
  1443.                 }
  1444.             }
  1445.         }
  1446.         break;
  1447.     } // switch()
  1448.     if (m_asAPC_Check.AnsiCompareIC("yes") == 0)
  1449.     {
  1450.         if ((SKY_77316 != m_pCal->e_PA_type) &&
  1451.             (SKY_77340 != m_pCal->e_PA_type) &&
  1452.             (RFMD_3159 != m_pCal->e_PA_type)
  1453.           )
  1454.         {
  1455.             if (!TXPCLCal_check())
  1456.             {
  1457.                 return false;
  1458.             }
  1459.             PostMessage(
  1460.                          ctrl.hPostMsgDestHandle,
  1461.                          WM_MF_RF_TX_LEVEL_CALIBRATION_DONE,
  1462.                          0,
  1463.                          0
  1464.                        );
  1465.             log->Add( "n" + DateToStr(Date()) + " "+ CurrentTimeStr() +
  1466.                " TX PCL calibration done! n"
  1467.             );
  1468.         }
  1469.         else // skyworks 77316
  1470.         {
  1471.             bool gmsk_check_result = true;
  1472.             bool epsk_check_result = true;
  1473.             if (m_pCal->i_APC_mod & APC_MOD_GMSK)
  1474.             {
  1475.                 gmsk_check_result = TXPCLCal_check();
  1476.                 if (!gmsk_check_result)
  1477.                 {
  1478.                     return false;
  1479.                 }
  1480.             }
  1481.             if (m_pCal->i_APC_mod & APC_MOD_EPSK)
  1482.             {
  1483.                 epsk_check_result = TXPCLCal_EPSK_check();
  1484.                 if (!epsk_check_result)
  1485.                 {
  1486.                     return false;
  1487.                 }
  1488.             }
  1489.             PostMessage(
  1490.                           ctrl.hPostMsgDestHandle,
  1491.                           WM_MF_RF_TX_LEVEL_CALIBRATION_DONE,
  1492.                           0,
  1493.                           0
  1494.                        );
  1495.             log->Add( "n" + DateToStr(Date()) + " "+ CurrentTimeStr() +
  1496.                       " TX PCL calibration done! n"
  1497.             );
  1498.         }
  1499.     }
  1500.     return true;
  1501. }
  1502. ////////////////////////////////// end of TX level calibration /////////////////////////////////////
  1503. //=========================================================================================
  1504. __fastcall T_META_factory_calibration::~T_META_factory_calibration( void )
  1505.  {
  1506.  }
  1507. //------------------------------------------------------------------------------
  1508. bool __fastcall T_META_factory_calibration::RestartTimerADCCal( int TimeoutID )
  1509. {
  1510.    TimerADCCalTimeoutID = TimeoutID;
  1511.    frmFatcory->TimerADCCal->Interval = ADC_TimeoutInterval;
  1512.    frmFatcory->TimerADCCal->Enabled = false;
  1513.    frmFatcory->TimerADCCal->Enabled = true;
  1514.    return true;
  1515. }
  1516. //------------------------------------------------------------------------------
  1517. bool __fastcall T_META_factory_calibration::RestartTimerCal( int TimeoutID )
  1518. {
  1519.    TimerCalTimeoutID = TimeoutID;
  1520.    frmFatcory->TimerCal->Interval = TimeoutInterval;
  1521.    frmFatcory->TimerCal->Enabled = false;
  1522.    frmFatcory->TimerCal->Enabled = true;
  1523.    return true;
  1524. }
  1525. //==============================================================================
  1526. void T_META_factory_calibration::PostErrorMsg( ErrorMsg error_msg )
  1527. {
  1528.      PostMessage(
  1529.                      ctrl.hPostMsgDestHandle,
  1530.                      error_msg,
  1531.                      0,
  1532.                      0
  1533.                     );
  1534. }
  1535. //------------------------------------------------------------------------------
  1536. //==============================================================================
  1537. void T_META_factory_calibration::CalErrorHandler( ErrorMsg error_msg )
  1538. {
  1539.      frmFatcory->DisableAllCalTimer();
  1540.      PostMessage(
  1541.                      ctrl.hPostMsgDestHandle,
  1542.                      error_msg,
  1543.                      0,
  1544.                      0
  1545.                     );
  1546. }