📄 charger.c
字号:
/* Read previous MADC_CTRL_P1 content */
previous_content = MSI2C_MasterPollingReceiveAbb(MADC_CTRL_P1) & 0xf;
/* START SP1 conversion (set SP1 bit) */
MSI2C_MasterPollingSendAbb(MADC_CTRL_P1,(previous_content | 0x8));
return (1);
}
/*--------------------------------------------------------------------------------------
// NAME : I2V_calib
//
// DESCRIPTION : Calibration of the I-to-V converter to get an accurate regulation
//
// PARAMETERS : No
//
// RETURN VALUE: '1' is returned when the conversion is finished
//
// LIMITATIONS : None
//--------------------------------------------------------------------------------------- */
int I2V_calib(int CGAIN4, int added_offset)
{
int offset_code;
int global_offset_binary_MSB;
int global_offset_binary_LSB;
int global_offset_binary;
int global_offset_analog;
int j;
switch(added_offset)
{
case 40 :
case 100 :
{
offset_code = 0x0;
}
break;
case 80 :
case 200 :
{
offset_code = 0x40;
}
break;
case 120 :
case 300 :
{
offset_code = 0x20;
}
break;
case 160 :
case 400 :
{
offset_code = 0x60;
}
break;
default:
break;
}
/* Access to the BCI register page */
MSI2C_AccessPage(BCI_page);
/* Settings for calibration routine */
MSI2C_MasterPollingSendAbb(BCI_BCICTL2,(0x88 | offset_code | (CGAIN4 << 4)));
/* Wait loop for analog offset to be set */
for (j=0;j<1000000;j++);
/* Conversion of all the MADC channels (and especially ICHG) */
if (MADC_SP1_poll())
{
global_offset_binary_MSB = MSI2C_MasterPollingReceiveAbb(MADC_GPCH8_MSB) & 0x3;
global_offset_binary_LSB = MSI2C_MasterPollingReceiveAbb(MADC_GPCH8_LSB);
global_offset_binary = ((global_offset_binary_MSB << 8) | global_offset_binary_LSB);
global_offset_analog = (int)(global_offset_binary * 1.71); // 1.71 = 1750 / 1023
// Access to the BCI register page
MSI2C_AccessPage(BCI_page);
// Disables the calibration routine
MSI2C_MasterPollingSendAbb(BCI_BCICTL2,0x0);
return global_offset_analog;
/* DONE */
}
else
{
return 0;
}
/* DONE */
}
/*-------------------------------------------------------------------------------
// NAME : Disable_Comparator
//
// DESCRIPTION : Disables the HW comparator
//
// PARAMETERS : No
//
// RETURN VALUE:
//
// LIMITATIONS : None
//------------------------------------------------------------------------------- */
void Disable_Comparator()
{
int previous_content;
/* Selects the BCI register page */
MSI2C_AccessPage(BCI_page);
/* Disables the HW comparator */
MSI2C_MasterPollingSendAbb(BCI_BCISKEY, 0x1A);
/* Read previous BCI_BCICTL3 content */
previous_content = MSI2C_MasterPollingReceiveAbb(BCI_BCICTL3) & 0xff;
/* Disables the MESBAT resistor bridge */
MSI2C_MasterPollingSendAbb(BCI_BCICTL3,(previous_content & 0xdf));
}
/*-------------------------------------------------------------------------------
// NAME : Disable_Watchdog
//
// DESCRIPTION : Disables the Watchdog timer
//
// PARAMETERS : No
//
// RETURN VALUE:
//
// LIMITATIONS : None
//------------------------------------------------------------------------------- */
void Disable_Watchdog()
{
/* Selects the BCI register page */
MSI2C_AccessPage(BCI_page);
/* Disables the Watchdog timer */
MSI2C_MasterPollingSendAbb(BCI_BCIWDKEY, 0x33);
}
/*--------------------------------------------------------------
// NAME : Read_BatteryVoltage
//DESCRIPTION : Read the main battery voltage during the charge
//PARAMETERS : NONE
//RETURN VALUE : Main battery voltage
//LIMITATIONS : None
//-------------------------------------------------------------*/
int Read_BatteryVoltage(void)
{
int voltage;
int LSB,MSB;
MSI2C_AccessPage(MADC_page);
MSB = MSI2C_MasterPollingReceiveAbb(MADC_RTCH10_MSB);
LSB = MSI2C_MasterPollingReceiveAbb(MADC_RTCH10_LSB);
voltage = ( ( voltage = (MSB & 0x3) )<<8) | LSB;
return voltage;
}
int Read_BatteryVoltage_GP(void)
{
int voltage;
int LSB,MSB;
MSI2C_AccessPage(MADC_page);
MSB = MSI2C_MasterPollingReceiveAbb(MADC_GPCH10_MSB);
LSB = MSI2C_MasterPollingReceiveAbb(MADC_GPCH10_LSB);
voltage = ((voltage =(MSB & 0x3))<<8)|LSB;
return voltage;
}
/*--------------------------------------------------------------
// NAME : Read_ChargeCurrent
//DESCRIPTION : Read the main battery charging current during the charge
//PARAMETERS : NONE
//RETURN VALUE : charge current
//LIMITATIONS : None
//-------------------------------------------------------------*/
int Read_ChargeCurrent_GP(void)
{
int current;
int LSB,MSB;
MSI2C_AccessPage(MADC_page);
MSB = MSI2C_MasterPollingReceiveAbb(MADC_GPCH8_MSB);
LSB = MSI2C_MasterPollingReceiveAbb(MADC_GPCH8_LSB);
current = ((current =(MSB & 0x3))<<8)|LSB;
return current;
}
/*-------------------------------------------------------------------------------
// NAME : CC_charge
//
// DESCRIPTION : Sets reference current in DAC register and starts the charge
//
// PARAMETERS : No
//
// RETURN VALUE:
//
// LIMITATIONS : None
//------------------------------------------------------------------------------- */
void CC_charge(int current_ref, int global_offset, int USB_AC)
{
short idacdata;
int G,cgain4;
int temp1;
BOOL temp2;
char st[80];
UWORD16 temp3;
if(current_ref <= 750)
{
G = 10;
cgain4 = 0x0;
}
else
{
G = 4;
cgain4 = 0x10;
}
idacdata = (short)((255*(0.22*current_ref*G + global_offset))/1750);
//sprintf(st,"\r The return value is %d\r\n",Read_BatteryVoltage_GP());
//SERI_Send(st);
/* Selects the TRITON BCI register page */
MSI2C_AccessPage(BCI_page);
/* Sets the current reference code in the DAC register */
MSI2C_MasterPollingSendAbb(BCI_CHGIREG,idacdata);
/* Sets and enables the maximum offset, sets the gain, selects the power path (USB or AC), 0x1 for USB and 0x2 for AC */
MSI2C_MasterPollingSendAbb(BCI_BCICTL2,(0xE0| cgain4 | USB_AC));
//MSI2C_MasterPollingSendAbb(BCI_BCICTL3,0xF0);
//MSI2C_MasterPollingSendAbb(BCI_CHGVREG1,0xff);
// MSI2C_MasterPollingSendAbb(BCI_CHGVREG2,0xd0);
/*Set the watchdog*/ //Li Junjie add
// temp2 = Refresh_Watchdog(8);
//MADC_SP1_poll_Measure();
//COMP_MODE_CTRL_0 = 0x0000EAEF;
//CONFIGURATION_COMP_MODE_CTRL_0 = 0xEAEF;
//temp1 = MADC_RT_poll(10);
//sprintf(st,"\r The return value is %d\r\n",Read_BatteryVoltage_GP());
// SERI_Send(st);
/* Selects Constant Current charge mode and starts the charge */
MSI2C_AccessPage(BCI_page);
MSI2C_MasterPollingSendAbb(BCI_BCICTL1,0x3);
// disable watchdog
// Disable_Watchdog();
/*
while(Read_BatteryVoltage_GP() < 0x28d)//while(Read_BatteryVoltage_GP() < 0x00EE)
{
// temp2 = Refresh_Watchdog(8);
// temp3 = Read_BatteryVoltage();
temp2 = Refresh_Watchdog(8);
TIME_WaitMs(7500);
if(MADC_SP1_poll_Measure())
{
UART_Printf(UART2,"\r The voltage is 0x%x\r\n",Read_BatteryVoltage_GP());
}
}
*/
while(RealTime_Voltage() < 0x28d)
{
temp2 = Refresh_Watchdog(8);
TIME_WaitMs(7500);
UART_Printf(UART2,"\rOooyeah~~ The voltage is 0x%x\r\n",RealTime_Voltage());
}
/*------------------------------------------------
//Li Junjie add
MSI2C_AccessPage(BCI_page);
MSI2C_MasterPollingSendAbb(BCI_CHGVREG1,0xff);
MSI2C_MasterPollingSendAbb(BCI_CHGVREG2,0xd0);
temp1 = MADC_RT_poll(10);
SERI_Send("\r\n\t return value is %d \r\n",temp1);
while(Read_voltage_RT() < (0x65|(0x02<<8)))
{
temp2 = Refresh_Watchdog(8);
SERI_Send("\r\n\t The voltage is 0x%x \r\n",Read_voltage_RT());
}
//Li Junjie Add end
------------------------------------------------------*/
}
/*-------------------------------------------------------------------------------
// NAME : CV_charge
//
// DESCRIPTION : Sets reference voltage in DAC register and starts the charge
//
// PARAMETERS : No
//
// RETURN VALUE:
//
// LIMITATIONS : None
//------------------------------------------------------------------------------- */
void CV_charge(int voltage_ref, int USB_AC)
{
int vdacdata;
int temp;
char st[80];
/* reference voltage limitation to not damage Li-Ion real battery */
if(voltage_ref > 4200)
{
voltage_ref = 4200;
UART_Printf(UART2,"\r\n WARNING: The reference voltage has been corrected to 4.2 V \r\n");
}
vdacdata = (int)((1023 * voltage_ref)/7000); // 7000 = 1750 mV * 4 (due to divider bridge)
/* Selects the TRITON BCI register page */
MSI2C_AccessPage(BCI_page);
/* Sets the MSB voltage reference code in the DAC register */
MSI2C_MasterPollingSendAbb(BCI_CHGVREG1,((vdacdata >> 2) & 0xff));
/* Sets the LSB voltage reference code in the DAC register */
MSI2C_MasterPollingSendAbb(BCI_CHGVREG2,((vdacdata & 0x3) << 6) & 0xC0);
/* Enables the divider resistor bridge for VBAT and stops any running precharge */
MSI2C_MasterPollingSendAbb(BCI_BCICTL3,0xE0);
/* Selects the power path (USB or AC) */
MSI2C_MasterPollingSendAbb(BCI_BCICTL2,USB_AC);
/* Selects Constant Voltage charge mode and starts the full charge */
MSI2C_MasterPollingSendAbb(BCI_BCICTL1,0x1);
/*Read the charge current value in MADC*/
while((Read_ChargeCurrent_GP() > 0x0F)|(Read_BatteryVoltage_GP()<0x28E))
{
temp = Refresh_Watchdog(8);
TIME_WaitMs(7500);
if(MADC_SP1_poll_Measure())
{
UART_Printf(UART2,"\r The current is 0x%x\r\n",Read_ChargeCurrent_GP());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -