📄 pmic_battery.cpp
字号:
//-----------------------------------------------------------------------------
//
// Copyright (C) 2005, Motorola Inc. All Rights Reserved
//
//------------------------------------------------------------------------------
/*---------------------------------------------------------------------------
* Copyright (C) 2006, Freescale Semiconductor, Inc. All Rights Reserved.
* THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
* AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
*--------------------------------------------------------------------------*/
//------------------------------------------------------------------------------
//
// File: pmic_battery.cpp
//
// This file contains the PMIC Battery SDK interface that is used by
// applications and other drivers to access registers of the PMIC (MC13783).
//
//-----------------------------------------------------------------------------
#include <windows.h>
#include <Devload.h>
#include "socarm_macros.h"
#include "pmic_ioctl.h"
#include "pmic_battery.h"
#include "pmic_adc.h"
#include "regs.h"
#include "regs_battery.h"
//-----------------------------------------------------------------------------
// External Functions
//-----------------------------------------------------------------------------
// External Variables
//-----------------------------------------------------------------------------
// Defines
//-----------------------------------------------------------------------------
// Types
//-----------------------------------------------------------------------------
// Global Variables
extern HANDLE hPMI;
#ifdef DEBUG
// Debug zone bit positions
#define ZONEID_ERROR 0
#define ZONEID_WARN 1
#define ZONEID_INIT 2
#define ZONEID_FUNC 3
#define ZONEID_INFO 4
// Debug zone masks
#define ZONEMASK_ERROR (1 << ZONEID_ERROR)
#define ZONEMASK_WARN (1 << ZONEID_WARN)
#define ZONEMASK_INIT (1 << ZONEID_INIT)
#define ZONEMASK_FUNC (1 << ZONEID_FUNC)
#define ZONEMASK_INFO (1 << ZONEID_INFO)
// Debug zone args to DEBUGMSG
#define ZONE_ERROR DEBUGZONE(ZONEID_ERROR)
#define ZONE_WARN DEBUGZONE(ZONEID_WARN)
#define ZONE_INIT DEBUGZONE(ZONEID_INIT)
#define ZONE_FUNC DEBUGZONE(ZONEID_FUNC)
#define ZONE_INFO DEBUGZONE(ZONEID_INFO)
extern DBGPARAM dpCurSettings;
#endif
//-----------------------------------------------------------------------------
// Local Variables
//-----------------------------------------------------------------------------
// Local Functions
//------------------------------------------------------------------------------
//
// Function: PmicBatterySetChargeVoltage
//
// This function programs the output voltage of charge regulator
//
// Parameters:
// chargevoltagelevel [IN] voltage level
// level 0 = 4.05V
// 1 = 4.10V
// 2 = 4.15V
// 3 = 4.20V
// 4 = 4.25V
// 5 = 4.30V
// 6 = 3.80V ... lowest setting
// 7 = 4.50V
//
// Returns:
// PMIC_STATUS.
//
//------------------------------------------------------------------------------
PMIC_STATUS PmicBatterySetChargeVoltage(UINT8 chargevoltagelevel)
{
PMIC_PARAM_LLA_WRITE_REG param;
DEBUGMSG(ZONE_FUNC, (TEXT("+%s()\r\n"), __WFUNCTION__));
// check charge voltage level parameter
if (chargevoltagelevel > MC13783_BATTERY_MAX_CHARGE_VOLTAGE_LEVEL)
{
ERRORMSG(1, (_T("PmicBatterySetChargeVoltage Invalid Parameter\r\n")));
return PMIC_PARAMETER_ERROR;
}
param.addr = MC13783_CHG0_ADDR;
param.data = CSP_BITFVAL(MC13783_CHG0_VCHRG, chargevoltagelevel);
param.mask = CSP_BITFMASK(MC13783_CHG0_VCHRG);
if(! DeviceIoControl(hPMI, PMIC_IOCTL_LLA_WRITE_REG, ¶m,
sizeof(param), NULL, 0, NULL, NULL))
return PMIC_ERROR;
DEBUGMSG(ZONE_FUNC, (TEXT("-%s()\r\n"), __WFUNCTION__));
return PMIC_SUCCESS;
}
//------------------------------------------------------------------------------
//
// Function: PmicBatteryGetChargeVoltage
//
// This function returns the output voltage of charge regulator
//
// Parameters:
// chargevoltagelevel [OUT] pointer to voltage level
//
// Returns:
// PMIC_STATUS.
//
//------------------------------------------------------------------------------
PMIC_STATUS PmicBatteryGetChargeVoltage(UINT8* chargevoltagelevel)
{
UINT32 param, temp;
DEBUGMSG(ZONE_FUNC, (TEXT("+%s()\r\n"), __WFUNCTION__));
if (chargevoltagelevel == NULL)
{
DEBUGMSG(ZONE_ERROR, (_T("PmicBatteryGetChargeVoltage:Invalid Parameter\r\n")));
return PMIC_PARAMETER_ERROR;
}
param = MC13783_CHG0_ADDR;
if(! DeviceIoControl(hPMI, PMIC_IOCTL_LLA_READ_REG, ¶m, sizeof(param),
&temp, sizeof(temp), NULL, NULL))
return PMIC_ERROR;
*chargevoltagelevel = CSP_BITFEXT(temp, MC13783_CHG0_VCHRG);
DEBUGMSG(ZONE_FUNC, (TEXT("-%s()\r\n"), __WFUNCTION__));
return PMIC_SUCCESS;
}
//------------------------------------------------------------------------------
//
// Function: PmicBatterySetChargeCurrent
//
// This function programs the charge current limit level to the main battery
//
// Parameters:
// chargecurrentlevel [IN] current level
// level 0 = 0 mA (max value)
// 1 = 100 mA (max value)
// ... (in increment of 100 mA)
// 13 = 1300 mA (max value)
// 14 = 1800 mA (max value)
// 15 = disables the current limit
//
// Returns:
// PMIC_STATUS.
//
//------------------------------------------------------------------------------
PMIC_STATUS PmicBatterySetChargeCurrent (UINT8 chargecurrentlevel)
{
PMIC_PARAM_LLA_WRITE_REG param;
DEBUGMSG(ZONE_FUNC, (TEXT("+%s()\r\n"), __WFUNCTION__));
// check charge current level parameter
if (chargecurrentlevel > MC13783_BATTERY_MAX_CHARGE_CURRENT_LEVEL)
{
ERRORMSG(1, (_T("PmicBatterySetChargeCurrent Invalid Parameter\r\n")));
return PMIC_PARAMETER_ERROR;
}
param.addr = MC13783_CHG0_ADDR;
param.data = CSP_BITFVAL(MC13783_CHG0_ICHRG, chargecurrentlevel);
param.mask = CSP_BITFMASK(MC13783_CHG0_ICHRG);
if(! DeviceIoControl(hPMI, PMIC_IOCTL_LLA_WRITE_REG, ¶m,
sizeof(param), NULL, 0, NULL, NULL))
return PMIC_ERROR;
DEBUGMSG(ZONE_FUNC, (TEXT("-%s()\r\n"), __WFUNCTION__));
return PMIC_SUCCESS;
}
//------------------------------------------------------------------------------
//
// Function: PmicBatteryGetChargeCurrent
//
// This function returns the charge current setting of the main battery
//
// Parameters:
// chargecurrentlevel [OUT] pointer to current level
//
// Returns:
// PMIC_STATUS.
//
//------------------------------------------------------------------------------
PMIC_STATUS PmicBatteryGetChargeCurrent (UINT8* chargecurrentlevel)
{
UINT32 param, temp;
DEBUGMSG(ZONE_FUNC, (TEXT("+%s()\r\n"), __WFUNCTION__));
if (chargecurrentlevel == NULL)
{
DEBUGMSG(ZONE_ERROR, (_T("PmicBatteryGetChargeCurrent:Invalid Parameter\r\n")));
return PMIC_PARAMETER_ERROR;
}
param = MC13783_CHG0_ADDR;
if(! DeviceIoControl(hPMI, PMIC_IOCTL_LLA_READ_REG, ¶m, sizeof(param),
&temp, sizeof(temp), NULL, NULL))
return PMIC_ERROR;
*chargecurrentlevel = CSP_BITFEXT(temp, MC13783_CHG0_ICHRG);
DEBUGMSG(ZONE_FUNC, (TEXT("-%s()\r\n"), __WFUNCTION__));
return PMIC_SUCCESS;
}
//------------------------------------------------------------------------------
//
// Function: PmicBatterySetTrickleCurrent
//
// This function programs the current of the trickle charger
//
// Parameters:
// tricklecurrentlevel [IN] trickle current level
// level 0 = 0 mA
// 1 = 12 mA
// ... (in addition of 12 mA per level)
// 6 = 72 mA
// 7 = 84 mA
//
// Returns:
// PMIC_STATUS.
//
//------------------------------------------------------------------------------
PMIC_STATUS PmicBatterySetTrickleCurrent(UINT8 tricklecurrentlevel)
{
PMIC_PARAM_LLA_WRITE_REG param;
DEBUGMSG(ZONE_FUNC, (TEXT("+%s()\r\n"), __WFUNCTION__));
// check trickle current level parameter
if (tricklecurrentlevel > MC13783_BATTERY_MAX_TRICKLE_CURRENT_LEVEL)
{
ERRORMSG(1, (_T("PmicBatterySetTrickleCurrent Invalid Parameter\r\n")));
return PMIC_PARAMETER_ERROR;
}
param.addr = MC13783_CHG0_ADDR;
param.data = CSP_BITFVAL(MC13783_CHG0_ICHRGTR, tricklecurrentlevel);
param.mask = CSP_BITFMASK(MC13783_CHG0_ICHRGTR);
if(! DeviceIoControl(hPMI, PMIC_IOCTL_LLA_WRITE_REG, ¶m,
sizeof(param), NULL, 0, NULL, NULL))
return PMIC_ERROR;
DEBUGMSG(ZONE_FUNC, (TEXT("-%s()\r\n"), __WFUNCTION__));
return PMIC_SUCCESS;
}
//------------------------------------------------------------------------------
//
// Function: PmicBatteryGetTrickleCurrent
//
// This function returns the current of the trickle charger
//
// Parameters:
// tricklecurrentlevel [OUT] pointer to trickle current level
//
//
// Returns:
// PMIC_STATUS.
//
//------------------------------------------------------------------------------
PMIC_STATUS PmicBatteryGetTrickleCurrent (UINT8* tricklecurrentlevel)
{
UINT32 param, temp;
DEBUGMSG(ZONE_FUNC, (TEXT("+%s()\r\n"), __WFUNCTION__));
if (tricklecurrentlevel == NULL)
{
DEBUGMSG(ZONE_ERROR, (_T("PmicBatteryGetTrickleCurrent:Invalid Parameter\r\n")));
return PMIC_PARAMETER_ERROR;
}
param = MC13783_CHG0_ADDR;
if(! DeviceIoControl(hPMI, PMIC_IOCTL_LLA_READ_REG, ¶m, sizeof(param),
&temp, sizeof(temp), NULL, NULL))
return PMIC_ERROR;
*tricklecurrentlevel = CSP_BITFEXT(temp, MC13783_CHG0_ICHRGTR);
DEBUGMSG(ZONE_FUNC, (TEXT("-%s()\r\n"), __WFUNCTION__));
return PMIC_SUCCESS;
}
//------------------------------------------------------------------------------
//
// Function: PmicBatteryFETControl
//
// This function programs the control mode and setting of BPFET and FETOVRD
// BATTFET and BPFET to be controlled by FETCTRL bit or hardware
//
// Parameters:
// fetcontrol [IN] BPFET and FETOVRD control mode and setting
//
// input = 0 (BATTFET and BPFET outputs are controlled by hardware)
// = 1 (BATTFET and BPFET outputs are controlled by hardware)
// = 2 (BATTFET low and BATTFET high, controlled by FETCTRL)
// = 3 (BATTFET high and BATTFET low, controlled by FETCTRL)
//
// Returns:
// PMIC_STATUS.
//
//------------------------------------------------------------------------------
PMIC_STATUS PmicBatteryFETControl(UINT8 fetcontrol)
{
PMIC_PARAM_LLA_WRITE_REG param;
DEBUGMSG(ZONE_FUNC, (TEXT("+%s()\r\n"), __WFUNCTION__));
// check FETcontrol input parameter
if (fetcontrol > MC13783_BATTERY_MAX_FETCONTROL_INPUT_LEVEL)
{
ERRORMSG(1, (_T("PmicBatteryFETControl Invalid Parameter\r\n")));
return PMIC_PARAMETER_ERROR;
}
param.addr = MC13783_CHG0_ADDR;
param.data = CSP_BITFVAL(MC13783_CHG0_FETOVRD, fetcontrol);
param.mask = (CSP_BITFMASK(MC13783_CHG0_FETOVRD) | CSP_BITFMASK(MC13783_CHG0_FETCTRL));
if(! DeviceIoControl(hPMI, PMIC_IOCTL_LLA_WRITE_REG, ¶m,
sizeof(param), NULL, 0, NULL, NULL))
return PMIC_ERROR;
DEBUGMSG(ZONE_FUNC, (TEXT("-%s()\r\n"), __WFUNCTION__));
return PMIC_SUCCESS;
}
//------------------------------------------------------------------------------
//
// Function: PmicBatteryReverseDisable
//
// This function disables the reverse mode
//
// Parameters:
// None.
//
//
// Returns:
// PMIC_STATUS.
//
//------------------------------------------------------------------------------
PMIC_STATUS PmicBatteryReverseDisable()
{
PMIC_PARAM_LLA_WRITE_REG param;
DEBUGMSG(ZONE_FUNC, (TEXT("+%s()\r\n"), __WFUNCTION__));
param.addr = MC13783_CHG0_ADDR;
param.data = CSP_BITFVAL(MC13783_CHG0_RVRSMODE, MC13783_CHG0_RVRSMODE_DISABLE);
param.mask = CSP_BITFMASK(MC13783_CHG0_RVRSMODE);
if(! DeviceIoControl(hPMI, PMIC_IOCTL_LLA_WRITE_REG, ¶m,
sizeof(param), NULL, 0, NULL, NULL))
return PMIC_ERROR;
DEBUGMSG(ZONE_FUNC, (TEXT("-%s()\r\n"), __WFUNCTION__));
return PMIC_SUCCESS;
}
//------------------------------------------------------------------------------
//
// Function: PmicBatteryReverseEnable
//
// This function enables the reverse mode
//
// Parameters:
// None.
//
//
// Returns:
// PMIC_STATUS.
//
//------------------------------------------------------------------------------
PMIC_STATUS PmicBatteryReverseEnable()
{
PMIC_PARAM_LLA_WRITE_REG param;
DEBUGMSG(ZONE_FUNC, (TEXT("+%s()\r\n"), __WFUNCTION__));
param.addr = MC13783_CHG0_ADDR;
param.data = CSP_BITFVAL(MC13783_CHG0_RVRSMODE, MC13783_CHG0_RVRSMODE_ENABLE);
param.mask = CSP_BITFMASK(MC13783_CHG0_RVRSMODE);
if(! DeviceIoControl(hPMI, PMIC_IOCTL_LLA_WRITE_REG, ¶m,
sizeof(param), NULL, 0, NULL, NULL))
return PMIC_ERROR;
DEBUGMSG(ZONE_FUNC, (TEXT("-%s()\r\n"), __WFUNCTION__));
return PMIC_SUCCESS;
}
//------------------------------------------------------------------------------
//
// Function: PmicBatterySetOvervoltageThreshold
//
// This function programs the overvoltage threshold value
//
// Parameters:
// ovthresholdlevel [IN] overvoltage threshold level
// High to low, Low to High (5.35V)
//
// Returns:
// PMIC_STATUS.
//
//------------------------------------------------------------------------------
PMIC_STATUS PmicBatterySetOvervoltageThreshold(UINT8 ovthresholdlevel)
{
PMIC_PARAM_LLA_WRITE_REG param;
DEBUGMSG(ZONE_FUNC, (TEXT("+%s()\r\n"), __WFUNCTION__));
// check overvoltage threshold level parameter
if (ovthresholdlevel > MC13783_BATTERY_MAX_OVERVOLTAGE_THRESHOLD_LEVEL)
{
ERRORMSG(1, (_T("PmicBatterySetOvervoltageThreshold Invalid Parameter\r\n")));
return PMIC_PARAMETER_ERROR;
}
param.addr = MC13783_CHG0_ADDR;
param.data = CSP_BITFVAL(MC13783_CHG0_OVCTRL, ovthresholdlevel);
param.mask = CSP_BITFMASK(MC13783_CHG0_OVCTRL);
if(! DeviceIoControl(hPMI, PMIC_IOCTL_LLA_WRITE_REG, ¶m,
sizeof(param), NULL, 0, NULL, NULL))
return PMIC_ERROR;
DEBUGMSG(ZONE_FUNC, (TEXT("-%s()\r\n"), __WFUNCTION__));
return PMIC_SUCCESS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -