forked from lincomatic/open_evse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
J1772EvseController.cpp
1557 lines (1371 loc) · 42.7 KB
/
J1772EvseController.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 file is part of Open EVSE.
* Open EVSE is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
* Open EVSE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Open EVSE; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "open_evse.h"
#ifdef FT_ENDURANCE
int g_CycleCnt = -1;
long g_CycleHalfStart;
uint8_t g_CycleState;
#endif
THRESH_DATA g_DefaultThreshData = {875,780,690,0,260};
J1772EVSEController g_EvseController;
#ifdef AMMETER
static inline unsigned long ulong_sqrt(unsigned long in)
{
unsigned long out;
// find the last int whose square is not too big
// Yes, it's wasteful, but we only theoretically ever have to go to 512.
// Removing floating point saves us almost 1K of flash.
for(out = 1; out*out <= in; out++) ;
return out - 1;
}
void J1772EVSEController::readAmmeter()
{
WDT_RESET();
unsigned long sum = 0;
unsigned int zero_crossings = 0;
unsigned long last_zero_crossing_time = 0, now_ms;
long last_sample = -1; // should be impossible - the A/d is 0 to 1023.
unsigned int sample_count = 0;
for(unsigned long start = millis(); ((now_ms = millis()) - start) < CURRENT_SAMPLE_INTERVAL; ) {
long sample = (long) adcCurrent.read();
// If this isn't the first sample, and if the sign of the value differs from the
// sign of the previous value, then count that as a zero crossing.
if (last_sample != -1 && ((last_sample > 512) != (sample > 512))) {
// Once we've seen a zero crossing, don't look for one for a little bit.
// It's possible that a little noise near zero could cause a two-sample
// inversion.
if ((now_ms - last_zero_crossing_time) > CURRENT_ZERO_DEBOUNCE_INTERVAL) {
zero_crossings++;
last_zero_crossing_time = now_ms;
}
}
last_sample = sample;
switch(zero_crossings) {
case 0:
continue; // Still waiting to start sampling
case 1:
case 2:
// Gather the sum-of-the-squares and count how many samples we've collected.
sum += (unsigned long)((sample - 512) * (sample - 512));
sample_count++;
continue;
case 3:
// The answer is the square root of the mean of the squares.
// But additionally, that value must be scaled to a real current value.
// we will do that elsewhere
m_AmmeterReading = ulong_sqrt(sum / sample_count);
return;
}
}
// ran out of time. Assume that it's simply not oscillating any.
m_AmmeterReading = 0;
WDT_RESET();
}
#define MA_PTS 32 // # points in moving average MUST BE power of 2
#define MA_BITS 5 // log2(MA_PTS)
/*
uint32_t MovingAverage(uint32_t samp)
{
static uint32_t samps[MA_PTS] = {0};
uint32_t tot = samp;
samps[0] = samp;
for (int8_t c=MA_PTS-1;c > 0;c--) {
samps[c] = samps[c-1];
tot += samps[c];
}
return tot >> MA_BITS;
}
*/
// to save memory
// instead of doing a real moving average we just do a non-overlapping
// sliding window and output a value every MA_PTS
uint32_t MovingAverage(uint32_t samp)
{
static uint32_t tot = 0;
static int8_t curidx = 0;
if (curidx == 0) {
tot = 0;
}
tot += samp;
if (++curidx == MA_PTS) {
curidx = 0;
return tot >> MA_BITS; // tot / MA_PTS
}
return 0xffffffff;
}
#endif // AMMETER
J1772EVSEController::J1772EVSEController() :
adcPilot(VOLT_PIN)
#ifdef CURRENT_PIN
, adcCurrent(CURRENT_PIN)
#endif
#ifdef VOLTMETER_PIN
, adcVoltMeter(VOLTMETER_PIN)
#endif
{
}
void J1772EVSEController::SaveSettings()
{
// n.b. should we add dirty bits so we only write the changed values? or should we just write them on the fly when necessary?
// ugly code below is smaller than this: eeprom_write_byte((uint8_t *)((GetCurSvcLevel() == 1) ? EOFS_CURRENT_CAPACITY_L1 : EOFS_CURRENT_CAPACITY_L2),(byte)GetCurrentCapacity());
if (GetCurSvcLevel() == 1) {
eeprom_write_byte((uint8_t *)EOFS_CURRENT_CAPACITY_L1,(byte)GetCurrentCapacity());
}
else {
eeprom_write_byte((uint8_t *)EOFS_CURRENT_CAPACITY_L2,(byte)GetCurrentCapacity());
}
SaveEvseFlags();
}
// use watchdog to perform a reset
void J1772EVSEController::Reboot()
{
m_Pilot.SetState(PILOT_STATE_P12);
#ifdef LCD16X2
g_OBD.LcdPrint_P(1,PSTR("Resetting..."));
#endif
if (chargingIsOn()) {
// give the EV some time to open its contactor in response to P12
delay(3000);
}
// hardware reset by forcing watchdog to timeout
wdt_enable(WDTO_1S); // enable watchdog timer
delay(1500);
}
#ifdef SHOW_DISABLED_TESTS
void J1772EVSEController::ShowDisabledTests()
{
if (m_wFlags & (ECF_DIODE_CHK_DISABLED|
ECF_VENT_REQ_DISABLED|
ECF_GND_CHK_DISABLED|
ECF_STUCK_RELAY_CHK_DISABLED|
ECF_GFI_TEST_DISABLED|
ECF_TEMP_CHK_DISABLED)) {
g_OBD.LcdSetBacklightColor(YELLOW);
if (!DiodeCheckEnabled()) {
g_OBD.LcdMsg_P(g_psDisabledTests,g_psDiodeCheck);
delay(SHOW_DISABLED_DELAY);
}
if (!VentReqEnabled()) {
g_OBD.LcdMsg_P(g_psDisabledTests,g_psVentReqChk);
delay(SHOW_DISABLED_DELAY);
}
#ifdef ADVPWR
if (!GndChkEnabled()) {
g_OBD.LcdMsg_P(g_psDisabledTests,g_psGndChk);
delay(SHOW_DISABLED_DELAY);
}
if (!StuckRelayChkEnabled()) {
g_OBD.LcdMsg_P(g_psDisabledTests,g_psRlyChk);
delay(SHOW_DISABLED_DELAY);
}
#endif // ADVPWR
#ifdef GFI_SELFTEST
if (!GfiSelfTestEnabled()) {
g_OBD.LcdMsg_P(g_psDisabledTests,g_psGfiTest);
delay(SHOW_DISABLED_DELAY);
}
#endif // GFI_SELFTEST
#ifdef TEMPERATURE_MONITORING
if (!TempChkEnabled()) {
g_OBD.LcdMsg_P(g_psDisabledTests,g_psTempChk);
delay(SHOW_DISABLED_DELAY);
}
#endif // TEMPERATURE_MONITORING
g_OBD.LcdSetBacklightColor(WHITE);
}
}
#endif //SHOW_DISABLED_TESTS
void J1772EVSEController::chargingOn()
{ // turn on charging current
pinCharging.write(1);
#ifdef CHARGING2_REG
pinCharging2.write(1);
#endif
#ifdef CHARGINGAC_REG
pinChargingAC.write(1);
#endif
m_bVFlags |= ECVF_CHARGING_ON;
m_ChargeOnTime = now();
m_ChargeOnTimeMS = millis();
}
void J1772EVSEController::chargingOff()
{ // turn off charging current
pinCharging.write(0);
#ifdef CHARGING2_REG
pinCharging2.write(0);
#endif
#ifdef CHARGINGAC_REG
pinChargingAC.write(0);
#endif
m_bVFlags &= ~ECVF_CHARGING_ON;
m_ChargeOffTime = now();
m_ChargeOffTimeMS = millis();
#ifdef AMMETER
m_ChargingCurrent = 0;
#endif
}
void J1772EVSEController::HardFault()
{
SetHardFault();
g_OBD.Update(OBD_UPD_HARDFAULT);
while (1) {
ProcessInputs(); // spin forever or until user resets via menu
// if we're in P12 state, we can recover from the hard fault when EV
// is unplugged
if (m_Pilot.GetState() == PILOT_STATE_P12) {
int plow,phigh;
ReadPilot(&plow,&phigh);
if (phigh >= m_ThreshData.m_ThreshAB) {
// EV disconnected - cancel fault
m_EvseState = EVSE_STATE_UNKNOWN;
break;
}
}
}
ClrHardFault();
}
#ifdef GFI
void J1772EVSEController::SetGfiTripped()
{
#ifdef GFI_SELFTEST
if (m_Gfi.SelfTestInProgress()) {
m_Gfi.SetTestSuccess();
return;
}
#endif
m_bVFlags |= ECVF_GFI_TRIPPED;
// this is repeated in Update(), but we want to keep latency as low as possible
// for safety so we do it here first anyway
chargingOff(); // turn off charging current
// turn off the PWM
m_Pilot.SetState(PILOT_STATE_P12);
m_Gfi.SetFault();
// the rest of the logic will be handled in Update()
}
#endif // GFI
void J1772EVSEController::EnableDiodeCheck(uint8_t tf)
{
if (tf) {
m_wFlags &= ~ECF_DIODE_CHK_DISABLED;
}
else {
m_wFlags |= ECF_DIODE_CHK_DISABLED;
}
SaveEvseFlags();
}
#ifdef GFI_SELFTEST
void J1772EVSEController::EnableGfiSelfTest(uint8_t tf)
{
if (tf) {
m_wFlags &= ~ECF_GFI_TEST_DISABLED;
}
else {
m_wFlags |= ECF_GFI_TEST_DISABLED;
}
SaveEvseFlags();
}
#endif
#ifdef TEMPERATURE_MONITORING
void J1772EVSEController::EnableTempChk(uint8_t tf)
{
if (tf) {
m_wFlags &= ~ECF_TEMP_CHK_DISABLED;
}
else {
m_wFlags |= ECF_TEMP_CHK_DISABLED;
}
SaveEvseFlags();
}
#endif TEMPERATURE_MONITORING
void J1772EVSEController::EnableVentReq(uint8_t tf)
{
if (tf) {
m_wFlags &= ~ECF_VENT_REQ_DISABLED;
}
else {
m_wFlags |= ECF_VENT_REQ_DISABLED;
}
SaveEvseFlags();
}
#ifdef ADVPWR
void J1772EVSEController::EnableGndChk(uint8_t tf)
{
if (tf) {
m_wFlags &= ~ECF_GND_CHK_DISABLED;
}
else {
m_NoGndRetryCnt = 0;
m_NoGndStart = 0;
m_wFlags |= ECF_GND_CHK_DISABLED;
}
SaveEvseFlags();
}
void J1772EVSEController::EnableStuckRelayChk(uint8_t tf)
{
if (tf) {
m_wFlags &= ~ECF_STUCK_RELAY_CHK_DISABLED;
}
else {
m_wFlags |= ECF_STUCK_RELAY_CHK_DISABLED;
}
SaveEvseFlags();
}
void J1772EVSEController::EnableAutoSvcLevel(uint8_t tf)
{
if (tf) {
m_wFlags &= ~ECF_AUTO_SVC_LEVEL_DISABLED;
}
else {
m_wFlags |= ECF_AUTO_SVC_LEVEL_DISABLED;
}
SaveEvseFlags();
}
#endif // ADVPWR
void J1772EVSEController::EnableSerDbg(uint8_t tf)
{
if (tf) {
m_wFlags |= ECF_SERIAL_DBG;
}
else {
m_wFlags &= ~ECF_SERIAL_DBG;
}
SaveEvseFlags();
}
#ifdef RGBLCD
int J1772EVSEController::SetBacklightType(uint8_t t,uint8_t update)
{
#ifdef RGBLCD
g_OBD.LcdSetBacklightType(t,update);
if (t == BKL_TYPE_MONO) m_wFlags |= ECF_MONO_LCD;
else m_wFlags &= ~ECF_MONO_LCD;
SaveEvseFlags();
#endif // RGBLCD
return 0;
}
#endif // RGBLCD
void J1772EVSEController::Enable()
{
if ((m_EvseState == EVSE_STATE_DISABLED)||
(m_EvseState == EVSE_STATE_SLEEPING)) {
#ifdef SLEEP_STATUS_REG
if (m_EvseState == EVSE_STATE_SLEEPING) {
pinSleepStatus.write(0);
}
#endif // SLEEP_STATUS_REG
#if defined(TIME_LIMIT) || defined(CHARGE_LIMIT)
SetLimitSleep(0);
#endif //defined(TIME_LIMIT) || defined(CHARGE_LIMIT)
m_PrevEvseState = EVSE_STATE_DISABLED;
m_EvseState = EVSE_STATE_UNKNOWN;
m_Pilot.SetState(PILOT_STATE_P12);
}
}
void J1772EVSEController::Disable()
{
if (m_EvseState != EVSE_STATE_DISABLED) {
m_Pilot.SetState(PILOT_STATE_N12);
m_EvseState = EVSE_STATE_DISABLED;
// panic stop so we won't wait for EV to open its contacts first
chargingOff();
g_OBD.Update(OBD_UPD_FORCE);
#ifdef RAPI
g_ERP.sendEvseState();
#endif // RAPI
}
}
void J1772EVSEController::Sleep()
{
if (m_EvseState != EVSE_STATE_SLEEPING) {
m_Pilot.SetState(PILOT_STATE_P12);
m_EvseState = EVSE_STATE_SLEEPING;
#ifdef SLEEP_STATUS_REG
pinSleepStatus.write(1);
#endif // SLEEP_STATUS_REG
g_OBD.Update(OBD_UPD_FORCE);
#ifdef RAPI
g_ERP.sendEvseState();
#endif // RAPI
// try to prevent arcing of our relay by waiting for EV to open its contacts first
// use the charge end time variable temporarily to count down
// when to open the contacts in Update()
m_ChargeOffTimeMS = millis();
}
}
void J1772EVSEController::LoadThresholds()
{
memcpy(&m_ThreshData,&g_DefaultThreshData,sizeof(m_ThreshData));
}
void J1772EVSEController::SetSvcLevel(uint8_t svclvl,uint8_t updatelcd)
{
#ifdef SERIALCLI
if (SerDbgEnabled()) {
g_CLI.printlnn();
g_CLI.print_P(PSTR("SetSvcLevel: "));Serial.println((int)svclvl);
}
#endif //#ifdef SERIALCLI
if (svclvl == 2) {
m_wFlags |= ECF_L2; // set to Level 2
}
else {
svclvl = 1;
m_wFlags &= ~ECF_L2; // set to Level 1
}
SaveEvseFlags();
uint8_t ampacity = eeprom_read_byte((uint8_t*)((svclvl == 1) ? EOFS_CURRENT_CAPACITY_L1 : EOFS_CURRENT_CAPACITY_L2));
if ((ampacity == 0xff) || (ampacity == 0)) {
ampacity = (svclvl == 1) ? DEFAULT_CURRENT_CAPACITY_L1 : DEFAULT_CURRENT_CAPACITY_L2;
}
if (ampacity < MIN_CURRENT_CAPACITY_L1) {
ampacity = MIN_CURRENT_CAPACITY_L1;
}
else {
if (svclvl == 1) { // L1
if (ampacity > MAX_CURRENT_CAPACITY_L1) {
ampacity = MAX_CURRENT_CAPACITY_L1;
}
}
else {
if (ampacity > MAX_CURRENT_CAPACITY_L2) {
ampacity = MAX_CURRENT_CAPACITY_L2;
}
}
}
LoadThresholds();
SetCurrentCapacity(ampacity);
if (updatelcd) {
g_OBD.Update(OBD_UPD_FORCE);
}
}
#ifdef ADVPWR
// acpinstate : bit 1 = AC pin 1, bit0 = AC pin 2
uint8_t J1772EVSEController::ReadACPins()
{
#ifndef OPENEVSE_2
#ifdef SAMPLE_ACPINS
//
// AC pins are active low, so we set them high
// and then if voltage is detected on a pin, it will go low
//
uint8_t ac1 = 2;
uint8_t ac2 = 1;
unsigned long startms = millis();
do {
if (ac1 && !pinAC1.read()) {
ac1 = 0;
}
if (ac2 && !pinAC2.read()) {
ac2 = 0;
}
} while ((ac1 || ac2) && ((millis() - startms) < AC_SAMPLE_MS));
return ac1 | ac2;
#else // !SAMPLE_ACPINS
return (pinAC1.read() ? 2 : 0) | (pinAC2.read() ? 1 : 0);
#endif // SAMPLE_ACPINS
#else
// For OpenEVSE II, there is only ACLINE1_PIN, and it is
// active *high*. '3' is the value for "both AC lines dead"
// and '0' is the value for "both AC lines live". There is
// no need to sample, as the hardware does a peak-hold.
return (pinAC1.read() ? 0 : 3);
#endif // OPENEVSE_2
}
uint8_t J1772EVSEController::doPost()
{
WDT_RESET();
uint8_t RelayOff, Relay1, Relay2; //Relay Power status
uint8_t svcState = UD; // service state = undefined
#ifdef SERIALCLI
if (SerDbgEnabled()) {
g_CLI.print_P(PSTR("POST start..."));
}
#endif //#ifdef SERIALCLI
m_Pilot.SetState(PILOT_STATE_P12); //check to see if EV is plugged in
g_OBD.SetRedLed(1);
#ifdef LCD16X2 //Adafruit RGB LCD
g_OBD.LcdMsg_P(g_psPwrOn,g_psSelfTest);
#endif //Adafruit RGB LCD
if (AutoSvcLevelEnabled()) {
#ifdef OPENEVSE_2
// For OpenEVSE II, there is a voltmeter for auto L1/L2.
uint32_t long ac_volts = ReadVoltmeter();
if (ac_volts > L2_VOLTAGE_THRESHOLD) {
svcState = L2;
} else {
svcState = L1;
}
#ifdef SERIALCLI
if (SerDbgEnabled()) {
g_CLI.print_P(PSTR("AC millivolts: "));Serial.println(ac_volts);
g_CLI.print_P(PSTR("SvcState: "));Serial.println((int)svcState);
}
#endif //#ifdef SERIALCLI
#ifdef LCD16X2
g_OBD.LcdMsg_P(g_psAutoDetect,(svcState == L2) ? g_psLevel2 : g_psLevel1);
#endif //LCD16x2
#else //!OPENEVSE_2
delay(150); // delay reading for stable pilot before reading
int reading = adcPilot.read(); //read pilot
#ifdef SERIALCLI
if (SerDbgEnabled()) {
g_CLI.printlnn();
g_CLI.print_P(PSTR("Pilot: "));Serial.println((int)reading);
}
#endif //#ifdef SERIALCLI
m_Pilot.SetState(PILOT_STATE_N12);
if (reading > 900) { // IF EV is not connected its Okay to open the relay the do the L1/L2 and ground Check
// save state with both relays off - for stuck relay state
RelayOff = ReadACPins();
// save state with Relay 1 on
pinCharging.write(1);
#ifdef CHARGINGAC_REG
pinChargingAC.write(1);
#endif
delay(RelaySettlingTime);
Relay1 = ReadACPins();
pinCharging.write(0);
#ifdef CHARGINGAC_REG
pinChargingAC.write(0);
#endif
delay(RelaySettlingTime); //allow relay to fully open before running other tests
// save state for Relay 2 on
#ifdef CHARGING2_REG
pinCharging2.write(1);
#endif
delay(RelaySettlingTime);
Relay2 = ReadACPins();
#ifdef CHARGING2_REG
pinCharging2.write(0);
#endif
delay(RelaySettlingTime); //allow relay to fully open before running other tests
// decide input power state based on the status read on L1 and L2
// either 2 SPST or 1 DPST relays can be configured
// valid svcState is L1 - one hot, L2 both hot, OG - open ground both off, SR - stuck relay when shld be off
//
if (RelayOff == none) { // relay not stuck on when off
switch ( Relay1 ) {
case ( both ): //
if ( Relay2 == none ) svcState = L2;
if (StuckRelayChkEnabled()) {
if ( Relay2 != none ) svcState = SR;
}
break;
case ( none ): //
if (GndChkEnabled()) {
if ( Relay2 == none ) svcState = OG;
}
if ( Relay2 == both ) svcState = L2;
if ( Relay2 == L1 || Relay2 == L2 ) svcState = L1;
break;
case ( L1on ): // L1 or L2
case ( L2on ):
if (StuckRelayChkEnabled()) {
if ( Relay2 != none ) svcState = SR;
}
if ( Relay2 == none ) svcState = L1;
if ( (Relay1 == L1on) && (Relay2 == L2on)) svcState = L2;
if ( (Relay1 == L2on) && (Relay2 == L1on)) svcState = L2;
break;
} // end switch
}
else { // Relay stuck on
if (StuckRelayChkEnabled()) {
svcState = SR;
}
}
#ifdef SERIALCLI
if (SerDbgEnabled()) {
g_CLI.print_P(PSTR("RelayOff: "));Serial.println((int)RelayOff);
g_CLI.print_P(PSTR("Relay1: "));Serial.println((int)Relay1);
g_CLI.print_P(PSTR("Relay2: "));Serial.println((int)Relay2);
g_CLI.print_P(PSTR("SvcState: "));Serial.println((int)svcState);
}
#endif //#ifdef SERIALCLI
// update LCD
#ifdef LCD16X2
if (svcState == L1) g_OBD.LcdMsg_P(g_psAutoDetect,g_psLevel1);
if (svcState == L2) g_OBD.LcdMsg_P(g_psAutoDetect,g_psLevel2);
if ((svcState == OG) || (svcState == SR)) {
g_OBD.LcdSetBacklightColor(RED);
}
if (svcState == OG) g_OBD.LcdMsg_P(g_psTestFailed,g_psNoGround);
if (svcState == SR) g_OBD.LcdMsg_P(g_psTestFailed,g_psStuckRelay);
#endif // LCD16X2
} // endif test, no EV is plugged in
else {
// since we can't auto detect, for safety's sake, we must set to L1
svcState = L1;
SetAutoSvcLvlSkipped(1);
// EV connected.. do stuck relay check
goto stuckrelaychk;
}
#endif //#else OPENEVSE_2
}
else { // ! AutoSvcLevelEnabled
stuckrelaychk:
if (StuckRelayChkEnabled()) {
RelayOff = ReadACPins();
if ((RelayOff & 3) != 3) {
svcState = SR;
#ifdef LCD16X2
g_OBD.LcdMsg_P(g_psTestFailed,g_psStuckRelay);
#endif // LCD16X2
}
}
} // endif AutoSvcLevelEnabled
#ifdef GFI_SELFTEST
// only run GFI test if no fault detected above
if (((svcState == UD)||(svcState == L1)||(svcState == L2)) &&
GfiSelfTestEnabled()) {
if (m_Gfi.SelfTest()) {
#ifdef LCD16X2
g_OBD.LcdMsg_P(g_psTestFailed,g_psGfci);
#endif // LCD16X2
svcState = FG;
}
}
#endif
if ((svcState == OG)||(svcState == SR)||(svcState == FG)) {
g_OBD.LcdSetBacklightColor(RED);
g_OBD.SetGreenLed(0);
g_OBD.SetRedLed(1);
}
else {
g_OBD.SetRedLed(0);
}
m_Pilot.SetState(PILOT_STATE_P12);
#ifdef SERIALCLI
if (SerDbgEnabled()) {
g_CLI.print_P(PSTR("POST result: "));
Serial.println((int)svcState);
}
#endif //#ifdef SERIALCLI
WDT_RESET();
return svcState;
}
#endif // ADVPWR
void J1772EVSEController::Init()
{
// read settings from EEPROM
uint16_t rflgs = eeprom_read_word((uint16_t*)EOFS_FLAGS);
#ifdef RGBLCD
if ((rflgs != 0xffff) && (rflgs & ECF_MONO_LCD)) {
g_OBD.LcdSetBacklightType(BKL_TYPE_MONO);
}
#endif // RGBLCD
pinCharging.init(CHARGING_REG,CHARGING_IDX,DigitalPin::OUT);
#ifdef CHARGING2_REG
pinCharging2.init(CHARGING2_REG,CHARGING2_IDX,DigitalPin::OUT);
#endif
#ifdef CHARGINGAC_REG
pinChargingAC.init(CHARGINGAC_REG,CHARGINGAC_IDX,DigitalPin::OUT);
#endif
#ifdef ACLINE1_REG
pinAC1.init(ACLINE1_REG,ACLINE1_IDX,DigitalPin::INP_PU);
#endif
#ifdef ACLINE2_REG
pinAC2.init(ACLINE2_REG,ACLINE2_IDX,DigitalPin::INP_PU);
#endif
#ifdef SLEEP_STATUS_REG
pinSleepStatus.init(SLEEP_STATUS_REG,SLEEP_STATUS_IDX,DigitalPin::OUT);
#endif
#ifdef GFI
m_Gfi.Init();
#endif // GFI
chargingOff();
m_Pilot.Init(); // init the pilot
uint8_t svclvl = (uint8_t)DEFAULT_SERVICE_LEVEL;
if (rflgs == 0xffff) { // uninitialized EEPROM
m_wFlags = ECF_DEFAULT;
#ifdef RGBLCD
if (DEFAULT_LCD_BKL_TYPE == BKL_TYPE_MONO) {
m_wFlags |= ECF_MONO_LCD;
}
#endif // RGBLCD
}
else {
m_wFlags = rflgs;
svclvl = GetCurSvcLevel();
}
#ifdef NOCHECKS
m_wFlags |= ECF_DIODE_CHK_DISABLED|ECF_VENT_REQ_DISABLED|ECF_GND_CHK_DISABLED|ECF_STUCK_RELAY_CHK_DISABLED|ECF_GFI_TEST_DISABLED|ECF_TEMP_CHK_DISABLED;
#endif
#ifdef AMMETER
m_AmmeterCurrentOffset = eeprom_read_word((uint16_t*)EOFS_AMMETER_CURR_OFFSET);
m_CurrentScaleFactor = eeprom_read_word((uint16_t*)EOFS_CURRENT_SCALE_FACTOR);
if (m_AmmeterCurrentOffset == 0x0000ffff) {
m_AmmeterCurrentOffset = DEFAULT_AMMETER_CURRENT_OFFSET;
}
if (m_CurrentScaleFactor == 0x0000ffff) {
m_CurrentScaleFactor = DEFAULT_CURRENT_SCALE_FACTOR;
}
m_AmmeterReading = 0;
m_ChargingCurrent = 0;
// m_LastAmmeterReadMs = 0;
#endif // AMMETER
#ifdef VOLTMETER
m_VoltOffset = eeprom_read_dword((uint32_t*)EOFS_VOLT_OFFSET);
m_VoltScaleFactor = eeprom_read_word((uint16_t*)EOFS_VOLT_SCALE_FACTOR);
if (m_VoltOffset == 0xffffffff) {
m_VoltOffset = DEFAULT_VOLT_OFFSET;
}
if (m_VoltScaleFactor == 0xffff) {
m_VoltScaleFactor = DEFAULT_VOLT_SCALE_FACTOR;
}
#endif // VOLTMETER
#ifndef RGBLCD
m_wFlags |= ECF_MONO_LCD;
#endif
m_bVFlags = ECVF_DEFAULT;
#ifdef GFI
m_GfiRetryCnt = 0;
m_GfiTripCnt = eeprom_read_byte((uint8_t*)EOFS_GFI_TRIP_CNT);
#endif // GFI
#ifdef ADVPWR
m_NoGndRetryCnt = 0;
m_NoGndTripCnt = eeprom_read_byte((uint8_t*)EOFS_NOGND_TRIP_CNT);
m_StuckRelayStartTimeMS = 0;
m_StuckRelayTripCnt = eeprom_read_byte((uint8_t*)EOFS_STUCK_RELAY_TRIP_CNT);
m_NoGndRetryCnt = 0;
m_NoGndStart = 0;
#endif // ADVPWR
m_EvseState = EVSE_STATE_UNKNOWN;
m_PrevEvseState = EVSE_STATE_UNKNOWN;
#ifdef ADVPWR
#ifdef FT_READ_AC_PINS
while (1) {
WDT_RESET();
sprintf(g_sTmp,"%d",(int)ReadACPins());
g_OBD.LcdMsg("AC Pins",g_sTmp);
}
#endif // FT_READ_AC_PINS
#ifdef SHOW_DISABLED_TESTS
ShowDisabledTests();
#endif
uint8_t fault;
do {
fault = 0; // reset post fault
uint8_t psvclvl = doPost(); // auto detect service level overrides any saved values
if ((AutoSvcLevelEnabled()) && ((psvclvl == L1) || (psvclvl == L2))) svclvl = psvclvl; //set service level
if ((GndChkEnabled()) && (psvclvl == OG)) { m_EvseState = EVSE_STATE_NO_GROUND; fault = 1;} // set No Ground error
if ((StuckRelayChkEnabled()) && (psvclvl == SR)) { m_EvseState = EVSE_STATE_STUCK_RELAY; fault = 1; } // set Stuck Relay error
#ifdef GFI_SELFTEST
if ((GfiSelfTestEnabled()) && (psvclvl == FG)) { m_EvseState = EVSE_STATE_GFI_TEST_FAILED; fault = 1; } // set GFI test fail error
#endif
if (fault) {
#ifdef UL_COMPLIANT
// UL wants EVSE to hard fault until power cycle if POST fails
while (1) { // spin forever
ProcessInputs();
}
#else // !UL_COMPLIANT
unsigned long faultms = millis();
// keep retrying POST every 2 minutes
while ((millis() - faultms) < 2*60000ul) {
ProcessInputs();
}
#endif
}
} while ( fault && ( m_EvseState == EVSE_STATE_GFI_TEST_FAILED || m_EvseState == EVSE_STATE_NO_GROUND || m_EvseState == EVSE_STATE_STUCK_RELAY ));
#endif // ADVPWR
SetSvcLevel(svclvl);
#ifdef DELAYTIMER
if (g_DelayTimer.IsTimerEnabled()) {
Sleep();
}
#endif
g_OBD.SetGreenLed(0);
}
void J1772EVSEController::ReadPilot(int *plow,int *phigh,int loopcnt)
{
int pl = 1023;
int ph = 0;
// 1x = 114us 20x = 2.3ms 100x = 11.3ms
for (int i=0;i < 100;i++) {
int reading = adcPilot.read(); // measures pilot voltage
if (reading > ph) {
ph = reading;
}
else if (reading < pl) {
pl = reading;
}
}
*plow = pl;
*phigh = ph;
}
//TABLE A1 - PILOT LINE VOLTAGE RANGES (recommended.. adjust as necessary
// Minimum Nominal Maximum
//Positive Voltage, State A 11.40 12.00 12.60
//Positive Voltage, State B 8.36 9.00 9.56
//Positive Voltage, State C 5.48 6.00 6.49
//Positive Voltage, State D 2.62 3.00 3.25
//Negative Voltage - States B, C, D, and F -11.40 -12.00 -12.60
void J1772EVSEController::Update()
{
int plow;
int phigh = -127;
unsigned long curms = millis();
if (m_EvseState == EVSE_STATE_DISABLED) {
m_PrevEvseState = m_EvseState; // cancel state transition
return;
}
else if (m_EvseState == EVSE_STATE_SLEEPING) {
int8_t cancelTransition = 1;
if (chargingIsOn()) {
ReadPilot(&plow,&phigh);
// wait for pilot voltage to go > STATE C. This will happen if
// a) EV reacts and goes back to state B (opens its contacts)
// b) user pulls out the charge connector
// if it doesn't happen within 3 sec, we'll just open our relay anyway
if ((phigh >= m_ThreshData.m_ThreshBC)
|| ((curms - m_ChargeOffTimeMS) >= 3000)) {
chargingOff();
#ifdef FT_SLEEP_DELAY
sprintf(g_sTmp,"SLEEP OPEN %d",(int)phigh);
g_OBD.LcdMsg(g_sTmp,(phigh >= m_ThreshData.m_ThreshBC) ? "THRESH" : "TIMEOUT");
delay(2000);
#endif
}
}
#if defined(TIME_LIMIT) || defined(CHARGE_LIMIT)
else if (LimitSleepIsSet()) {
ReadPilot(&plow,&phigh);
if (phigh >= m_ThreshData.m_ThreshAB) {
// if we went into sleep due to time/charge limit met, then
// automatically cancel the sleep when the car is unplugged
cancelTransition = 0;
SetLimitSleep(0);
}
}
#endif //defined(TIME_LIMIT) || defined(CHARGE_LIMIT)
if (cancelTransition) {
m_PrevEvseState = m_EvseState; // cancel state transition
return;
}
}
uint8_t prevevsestate = m_EvseState;
uint8_t tmpevsestate = EVSE_STATE_UNKNOWN;
uint8_t nofault = 1;
#ifdef ADVPWR
uint8_t acpinstate = ReadACPins();
if (chargingIsOn()) { // relay closed
if ((curms - m_ChargeOnTimeMS) > GROUND_CHK_DELAY) {
// ground check - can only test when relay closed
if (GndChkEnabled() && ((acpinstate & 3) == 3)) {
// bad ground
tmpevsestate = EVSE_STATE_NO_GROUND;
m_EvseState = EVSE_STATE_NO_GROUND;