bb_adc.cpp
上传用户:gelin96
上传日期:2017-01-08
资源大小:20993k
文件大小:19k
- /*****************************************************************************
- * Copyright Statement:
- * --------------------
- * This software is protected by Copyright and the information contained
- * herein is confidential. The software may not be copied and the information
- * contained herein may not be used or disclosed except with the written
- * permission of MediaTek Inc. (C) 2005
- *
- * BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
- * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
- * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
- * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
- * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
- * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
- * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
- * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
- * NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
- * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
- *
- * BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
- * LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
- * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
- * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
- * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
- *
- * THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
- * WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
- * LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
- * RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
- * THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
- *
- *****************************************************************************/
- /*****************************************************************************
- *
- * Filename:
- * ---------
- * bb_adc.cpp
- *
- * Project:
- * --------
- * Maui META APP
- *
- * Description:
- * ------------
- * Baseband ADC source
- *
- * Author:
- * -------
- * Andy Ueng (mtk00490)
- *
- *============================================================================
- * HISTORY
- * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
- *------------------------------------------------------------------------------
- * $Revision$
- * $Modtime$
- * $Log$
- *
- *------------------------------------------------------------------------------
- * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
- *============================================================================
- ****************************************************************************/
- #include <vcl.h>
- #include <IniFiles.hpp>
- #pragma hdrstop
- #ifndef _FORM_MAIN_H_
- #include "form_main.h"
- #endif
- #ifndef _BB_ADC_H_
- #include "bb_adc.h"
- #endif
- #ifndef _MAN_ACTIVE_H_
- #include "man_active.h"
- #endif
- #ifndef _MAN_FDM_H_
- #include "man_fdm.h"
- #endif
- // misc
- #ifndef _MISC_BB_H_
- #include "misc_bb.h"
- #endif
- #ifndef _FT_UTILS_H_
- #include "ft_utils.h"
- #endif
- // NVRAM
- #ifndef _NVRAM_VER_H_
- #include "nvram_ver.h"
- #endif
- //===========================================================================
- static CBBADC* bb_adc_ptr;
- static bool g_bIsRunning = false;
- //===========================================================================
- static void REQ_MeasureADC(void)
- {
- bb_adc_ptr->REQ_MeasureADC();
- }
- //---------------------------------------------------------------------------
- static void __stdcall CNF_MeasureADC(const ADCMeaData_Cnf *cnf, const short token, void *usrData)
- {
- bb_adc_ptr->CNF_MeasureADC(cnf, token, usrData);
- }
- //---------------------------------------------------------------------------
- static void CNF_ReadFromNVRAM(void)
- {
- bb_adc_ptr->CNF_ReadFromNVRAM();
- }
- //---------------------------------------------------------------------------
- static void CNF_WriteToNVRAM(void)
- {
- bb_adc_ptr->CNF_WriteToNVRAM();
- }
- //===========================================================================
- CBBADC::CBBADC(void)
- {
- g_bIsRunning = false;
- ConfirmCallback = 0;
- m_uiAdcBufSize = 0;
- m_pcAdcBuf = 0;
- for (int i=0; i<TOTAL_6218_ADC_NUM; i++)
- {
- m_sAdcData.i_ADCSlope[i] = 1;
- m_sAdcData.i_ADCOffset[i] = 0;
- }
- m_iCurChannel = 0;
- m_iCurIndex = 0;
- m_iADC_Input[0] = 0;
- m_iADC_Input[1] = 0;
- m_fADC_Output[0] = 0;
- m_fADC_Output[1] = 0;
- m_psBBReq_ADC = NULL;
- }
- //---------------------------------------------------------------------------
- CBBADC::~CBBADC()
- {
- g_bIsRunning = false;
- ConfirmCallback = 0;
- if (m_pcAdcBuf)
- {
- delete [] m_pcAdcBuf;
- m_pcAdcBuf = NULL;
- }
- if (m_psBBReq_ADC)
- {
- delete m_psBBReq_ADC;
- m_psBBReq_ADC = NULL;
- }
- }
- //---------------------------------------------------------------------------
- void CBBADC::REQ_Stop(void)
- {
- if (!g_bIsRunning)
- {
- return;
- }
- META_Cancel_r(m_META_HANDLE_Obj.Get_MainHandle(), m_sBBID_ADC);
- Confirm(METAAPP_STOP);
- }
- //---------------------------------------------------------------------------
- void CBBADC::Confirm(E_METAAPP_RESULT_T confirm_state)
- {
- if (!g_bIsRunning)
- {
- return;
- }
- g_bIsRunning = false;
- if (NULL == ConfirmCallback)
- {
- return;
- }
- m_eConfirmState = confirm_state;
- ActiveMan->SetActiveFunction(ConfirmCallback);
- }
- //===========================================================================
- void CBBADC::REQ_Start(int adc_channel, int input, int index, int measure_count)
- {
- bb_adc_ptr = this;
- g_bIsRunning = true;
- m_iCurChannel = adc_channel;
- m_iCurIndex = index;
- m_iMeasureCount = measure_count;
- m_iADC_Input[m_iCurIndex] = input;
- ActiveMan->SetActiveFunction(::REQ_MeasureADC);
- }
- //---------------------------------------------------------------------------
- void CBBADC::REQ_MeasureADC(void)
- {
- if (!g_bIsRunning)
- {
- return;
- }
- if (NULL == m_psBBReq_ADC)
- {
- m_psBBReq_ADC = new ADCMeaData_Req;
- }
- m_psBBReq_ADC->channel = m_iCurChannel;
- m_psBBReq_ADC->Meacount = m_iMeasureCount;
- META_RESULT MetaResult = META_BB_ADCGetMeaSumData_r(m_META_HANDLE_Obj.Get_MainHandle(), m_psBBReq_ADC, ::CNF_MeasureADC, &m_sBBID_ADC, NULL);
- if (MetaResult != META_SUCCESS)
- {
- Confirm(METAAPP_FAIL);
- return;
- }
- }
- //-------------------------------------
- void __stdcall CBBADC::CNF_MeasureADC(const ADCMeaData_Cnf *cnf , const short token, void *usrData)
- {
- if (!g_bIsRunning)
- {
- return;
- }
- m_psBBCnf_ADC = cnf;
- if (cnf->status != BB_STATUS_SUCCESS)
- {
- Confirm(METAAPP_FAIL);
- return;
- }
- m_fADC_Output[m_iCurIndex] = cnf->value/((float)m_psBBReq_ADC->Meacount*1.0);
- Confirm(METAAPP_SUCCESS);
- }
- //===========================================================================
- //////////////////////////////// NVRAM //////////////////////////////////
- //===========================================================================
- bool CBBADC::Decompose_AdcCalTable(void)
- {
- int total_adc_num;
- char adc_slope_str[] = "ADCSlope[0]";
- char adc_offset_str[] = "ADCOffset[0]";
- META_RESULT MetaResult;
- BBCHIP_TYPE bb_chip = frmMainSel->Get_FinalBBChip();
- if (bb_chip == MT6205 || bb_chip == MT6205B)
- {
- total_adc_num = TOTAL_6205B_ADC_NUM;
- }
- else
- {
- total_adc_num = TOTAL_6218_ADC_NUM;
- }
- for (int i=0; i<total_adc_num; i++)
- {
- // slope
- adc_slope_str[9]= '0'+i;
- MetaResult = META_NVRAM_GetRecFieldValue("NVRAM_EF_ADC_LID",
- adc_slope_str,
- m_pcAdcBuf,
- m_uiAdcBufSize,
- &m_sAdcData.i_ADCSlope[i],
- sizeof(m_sAdcData.i_ADCSlope[i])
- );
- if (MetaResult != META_SUCCESS)
- {
- return false;
- }
- adc_offset_str[10]= '0'+i;
- // offset
- MetaResult = META_NVRAM_GetRecFieldValue("NVRAM_EF_ADC_LID",
- adc_offset_str,
- m_pcAdcBuf,
- m_uiAdcBufSize,
- &m_sAdcData.i_ADCOffset[i],
- sizeof(m_sAdcData.i_ADCOffset[i])
- );
- if (MetaResult != META_SUCCESS)
- {
- return false;
- }
- }
- return true;
- }
- //---------------------------------------------------------------------------
- bool CBBADC::Compose_AdcCalTable(void)
- {
- char adc_slope_str[] = "ADCSlope[0]";
- char adc_offset_str[] = "ADCOffset[0]";
- int total_adc_num;
- META_RESULT MetaResult;
- BBCHIP_TYPE bb_chip = frmMainSel->Get_FinalBBChip();
- if ((bb_chip == MT6205) || (bb_chip == MT6205B))
- {
- total_adc_num = TOTAL_6205B_ADC_NUM;
- }
- else
- {
- total_adc_num = TOTAL_6218_ADC_NUM;
- }
- for (int i=0; i<total_adc_num; i++)
- {
- // slope
- adc_slope_str[9]= '0'+i;
- MetaResult = META_NVRAM_SetRecFieldValue( "NVRAM_EF_ADC_LID",
- adc_slope_str,
- m_pcAdcBuf,
- m_uiAdcBufSize,
- &m_sAdcData.i_ADCSlope[i],
- sizeof(m_sAdcData.i_ADCSlope[i]) );
- if (MetaResult != META_SUCCESS)
- {
- return false;
- }
- // offset
- adc_offset_str[10] = '0'+i;
- MetaResult = META_NVRAM_SetRecFieldValue("NVRAM_EF_ADC_LID",
- adc_offset_str,
- m_pcAdcBuf,
- m_uiAdcBufSize,
- &m_sAdcData.i_ADCOffset[i],
- sizeof(m_sAdcData.i_ADCOffset[i])
- );
- if (MetaResult != META_SUCCESS)
- {
- return false;
- }
- }
- return true;
- }
- //===========================================================================
- void CBBADC::REQ_Read_From_NVRAM_Start(void)
- {
- bb_adc_ptr = this;
- g_bIsRunning = true;
- m_bDownloadTrigger = false;
- REQ_Read_From_NVRAM();
- }
- //---------------------------------------------------------------------------
- void CBBADC::REQ_Read_From_NVRAM(void)
- {
- if (!g_bIsRunning)
- {
- return;
- }
- META_RESULT MetaResult;
- if (NULL == m_pcAdcBuf)
- {
- MetaResult = META_NVRAM_GetRecLen("NVRAM_EF_ADC_LID",
- (int *) &m_uiAdcBufSize);
- if (MetaResult != META_SUCCESS)
- {
- Confirm(METAAPP_FAIL);
- return;
- }
- m_pcAdcBuf = new char[m_uiAdcBufSize];
- }
- NVRAMMan->ConfirmCallback = ::CNF_ReadFromNVRAM;
- NVRAMMan->REQ_ReadNVRAM_Start("NVRAM_EF_ADC_LID",
- 1,
- m_uiAdcBufSize,
- m_pcAdcBuf);
- }
- //-------------------------------------
- void CBBADC::CNF_ReadFromNVRAM(void)
- {
- if (!g_bIsRunning)
- {
- return;
- }
- E_METAAPP_RESULT_T state = NVRAMMan->Get_ConfirmState();
- if (METAAPP_SUCCESS == state)
- {
- if (m_bDownloadTrigger)
- {
- REQ_Write_To_NVRAM();
- }
- else
- {
- if (!Decompose_AdcCalTable())
- {
- Confirm(METAAPP_FAIL);
- return;
- }
- Confirm(METAAPP_SUCCESS);
- }
- }
- else
- {
- Confirm(state);
- }
- }
- //===========================================================================
- void CBBADC::REQ_Write_To_NVRAM_Start(void)
- {
- bb_adc_ptr = this;
- g_bIsRunning = true;
- m_bDownloadTrigger = true;
- REQ_Write_To_NVRAM();
- }
- //---------------------------------------------------------------------------
- void CBBADC::REQ_Write_To_NVRAM(void)
- {
- if (!g_bIsRunning)
- {
- return;
- }
- META_RESULT MetaResult;
- unsigned short lid_ver;
- MetaResult = META_NVRAM_GetLIDVersion("NVRAM_EF_ADC_LID", &lid_ver);
- if (lid_ver > NVRAM_EF_ADC_LID_VERNO)
- {
- Confirm(METAAPP_NVRAM_LID_VER_NOT_SUPPORT);
- return;
- }
- if (NULL == m_pcAdcBuf)
- {
- MetaResult = META_NVRAM_GetRecLen("NVRAM_EF_ADC_LID", (int *) &m_uiAdcBufSize);
- if (MetaResult!=META_SUCCESS)
- {
- Confirm(METAAPP_FAIL);
- return;
- }
- m_pcAdcBuf = new char[m_uiAdcBufSize];
- }
- if (!Compose_AdcCalTable())
- {
- Confirm(METAAPP_FAIL);
- return;
- }
- NVRAMMan->ConfirmCallback = ::CNF_WriteToNVRAM;
- NVRAMMan->REQ_WriteNVRAM_Start("NVRAM_EF_ADC_LID",
- 1,
- m_uiAdcBufSize,
- m_pcAdcBuf);
- }
- //-------------------------------------
- void CBBADC::CNF_WriteToNVRAM(void)
- {
- if (!g_bIsRunning)
- {
- return;
- }
- E_METAAPP_RESULT_T state = NVRAMMan->Get_ConfirmState();
- Confirm(state);
- }
- //===========================================================================
- bool CBBADC::AdcSectionExist(char *filename)
- {
- TIniFile *ini_file;
- ini_file = new TIniFile( filename);
- if (ini_file == NULL)
- {
- return false;
- }
- if (ini_file->SectionExists("ADC control"))
- {
- return true;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- bool CBBADC::REQ_Read_From_File(char *filename)
- {
- TIniFile *ini_file;
- AnsiString data;
- char str[2048];
- int idata[10];
- int i;
- int total_adc_num;
- ini_file = new TIniFile( filename );
- if (ini_file == NULL)
- {
- return false;
- }
- BBCHIP_TYPE bb_chip = frmMainSel->Get_FinalBBChip();
- if ((bb_chip == MT6205) || (bb_chip == MT6205B))
- {
- total_adc_num = TOTAL_6205B_ADC_NUM;
- data = ini_file->ReadString("ADC control",
- "offset",
- "0,0,0,0,0" );
- }
- else
- {
- total_adc_num = TOTAL_6218_ADC_NUM;
- data = ini_file->ReadString("ADC control",
- "offset",
- "0,0,0,0,0,0,0" );
- }
- strcpy(str, data.c_str());
- String_To_Array_int(str, idata, total_adc_num);
- for (i=0; i<total_adc_num; i++)
- {
- m_sAdcData.i_ADCOffset[i] = idata[i];
- }
- if ((bb_chip == MT6205) || (bb_chip == MT6205B))
- {
- data = ini_file->ReadString("ADC control",
- "slope",
- "1,1,1,1,1");
- }
- else
- {
- data = ini_file->ReadString("ADC control",
- "slope",
- "1,1,1,1,1,1,1");
- }
- strcpy(str, data.c_str());
- String_To_Array_int(str, idata, total_adc_num);
- for (i=0; i<total_adc_num; i++)
- {
- m_sAdcData.i_ADCSlope[i] = idata[i];
- }
- delete ini_file;
- return true;
- }
- //===========================================================================
- bool CBBADC::REQ_Write_To_File(char *filename)
- {
- TIniFile *ini_file;
- AnsiString data;
- char str[2048];
- int idata[10];
- int total_adc_num;
- ini_file = new TIniFile(filename);
- if (ini_file == NULL)
- {
- return false;
- }
- BBCHIP_TYPE bb_chip = frmMainSel->Get_FinalBBChip();
- if ((bb_chip == MT6205) || (bb_chip == MT6205B))
- {
- total_adc_num = TOTAL_6205B_ADC_NUM;
- }
- else
- {
- total_adc_num = TOTAL_6218_ADC_NUM;
- }
- for (int i=0; i<total_adc_num; i++)
- {
- idata[i] = m_sAdcData.i_ADCOffset[i];
- }
- Array_To_String_int(str, idata, total_adc_num, ',');
- ini_file->WriteString("ADC control",
- "offset",
- str);
- for (int i=0; i<total_adc_num; i++)
- {
- idata[i] = m_sAdcData.i_ADCSlope[i];
- }
- Array_To_String_int(str, idata, total_adc_num, ',');
- ini_file->WriteString("ADC control",
- "slope",
- str);
- delete ini_file;
- return true;
- }
- //===========================================================================
- void CBBADC::Evaluate(void)
- {
- int y0 = m_iADC_Input[0];
- int y1 = m_iADC_Input[1];
- float x0 = m_fADC_Output[0];
- float x1 = m_fADC_Output[1];
- float fdata;
- if (x0-x1 != 0)
- {
- fdata = 1.0*(y1-y0)/(x1-x0);
- fdata += (fdata>=0) ? 0.5 : -0.5 ;
- m_iADC_Slope = (int)fdata;
- fdata = y0 - 1.0*(y1-y0)/(x1-x0)*x0;
- fdata += (fdata>=0) ? 0.5 : -0.5 ;
- m_iADC_Offset = (int)fdata;
- }
- }
- //===========================================================================
- //////////////////////////// Global information ///////////////////////////
- //===========================================================================
- E_METAAPP_RESULT_T CBBADC::Get_ConfirmState(void)
- {
- return m_eConfirmState;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- float CBBADC::Get_ADC_Output(int idx)
- {
- return m_fADC_Output[idx];
- }
- //---------------------------------------------------------------------------
- void CBBADC::Set_ADC_Output(int idx, float adc_output)
- {
- m_fADC_Output[idx] = adc_output;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- bmt_adcdata CBBADC::Get_AdcData(void)
- {
- return m_sAdcData;
- }
- //---------------------------------------------------------------------------
- void CBBADC::Set_AdcData(bmt_adcdata &adc_data)
- {
- m_sAdcData = adc_data;
- }