Commit 9152e208 authored by Projects's avatar Projects

Finished the fuel gauge driver. *Needs testing* with real battery: charge state, remaining time.

parent f735d785
......@@ -90,37 +90,35 @@ uint16_t max17047_get_status(void)
uint16_t max17047_get_voltage(void)
{
uint16_t vcell;
uint16_t tmp;
max17047_read_reg(MAX17047_REG_VCELL, 2, (uint8_t*) &vcell);
max17047_read_reg(MAX17047_REG_VCELL, 2, (uint8_t*) &tmp);
// convert to mV
vcell >>= 3;
vcell *= 5;
uint32_t volt = tmp * 80 >> 10;
return vcell;
return (uint16_t) volt;
}
uint16_t max17047_get_current(void)
int16_t max17047_get_current(void)
{
uint16_t current;
int16_t tmp;
max17047_read_reg(MAX17047_REG_CURRENT, 2, (uint8_t*) &current);
max17047_read_reg(MAX17047_REG_CURRENT, 2, (uint8_t*) &tmp);
// convert to mA
// TODO
uint32_t curr = tmp * 160 >> 10;
return current;
return curr;
}
int8_t max17047_get_temperature(void)
{
// TODO requires configuration of TOFF and TGAIN registers
uint8_t temperature[2];
max17047_read_reg(MAX17047_REG_TEMPERATURE, 2, temperature);
return temperature[0];
return temperature[1];
}
uint8_t max17047_get_charge(void)
......@@ -129,12 +127,19 @@ uint8_t max17047_get_charge(void)
max17047_read_reg(MAX17047_REG_SOC_REP, 2, charge);
return charge[0];
return charge[1];
}
uint16_t max17047_get_time_left(void)
{
// TODO see what is missing?
return 0;
uint16_t time;
max17047_read_reg(MAX17047_REG_TIME_TO_EMPTY, 2, &time);
// convert to minutes
time >>= 5;
time *= 3;
return time;
}
......@@ -32,9 +32,6 @@
// Two-wire protocol address
#define MAX17047_ADDRESS 0x6C
// Sense resistor value (milliohm)
#define MAX17047_SENS_RES 10
// Register addresses
#define MAX17047_REG_STATUS 0x00
#define MAX17047_REG_VALRT_TH 0x01
......@@ -141,7 +138,7 @@ uint8_t max17047_write_reg(uint8_t address, uint8_t length, uint8_t* buffer);
/**
* @brief Reads the status.
* @return Status of max17047.
* @return Status of max17047. Check MAX170474_STS_xxx defines for details.
*/
uint16_t max17047_get_status(void);
......@@ -152,10 +149,11 @@ uint16_t max17047_get_status(void);
uint16_t max17047_get_voltage(void);
/**
* @brief Reads current from the battery.
* @return Current (in milliampers).
* @brief Reads current drawn from the battery.
* @return Current (in milliampers). Negative values represent discharging,
* positive values represent charging.
*/
uint16_t max17047_get_current(void);
int16_t max17047_get_current(void);
/**
* @brief Reads temperature sensor.
......@@ -164,7 +162,7 @@ uint16_t max17047_get_current(void);
int8_t max17047_get_temperature(void);
/**
* @brief Reads the current state of charge.
* @brief Reads state of charge.
* @return State of charge (expressed as percentage: 0-100).
*/
uint8_t max17047_get_charge(void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment