-
Notifications
You must be signed in to change notification settings - Fork 0
Battery Monitor Test Code #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tanmay-s1
wants to merge
11
commits into
main
Choose a base branch
from
tanmay_monitor_testing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0b75f04
made a branch
tanmay-s1 b0b2094
file structure
tanmay-s1 0676fd4
added files to test
tanmay-s1 028ecfe
test code
tanmay-s1 25747b6
Merge branch 'main' of https://github.com/UWCubeSat/HS2-EPS-flightSW …
tanmay-s1 d773695
added new versions of bq code
tanmay-s1 76a6cd6
typo
tanmay-s1 2ec2123
current broken code
tanmay-s1 7892853
functions compile now
tanmay-s1 7130b7c
code works for ~30 seconds
tanmay-s1 030a48e
functional code
tanmay-s1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
247 changes: 247 additions & 0 deletions
247
HS2-EPS-BatteryManager/test/test_batterymonitor/BQ25756.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| { | ||
| 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); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
54
HS2-EPS-BatteryManager/test/test_batterymonitor/BQ25756_reg.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add commit changes like this :)