-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLTC681x.cpp
More file actions
2187 lines (1923 loc) · 57.6 KB
/
LTC681x.cpp
File metadata and controls
2187 lines (1923 loc) · 57.6 KB
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
/*! General BMS Library
***************************************************************************//**
* @file LTC681x.cpp
* @author BMS (bms.support@analog.com)
********************************************************************************
* Copyright 2019(c) Analog Devices, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* - Neither the name of Analog Devices, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* - The use of this software may or may not infringe the patent rights
* of one or more patent holders. This license does not release you
* from the requirement that you obtain separate licenses from these
* patent holders to use this software.
* - Use of the software either in source or binary form, must be run
* on or directly connected to an Analog Devices Inc. component.
*
* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
/*! @file
Library for LTC681x Multi-cell Battery Monitor
*/
#include <stdint.h>
#include "LTC681x.h"
#include "bms_hardware.h"
#ifdef LINDUINO
#include <Arduino.h>
#endif
/* Wake isoSPI up from IDlE state and enters the READY state */
void wakeup_idle(uint8_t total_ic) //Number of ICs in the system
{
for (int i =0; i<total_ic; i++)
{
cs_low(CS_PIN);
spi_read_byte(0xff);//Guarantees the isoSPI will be in ready mode
//delay_u(300);
cs_high(CS_PIN);
delay_u(10);
}
delay_u(1000);
}
/* Generic wakeup command to wake the LTC681x from sleep state */
void wakeup_sleep(uint8_t total_ic) //Number of ICs in the system
{
for (int i =0; i<total_ic; i++)
{
cs_low(CS_PIN);
delay_u(1000); // Guarantees the LTC681x will be in standby
cs_high(CS_PIN);
delay_u(10);
}
delay_u(1000);
}
/* Generic function to write 68xx commands. Function calculates PEC for tx_cmd data. */
void cmd_68(uint8_t tx_cmd[2]) //The command to be transmitted
{
uint8_t cmd[4];
uint16_t cmd_pec;
uint8_t md_bits;
cmd[0] = tx_cmd[0];
cmd[1] = tx_cmd[1];
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
cs_low(CS_PIN);
spi_write_array(4,cmd);
cs_high(CS_PIN);
}
/*
Generic function to write 68xx commands and write payload data.
Function calculates PEC for tx_cmd data and the data to be transmitted.
*/
void write_68(uint8_t total_ic, //Number of ICs to be written to
uint8_t tx_cmd[2], //The command to be transmitted
uint8_t data[] // Payload Data
)
{
const uint8_t BYTES_IN_REG = 6;
const uint8_t CMD_LEN = 4+(8*total_ic);
uint8_t *cmd;
uint16_t data_pec;
uint16_t cmd_pec;
uint8_t cmd_index;
cmd = (uint8_t *)malloc(CMD_LEN*sizeof(uint8_t));
cmd[0] = tx_cmd[0];
cmd[1] = tx_cmd[1];
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
cmd_index = 4;
for (uint8_t current_ic = total_ic; current_ic > 0; current_ic--) // Executes for each LTC681x, this loops starts with 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++)
{
cmd[cmd_index] = data[((current_ic-1)*6)+current_byte];
cmd_index = cmd_index + 1;
}
data_pec = (uint16_t)pec15_calc(BYTES_IN_REG, &data[(current_ic-1)*6]); // Calculating the PEC for each ICs configuration register data
cmd[cmd_index] = (uint8_t)(data_pec >> 8);
cmd[cmd_index + 1] = (uint8_t)data_pec;
cmd_index = cmd_index + 2;
}
cs_low(CS_PIN);
spi_write_array(CMD_LEN, cmd);
cs_high(CS_PIN);
free(cmd);
}
/* Generic function to write 68xx commands and read data. Function calculated PEC for tx_cmd data */
int8_t read_68( uint8_t total_ic, // Number of ICs in the system
uint8_t tx_cmd[2], // The command to be transmitted
uint8_t *rx_data // Data to be read
)
{
const uint8_t BYTES_IN_REG = 8;
uint8_t cmd[4];
uint8_t data[256];
int8_t pec_error = 0;
uint16_t cmd_pec;
uint16_t data_pec;
uint16_t received_pec;
cmd[0] = tx_cmd[0];
cmd[1] = tx_cmd[1];
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
cs_low(CS_PIN);
spi_write_read(cmd, 4, data, (BYTES_IN_REG*total_ic)); //Transmits the command and reads the configuration data of all ICs on the daisy chain into rx_data[] array
cs_high(CS_PIN);
for (uint8_t current_ic = 0; current_ic < total_ic; current_ic++) //Executes for each LTC681x in the daisy chain and packs the data
{ //into the rx_data array as well as check the received data for any bit errors
for (uint8_t current_byte = 0; current_byte < BYTES_IN_REG; current_byte++)
{
rx_data[(current_ic*8)+current_byte] = data[current_byte + (current_ic*BYTES_IN_REG)];
}
received_pec = (rx_data[(current_ic*8)+6]<<8) + rx_data[(current_ic*8)+7];
data_pec = pec15_calc(6, &rx_data[current_ic*8]);
if (received_pec != data_pec)
{
pec_error = -1;
}
}
return(pec_error);
}
/* Calculates and returns the CRC15 */
uint16_t pec15_calc(uint8_t len, //Number of bytes that will be used to calculate a PEC
uint8_t *data //Array of data that will be used to calculate a PEC
)
{
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
#ifdef MBED
remainder = (remainder<<8)^crc15Table[addr];
#else
remainder = (remainder<<8)^pgm_read_word_near(crc15Table+addr);
#endif
}
return(remainder*2);//The CRC15 has a 0 in the LSB so the remainder must be multiplied by 2
}
/* Write the LTC681x CFGRA */
void LTC681x_wrcfg(uint8_t total_ic, //The number of ICs being written to
cell_asic ic[] // A two dimensional array of the configuration data that will be written
)
{
uint8_t cmd[2] = {0x00 , 0x01} ;
uint8_t write_buffer[256];
uint8_t write_count = 0;
uint8_t c_ic = 0;
for (uint8_t current_ic = 0; current_ic<total_ic; current_ic++)
{
if (ic->isospi_reverse == false)
{
c_ic = current_ic;
}
else
{
c_ic = total_ic - current_ic - 1;
}
for (uint8_t data = 0; data<6; data++)
{
write_buffer[write_count] = ic[c_ic].config.tx_data[data];
write_count++;
}
}
write_68(total_ic, cmd, write_buffer);
}
/* Write the LTC681x CFGRB */
void LTC681x_wrcfgb(uint8_t total_ic, //The number of ICs being written to
cell_asic ic[] // A two dimensional array of the configuration data that will be written
)
{
uint8_t cmd[2] = {0x00 , 0x24} ;
uint8_t write_buffer[256];
uint8_t write_count = 0;
uint8_t c_ic = 0;
for (uint8_t current_ic = 0; current_ic<total_ic; current_ic++)
{
if (ic->isospi_reverse == false)
{
c_ic = current_ic;
}
else
{
c_ic = total_ic - current_ic - 1;
}
for (uint8_t data = 0; data<6; data++)
{
write_buffer[write_count] = ic[c_ic].configb.tx_data[data];
write_count++;
}
}
write_68(total_ic, cmd, write_buffer);
}
/* Read the LTC681x CFGA */
int8_t LTC681x_rdcfg(uint8_t total_ic, //Number of ICs in the system
cell_asic ic[] // A two dimensional array that the function stores the read configuration data.
)
{
uint8_t cmd[2]= {0x00 , 0x02};
uint8_t read_buffer[256];
int8_t pec_error = 0;
uint16_t data_pec;
uint16_t calc_pec;
uint8_t c_ic = 0;
pec_error = read_68(total_ic, cmd, read_buffer);
for (uint8_t current_ic = 0; current_ic<total_ic; current_ic++)
{
if (ic->isospi_reverse == false)
{
c_ic = current_ic;
}
else
{
c_ic = total_ic - current_ic - 1;
}
for (int byte=0; byte<8; byte++)
{
ic[c_ic].config.rx_data[byte] = read_buffer[byte+(8*current_ic)];
}
calc_pec = pec15_calc(6,&read_buffer[8*current_ic]);
data_pec = read_buffer[7+(8*current_ic)] | (read_buffer[6+(8*current_ic)]<<8);
if (calc_pec != data_pec )
{
ic[c_ic].config.rx_pec_match = 1;
}
else ic[c_ic].config.rx_pec_match = 0;
}
LTC681x_check_pec(total_ic,CFGR,ic);
return(pec_error);
}
/* Reads the LTC681x CFGB */
int8_t LTC681x_rdcfgb(uint8_t total_ic, //Number of ICs in the system
cell_asic ic[] // A two dimensional array that the function stores the read configuration data.
)
{
uint8_t cmd[2]= {0x00 , 0x26};
uint8_t read_buffer[256];
int8_t pec_error = 0;
uint16_t data_pec;
uint16_t calc_pec;
uint8_t c_ic = 0;
pec_error = read_68(total_ic, cmd, read_buffer);
for (uint8_t current_ic = 0; current_ic<total_ic; current_ic++)
{
if (ic->isospi_reverse == false)
{
c_ic = current_ic;
}
else
{
c_ic = total_ic - current_ic - 1;
}
for (int byte=0; byte<8; byte++)
{
ic[c_ic].configb.rx_data[byte] = read_buffer[byte+(8*current_ic)];
}
calc_pec = pec15_calc(6,&read_buffer[8*current_ic]);
data_pec = read_buffer[7+(8*current_ic)] | (read_buffer[6+(8*current_ic)]<<8);
if (calc_pec != data_pec )
{
ic[c_ic].configb.rx_pec_match = 1;
}
else ic[c_ic].configb.rx_pec_match = 0;
}
LTC681x_check_pec(total_ic,CFGRB,ic);
return(pec_error);
}
/* Starts ADC conversion for cell voltage */
void LTC681x_adcv( uint8_t MD, //ADC Mode
uint8_t DCP, //Discharge Permit
uint8_t CH //Cell Channels to be measured
)
{
uint8_t cmd[2];
uint8_t md_bits;
md_bits = (MD & 0x02) >> 1;
cmd[0] = md_bits + 0x02;
md_bits = (MD & 0x01) << 7;
cmd[1] = md_bits + 0x60 + (DCP<<4) + CH;
cmd_68(cmd);
}
/* Start ADC Conversion for GPIO and Vref2 */
void LTC681x_adax(uint8_t MD, //ADC Mode
uint8_t CHG //GPIO Channels to be measured
)
{
uint8_t cmd[4];
uint8_t md_bits;
md_bits = (MD & 0x02) >> 1;
cmd[0] = md_bits + 0x04;
md_bits = (MD & 0x01) << 7;
cmd[1] = md_bits + 0x60 + CHG ;
cmd_68(cmd);
}
/* Start ADC Conversion for Status */
void LTC681x_adstat(uint8_t MD, //ADC Mode
uint8_t CHST //Stat Channels to be measured
)
{
uint8_t cmd[4];
uint8_t md_bits;
md_bits = (MD & 0x02) >> 1;
cmd[0] = md_bits + 0x04;
md_bits = (MD & 0x01) << 7;
cmd[1] = md_bits + 0x68 + CHST ;
cmd_68(cmd);
}
/* Starts cell voltage and SOC conversion */
void LTC681x_adcvsc(uint8_t MD, //ADC Mode
uint8_t DCP //Discharge Permit
)
{
uint8_t cmd[2];
uint8_t md_bits;
md_bits = (MD & 0x02) >> 1;
cmd[0] = md_bits | 0x04;
md_bits = (MD & 0x01) << 7;
cmd[1] = md_bits | 0x60 | (DCP<<4) | 0x07;
cmd_68(cmd);
}
/* Starts cell voltage and GPIO 1&2 conversion */
void LTC681x_adcvax(uint8_t MD, //ADC Mode
uint8_t DCP //Discharge Permit
)
{
uint8_t cmd[2];
uint8_t md_bits;
md_bits = (MD & 0x02) >> 1;
cmd[0] = md_bits | 0x04;
md_bits = (MD & 0x01) << 7;
cmd[1] = md_bits | ((DCP&0x01)<<4) + 0x6F;
cmd_68(cmd);
}
/*
Reads and parses the LTC681x cell voltage registers.
The function is used to read the parsed Cell voltages codes of the LTC681x.
This function will send the requested read commands parse the data
and store the cell voltages in c_codes variable.
*/
uint8_t LTC681x_rdcv(uint8_t reg, // Controls which cell voltage register is read back.
uint8_t total_ic, // The number of ICs in the system
cell_asic *ic // Array of the parsed cell codes
)
{
int8_t pec_error = 0;
uint8_t *cell_data;
uint8_t c_ic = 0;
cell_data = (uint8_t *) malloc((NUM_RX_BYT*total_ic)*sizeof(uint8_t));
if (reg == 0)
{
for (uint8_t cell_reg = 1; cell_reg<ic[0].ic_reg.num_cv_reg+1; cell_reg++) //Executes once for each of the LTC681x cell voltage registers
{
LTC681x_rdcv_reg(cell_reg, total_ic,cell_data );
for (int current_ic = 0; current_ic<total_ic; current_ic++)
{
if (ic->isospi_reverse == false)
{
c_ic = current_ic;
}
else
{
c_ic = total_ic - current_ic - 1;
}
pec_error = pec_error + parse_cells(current_ic,cell_reg, cell_data,
&ic[c_ic].cells.c_codes[0],
&ic[c_ic].cells.pec_match[0]);
}
}
}
else
{
LTC681x_rdcv_reg(reg, total_ic,cell_data);
for (int current_ic = 0; current_ic<total_ic; current_ic++)
{
if (ic->isospi_reverse == false)
{
c_ic = current_ic;
}
else
{
c_ic = total_ic - current_ic - 1;
}
pec_error = pec_error + parse_cells(current_ic,reg, &cell_data[8*c_ic],
&ic[c_ic].cells.c_codes[0],
&ic[c_ic].cells.pec_match[0]);
}
}
LTC681x_check_pec(total_ic,CELL,ic);
free(cell_data);
return(pec_error);
}
/*
The function is used to read the parsed GPIO codes of the LTC681x.
This function will send the requested read commands parse the data
and store the gpio voltages in a_codes variable.
*/
int8_t LTC681x_rdaux(uint8_t reg, //Determines which GPIO voltage register is read back.
uint8_t total_ic,//The number of ICs in the system
cell_asic *ic//A two dimensional array of the gpio voltage codes.
)
{
uint8_t *data;
int8_t pec_error = 0;
uint8_t c_ic =0;
data = (uint8_t *) malloc((NUM_RX_BYT*total_ic)*sizeof(uint8_t));
if (reg == 0)
{
for (uint8_t gpio_reg = 1; gpio_reg<ic[0].ic_reg.num_gpio_reg+1; gpio_reg++) //Executes once for each of the LTC681x aux voltage registers
{
LTC681x_rdaux_reg(gpio_reg, total_ic,data); //Reads the raw auxiliary register data into the data[] array
for (int current_ic = 0; current_ic<total_ic; current_ic++)
{
if (ic->isospi_reverse == false)
{
c_ic = current_ic;
}
else
{
c_ic = total_ic - current_ic - 1;
}
pec_error = parse_cells(current_ic,gpio_reg, data,
&ic[c_ic].aux.a_codes[0],
&ic[c_ic].aux.pec_match[0]);
}
}
}
else
{
LTC681x_rdaux_reg(reg, total_ic, data);
for (int current_ic = 0; current_ic<total_ic; current_ic++)
{
if (ic->isospi_reverse == false)
{
c_ic = current_ic;
}
else
{
c_ic = total_ic - current_ic - 1;
}
pec_error = parse_cells(current_ic,reg, data,
&ic[c_ic].aux.a_codes[0],
&ic[c_ic].aux.pec_match[0]);
}
}
LTC681x_check_pec(total_ic,AUX,ic);
free(data);
return (pec_error);
}
/*
Reads and parses the LTC681x stat registers.
The function is used to read the parsed Stat codes of the LTC681x.
This function will send the requested read commands parse the data
and store the gpio voltages in stat_codes variable.
*/
int8_t LTC681x_rdstat(uint8_t reg, //Determines which Stat register is read back.
uint8_t total_ic,//The number of ICs in the system
cell_asic *ic //A two dimensional array of the stat codes.
)
{
const uint8_t BYT_IN_REG = 6;
const uint8_t STAT_IN_REG = 3;
uint8_t *data;
uint8_t data_counter = 0;
int8_t pec_error = 0;
uint16_t parsed_stat;
uint16_t received_pec;
uint16_t data_pec;
uint8_t c_ic = 0;
data = (uint8_t *) malloc((12*total_ic)*sizeof(uint8_t));
if (reg == 0)
{
for (uint8_t stat_reg = 1; stat_reg< 3; stat_reg++) //Executes once for each of the LTC681x stat voltage registers
{
data_counter = 0;
LTC681x_rdstat_reg(stat_reg, total_ic,data); //Reads the raw status register data into the data[] array
for (uint8_t current_ic = 0 ; current_ic < total_ic; current_ic++) // Executes for every LTC681x in the daisy chain
{ // current_ic is used as the IC counter
if (ic->isospi_reverse == false)
{
c_ic = current_ic;
}
else
{
c_ic = total_ic - current_ic - 1;
}
if (stat_reg ==1)
{
for (uint8_t current_stat = 0; current_stat< STAT_IN_REG; current_stat++) // This loop parses the read back data into Status registers,
{ // it loops once for each of the 3 stat codes in the register
parsed_stat = data[data_counter] + (data[data_counter+1]<<8); //Each stat codes is received as two bytes and is combined to create the parsed status code
ic[c_ic].stat.stat_codes[current_stat] = parsed_stat;
data_counter=data_counter+2; //Because stat codes are two bytes the data counter
}
}
else if (stat_reg == 2)
{
parsed_stat = data[data_counter] + (data[data_counter+1]<<8); //Each stat is received as two bytes and is combined to create the parsed status code
data_counter = data_counter +2;
ic[c_ic].stat.stat_codes[3] = parsed_stat;
ic[c_ic].stat.flags[0] = data[data_counter++];
ic[c_ic].stat.flags[1] = data[data_counter++];
ic[c_ic].stat.flags[2] = data[data_counter++];
ic[c_ic].stat.mux_fail[0] = (data[data_counter] & 0x02)>>1;
ic[c_ic].stat.thsd[0] = data[data_counter++] & 0x01;
}
received_pec = (data[data_counter]<<8)+ data[data_counter+1]; //The received PEC for the current_ic is transmitted as the 7th and 8th
//after the 6 status data bytes
data_pec = pec15_calc(BYT_IN_REG, &data[current_ic*NUM_RX_BYT]);
if (received_pec != data_pec)
{
pec_error = -1; //The pec_error variable is simply set negative if any PEC errors
ic[c_ic].stat.pec_match[stat_reg-1]=1; //are detected in the received serial data
}
else
{
ic[c_ic].stat.pec_match[stat_reg-1]=0;
}
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 status data
}
}
}
else
{
LTC681x_rdstat_reg(reg, total_ic, data);
for (int current_ic = 0 ; current_ic < total_ic; current_ic++) // Executes for every LTC681x in the daisy chain
{ // current_ic is used as an IC counter
if (ic->isospi_reverse == false)
{
c_ic = current_ic;
}
else
{
c_ic = total_ic - current_ic - 1;
}
if (reg ==1)
{
for (uint8_t current_stat = 0; current_stat< STAT_IN_REG; current_stat++) // This loop parses the read back data into Status voltages, it
{ // loops once for each of the 3 stat codes in the register
parsed_stat = data[data_counter] + (data[data_counter+1]<<8); //Each stat codes is received as two bytes and is combined to
// create the parsed stat code
ic[c_ic].stat.stat_codes[current_stat] = parsed_stat;
data_counter=data_counter+2; //Because stat codes are two bytes the data counter
//must increment by two for each parsed stat code
}
}
else if (reg == 2)
{
parsed_stat = data[data_counter++] + (data[data_counter++]<<8); //Each stat codes is received as two bytes and is combined to
ic[c_ic].stat.stat_codes[3] = parsed_stat;
ic[c_ic].stat.flags[0] = data[data_counter++];
ic[c_ic].stat.flags[1] = data[data_counter++];
ic[c_ic].stat.flags[2] = data[data_counter++];
ic[c_ic].stat.mux_fail[0] = (data[data_counter] & 0x02)>>1;
ic[c_ic].stat.thsd[0] = data[data_counter++] & 0x01;
}
received_pec = (data[data_counter]<<8)+ data[data_counter+1]; //The received PEC for the current_ic is transmitted as the 7th and 8th
//after the 6 status data bytes
data_pec = pec15_calc(BYT_IN_REG, &data[current_ic*NUM_RX_BYT]);
if (received_pec != data_pec)
{
pec_error = -1; //The pec_error variable is simply set negative if any PEC errors
ic[c_ic].stat.pec_match[reg-1]=1;
}
data_counter=data_counter+2;
}
}
LTC681x_check_pec(total_ic,STAT,ic);
free(data);
return (pec_error);
}
/* Writes the command and reads the raw cell voltage register data */
void LTC681x_rdcv_reg(uint8_t reg, //Determines which cell voltage register is read back
uint8_t total_ic, //the number of ICs in the
uint8_t *data //An array of the unparsed cell codes
)
{
const uint8_t REG_LEN = 8; //Number of bytes in each ICs register + 2 bytes for the PEC
uint8_t cmd[4];
uint16_t cmd_pec;
if (reg == 1) //1: RDCVA
{
cmd[1] = 0x04;
cmd[0] = 0x00;
}
else if (reg == 2) //2: RDCVB
{
cmd[1] = 0x06;
cmd[0] = 0x00;
}
else if (reg == 3) //3: RDCVC
{
cmd[1] = 0x08;
cmd[0] = 0x00;
}
else if (reg == 4) //4: RDCVD
{
cmd[1] = 0x0A;
cmd[0] = 0x00;
}
else if (reg == 5) //4: RDCVE
{
cmd[1] = 0x09;
cmd[0] = 0x00;
}
else if (reg == 6) //4: RDCVF
{
cmd[1] = 0x0B;
cmd[0] = 0x00;
}
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
cs_low(CS_PIN);
spi_write_read(cmd,4,data,(REG_LEN*total_ic));
cs_high(CS_PIN);
}
/*
The function reads a single GPIO voltage register and stores the read data
in the *data point as a byte array. This function is rarely used outside of
the LTC681x_rdaux() command.
*/
void LTC681x_rdaux_reg(uint8_t reg, //Determines which GPIO voltage register is read back
uint8_t total_ic, //The number of ICs in the system
uint8_t *data //Array of the unparsed auxiliary codes
)
{
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;
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 if (reg == 3) //Read back auxiliary group C
{
cmd[1] = 0x0D;
cmd[0] = 0x00;
}
else if (reg == 4) //Read back auxiliary group D
{
cmd[1] = 0x0F;
cmd[0] = 0x00;
}
else //Read back auxiliary group A
{
cmd[1] = 0x0C;
cmd[0] = 0x00;
}
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
cs_low(CS_PIN);
spi_write_read(cmd,4,data,(REG_LEN*total_ic));
cs_high(CS_PIN);
}
/*
The function reads a single stat register and stores the read data
in the *data point as a byte array. This function is rarely used outside of
the LTC681x_rdstat() command.
*/
void LTC681x_rdstat_reg(uint8_t reg, //Determines which stat register is read back
uint8_t total_ic, //The number of ICs in the system
uint8_t *data //Array of the unparsed stat codes
)
{
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;
if (reg == 1) //Read back status group A
{
cmd[1] = 0x10;
cmd[0] = 0x00;
}
else if (reg == 2) //Read back status group B
{
cmd[1] = 0x12;
cmd[0] = 0x00;
}
else //Read back status group A
{
cmd[1] = 0x10;
cmd[0] = 0x00;
}
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
cs_low(CS_PIN);
spi_write_read(cmd,4,data,(REG_LEN*total_ic));
cs_high(CS_PIN);
}
/* Helper function that parses voltage measurement registers */
int8_t parse_cells(uint8_t current_ic, // Current IC
uint8_t cell_reg, // Type of register
uint8_t cell_data[], // Unparsed data
uint16_t *cell_codes, // Parsed data
uint8_t *ic_pec // PEC error
)
{
const uint8_t BYT_IN_REG = 6;
const uint8_t CELL_IN_REG = 3;
int8_t pec_error = 0;
uint16_t parsed_cell;
uint16_t received_pec;
uint16_t data_pec;
uint8_t data_counter = current_ic*NUM_RX_BYT; //data counter
for (uint8_t current_cell = 0; current_cell<CELL_IN_REG; current_cell++) // This loop parses the read back data into the register codes, it
{ // loops once for each of the 3 codes in the register
parsed_cell = cell_data[data_counter] + (cell_data[data_counter + 1] << 8);//Each code is received as two bytes and is combined to
// create the parsed code
cell_codes[current_cell + ((cell_reg - 1) * CELL_IN_REG)] = parsed_cell;
data_counter = data_counter + 2; //Because the codes are two bytes, the data counter
//must increment by two for each parsed code
}
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[(current_ic) * NUM_RX_BYT]);
if (received_pec != data_pec)
{
pec_error = 1; //The pec_error variable is simply set negative if any PEC errors
ic_pec[cell_reg-1]=1;
}
else
{
ic_pec[cell_reg-1]=0;
}
data_counter=data_counter+2;
return(pec_error);
}
/* Sends the poll ADC command */
uint8_t LTC681x_pladc()
{
uint8_t cmd[4];
uint8_t adc_state = 0xFF;
uint16_t cmd_pec;
cmd[0] = 0x07;
cmd[1] = 0x14;
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
cs_low(CS_PIN);
spi_write_array(4,cmd);
adc_state = spi_read_byte(0xFF);
cs_high(CS_PIN);
return(adc_state);
}
/* This function will block operation until the ADC has finished it's conversion */
uint32_t LTC681x_pollAdc()
{
uint32_t counter = 0;
uint8_t finished = 0;
uint8_t current_time = 0;
uint8_t cmd[4];
uint16_t cmd_pec;
cmd[0] = 0x07;
cmd[1] = 0x14;
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 8);
cmd[3] = (uint8_t)(cmd_pec);
cs_low(CS_PIN);
spi_write_array(4,cmd);
while ((counter<200000)&&(finished == 0))
{
current_time = spi_read_byte(0xff);
if (current_time>0)
{
finished = 1;
}
else
{
counter = counter + 10;
}
}
cs_high(CS_PIN);
return(counter);
}
/*
The command clears the cell voltage registers and initializes
all values to 1. The register will read back hexadecimal 0xFF
after the command is sent.
*/
void LTC681x_clrcell()
{
uint8_t cmd[2]= {0x07 , 0x11};
cmd_68(cmd);
}
/*
The command clears the Auxiliary registers and initializes
all values to 1. The register will read back hexadecimal 0xFF
after the command is sent.
*/
void LTC681x_clraux()
{
uint8_t cmd[2]= {0x07 , 0x12};
cmd_68(cmd);
}
/*
The command clears the Stat registers and initializes
all values to 1. The register will read back hexadecimal 0xFF
after the command is sent.
*/
void LTC681x_clrstat()
{
uint8_t cmd[2]= {0x07 , 0x13};
cmd_68(cmd);
}
/* Starts the Mux Decoder diagnostic self test */
void LTC681x_diagn()
{
uint8_t cmd[2] = {0x07 , 0x15};
cmd_68(cmd);
}
/* Starts cell voltage self test conversion */
void LTC681x_cvst(uint8_t MD, //ADC Mode
uint8_t ST //Self Test
)
{
uint8_t cmd[2];
uint8_t md_bits;
md_bits = (MD & 0x02) >> 1;
cmd[0] = md_bits + 0x02;
md_bits = (MD & 0x01) << 7;
cmd[1] = md_bits + ((ST)<<5) +0x07;
cmd_68(cmd);
}
/* Start an Auxiliary Register Self Test Conversion */
void LTC681x_axst(uint8_t MD, //ADC Mode
uint8_t ST //Self Test
)
{
uint8_t cmd[2];
uint8_t md_bits;