-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathltc2943.cpp
1420 lines (1209 loc) · 43.7 KB
/
ltc2943.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* mbed Microcontroller Library
* Copyright (c) 2017 u-blox
*
* 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.
*/
/**
* @file ltc2943.cpp
* This file defines the API to the Linear Technology LTC2943 battery gauge chip.
*/
/// Define this to print debug information.
//#define DEBUG_LTC2943
#include <mbed.h>
#include <battery_gauge_ltc2943.h>
#ifdef DEBUG_LTC2943
# include <stdio.h>
#endif
// ----------------------------------------------------------------
// COMPILE-TIME MACROS
// ----------------------------------------------------------------
/// How long to wait for one ADC read of temperature, voltage and current
// to be performed.
#define ADC_READ_WAIT_MS 100
/// The tolerance allowed for value conversions into the threshold registers.
#define LTC_2943_TOLERANCE 2
/// Check that a value is within tolerance.
#define TOLERANCE_CHECK(value, intendedValue, tolerance) ((value - intendedValue <= tolerance) && (value - intendedValue >= -tolerance))
// ----------------------------------------------------------------
// PRIVATE VARIABLES
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// GENERIC PRIVATE FUNCTIONS
// ----------------------------------------------------------------
/// Read two bytes from an address.
// Note: gpI2c should be locked before this is called.
bool BatteryGaugeLtc2943::getTwoBytes (uint8_t registerAddress, uint16_t *pBytes)
{
bool success = false;
char data[3];
if (gpI2c != NULL) {
// Send a command to read from registerAddress
data[0] = registerAddress;
data[1] = 0;
data[2] = 0;
if ((gpI2c->write(gAddress, &(data[0]), 1, true) == 0) &&
(gpI2c->read(gAddress, &(data[1]), 2) == 0)) {
success = true;
if (pBytes) {
*pBytes = (((uint16_t) data[1]) << 8) + data[2];
}
}
}
return success;
}
/// Set two bytes, starting from an address.
// Note: gpI2c should be locked before this is called.
bool BatteryGaugeLtc2943::setTwoBytes (uint8_t registerAddress, uint16_t bytes)
{
bool success = false;
char data[3];
if (gpI2c != NULL) {
// Send a command to write from registerAddress
data[0] = registerAddress;
data[1] = (char) (bytes >> 8);
data[2] = (char) bytes;
if (gpI2c->write(gAddress, &(data[0]), 3) == 0) {
success = true;
}
}
return success;
}
/// Ensure that the ADC of the chip has taken a reading recently.
// Note: gpI2c should be locked before this is called.
bool BatteryGaugeLtc2943::makeAdcReading (void)
{
bool success = false;
char storedControlRegister;
char data[2];
if (gReady && (gpI2c != NULL)){
// Read the control register
data[0] = 0x01; // Address of the control register
data[1] = 0;
if ((gpI2c->write(gAddress, &(data[0]), 1, true) == 0) &&
(gpI2c->read(gAddress, &(data[1]), 1) == 0)) {
storedControlRegister = data[1];
// If the ADC mode, in bits 6 and 7, is zero, set
// it to 01, which will take a manual reading of voltage,
// current and temperature and then return them to zero.
// Also make sure that the power-down bit is not set.
if ((data[1] & 0xC0) == 0) {
data[1] |= 0x40;
data[1] &= ~0x01;
if (gpI2c->write(gAddress, &(data[0]), 2) == 0) {
wait_ms(ADC_READ_WAIT_MS);
// Now set the shutdown bit to ensure lowest power state
data[1] = storedControlRegister | 0x01;
if (gpI2c->write(gAddress, &(data[0]), 2) == 0) {
success = true;
}
}
} else {
// Readings are already being made, nothing to do here
success = true;
}
}
}
return success;
}
/// Convert a 16 bit register reading into a temperature reading in C.
// Note: gpI2c should be locked before this is called.
int32_t BatteryGaugeLtc2943::registerToTemperatureC (uint16_t data)
{
// From the data sheet the temperature (in Kelvin) is
// T = 510 * data / 0xffff
return (((uint32_t) data) * 510 / 0xffff) - 273;
}
/// Convert a temperature in C to a register value.
// Note: no overflow checking is done here, such effects must be checked by the caller.
uint16_t BatteryGaugeLtc2943::temperatureCToRegister (int32_t temperatureC)
{
int32_t registerValue;
// Check against the laws of physics
MBED_ASSERT (temperatureC >= -273);
// Rearranging from the above
registerValue = (temperatureC + 273) * 0xffff / 510;
if (registerValue > 0xffff) {
registerValue = 0xffff;
}
if (registerValue < 0) {
registerValue = 0;
}
return (uint16_t) registerValue;
}
/// Convert a 16 bit register reading into a voltage reading in mV.
int32_t BatteryGaugeLtc2943::registerToVoltageMV (uint16_t data)
{
// From the data sheet the voltage (in mV) is
// V = 23600 * data / 0xffff
return (((int32_t) data) * 23600) / 0xffff;
}
/// Convert a voltage in mV to a register value.
// Note: no overflow checking is done here, such effects must be checked by the caller.
uint16_t BatteryGaugeLtc2943::voltageMVToRegister (int32_t voltageMV)
{
int32_t registerValue;
// Rearranging from the above
registerValue = (voltageMV * 0xffff) / 23600;
if (registerValue > 0xffff) {
registerValue = 0xffff;
}
if (registerValue < 0) {
registerValue = 0;
}
return (uint16_t) registerValue;
}
/// Convert a 16 bit register reading into a current reading in mA.
int32_t BatteryGaugeLtc2943::registerToCurrentMA (uint16_t data, int32_t rSenseMOhm)
{
// From the data sheet, max current (in Amps) corresponds to 0xffff
// (which is 60 mV across RSense) while min (most negative)
// current corresponds to 0 (which is -60 mV across RSense).
return ((int32_t) (data - 0x7fff)) * 60 * 1000 / 0x7fff / rSenseMOhm;
}
/// Convert a current in mA to a register value.
// Note: no overflow checking is done here, such effects must be checked by the caller.
uint16_t BatteryGaugeLtc2943::currentMAToRegister (int32_t currentMA, int32_t rSenseMOhm)
{
int32_t registerValue;
int32_t value = currentMA * rSenseMOhm;
// Rearranging from the above
registerValue = ((value) * 544 / 1000) + 0x7fff;
if (registerValue > 0xffff) {
registerValue = 0xffff;
}
if (registerValue < 0) {
registerValue = 0;
}
return (uint16_t) registerValue;
}
/// Convert a 16 bit register reading into a charge reading in mAh.
int32_t BatteryGaugeLtc2943::registerToChargeMAH (uint16_t data, int32_t rSenseMOhm, int32_t prescaler)
{
// From the data sheet, each bit of the charge registers has
// the following value in mAh:
// 0.34 * 50 * prescaler / RSense / 4096.
return ((((int32_t) data) * 17 * prescaler) / rSenseMOhm) >> 12;
}
/// Convert a charge in mAh to a register value.
// Note: no overflow checking is done here, such effects must be checked by the caller.
uint16_t BatteryGaugeLtc2943::chargeMAHToRegister (int32_t chargeMAH, int32_t rSenseMOhm, int32_t prescaler)
{
int32_t registerValue;
// Rearranging from the above
registerValue = ((chargeMAH << 12) * rSenseMOhm) / prescaler / 17;
if (registerValue > 0xffff) {
registerValue = 0xffff;
}
if (registerValue < 0) {
registerValue = 0;
}
return (uint16_t) registerValue;
}
//----------------------------------------------------------------
// PUBLIC FUNCTIONS: initialisation and monitoring
//----------------------------------------------------------------
/// Constructor.
BatteryGaugeLtc2943::BatteryGaugeLtc2943(void)
{
gpI2c = NULL;
gReady = false;
gBatteryCapacityMAH = 0;
}
/// Destructor.
BatteryGaugeLtc2943::~BatteryGaugeLtc2943(void)
{
}
/// Initialise ourselves.
bool BatteryGaugeLtc2943::init (I2C * pI2c, int32_t rSenseMOhm, uint8_t address, int32_t prescaler, Alcc alcc)
{
char data[2];
gpI2c = pI2c;
gAddress = address << 1;
gRSenseMOhm = rSenseMOhm;
gPrescaler = prescaler;
MBED_ASSERT(alcc < MAX_NUM_ALCCS);
if (gpI2c != NULL) {
gpI2c->lock();
// Set up the prescaler, ALCC and set everything to lowest power state for now
data[0] = 0x01; // Address of the control register
// Bits 7:6 top two bits zero == ADC asleep
// Bits 5:3 coded prescaler value
// Bits 2:1 ALCC mode (10 for Alert Output, 01 for Charge Complete Input, 00 for Off
// Bit 0 1 to power down analogue components
data[1] = 0x01;
data[1] |= alcc << 1;
switch (prescaler) {
case 1:
// Coded prescaler value is zero, nothing to do
break;
case 4:
data[1] |= 1 << 3;
break;
case 16:
data[1] |= 2 << 3;
break;
case 64:
data[1] |= 3 << 3;
break;
case 256:
data[1] |= 4 << 3;
break;
case 1024:
data[1] |= 5 << 3;
break;
case 4096:
data[1] |= 6 << 3;
break;
default:
gPrescaler = 0;
MBED_ASSERT(false);
break;
}
// Now write to the control register
if (gpI2c->write(gAddress, &(data[0]), 2) == 0) {
gReady = true;
}
gpI2c->unlock();
}
#ifdef DEBUG_LTC2943
if (gReady) {
printf("BatteryGaugeLtc2943 (I2C 0x%02x): handler initialised (control register set to 0x%02x).\n", gAddress >> 1, data[1]);
} else {
printf("BatteryGaugeLtc2943 (I2C 0x%02x): device NOT found.\r\n", gAddress >> 1);
}
#endif
return gReady;
}
/// Switch battery charge monitoring on.
bool BatteryGaugeLtc2943::enableGauge (bool isSlow)
{
bool success = false;
char data[2];
if (gReady && (gpI2c != NULL)){
gpI2c->lock();
// Read the control register
data[0] = 0x01; // Address of the control register
data[1] = 0;
if ((gpI2c->write(gAddress, &(data[0]), 1, true) == 0) &&
(gpI2c->read(gAddress, &(data[1]), 1) == 0)) {
// Set the ADC mode in bits 6 and 7 to 11 or, if
// isSlow is true, to 10 (in which case a measurement
// is only performed every ten seconds).
data[1] |= 0xc0;
if (isSlow) {
data[1] &= ~0x40;
}
// Also make sure that the power-down bit is not set.
data[1] &= ~0x01;
// Write the new value to the control register
if (gpI2c->write(gAddress, &(data[0]), 2) == 0) {
success = true;
}
}
gpI2c->unlock();
}
return success;
}
/// Switch battery charge monitoring off.
bool BatteryGaugeLtc2943::disableGauge (void)
{
bool success = false;
char data[2];
if (gReady && (gpI2c != NULL)){
gpI2c->lock();
// Read the control register
data[0] = 0x01; // Address of the control register
data[1] = 0;
if ((gpI2c->write(gAddress, &(data[0]), 1, true) == 0) &&
(gpI2c->read(gAddress, &(data[1]), 1) == 0)) {
// Set the ADC mode to 00 and the power down bit to 1
data[1] &= ~0xc0;
data[1] |= 0x01;
// Write the new value to the control register
if (gpI2c->write(gAddress, &(data[0]), 2) == 0) {
success = true;
}
}
gpI2c->unlock();
}
return success;
}
//----------------------------------------------------------------
// PUBLIC FUNCTIONS: getting readings
//----------------------------------------------------------------
/// Get the temperature of the chip.
bool BatteryGaugeLtc2943::getTemperature (int32_t *pTemperatureC)
{
bool success = false;
uint16_t data;
if (gReady && (gpI2c != NULL)){
gpI2c->lock();
if (makeAdcReading ()) {
// Read from the temperature register address
if (getTwoBytes (0x14, &data)) {
success = true;
if (pTemperatureC) {
*pTemperatureC = registerToTemperatureC (data);
}
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): temperature registers report 0x%04x, so %d C.\n",
gAddress >> 1, data, registerToTemperatureC (data));
#endif
}
}
gpI2c->unlock();
}
return success;
}
/// Get the voltage of the battery.
bool BatteryGaugeLtc2943::getVoltage (int32_t *pVoltageMV)
{
bool success = false;
uint16_t data;
if (gReady && (gpI2c != NULL)){
gpI2c->lock();
if (makeAdcReading()) {
// Read from the voltage register address
if (getTwoBytes (0x08, &data)) {
success = true;
if (pVoltageMV) {
*pVoltageMV = registerToVoltageMV (data);
}
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): voltage registers report 0x%04x, giving a voltage of %.3f V.\n",
gAddress >> 1, data, (float) registerToVoltageMV (data) / 1000);
#endif
}
}
gpI2c->unlock();
}
return success;
}
/// Get the current flowing through RSense.
bool BatteryGaugeLtc2943::getCurrent (int32_t *pCurrentMA)
{
bool success = false;
uint16_t data;
if (gReady && (gpI2c != NULL)){
gpI2c->lock();
if (makeAdcReading()) {
// Read from the current register address
if (getTwoBytes (0x0e, &data)) {
success = true;
if (pCurrentMA) {
*pCurrentMA = registerToCurrentMA (data, gRSenseMOhm);
}
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): current registers report 0x%04x, giving a current of %.3f A.\n",
gAddress >> 1, data, (float) registerToCurrentMA (data, gRSenseMOhm) / 1000);
#endif
}
}
gpI2c->unlock();
}
return success;
}
/// Tell the LTC2943 chip that charging is complete.
bool BatteryGaugeLtc2943::setChargingComplete (int32_t capacityMAH)
{
bool success = false;
bool analoguePowerReady = true;
char data[2];
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
// First read the control register as we have to power
// down the analogue parts when setting the charge registers
data[0] = 0x01; // Address of the control register
data[1] = 0;
if ((gpI2c->write(gAddress, &(data[0]), 1, true) == 0) &&
(gpI2c->read(gAddress, &(data[1]), 1) == 0)) {
// If the power down bit is not set, set it
if ((data[1] & 0x01) != 0x01) {
data[1] |= 0x01;
if (gpI2c->write(gAddress, &(data[0]), 2) != 0) {
analoguePowerReady = false;
}
}
if (analoguePowerReady) {
// Remember the battery charge
gBatteryCapacityMAH = capacityMAH;
// Set the charge counter to max
success = setTwoBytes(0x02, 0xffff);
// If the ADC mode, in bits 6 and 7, is not zero then
// we must switch on the analogue power again.
if ((data[1] & 0xc0) != 0) {
data[1] &= ~0x01;
if (gpI2c->write(gAddress, &(data[0]), 2) != 0) {
success = false;
}
}
}
}
gpI2c->unlock();
}
return success;
}
/// Get the remaining battery charge.
bool BatteryGaugeLtc2943::getRemainingCharge (int32_t *pChargeMAH)
{
bool success = false;
int32_t chargeMAH;
uint16_t data;
if (gReady && (gpI2c != NULL)){
gpI2c->lock();
// Read from the charge accumulator register address
if (getTwoBytes (0x02, &data)) {
success = true;
// Full scale corresponds to the capacity of the battery when
// fully charged, so the capacity used is 0xffff - data
chargeMAH = registerToChargeMAH (0xffff - data, gRSenseMOhm, gPrescaler);
if (pChargeMAH) {
*pChargeMAH = gBatteryCapacityMAH - chargeMAH;
if (gBatteryCapacityMAH == 0) {
// If gBatteryCapacityMAH is 0, we can't actually calculate the remaining
// capacity but we can calculate the charge used using the fact that the default
// starting value is 0x7FFF. So set success to false so that the user knows
// we can't supply what was requested but report the charge used number anyway
// in case it is useful
success = false;
*pChargeMAH = registerToChargeMAH (0x7FFF - data, gRSenseMOhm, gPrescaler);
}
}
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): charge accumulator registers report 0x%04x, battery capacity is %d giving a charge remaining of %.3f AH.\n",
gAddress >> 1, data, gBatteryCapacityMAH, (float) (gBatteryCapacityMAH - chargeMAH) / 1000);
#endif
}
gpI2c->unlock();
}
return success;
}
/// Get the battery percentage remaining.
bool BatteryGaugeLtc2943::getRemainingPercentage (int32_t *pBatteryPercent)
{
bool success;
int32_t chargeMAH;
success = getRemainingCharge (&chargeMAH);
if (success) {
if (pBatteryPercent) {
*pBatteryPercent = 100 * gBatteryCapacityMAH / chargeMAH;
}
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): remaining charge is %d mAh, battery capacity is %d mAh, so percentage remaining is %d%%.\n",
gAddress >> 1, chargeMAH, gBatteryCapacityMAH, 100 * gBatteryCapacityMAH / chargeMAH);
#endif
}
return success;
}
/// Get the reason(s) for an alert.
char BatteryGaugeLtc2943::getAlertReason (void)
{
char result = 0;
char data[2];
if (gReady && (gpI2c != NULL)){
gpI2c->lock();
data[0] = 0x00; // Address of the control register
data[1] = 0;
// Read the status register
if ((gpI2c->write(gAddress, &(data[0]), 1, true) == 0) &&
(gpI2c->read(gAddress, &(data[1]), 1))) {
result = data[1];
}
gpI2c->unlock();
}
return result;
}
//----------------------------------------------------------------
// PUBLIC FUNCTIONS: setting/getting/checking/clearing thresholds
//----------------------------------------------------------------
/// Set temperature alert upper threshold.
bool BatteryGaugeLtc2943::setTemperatureHigh (int32_t temperatureC)
{
bool success = false;
uint16_t registerValue = temperatureCToRegister(temperatureC);
char data[2];
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
// Note that the temperature threshold is 8 bit, not 16 bits
// like all the other thresholds
registerValue >>= 8;
// Check for overflow in conversion to the register value
printf ("registerValue %d, temperatureC %d, registerToTemperatureC(registerValue << 8) %d\n",
registerValue, (int) temperatureC, (int) registerToTemperatureC(registerValue << 8));
if (TOLERANCE_CHECK (registerToTemperatureC(registerValue << 8), temperatureC, LTC_2943_TOLERANCE)) {
data[0] = 0x16; // Temperature threshold high address
// Only write the value if it fits (and taking into
// account the fact that 0xFF means "no upper threshold")
if ((registerValue < 0xff)) {
data[1] = (char) registerValue;
if (gpI2c->write(gAddress, &(data[0]), 2) == 0) {
success = true;
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): temperature high threshold register set to 0x%02x, (%d C).\n",
gAddress >> 1, (char) registerValue, registerToTemperatureC(registerValue << 8));
#endif
}
} else {
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): trying to set temperature high threshold to %d C, value is too large (0x%04x < 0xff).\n",
gAddress >> 1, temperatureC, registerValue);
#endif
}
}
gpI2c->unlock();
}
return success;
}
/// Get temperature alert upper threshold.
bool BatteryGaugeLtc2943::getTemperatureHigh (int32_t *pTemperatureC)
{
bool success = false;
char data[2];
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
data[0] = 0x16; // Temperature threshold high address
data[1] = 0;
if ((gpI2c->write(gAddress, &(data[0]), 1, true) == 0) &&
(gpI2c->read(gAddress, &(data[1]), 1) == 0)) {
success = true;
if (pTemperatureC) {
// Note that the temperature threshold is 8 bit, not 16 bits
// like all the other thresholds
*pTemperatureC = registerToTemperatureC(((uint16_t) data[1]) << 8);
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): temperature high threshold register reports 0x%02x (a temperature of %d C).\n",
gAddress >> 1, ((uint16_t) data[1]) << 8, registerToTemperatureC(((uint16_t) data[1]) << 8));
#endif
}
}
gpI2c->unlock();
}
return success;
}
/// Is the temperature alert upper threshold set.
bool BatteryGaugeLtc2943::isTemperatureHighSet (void)
{
bool isSet = false;
char data[2];
// Note that the temperature threshold is 8 bit, not 16 bits
// like all the other thresholds
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
data[0] = 0x16; // Temperature threshold high address
data[1] = 0;
if ((gpI2c->write(gAddress, &(data[0]), 1, true) == 0) &&
(gpI2c->read(gAddress, &(data[1]), 1) == 0)) {
if (data[1] < 0xff) {
isSet = true;
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): temperature high threshold register is set.\n", gAddress >> 1);
#endif
}
}
gpI2c->unlock();
}
return isSet;
}
/// Clear temperature alert upper threshold.
bool BatteryGaugeLtc2943::clearTemperatureHigh (void)
{
bool success = false;
char data[2];
// Note that the temperature threshold is 8 bit, not 16 bits
// like all the other thresholds
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
data[0] = 0x16; // Temperature threshold high address
data[1] = 0xff; // 0xFF means no upper threshold set
if (gpI2c->write(gAddress, &(data[0]), 2) == 0) {
success = true;
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): temperature high threshold register cleared.\n", gAddress >> 1);
#endif
}
gpI2c->unlock();
}
return success;
}
/// Set temperature alert lower threshold.
bool BatteryGaugeLtc2943::setTemperatureLow (int32_t temperatureC)
{
bool success = false;
uint16_t registerValue = temperatureCToRegister (temperatureC);
char data[2];
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
// Note that the temperature threshold is 8 bit, not 16 bits
// like all the other thresholds
registerValue >>= 8;
// Check for overflow in conversion to the register value
if (TOLERANCE_CHECK (registerToTemperatureC(registerValue << 8), temperatureC, LTC_2943_TOLERANCE)) {
data[0] = 0x17; // Temperature threshold low address
// Only write the value if it fits (and taking into
// account the fact that 0 means "no lower threshold")
if (registerValue > 0) {
data[1] = (char) registerValue;
if (gpI2c->write(gAddress, &(data[0]), 2) == 0) {
success = true;
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): temperature low threshold register set to 0x%02x, (%d C).\n",
gAddress >> 1, (char) registerValue, registerToTemperatureC(registerValue << 8));
#endif
}
} else {
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): trying to set temperature low threshold to %d C, value is too small (0x%04x > 0).\n",
gAddress >> 1, temperatureC, registerValue);
#endif
}
}
gpI2c->unlock();
}
return success;
}
/// get temperature alert lower threshold.
bool BatteryGaugeLtc2943::getTemperatureLow (int32_t *pTemperatureC)
{
bool success = false;
char data[2];
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
data[0] = 0x17; // Temperature threshold low address
data[1] = 0;
if ((gpI2c->write(gAddress, &(data[0]), 1, true) == 0) &&
(gpI2c->read(gAddress, &(data[1]), 1) == 0)) {
success = true;
if (pTemperatureC) {
// Note that the temperature threshold is 8 bit, not 16 bits
// like all the other thresholds
*pTemperatureC = registerToTemperatureC(((uint16_t) data[1]) << 8);
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): temperature low threshold register reports 0x%02x (a temperature of %d C).\n",
gAddress >> 1, ((uint16_t) data[1]) << 8, registerToTemperatureC(((uint16_t) data[1]) << 8));
#endif
}
}
gpI2c->unlock();
}
return success;
}
/// Is the temperature alert lower threshold set.
bool BatteryGaugeLtc2943::isTemperatureLowSet (void)
{
bool isSet = false;
char data[2];
// Note that the temperature threshold is 8 bit, not 16 bits
// like all the other thresholds
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
data[0] = 0x17; // Temperature threshold low address
data[1] = 0;
if ((gpI2c->write(gAddress, &(data[0]), 1, true) == 0) &&
(gpI2c->read(gAddress, &(data[1]), 1) == 0)) {
if (data[1] > 0) {
isSet = true;
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): temperature low threshold register is set.\n", gAddress >> 1);
#endif
}
}
gpI2c->unlock();
}
return isSet;
}
/// Clear temperature alert lower threshold.
bool BatteryGaugeLtc2943::clearTemperatureLow (void)
{
bool success = false;
char data[2];
// Note that the temperature threshold is 8 bit, not 16 bits
// like all the other thresholds
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
data[0] = 0x17; // Temperature threshold low address
data[1] = 0; // 0 means no lower threshold set
if (gpI2c->write(gAddress, &(data[0]), 2) == 0) {
success = true;
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): temperature low threshold register is cleared.\n", gAddress >> 1);
#endif
}
gpI2c->unlock();
}
return success;
}
/// Set voltage alert upper threshold.
bool BatteryGaugeLtc2943::setVoltageHigh (int32_t voltageMV)
{
bool success = false;
uint16_t registerValue = voltageMVToRegister(voltageMV);
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
// Check for overflow in conversion to the register value
if (TOLERANCE_CHECK (registerToVoltageMV(registerValue), voltageMV, LTC_2943_TOLERANCE)) {
// Only write the value if it is less than 0xffff (as
// 0xffff means no threshold set)
if (registerValue < 0xfff) {
success = setTwoBytes (0x0a, registerValue);
#ifdef DEBUG_LTC2943
if (success) {
printf("BatteryGaugeLtc2943 (I2C 0x%02x): voltage high threshold register set to 0x%04x, (%d mV).\n",
gAddress >> 1, registerValue, registerToVoltageMV(registerValue));
}
#endif
}
}
gpI2c->unlock();
}
return success;
}
/// Get voltage alert upper threshold.
bool BatteryGaugeLtc2943::getVoltageHigh (int32_t *pVoltageMV)
{
bool success = false;
uint16_t data;
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
success = getTwoBytes (0x0a, &data);
if (success && pVoltageMV) {
*pVoltageMV = registerToVoltageMV (data);
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): voltage high threshold register is 0x%04x, (%d mV).\n",
gAddress >> 1, data, registerToVoltageMV(data));
#endif
}
gpI2c->unlock();
}
return success;
}
/// Is the voltage alert upper threshold set.
bool BatteryGaugeLtc2943::isVoltageHighSet (void)
{
bool isSet = false;
uint16_t data;
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
// Default value is 0xffff, so if it is not that it is set
if (getTwoBytes (0x0a, &data) && (data != 0xffff)) {
isSet = true;
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): voltage high threshold register is set.\n", gAddress >> 1);
#endif
}
gpI2c->unlock();
}
return isSet;
}
/// Clear voltage alert upper threshold.
bool BatteryGaugeLtc2943::clearVoltageHigh (void)
{
bool success = false;
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
success = setTwoBytes (0x0a, 0xffff);
#ifdef DEBUG_LTC2943
printf("BatteryGaugeLtc2943 (I2C 0x%02x): voltage high threshold register is cleared.\n", gAddress >> 1);
#endif
gpI2c->unlock();
}
return success;
}
/// Set voltage alert lower threshold.
bool BatteryGaugeLtc2943::setVoltageLow (int32_t voltageMV)
{
bool success = false;
uint16_t registerValue = voltageMVToRegister (voltageMV);
if (gReady && (gpI2c != NULL)) {
gpI2c->lock();
// Check for overflow in conversion to the register value
if (TOLERANCE_CHECK (registerToVoltageMV(registerValue), voltageMV, LTC_2943_TOLERANCE)) {
// Only write the value if it is greater than 0 (as
// 0 means no threshold set)
if (registerValue > 0) {
success = setTwoBytes (0x0c, registerValue);
#ifdef DEBUG_LTC2943
if (success) {
printf("BatteryGaugeLtc2943 (I2C 0x%02x): voltage low threshold register set to 0x%04x, (%d mV).\n",
gAddress >> 1, registerValue, registerToVoltageMV(registerValue));
}
#endif
}
}
gpI2c->unlock();
}
return success;
}
/// Get voltage alert lower threshold.