This repository has been archived by the owner on Aug 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bq769x0.h
178 lines (137 loc) · 5.45 KB
/
bq769x0.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Battery management system based on bq769x0 for ARM mbed
* Copyright (c) 2015-2018 Martin Jäger (www.libre.solar)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef BQ769X0_H
#define BQ769X0_H
#include "mbed.h"
#define MAX_NUMBER_OF_CELLS 15
#define MAX_NUMBER_OF_THERMISTORS 3
#define NUM_OCV_POINTS 21
// IC type/size
#define bq76920 1
#define bq76930 2
#define bq76940 3
// output information to serial console for debugging
#define BQ769X0_DEBUG 1
class bq769x0 {
public:
// initialization, status update and shutdown
bq769x0(I2C& bqI2C, PinName alertPin, int bqType = bq76930, int bqI2CAddress = 0x08, bool crc = true);
int checkStatus(); // returns 0 if everything is OK
void update(void);
void boot(PinName bootPin);
void shutdown(void);
// charging control
bool enableCharging(void);
void disableCharging(void);
bool enableDischarging(void);
void disableDischarging(void);
// hardware settings
void setShuntResistorValue(float res_mOhm);
void setThermistorBetaValue(int beta_K);
void resetSOC(int percent = -1); // 0-100 %, -1 for automatic reset based on OCV
void setBatteryCapacity(long capacity_mAh);
void setOCV(int voltageVsSOC[NUM_OCV_POINTS]);
int getNumberOfCells(void);
int getNumberOfConnectedCells(void);
// limit settings (for battery protection)
void setTemperatureLimits(int minDischarge_degC, int maxDischarge_degC, int minCharge_degC, int maxCharge_degC, int hysteresis_degC = 2); // °C
long setShortCircuitProtection(long current_mA, int delay_us = 70);
long setOvercurrentChargeProtection(long current_mA, int delay_ms = 8);
long setOvercurrentDischargeProtection(long current_mA, int delay_ms = 8);
int setCellUndervoltageProtection(int voltage_mV, int delay_s = 1);
int setCellOvervoltageProtection(int voltage_mV, int delay_s = 1);
// balancing settings
void setBalancingThresholds(int idleTime_min = 30, int absVoltage_mV = 3400, int voltageDifference_mV = 20);
void setIdleCurrentThreshold(int current_mA);
// automatic balancing when battery is within balancing thresholds
void enableAutoBalancing(void);
void disableAutoBalancing(void);
// battery status
int getBatteryCurrent(void);
int getBatteryVoltage(void);
int getCellVoltage(int idCell); // from 1 to 15
int getMinCellVoltage(void);
int getMaxCellVoltage(void);
int getAvgCellVoltage(void);
float getTemperatureDegC(int channel = 1);
float getTemperatureDegF(int channel = 1);
float getSOC(void);
int getBalancingStatus(void);
// interrupt handling (not to be called manually!)
void setAlertInterruptFlag(void);
#if BQ769X0_DEBUG
void printRegisters(void);
#endif
private:
// Variables
I2C& _i2c;
Timer _timer;
InterruptIn _alertInterrupt;
int I2CAddress;
int type;
bool crcEnabled;
float shuntResistorValue_mOhm;
int thermistorBetaValue; // typical value for Semitec 103AT-5 thermistor: 3435
int *OCV; // Open Circuit Voltage of cell for SOC 100%, 95%, ..., 5%, 0%
// indicates if a new current reading or an error is available from BMS IC
bool alertInterruptFlag;
int numberOfCells; // number of cells allowed by IC
int connectedCells; // actual number of cells connected
int cellVoltages[MAX_NUMBER_OF_CELLS]; // mV
int idCellMaxVoltage;
int idCellMinVoltage;
long batVoltage; // mV
long batCurrent; // mA
int temperatures[MAX_NUMBER_OF_THERMISTORS]; // °C/10
long nominalCapacity; // mAs, nominal capacity of battery pack, max. 1193 Ah possible
long coulombCounter; // mAs (= milli Coulombs) for current integration
// Current limits (mA)
long maxChargeCurrent;
long maxDischargeCurrent;
int idleCurrentThreshold; // mA
// Temperature limits (°C/10)
int minCellTempCharge;
int minCellTempDischarge;
int maxCellTempCharge;
int maxCellTempDischarge;
int cellTempHysteresis;
// Cell voltage limits (mV)
int maxCellVoltage;
int minCellVoltage;
int balancingMinCellVoltage_mV;
int balancingMaxVoltageDifference_mV;
int adcGain; // uV/LSB
int adcOffset; // mV
int errorStatus;
bool autoBalancingEnabled;
unsigned int balancingStatus; // holds on/off status of balancing switches
int balancingMinIdleTime_s;
unsigned long idleTimestamp;
unsigned int secSinceErrorCounter;
unsigned long interruptTimestamp;
bool cellTempChargeErrorFlag;
bool cellTempDischargeErrorFlag;
// Methods
bool determineAddressAndCrc(void);
void updateVoltages(void);
void updateCurrent(void);
void updateTemperatures(void);
void updateBalancingSwitches(void);
void checkCellTemp(void);
int readRegister(int address);
void writeRegister(int address, int data);
};
#endif // BQ769X0_H