Skip to content
Snippets Groups Projects
Commit 8edbd031 authored by Zoe Pfister's avatar Zoe Pfister :speech_balloon:
Browse files

minor changes in LC709203F (fix warning, add measurement enum)

parent 775d8ddd
No related branches found
No related tags found
3 merge requests!39Merge Develop into Main,!19development into master,!18Power management satellite
...@@ -31,22 +31,21 @@ LC709203F::LC709203F(void) {} ...@@ -31,22 +31,21 @@ LC709203F::LC709203F(void) {}
LC709203F::~LC709203F(void) {} LC709203F::~LC709203F(void) {}
uint8_t i2c_address = LC709203F_I2C_ADDR ; uint8_t i2c_address = LC709203F_I2C_ADDR;
/*! /*!
* @brief Sets up the hardware and initializes I2C * @brief Sets up the hardware and initializes I2C
* @param * @param
* @return True if initialization was successful, otherwise false. * @return True if initialization was successful, otherwise false.
*/ */
bool LC709203F::begin( void ) bool LC709203F::begin(void) {
{ Wire.begin();
Wire.begin(); setPowerMode(LC709203F_POWER_OPERATE);
setPowerMode(LC709203F_POWER_OPERATE); setCellCapacity(LC709203F_APA_1000MAH);
setCellCapacity(LC709203F_APA_1000MAH); setTemperatureMode(LC709203F_TEMPERATURE_I2C);
setTemperatureMode(LC709203F_TEMPERATURE_I2C); setCellProfile(LC709203_NOM3p7_Charge4p2);
setCellProfile(LC709203_NOM3p7_Charge4p2);
return true;
return true;
} }
...@@ -54,11 +53,10 @@ bool LC709203F::begin( void ) ...@@ -54,11 +53,10 @@ bool LC709203F::begin( void )
* @brief Get IC version * @brief Get IC version
* @return 16-bit value read from LC709203F_RO_ICVERSION registers * @return 16-bit value read from LC709203F_RO_ICVERSION registers
*/ */
uint16_t LC709203F::getICversion(void) uint16_t LC709203F::getICversion(void) {
{ uint16_t vers = 0;
uint16_t vers = 0; vers = read16(LC709203F_RO_ICVERSION);
vers = read16(LC709203F_RO_ICVERSION); return vers;
return vers;
} }
...@@ -66,9 +64,8 @@ uint16_t LC709203F::getICversion(void) ...@@ -66,9 +64,8 @@ uint16_t LC709203F::getICversion(void)
* @brief Initialize the RSOC algorithm * @brief Initialize the RSOC algorithm
* @return * @return
*/ */
void LC709203F::initRSOC(void) void LC709203F::initRSOC(void) {
{ write16(LC709203F_WO_INITRSOC, 0xAA55);
write16(LC709203F_WO_INITRSOC, 0xAA55);
} }
...@@ -76,11 +73,10 @@ void LC709203F::initRSOC(void) ...@@ -76,11 +73,10 @@ void LC709203F::initRSOC(void)
* @brief Get battery voltage * @brief Get battery voltage
* @return Cell voltage in milliVolt * @return Cell voltage in milliVolt
*/ */
uint16_t LC709203F::cellVoltage_mV(void) uint16_t LC709203F::cellVoltage_mV(void) {
{ uint16_t mV = 0;
uint16_t mV = 0; mV = read16(LC709203F_RO_CELLVOLTAGE);
mV = read16(LC709203F_RO_CELLVOLTAGE); return 1000 * (mV / 1000.0);
return 1000 * ( mV / 1000.0 ) ;
} }
...@@ -88,22 +84,20 @@ uint16_t LC709203F::cellVoltage_mV(void) ...@@ -88,22 +84,20 @@ uint16_t LC709203F::cellVoltage_mV(void)
* @brief Get cell remaining charge in percent (0-100%) * @brief Get cell remaining charge in percent (0-100%)
* @return point value from 0 to 1000 * @return point value from 0 to 1000
*/ */
uint16_t LC709203F::cellRemainingPercent10(void) uint16_t LC709203F::cellRemainingPercent10(void) {
{ uint16_t percent = 0;
uint16_t percent = 0; percent = read16(LC709203F_RO_ITE);
percent = read16(LC709203F_RO_ITE ); return percent;
return percent ;
} }
/*! /*!
* @brief Get battery state of charge in percent (0-100%) * @brief Get battery state of charge in percent (0-100%)
* @return point value from 0 to 100 * @return point value from 0 to 100
*/ */
uint16_t LC709203F::cellStateOfCharge(void) uint16_t LC709203F::cellStateOfCharge(void) {
{ uint16_t percent = 0;
uint16_t percent = 0; percent = read16(LC709203F_RW_RSOC);
percent = read16(LC709203F_RW_RSOC ); return percent;
return percent ;
} }
...@@ -111,11 +105,10 @@ uint16_t LC709203F::cellStateOfCharge(void) ...@@ -111,11 +105,10 @@ uint16_t LC709203F::cellStateOfCharge(void)
* @brief Get battery thermistor temperature * @brief Get battery thermistor temperature
* @return value from -20 to 60 *C // CdB Needs testing, no thermistor on ESP32_Bat_R2 board * @return value from -20 to 60 *C // CdB Needs testing, no thermistor on ESP32_Bat_R2 board
*/ */
uint16_t LC709203F::getCellTemperature(void) uint16_t LC709203F::getCellTemperature(void) {
{ uint16_t temp = 0;
uint16_t temp = 0; temp = read16(LC709203F_RW_CELLTEMPERATURE);
temp = read16(LC709203F_RW_CELLTEMPERATURE ); return temp;
return temp ;
} }
...@@ -124,9 +117,8 @@ uint16_t LC709203F::getCellTemperature(void) ...@@ -124,9 +117,8 @@ uint16_t LC709203F::getCellTemperature(void)
* @param t The desired mode: LC709203F_TEMPERATURE_I2C or * @param t The desired mode: LC709203F_TEMPERATURE_I2C or
* LC709203F_TEMPERATURE_THERMISTOR * LC709203F_TEMPERATURE_THERMISTOR
*/ */
void LC709203F::setTemperatureMode(lc709203_tempmode_t t) void LC709203F::setTemperatureMode(lc709203_tempmode_t t) {
{ return write16(LC709203F_RW_STATUSBIT, (uint16_t) t);
return write16(LC709203F_RW_STATUSBIT, (uint16_t)t);
} }
...@@ -134,9 +126,8 @@ void LC709203F::setTemperatureMode(lc709203_tempmode_t t) ...@@ -134,9 +126,8 @@ void LC709203F::setTemperatureMode(lc709203_tempmode_t t)
* @brief Set the cell capacity, * @brief Set the cell capacity,
* @param apa The lc709203_adjustment_t enumerated approximate cell capacity * @param apa The lc709203_adjustment_t enumerated approximate cell capacity
*/ */
void LC709203F::setCellCapacity(lc709203_adjustment_t apa) void LC709203F::setCellCapacity(lc709203_adjustment_t apa) {
{ write16(LC709203F_RW_APA, (uint16_t) apa);
write16(LC709203F_RW_APA, (uint16_t)apa);
} }
...@@ -144,9 +135,8 @@ void LC709203F::setCellCapacity(lc709203_adjustment_t apa) ...@@ -144,9 +135,8 @@ void LC709203F::setCellCapacity(lc709203_adjustment_t apa)
* @brief Set the alarm pin to respond to an RSOC percentage level * @brief Set the alarm pin to respond to an RSOC percentage level
* @param percent The threshold value, set to 0 to disable alarm * @param percent The threshold value, set to 0 to disable alarm
*/ */
void LC709203F::setAlarmRSOC(uint8_t percent) void LC709203F::setAlarmRSOC(uint8_t percent) {
{ write16(LC709203F_RW_ALARMRSOC, percent);
write16(LC709203F_RW_ALARMRSOC, percent);
} }
...@@ -154,9 +144,8 @@ void LC709203F::setAlarmRSOC(uint8_t percent) ...@@ -154,9 +144,8 @@ void LC709203F::setAlarmRSOC(uint8_t percent)
* @brief Set the alarm pin to respond to a battery voltage level * @brief Set the alarm pin to respond to a battery voltage level
* @param voltage The threshold value, set to 0 to disable alarm * @param voltage The threshold value, set to 0 to disable alarm
*/ */
void LC709203F::setAlarmVoltage(float voltage) void LC709203F::setAlarmVoltage(float voltage) {
{ write16(LC709203F_RW_ALARMVOLT, voltage * 1000);
write16(LC709203F_RW_ALARMVOLT, voltage * 1000);
} }
...@@ -166,9 +155,8 @@ void LC709203F::setAlarmVoltage(float voltage) ...@@ -166,9 +155,8 @@ void LC709203F::setAlarmVoltage(float voltage)
* @param t The power mode desired * @param t The power mode desired
* @return * @return
*/ */
void LC709203F::setPowerMode(lc709203_powermode_t t) void LC709203F::setPowerMode(lc709203_powermode_t t) {
{ write16(LC709203F_RW_POWERMODE, (uint16_t) t);
write16(LC709203F_RW_POWERMODE, (uint16_t)t);
} }
/*! /*!
...@@ -176,20 +164,18 @@ void LC709203F::setPowerMode(lc709203_powermode_t t) ...@@ -176,20 +164,18 @@ void LC709203F::setPowerMode(lc709203_powermode_t t)
* @param t The profile, Table 8. Normally 1 for 3.7 nominal 4.2V Full carge cells * @param t The profile, Table 8. Normally 1 for 3.7 nominal 4.2V Full carge cells
* @return * @return
*/ */
void LC709203F::setCellProfile(lc709203_cell_profile_t t) void LC709203F::setCellProfile(lc709203_cell_profile_t t) {
{ write16(LC709203F_RW_PROFILE, (uint16_t) t);
write16(LC709203F_RW_PROFILE, (uint16_t)t);
} }
/*! /*!
* @brief Get the thermistor Beta value //For completeness since we have to write it. * @brief Get the thermistor Beta value //For completeness since we have to write it.
* @return The uint16_t Beta value * @return The uint16_t Beta value
*/ */
uint16_t LC709203F::getThermistorBeta(void) uint16_t LC709203F::getThermistorBeta(void) {
{ uint16_t val = 0;
uint16_t val = 0; val = read16(LC709203F_RW_THERMISTORB);
val = read16(LC709203F_RW_THERMISTORB); return val;
return val;
} }
...@@ -198,20 +184,18 @@ uint16_t LC709203F::getThermistorBeta(void) ...@@ -198,20 +184,18 @@ uint16_t LC709203F::getThermistorBeta(void)
* @param b The value to set it to * @param b The value to set it to
* @return * @return
*/ */
void LC709203F::setThermistorB(uint16_t beta) void LC709203F::setThermistorB(uint16_t beta) {
{ write16(LC709203F_RW_THERMISTORB, beta);
write16(LC709203F_RW_THERMISTORB, beta);
} }
std::list<Message> LC709203F::buildMessages() std::list<Message> LC709203F::buildMessages() {
{ std::list<Message> messages;
std::list<Message> messages; float data = cellVoltage_mV() / 1000.0;
float data =cellVoltage_mV()/1000.0; MeasurementData IncrementData{data, 0, {},
//FIXME: use enum instead of string measurementTypeToString.at(MeasurementType::BATTERY_VOLTAGE)};
MeasurementData IncrementData{data, 0, {},"Voltage-Batterie"}; messages.emplace_back(IncrementData, sensorInformation, Time::getInstance().getEpochSeconds());
messages.emplace_back(IncrementData, sensorInformation, Time::getInstance().getEpochSeconds()); return messages;
return messages;
} }
...@@ -231,56 +215,53 @@ std::list<Message> LC709203F::buildMessages() ...@@ -231,56 +215,53 @@ std::list<Message> LC709203F::buildMessages()
* *
* @return The computed CRC8 value. * @return The computed CRC8 value.
*/ */
static uint8_t crc8(uint8_t *data, int len) static uint8_t crc8(uint8_t *data, int len) {
{ const uint8_t POLYNOMIAL(0x07);
const uint8_t POLYNOMIAL(0x07); uint8_t crc(0x00);
uint8_t crc(0x00);
for (int j = len; j; --j) { for (int j = len; j; --j) {
crc ^= *data++; crc ^= *data++;
for (int i = 8; i; --i) { for (int i = 8; i; --i) {
crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1); crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1);
}
} }
} return crc;
return crc;
} }
// writes a 16-bit word (d) to register pointer regAddress // writes a 16-bit word (d) to register pointer regAddress
// when selecting a register pointer to read from, data = 0 // when selecting a register pointer to read from, data = 0
void LC709203F::write16(uint8_t regAddress, uint16_t data) void LC709203F::write16(uint8_t regAddress, uint16_t data) {
{ // Setup array to hold bytes to send including CRC-8
// Setup array to hold bytes to send including CRC-8 uint8_t crcArray[5];
uint8_t crcArray[5]; crcArray[0] = 0x16;
crcArray[0] = 0x16; crcArray[1] = regAddress;
crcArray[1] = regAddress; crcArray[2] = lowByte(data);
crcArray[2] = lowByte(data); crcArray[3] = highByte(data);
crcArray[3] = highByte(data); // Calculate crc of preceding four bytes and place in crcArray[4]
// Calculate crc of preceding four bytes and place in crcArray[4] crcArray[4] = crc8(crcArray, 4);
crcArray[4] = crc8( crcArray, 4 ); // Device address
// Device address Wire.beginTransmission(i2c_address);
Wire.beginTransmission(i2c_address); // Register address
// Register address Wire.write(regAddress);
Wire.write(regAddress); // low byte
// low byte Wire.write(crcArray[2]);
Wire.write(crcArray[2]); // high byte
// high byte Wire.write(crcArray[3]);
Wire.write(crcArray[3]); // Send crc8
// Send crc8 Wire.write(crcArray[4]);
Wire.write(crcArray[4]); Wire.endTransmission();
Wire.endTransmission();
} }
int16_t LC709203F::read16( uint8_t regAddress) int16_t LC709203F::read16(uint8_t regAddress) {
{ int16_t data = 0;
int16_t data = 0; Wire.beginTransmission(i2c_address);
Wire.beginTransmission(i2c_address); Wire.write(regAddress);
Wire.write(regAddress); Wire.endTransmission(false);
Wire.endTransmission(false); Wire.requestFrom(i2c_address, static_cast<uint8_t>(2));
Wire.requestFrom(i2c_address, 2); uint8_t lowByteData = Wire.read();
uint8_t lowByteData = Wire.read(); uint8_t highByteData = Wire.read();
uint8_t highByteData = Wire.read(); data = word(highByteData, lowByteData);
data = word(highByteData, lowByteData); return (data);
return( data );
} }
...@@ -19,54 +19,54 @@ ...@@ -19,54 +19,54 @@
// Registers per datasheet Table 6 // Registers per datasheet Table 6
#define LC709203F_WO_BEFORE_RSOC 0x04 // Executes RSOC initialization with sampled maximum voltage when 0xAA55 is set #define LC709203F_WO_BEFORE_RSOC 0x04 // Executes RSOC initialization with sampled maximum voltage when 0xAA55 is set
#define LC709203F_RW_THERMISTORB 0x06 // Sets B−constant of the thermistor to be measured. #define LC709203F_RW_THERMISTORB 0x06 // Sets B−constant of the thermistor to be measured.
#define LC709203F_WO_INITRSOC 0x07 // Executes RSOC initialization when 0xAA55 is set. #define LC709203F_WO_INITRSOC 0x07 // Executes RSOC initialization when 0xAA55 is set.
#define LC709203F_RW_CELLTEMPERATURE 0x08 // Read or Write Cell Temperature For ESP32-Bar-Rev2 must write temperature, no thermistor on board #define LC709203F_RW_CELLTEMPERATURE 0x08 // Read or Write Cell Temperature For ESP32-Bar-Rev2 must write temperature, no thermistor on board
#define LC709203F_RO_CELLVOLTAGE 0x09 // Read cell voltage #define LC709203F_RO_CELLVOLTAGE 0x09 // Read cell voltage
#define LC709203F_RW_CURRENTDIRECTION 0x0A // Selects Auto/Charge/Discharge mode 0x0000: Auto mode, 0x0001: Charge mode, 0xFFFF: Discharge mode #define LC709203F_RW_CURRENTDIRECTION 0x0A // Selects Auto/Charge/Discharge mode 0x0000: Auto mode, 0x0001: Charge mode, 0xFFFF: Discharge mode
#define LC709203F_RW_APA 0x0B // APA Register, Table 7 #define LC709203F_RW_APA 0x0B // APA Register, Table 7
#define LC709203F_RW_APTHERMISTOR 0x0C // Sets a value to adjust temperature measurement delay timing. Defaults to0x001E #define LC709203F_RW_APTHERMISTOR 0x0C // Sets a value to adjust temperature measurement delay timing. Defaults to0x001E
#define LC709203F_RW_RSOC 0x0D // Read cell indicator to empty in 1% steps #define LC709203F_RW_RSOC 0x0D // Read cell indicator to empty in 1% steps
#define LC709203F_RO_ITE 0x0F // Read cell indicator to empty in 0.1% steps #define LC709203F_RO_ITE 0x0F // Read cell indicator to empty in 0.1% steps
#define LC709203F_RO_ICVERSION 0x11 // Contains the ID number of the IC #define LC709203F_RO_ICVERSION 0x11 // Contains the ID number of the IC
#define LC709203F_RW_PROFILE 0x12 // Adjusts the battery profile for nominal and fully charged voltages, see Table 8 #define LC709203F_RW_PROFILE 0x12 // Adjusts the battery profile for nominal and fully charged voltages, see Table 8
#define LC709203F_RW_ALARMRSOC 0x13 // Alarm on percent threshold #define LC709203F_RW_ALARMRSOC 0x13 // Alarm on percent threshold
#define LC709203F_RW_ALARMVOLT 0x14 // Alarm on voltage threshold #define LC709203F_RW_ALARMVOLT 0x14 // Alarm on voltage threshold
#define LC709203F_RW_POWERMODE 0x15 // Sets sleep/power mode 0x0001: Operational mode 0x0002: Sleep mode #define LC709203F_RW_POWERMODE 0x15 // Sets sleep/power mode 0x0001: Operational mode 0x0002: Sleep mode
#define LC709203F_RW_STATUSBIT 0x16 // Temperature method, 0x0000: I2C mode, 0x0001: Thermistor mode #define LC709203F_RW_STATUSBIT 0x16 // Temperature method, 0x0000: I2C mode, 0x0001: Thermistor mode
#define LC709203F_RO_CELLPROFILE 0x1A // Displays battery profile code #define LC709203F_RO_CELLPROFILE 0x1A // Displays battery profile code
static uint8_t crc8(uint8_t *data, int len); static uint8_t crc8(uint8_t *data, int len);
/*! Approx cell capacity Table 7 */ /*! Approx cell capacity Table 7 */
typedef enum { typedef enum {
LC709203F_APA_100MAH = 0x08, LC709203F_APA_100MAH = 0x08,
LC709203F_APA_200MAH = 0x0B, LC709203F_APA_200MAH = 0x0B,
LC709203F_APA_500MAH = 0x10, LC709203F_APA_500MAH = 0x10,
LC709203F_APA_1000MAH = 0x19, LC709203F_APA_1000MAH = 0x19,
LC709203F_APA_2000MAH = 0x2D, LC709203F_APA_2000MAH = 0x2D,
LC709203F_APA_3000MAH = 0x36, LC709203F_APA_3000MAH = 0x36,
} lc709203_adjustment_t; } lc709203_adjustment_t;
/*! Cell profile */ /*! Cell profile */
typedef enum { typedef enum {
LC709203_NOM3p7_Charge4p2 = 1, LC709203_NOM3p7_Charge4p2 = 1,
LC709203_NOM3p8_Charge4p35 = 3, LC709203_NOM3p8_Charge4p35 = 3,
LC709203_NOM3p8_Charge4p35_Less500mAh = 6, LC709203_NOM3p8_Charge4p35_Less500mAh = 6,
LC709203_ICR18650_SAMSUNG = 5, LC709203_ICR18650_SAMSUNG = 5,
LC709203_ICR18650_PANASONIC = 4 LC709203_ICR18650_PANASONIC = 4
}lc709203_cell_profile_t ; } lc709203_cell_profile_t;
/*! Cell temperature source */ /*! Cell temperature source */
typedef enum { typedef enum {
LC709203F_TEMPERATURE_I2C = 0x0000, LC709203F_TEMPERATURE_I2C = 0x0000,
LC709203F_TEMPERATURE_THERMISTOR = 0x0001, LC709203F_TEMPERATURE_THERMISTOR = 0x0001,
} lc709203_tempmode_t; } lc709203_tempmode_t;
/*! Chip power state */ /*! Chip power state */
typedef enum { typedef enum {
LC709203F_POWER_OPERATE = 0x0001, LC709203F_POWER_OPERATE = 0x0001,
LC709203F_POWER_SLEEP = 0x0002, LC709203F_POWER_SLEEP = 0x0002,
} lc709203_powermode_t; } lc709203_powermode_t;
/*! /*!
...@@ -75,38 +75,56 @@ typedef enum { ...@@ -75,38 +75,56 @@ typedef enum {
*/ */
class LC709203F { class LC709203F {
public: public:
LC709203F(); LC709203F();
~LC709203F();
bool begin( void ); ~LC709203F();
void initRSOC(void);
void setPowerMode(lc709203_powermode_t t); bool begin(void);
void setCellCapacity(lc709203_adjustment_t apa);
void setCellProfile(lc709203_cell_profile_t t);
uint16_t getICversion(void);
uint16_t cellVoltage_mV(void); void initRSOC(void);
uint16_t cellRemainingPercent10(void); // Remaining capacity in increments of 0.1% as integer
uint16_t cellStateOfCharge(void); // In increments of 1% as integer
uint16_t getThermistorBeta(void); void setPowerMode(lc709203_powermode_t t);
void setThermistorB(uint16_t beta);
void setTemperatureMode(lc709203_tempmode_t t); void setCellCapacity(lc709203_adjustment_t apa);
uint16_t getCellTemperature(void);
void setAlarmRSOC(uint8_t percent); void setCellProfile(lc709203_cell_profile_t t);
void setAlarmVoltage(float voltage);
std::list<Message> buildMessages(); uint16_t getICversion(void);
uint16_t cellVoltage_mV(void);
uint16_t cellRemainingPercent10(void); // Remaining capacity in increments of 0.1% as integer
uint16_t cellStateOfCharge(void); // In increments of 1% as integer
uint16_t getThermistorBeta(void);
void setThermistorB(uint16_t beta);
void setTemperatureMode(lc709203_tempmode_t t);
uint16_t getCellTemperature(void);
void setAlarmRSOC(uint8_t percent);
void setAlarmVoltage(float voltage);
std::list<Message> buildMessages();
protected: protected:
void write16( uint8_t regAddress, uint16_t data); void write16(uint8_t regAddress, uint16_t data);
int16_t read16(uint8_t regAddress);
int16_t read16(uint8_t regAddress);
private: private:
const SensorInformation sensorInformation{"LC709203", Protocol::I2C}; const SensorInformation sensorInformation{"LC709203", Protocol::I2C};
enum class MeasurementType {
BATTERY_VOLTAGE,
};
std::map<MeasurementType, const char *> measurementTypeToString = {
{MeasurementType::BATTERY_VOLTAGE, "BATTERY_VOLTAGE"}};
}; };
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment