-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
832 lines (681 loc) · 17.5 KB
/
main.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
#define __AVR_ATmega328P__
#define F_CPU 16000000UL
#pragma device atmega328p
#pragma executable ihex
#pragma output Output
#pragma platform avr
#pragma optimization 0
void debug(unsigned char data);
#include "avr/io.h"
#include "avr/delay.h"
#include "stddef.h"
#include "avr/interrupt.h"
#define CHG_LED_DDR DDRD
#define CHG_LED_PORT PORTD
#define CHG_LED_MASK (1<<7)
#define OK_LED_DDR DDRD
#define OK_LED_PORT PORTD
#define OK_LED_MASK (1<<6)
#define FAN_DDR DDRB
#define FAN_PORT PORTB
#define FAN_MASK (1<<1)
#define HIGH_CHARGING_VOLTAGE 41000
#define LOW_CHARGING_VOLTAGE 40900
#define HIGH_RES_CURRENT_SENSOR_ADC 0
#define LOW_RES_CURRENT_SENSOR_ADC 1
#define BATTERY_VOLTAGE_HIGH_CUTOFF 42000
#define BATTERY_VOLTAGE_LOW_CUTOFF 30000
#define BATTERY_TEMP_MAX 297
#define BATTERY_TEMP_FAN_ON 326
#define BATTERY_TEMP_FAN_OFF 344
int16_t debugData;
/*
So we have two current sensors....a low res one and a high res one
The 0 to 1023 on the high res one is -75 to 75 Amps, while the same
range on the low res one is -750 to 750 Amps.
We will store the current as a value between -5120 and 5110, so that the
low one can be used when appropriate, and vice versa
mh = 75amps/512adc
ml = 750amps/512adc
H = 512 * A / 75
*/
// 600 Amps
#define CURRENT_HARD_CUT_OFF 4096
// 400 Amps
#define CURRENT_SOFT_CUT_OFF 2731
//5 seconds
#define CURRENT_SOFT_TIME_OUT 5000
#include "typedefs.h"
#include "spi.h"
#include "BmsBoard.h"
#include "Timer.h"
#include "Serial.h"
#include "Adc.h"
#include "stdlib.h"
Serial_t serial;
void debug(uint8_t data)
{
DDRC |= 0x1F;
PORTC |= 0x10;
PORTC = (0xF0 & PORTC) | (data&0x0F); //Bottom half
_delay_ms(1000);
PORTC &= ~0x10;
PORTC = (0xF0 & PORTC) | ((data>>4)&0x0F); //Upper half
_delay_ms(1000);
}
bool IsAnyDischarging(uint8_t x,uint8_t y);
bool IsAnyDischargingAtAll();
volatile BmsChain_t chain;
volatile uint16_t ms_count = 0;
//Serial_t serial;
uint16_t lowChargingVoltage = 0;
uint16_t int_count = 0;
//This is the 10 millisecond interrupt
ISR(TIMER0_COMPA_vect){
ms_count += 10;
int_count++;
if (int_count >= 100)
{
int_count = 0;
}
BmsChain_t* localChain = (BmsChain_t*)&chain;
if (CHG_LED_PORT & CHG_LED_MASK)
{
for (uint8_t x = 0; x < BMS_BOARD_COUNT; x++)
{
for (uint8_t y = 0; y < BMS_CELL_COUNT; y++)
{
if (localChain->boards[x].cells[y].DischargeFlag)
{
CHG_LED_PORT &= ~CHG_LED_MASK;
}
}
}
}
#define DEBUG
#ifdef DEBUG
for (uint8_t x = 0; x < BMS_BOARD_COUNT; x++)
{
for (uint8_t y = 0; y < BMS_CELL_COUNT; y++)
{
if (CHG_LED_PORT & CHG_LED_MASK)
{
if (rand() % 100 < 40)
{
localChain->boards[x].cells[y].LastReading+=rand()%100;
}
}
else
{
if (localChain->boards[x].cells[y].DischargeFlag)
{
if (rand() % 100 < 40)
{
localChain->boards[x].cells[y].LastReading-=rand()%100;
}
}
}
}
}
#else
for (uint8_t x = 0; x < BMS_BOARD_COUNT; x++)
{
uint16_t Discharge = 0;
for (uint8_t y = 0; y < BMS_CELL_COUNT; y++)
{
if (localChain->boards[x].cells[y].DischargeFlag && int_count > 1)
{
Discharge |= 1 << y;
}
if (Discharge != localChain->boards[x].config.DischargeCell)
{
localChain->boards[x].config.DischargeCell = Discharge;
localChain->updateConfig = true;
}
}
}
if (int_count == 1 || !(IsAnyDischargingAtAll()))
{
ChainUpdateCellReadings(localChain);
//while (true) { OK_LED_PORT ^= OK_LED_MASK; _delay_ms(1000);}
}
ChainUpdateGpioReadings(localChain);
if (localChain->updateConfig)
{
localChain->updateConfig = false;
ChainFlushConfig(localChain);
}
#endif
}
void numberToString(uint16_t number, char* ptr, uint8_t dp)
{
if (number == 0)
{
ptr[0] = '0';
ptr[1] = 0;
return;
}
uint16_t div = 10000;
uint8_t x = 6;
bool leadingZeros = false;
while (div != 0)
{
uint8_t digit = number / div;
char temp = digit + '0';
if (leadingZeros || temp != '0')
{
*ptr = temp;
ptr++;
leadingZeros = true;
}
number %= div;
div /= 10;
if (x == dp)
{
*ptr='.';
ptr++;
}
x--;
}
*ptr = 0;
}
int16_t GetCurrentFlow()
{
return debugData;
int16_t lowRes;
int16_t highRes;
cli();
lowRes = Adc.conversions[LOW_RES_CURRENT_SENSOR_ADC];
highRes = Adc.conversions[HIGH_RES_CURRENT_SENSOR_ADC];
sei();
lowRes -= 511;
lowRes *= 10;
if (lowRes >= 500)
{
return lowRes;
}
highRes -= 511;
return highRes;
}
bool IsAnyDischargingAtAll(void)
{
for (uint8_t x = 0; x < BMS_BOARD_COUNT; x++) //Go through each cell and find the highest
{
for (uint8_t y = 0; y < BMS_CELL_COUNT; y++)
{
if (!chain.boards[x].cells[y].IgnoreFlag){
bool ret = false;
cli();
if (chain.boards[x].cells[y].DischargeFlag)
{
ret = true;
}
sei();
if (ret)
return true;
}
}
}
return false;
}
bool IsAnyDischarging(uint8_t boardIndex,uint8_t cellindex)
{
bool odd = chain.boards[boardIndex].cells[cellindex].OddFlag;
for (uint8_t x = 0; x < BMS_BOARD_COUNT; x++) //Go through each cell and find the highest
{
for (uint8_t y = 0; y < BMS_CELL_COUNT; y++)
{
if (!chain.boards[x].cells[y].IgnoreFlag && odd != chain.boards[x].cells[y].OddFlag){
bool ret = false;
cli();
if (chain.boards[x].cells[y].DischargeFlag)
{
ret = true;
}
sei();
if (ret)
return true;
}
}
}
return false;
}
void BalanceBmsCells()
{
uint16_t highestVoltage = 0;
uint8_t highestBoard;
uint8_t highestCell;
uint8_t allIn = true;
uint16_t highestDischargeVoltage = 0;
if (!IsAnyDischargingAtAll())
lowChargingVoltage = 0xFFFF;
for (uint8_t x = 0; x < BMS_BOARD_COUNT; x++) //Go through each cell and find the highest
{
for (uint8_t y = 0; y < BMS_CELL_COUNT; y++)
{
if (!chain.boards[x].cells[y].IgnoreFlag){
cli();
uint16_t reading = chain.boards[x].cells[y].LastReading;
bool discharging = chain.boards[x].cells[y].DischargeFlag;
sei();
if (reading >= highestVoltage)
{
highestVoltage = reading;
highestBoard = x;
highestCell = y;
}
if (reading >= highestDischargeVoltage && discharging)
{
highestDischargeVoltage = reading;
}
if (reading < lowChargingVoltage && !(IsAnyDischargingAtAll()))
{
lowChargingVoltage = reading;
}
if ((reading <= LOW_CHARGING_VOLTAGE || reading >= HIGH_CHARGING_VOLTAGE))
{
allIn = false;
}
allIn = false;
}
}
}
debugData = lowChargingVoltage;
if (highestVoltage >= HIGH_CHARGING_VOLTAGE && !IsAnyDischarging(highestBoard,highestCell)) // if the highest voltage is greater than the threshold than discharge the cell
{
cli();
chain.boards[highestBoard].cells[highestCell].DischargeFlag = true;
chain.updateConfig = true;
sei();
CHG_LED_PORT &= ~CHG_LED_MASK;
}
if (allIn)
{
CHG_LED_PORT &= ~CHG_LED_MASK;
for (uint8_t x = 0; x < BMS_BOARD_COUNT; x++)
{
for (uint8_t y = 0; y < BMS_CELL_COUNT; y++)
{
if (!chain.boards[x].cells[y].IgnoreFlag)
{
cli();
bool dis = chain.boards[x].cells[y].DischargeFlag;
sei();
if (dis)
{
cli();
chain.boards[x].cells[y].DischargeFlag = false;
chain.updateConfig = true;
sei();
}
}
}
}
}
else if (!(CHG_LED_PORT & CHG_LED_MASK))
{
uint8_t startCharging = true;
for (uint8_t x = 0; x < BMS_BOARD_COUNT; x++)
{
for (uint8_t y = 0; y < BMS_CELL_COUNT; y++)
{
if (!chain.boards[x].cells[y].IgnoreFlag)
{
cli();
BmsCell_t cell;
cell.LastReading = chain.boards[x].cells[y].LastReading;
cell.DischargeFlag = chain.boards[x].cells[y].DischargeFlag;
sei();
if (cell.LastReading >= HIGH_CHARGING_VOLTAGE ||
(cell.LastReading >= highestDischargeVoltage && highestDischargeVoltage != 0))
{
/*
char thing[10];
numberToString(highestDischargeVoltage, thing,-1);
SerialSend(&serial,"\n\n\n\n\n\n\n\n\n\n");
_delay_ms(1000);
SerialSend(&serial,"\n\n\n\n\n\n\n\n\n\n");
SerialSend(&serial, thing);
while(1);*/
startCharging = false;
if (!cell.DischargeFlag && !IsAnyDischarging(x,y))
{
cli();
chain.boards[x].cells[y].DischargeFlag = true;
chain.updateConfig = true;
sei();
}
}
if (cell.DischargeFlag)
{
startCharging = false;
if (cell.LastReading <= lowChargingVoltage)
{
/*
char thing[10];
numberToString(lowChargingVoltage, thing,-1);
SerialSend(&serial,"\n\n\n\n\n\n\n\n\n\n");
_delay_ms(1000);
SerialSend(&serial, thing);
SerialSend(&serial,"\n\n\n\n\n\n\n\n\n\nCells");
for (uint8_t X = 0; X < BMS_BOARD_COUNT; X++)
{
for (uint8_t Y = 0; Y < BMS_CELL_COUNT; Y++)
{
if (!chain.boards[x].cells[y].IgnoreFlag)
{
numberToString(chain.boards[X].cells[Y].LastReading, thing,-1);
SerialSend(&serial,thing);
SerialSend(&serial, " ");
SerialSend(&serial, chain.boards[X].cells[Y].DischargeFlag ? "1" : "0");
SerialSend(&serial,"\n ");
}
}
}
*/
cli();
chain.boards[x].cells[y].DischargeFlag = false;
chain.updateConfig = true;
sei();
if (!(IsAnyDischargingAtAll()))
{
startCharging = true;
}
}
}
}
}
}
if (startCharging)
{
CHG_LED_PORT |= CHG_LED_MASK;
}
}
}
uint16_t GetBatteryTemp()
{
//#warning WE NEED A METHOD OF GETTING THE TEMP
cli();
uint16_t ret = chain.boards[1].gpio_voltage[0];
sei();
return ret;
}
bool IsTempSafe()
{
// Higher voltage means lower temp
//return true;
return GetBatteryTemp() > BATTERY_TEMP_MAX;
}
bool AreBatteriesSafe()
{
for (int x = 0; x < BMS_BOARD_COUNT; x++)
{
for (int y = 0; y < BMS_CELL_COUNT; y++)
{
if (!chain.boards[x].cells[y].IgnoreFlag)
{
cli();
uint16_t reading = chain.boards[x].cells[y].LastReading;
sei();
if (reading <= BATTERY_VOLTAGE_LOW_CUTOFF)
return false;
if (reading >= BATTERY_VOLTAGE_HIGH_CUTOFF)
return false;
}
}
}
return true;
}
bool IsCurrentSafe()
{
static uint16_t ms_since_low = 0;
int16_t current = GetCurrentFlow();
if (current >= CURRENT_HARD_CUT_OFF)
{
return false;
}
if (current >= CURRENT_SOFT_CUT_OFF)
{
cli();
uint16_t ms = ms_count;
sei();
if ((uint16_t)(ms - ms_since_low) >= CURRENT_SOFT_TIME_OUT)
{
return false;
}
}
else
{
cli();
ms_since_low = ms_count;
sei();
}
return true;
}
bool IsCarSafe()
{
return IsCurrentSafe() && AreBatteriesSafe() && IsTempSafe();
}
void HumanReadablePrintCells()
{
static uint16_t lastDisplay = 0;
cli();
uint16_t ms_current = ms_count;
sei();
if ((uint16_t)(ms_current - lastDisplay) > 1000)
{
lastDisplay = ms_current;
for (uint8_t y = 0; y < BMS_BOARD_COUNT; y++)
{
for (uint8_t x = 0; x < 12; x++)
{
if (!chain.boards[y].cells[x].IgnoreFlag){
char reading[8];
cli();
numberToString(chain.boards[y].cells[x].LastReading, reading,6);
sei();
SerialSend(&serial,reading);
SerialSend(&serial,"=C#");
numberToString(x+1, reading,255);
SerialSend(&serial,reading);
if (chain.boards[y].cells[x].DischargeFlag)
{
SerialSend(&serial,"*, ");
}
else
{
SerialSend(&serial," , ");
}
}
}
SerialSend(&serial,'\n');
for (uint8_t x = 0; x < 6; x++)
{
char reading[8];
cli();
numberToString(chain.boards[y].gpio_voltage[x], reading,6);
sei();
SerialSend(&serial,reading);
SerialSend(&serial,"=GPIO");
numberToString(x, reading,255);
SerialSend(&serial,reading);
SerialSend(&serial," , ");
}
SerialSend(&serial,'\n');
}
SerialSend(&serial,'\n');
SerialSend(&serial,'\n');
SerialSend(&serial,'\n');
}
}
void GraphingPrintCells()
{
static uint16_t lastDisplay = 0;
cli();
uint16_t ms_current = ms_count;
sei();
if ((uint16_t)(ms_current - lastDisplay) > 100)
{
lastDisplay = ms_current;
for (uint8_t y = 0; y < BMS_BOARD_COUNT; y++)
{
for (uint8_t x = 0; x < 12; x++)
{
if (!chain.boards[y].cells[x].IgnoreFlag){
cli();
uint16_t last = chain.boards[y].cells[x].LastReading;
bool dis = chain.boards[y].cells[x].DischargeFlag;
sei();
SerialSend(&serial, last>>0);
SerialSend(&serial, last>>8);
SerialSend(&serial, dis ? 1 : 0);
}
}
}
uint16_t current = GetCurrentFlow();
uint16_t temp = GetBatteryTemp();
SerialSend(&serial, current >> 0);
SerialSend(&serial, current >> 8);
SerialSend(&serial, temp >> 0);
SerialSend(&serial, temp >> 8);
SerialSend(&serial,"\n\n\n\n\n\n\n\n");
}
}
int main()
{
CHG_LED_DDR |= CHG_LED_MASK;
CHG_LED_PORT |= CHG_LED_MASK;
OK_LED_PORT |= OK_LED_MASK;
OK_LED_PORT |= OK_LED_MASK;
srand(1234);
for (uint8_t x = 0; x < BMS_BOARD_COUNT; x++)
{
for (uint8_t y = 0; y < BMS_CELL_COUNT; y++)
{
chain.boards[x].cells[y].LastReading= 0;//rand()%(HIGH_CHARGING_VOLTAGE-3500)+3500;
}
}
SerialBegin(&serial,9600, &UDR0, &UCSR0A, &UCSR0B, &UBRR0);
OK_LED_DDR |= OK_LED_MASK;
Timer_t timer;
IntializeTimer(&timer,&TCCR0A,&TCCR0B, &OCR0A, &TIMSK0, &TCNT0,0);
timer.timeMask = CS_DIV1024;
*(uint8_t*)timer.ocra = 156;
TimerStart(&timer);
TimerSetOverflowInterrupt(&timer,true);
InitializeAdc((Adc_t*)&Adc,&ADCSRA,&ADCSRB,&ADMUX,&ADC);
Adc.doConversions[0] = true;
Adc.doConversions[1] = true;
EnableAdc((Adc_t*)&Adc);
spi_t spi;
spi_device_t dev;
InitializeSpiDevice(&dev,&PORTB,&DDRB,1<<2);
InitializeSpiMaster(&spi,
&SPCR,&SPDR,&SPSR,
&DDRB,1<<5,
&DDRB,1<<3);
SetClockDividerSpi(&spi,SPI_CLOCK_DIV16);
chain.spi = &spi;
chain.device = &dev;
for (int x = 0; x < BMS_BOARD_COUNT; x++)
{
chain.boards[x].config.GPIO1 = 0; //Set pulldowns
chain.boards[x].config.GPIO2 = 0;
chain.boards[x].config.GPIO3 = 0;
chain.boards[x].config.GPIO4 = 0;
chain.boards[x].config.GPIO5 = 0;
for (int y = 0; y < BMS_CELL_COUNT; y++)
{
chain.boards[x].cells[y].DischargeFlag = false;
chain.boards[x].cells[y].IgnoreFlag = ignoreCell[y];
chain.boards[x].cells[y].OddFlag = oddCells[y];
}
}
chain.boards[0].cells[0].Error = 1; //C1
chain.boards[0].cells[1].Error = -3; //C2
chain.boards[0].cells[2].Error = -3; //C3
chain.boards[0].cells[3].Error = -3; //C4
chain.boards[0].cells[4].Error = -3; //C5
//chain.boards[0].cells[5].Error = 1; //C6
chain.boards[0].cells[6].Error = -5; //C7
chain.boards[0].cells[7].Error = -3; //C8
chain.boards[0].cells[8].Error = -3; //C9
chain.boards[0].cells[9].Error = -3; //C10
chain.boards[0].cells[10].Error = 106; //C11
//chain.boards[0].cells[11].Error = 1; //C12
chain.boards[1].cells[0].Error = 3; //C1
chain.boards[1].cells[1].Error = -4; //C2
chain.boards[1].cells[2].Error = -3; //C3
chain.boards[1].cells[3].Error = -1; //C4
chain.boards[1].cells[4].Error = -5; //C5
//chain.boards[1].cells[5].Error = 1; //C6
chain.boards[1].cells[6].Error = -6; //C7
chain.boards[1].cells[7].Error = -7; //C8
chain.boards[1].cells[8].Error = -6; //C9
chain.boards[1].cells[9].Error = -6; //C10
chain.boards[1].cells[10].Error = 199; //C11
//chain.boards[1].cells[11].Error = 1; //C12
chain.adcConversionMode = 0x260 | 0x2 << 7;
chain.gpioConversionMode = 0b10101100000;
_delay_ms(1000);
TimerSetOverflowInterrupt(&timer,true);
sei();
_delay_ms(1000);
while(true){
//HumanReadablePrintCells();
//while (true) { OK_LED_PORT ^= OK_LED_MASK; _delay_ms(1000);}
GraphingPrintCells();
if (IsCarSafe())
{
OK_LED_PORT |= OK_LED_MASK;
}
else
{
OK_LED_PORT &= ~OK_LED_MASK;
}
HandleFan();
#define USE_CELL_BALANCING
#ifdef USE_CELL_BALANCING
BalanceBmsCells();
#endif
}
//DDRC |= 0x1F;
uint8_t x = 0xaa;
/*WriteSpi(&spi,&dev,&x,1);
_delay_ms(1000);
WriteSpi(&spi,&dev,&x,1);
_delay_ms(1000);*/
//writeConfig(&spi,&dev);
//uint8_t config2[12];
//CopyDataAndPEC(config2+4,((uint8_t*)&config),6);
//ChainWriteCommand(&chain,1,(uint8_t*)config2,8);
// ChainWriteCommand(&chain,1,(uint8_t*)config2,8);
//writeConfig(&spi,&dev);
cli();
ChainFlushConfig((BmsChain_t*)&chain);
sei();
//ChainWriteCommand(&chain,1,(uint8_t*)config2,8);
//writeConfig(&spi,&dev);
while(true){
/*
_delay_ms(1000);
*dev.SS_out &= ~dev.SS_mask;
_delay_ms(5);
*dev.SS_out |= dev.SS_mask;
writeConfig(&spi,&dev);
writeConfig(&spi,&dev);
writeConfig(&spi,&dev);*/
_delay_ms(1000);
//ChainUpdateCellReadings(&chain);
chain.boards[0].config.DischargeCell = 0;
cli();
ChainFlushConfig((BmsChain_t*)&chain);
sei();
//debug(ChainReadGpioAdc(&chain,BMS_TEMP_SENSOR_REGISTER,BMS_TEMP_SENSOR_OFFSET));
/*
for (int x = 0; x < 12; x++)
{
debug(chain.boards[0].cells[x].LastReading>>8);
}*/
}
}