📄 powermanagement.c
字号:
regvalue = last & 0xBF;
}
else if(value == 30)
{
regvalue = last | 0x40;
}
else
return 0;
WriteAs3603(DIG2VOLTAGE_REG, regvalue);
return 1;
}
/*
nor flash
*/
void PM_NorFlash(double val)
{
PM_SetDig1Voltage(val);
PM_EnableDig1(ON);
}
/*
nand flash
*/
void PM_NandFlash(double val)
{
PM_SetAna2Voltage(val);
PM_EnableAna2(ON);
}
/*
cmos
*/
void PM_Cmos(double val)
{
PM_SetDig2Voltage(val);
PM_EnableDig2(ON);
}
/*
sim
*/
void PM_Sim(double val)
{
PM_SetSimVoltage(val);
PM_EnableSim(ON);
}
/*
z228_3v3
*/
void PM_Z228_3v3(double val)
{
PM_SetAna1Voltage(val);
PM_EnableAna1(ON);
}
/*
z228_1v2
*/
void PM_Z228_1v2(double val)
{
PM_SetBuckVoltage(val);
PM_EnableBuck(ON);
}
/*
WIFI
*/
void PM_WiFi(double val)
{
PM_SetRf1Voltage(val);
PM_EnableRf1(ON);
}
/*
SDRAM
*/
void PM_Sdram(double val)
{
PM_SetRf2Voltage(val);
PM_EnableRf2(ON);
}
// Set Ana1 channel on/off
U32 PM_EnableAna1(U32 on)
{
unsigned char regvalue;
regvalue = ReadAs3603(POWERCTRL_REG);
if(on == 0)
regvalue = regvalue & 0xFE;
else
regvalue = regvalue | 0x01;
WriteAs3603(POWERCTRL_REG, regvalue);
return 1;
}
// Set Ana2 channel on/off
U32 PM_EnableAna2(U32 on)
{
unsigned char regvalue;
regvalue = ReadAs3603(POWERCTRL_REG);
if(on == 0)
regvalue = regvalue & 0xFD;
else
regvalue = regvalue | 0x02;
WriteAs3603(POWERCTRL_REG, regvalue);
return 1;
}
// Set Dig1 channel on/off
U32 PM_EnableDig1(U32 on)
{
unsigned char regvalue;
regvalue = ReadAs3603(POWERCTRL_REG);
if(on == 0)
regvalue = regvalue & 0xFB;
else
regvalue = regvalue | 0x04;
WriteAs3603(POWERCTRL_REG, regvalue);
return 1;
}
// Set Dig2 channel on/off
U32 PM_EnableDig2(U32 on)
{
unsigned char regvalue;
regvalue = ReadAs3603(POWERCTRL_REG);
if(on == 0)
regvalue = regvalue & 0xF7;
else
regvalue = regvalue | 0x08;
WriteAs3603(POWERCTRL_REG, regvalue);
return 1;
}
// Set Sim channel on/off
U32 PM_EnableSim(U32 on)
{
unsigned char regvalue;
regvalue = ReadAs3603(POWERCTRL_REG);
if(on == 0)
regvalue = regvalue & 0xEF;
else
regvalue = regvalue | 0x10;
WriteAs3603(POWERCTRL_REG, regvalue);
return 1;
}
//======================LDO END=====================
//======================Step Up BEGIN=====================
U32 PM_EnableStepUp(U32 on)
{
unsigned char regvalue;
regvalue = ReadAs3603(STEPUPCTRL_REG);
if(on == 0)
regvalue = regvalue & 0xFE;
else
regvalue = regvalue | 0x01;
WriteAs3603(STEPUPCTRL_REG, regvalue);
return 1;
}
//======================Step Up END=====================
//======================GPIO BEGIN=====================
// Sets GPIO Read
U32 PM_GpioRead(void)
{
unsigned char regvalue;
regvalue = ReadAs3603(GPIOSIGNAL_REG);
return regvalue;
}
// Sets GPIO Write
U32 PM_GpioWrite(U32 channel, U32 data)
{
unsigned char regvalue, last;
if(channel == 2)
{
regvalue = ReadAs3603(GPIO2CTRL_REG);
if((regvalue & 0x04) != 0)
{
#ifdef _DEBUG
printf("Pin GPIO2 is used as feedback-pin for Step Down DC/DC Converter");
#endif
return -1;
}
}
last = ReadAs3603(GPIOSIGNAL_REG);
regvalue = 0x01<<(channel-1);
regvalue = ~regvalue;
last &= regvalue;
regvalue = 0x01<<(channel-1);
if(data != 0)
regvalue += last;
WriteAs3603(GPIOSIGNAL_REG, regvalue);
return 1;
}
// Read interrupt status
U32 PM_ReadInterruptStatus(void)
{
return ReadAs3603(IRQSTATUS_REG);
}
//======================GPIO END=====================
//======================Current Sink BEGIN=====================
/*
back light
*/
void PM_BackLight(double val)
{
PM_EnableStepUp(ON);
PM_SetCurrent(1,val);
PM_EnableCurrent(1,ON);
}
// Sets the current of current source CURR X.
// Unit is mA
U32 PM_SetCurrent(U32 channel, double current)
{
unsigned char regvalue;
double f = current * 10.0;
int value = (int)f;
if (value<0 || value>1600 )
{
#ifdef _DEBUG
printf("CURR%d channel current is between 0 and 160mA", channel);
#endif
return 0;
}
else if(channel<1 || channel>4)
{
#ifdef _DEBUG
printf("Error: channel number!");
#endif
return 0;
}
value = value * 100 / 625;
regvalue = (unsigned char) value;
WriteAs3603(CURR1VALUE_REG+channel-1, regvalue);
return 1;
}
U32 PM_EnableCurrent(U32 channel, U32 on)
{
unsigned char last, regvalue;
if(channel<1 || channel>4)
{
#ifdef _DEBUG
printf("Error: channel number!");
#endif
return 0;
}
last = ReadAs3603(CURRCTRL_REG);
regvalue = 0x03;
regvalue <<= (channel-1)*2;
regvalue = ~regvalue;
last &= regvalue;
if (on)
regvalue = 0x01;
else
regvalue = 0x00;
regvalue <<= (channel-1)*2;
regvalue += last;
WriteAs3603(CURRCTRL_REG, regvalue);
return 1;
}
//======================Current Sink END=====================
/************************************************************************/
/* */
/************************************************************************/
U32 PM_PowerOnInitial(void)
{
// unsigned char regvalue;
/* Sets Z228 3.2V*/
PM_SetAna1Voltage((double)3.2);
/* Sets Z228 1.2V*/
PM_SetBuckVoltage((double)1.6);
/* Sets SDRAM 3.3V*/
PM_SetRf2Voltage((double)3.3);
//Sets NOR flash
PM_SetDig1Voltage((double)1.8);
// Sets NAND 3.3V
PM_SetAna2Voltage((double)3.3);
/* Sets CMOS 2.6V*/
PM_SetDig2Voltage((double)2.5);
/* Sets SIM 1.8V*/
PM_SetSimVoltage((double)1.8);
// Sets WIFI 3.3V
PM_SetRf1Voltage((double)3.3);
// Sets TV1 3.2V
PM_SetRf3Voltage((double)3.3);
/* Sets TV2 1.2V*/
PM_SetRf4Voltage((double)3.3);
// Enable overtemperature detetion
WriteAs3603(OVERTEMPETURE_REG, 0x01);
// Clear overtemperature flag
WriteAs3603(OVERTEMPETURE_REG, 0x09);
WriteAs3603(OVERTEMPETURE_REG, 0x01);
// Disable the watchdog
WriteAs3603(WATDOGCTRL_REG, 0x00);
/* Disable audio amplifier */
WriteAs3603(AUDIOCTRL_REG, 0x00);
// Programmable Frequency Generator
WriteAs3603(CLKGENERATION_REG, 0x00);
WriteAs3603(HIGHTTIME_REG, 0x00);
WriteAs3603(LOWTIME_REG, 0x00);
// initializing GPIO register
WriteAs3603(GPIOACTIVE_REG, 0x00); //Activates GPIO on/off control for voltage regulators
WriteAs3603(ADGPIO_REG, 0x00); //Selects GPIO pin for power on/off control.
WriteAs3603(RFGPIO_REG, 0x00); //Selects GPIO pin for power on/off control for RF LDOs VRF_1 - VRF_4.
WriteAs3603(CHARGEPUMP_REG, 0x00); //Sets the operation mode of the Charge Pump.
/* Sets GPIO1 Control Register
01 gpio1_mode
0 gpio1_voltage
01 gpio1_pulls
0 gpio1_invert
00 gpio1_out_src
*/
WriteAs3603(GPIO1CTRL_REG, 0x09);
/* Sets GPIO2 Control Register
00 gpio2_mode
1 stepdown_fb
00 gpio2_pulls
0 gpio2_invert
00 gpio2_out_src
*/
WriteAs3603(GPIO2CTRL_REG, 0x04);
/* Sets GPIO3 Control Register
01 gpio3_mode
0 gpio3_voltage
10 gpio3_pulls
0 gpio3_invert
00 gpio3_out_src
*/
WriteAs3603(GPIO3CTRL_REG, 0x11);
/* Sets GPIO4 Control Register
00 gpio4_mode
0 gpio4_voltage
10 gpio4_pulls
0 gpio4_invert
00 gpio4_out_src
*/
WriteAs3603(GPIO4CTRL_REG, 0x10);
WriteAs3603(GPIOSIGNAL_REG, 0x00);
/* Enable interrupt
0 Disables watchdog alarm as interrupt source signal.
1 Enables charge termination voltage as interrupt source signal.
1 Enables ov_temp_110 (device temperature alert at 110oC).
1 Enables pin ON
1 Enables charger detection.
*/
WriteAs3603(IRQENABLE_REG, 0x1E);
/* Configures current mode register
0 Pin CURR1 is operating as current sink.
0 Pin CURR2 is operating as current sink.
0 Pin CURR3 is operating as current sink.
0 Pin CURR4 is operating as current sink.
*/
WriteAs3603(CURRMODE_REG, 0x00);
/* Configures current control register
01 Pin CURR1 is active.
00 Pin CURR2 is turned off.
01 Pin CURR3 is active.
00 Pin CURR4 is turned off.
*/
WriteAs3603(CURRCTRL_REG, 0x11);
/* Selects GPIO pin to control current sources.*/
WriteAs3603(CURRGPIO_REG, 0x00);
/* Sets current snik value (unit mA) */
PM_SetCurrent(1, 20); //LCD backlight
PM_SetCurrent(3, 20); //power led
/* Config step up mode
0 Step Up DC/DC Converter is off.
0 Switch open(STEPUP doubleing).
0 System clock (typ. 1.1MHZ).
0 High current limit (600mA).
*/
WriteAs3603(STEPUPCTRL_REG, 0x00);
/* Sets timer for next reset */
WriteAs3603(RESETTIMER_REG, 0x05); //90ms
return 1;
}
/************************************************************************/
//======================Battery Charger Controller BEGIN=====================
void PM_ChargeDefaultConfig(void)
{
/* Sets charger config register
000 N/A
0 Enable battery presence indication
0 Enable operation without battery
0 Li+ battery with coke anode
0 Disable CVM testmode
*/
WriteAs3603(CHARGECONF_REG, 0x00);
/* Sets charger current
TrickleCurrent 2.5mV/RSENSE
ConstantCurrent 15mV/Rsense
*/
WriteAs3603(CHARGECURRENT_REG, 0x0d);
WriteAs3603(PRECURDAC_REG, 0x32);//0.5v r=2k
}
void PM_ChargeInitial(void)
{
WriteAs3603(FUELGAUGE_REG, 0x00);
WriteAs3603(CHARGECTRL_REG, 0x00);
PM_ChargeDefaultConfig();
g_chargeUpdateNumber = 0;
//得到充电参数
}
U8 PM_ChargeUpdate(double* uiQacc,U32* uiTime)
{
volatile unsigned char value;
volatile double fQacc;
volatile U32 time;
value = ReadAs3603(FUELGAUGE_REG);
value |= 0x02;
WriteAs3603(FUELGAUGE_REG, value);
value = ReadAs3603(FUELGAUGE_REG);
while((value & 0x20) == 0x20);//wait update
value = ReadAs3603(CHARGESTATUS_REG);
if(g_chargeQAcc == 0)//first use or rechange battery
{
if((value & 0x20) == 0x20)
{
g_chargeQAcc = FULL_RESET;
*uiQacc = g_chargeQAcc;
return true;
}
}
fQacc = g_chargeQAcc;
time = g_chargeTime;
if(g_chargeQAcc > 0 && (value & 0x01) == 0)//discharge
{
g_chargeQAcc -= ((((U32)(0x7f-ReadAs3603(DELTAMSB_REG)))<<8) + (0xff-ReadAs3603(DELTALSB_REG)))*61.03;
g_dischargeTime += (U32)(((((U32)ReadAs3603(ELAPSEDMSB_REG))<<8) + ReadAs3603(ELAPSEDLSB_REG))*0.8788);
}
else//charge
{
if((ReadAs3603(DELTAMSB_REG) & 0x80) == 0)
{
g_chargeQAcc += ((((U32)ReadAs3603(DELTAMSB_REG))<<8) + ReadAs3603(DELTALSB_REG))*61.03;
g_chargeTime += (U32)(((((U32)ReadAs3603(ELAPSEDMSB_REG))<<8) + ReadAs3603(ELAPSEDLSB_REG))*0.8788);
*uiTime = g_chargeTime;
}
}
if(g_chargeQAcc == fQacc)
{
return false;
}
*uiQacc = g_chargeQAcc;
g_IAvg = (g_chargeQAcc-fQacc) / ( (g_chargeTime-time) * 91.0 * 0.05);
if(g_chargeUpdateNumber++ >= 20)
{
g_chargeUpdateNumber = 0;
//保存充电参数
}
return true;
}
U8 PM_ChargeIsFull(void)
{
U8 value;
value = ReadAs3603(CHARGESTATUS_REG);
if((value & 0x20) == 0x20)
{
return true;
}
else
{
return false;
}
}
//ms
U32 PM_ChargePreTime(void)
{
U32 val;
if(g_IAvg > 0)
{
val = (U32)((FULL_RESET - g_chargeQAcc)/g_IAvg);
return val;
}
else
{
return 0;
}
}
int PM_ChargeStart(void)
{
unsigned char status;
// Detectes battery
status = ReadAs3603(CHARGESTATUS_REG);
if(status & 0x40)
{
#ifdef _DEBUG
printf("Not detecte battery!");
#endif
return -1;
}
// Detectes status
if((status & 0x01) == 0)
{
#ifdef _DEBUG
printf("Not detecte charger!");
#endif
return -2;
}
if((status & 0x02) == 0)
{
#ifdef _DEBUG
printf("Not active charger!");
#endif
}
/* Sets Fuel Gauge Register
1 Enables Fuel Gauge.
0 Controls the updates of the Delta Charge MSB/LSB registers and the Elapsed Time MSB/LSB registers
1 Request offset calibration.
0 Sets the offset calibration mode.
*/
WriteAs3603(FUELGAUGE_REG, 0x01);
/* Sets parameter of the charger
0 Disables charging
0 lithium-based battery
0 Selects Constant Current charge mode
0 Li+ battery with coke anode
0 Select constant voltage charging mode
0 Normal charger operation.
0 Nominal current
*/
/* Enable charger*/
WriteAs3603(CHARGECTRL_REG, 0x41);//boost = 1
//clean charge time
g_chargeTime = 0;
g_dischargeTime = 0;
return 1;
}
U32 PM_ChargeFinish(void)
{
unsigned char status, ctrlreg;
status = ReadAs3603(CHARGESTATUS_REG);
if (status && 0x20)
{
ctrlreg = ReadAs3603(CHARGECTRL_REG);
ctrlreg &= 0xFE;
WriteAs3603(CHARGECTRL_REG, ctrlreg);
return 1;
}
return 0;
}
/************************************************************************/
//======================Battery Charger Controller END=====================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -