📄 triton_functions.c
字号:
MSI2C_MasterPollingSendAbb(AUX_REG_TOGGLE1,0x2);
/* channel selection (LSB) */
MSI2C_MasterPollingSendAbb(MADC_RTSELECT_LSB,(list & 0xff));
/* channel selection (MSB) */
MSI2C_MasterPollingSendAbb(MADC_RTSELECT_MSB,(list & 0x700)>>8);
/* START Real Time conversion */
/* TSP Configuration */
ULPD_GsmClockEn(); /* Enable GSM clock in ULPD */
TPU_ClkEnable(1); /* Enable the TPU clock */
/* TPU scenario launch */
TPU_LoadScenario(startADC); /* load first scenario in TPU RAM */
TPU_Reset(0); /* Suppress TPU reset */
for(tempo=0; tempo<5; tempo++); /* Wait before next command */
TPU_Enable(1); /* Enable the TPU */
for(tempo=0; tempo<10000; tempo++); /* Wait loop for TPU scenario to execute */
return (1);
}
/*-------------------------------------------------------------------------------
// NAME : MADC_SP1_poll
//
// DESCRIPTION : Performs an MADC asynchronous conversion of all the channels
//
// PARAMETERS : No
//
// RETURN VALUE: '1' is returned when the conversion is finished
//
// LIMITATIONS : None
//------------------------------------------------------------------------------- */
int MADC_SP1_poll()
{
int madc_busy = 1; /* MADC status bit initialized as busy */
int madc_eocrt = 0; /* MADC real time conversion initialized as working */
int previous_content;
char st[80] ;
/* access to the BCI register page */
MSI2C_AccessPage(BCI_page);
/* Read previous BCICTL3 content */
previous_content = MSI2C_MasterPollingReceiveAbb(BCI_BCICTL3) & 0xff;
/* Set the resistor divider bridges for MESBAT and MESVAC */
MSI2C_MasterPollingSendAbb(BCI_BCICTL3,(previous_content | 0x30));
/* access to the INTH module register page */
MSI2C_AccessPage(INT2_page);
/* mask the end-of-conversion INT2P1 */
MSI2C_MasterPollingSendAbb(INT2_IT2MASKP1L,0x8);
/* access to the MADC module register page */
MSI2C_AccessPage(MADC_page);
/* MADC ON */
MSI2C_MasterPollingSendAbb(AUX_REG_TOGGLE1,0x2);
/* 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));
/* Polling on the BUSY bit */
do
{
madc_busy = MSI2C_MasterPollingReceiveAbb(MADC_CTRL_P1) & 0x1;
madc_eocrt = (MSI2C_MasterPollingReceiveAbb(MADC_CTRL_P1) & 0x4)>>2;
sprintf(st,"\r\n BUSY bit = %d , expected '0' when conversion finished\r\n",madc_busy);
SERI_Send(st);
sprintf(st,"\r EOCRT bit = %d , expected '1' when conversion finished\r\n\n",madc_eocrt);
SERI_Send(st);
} while ((madc_busy)||(!madc_eocrt));
/* access to the INTH module register page */
MSI2C_AccessPage(INT2_page);
/* unmask the end-of-conversion INT2P1 */
MSI2C_MasterPollingSendAbb(INT2_IT2MASKP1L,0x0);
return (1);
}
/*-------------------------------------------------------------------------------
// NAME : MADC_SP1_INT2
//
// DESCRIPTION : Performs an MADC asynchronous conversion of all the channels
//
// PARAMETERS : No
//
// RETURN VALUE: '1' is returned when the conversion is finished
//
// LIMITATIONS : None
//------------------------------------------------------------------------------- */
int MADC_SP1_INT2()
{
int previous_content;
/* access to the BCI register page */
MSI2C_AccessPage(BCI_page);
/* Read previous BCICTL3 content */
previous_content = MSI2C_MasterPollingReceiveAbb(BCI_BCICTL3) & 0xff;
/* Set the resistor divider bridges for MESBAT and MESVAC */
MSI2C_MasterPollingSendAbb(BCI_BCICTL3,(previous_content | 0x30));
/* access to the INTH module register page */
MSI2C_AccessPage(INT2_page);
/* enable all INT2P1 sources */
MSI2C_MasterPollingSendAbb(INT2_IT2MASKP1L,0x00);
/* access to the MADC module register page */
MSI2C_AccessPage(MADC_page);
/* MADC ON */
MSI2C_MasterPollingSendAbb(AUX_REG_TOGGLE1,0x2);
/* 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 : 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;
if(current_ref <= 750)
{
G = 10;
cgain4 = 0x0;
}
else
{
G = 4;
cgain4 = 0x10;
}
idacdata = (short)((255*(0.22*current_ref*G + global_offset))/1750);
/* 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) */
MSI2C_MasterPollingSendAbb(BCI_BCICTL2,(0xE0| cgain4 | USB_AC));
/* Selects Constant Current charge mode and starts the charge */
MSI2C_MasterPollingSendAbb(BCI_BCICTL1,0x3);
}
/*-------------------------------------------------------------------------------
// 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;
/* reference voltage limitation to not damage Li-Ion real battery */
if(voltage_ref > 4200)
{
voltage_ref = 4200;
SERI_Send("\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);
}
/*-------------------------------------------------------------------------------
// NAME : Start_BB_charge
//
// DESCRIPTION : Start the back-up battery charging
//
// PARAMETERS : End of BB charging threshold
//
// RETURN VALUE:
//
// LIMITATIONS : None
//------------------------------------------------------------------------------- */
void Start_BB_charge(int threshold)
{
/* Selects the PMC_SLAVE register page */
MSI2C_AccessPage(PMC_SLAVE_page);
/* Sets the end-of-charge threshold and enables the back-up battery charging */
MSI2C_MasterPollingSendAbb(PMC_SLAVE_BBSPOR_CFG,(0x18 | (threshold << 1)));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -