Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
BQ25713.h
Go to the documentation of this file.
1/**************************************************************************/
11/**************************************************************************/
12
13#ifndef BQ25713_H
14#define BQ25713_H
15
16#include <cmath>
17#include <cstdint>
18
19namespace bq25713 {
20
21using byte = uint8_t;
22
23// Platform specific
24constexpr uint8_t i2cObjectIndex = 0;
25// \Platform specific
26
27constexpr bool usesStopBit = true;
28
29constexpr uint16_t DEVICE_ID = 0x88;
30constexpr uint16_t MANUFACTURER_ID = 0x40;
31
32constexpr uint16_t CHARGE_CURRENT_ADDR = 0x02;
33constexpr uint16_t MAX_CHARGE_VOLTAGE_ADDR = 0x04;
34constexpr uint16_t MINIMUM_SYSTEM_VOLTAGE_ADDR = 0x0C;
35
36constexpr uint16_t OTG_VOLTAGE_ADDR = 0x06;
37constexpr uint16_t OTG_CURRENT_ADDR = 0x08;
38
39constexpr uint16_t INPUT_VOLTAGE_ADDR = 0x0A;
40constexpr uint16_t IIN_HOST_ADDR = 0x0E;
41constexpr uint16_t IIN_DPM_ADDR = 0x24;
42
43constexpr uint16_t CHARGE_OPTION_0_ADDR = 0x00;
44constexpr uint16_t CHARGE_OPTION_1_ADDR = 0x30;
45constexpr uint16_t CHARGE_OPTION_2_ADDR = 0x32;
46constexpr uint16_t CHARGE_OPTION_3_ADDR = 0x34;
47
48constexpr uint16_t PROCHOT_OPTION_0_ADDR = 0x36;
49constexpr uint16_t PROCHOT_OPTION_1_ADDR = 0x38;
50
51constexpr uint16_t ADC_OPTION_ADDR = 0x3A;
52constexpr uint16_t CHARGE_STATUS_ADDR = 0x20;
53constexpr uint16_t PROCHOT_STATUS_ADDR = 0x22;
54constexpr uint16_t ADC_VBUS_PSYS_ADC_ADDR = 0x26;
55constexpr uint16_t ADC_IBAT_ADDR = 0x28;
56constexpr uint16_t CMPIN_ADC_ADDR = 0x2A;
57constexpr uint16_t VBAT_ADC_ADDR = 0x2C;
58
59constexpr uint16_t MANUFACTURER_ID_ADDR = 0x2E;
60constexpr uint16_t DEVICE_ID_ADDR = 0x2F;
61
62constexpr uint16_t OTG_ENABLE_CELL_LOW_VOLTAGE =
63 3550; // minimum single cell voltage at which the OTG will refuse to start
64
66{
67public:
68 BQ25713() {};
69 // Initialise the variable here, but it will be written from the main program
70 static const byte BQ25713addr = 0x6B; // I2C address
71
72 template<typename T> bool readRegEx(T& dataParam)
73 {
74 // This is a function for reading data words.
75 // The number of bytes that make up a word is 2.
76 constexpr uint8_t arrLen = 2;
77 // Create an array to hold the returned data
78 byte valBytes[arrLen];
79
80 if (readDataReg(dataParam.addr, valBytes, arrLen))
81 {
82 // Cycle through array of data
83 dataParam.val0 = (byte)valBytes[0];
84 dataParam.val1 = (byte)valBytes[1];
85 return true;
86 }
87 return false;
88 }
89 // used by external functions to write registers
90 template<typename T> static bool writeRegEx(T dataParam)
91 {
92 byte valBytes[2];
93 valBytes[0] = dataParam.val0;
94 valBytes[1] = dataParam.val1;
95 return writeData(dataParam.addr, 2, valBytes);
96 }
97// macro to generate bit mask to access bits
98#define GETMASK(index, size) (((1 << (size)) - 1) << (index))
99// macro to read bits from variable, using mask
100#define READFROM(data, index, size) (((data) & GETMASK((index), (size))) >> (index))
101// macro to write bits into variable, using mask
102#define WRITETO(data, index, size, value) ((data) = ((data) & (~GETMASK((index), (size)))) | ((value) << (index)))
103// macro to wrap functions for easy access
104// if name is called with empty brackets, read bits and return value
105// if name is prefixed with set_, write value in brackets into bits defined in
106// FIELD
107#define FIELD(data, name, index, size) \
108 inline decltype(data) name() { return READFROM(data, index, size); } \
109 inline void set_##name(decltype(data) value) { WRITETO(data, index, size, value); }
110
111// macro to wrap functions for easy access, read only
112// if name is called with empty brackets, read bits and return value
113#define FIELD_RO(data, name, index, size) \
114 inline decltype(data) name() { return READFROM(data, index, size); }
115
116 // used to set and reset the error flag
117 inline static bool isFlagRaised = false;
118
119 // Base class for register operations
121 {
122 virtual ~IBaseReadRegister() {};
123
124 virtual uint16_t address() const = 0;
125
126 virtual uint16_t minVal() const = 0;
127 virtual uint16_t maxVal() const = 0;
128 virtual float resolution() const = 0;
129
130 virtual uint8_t bitLenght() const = 0;
131 virtual uint8_t offset() const = 0;
132
133 uint16_t mask() const
134 {
135 // convert a bit count to a bit mask
136 uint16_t mask = 1;
137 mask <<= (bitLenght() + 1);
138 return mask - 1;
139 }
140
141 uint16_t get()
142 {
143 byte valBytes[2];
144 if (readDataReg(address(), valBytes, 2))
145 {
146 // Cycle through array of data
147 byte val0 = (byte)valBytes[0];
148 byte val1 = (byte)valBytes[1];
149
150 // fuse them
151 uint16_t res = val1 << 8 | val0;
152 // unpack
153 res >>= offset();
154 res &= mask();
155 // convert to real units
156 return res * resolution() + minVal();
157 }
158 isFlagRaised = true;
159 // error
160 return 0;
161 }
162 };
163
165 {
166 uint16_t set(const uint16_t value)
167 {
168 // convert to authorized values
169 uint16_t constraint = value;
170 if (constraint < minVal())
171 constraint = minVal();
172 else if (constraint > maxVal())
173 constraint = maxVal();
174 constraint -= minVal();
175 // break it down to the correct resolution (integer division)
176 constraint = round(constraint / resolution());
177
178 // convert to binary word
179 uint16_t binaryWord = constraint;
180 binaryWord &= mask(); // mask off unused bits (with &= for 16bits)
181 binaryWord <<= offset(); // offset the register (with <<= for 16 bits)
182
183 if (!writeData16(address(), binaryWord))
184 {
185 isFlagRaised = true;
186 }
187
188 // return the value that was written to the register
189 return (constraint * resolution()) + minVal();
190 }
191 };
192
193 // Base class for distinct stored value operations
195 {
196 virtual uint16_t address() const = 0;
197
198 virtual uint16_t minVal0() const = 0;
199 virtual uint16_t maskVal0() const = 0;
200 virtual uint16_t resolutionVal0() const = 0;
201
202 virtual uint16_t minVal1() const = 0;
203 virtual uint16_t maskVal1() const = 0;
204 virtual uint16_t resolutionVal1() const = 0;
205
206 uint16_t getVal0()
207 {
208 byte valBytes[2];
209 if (readDataReg(address(), valBytes, 2))
210 {
211 // Cycle through array of data
212 byte val0 = (byte)valBytes[0];
213 // fuse them
214 uint16_t res = val0 & maskVal0();
215 // convert to real units
216 return res * resolutionVal0() + minVal0();
217 }
218 isFlagRaised = true;
219 // error
220 return 0;
221 }
222
223 uint16_t getVal1()
224 {
225 byte valBytes[2];
226 if (readDataReg(address(), valBytes, 2))
227 {
228 // Cycle through array of data
229 byte val1 = (byte)valBytes[1];
230 // fuse them
231 uint16_t res = val1 & maskVal1();
232 // convert to real units
233 return res * resolutionVal1() + minVal1();
234 }
235 isFlagRaised = true;
236 // error
237 return 0;
238 }
239 };
240
241 struct Regt
242 {
244 {
245 uint8_t addr = CHARGE_OPTION_0_ADDR;
246 byte val0 = 0x0E;
247 byte val1 = 0x82;
248 // To disable the hiccup mode during the system short protection
249 FIELD(val0, SYS_SHORT_DISABLE, 0x06, 0x01)
250 // Learn mode. Discharges with power connected. Default disabled
251 FIELD(val0, EN_LEARN, 0x05, 0x01)
252 // Current shunt amplifier 20x or 40x. Default is 20x
253 FIELD(val0, IADPT_GAIN, 0x04, 0x01)
254 // Bat current shunt amplifier 8x or 16x. Default is 16x
255 FIELD(val0, IBAT_GAIN, 0x03, 0x01)
256 // LDO mode - use of pre charge. Default is precharge enabled
257 FIELD(val0, EN_LDO, 0x02, 0x01)
258 // Enable IDPM current control. Default is high(enabled)
259 FIELD(val0, EN_IDPM, 0x01, 0x01)
260 // Inhibit charging. Default is low(enabled)
261 FIELD(val0, CHRG_INHIBIT, 0x00, 0x01)
262 // Enable low power mode. Default is enabled
263 FIELD(val1, EN_LWPWR, 0x07, 0x01)
264 // Watchdog timer. Reset it by setting ChargeCurrent or
265 // MaxChargeVoltage commands 00b: Disable Watchdog Timer 01b: Enabled, 5
266 // sec 10b: Enabled, 88 sec 11b: Enable Watchdog Timer, 175 sec <default
267 // at POR>
268 FIELD(val1, WDTMR_ADJ, 0x05, 0x02)
269 // Disable IDPM. Default is low (IDPM enabled)
270 FIELD(val1, IDPM_AUTO_DISABLE, 0x04, 0x01)
271 // Turn Chrgok on if OTG is enabled. Default is low
272 FIELD(val1, OTG_ON_CHRGOK, 0x03, 0x01)
273 // Out of audio switch frequency. Default is low(disabled)
274 FIELD(val1, EN_OOA, 0x02, 0x01)
275 // PWM switching frequency, 800kHz or 1.2MHz. Default is high (800kHz)
276 FIELD(val1, PWM_FREQ, 0x01, 0x01)
277 // PTM mode input voltage and current ripple reduction
278 FIELD(val1, LOW_PTM_RIPPLE, 0x00, 0x01)
279 } chargeOption0;
281 {
282 uint8_t addr = CHARGE_OPTION_1_ADDR;
283 byte val0 = 0x11;
284 byte val1 = 0x02;
285 // Internal comparator reference 2.3V or 1.2V. Default is 2.3V
286 FIELD(val0, CMP_REF, 0x07, 0x01)
287 // Internal comparator polarity
288 FIELD(val0, CMP_POL, 0x06, 0x01)
289 // Internal comparator deglitch time; off, 1us, 2ms, 5s. Default is 1us
290 FIELD(val0, CMP_DEG, 0x04, 0x02)
291 // Force power path to switch off. Default is disabled
292 FIELD(val0, FORCE_LATCHOFF, 0x03, 0x01)
293 // Discharge SRN pin for shipping. Default is disabled
294 FIELD(val0, EN_SHIP_DCHG, 0x01, 0x01)
295 // Automatically charge for 30mins at 128mA if voltage is below min.
296 // Default enabled.
297 FIELD(val0, AUTO_WAKEUP_EN, 0x00, 0x01)
298 // Enable IBAT output buffer. Default is disabled
299 FIELD(val1, EN_IBAT, 0x07, 0x01)
300 // PROCHOT during battery only; disabled, IDCHG, VSYS. Default is
301 // disabled
302 FIELD(val1, EN_PROCHOT_LPWR, 0x05, 0x02)
303 // Enable PSYS buffer. Default is disabled
304 FIELD(val1, EN_PSYS, 0x04, 0x01)
305 // Charge sense resistor; 10mR or 20mR. Default is 10mR
306 FIELD(val1, RSNS_RAC, 0x03, 0x01)
307 // Input sense resistor; 10mR or 20mR. Default is 10mR
308 FIELD(val1, RSNS_RSR, 0x02, 0x01)
309 // PSYS gain; 0.25uA/W or 1uA/W. Default is 1uA/W
310 FIELD(val1, PSYS_RATIO, 0x01, 0x01)
311 // elect the ILIM_HIZ pin function
312 // 0b: charger enters HIZ mode when pull low the ILIM_HIZ pin. <default at POR>
313 // 1b: charger enters PTM when pull low the ILIM_HIZ pin
314 FIELD(val1, PTM_PINSEL, 0x00, 0x01)
315 } chargeOption1;
317 {
318 uint8_t addr = CHARGE_OPTION_2_ADDR;
319 byte val0 = 0xB7;
320 byte val1 = 0x02;
321 // Allow ILIM_HIZ pin to set current limit. Default is enabled
322 FIELD(val0, EN_EXTILIM, 0x07, 0x01)
323 // Function of IBAT pin; discharge or charge. Default is discharge
324 FIELD(val0, EN_ICHG_IDCHG, 0x06, 0x01)
325 // Over Current Protection for Q2 by sensing VDS; 210mV or 150mV.
326 // Default is 150mV
327 FIELD(val0, Q2_OCP, 0x05, 0x01)
328 // Over Current Protection for input between ACP and ACN; 210mV or
329 // 150mV. default is 150mV
330 FIELD(val0, ACX_OCP, 0x04, 0x01)
331 // Input adapter OCP enable. Default is disabled
332 FIELD(val0, EN_ACOC, 0x03, 0x01)
333 // Input adapter OCP disabled current limit; 125% of ICRIT or 210% of
334 // ICRIT. Default is 210%
335 FIELD(val0, ACOC_VTH, 0x02, 0x01)
336 // Bat OCP; disabled or related to PROCHOT IDCHG. Default is IDPM
337 FIELD(val0, EN_BATOC, 0x01, 0x01)
338 // OCP related to PROCHOT IDCHG; 125% or 200%. Default is 200%
339 FIELD(val0, BATOC_VTH, 0x00, 0x01)
340 // Input overload time; 1ms, 2mS, 10mS, 20mS. Default is 1mS
341 FIELD(val1, PKPWR_TOVLD_DEG, 0x06, 0x02)
342 // Enable peak power mode from over current. Default is disabled
343 FIELD(val1, EN_PKPWR_IDPM, 0x05, 0x01)
344 // Enable peak power mode from under voltage. Default is disabled
345 FIELD(val1, EN_PKPWR_VSYS, 0x04, 0x01)
346 // Indicator that device is in overloading cycle. Default disabled
347 FIELD(val1, PKPWR_OVLD_STAT, 0x03, 0x01)
348 // Indicator that device is in relaxation cycle. Default disabled
349 FIELD(val1, PKPWR_RELAX_STAT, 0x02, 0x01)
350 // Peak power mode overload and relax cycle times; 5mS, 10mS, 20mS,
351 // 40mS. Default is 20mS
352 FIELD(val1, PKPWR_TMAX, 0x00, 0x02)
353 } chargeOption2;
355 {
356 uint8_t addr = CHARGE_OPTION_3_ADDR;
357 byte val0 = 0x00;
358 byte val1 = 0x00;
359 // Enable the conservative VAP mode.
360 // 0b: Disabled <default at POR>
361 // 1b: Enabled
362 FIELD(val0, EN_CON_VAP, 0x06, 0x01)
363 // The selection of the external OTG/VAP pin control.
364 // 0b: the external OTG/VAP pin controls the EN/DIS VAP mode
365 // 1b: the external OTG/VAP pin controls the EN/DIS OTG mode <default at POR>
366 FIELD(val0, OTG_VAP_MODE, 0x05, 0x01)
367 // 4 levels inductor average current clamp.
368 // 00b: 6A
369 // 01b: 10A
370 // 10b: 15A <default at POR>
371 // 11b: Disabled
372 FIELD(val0, IL_AVG, 0x03, 0x02)
373 // Selection of the different OTG output voltage range.
374 // 0b: VOTG high range 4.28 V - 20.8 V <default at POR>
375 // 1b: VOTG low range 3 V - 19.52 V
376 FIELD(val0, OTG_RANGE_LOW, 0x02, 0x01)
377 // Control BAT FET during Hi-Z state. Default is disabled
378 FIELD(val0, BATFETOFF_HIZ, 0x01, 0x01)
379 // PSYS function during OTG mode. PSYS = battery discharge - IOTG or
380 // PSYS = battery discharge. Default 0
381 FIELD(val0, PSYS_OTG_IDCHG, 0x00, 0x01)
382 // Enable Hi-Z(low power) mode. Default is disabled
383 FIELD(val1, EN_HIZ, 0x07, 0x01)
384 // Reset registers. Set this bit to 1 to reset all other registers
385 FIELD(val1, RESET_REG, 0x06, 0x01)
386 // Reset VINDPM register. Default is idle (0)
387 FIELD(val1, RESET_VINDPM, 0x05, 0x01)
388 // Enable OTG mode to output power to VBUS. EN_OTG pin needs to be high.
389 // Default is disabled.
390 FIELD(val1, EN_OTG, 0x04, 0x01)
391 // Enable Input Current Optimiser. Default is disabled
392 FIELD(val1, EN_ICO_MODE, 0x03, 0x01)
393 } chargeOption3;
395 {
396 uint8_t addr = PROCHOT_OPTION_0_ADDR;
397 byte val0 = 0x50;
398 byte val1 = 0x92;
399 // VSYS Threshold to trigger discharging VBUS in VAP mode.
400 // Measure on VSYS with fixed 5-μs deglitch time. Trigger when SYS pin voltage is
401 // below the thresholds.
402 // 2S - 4S battery
403 // 0000b - 1111b: 5.9 V - 7.4V with 0.1 V step size.
404 // 1S battery
405 // 0000b - 0111b: 3.1 V - 3.8 V with 0.1 V step size.
406 // 1000b - 1111b: 3.1 V - 3.8 V with 0.1 V step size.
407 FIELD(val0, VSYS_TH1, 0x04, 0x03)
408 // VSYS Threshold to assert /PROCHOT_VSYS.
409 // Measure on VSYS with fixed 5-μs deglitch time. Trigger when SYS pin voltage is
410 // below the thresholds.
411 // 2S - 4S battery
412 // 00b: 5.9V; 01b: 6.2V <default at POR>;
413 // 10b: 6.5V; 11b: 6.8V.
414 // 1S battery
415 // 00b: 3.1V; 01b: 3.3V <default at POR>;
416 // 10b: 3.5V; 11b: 3.7V
417 FIELD(val0, VSYS_TH2, 0x02, 0x02)
418 // INOM deglitch time; 1ms or 50ms. Default is 1ms
419 FIELD(val0, INOM_DEG, 0x01, 0x01)
420 // Enable the lower threshold of the PROCHOT_VDPM comparator
421 // 0b: the threshold of the PROCHOT_VDPM comparator follows the same VinDPM
422 // REG0x0A/0B() setting.
423 // 1b: the threshold of the PROCHOT_VDPM comparator is lower and determined by
424 // REG0x37[0] setting. <default at POR>
425 FIELD(val0, LOWER_PROCHOT_VDPM, 0x00, 0x01)
426 // ILIM2 threshold as percentage of IDPM; 110%-230%(5% step),
427 // 250%-450%(50% step). Default is 150%
428 FIELD(val1, ILIM2_VTH, 0x03, 0x05)
429 // ICRIT deglitch time. ICRIT is 110% of ILIM2; 15us, 100us, 400us,
430 // 800us. Default is 100us.
431 FIELD(val1, ICRIT_DEG, 0x01, 0x02)
432 // Lower threshold of the PROCHOT_VDPM comparator
433 // When REG0x36[0]=1, the threshold of the PROCHOT_VDPM comparator is
434 // determined by this bit setting.
435 // 0b: 80% of VinDPM threshold <default at POR>.
436 // 1b: 90% of VinDPM threshold
437 FIELD(val1, PROCHOT_VDPM_80_90, 0x00, 0x00)
438 } prochotOption0;
440 {
441 uint8_t addr = PROCHOT_OPTION_1_ADDR;
442 byte val0 = 0x20;
443 byte val1 = 0x41;
444 // When all the REG0x38[7:0] bits are 0, PROCHOT function is disabled.
445 // Bit7 PP_VDPM detects VBUS voltage
446 FIELD(val0, PROCHOT_PROFILE_VDPM, 0x07, 0x01)
447 // PROCHOT profile comparator. Default is disabled.
448 FIELD(val0, PROCHOT_PROFILE_COMP, 0x06, 0x01)
449 // Prochot is triggered if ICRIT threshold is reached. Default enabled.
450 FIELD(val0, PROCHOT_PROFILE_ICRIT, 0x05, 0x01)
451 // Prochot is triggered if INOM threshold is reached. Default disabled.
452 FIELD(val0, PROCHOT_PROFILE_INOM, 0x04, 0x01)
453 // Enable battery Current Discharge current reading. Default is
454 // disabled.
455 FIELD(val0, PROCHOT_PROFILE_IDCHG, 0x03, 0x01)
456 // Prochot is triggered if VSYS threshold is reached. Default disabled.
457 FIELD(val0, PROCHOT_PROFILE_VSYS, 0x02, 0x01)
458 // PROCHOT will be triggered if the battery is removed. Default is
459 // disabled.
460 FIELD(val0, PROCHOT_PROFILE_BATPRES, 0x01, 0x01)
461 // PROCHOT will be triggered if the adapter is removed. Default is
462 // disabled.
463 FIELD(val0, PROCHOT_PROFILE_ACOK, 0x00, 0x01)
464 // range 0 A to 32256 mA, step 512 mA. There is a 128 mA offset. Measure current between SRN and SRP.
465 FIELD(val1, IDCHG_VTH, 0x02, 0x06)
466 // IDCHG deglitch time; 1.6ms, 100us, 6ms, 12ms. Default is 100us.
467 FIELD(val1, IDCHG_DEG, 0x00, 0x02)
468 } prochotOption1;
470 {
471 uint8_t addr = ADC_OPTION_ADDR;
472 byte val0 = 0x00;
473 byte val1 = 0x20;
474 // Enable comparator voltage reading. Default is disabled.
475 FIELD(val0, EN_ADC_CMPIN, 0x07, 0x01)
476 // Enable VBUS voltage reading. Default is disabled.
477 FIELD(val0, EN_ADC_VBUS, 0x06, 0x01)
478 // Enable PSYS voltage reading for calculating system power. Default is
479 // disabled.
480 FIELD(val0, EN_ADC_PSYS, 0x05, 0x01)
481 // Enable Current In current reading. Default is disabled.
482 FIELD(val0, EN_ADC_IIN, 0x04, 0x01)
483 // Enable battery Current Discharge current reading. Default is
484 // disabled.
485 FIELD(val0, EN_ADC_IDCHG, 0x03, 0x01)
486 // Enable battery Current Charge current reading. Default is disabled.
487 FIELD(val0, EN_ADC_ICHG, 0x02, 0x01)
488 // Enable Voltage of System voltage reading. Default is disabled.
489 FIELD(val0, EN_ADC_VSYS, 0x01, 0x01)
490 // Enable Voltage of Battery voltage reading. Default is disabled.
491 FIELD(val0, EN_ADC_VBAT, 0x00, 0x01)
492 // ADC mode; one shot reading or continuous. Default is one shot
493 FIELD(val1, ADC_CONV, 0x07, 0x01)
494 // Start a one shot reading of the ADC. Resets to 0 after reading
495 FIELD(val1, ADC_START, 0x06, 0x01)
496 // ADC scale; 2.04V or 3.06V. Default is 3.06V
497 FIELD(val1, ADC_FULLSCALE, 0x05, 0x01)
498 } aDCOption;
500 {
501 uint8_t addr = CHARGE_STATUS_ADDR;
502 byte val0, val1;
503 // Latched fault flag of adapter over voltage. Default is no fault.
504 FIELD_RO(val0, Fault_ACOV, 0x07, 0x01)
505 // Latched fault flag of battery over current. Default is no fault.
506 FIELD_RO(val0, Fault_BATOC, 0x06, 0x01)
507 // Latched fault flag of adapter over current. Default is no fault.
508 FIELD_RO(val0, Fault_ACOC, 0x05, 0x01)
509 // Latched fault flag of system over voltage protection. Default is no
510 // fault.
511 FIELD(val0, SYSOVP_STAT, 0x04, 0x01)
512 // When SYS is lower than 2.4V, then 7 times restart tries are failed.
513 FIELD(val0, Fault_SYS_SHORT, 0x03, 0x01)
514 // Resets faults latch. Default is disabled
515 FIELD_RO(val0, Fault_Latchoff, 0x02, 0x01)
516 // Latched fault flag of OTG over voltage protection. Default is no
517 // fault.
518 FIELD_RO(val0, Fault_OTG_OVP, 0x01, 0x01)
519 // Latched fault flag of OTG over current protection. Default is no
520 // fault.
521 FIELD_RO(val0, Fault_OTG_UVP, 0x00, 0x01)
522 // Input source present. Default is not connected.
523 FIELD_RO(val1, AC_STAT, 0x07, 0x01)
524 // After ICO routine is done, bit goes to zero.
525 FIELD_RO(val1, ICO_DONE, 0x06, 0x01)
526 // Charger is not operated in VAP mode
527 FIELD_RO(val1, IN_VAP, 0x05, 0x01)
528 // Charger is in VINDPM or OTG mode. Default is not
529 FIELD_RO(val1, IN_VINDPM, 0x04, 0x01)
530 // Device is in current in DPM mode. Default is not
531 FIELD_RO(val1, IN_IINDPM, 0x03, 0x01)
532 // Device is in fast charge mode. Default is not
533 FIELD_RO(val1, IN_FCHRG, 0x02, 0x01)
534 // Device is in pre charge mode. Default is not
535 FIELD_RO(val1, IN_PCHRG, 0x01, 0x01)
536 // Device is in OTG mode. Default is not
537 FIELD_RO(val1, IN_OTG, 0x00, 0x01)
538 } chargerStatus;
540 {
541 uint8_t addr = PROCHOT_STATUS_ADDR;
542 byte val0, val1;
543 // PROCHOT VDPM status. Default is not triggered.
544 FIELD_RO(val0, STAT_VDPM, 0x07, 0x01)
545 // PROCHOT comparator trigger status. Default is not triggered.
546 FIELD_RO(val0, STAT_COMP, 0x06, 0x01)
547 // PROCHOT current critical trigger status. Default is not triggered.
548 FIELD_RO(val0, STAT_ICRIT, 0x05, 0x01)
549 // PROCHOT input current exceeds 110% threshold trigger. Default is not
550 // triggered.
551 FIELD_RO(val0, STAT_INOM, 0x04, 0x01)
552 // PROCHOT discharge current trigger status. Default is not triggered.
553 FIELD_RO(val0, STAT_IDCHG, 0x03, 0x01)
554 // PROCHOT system voltage trigger status. Default is not triggered.
555 FIELD_RO(val0, STAT_VSYS, 0x02, 0x01)
556 // PROCHOT battery removal trigger status. Default is not triggered.
557 FIELD_RO(val0, STAT_Battery_Removal, 0x01, 0x01)
558 // PROCHOT adapter removal trigger status. Default is not triggered.
559 FIELD_RO(val0, STAT_Adapter_Removal, 0x00, 0x01)
560
561 // PROCHOT Pulse Extension Enable. When pulse extension is
562 // enabled, keep the PROCHOT pin voltage LOW until host writes
563 // REG0x23[3] = 0.
564 // 0b: Disable pulse extension <default at POR>
565 // 1b: Enable pulse extension
566 FIELD(val1, EN_PROCHOT_EXIT, 0x06, 0x01)
567 // PROCHOT Pulse Width.
568 // Minimum PROCHOT pulse width when REG0x23[6] = 0
569 // 00b: 100 us
570 // 01b: 1 ms
571 // 10b: 10 ms <default at POR>
572 // 11b: 5 ms
573 FIELD(val1, PROCHOT_WIDTH, 0x04, 0x02)
574 // PROCHOT Pulse Clear.
575 // Clear PROCHOT pulse when 0x23[6] = 1.
576 // 0b: Clear PROCHOT pulse and drive PROCHOT pin HIGH
577 // 1b: Idle <default at POR>
578 FIELD(val1, PROCHOT_CLEAR, 0x03, 0x01)
579 // This status bit reports a failure to load VBUS 7 consecutive times
580 // in VAP mode, which indicates the battery voltage might be not
581 // high enough to enter VAP mode, or the VAP loading current
582 // settings are too high.
583 // 0b: Not is VAP failure <default at POR>
584 // 1b: In VAP failure, the charger exits VAP mode, and latches off
585 // until the host writes this bit to 0.
586 FIELD(val1, STAT_VAP_FAIL, 0x01, 0x01)
587 // When the charger is operated in VAP mode, it can exit VAP by
588 // either being disabled through host, or there is any charger faults.
589 // 0b: PROCHOT_EXIT_VAP is not active <default at POR>
590 // 1b: PROCHOT_EXIT_VAP is active, PROCHOT pin is low until
591 // host writes this status bit to 0.
592 FIELD(val1, STAT_EXIT_VAP, 0x00, 0x01)
593 } prochotStatus;
595 {
596 uint16_t address() const override { return CHARGE_CURRENT_ADDR; }
597
598 virtual uint16_t minVal() const override { return 0; }
599 virtual uint16_t maxVal() const override { return 8128; }
600 virtual float resolution() const override { return 64; }
601
602 virtual uint8_t bitLenght() const override { return 7; }
603 virtual uint8_t offset() const override { return 6; }
604 } chargeCurrent;
606 {
607 uint16_t address() const override { return MAX_CHARGE_VOLTAGE_ADDR; }
608
609 // the min in the doc is set to 1024, but this is wrong in practice
610 virtual uint16_t minVal() const override { return 0; }
611 virtual uint16_t maxVal() const override { return 19200; }
612 virtual float resolution() const override { return 8; }
613
614 virtual uint8_t bitLenght() const override { return 12; }
615 virtual uint8_t offset() const override { return 3; }
616 } maxChargeVoltage;
618 {
619 uint16_t address() const override { return MINIMUM_SYSTEM_VOLTAGE_ADDR; }
620
621 // the min in the doc is set to 1024, but this is wrong in practice
622 virtual uint16_t minVal() const override { return 0; }
623 virtual uint16_t maxVal() const override { return 16128; }
624 virtual float resolution() const override { return 256; }
625
626 virtual uint8_t bitLenght() const override { return 6; }
627 virtual uint8_t offset() const override { return 8; }
628 } minSystemVoltage;
629
630 struct IIN_HOSTt : public IBaseRegister
631 {
632 uint16_t address() const override { return IIN_HOST_ADDR; }
633
634 virtual uint16_t minVal() const override { return 50; }
635 virtual uint16_t maxVal() const override { return 6400; }
636 virtual float resolution() const override { return 50; }
637
638 virtual uint8_t bitLenght() const override { return 7; }
639 virtual uint8_t offset() const override { return 8; }
640 } iIN_HOST;
641 // IIN_DPM register reflects the actual input current limit programmed in
642 // the register, either from host or from ICO.
644 {
645 uint16_t address() const override { return IIN_DPM_ADDR; }
646
647 virtual uint16_t minVal() const override { return 50; }
648 virtual uint16_t maxVal() const override { return 6400; }
649 virtual float resolution() const override { return 50; }
650
651 virtual uint8_t bitLenght() const override { return 7; }
652 virtual uint8_t offset() const override { return 8; }
653 } iIN_DPM;
654 // If the input voltage drops more than the InputVoltage register allows,
655 // the device enters DPM and reduces the charge current
657 {
658 uint16_t address() const override { return INPUT_VOLTAGE_ADDR; }
659
660 virtual uint16_t minVal() const override { return 3200; }
661 virtual uint16_t maxVal() const override { return 19584; }
662 virtual float resolution() const override { return 64; }
663
664 virtual uint8_t bitLenght() const override { return 8; }
665 virtual uint8_t offset() const override { return 6; }
666 } inputVoltage;
668 {
669 uint16_t address() const override { return OTG_VOLTAGE_ADDR; }
670
671 uint16_t get(bool isLowVoltageMode)
672 {
673 // set min value depending on low otg voltage mode
674 minValue = isLowVoltageMode ? 0 : 1280;
675
676 return IBaseRegister::get();
677 }
678
679 uint16_t set(uint16_t val, bool isLowVoltageMode)
680 {
681 // set min value depending on low otg voltage mode
682 minValue = isLowVoltageMode ? 0 : 1280;
683
684 return IBaseRegister::set(val);
685 }
686
687 // min val should be 3000mV, but in practise thats not it
688 virtual uint16_t minVal() const override { return minValue; }
689 virtual uint16_t maxVal() const override { return 20800; }
690 virtual float resolution() const override { return 8.1328125; }
691
692 virtual uint8_t bitLenght() const override { return 12; }
693 virtual uint8_t offset() const override { return 2; }
694
695 uint16_t minValue = 0;
696 } oTGVoltage;
697 // The OTGCurrent register is a limit after which the device will raise the
698 // OTG_OVP flag
700 {
701 uint16_t address() const override { return OTG_CURRENT_ADDR; }
702
703 virtual uint16_t minVal() const override { return 0; }
704 virtual uint16_t maxVal() const override { return 6400; }
705 virtual float resolution() const override { return 50; }
706
707 virtual uint8_t bitLenght() const override { return 7; }
708 virtual uint8_t offset() const override { return 8; }
709 } oTGCurrent;
710 // Allows to read the VBUS & PSYS voltage from the ADC
712 { // read only
713 uint16_t address() const override { return ADC_VBUS_PSYS_ADC_ADDR; };
714
715 uint16_t minVal0() const override { return 0; }; // TODO: check this
716 uint16_t maskVal0() const override { return 0b11111111; }
717 uint16_t resolutionVal0() const override { return 12; };
718
719 uint16_t minVal1() const override { return 3200; };
720 uint16_t maskVal1() const override { return 0b11111111; }
721 uint16_t resolutionVal1() const override { return 64; };
722
723 // input voltage on VBUS
724 uint16_t get_VBUS() { return getVal1(); }
725 // system power (exprimed as mV, which is strange...)
726 uint16_t get_PSYS() { return getVal0(); }
727 } aDCVBUSPSYS;
728 // Allows to read the battery charge and discharge current from DAC
729 struct ADCIBATt : public IDoubleRegister
730 { // read only
731 uint16_t address() const override { return ADC_IBAT_ADDR; };
732
733 uint16_t minVal0() const override { return 0; };
734 uint16_t maskVal0() const override { return 0b01111111; }
735 uint16_t resolutionVal0() const override { return 256; };
736
737 uint16_t minVal1() const override { return 0; };
738 uint16_t maskVal1() const override { return 0b01111111; }
739 uint16_t resolutionVal1() const override { return 64; };
740
741 // ICHG battery charging current value
742 uint16_t get_ICHG() { return getVal1(); }
743 // IDCHG battery discharging current value
744 uint16_t get_IDCHG() { return getVal0(); }
745 } aDCIBAT;
747 { // read only
748 uint16_t address() const override { return CMPIN_ADC_ADDR; };
749
750 uint16_t minVal0() const override { return 0; };
751 uint16_t maskVal0() const override { return 0b11111111; }
752 uint16_t resolutionVal0() const override { return 12; };
753
754 uint16_t minVal1() const override { return 0; };
755 uint16_t maskVal1() const override { return 0b11111111; }
756 uint16_t resolutionVal1() const override { return 50; };
757
758 uint16_t IIN, CMPIN;
759 byte val0, val1;
760 uint8_t addr = CMPIN_ADC_ADDR;
761 // IIN input current reading
762 uint16_t get_IIN() { return getVal1(); }
763 // CMPIN voltage on comparator pin
764 uint16_t get_CMPIN() { return getVal0(); }
765 } aDCIINCMPIN;
767 { // read only
768 uint16_t address() const override { return VBAT_ADC_ADDR; };
769
770 uint16_t minVal0() const override { return 2880; };
771 uint16_t maskVal0() const override { return 0b11111111; }
772 uint16_t resolutionVal0() const override { return 64; };
773
774 uint16_t minVal1() const override { return 2880; };
775 uint16_t maskVal1() const override { return 0b11111111; }
776 uint16_t resolutionVal1() const override { return 64; };
777
778 // VSYS system voltage
779 uint16_t get_VSYS() { return getVal1(); }
780 // VBAT voltage of battery
781 uint16_t get_VBAT() { return getVal0(); }
782 } aDCVSYSVBAT;
783
785 { // read only
786 // Manufacturer ID
787 uint8_t addr = MANUFACTURER_ID_ADDR;
788 byte get_manufacturerID()
789 {
790 byte valBytes[1];
791 // Function to handle the I2C comms.
792 readDataReg(addr, valBytes, 1);
793 return (byte)valBytes[0];
794 }
795 } manufacturerID;
796 struct DeviceID
797 { // read only
798 // Device ID
799 uint8_t addr = DEVICE_ID_ADDR;
800 byte get_deviceID()
801 {
802 byte valBytes[1];
803 // Function to handle the I2C comms.
804 readDataReg(addr, valBytes, 1);
805 return (byte)valBytes[0];
806 }
807 } deviceID;
808 };
809
810private:
811 static bool readDataReg(const byte regAddress, byte* dataVal, const uint8_t arrLen)
812 {
813 // IMPLEMENT THIS FUNCTION
814 return ::lampda::platform::i2c::i2c_readData(
815 i2cObjectIndex, BQ25713addr, regAddress, arrLen, dataVal, usesStopBit ? 1 : 0) == 0;
816 }
817
818 static bool writeData(const byte regAddress, uint8_t lenght, uint8_t* data)
819 {
820 // IMPLEMENT THIS FUNCTION
821 return ::lampda::platform::i2c::i2c_writeData(
822 i2cObjectIndex, BQ25713addr, regAddress, lenght, data, usesStopBit ? 1 : 0) == 0;
823 }
824
825 static bool writeData16(const byte regAddress, uint16_t val)
826 {
827 uint8_t data[2];
828 data[0] = val & 0xFF;
829 data[1] = (val >> 8) & 0xFF;
830
831 // IMPLEMENT THIS FUNCTION
832 return writeData(regAddress, 2, data);
833 }
834};
835
836} // namespace bq25713
837
838#endif // BQ25713_H
Definition: BQ25713.h:66
Definition: BQ25713.h:121
Definition: BQ25713.h:165
Definition: BQ25713.h:195
Definition: BQ25713.h:730
Definition: BQ25713.h:747
Definition: BQ25713.h:470
Definition: BQ25713.h:712
Definition: BQ25713.h:767
Definition: BQ25713.h:797
Definition: BQ25713.h:644
Definition: BQ25713.h:631
Definition: BQ25713.h:657
Definition: BQ25713.h:700
Definition: BQ25713.h:668
Definition: BQ25713.h:242