📄 bspbattdrvr.cpp
字号:
//-----------------------------------------------------------------------------
//
// Copyright (C) 2006, Motorola Inc. All Rights Reserved
//
//-----------------------------------------------------------------------------
//
// Copyright (C) 2006, Freescale Semiconductor, Inc. All Rights Reserved
// THIS SOURCE CODE IS CONFIDENTIAL AND PROPRIETARY AND MAY NOT
// BE USED OR DISTRIBUTED WITHOUT THE WRITTEN PERMISSION OF
// Freescale Semiconductor, Inc.
//
//------------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
// File: bspbattdrvr.cpp
//
// Provides the implementation for BSP-specific battdrvr support.
//
//-----------------------------------------------------------------------------
#include <windows.h>
#include "csp.h"
#include "pmic_lla.h"
#include "pmic_battery.h"
#include "pmic_adc.h"
#include "regs_adc.h"
#include "bspbattdrvr.h"
#include "ioctl_mgn.h"
//-----------------------------------------------------------------------------
// External Functions
//-----------------------------------------------------------------------------
// External Variables
//-----------------------------------------------------------------------------
// Defines
#define BSP_BATTDRVR_DEBUG FALSE
#define MAX_SAFE_VOLTAGE_LEVEL 4 //4.25V
#define MAX_SAFE_CURRENT_LEVEL 11 //1073 mA
#define MIN_VOLTAGE_LEVEL 6 //3.8V
//-----------------------------------------------------------------------------
// Types
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
// Local Variables
static LPTSTR gChargeDetectEventName = TEXT("Charge_Detect");
static HANDLE hIntrEventChargeDetect;
static HANDLE hChargeDetectThread;
static BOOL bChargeDetech = FALSE; // charger attach or removal
static HANDLE hChargeDetechMutex;
//-----------------------------------------------------------------------------
// Local Functions
//-----------------------------------------------------------------------------
//
// Function: BSPBattdrvrGetInfo
//
// This function returns the hardware specific battery information
//
// Parameters:
// pBattInfo
// [out] pointer to the structure containing the battery information
//
// Returns:
// TRUE
//
//-----------------------------------------------------------------------------
BOOL BSPBattdrvrGetInfo(PBATT_INFO pBattInfo)
{
pBattInfo->adc_level = ADC_SAMPLE_MAX_VALUE;
pBattInfo->adc_batt_max_V = ADC_BATT_MAX_VOLTAGE;
pBattInfo->adc_batt_min_V = ADC_BATT_MIN_VOLTAGE;
pBattInfo->adc_batt_max_I = ADC_BATT_MAX_CURRENT;
pBattInfo->adc_batt_min_I = ADC_BATT_MIN_CURRENT;
pBattInfo->adc_charger_max_V = ADC_CHARGER_MAX_VOLTAGE;
pBattInfo->adc_charger_min_V = ADC_CHARGER_MIN_VOLTAGE;
pBattInfo->adc_charger_max_I = ADC_CHARGER_MAX_CURRENT;
pBattInfo->adc_charger_min_I = ADC_CHARGER_MIN_CURRENT;
pBattInfo->charger_V_limit = MC13783_BATTERY_CHRGDET_VOLT_THRESHOLD_HIGH;
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: BSPBattdrvrGetChargingMode
//
// This function returns the whether MC13783 is using the Dual charging mode
//
// Parameters:
// ChargeMode
// [out] pointer to the charge mode, TRUE for dual path charge mode,
// FALSE for else
//
// Returns:
// TRUE if success FALSE if else
//
//-----------------------------------------------------------------------------
BOOL BSPBattdrvrGetChargingMode(BOOL *ChargeMode)
{
PMIC_STATUS status;
CHARGER_MODE mode;
//later need to call pmic batt interface api to get the mode
status = PmicBatteryGetChargerMode(&mode);
if (status != PMIC_SUCCESS)
{
ERRORMSG(TRUE, (TEXT("PmicBatteryGetChargerMode failed\r\n")));
return FALSE;
}
if (mode == DUAL_PATH || mode == DUAL_INPUT_DUAL_PATH)
*ChargeMode = TRUE;
else
*ChargeMode = FALSE;
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: BSPBattdrvrGetSample
//
// This function returns the batt voltage sample
//
// Parameters:
// psample
// [out] pointer to the sample value,
// psample[0] = battery voltage;
// psample[1] = battery current;
// psample[2] = charger voltage;
// psample[3] = charger current;
//
// Returns:
// TRUE if success FALSE if else
//
//-----------------------------------------------------------------------------
BOOL BSPBattdrvrGetSample(UINT16 *psample)
{
BOOL ret = TRUE;
PMIC_STATUS status;
UINT16 adc_mul_samples[8]; // must reserve 8
UINT16 adc_i_sample[8]; // reserve eight ...depending on
// the mode you want to use
DEBUGMSG(BSP_BATTDRVR_DEBUG, (TEXT("+BSPBattdrvrGetSample\r\n")));
//call PmicADCGetMultipleChannelsSamples to get the battery voltage
// and charger voltage & current samples
status = PmicADCGetMultipleChannelsSamples(
(1<<MC13783_ADC1_ADA_SEL0_BATT)|
(1<<MC13783_ADC1_ADA_SEL0_CHRGRAW)|
(1<<MC13783_ADC1_ADA_SEL0_CHRGISNS), adc_mul_samples);
if (status != PMIC_SUCCESS)
{
ERRORMSG(TRUE, (TEXT("PmicADCGetBattSample failed\r\n")));
ret = FALSE;
}
//call PmicADCGetHandsetCurrent to get the battery current sample
if (ret == TRUE)
{
status = PmicADCGetHandsetCurrent(ADC_8CHAN_1X, &adc_i_sample[1]);
if (status != PMIC_SUCCESS)
{
ERRORMSG(TRUE, (TEXT("PmicADCGetBattSample failed\r\n")));
ret = FALSE;
}
}
psample[BattVoltage] = adc_mul_samples[0];
psample[BattCurrent] = adc_i_sample[1];
psample[ChargerVoltage] = adc_mul_samples[1];
psample[ChargerCurrent] = adc_mul_samples[2];
DEBUGMSG(BSP_BATTDRVR_DEBUG, (TEXT("samples %d %d %d %d\r\n"),
psample[BattVoltage], psample[BattCurrent], psample[ChargerVoltage],
psample[ChargerCurrent]));
DEBUGMSG(BSP_BATTDRVR_DEBUG, (TEXT("-BSPBattdrvrGetSample\r\n")));
return ret;
}
//-----------------------------------------------------------------------------
//
// Function: BSPBattdrvrGetParameters
//
// This function returns the battery and charger voltages, the current as well
// as a flag that indicates if the curren tis charging or discharging
//
// Parameters:
// pBatt_V
// [out] pointer of the battery voltage
// pCharger_V
// [out] pointer of the charger voltage
// fCharge
// [out] pointer of the flag TRUE for charging FALSE for discharging
// I
// [out] pointer of the charging/discharging current
//
// Returns:
// TRUE if successful, FALSE for else
//
//-----------------------------------------------------------------------------
BOOL BSPBattdrvrGetParameters(DWORD *pBatt_V,
DWORD *pCharger_V,
BOOL *fCharge,
DWORD *I)
{
BOOL ret = TRUE;
BOOL fDualCharge;
UINT16 samples[TotalChannels];
UINT16 batt_i_sample;
UINT16 charger_i_sample;
LONG batt_I;
LONG charger_I;
BATT_INFO BattInfo; //batt info structure obtained from bsp layer
DEBUGMSG(BSP_BATTDRVR_DEBUG, (TEXT("+BSPBattdrvrGetParameters\r\n")));
//Get Info from BSP layer
if(BSPBattdrvrGetInfo(&BattInfo) != TRUE)
{
ERRORMSG(TRUE, (TEXT("BSPBattdrvrGetInfo fail !!\r\n")));
ret = FALSE;
}
if (ret == TRUE)
{
//check whether the dual charging mode is used
if (BSPBattdrvrGetChargingMode(&fDualCharge) != TRUE)
{
ERRORMSG(TRUE, (TEXT("Get battery charge mode fail !!\r\n")));
ret = FALSE;
}
}
if (ret == TRUE)
{
//check whether charger is available -- using the threshold voltage
//from pmic spec
if(BSPBattdrvrGetSample(samples) != TRUE)
{
ERRORMSG(TRUE, (TEXT("Get battery sample fail !!\r\n")));
ret = FALSE;
}
}
// determine the battery voltage according to the corresponding ADC sample
*pBatt_V =
BattInfo.adc_batt_min_V +
(DWORD)((BattInfo.adc_batt_max_V-BattInfo.adc_batt_min_V) *
samples[BattVoltage]/BattInfo.adc_level);
DEBUGMSG(BSP_BATTDRVR_DEBUG, (TEXT("batt_V = %d sample = %d\r\n"),
*pBatt_V, samples[BattVoltage]));
// determine the battery current according to the corresponding ADC sample
batt_i_sample = samples[BattCurrent];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -