Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 247 additions & 0 deletions HS2-EPS-BatteryManager/test/test_batterymonitor/BQ25756.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
// Outer class for BQ25756 should be included here
#include "BQ25756.h"
#include "BatteryMonitor.h"

/**
* @brief Reset register
*
* Reset all the registers to the default value
* by writing REG_RST to 1.
* REG_RST goes back to 0 automatically after writing to 1.
*/

BQ25756::BQ25756()
{
bm = new BatteryMonitor();
}

void BQ25756::resetRegister()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void BQ25756::resetRegister()
// Reset register
// Register reset to default values
void BQ25756::resetRegister()

Add commit changes like this :)

{
uint8_t regRstVal = read8bitRegister(POW_PATH_REV_CONT);
uint8_t writeVal = regRstVal | 0x80;
writeRegister(POW_PATH_REV_CONT, writeVal);

}

// Return
// bool: True if ADC control is enabled
bool BQ25756::ADCControl::isADCEnabled()
{
return (read8bitRegister(ADC_CONT) >> 7);
}

// Get ADC conversion rate
// Return
// bool: True if One-shot conversion, False if Continuous conversion
bool BQ25756::ADCControl::isADCRateOneshot()
{
return (read8bitRegister(ADC_CONT) >> 6);
}

// Check if IBAT ADC is disabled
// Return
// bool : True if IBAT ADC control is disabled, False otherwise
bool BQ25756::ADCControl::isIBAT_ADCDisabled()
{
return ((read8bitRegister(ADC_CHANNEL_CONT) >> 6) & 0x01);
}

// Check if IAC ADC is disabled
// Return
// bool : True if IAC ADC disabled, False otherwise
bool BQ25756::ADCControl::isIAC_ADCDisabled()
{
return ((read8bitRegister(ADC_CHANNEL_CONT) >> 7) & 0x01);
}

// Check if VAC ADC is disabled
// Return
// bool : True if VAC ADC disabled, False otherwise
bool BQ25756::ADCControl::isVAC_ADCDisabled()
{
return ((read8bitRegister(ADC_CHANNEL_CONT) >> 5) & 0x01);
}

// Check if VBAT ADC is disabled
// Return
// bool : True if VBAT ADC disabled, False otherwise
bool BQ25756::ADCControl::isVBAT_ADCDisabled()
{
return ((read8bitRegister(ADC_CHANNEL_CONT) >> 4) & 0x01);
}

// Check if TS ADC is disabled
// Return
// bool : True if TS ADC disabled, False otherwise
bool BQ25756::ADCControl::isTS_ADCDisabled()
{
return ((read8bitRegister(ADC_CHANNEL_CONT) >> 2) & 0x01);
}

// Check if VFB ADC is disabled
// Return
// bool : True if VFB ADC disabled, False otherwise
bool BQ25756::ADCControl::isVFB_ADCDisabled()
{
return ((read8bitRegister(ADC_CHANNEL_CONT) >> 1) & 0x01);
}


//------------------------------ Enable function-------------------------------------------

// Set ADC rate continuous instead of One-shot conversion (default)
// ADC rate should be continuous, otherwise ADC_EN is always cleared
void BQ25756::ADCControl::setADCContinuous()
{
uint8_t currVal = read8bitRegister(ADC_CONT);
uint8_t newVal = currVal & ~(1 << 6);
writeRegister(ADC_CONT, newVal);
}

//Set ADC one-Shot
void BQ25756::ADCControl::setADCOneShot()
{
uint8_t currVal = read8bitRegister(ADC_CONT);
uint8_t newVal = currVal | (1 << 6);
writeRegister(ADC_CONT, newVal);
}

// Enable ADC Control
// ADC should be enabled before reading ADC value
void BQ25756::ADCControl::enableADC()
{
uint8_t currVal = read8bitRegister(ADC_CONT);
uint8_t newVal = currVal | (1 << 7);
writeRegister(ADC_CONT, newVal);
}

// Enable ADC when ADC is one-shot conversion
void BQ25756::ADCControl::enableADCReadingForOneshot()
{
if (!isADCRateOneshot()) {
setADCOneShot();
}

enableADC();

if (read8bitRegister(ADC_CHANNEL_CONT) != 0x00) {
enableAllADCControl();
}
}

// Enable All ADC Channel Control
// This enable ADC Control for IAC, IBAT, VAC, VBAT, TS, VFB
// This function should be called before reading any battery management register
void BQ25756::ADCControl::enableAllADCControl()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = 0x00;
writeRegister(ADC_CHANNEL_CONT, newVal);
}

// Enable IAC ADC Control
void BQ25756::ADCControl::enableIAC_ADC()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = currVal & 0x7F;
writeRegister(ADC_CHANNEL_CONT, newVal);
}

// Enable IBAT ADC Control
void BQ25756::ADCControl::enableIBAT_ADC()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = currVal & 0xBF;
writeRegister(ADC_CHANNEL_CONT, newVal);
}

// Enable VAC ADC Control
void BQ25756::ADCControl::enableVAC_ADC()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = currVal & 0xDF;
writeRegister(ADC_CHANNEL_CONT, newVal);
}

// Enable VBAT ADC Control
void BQ25756::ADCControl::enableVBAT_ADC()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = currVal & 0xEF;
writeRegister(ADC_CHANNEL_CONT, newVal);
}

// Enable TS ADC Control
void BQ25756::ADCControl::enableTS_ADC()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = currVal & 0xFB;
writeRegister(ADC_CHANNEL_CONT, newVal);
}

// Enable VFB ADC Control
void BQ25756::ADCControl::enableVFB_ADC()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = currVal & 0xFD;
writeRegister(ADC_CHANNEL_CONT, newVal);
}


//------------------------------ Disable function-------------------------------------------

// Disable ADC Control
void BQ25756::ADCControl::disableADC()
{
uint8_t currVal = read8bitRegister(ADC_CONT);
uint8_t newVal = currVal & ~(1 << 7);
writeRegister(ADC_CONT, newVal);
}

// Disable IAC ADC Control
void BQ25756::ADCControl::disableIAC_ADC()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = currVal | (1 << 7);
writeRegister(ADC_CHANNEL_CONT, newVal);
}

// Disable IBAT ADC Control
void BQ25756::ADCControl::disableIBAT_ADC()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = currVal | (1 << 6);
writeRegister(ADC_CHANNEL_CONT, newVal);
}

// Disable VAC ADC Control
void BQ25756::ADCControl::disableVAC_ADC()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = currVal | (1 << 5);
writeRegister(ADC_CHANNEL_CONT, newVal);
}

// Disable VBAT ADC Control
void BQ25756::ADCControl::disableVBAT_ADC()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = currVal | (1 << 4);
writeRegister(ADC_CHANNEL_CONT, newVal);
}

// Disable TS ADC Control
void BQ25756::ADCControl::disableTS_ADC()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = currVal | (1 << 2);
writeRegister(ADC_CHANNEL_CONT, newVal);
}

// Disable VFB ADC Control
void BQ25756::ADCControl::disableVFB_ADC()
{
uint8_t currVal = read8bitRegister(ADC_CHANNEL_CONT);
uint8_t newVal = currVal | (1 << 1);
writeRegister(ADC_CHANNEL_CONT, newVal);
}
54 changes: 54 additions & 0 deletions HS2-EPS-BatteryManager/test/test_batterymonitor/BQ25756.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef BQ25756_H
#define BQ25756_H

#include "i2c.h"
#include "BQ25756_reg.h"


class BQ25756 {
public:
BQ25756();
void resetRegister();
class ADCControl {
public:
// bool functions
bool isADCEnabled();
bool isADCRateOneshot();
bool isIBAT_ADCDisabled();
bool isIAC_ADCDisabled();
bool isVAC_ADCDisabled();
bool isVBAT_ADCDisabled();
bool isTS_ADCDisabled();
bool isVFB_ADCDisabled();

// Enable ADC functions
void setADCContinuous();
void setADCOneShot();
void enableADC();
void enableADCReadingForOneshot();
void enableIAC_ADC();
void enableIBAT_ADC();
void enableVAC_ADC();
void enableVBAT_ADC();
void enableTS_ADC();
void enableVFB_ADC();
void enableAllADCControl();

// Disable ADC functions
void disableADC();
void disableIAC_ADC();
void disableIBAT_ADC();
void disableVAC_ADC();
void disableVBAT_ADC();
void disableTS_ADC();
void disableVFB_ADC();
};
class BatteryMonitor;
class HeatShutup;
class SafetyConfig;
class FaultStatus;
ADCControl adc;
BatteryMonitor* bm;
};

#endif
54 changes: 54 additions & 0 deletions HS2-EPS-BatteryManager/test/test_batterymonitor/BQ25756_reg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// define all the register here

#define I2C_BUS_ADDR 0x6B

// Define the Register address which gives you 16 bits data by riting the register

#define CHARGE_VOLT_LIM 0x00 // Charge_Voltage_Limit
#define CHARGE_CURR_LIM 0x02 // Charge_Current_Limit
#define IMP_CURR_DPM_LIM 0x06 // Input_Current_DPM_Limit
#define IMP_VOLT_DPM_LIM 0x08 // Input_Voltage_DPM_Limit
#define REV_IMP_CURR_LIM 0x0A // Reverse_Mode_Input_Current_Limit
#define REV_IMP_VOLT_LIM 0x0C // Reverse_Mode_Input_Voltage_Limit
#define PRECHARGE_CURR_LIM 0x10 // Precharge_Current_Limit
#define TERM_CURR_LIM 0x12 // Termination_Current_Limit

#define VAC_MAX_POW_POINT 0x1F // VAC_Max_Power_Point_Detected

#define IAC_ADC 0x2D // IAC_ADC Register
#define IBAT_ADC 0x2F // IBAT_ADC Register
#define VAC_ADC 0x31 // VAC_ADC Register
#define VBAT_ADC 0x33 // VBAT_ADC Register
#define TS_ADC 0x37 // TS_ADC register
#define VFB_ADC 0x39 // VFB_ADC Register


// THESE REGISTERS READ 8 BITS
#define PRECHARGE_TERM_CONT 0x14 // Precharge_and_Termination_Control
#define TIME_CONT 0x15 // Timer_Control Register
#define THR_STG_CHARGE_CONT 0x16 // Three-Stage_Charge_Control
#define CHARGER_CONT 0x17 // Charger_Control
#define PIN_CONT_REG 0x18 // Pin_Control Register
#define POW_PATH_REV_CONT 0x19 // Power_Path_and_Reverse_Mode_Control
#define MPPT_CONT 0x1A // MPPT_Control Register
#define CHARGE_THRESH_CONT 0x1B // Charging_Threshold_Control
#define CHARGE_REGION_CONT 0x1C // Charging_Region_Behavior_Control
#define REV_THRESH_CONT 0x1D // Reverse_Mode_Threshold_Control
#define REV_UNDERVOLT_CONT 0x1E // Reverse_Undervoltage_Control
#define CHARGER_STATUS_1 0x21 // Charger_Status_1
#define CHARGER_STATUS_2 0x22 // Charger_Status_2
#define CHARGER_STATUS_3 0x23 // Charger_Status_3
#define FAULT_STATUS 0x24 // Fault_Status
#define CHARGER_FLAG_1 0x25 // Charger_Flag_1
#define CHARGER_FLAG_2 0x26 // Charger_Flag_2
#define FAULT_FLAG 0x27 // Fault_Flag Register
#define CHARGER_MASK_1 0x28 // Charger_Mask_1
#define CHARGER_MASK_2 0x29 // Charger_Mask_2
#define FAULT_MASK 0x2A // Fault_Mask
#define ADC_CONT 0x2B // ADC_Control Register
#define ADC_CHANNEL_CONT 0x2C // ADC_Channel_Control Register

#define GATE_DRV_STRG_CONT 0x3B // Gate_Driver_Strength_Control
#define GATE_DRV_DEATTIME_CONT 0x3C // Gate_Driver_Dead_Time_Control
#define PART_INFO 0x3D // Part_Information Register
#define REV_BATT_DISCHARGE_CURR 0x62 // Reverse_Mode_Battery_Discharge_Current
Loading