Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of workinQM_5thJan_azad by
EPS.cpp
- Committer:
- sakthipriya
- Date:
- 2015-12-25
- Revision:
- 2:c823d84b4cb0
- Parent:
- 1:446a959e36ce
- Child:
- 3:07e15677a75c
File content as of revision 2:c823d84b4cb0:
#include "EPS.h"
#include "pin_config.h"
/***********************************************global variable declaration***************************************************************/
extern uint32_t BAE_STATUS;
extern uint32_t BAE_ENABLE;
//m_I2C.frequency(10000)
const char RCOMP0= 0x97;
BAE_HK_actual actual_data;
BAE_HK_quant quant_data;
BAE_HK_min_max bae_HK_minmax;
BAE_HK_arch arch_data;
//......................................Peripheral declarations.........................................................//
Serial pc_eps(USBTX,USBRX);
I2C m_I2C(PIN85,PIN84);
DigitalOut TRXY(TRXY_DR_EN); //active high
DigitalOut TRZ(TRZ_DR_EN); //active high
DigitalOut EN3V3A(ENBL3V3A);
DigitalOut EN_BTRY_HT(BATT_HEAT);
//DigitalIn BTRY_HT_OUTPUT(BATT_HEAT_OUTPUT);
AnalogIn Vbatt_ang(VBATT);
SPI spi_bt(PIN99,PIN100,PIN98); //MOSI,MISO,SLK
DigitalOut ssn1(PIN19); //Slave select1
DigitalOut ssn2(PIN21);//Slave select2
//DigitalOut PS(PTB0);
//DigitalOut HS(PTB1);
//*********************************************************flags********************************************************//
extern char EPS_INIT_STATUS ;
extern char EPS_BATTERY_GAUGE_STATUS ;
extern char EPS_MAIN_STATUS;
extern char EPS_BATTERY_TEMP_STATUS ;
extern char EPS_STATUS ;
extern char EPS_BATTERY_HEAT_ENABLE ;
//........................................... FUCTIONS.................................................//
void FCTN_EPS_INIT()
{
printf("\n\r eps init \n");
EPS_INIT_STATUS = 's' ; //set EPS_INIT_STATUS flag
// FLAG();
FCTN_BATTERYGAUGE_INIT();
//FCTN_BATTTEMP_INIT();
EN3V3A = 1; //enable dc dc converter A
char value=alertFlags();
unsigned short value_u= (short int )value;
value_u &=0x0001;
if(value_u ==0x0001) // battery gauge not initialised
{
actual_data.power_mode = 1;
EPS_BATTERY_GAUGE_STATUS = 'c'; //clear EPS_BATTERY_GAUGE_STATUS
}
else
{
actual_data.Batt_gauge_actual[1] = soc();
actual_data.Batt_voltage_actual = Vbatt_ang.read()*3.3;
FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]);
EPS_BATTERY_GAUGE_STATUS = 's'; //set EPS_BATTERY_GAUGE_STATUS
}
EPS_INIT_STATUS = 'c' ; //clear EPS_INIT_STATUS flag
}
//----------------------------------------------------Power algo code--------------------------------------------------------------------//
void FCTN_EPS_POWERMODE(float soc) //dummy algo
{
if(soc >= 80)
actual_data.power_mode = 4;
else if(soc >= 70 & soc < 80)
actual_data.power_mode = 3;
else if(soc >= 60 & soc < 70)
actual_data.power_mode = 2;
else if(soc < 60)
actual_data.power_mode = 1;
}
//...................................................HK...........................................//
int quantiz(float start,float step,float x)
{
int y=(x-start)/step;
if(y<=0)y=0;
if(y>=255)y=255;
return y;
}
void HK_main()
{
}
//............................................BATTERY GAUGE......................................//
void FCTN_BATTERYGAUGE_INIT()
{
disable_sleep();
disable_hibernate();
socChangeAlertEnabled(true); //enabling alert on soc changing by 1%
emptyAlertThreshold(32);//setting empty alert threshold to 32% soc
vAlertMinMaxThreshold();//set min, max value of Valrt register
vResetThresholdSet();//set threshold voltage for reset
vResetAlertEnabled(true);//enable alert on reset for V < Vreset
}
void FCTN_BATTERYGAUGE_MAIN(float Battery_parameters[4])
{
printf("\n\r battery gauge \n");
float temp=25; //=Battery_temp (from temp sensor on battery board) //value of battery temperature in C currently given a dummy value. Should be updated everytime.
tempCompensation(temp);
Battery_parameters[0]=vcell();
Battery_parameters[1]=soc();
Battery_parameters[2]=crate();
printf("\nVcell=%f",vcell()); //remove this for final code
printf("\nSOC=%f",soc()); //remove this for final code
printf("\nC_rate=%f",crate()); //remove this for final code
if (alerting()== true) //alert is on
{
Battery_parameters[3]=alertFlags();
clearAlert();//clear alert
clearAlertFlags();//clear all alert flags
}
}
unsigned short read(char reg)
{
//Create a temporary buffer
char buff[2];
//Select the register
m_I2C.write(m_ADDR, ®, 1, true);
//Read the 16-bit register
m_I2C.read(m_ADDR, buff, 2);
//Return the combined 16-bit value
return (buff[0] << 8) | buff[1];
}
unsigned short read_soc(char reg , bool ack = true)
{
//Create a temporary buffer
char buff[2];
//Select the register
m_I2C.write(m_ADDR, ®, 1, true);
//Read the 16-bit register
ack = m_I2C.read(m_ADDR, buff, 2);
//Return the combined 16-bit value
return (buff[0] << 8) | buff[1];
}
void write(char reg, unsigned short data)
{
//Create a temporary buffer
char buff[3];
//Load the register address and 16-bit data
buff[0] = reg;
buff[1] = data >> 8;
buff[2] = data;
//Write the data
m_I2C.write(m_ADDR, buff, 3);
}
// Command the MAX17049 to perform a power-on reset
void reset()
{
//Write the POR command
write(REG_CMD, 0x5400);
}
// Command the MAX17049 to perform a QuickStart
void quickStart()
{
//Read the current 16-bit register value
unsigned short value = read(REG_MODE);
//Set the QuickStart bit
value |= (1 << 14);
//Write the value back out
write(REG_MODE, value);
}
//disable sleep
void disable_sleep()
{
unsigned short value = read(REG_MODE);
value &= ~(1 << 13);
write(REG_MODE, value);
}
//disable the hibernate of the MAX17049
void disable_hibernate()
{
write(REG_HIBRT, 0x0000);
}
// Enable or disable the SOC 1% change alert on the MAX17049
void socChangeAlertEnabled(bool enabled)
{
//Read the current 16-bit register value
unsigned short value = read(REG_CONFIG);
//Set or clear the ALSC bit
if (enabled)
value |= (1 << 6);
else
value &= ~(1 << 6);
//Write the value back out
write(REG_CONFIG, value);
}
void compensation(char rcomp)
{
//Read the current 16-bit register value
unsigned short value = read(REG_CONFIG);
//Update the register value
value &= 0x00FF;
value |= rcomp << 8;
//Write the value back out
write(REG_CONFIG, value);
}
void tempCompensation(float temp)
{
//Calculate the new RCOMP value
char rcomp;
if (temp > 20.0) {
rcomp = RCOMP0 + (temp - 20.0) * -0.5;
} else {
rcomp = RCOMP0 + (temp - 20.0) * -5.0;
}
//Update the RCOMP value
compensation(rcomp);
}
// Command the MAX17049 to de-assert the ALRT pin
void clearAlert()
{
//Read the current 16-bit register value
unsigned short value = read(REG_CONFIG);
//Clear the ALRT bit
value &= ~(1 << 5);
//Write the value back out
write(REG_CONFIG, value);
}
//Set the SOC empty alert threshold of the MAX17049
void emptyAlertThreshold(char threshold)
{
//Read the current 16-bit register value
unsigned short value = read(REG_CONFIG);
//Update the register value
value &= 0xFFE0;
value |= 32 - threshold;
//Write the 16-bit register
write(REG_CONFIG, value);
}
// Set the low and high voltage alert threshold of the MAX17049
void vAlertMinMaxThreshold()
{
//Read the current 16-bit register value
unsigned short value = read(REG_VALRT);
//Mask off the old value
value = 0x96D2;
//Write the 16-bit register
write(REG_VALRT, value);
}
// Set the reset voltage threshold of the MAX17049
void vResetThresholdSet()
{
//Read the current 16-bit register value
unsigned short value = read(REG_VRESET_ID);
//Mask off the old //value
value &= 0x00FF;//Dis=0
value |= 0x9400;//corresponding to 2.5 V
//Write the 16-bit register
write(REG_VRESET_ID, value);
}
// Enable or disable the voltage reset alert on the MAX17049
void vResetAlertEnabled(bool enabled)
{
//Read the current 16-bit register value
unsigned short value = read(REG_STATUS);
//Set or clear the EnVR bit
if (enabled)
value |= (1 << 14);
else
value &= ~(1 << 14);
//Write the value back out
write(REG_STATUS, value);
}
//Get the current alert flags on the MAX17049
//refer datasheet-status registers section to decode it.
char alertFlags()
{
//Read the 16-bit register value
unsigned short value = read(REG_STATUS);
//Return only the flag bits
return (value >> 8) & 0x3F;
}
// Clear all the alert flags on the MAX17049
void clearAlertFlags()
{
//Read the current 16-bit register value
unsigned short value = read(REG_STATUS);
//Clear the specified flag bits
value &= ~( 0x3F<< 8);
//Write the value back out
write(REG_STATUS, value);
}
// Get the current cell voltage measurement of the MAX17049
float vcell()
{
//Read the 16-bit raw Vcell value
unsigned short value = read(REG_VCELL);
//Return Vcell in volts
return value * 0.000078125*2;
}
// Get the current state of charge measurement of the MAX17049 as a float
float soc()
{
//unsigned short value;
char buff[2];
bool ack;
//Read the 16-bit raw SOC value
unsigned short value = read_soc(REG_SOC, ack);
//Return SOC in percent
if(ack == 0)
return value * 0.00390625;
else
return 200;
}
// Get the current C rate measurement of the MAX17049
float crate()
{
//Read the 16-bit raw C/Rate value
short value = read(REG_CRATE);
//Return C/Rate in %/hr
return value * 0.208;
}
// Determine whether or not the MAX17049 is asserting the ALRT pin
bool alerting()
{
//Read the 16-bit register value
unsigned short value = read(REG_CONFIG);
//Return the status of the ALRT bit
if (value & (1 << 5))
return true;
else
return false;
}
//.............................Battery board Temp sensor........................//
void FCTN_BATTTEMP_INIT()
{
ssn1=1;ssn2=1;
//PS=0;
//HS=0;
spi_bt.format(8,3);
spi_bt.frequency(1000000);
}
void FCTN_BATT_TEMP_SENSOR_MAIN(float temp[2])
{
uint8_t MSB, LSB;
int16_t bit_data;
float sensitivity=0.0078125; //1 LSB = sensitivity degree celcius
wait_ms(320);
ssn1=0;
spi_bt.write(0x80);//Reading digital data from Sensor 1
LSB = spi_bt.write(0x00);//LSB first
wait_ms(0);
MSB = spi_bt.write(0x00);
wait_ms(10);
pc_eps.printf("%d %d\n",MSB,LSB);
bit_data= ((uint16_t)MSB<<8)|LSB;
wait_ms(10);
temp[0]=(float)bit_data*sensitivity;//Converting into decimal value
ssn1=1;
wait_ms(10);
ssn2=0;//Reading data from sensor 2
spi_bt.write(0x80);
LSB = spi_bt.write(0x00);
wait_ms(10);
MSB = spi_bt.write(0x00);
wait_ms(10);
bit_data= ((int16_t)MSB<<8)|LSB;
wait_ms(10);
temp[1]=(float)bit_data*sensitivity;
ssn2=1;
}
