-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLTC68041ESP.cpp
1817 lines (1515 loc) · 58.1 KB
/
LTC68041ESP.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
/************************************************************
This library is based on the LTC68041.cpp by linear technology.
http://www.linear.com/product/LTC6804-1
I modified it make it compatible with the ESP8622
https://github.com/jontubs/EasyBMS
***********************************************************/
#include <stdint.h>
#include <Arduino.h>
#include "LTC68041ESP.h"
#include <SPI.h>
/*!*******************************************************************************************************
Creating of the object LTC68041
*********************************************************************************************************/
LTC68041::LTC68041(byte pinMOSI, byte pinMISO, byte pinCLK, byte csPin)
{
Serial.print("Objekt angelegt");
pinMode(pinMOSI, OUTPUT);
pinMode(pinMISO, INPUT);
pinMode(pinCLK, OUTPUT);
pinMode(csPin, OUTPUT);
}
/*!******************************************************************************************************
Reads and parses the LTC6804 cell voltage registers.
This function will initialize all 6804 variables and the SPI port.
*********************************************************************************************************/
void LTC68041::initialize()
{
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE3);
SPI.setClockDivider(SPI_CLOCK_DIV128);
SPI.begin();
initCFGR();
}
/*!******************************************************************************************************
Simple Serial print to check in object exists. Just for Debug
*********************************************************************************************************/
void LTC68041::helloworld()
{
Serial.print("Hello World, used Pins are MOSI:");
Serial.print(pinMOSI);
Serial.print("\t MISO:");
Serial.print(pinMISO);
Serial.print("\t Clock:");
Serial.print(pinCLK);
Serial.print("\t Chipselect:");
Serial.println(csPin);
}
/*!******************************************************************************************************
Wake isoSPI up from idle state
Generic wakeup commannd to wake isoSPI up out of idle
*********************************************************************************************************/
void LTC68041::wakeup_idle()
{
digitalWrite(csPin, LOW);
delay(2); //Guarantees the isoSPI will be in ready mode
//SPI.transfer(dummy); //Test
digitalWrite(csPin, HIGH);
delay(2); //Guarantees the isoSPI will be in ready mode
}
/*!******************************************************************************************************
Calculates the CRC sum of some data bytes given by the pointer "data" with the length "len"
*********************************************************************************************************/
uint16_t LTC68041::pec15_calc(uint8_t len, uint8_t *data)
{
uint16_t remainder,addr;
remainder = 16;//initialize the PEC
for (uint8_t i = 0; i<len; i++) // loops for each byte in data array
{
addr = ((remainder>>7)^data[i])&0xff;//calculate PEC table address
remainder = (remainder<<8)^crc15Table[addr];
}
return(remainder*2);//The CRC15 has a 0 in the LSB so the remainder must be multiplied by 2
}
/*!******************************************************************************************************
Writes and read a set number of bytes using the SPI port.
Tested and runs fine
[in] uint8_t tx_data[] array of data to be written on the SPI port
[in] uint8_t tx_len length of the tx_data array
[out] uint8_t rx_data array that read data will be written too.
[in] uint8_t rx_len number of bytes to be read from the SPI port.
*********************************************************************************************************/
void LTC68041::spi_write_read(uint8_t tx_Data[], uint8_t tx_len, uint8_t *rx_data, uint8_t rx_len)
{
digitalWrite(csPin, LOW);
for (uint8_t i = 0; i < tx_len; i++)
{
SPI.transfer(tx_Data[i]);
}
for (uint8_t i = 0; i < rx_len; i++)
{
rx_data[i]=SPI.transfer(1);
}
digitalWrite(csPin, HIGH);
}
/*!******************************************************************************************************
Writes and read a set number of bytes using the SPI port without expecting an answer
uint8_t len, // Option: Number of bytes to be written on the SPI port
uint8_t data[] //Array of bytes to be written on the SPI port
*********************************************************************************************************/
void LTC68041::spi_write_array(uint8_t len, uint8_t data[])
{
digitalWrite(csPin, LOW);
for (uint8_t i = 0; i < len; i++)
{
SPI.transfer((int8_t)data[i]);
}
digitalWrite(csPin, HIGH);
}
/*!******************************************************************************************************
calculates the bitpattern in the config for Undervoltage detection
the config has to be written to the chip after this!
*********************************************************************************************************/
void LTC68041::initCFGR()
{
CFGRw[0] = 0xFE;
CFGRw[1] = 0 ;
CFGRw[2] = 0 ;
CFGRw[3] = 0 ;
CFGRw[4] = 0 ;
CFGRw[5] = 0 ;
}
/*!******************************************************************************************************
calculates the bitpattern in the config for Undervoltage detection
the config has to be written to the chip after this!
*********************************************************************************************************/
void LTC68041::setVUV(float Undervoltage)
{
uint16_t VUV=0;
VUV=(Undervoltage/(0.0001*16))-1; //calc bitpattern for UV
//CFGRw[0] = 0xFE;
CFGRw[1] = (uint8_t) VUV; //0x4E1 ; // 2.0V
CFGRw[2] = (VUV >> 8) & 0x0F ;
}
/*!******************************************************************************************************
calculates the bitpattern in the config for Overvoltage detection
the config has to be written to the chip after this!
*********************************************************************************************************/
void LTC68041::setVOV(float Overvoltage)
{
//float Undervoltage=3.123;
//float Overvoltage=3.923;
uint16_t VOV=0;
VOV=(Overvoltage/(0.0001*16)); //Calc bitpattern for OV
//CFGRw[0] = 0xFE;
CFGRw[2] = CFGRw[2] | ((VOV&0x0F) << 4) ;
CFGRw[3] = (uint8_t)(VOV>>4) ;
}
/*!*******************************************************************************************************
Reads configuration registers of a LTC6804
[out] uint8_t r_config[8] is an array that the function stores the read configuration data.
return int8_t, PEC Status.
0: Data read back has matching PEC
-1: Data read back has incorrect PEC
Giving additional Debug infos via Serial
*********************************************************************************************************/
int8_t LTC68041::rdcfg_debug(uint8_t r_config[8]) //An array that the function stores the read configuration data.
{
const uint8_t BYTES_IN_REG = 8;
uint8_t cmd[4];
uint8_t *rx_data_pointer;
int8_t pec_error = 0;
uint16_t data_pec;
uint16_t received_pec;
uint8_t rx_data[8];
//1
cmd[0] = 0x00;
cmd[1] = 0x02;
cmd[2] = 0x2b;
cmd[3] = 0x0A;
//2
//wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake. This command can be removed.
//3
digitalWrite(csPin, LOW);
spi_write_read(cmd, 4, rx_data, (BYTES_IN_REG)); //Read the configuration data of all ICs on the daisy chain into
digitalWrite(csPin, HIGH);; //rx_data[] array
Serial.print("gelesene Config: ");
for (int i = 0 ; i < 8; i++)
{
Serial.print(rx_data[i], HEX);
Serial.print("\t");
}
Serial.print("\n");
//into the r_config array as well as check the received Config data
//for any bit errors
//4.a
for (uint8_t current_byte = 0; current_byte < BYTES_IN_REG; current_byte++)
{
r_config[current_byte] = rx_data[current_byte + (BYTES_IN_REG)];
}
//4.b
received_pec = (r_config[6]<<8) + r_config[7];
data_pec = pec15_calc(6, &r_config[0]);
if (received_pec != data_pec)
{
pec_error = -1;
}
//5
return(pec_error);
}
/*!******************************************************************************************************
Sets the configuration array for cell balancing
1. Reset all Discharge Pins
2. Calculate adcv cmd PEC and load pec into cmd array
Discharge this cell (1-12), disable all other, IF -1 then all off
*********************************************************************************************************/
void LTC68041::balance_cfg(int cell, uint8_t cfg[6])
{
cfg[4] = 0x00; // clears S1-8
cfg[5] = 0x00; // clears S9-12 and keep value of software timer cfg[5] & 0xF0
//Serial.println(tx_cfg[ic][5] & 0xF0,BIN);
if (cell >= 0 and cell <= 7) {
cfg[4] = 0x1;//cfg[4] | 1 << cell;
}
if ( cell > 7) {
cfg[5] = 0x2;//cfg[5] | ( 1 << (cell - 8));
}
Serial.print("\n erzeugte Config");
for (int i = 0 ; i < 8; i++)
{
Serial.print(cfg[i], HEX);
Serial.print("\t");
}
Serial.print("\n");
}
/*!******************************************************************************************************
This command will write the configuration registers of the LTC6804-1.
Write the LTC6804 configuration register
1. Load cmd array with the write configuration command and PEC
2. Load the cmd with LTC6804 configuration data
3. Calculate the pec for the LTC6804 configuration data being transmitted
4. Write configuration data to the LTC6804
uint8_t config[6] is an array of the configuration data that will be written.
*********************************************************************************************************/
void LTC68041::wrcfg(uint8_t config[6])//A two dimensional array of the configuration data that will be written
{
const uint8_t BYTES_IN_REG = 6;
const uint8_t CMD_LEN = 4+(8);
uint8_t *cmd;
uint16_t cfg_pec;
uint8_t cmd_index; //command counter
cmd = (uint8_t *)malloc(CMD_LEN*sizeof(uint8_t));
//1
cmd[0] = 0x00;
cmd[1] = 0x01;
cmd[2] = 0x3d;
cmd[3] = 0x6e;
//2
cmd_index = 4;
// the last IC on the stack. The first configuration written is
// received by the last IC in the daisy chain
for (uint8_t current_byte = 0; current_byte < BYTES_IN_REG; current_byte++) // executes for each of the 6 bytes in the CFGR register
{
// current_byte is the byte counter
cmd[cmd_index] = config[current_byte]; //adding the config data to the array to be sent
cmd_index = cmd_index + 1;
}
//3
cfg_pec = (uint16_t)pec15_calc(BYTES_IN_REG, &config[0]); // calculating the PEC for each ICs configuration register data
cmd[cmd_index] = (uint8_t)(cfg_pec >> 8);
cmd[cmd_index + 1] = (uint8_t)cfg_pec;
cmd_index = cmd_index + 2;
//4
//wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake.This command can be removed.
//5
digitalWrite(csPin, LOW);
spi_write_array(CMD_LEN, cmd);
digitalWrite(csPin, HIGH);;
free(cmd);
}
/*!*******************************************************************************************************
Perform LUT lookup of cell SOC based on cell open circuit voltage.
SOC based on OCV lookup table.
SOC as a function of open cell voltage is non=linear, a lookup table seems to
be the best way to map between these two values.
voc: Cell open circuit voltage, Volts.
return Function returns SOC from 0-1.
Funktion ist kaputt , muss ich mal fixen
*********************************************************************************************************/
float LTC68041::cell_compute_soc(float voc) {
/*const double t_offset = 3.00205;
const double t_step = 0.01;
const double t_gain = 1000;
u_int16_t tbl[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,17,18,19,21,22,23,25,26,28,30,31,33,35,37,39,41,44,46,49,52,55,58,61,65,69,73,78,84,90,97,105,115,126,141,159,182,210,242,277,314,352,389,427,465,503,541,579,616,654,692,730,768,806,843,878,905,923,935,943,949,954,958,961,964,967,969,971,973,975,977,978,979,981,982,983,984,985,986,987,988,989,990,991,991,992,993,994,994,995,996,996,997,997,998,};*/
// offset: 2.41498V, step size:0.01V
const float t_offset = 2.415;
const float t_step = 0.01;
const float t_gain = 1000;
float result;
uint16_t tbl[] = {0,1,2,3,5,6,7,8,10,11,12,14,15,17,18,20,22,24,25,27,29,32,34,36,39,42,45,48,51,55,59,63,68,73,79,87,95,105,117,133,153,181,216,258,303,349,396,443,490,537,584,632,679,726,773,820,865,902,925,938,947,953,958,962,966,969,971,974,976,978,979,981,983,984,985,986,988,989,990,991,992,993,994,994,995,996,997,997,998,999,999,1000,};
// number of elements in lut
if (voc <= t_offset) {
return 0.0;
}
// table size
unsigned int n = sizeof (tbl) / sizeof (tbl[0]);
// compute index
unsigned int index = (unsigned int) ((voc - t_offset) / t_step);
// compute fractional index
float f_index = ((voc - t_offset) / t_step) - index;
// limit to valid table index, -1
index = (index < n-2) ? index : n-2;
f_index = (f_index < 1.0) ? f_index : 1.0; // don't extrapolate beyond LUT limits
// compute local slope
float delta = (tbl[index+1] - tbl[index]) / t_gain;
result=tbl[index] / t_gain + f_index * delta;
Serial.print("\nresult:");
Serial.print(result);
return result;
}
/*!******************************************************************************************************
Clears the LTC6804 Auxiliary registers
The command clears the Auxiliary registers and intiallizes
all values to 1. The register will read back hexadecimal 0xFF
after the command is sent.
LTC68041::clraux Function sequence:
1. Load clraux command into cmd array
2. Calculate clraux cmd PEC and load pec into cmd array
3. send broadcast clraux command
*********************************************************************************************************/
void LTC68041::clraux()
{
uint8_t cmd[4];
uint16_t cmd_pec;
//1
cmd[0] = 0x07;
cmd[1] = 0x12;
//2
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
//3
wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake.This command can be removed.
//4
digitalWrite(csPin, LOW);
spi_write_read(cmd,4,0,0);
digitalWrite(csPin, HIGH);
}
/*!*******************************************************************************************************
Reads and parses the LTC6804 cell voltage registers.
The function is used to read the cell codes of the LTC6804.
This function will send the requested read commands parse the data
and store the cell voltages in cell_codes variable.
1. Read every single cell voltage register
2. Parse raw cell voltage data in cell_codes array
3. Check the PEC of the data read back vs the calculated PEC for each read register command
4. Return pec_error flag
*********************************************************************************************************/
uint8_t LTC68041::rdcv() // Array of the parsed cell codes
{
const uint8_t NUM_RX_BYT = 8;
const uint8_t BYT_IN_REG = 6;
const uint8_t CELL_IN_REG = 3;
uint8_t *cell_pointer;
uint8_t pec_error = 0;
uint16_t parsed_cell;
uint16_t received_pec;
uint16_t data_pec;
uint8_t data_counter=0; //data counter
uint8_t cell_data[100];
//1.
//Lies alle
//läuft von 1-4
for (uint8_t cell_reg = 1; cell_reg<5; cell_reg++) //executes once for each of the LTC6804 cell voltage registers
{
data_counter = 0;
rdcv_reg(cell_reg, cell_data ); //Reads a single Cell voltage register
//2.
for (uint8_t current_cell = 0; current_cell<CELL_IN_REG; current_cell++) // This loop parses the read back data into cell voltages, it
{
// loops once for each of the 3 cell voltage codes in the register
parsed_cell = cell_data[data_counter] + (cell_data[data_counter + 1] << 8);//Each cell code is received as two bytes and is combined to
// create the parsed cell voltage code
cellCodes[current_cell + ((cell_reg - 1) * CELL_IN_REG)] = parsed_cell;
data_counter = data_counter + 2; //Because cell voltage codes are two bytes the data counter
//must increment by two for each parsed cell code
}
//3.
received_pec = (cell_data[data_counter] << 8) + cell_data[data_counter+1]; //The received PEC for the current_ic is transmitted as the 7th and 8th
//after the 6 cell voltage data bytes
data_pec = pec15_calc(BYT_IN_REG, &cell_data[NUM_RX_BYT]);
if (received_pec != data_pec)
{
pec_error = -1; //The pec_error variable is simply set negative if any PEC errors
//are detected in the serial data
}
data_counter=data_counter+2; //Because the transmitted PEC code is 2 bytes long the data_counter
//must be incremented by 2 bytes to point to the next ICs cell voltage data
}
//4
return(pec_error);
}
/*!*******************************************************************************************************
The function is used to read the of the Config Register Group A of one LTC6804.
This function sends the read commands, parses the data and stores the response in CFGR.
1. Set command to RDCFG
2. Calc the PEC
3. Wakeup Chip
4. Send command and read response
5. Check PEC
6. Copy data to object
7. Return Result -1= Error , 0= DataOkay
*********************************************************************************************************/
uint8_t LTC68041::rdcfg()
{
const uint8_t NUM_RX_BYT = 8; // number of bytes in the register + 2 bytes for the PEC
const uint8_t BYT_IN_REG = 6;
uint8_t pec_error = 0;
uint16_t received_pec;
uint8_t received_data[NUM_RX_BYT]; //data counter
uint16_t data_pec;
uint8_t data_counter=0; //data counter
uint8_t cmd[4];
uint16_t cmd_pec;
//1
cmd[0] = 0x00;
cmd[1] = 0x02;
//2
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
//3
wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake, this command can be removed.
//4
spi_write_read(cmd,4,received_data,NUM_RX_BYT);
//5
//Set data pointer to the first PEC byte
data_counter= BYT_IN_REG;
received_pec = (received_data[data_counter] << 8) + received_data[data_counter+1]; //The received PEC for the current_ic is transmitted as the 7th and 8th
//(uint8_t len, uint8_t *data)
data_pec = pec15_calc(BYT_IN_REG, received_data);
if (received_pec != data_pec)
{
pec_error = -1; //The pec_error variable is simply set negative if any PEC errors
//are detected in the serial data
}
else
{
//6
//Copy Target Source Size
memcpy( CFGRr, received_data, BYT_IN_REG );
}
//printArray(NUM_RX_BYT, received_data);
//7
return(pec_error);
}
/*!*******************************************************************************************************
The function is used to read the of the Auxiliary Register Group A of one LTC6804.
This function sends the read commands, parses the data and stores the response in AVAR.
1. Set command to AVAR
2. Calc the PEC
3. Wakeup Chip
4. Send command and read response
5. Check PEC
6. Copy data to object
7. Return Result -1= Error , 0= DataOkay
*********************************************************************************************************/
uint8_t LTC68041::rdauxa()
{
const uint8_t NUM_RX_BYT = 8; // number of bytes in the register + 2 bytes for the PEC
const uint8_t BYT_IN_REG = 6;
uint8_t pec_error = 0;
uint16_t received_pec;
uint8_t received_data[NUM_RX_BYT]; //data counter
uint16_t data_pec;
uint8_t data_counter=0; //data counter
uint8_t cmd[4];
uint16_t cmd_pec;
//1
cmd[0] = 0x00;
cmd[1] = 0x0C;
//2
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
//3
wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake, this command can be removed.
//4
spi_write_read(cmd,4,received_data,NUM_RX_BYT);
//5
//Set data pointer to the first PEC byte
data_counter= BYT_IN_REG;
received_pec = (received_data[data_counter] << 8) + received_data[data_counter+1]; //The received PEC for the current_ic is transmitted as the 7th and 8th
//(uint8_t len, uint8_t *data)
data_pec = pec15_calc(BYT_IN_REG, received_data);
if (received_pec != data_pec)
{
pec_error = -1; //The pec_error variable is simply set negative if any PEC errors
//are detected in the serial data
}
else
{
//6
//Copy Target Source Size
memcpy( AVAR, received_data, BYT_IN_REG );
}
//printArray(NUM_RX_BYT, received_data);
//7
return(pec_error);
}
/*!*******************************************************************************************************
The function is used to read the of the Auxiliary Register Group B of one LTC6804.
This function sends the read commands, parses the data and stores the response in AVBR.
1. Set command to AVBR
2. Calc the PEC
3. Wakeup Chip
4. Send command and read response
5. Check PEC
6. Copy data to object
7. Return Result -1= Error , 0= DataOkay
*********************************************************************************************************/
uint8_t LTC68041::rdauxb()
{
const uint8_t NUM_RX_BYT = 8; // number of bytes in the register + 2 bytes for the PEC
const uint8_t BYT_IN_REG = 6;
uint8_t pec_error = 0;
uint16_t received_pec;
uint8_t received_data[NUM_RX_BYT]; //data counter
uint16_t data_pec;
uint8_t data_counter=0; //data counter
uint8_t cmd[4];
uint16_t cmd_pec;
//1
cmd[0] = 0x00;
cmd[1] = 0x0E;
//2
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
//3
wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake, this command can be removed.
//4
spi_write_read(cmd,4,received_data,NUM_RX_BYT);
//5
//Set data pointer to the first PEC byte
data_counter= BYT_IN_REG;
received_pec = (received_data[data_counter] << 8) + received_data[data_counter+1]; //The received PEC for the current_ic is transmitted as the 7th and 8th
//(uint8_t len, uint8_t *data)
data_pec = pec15_calc(BYT_IN_REG, received_data);
if (received_pec != data_pec)
{
pec_error = -1; //The pec_error variable is simply set negative if any PEC errors
//are detected in the serial data
}
else
{
//6
//Copy Target Source Size
memcpy( AVBR, received_data, BYT_IN_REG );
}
//printArray(NUM_RX_BYT, received_data);
//7
return(pec_error);
}
/*!*******************************************************************************************************
The function is used to read the of the Status Register Group A of one LTC6804.
This function sends the read commands, parses the data and stores the response in STAR.
1. Set command to RDSTATA
2. Calc the PEC
3. Wakeup Chip
4. Send command and read response
5. Check PEC
6. Copy data to object
7. Return Result -1= Error , 0= DataOkay
*********************************************************************************************************/
uint8_t LTC68041::rdstata()
{
const uint8_t NUM_RX_BYT = 8; // number of bytes in the register + 2 bytes for the PEC
const uint8_t BYT_IN_REG = 6;
uint8_t pec_error = 0;
uint16_t received_pec;
uint8_t received_data[NUM_RX_BYT]; //data counter
uint16_t data_pec;
uint8_t data_counter=0; //data counter
uint8_t cmd[4];
uint16_t cmd_pec;
//1
cmd[0] = 0x00;
cmd[1] = 0x10;
//2
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
//3
wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake, this command can be removed.
//4
spi_write_read(cmd,4,received_data,NUM_RX_BYT);
//5
//Set data pointer to the first PEC byte
data_counter= BYT_IN_REG;
received_pec = (received_data[data_counter] << 8) + received_data[data_counter+1]; //The received PEC for the current_ic is transmitted as the 7th and 8th
//(uint8_t len, uint8_t *data)
data_pec = pec15_calc(BYT_IN_REG, received_data);
if (received_pec != data_pec)
{
pec_error = -1; //The pec_error variable is simply set negative if any PEC errors
//are detected in the serial data
}
else
{
//6
//Copy Target Source Size
memcpy( STAR, received_data, BYT_IN_REG );
Serial.println("Data Ok");
}
printArrayByte(BYT_IN_REG, STAR);
//printArray(NUM_RX_BYT, received_data);
//7
return(pec_error);
}
/*!*******************************************************************************************************
The function is used to read the of the Status Register Group B of one LTC6804.
This function sends the read commands, parses the data and stores the response in STBR.
1. Set command to RDSTATB
2. Calc the PEC
3. Wakeup Chip
4. Send command and read response
5. Check PEC
6. Copy data to object
7. Return Result -1= Error , 0= DataOkay
*********************************************************************************************************/
uint8_t LTC68041::rdstatb()
{
const uint8_t NUM_RX_BYT = 8; // number of bytes in the register + 2 bytes for the PEC
const uint8_t BYT_IN_REG = 6;
uint8_t pec_error = 0;
uint16_t received_pec;
uint8_t received_data[NUM_RX_BYT]; //data counter
uint16_t data_pec;
uint8_t data_counter=0; //data counter
uint8_t cmd[4];
uint16_t cmd_pec;
//1
cmd[0] = 0x00;
cmd[1] = 0x12;
//2
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
//3
wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake, this command can be removed.
//4
spi_write_read(cmd,4,received_data,NUM_RX_BYT);
//5
//Set data pointer to the first PEC byte
data_counter= BYT_IN_REG;
received_pec = (received_data[data_counter] << 8) + received_data[data_counter+1]; //The received PEC for the current_ic is transmitted as the 7th and 8th
//(uint8_t len, uint8_t *data)
data_pec = pec15_calc(BYT_IN_REG, received_data);
if (received_pec != data_pec)
{
pec_error = -1; //The pec_error variable is simply set negative if any PEC errors
//are detected in the serial data
}
else
{
//6
//Copy Target Source Size
memcpy( STBR, received_data, BYT_IN_REG );
}
//printArray(NUM_RX_BYT, received_data);
//7
return(pec_error);
}
/*!*******************************************************************************************************
The command clears the cell voltage registers and intiallizes
all values to 1. The register will read back hexadecimal 0xFF
after the command is sent.
1. Load clrcell command into cmd array
2. Calculate clrcell cmd PEC and load pec into cmd array
3. send broadcast clrcell command to LTC6804
*********************************************************************************************************/
void LTC68041::clrcell()
{
uint8_t cmd[4];
uint16_t cmd_pec;
//1
cmd[0] = 0x07;
cmd[1] = 0x11;
//2
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec );
//3
//wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake. This command can be removed.
//4
digitalWrite(csPin, LOW);
spi_write_read(cmd,4,0,0);
digitalWrite(csPin, HIGH);;
}
/*!*******************************************************************************************************
The function reads a single GPIO voltage register and stores thre read data
in the *data point as a byte array. This function is rarely used outside of
the LTC68041::rdaux() command.
1. Determine Command and initialize command array
2. Calculate Command PEC
3. Wake up isoSPI, this step is optional
4. Send Global Command to LTC6804
[in] uint8_t reg; This controls which GPIO voltage register is read back.
1: Read back auxiliary group A
2: Read back auxiliary group B
[out] uint8_t *data; An array of the unparsed aux codes
*********************************************************************************************************/
void LTC68041::rdaux_reg(uint8_t reg, uint8_t *data)
{
const uint8_t REG_LEN = 8; // number of bytes in the register + 2 bytes for the PEC
uint8_t cmd[4];
uint16_t cmd_pec;
//1
if (reg == 1) //Read back auxiliary group A
{
cmd[1] = 0x0C;
cmd[0] = 0x00;
}
else if (reg == 2) //Read back auxiliary group B
{
cmd[1] = 0x0e;
cmd[0] = 0x00;
}
else //Read back auxiliary group A
{
cmd[1] = 0x0C;
cmd[0] = 0x00;
}
//2
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
//3
wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake, this command can be removed.
//4
digitalWrite(csPin, LOW);
spi_write_read(cmd,4,data,REG_LEN);
digitalWrite(csPin, HIGH);;
}
/*!*******************************************************************************************************
Maps global ADC control variables to the appropriate control bytes for each of the different ADC commands
[in] uint8_t MD The adc conversion mode
[in] uint8_t DCP Controls if Discharge is permitted during cell conversions
[in] uint8_t CH Determines which cells are measured during an ADC conversion command
[in] uint8_t CHG Determines which GPIO channels are measured during Auxiliary conversion command
*********************************************************************************************************/
/*
void LTC68041::set_adc(uint8_t MD, uint8_t DCP, uint8_t CH, uint8_t CHG )
{
uint8_t md_bits;
md_bits = (MD & 0x02) >> 1;
ADCV[0] = md_bits + 0x02;
md_bits = (MD & 0x01) << 7;
ADCV[1] = md_bits + 0x60 + (DCP<<4) + CH;
md_bits = (MD & 0x02) >> 1;
ADAX[0] = md_bits + 0x04;
md_bits = (MD & 0x01) << 7;
ADAX[1] = md_bits + 0x60 + CHG ;
}
*/
/*!*******************************************************************************************************
A complete SPI communication check.
Reads the Status register and checks PECs of the response
No other command necessary, Just call this and get
[in] bool Result of the Check 1=Communication ok, 0=failure
1. Request for status register
2. Read fully status register
3. calc PEC from response
4. extract PEC from response
5. compare PECs
6. Send Serial message with result
*********************************************************************************************************/
void LTC68041::checkSPI()
{
uint16_t response_pec_calc;
uint16_t response_pec;
uint8_t cmd[8];
uint8_t response[16];
cmd[0] = 0x00;
cmd[1] = 0x02;
cmd[2] = 0x2b;
cmd[3] = 0x0A;
Serial.print("CMD: ");
Serial.print(cmd[0], HEX);
Serial.print(" ");
Serial.print(cmd[1], HEX);
Serial.print(" ");
Serial.print(cmd[2], HEX);
Serial.print(" ");
Serial.print(cmd[3], HEX);
//wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake. This command can be removed.
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(csPin, LOW);
SPI.transfer(cmd[0]);
SPI.transfer(cmd[1]);
SPI.transfer(cmd[2]);
SPI.transfer(cmd[3]);
for(int i=0; i<(SizeConfigReg+PEClen);i++)
{
response[i] =SPI.transfer(0);
}
digitalWrite(csPin, HIGH);
digitalWrite(LED_BUILTIN, LOW);
Serial.print("\nRSP: ");
for(int i=0; i<(SizeConfigReg+PEClen);i++)
{
Serial.print(response[i], HEX);
Serial.print(" ");
}
response_pec_calc = pec15_calc(SizeConfigReg, response);
response_pec=(response[SizeConfigReg]<<8)+response[SizeConfigReg+1];
Serial.print("\nPEC Calc: ");
Serial.print(response_pec_calc, HEX);
Serial.print("\nPEC Resp: ");
Serial.print(response_pec, HEX);
if(response_pec==response_pec_calc&(response_pec_calc!=0))
{
Serial.print("\nPEC was correct ");
}
else
{
Serial.print("\nPEC was NOT correct, check Hardware ");
}
Serial.print("\n\n");
}
/*!*******************************************************************************************************
A complete SPI communication check.
Reads the Status register and checks PECs of the response
No other command necessary, Just call this and get
[return] bool Result of the Check 1=Communication ok, 0=failure
This command does not send messages via Serial
1. Request for status register
2. Read fully status register
3. calc PEC from response
4. extract PEC from response
5. compare PECs
6. Return Result
*********************************************************************************************************/
bool LTC68041::checkSPI_mute()
{
uint16_t response_pec_calc;
uint16_t response_pec;
uint8_t cmd[8];
uint8_t response[16];
cmd[0] = 0x00;
cmd[1] = 0x02;
cmd[2] = 0x2b;
cmd[3] = 0x0A;
//wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake. This command can be removed.
digitalWrite(csPin, LOW);
SPI.transfer(cmd[0]);
SPI.transfer(cmd[1]);
SPI.transfer(cmd[2]);
SPI.transfer(cmd[3]);
for(int i=0; i<(SizeConfigReg+PEClen);i++)
{
response[i] =SPI.transfer(0);
}
digitalWrite(csPin, HIGH);