Module_InternalComm.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <arpa/inet.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <string.h>
  23. #include <time.h>
  24. #include <ctype.h>
  25. #include <ifaddrs.h>
  26. #include <math.h>
  27. #include "../../define.h"
  28. #include "internalComm.h"
  29. #include <stdbool.h>
  30. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  31. #define PASS 1
  32. #define FAIL -1
  33. #define YES 1
  34. #define NO 0
  35. #define STOP 0
  36. #define START 1
  37. #define NORMAL 0
  38. #define ABNORMAL 1
  39. #define RELAY_CHECK_TIME 5 // s
  40. #define OUTPUT_VOL_CHK_TIME 200 // ms
  41. #define TEN_MINUTES 600 // s
  42. //#define TEN_MINUTES 10 // s
  43. #define FORCE_STOP_TIME 30
  44. //#define TEN_MINUTES 5
  45. #define ENV_TEMP_MIN 45
  46. #define ENV_TEMP_MAX 50
  47. #define DEFAULT_AC_INDEX 2
  48. #define EQUAL 0
  49. #define COLOR_MAX_LV 100
  50. #define COLOR_MIN_LV 0
  51. #define MIN_OUTPUT_CUR 0
  52. #define AC_OUTPUT_VOL 220
  53. #define NO_DEFINE 255
  54. #define NDEFAULT_AC_INDEX 2
  55. #define OVP_UVP_CHK_COUNT 3
  56. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  57. struct StatusCodeData *ShmStatusCodeData;
  58. struct FanModuleData *ShmFanModuleData;
  59. struct RelayModuleData *ShmRelayModuleData;
  60. struct LedModuleData *ShmLedModuleData;
  61. struct CcsData *ShmCcsData;
  62. struct PsuData *ShmPsuData;
  63. struct OCPP16Data *ShmOCPP16Data;
  64. struct OCPP20Data *ShmOCPP20Data;
  65. struct PrimaryMcuData *ShmPrimaryMcuData;
  66. struct DcCommonInformation *ShmDcCommonData;
  67. struct SmartBoxData *ShmSmartBoxData;
  68. #define VIN_MAX_VOLTAGE_IEC 285 // 大於該值 : OVP
  69. #define VIN_MAX_REV_VOLTAGE_IEC 275 // 小於賦歸 OVP
  70. #define VIN_MIN_VOLTAGE_IEC 160 // 小於該值 : UVP
  71. #define VIN_MIN_REV_VOLTAGE_IEC 170 // 大於賦歸 UVP
  72. #define VIN_MAX_VOLTAGE_UL 315 // 大於該值 : OVP // 美規 (W)
  73. #define VIN_MAX_REV_VOLTAGE_UL 305 // 小於賦歸 OVP
  74. #define VIN_MIN_VOLTAGE_UL 210 // 小於該值 : UVP
  75. #define VIN_MIN_REV_VOLTAGE_UL 220 // 大於賦歸 UVP
  76. //LWADD
  77. #define VIN_MAX_VOLTAGE_JARI 285 // 大於該值 : OVP // 日規 (W)
  78. #define VIN_MAX_REV_VOLTAGE_JARI 275 // 小於賦歸 OVP
  79. #define VIN_MIN_VOLTAGE_JARI 180 // 小於該值 : UVP
  80. #define VIN_MIN_REV_VOLTAGE_JARI 190 // 大於賦歸 UVP
  81. // 50KW 系統
  82. #define VIN_MAX_VOLTAGE_JARI_50KW 250 // 大於該值 : OVP // 日規 (W)
  83. #define VIN_MAX_REV_VOLTAGE_JARI_50KW 240 // 小於賦歸 OVP
  84. #define VIN_MIN_VOLTAGE_JARI_50KW 165 // 小於該值 : UVP
  85. #define VIN_MIN_REV_VOLTAGE_JARI_50KW 175 // 大於賦歸 UVP
  86. #define VIN_DROP_VOLTAGE 150 // 小於該值 : ac drop
  87. #define VOUT_MAX_VOLTAGE 995
  88. #define VOUT_MIN_VOLTAGE 150
  89. #define IOUT_MAX_CURRENT 50
  90. #define MAX_FAN_SPEED 14000
  91. #define MIN_FAN_SPEED 3000
  92. #define NORMAL_FAN_SPEED 7000
  93. // GFD Status
  94. #define GFD_IDLE 0
  95. #define GFD_CABLECHK 1
  96. #define GFD_PRECHARGE 2
  97. #define GFD_CHARGING 3
  98. // LED Intensity (rate)
  99. #define LED_INTENSITY_DARKEST 0.2
  100. #define LED_INTENSITY_MEDIUM 0.6
  101. #define LED_INTENSITY_BRIGHTEST 1
  102. // EE Spec
  103. #define LED_BRIGHTNESS_LV_HIGH 1
  104. #define LED_BRIGHTNESS_LV_MID 0.5
  105. #define LED_BRIGHTNESS_LV_LOW 0.2
  106. // 最小切換 Relay 電壓
  107. #define SELF_TO_CHANGE_RELAY_STATUS 600
  108. // 透過電壓確認 Relay 是否搭上的依據電壓
  109. #define CHECK_RELAY_STATUS 300
  110. #define CHECK_RELAY_STATUS_GAP 100
  111. // 安全在停止充電程序中斷開 Relay 的電流
  112. #define SEFETY_SWITCH_RELAY_CUR 50
  113. // 確認 Relay Welding 電壓
  114. #define RELAY_WELDING_DET 300
  115. // CCS minimum precharge voltage
  116. #define CCS_PRECHARGE_VOL 2000
  117. #define CCS_PRECHARGE_STARTU 40
  118. unsigned int _acErrRecord;
  119. byte gunCount;
  120. byte acgunCount;
  121. // 槍資訊
  122. struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  123. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  124. unsigned short AcChargingRatingCurrent;
  125. float _ChargingProfilePwBuffer[AC_QUANTITY];
  126. byte ac_startTransationFlag;
  127. Relay acOutputRelay;
  128. bool _isOutputNoneMatch[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  129. struct timespec _checkOutputNoneMatchTimer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  130. //bool _isRelayWelding[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  131. //struct timeval _checkRelayWeldingTimer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  132. bool _isOvpChkTimeFlag[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  133. struct timespec _checkOutputVolProtectTimer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  134. // SMR1 *2 + SMR2 * 2 + Parallel * 2
  135. struct timespec _relayStateChkTimer[6];
  136. byte _threePhaseOvp[3] = {0, 0, 0};
  137. byte _threePhaseUvp[3] = {0, 0, 0};
  138. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData);
  139. int Uart5Fd;
  140. char *relayRs485PortName = "/dev/ttyS5";
  141. unsigned short fanSpeedSmoothValue = 500;
  142. bool isStopChargingCount = false;
  143. struct timespec _close_ac_contactor;
  144. struct timespec _priority_time;
  145. struct timespec _led_priority_time;
  146. struct timespec _led_blink_time;
  147. struct timespec _ac_charging_comp;
  148. struct timespec _ac_preparing;
  149. struct timeb _ac_startChargingTime;
  150. struct timeb _ac_endChargingTime;
  151. byte _ac_duty;
  152. bool isCriticalStop;
  153. struct timespec _psuCriticalStop;
  154. bool isSelftestComp = false;
  155. unsigned short _setFanSpeed = 0;
  156. float _beforeChargingTotalEnergy = 0.0;
  157. byte _checkLedChanged = 3;
  158. byte relayMatchFlag = 0;
  159. // 50KW系統
  160. bool is50kwSystem;
  161. Ver ver;
  162. PresentInputVoltage inputVoltage;
  163. PresentOutputVoltage outputVoltage;
  164. FanSpeed fanSpeed;
  165. Temperature temperature;
  166. AuxPower auxPower;
  167. Gfd gfd_adc;
  168. Gfd_config gfd_config;
  169. Gpio_in gpio_in;
  170. Gpio_out gpio_out;
  171. Relay outputRelay;
  172. Relay regRelay;
  173. Rtc rtc;
  174. Led_Color cur_led_color;
  175. Led_Color led_color;
  176. Ac_Status acStatus;
  177. Ac_Led_Status ledStatus;
  178. Ac_Alarm_code acAlarmCode;
  179. Ac_Charging_energy acChargingEnergy;
  180. Ac_Charging_current acChargingCurrent;
  181. PresentInputVoltage acChargingVoltage;
  182. bool isAcModuleInitComp;
  183. int mask_table[] = {
  184. 0x00000001, 0x00000002, 0x00000004, 0x00000008,
  185. 0x00000010, 0x00000020, 0x00000040, 0x00000080,
  186. 0x00000100, 0x00000200, 0x00000400, 0x00000800,
  187. 0x00001000, 0x00002000, 0x00004000, 0x00008000,
  188. 0x00010000, 0x00020000, 0x00040000, 0x00080000,
  189. 0x00100000, 0x00200000, 0x00400000, 0x00800000,
  190. 0x01000000, 0x02000000, 0x04000000, 0x08000000,
  191. 0x10000000, 0x20000000, 0x40000000, 0x80000000
  192. };
  193. #define L1_AC_OVP 0
  194. #define L1_AC_UVP 1
  195. #define L1_AC_OCP 2
  196. #define AC_OTP 3
  197. #define AC_GMI_FAULT 4
  198. #define AC_CP_ERROR 5
  199. #define AC_AC_LEAKAGE 6
  200. #define AC_DC_LEAKAGE 7
  201. #define AC_SYSTEM_SELFTEST_FAULT 8
  202. #define AC_HANDSHAKE_TIMEOUT 9
  203. #define AC_EMC_STOP 10
  204. #define AC_RELAY_WELDING 11
  205. #define AC_LEAK_MODULE_FAULT 12
  206. #define AC_SHUTTER_FAULT 13
  207. #define AC_LOCKER_FAULT 14
  208. #define AC_POWER_DROP 15
  209. #define L1_AC_CIRCUIT_SHORT 16
  210. #define AC_ROTARY_SWITCH_FAULT 17
  211. #define AC_RELAY_DRIVE_FAULT 18
  212. #define RESERVED_1 19
  213. #define L2_AC_OVP 20
  214. #define L3_AC_OVP 21
  215. #define L2_AC_UVP 22
  216. #define L3_AC_UVP 23
  217. #define L2_AC_OCP 24
  218. #define L3_AC_OCP 25
  219. #define L2_AC_CIRCUIT_SHORT 26
  220. #define L3_AC_CIRCUIT_SHORT 27
  221. #define AC_METER_TIMEOUT 28
  222. #define AC_METER_IC_COMM_TIMEOUT 29
  223. #define AC_CP_NEGATIVE_ERR 30
  224. #define RESERVED_2 31
  225. void PRINTF_FUNC(char *string, ...);
  226. int StoreLogMsg(const char *fmt, ...);
  227. int GetTimeoutValue(struct timespec *startTime);
  228. void ocpp_ac_clear_errorCode_cmd(bool isError);
  229. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  230. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  231. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  232. long int GetTimeoutMValue(struct timespec *startTime)
  233. {
  234. struct timespec endTime;
  235. clock_gettime(CLOCK_MONOTONIC, &endTime);
  236. return 1000 * (endTime.tv_sec - startTime->tv_sec) + (endTime.tv_nsec - startTime->tv_nsec) / 1000000;
  237. }
  238. void GetTimespecMFunc(struct timespec *time)
  239. {
  240. clock_gettime(CLOCK_MONOTONIC, time);
  241. }
  242. int GetTimeoutValue(struct timespec *startTime)
  243. {
  244. struct timespec endTime;
  245. clock_gettime(CLOCK_MONOTONIC_COARSE, &endTime);
  246. return endTime.tv_sec - startTime->tv_sec;
  247. }
  248. void GetTimespecFunc(struct timespec *time)
  249. {
  250. clock_gettime(CLOCK_MONOTONIC_COARSE, time);
  251. }
  252. int StoreLogMsg(const char *fmt, ...)
  253. {
  254. char Buf[4096+256];
  255. char buffer[4096];
  256. va_list args;
  257. struct timeb SeqEndTime;
  258. struct tm *tm;
  259. va_start(args, fmt);
  260. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  261. va_end(args);
  262. memset(Buf,0,sizeof(Buf));
  263. ftime(&SeqEndTime);
  264. SeqEndTime.time = time(NULL);
  265. tm=localtime(&SeqEndTime.time);
  266. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  267. {
  268. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  269. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  270. printf("%s \n", Buf);
  271. }
  272. else
  273. {
  274. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog_%s_Log",
  275. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  276. buffer,
  277. tm->tm_year+1900,tm->tm_mon+1,
  278. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  279. system(Buf);
  280. }
  281. return rc;
  282. }
  283. int DiffTimeb(struct timeb ST, struct timeb ET)
  284. {
  285. //return milli-second
  286. unsigned int StartTime, StopTime;
  287. StartTime = (unsigned int) ST.time;
  288. StopTime = (unsigned int) ET.time;
  289. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  290. return (StopTime - StartTime);
  291. }
  292. unsigned short MaxValue(unsigned short value1, unsigned short value2)
  293. {
  294. return value1 >= value2 ? value1 : value2;
  295. }
  296. void PRINTF_FUNC(char *string, ...)
  297. {
  298. va_list args;
  299. char buffer[4096];
  300. va_start(args, string);
  301. vsnprintf(buffer, sizeof(buffer), string, args);
  302. va_end(args);
  303. DEBUG_INFO("%s ", buffer);
  304. }
  305. //==========================================
  306. // Communication Function
  307. //==========================================
  308. void GetFwAndHwVersion_Fan()
  309. {
  310. if(Query_FW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
  311. {
  312. // FanModuleData
  313. strcpy((char *) ShmFanModuleData->version, ver.Version_FW);
  314. // SystemInfo
  315. strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, ver.Version_FW);
  316. //PRINTF_FUNC("GetFwAndHwVersion_Fan s1 = %s \n", ver.Version_FW);
  317. }
  318. if (Query_HW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
  319. {
  320. // SystemInfo
  321. strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, ver.Version_FW);
  322. //PRINTF_FUNC("GetFwAndHwVersion_Fan s2 = %s \n", ver.Version_HW);
  323. }
  324. }
  325. void GetFwAndHwVersion_Relay()
  326. {
  327. if (Query_FW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
  328. {
  329. // RelayModuleData
  330. strcpy((char *) ShmRelayModuleData->version, ver.Version_FW);
  331. // SystemInfo
  332. strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, ver.Version_FW);
  333. //PRINTF_FUNC("GetFwAndHwVersion_Relay s1 = %s \n", ver.Version_FW);
  334. }
  335. if (Query_HW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
  336. {
  337. // SystemInfo
  338. strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, ver.Version_FW);
  339. //PRINTF_FUNC("GetFwAndHwVersion_Relay s2 = %s \n", ver.Version_HW);
  340. }
  341. }
  342. void GetFwAndHwVersion_Led()
  343. {
  344. if (Query_FW_Ver(Uart5Fd, Addr.Led, &ver) == PASS)
  345. {
  346. // LedModuleData
  347. strcpy((char *) ShmLedModuleData->version, ver.Version_FW);
  348. // SystemInfo
  349. strcpy((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, ver.Version_FW);
  350. ShmLedModuleData->SelfTest_Comp = YES;
  351. }
  352. else
  353. {
  354. //PRINTF_FUNC("GetFwAndHwVersion_Led fail \n");
  355. }
  356. // if (Query_HW_Ver(Uart5Fd, Addr.Led, &ver) == PASS)
  357. // {
  358. // // SystemInfo
  359. // strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, ver.Version_FW);
  360. // //PRINTF_FUNC("GetFwAndHwVersion_Relay s2 = %s \n", ver.Version_HW);
  361. // }
  362. }
  363. void GetFwVersion_AC()
  364. {
  365. if (Query_FW_Ver(Uart5Fd, Addr.AcPlug, &ver) == PASS)
  366. {
  367. ac_chargingInfo[0]->SelfTest_Comp = YES;
  368. strcpy((char *) ac_chargingInfo[0]->version, ver.Version_FW);
  369. }
  370. }
  371. void GetAcModelName()
  372. {
  373. memset(ShmSysConfigAndInfo->SysConfig.AcModelName, 0x00, sizeof(ShmSysConfigAndInfo->SysConfig.AcModelName));
  374. if (Query_Model_Name(Uart5Fd, Addr.AcPlug, ShmSysConfigAndInfo->SysConfig.AcModelName) == PASS)
  375. {
  376. PRINTF_FUNC("ac model name = %s \n", ShmSysConfigAndInfo->SysConfig.AcModelName);
  377. }
  378. }
  379. void SetRtcData_Relay()
  380. {
  381. struct timeb csuTime;
  382. struct tm *tmCSU;
  383. ftime(&csuTime);
  384. tmCSU = localtime(&csuTime.time);
  385. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  386. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  387. // tmCSU->tm_sec);
  388. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  389. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  390. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  391. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  392. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  393. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  394. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  395. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  396. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  397. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  398. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  399. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  400. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  401. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  402. if (Config_Rtc_Data(Uart5Fd, Addr.Relay, &rtc) == PASS)
  403. {
  404. //PRINTF_FUNC("SetRtc (RB) sucessfully. \n");
  405. }
  406. }
  407. void SetModelName_Relay()
  408. {
  409. if (Config_Model_Name(Uart5Fd, Addr.Relay, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  410. {
  411. //PRINTF_FUNC("Set Model name (RB) PASS = %s \n", ShmSysConfigAndInfo->SysConfig.ModelName);
  412. }
  413. }
  414. void SetRtcData_Fan()
  415. {
  416. struct timeb csuTime;
  417. struct tm *tmCSU;
  418. ftime(&csuTime);
  419. tmCSU = localtime(&csuTime.time);
  420. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  421. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  422. // tmCSU->tm_sec);
  423. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  424. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  425. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  426. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  427. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  428. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  429. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  430. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  431. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  432. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  433. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  434. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  435. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  436. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  437. if (Config_Rtc_Data(Uart5Fd, Addr.Fan, &rtc) == PASS)
  438. {
  439. //PRINTF_FUNC("SetRtc (FB) sucessfully. \n");
  440. }
  441. }
  442. void SetModelName_Fan()
  443. {
  444. if (Config_Model_Name(Uart5Fd, Addr.Fan, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  445. {
  446. //PRINTF_FUNC("Set Model name (FAN) PASS = %s \n", ShmSysConfigAndInfo->SysConfig.ModelName);
  447. }
  448. }
  449. // AC 三相輸入電壓
  450. void GetPresentInputVol()
  451. {
  452. if (Query_Present_InputVoltage(Uart5Fd, Addr.Relay, &inputVoltage) == PASS)
  453. {
  454. // resolution : 0.1
  455. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_JARI &&
  456. is50kwSystem)
  457. {
  458. inputVoltage.L1N_L12 *= 1.732;
  459. inputVoltage.L2N_L23 *= 1.732;
  460. inputVoltage.L3N_L31 *= 1.732;
  461. }
  462. ShmSysConfigAndInfo->SysInfo.InputVoltageR = ShmRelayModuleData->InputL1Volt = inputVoltage.L1N_L12;
  463. ShmSysConfigAndInfo->SysInfo.InputVoltageS = ShmRelayModuleData->InputL2Volt = inputVoltage.L2N_L23;
  464. ShmSysConfigAndInfo->SysInfo.InputVoltageT = ShmRelayModuleData->InputL3Volt = inputVoltage.L3N_L31;
  465. //********************************************************************************************************//
  466. // Vin (UVP)
  467. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_IEC)
  468. {
  469. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == NO)
  470. {
  471. if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE_IEC)
  472. {
  473. PRINTF_FUNC("In Uvp L1N_L12 = %f \n", inputVoltage.L1N_L12);
  474. if (_threePhaseUvp[0] >= OVP_UVP_CHK_COUNT)
  475. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = YES;
  476. else
  477. _threePhaseUvp[0] += 1;
  478. }
  479. }
  480. else
  481. {
  482. if (inputVoltage.L1N_L12 > VIN_MIN_REV_VOLTAGE_IEC)
  483. {
  484. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = NO;
  485. _threePhaseUvp[0] = 0;
  486. }
  487. }
  488. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == NO)
  489. {
  490. if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE_IEC)
  491. {
  492. PRINTF_FUNC("In Uvp L2N_L23 = %f \n", inputVoltage.L2N_L23);
  493. if (_threePhaseUvp[1] >= OVP_UVP_CHK_COUNT)
  494. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = YES;
  495. else
  496. _threePhaseUvp[1] += 1;
  497. }
  498. }
  499. else
  500. {
  501. if (inputVoltage.L2N_L23 > VIN_MIN_REV_VOLTAGE_IEC)
  502. {
  503. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = NO;
  504. _threePhaseUvp[1] = 0;
  505. }
  506. }
  507. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == NO)
  508. {
  509. if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE_IEC)
  510. {
  511. PRINTF_FUNC("In Uvp L3N_L31 = %f \n", inputVoltage.L3N_L31);
  512. if (_threePhaseUvp[2] >= OVP_UVP_CHK_COUNT)
  513. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = YES;
  514. else
  515. _threePhaseUvp[2] += 1;
  516. }
  517. }
  518. else
  519. {
  520. if (inputVoltage.L3N_L31 > VIN_MIN_REV_VOLTAGE_IEC)
  521. {
  522. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = NO;
  523. _threePhaseUvp[2] = 0;
  524. }
  525. }
  526. }
  527. else if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_UL)
  528. {
  529. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == NO)
  530. {
  531. if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE_UL)
  532. {
  533. PRINTF_FUNC("In Uvp L1N_L12 = %f \n", inputVoltage.L1N_L12);
  534. if (_threePhaseUvp[0] >= OVP_UVP_CHK_COUNT)
  535. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = YES;
  536. else
  537. _threePhaseUvp[0] += 1;
  538. }
  539. }
  540. else
  541. {
  542. if (inputVoltage.L1N_L12 > VIN_MIN_REV_VOLTAGE_UL)
  543. {
  544. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = NO;
  545. _threePhaseUvp[0] = 0;
  546. }
  547. }
  548. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == NO)
  549. {
  550. if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE_UL)
  551. {
  552. PRINTF_FUNC("In Uvp L2N_L23 = %f \n", inputVoltage.L2N_L23);
  553. if (_threePhaseUvp[1] >= OVP_UVP_CHK_COUNT)
  554. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = YES;
  555. else
  556. _threePhaseUvp[1] += 1;
  557. }
  558. }
  559. else
  560. {
  561. if (inputVoltage.L2N_L23 > VIN_MIN_REV_VOLTAGE_UL)
  562. {
  563. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = NO;
  564. _threePhaseUvp[1] = 0;
  565. }
  566. }
  567. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == NO)
  568. {
  569. if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE_UL)
  570. {
  571. PRINTF_FUNC("In Uvp L3N_L31 = %f \n", inputVoltage.L3N_L31);
  572. if (_threePhaseUvp[2] >= OVP_UVP_CHK_COUNT)
  573. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = YES;
  574. else
  575. _threePhaseUvp[2] += 1;
  576. }
  577. }
  578. else
  579. {
  580. if (inputVoltage.L3N_L31 > VIN_MIN_REV_VOLTAGE_UL)
  581. {
  582. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = NO;
  583. _threePhaseUvp[2] = 0;
  584. }
  585. }
  586. }
  587. else if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_JARI)
  588. {
  589. if (!is50kwSystem)
  590. {
  591. //LWADD
  592. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == NO)
  593. {
  594. if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE_JARI)
  595. {
  596. PRINTF_FUNC("In Uvp L1N_L12 = %f \n", inputVoltage.L1N_L12);
  597. if (_threePhaseUvp[0] >= OVP_UVP_CHK_COUNT)
  598. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = YES;
  599. else
  600. _threePhaseUvp[0] += 1;
  601. }
  602. }
  603. else
  604. {
  605. if (inputVoltage.L1N_L12 > VIN_MIN_REV_VOLTAGE_JARI)
  606. {
  607. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = NO;
  608. _threePhaseUvp[0] = 0;
  609. }
  610. }
  611. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == NO)
  612. {
  613. if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE_JARI)
  614. {
  615. PRINTF_FUNC("In Uvp L2N_L23 = %f \n", inputVoltage.L2N_L23);
  616. if (_threePhaseUvp[1] >= OVP_UVP_CHK_COUNT)
  617. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = YES;
  618. else
  619. _threePhaseUvp[1] += 1;
  620. }
  621. }
  622. else
  623. {
  624. if (inputVoltage.L2N_L23 > VIN_MIN_REV_VOLTAGE_JARI)
  625. {
  626. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = NO;
  627. _threePhaseUvp[1] = 0;
  628. }
  629. }
  630. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == NO)
  631. {
  632. if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE_JARI)
  633. {
  634. PRINTF_FUNC("In Uvp L3N_L31 = %f \n", inputVoltage.L3N_L31);
  635. if (_threePhaseUvp[2] >= OVP_UVP_CHK_COUNT)
  636. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = YES;
  637. else
  638. _threePhaseUvp[2] += 1;
  639. }
  640. }
  641. else
  642. {
  643. if (inputVoltage.L3N_L31 > VIN_MIN_REV_VOLTAGE_JARI)
  644. {
  645. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = NO;
  646. _threePhaseUvp[2] = 0;
  647. }
  648. }
  649. }
  650. else
  651. {
  652. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == NO)
  653. {
  654. if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE_JARI_50KW)
  655. {
  656. PRINTF_FUNC("In Uvp L1N_L12 = %f \n", inputVoltage.L1N_L12);
  657. if (_threePhaseUvp[0] >= OVP_UVP_CHK_COUNT)
  658. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = YES;
  659. else
  660. _threePhaseUvp[0] += 1;
  661. }
  662. }
  663. else
  664. {
  665. if (inputVoltage.L1N_L12 > VIN_MIN_REV_VOLTAGE_JARI_50KW)
  666. {
  667. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = NO;
  668. _threePhaseUvp[0] = 0;
  669. }
  670. }
  671. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == NO)
  672. {
  673. if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE_JARI_50KW)
  674. {
  675. PRINTF_FUNC("In Uvp L2N_L23 = %f \n", inputVoltage.L2N_L23);
  676. if (_threePhaseUvp[1] >= OVP_UVP_CHK_COUNT)
  677. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = YES;
  678. else
  679. _threePhaseUvp[1] += 1;
  680. }
  681. }
  682. else
  683. {
  684. if (inputVoltage.L2N_L23 > VIN_MIN_REV_VOLTAGE_JARI_50KW)
  685. {
  686. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = NO;
  687. _threePhaseUvp[1] = 0;
  688. }
  689. }
  690. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == NO)
  691. {
  692. if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE_JARI_50KW)
  693. {
  694. PRINTF_FUNC("In Uvp L3N_L31 = %f \n", inputVoltage.L3N_L31);
  695. if (_threePhaseUvp[2] >= OVP_UVP_CHK_COUNT)
  696. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = YES;
  697. else
  698. _threePhaseUvp[2] += 1;
  699. }
  700. }
  701. else
  702. {
  703. if (inputVoltage.L3N_L31 > VIN_MIN_REV_VOLTAGE_JARI_50KW)
  704. {
  705. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = NO;
  706. _threePhaseUvp[2] = 0;
  707. }
  708. }
  709. }
  710. }
  711. //********************************************************************************************************//
  712. // Vin (OVP)
  713. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_IEC)
  714. {
  715. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == NO)
  716. {
  717. if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE_IEC)
  718. {
  719. PRINTF_FUNC("In Ovp L1N_L12 = %f \n", inputVoltage.L1N_L12);
  720. if (_threePhaseOvp[0] >= OVP_UVP_CHK_COUNT)
  721. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = YES;
  722. else
  723. _threePhaseOvp[0] += 1;
  724. }
  725. }
  726. else
  727. {
  728. if (inputVoltage.L1N_L12 < VIN_MAX_REV_VOLTAGE_IEC)
  729. {
  730. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = NO;
  731. _threePhaseOvp[0] = 0;
  732. }
  733. }
  734. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == NO)
  735. {
  736. if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE_IEC)
  737. {
  738. PRINTF_FUNC("In Ovp L2N_L23 = %f \n", inputVoltage.L2N_L23);
  739. if (_threePhaseOvp[1] >= OVP_UVP_CHK_COUNT)
  740. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = YES;
  741. else
  742. _threePhaseOvp[1] += 1;
  743. }
  744. }
  745. else
  746. {
  747. if (inputVoltage.L2N_L23 < VIN_MAX_REV_VOLTAGE_IEC)
  748. {
  749. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = NO;
  750. _threePhaseOvp[1] = 0;
  751. }
  752. }
  753. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == NO)
  754. {
  755. if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE_IEC)
  756. {
  757. PRINTF_FUNC("In Ovp L3N_L31 = %f \n", inputVoltage.L3N_L31);
  758. if (_threePhaseOvp[2] >= OVP_UVP_CHK_COUNT)
  759. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = YES;
  760. else
  761. _threePhaseOvp[2] += 1;
  762. }
  763. }
  764. else
  765. {
  766. if (inputVoltage.L3N_L31 < VIN_MAX_REV_VOLTAGE_IEC)
  767. {
  768. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = NO;
  769. _threePhaseOvp[2] = 0;
  770. }
  771. }
  772. }
  773. else if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_UL)
  774. {
  775. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == NO)
  776. {
  777. if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE_UL)
  778. {
  779. PRINTF_FUNC("In Ovp L1N_L12 = %f \n", inputVoltage.L1N_L12);
  780. if (_threePhaseOvp[0] >= OVP_UVP_CHK_COUNT)
  781. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = YES;
  782. else
  783. _threePhaseOvp[0] += 1;
  784. }
  785. }
  786. else
  787. {
  788. if (inputVoltage.L1N_L12 < VIN_MAX_REV_VOLTAGE_UL)
  789. {
  790. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = NO;
  791. _threePhaseOvp[0] = 0;
  792. }
  793. }
  794. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == NO)
  795. {
  796. if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE_UL)
  797. {
  798. PRINTF_FUNC("In Ovp L2N_L23 = %f \n", inputVoltage.L2N_L23);
  799. if (_threePhaseOvp[1] >= OVP_UVP_CHK_COUNT)
  800. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = YES;
  801. else
  802. _threePhaseOvp[1] += 1;
  803. }
  804. }
  805. else
  806. {
  807. if (inputVoltage.L2N_L23 < VIN_MAX_REV_VOLTAGE_UL)
  808. {
  809. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = NO;
  810. _threePhaseOvp[1] = 0;
  811. }
  812. }
  813. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == NO)
  814. {
  815. if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE_UL)
  816. {
  817. PRINTF_FUNC("In Ovp L3N_L31 = %f \n", inputVoltage.L3N_L31);
  818. if (_threePhaseOvp[2] >= OVP_UVP_CHK_COUNT)
  819. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = YES;
  820. else
  821. _threePhaseOvp[2] += 1;
  822. }
  823. }
  824. else
  825. {
  826. if (inputVoltage.L3N_L31 < VIN_MAX_REV_VOLTAGE_UL)
  827. {
  828. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = NO;
  829. _threePhaseOvp[2] = 0;
  830. }
  831. }
  832. }
  833. else if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_JARI)
  834. {
  835. if (!is50kwSystem)
  836. {
  837. //LWADD
  838. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == NO)
  839. {
  840. if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE_JARI)
  841. {
  842. PRINTF_FUNC("In Ovp L1N_L12 = %f \n", inputVoltage.L1N_L12);
  843. if (_threePhaseOvp[0] >= OVP_UVP_CHK_COUNT)
  844. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = YES;
  845. else
  846. _threePhaseOvp[0] += 1;
  847. }
  848. }
  849. else
  850. {
  851. if (inputVoltage.L1N_L12 < VIN_MAX_REV_VOLTAGE_JARI)
  852. {
  853. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = NO;
  854. _threePhaseOvp[0] = 0;
  855. }
  856. }
  857. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == NO)
  858. {
  859. if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE_JARI)
  860. {
  861. PRINTF_FUNC("In Ovp L2N_L23 = %f \n", inputVoltage.L2N_L23);
  862. if (_threePhaseOvp[1] >= OVP_UVP_CHK_COUNT)
  863. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = YES;
  864. else
  865. _threePhaseOvp[1] += 1;
  866. }
  867. }
  868. else
  869. {
  870. if (inputVoltage.L2N_L23 < VIN_MAX_REV_VOLTAGE_JARI)
  871. {
  872. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = NO;
  873. _threePhaseOvp[1] = 0;
  874. }
  875. }
  876. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == NO)
  877. {
  878. if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE_JARI)
  879. {
  880. PRINTF_FUNC("In Ovp L3N_L31 = %f \n", inputVoltage.L3N_L31);
  881. if (_threePhaseOvp[2] >= OVP_UVP_CHK_COUNT)
  882. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = YES;
  883. else
  884. _threePhaseOvp[2] += 1;
  885. }
  886. }
  887. else
  888. {
  889. if (inputVoltage.L3N_L31 < VIN_MAX_REV_VOLTAGE_JARI)
  890. {
  891. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = NO;
  892. _threePhaseOvp[2] = 0;
  893. }
  894. }
  895. }
  896. else
  897. {
  898. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == NO)
  899. {
  900. if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE_JARI_50KW)
  901. {
  902. PRINTF_FUNC("In Ovp L1N_L12 = %f \n", inputVoltage.L1N_L12);
  903. if (_threePhaseOvp[0] >= OVP_UVP_CHK_COUNT)
  904. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = YES;
  905. else
  906. _threePhaseOvp[0] += 1;
  907. }
  908. }
  909. else
  910. {
  911. if (inputVoltage.L1N_L12 < VIN_MAX_REV_VOLTAGE_JARI_50KW)
  912. {
  913. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = NO;
  914. _threePhaseOvp[0] = 0;
  915. }
  916. }
  917. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == NO)
  918. {
  919. if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE_JARI_50KW)
  920. {
  921. PRINTF_FUNC("In Ovp L2N_L23 = %f \n", inputVoltage.L2N_L23);
  922. if (_threePhaseOvp[1] >= OVP_UVP_CHK_COUNT)
  923. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = YES;
  924. else
  925. _threePhaseOvp[1] += 1;
  926. }
  927. }
  928. else
  929. {
  930. if (inputVoltage.L2N_L23 < VIN_MAX_REV_VOLTAGE_JARI_50KW)
  931. {
  932. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = NO;
  933. _threePhaseOvp[1] = 0;
  934. }
  935. }
  936. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == NO)
  937. {
  938. if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE_JARI_50KW)
  939. {
  940. PRINTF_FUNC("In Ovp L3N_L31 = %f \n", inputVoltage.L3N_L31);
  941. if (_threePhaseOvp[2] >= OVP_UVP_CHK_COUNT)
  942. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = YES;
  943. else
  944. _threePhaseOvp[2] += 1;
  945. }
  946. }
  947. else
  948. {
  949. if (inputVoltage.L3N_L31 < VIN_MAX_REV_VOLTAGE_JARI_50KW)
  950. {
  951. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = NO;
  952. _threePhaseOvp[2] = 0;
  953. }
  954. }
  955. }
  956. }
  957. }
  958. }
  959. // 左右槍的 Relay 前後的輸出電壓
  960. void GetPersentOutputVol()
  961. {
  962. if (Query_Present_OutputVoltage(Uart5Fd, Addr.Relay, &outputVoltage) == PASS)
  963. {
  964. // PRINTF_FUNC("Conn1 fuse 1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
  965. // PRINTF_FUNC("Conn1 relay 1 = %f \n", outputVoltage.behindRelay_Voltage_C1);
  966. // PRINTF_FUNC("Conn2 fuse 2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
  967. // PRINTF_FUNC("Conn2 relay 2 = %f \n", outputVoltage.behindRelay_Voltage_C2);
  968. //PRINTF_FUNC("outputVoltage.behindFuse_Voltage_C1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
  969. //PRINTF_FUNC("outputVoltage.behindFuse_Voltage_C2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
  970. ShmRelayModuleData->Gun1FuseOutputVolt = outputVoltage.behindFuse_Voltage_C1;
  971. ShmRelayModuleData->Gun1RelayOutputVolt = outputVoltage.behindRelay_Voltage_C1;
  972. ShmRelayModuleData->Gun2FuseOutputVolt = outputVoltage.behindFuse_Voltage_C2;
  973. ShmRelayModuleData->Gun2RelayOutputVolt = outputVoltage.behindRelay_Voltage_C2;
  974. for (int index = 0; index < gunCount; index++)
  975. {
  976. if (index == 0)
  977. {
  978. if (_chargingData[index]->Evboard_id == 0x01)
  979. {
  980. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun1FuseOutputVolt;
  981. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun1RelayOutputVolt;
  982. }
  983. else if (_chargingData[index]->Evboard_id == 0x02)
  984. {
  985. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
  986. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
  987. }
  988. }
  989. else if (index == 1)
  990. {
  991. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
  992. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
  993. }
  994. //unsigned short Ovp = 0;
  995. //unsigned short Ocp = 0;
  996. //Ovp = MIN [VOUT_MAX_VOLTAGE, EV_BATTERY_VOLTAGE] // 最大輸出電壓與電池電壓最大值
  997. //Ocp = MIN [IOUT_MAX_CURRENT, EV_CURRENT_REQ] // 最大輸出電流與需求電流最小值
  998. if (_chargingData[index]->Type == _Type_Chademo)
  999. {
  1000. //Ovp = MaxValue(_chargingData[index]->MaximumChargingVoltage, _chargingData[index]->EvBatteryMaxVoltage);
  1001. //Ocp = MaxValue(_chargingData[index]->PresentChargingCurrent, ShmCHAdeMOData->ev[_chargingData[index]->type_index].ChargingCurrentRequest);
  1002. }
  1003. else if (_chargingData[index]->Type == _Type_CCS)
  1004. {
  1005. }
  1006. }
  1007. }
  1008. }
  1009. // 風扇速度
  1010. void GetFanSpeed()
  1011. {
  1012. //PRINTF_FUNC("Get fan board speed \n");
  1013. if (Query_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed) == PASS)
  1014. {
  1015. ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0];
  1016. ShmFanModuleData->PresentFan2Speed = fanSpeed.speed[1];
  1017. ShmFanModuleData->PresentFan3Speed = fanSpeed.speed[2];
  1018. ShmFanModuleData->PresentFan4Speed = fanSpeed.speed[3];
  1019. // PRINTF_FUNC("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
  1020. // PRINTF_FUNC("SystemFanRotaSpeed_2 = %d \n", fanSpeed.speed[1]);
  1021. // PRINTF_FUNC("SystemFanRotaSpeed_3 = %d \n", fanSpeed.speed[2]);
  1022. // PRINTF_FUNC("SystemFanRotaSpeed_4 = %d \n", fanSpeed.speed[3]);
  1023. // Config_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed[0]);
  1024. //SysInfoData (SystemFanRotaSpeed)
  1025. }
  1026. }
  1027. // 讀取 Relay 狀態
  1028. void GetRelayOutputStatus()
  1029. {
  1030. if (Query_Relay_Output(Uart5Fd, Addr.Relay, &regRelay) == PASS)
  1031. {
  1032. //regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  1033. }
  1034. }
  1035. // 確認 K1 K2 relay 的狀態
  1036. void CheckK1K2RelayOutput(byte index)
  1037. {
  1038. if (index == 0)
  1039. {
  1040. if (_chargingData[index]->Evboard_id == 0x01)
  1041. {
  1042. if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.Gun1_P == YES)
  1043. _chargingData[index]->RelayK1K2Status = YES;
  1044. else
  1045. _chargingData[index]->RelayK1K2Status = NO;
  1046. if(_chargingData[index]->Type == _Type_CCS)
  1047. {
  1048. // if (gunCount == 1)
  1049. // {
  1050. // if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  1051. // _chargingData[index]->RelayKPK2Status = YES;
  1052. // else
  1053. // _chargingData[index]->RelayKPK2Status = NO;
  1054. // }
  1055. // else
  1056. {
  1057. if (_chargingData[index]->SystemStatus == SYS_MODE_CCS_PRECHARGE_STEP0)
  1058. _chargingData[index]->RelayKPK2Status = YES;
  1059. else
  1060. _chargingData[index]->RelayKPK2Status = NO;
  1061. }
  1062. }
  1063. }
  1064. else if (_chargingData[index]->Evboard_id == 0x02)
  1065. {
  1066. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
  1067. _chargingData[index]->RelayK1K2Status = YES;
  1068. else
  1069. _chargingData[index]->RelayK1K2Status = NO;
  1070. if(_chargingData[index]->Type == _Type_CCS)
  1071. {
  1072. // if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  1073. // _chargingData[index]->RelayKPK2Status = YES;
  1074. // else
  1075. // _chargingData[index]->RelayKPK2Status = NO;
  1076. if (_chargingData[index]->SystemStatus == SYS_MODE_CCS_PRECHARGE_STEP0)
  1077. _chargingData[index]->RelayKPK2Status = YES;
  1078. else
  1079. _chargingData[index]->RelayKPK2Status = NO;
  1080. }
  1081. }
  1082. }
  1083. else if (index == 1)
  1084. {
  1085. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
  1086. _chargingData[index]->RelayK1K2Status = YES;
  1087. else
  1088. _chargingData[index]->RelayK1K2Status = NO;
  1089. if(_chargingData[index]->Type == _Type_CCS)
  1090. {
  1091. // if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  1092. // _chargingData[index]->RelayKPK2Status = YES;
  1093. // else
  1094. // _chargingData[index]->RelayKPK2Status = NO;
  1095. if (_chargingData[index]->SystemStatus == SYS_MODE_CCS_PRECHARGE_STEP0)
  1096. _chargingData[index]->RelayKPK2Status = YES;
  1097. else
  1098. _chargingData[index]->RelayKPK2Status = NO;
  1099. }
  1100. }
  1101. if (ShmSmartBoxData->RcbParallelStatus[0] == YES)
  1102. {
  1103. if (regRelay.relay_event.bits.R3_BOTH_K1_K4 == NO)
  1104. ShmSmartBoxData->RcbParallelStatus[0] = NO;
  1105. }
  1106. else if (ShmSmartBoxData->RcbParallelStatus[0] == NO)
  1107. {
  1108. if (regRelay.relay_event.bits.R3_BOTH_K1_K4 == YES)
  1109. ShmSmartBoxData->RcbParallelStatus[0] = YES;
  1110. }
  1111. if (ShmSmartBoxData->RcbParallelStatus[1] == YES)
  1112. {
  1113. if (regRelay.relay_event.bits.R4_BOTH_K2_K5 == NO)
  1114. ShmSmartBoxData->RcbParallelStatus [1] = NO;
  1115. }
  1116. else if (ShmSmartBoxData->RcbParallelStatus [1] == NO)
  1117. {
  1118. if (regRelay.relay_event.bits.R4_BOTH_K2_K5 == YES)
  1119. ShmSmartBoxData->RcbParallelStatus [1] = YES;
  1120. }
  1121. if (ShmSmartBoxData->RcbParallelStatus[2] == YES)
  1122. {
  1123. if (regRelay.relay_event.bits.R5_BOTH_K3_K6 == NO)
  1124. ShmSmartBoxData->RcbParallelStatus [2] = NO;
  1125. }
  1126. else if (ShmSmartBoxData->RcbParallelStatus [2] == NO)
  1127. {
  1128. if (regRelay.relay_event.bits.R5_BOTH_K3_K6 == YES)
  1129. ShmSmartBoxData->RcbParallelStatus [2] = YES;
  1130. }
  1131. // PRINTF_FUNC("Check Relay Output. index = %d, RelayKPK2Status = %d, BridgeRelayStatus = %d \n",
  1132. // index, _chargingData[index]->RelayKPK2Status, ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus);
  1133. }
  1134. void GetGfdAdc()
  1135. {
  1136. // define : 每 0.2 ~ 1 秒一次
  1137. // occur : <= 75k 歐姆 @ 150 - 750 Vdc
  1138. // warning : >= 100 歐姆 && <= 500 歐姆 @ 150-750 Vdc
  1139. if (Query_Gfd_Adc(Uart5Fd, Addr.Relay, &gfd_adc) == PASS)
  1140. {
  1141. for (int i = 0; i < gunCount; i++)
  1142. {
  1143. if (_chargingData[i]->Type == 0x09 && !ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag)
  1144. {
  1145. if (_chargingData[i]->FireChargingVoltage >= VOUT_MIN_VOLTAGE)
  1146. _chargingData[i]->GroundFaultStatus = GFD_PASS;
  1147. continue;
  1148. }
  1149. if (i == 0)
  1150. {
  1151. _chargingData[i]->GroundFaultStatus = gfd_adc.result_conn1;
  1152. // PRINTF_FUNC("GFD ******** Result = %d, Step = %d, R = %d, Vol = %d \n",
  1153. // _chargingData[i]->GroundFaultStatus, gfd_adc.rb_step_1, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  1154. if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
  1155. {
  1156. PRINTF_FUNC("GFD Fail. index = %d, Step = %d, R = %d, Vol = %d \n",
  1157. i, gfd_adc.rb_step_1, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  1158. }
  1159. else if (_chargingData[i]->GroundFaultStatus == GFD_PASS ||
  1160. _chargingData[i]->GroundFaultStatus == GFD_WARNING)
  1161. {
  1162. if (_chargingData[i]->GroundFaultStatus == GFD_WARNING)
  1163. {
  1164. PRINTF_FUNC("GFD Warning. index = %d, Result = %d, R = %d, Vol = %d \n",
  1165. i, _chargingData[i]->GroundFaultStatus, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  1166. }
  1167. }
  1168. }
  1169. else if (i == 1)
  1170. {
  1171. _chargingData[i]->GroundFaultStatus = gfd_adc.result_conn2;
  1172. if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
  1173. {
  1174. PRINTF_FUNC("GFD Fail. index = %d, Step = %d, R = %d, Vol = %d \n",
  1175. i, gfd_adc.rb_step_2, gfd_adc.Resister_conn2, gfd_adc.voltage_conn2);
  1176. }
  1177. else if (_chargingData[i]->GroundFaultStatus == GFD_PASS ||
  1178. _chargingData[i]->GroundFaultStatus == GFD_WARNING)
  1179. {
  1180. if (_chargingData[i]->GroundFaultStatus == GFD_WARNING)
  1181. {
  1182. PRINTF_FUNC("GFD Warning. index = %d, Result = %d, R = %d, Vol = %d \n",
  1183. i, _chargingData[i]->GroundFaultStatus, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  1184. }
  1185. }
  1186. }
  1187. }
  1188. }
  1189. }
  1190. void GetGpioInput()
  1191. {
  1192. if (Query_Gpio_Input(Uart5Fd, Addr.Aux, &gpio_in) == PASS)
  1193. {
  1194. // AC Contactor Status
  1195. if (gpio_in.AC_MainBreaker == 1)
  1196. {
  1197. // AC Main Breaker ON
  1198. PRINTF_FUNC("RB AC Main Breaker. \n");
  1199. }
  1200. if (gpio_in.SPD == 1)
  1201. {
  1202. // SPD (雷擊保護) ON
  1203. PRINTF_FUNC("RB SPD. \n");
  1204. }
  1205. if (gpio_in.Door_Open == 1)
  1206. {
  1207. // Door Open
  1208. PRINTF_FUNC("RB Door Open. \n");
  1209. }
  1210. if (gpio_in.GFD[0] == 1)
  1211. {
  1212. // GFD_1 Trigger
  1213. }
  1214. if (gpio_in.GFD[1] == 1)
  1215. {
  1216. // GFD_2 Trigger
  1217. }
  1218. if (gpio_in.AC_Drop == 1)
  1219. {
  1220. // AC Drop
  1221. PRINTF_FUNC("RB AC Drop. \n");
  1222. }
  1223. if (gpio_in.Emergency_IO == 1)
  1224. {
  1225. // Emergency IO ON
  1226. PRINTF_FUNC("RB Emergency IO ON. \n");
  1227. }
  1228. if (gpio_in.Button_Emergency_Press == 1)
  1229. {
  1230. // Emergency button Press
  1231. }
  1232. if (gpio_in.Button_On_Press == 1)
  1233. {
  1234. // On button Press
  1235. }
  1236. if (gpio_in.Button_Off_Press == 1)
  1237. {
  1238. // Off button Press
  1239. }
  1240. if (gpio_in.Key_1_Press == 1)
  1241. {
  1242. // key 1 press
  1243. }
  1244. if (gpio_in.Key_2_Press == 1)
  1245. {
  1246. // key 2 press
  1247. }
  1248. if (gpio_in.Key_3_Press == 1)
  1249. {
  1250. // key 3 press
  1251. }
  1252. if (gpio_in.Key_4_Press == 1)
  1253. {
  1254. // key 4 press
  1255. }
  1256. }
  1257. }
  1258. // 5V 12V 24V 48V
  1259. void GetAuxPower()
  1260. {
  1261. if (Query_Aux_PowerVoltage(Uart5Fd, Addr.Fan, &auxPower) == PASS)
  1262. {
  1263. ShmSysConfigAndInfo->SysInfo.AuxPower48V = auxPower.voltage[0];
  1264. ShmSysConfigAndInfo->SysInfo.AuxPower24V = auxPower.voltage[1];
  1265. //ShmSysConfigAndInfo->SysInfo.AuxPower12V = auxPower.voltage[4];
  1266. //ShmSysConfigAndInfo->SysInfo.AuxPower5V = auxPower.voltage[6];
  1267. // aux power voltage
  1268. //PRINTF_FUNC("aux1 = %x, \n", auxPower.voltage[0]);
  1269. //PRINTF_FUNC("aux2 = %x, \n", auxPower.voltage[1]);
  1270. }
  1271. }
  1272. void SetFanModuleSpeed()
  1273. {
  1274. {
  1275. FanSpeed _fanSpeed;
  1276. _setFanSpeed += fanSpeedSmoothValue;
  1277. if (_setFanSpeed >= ShmFanModuleData->SetFan1Speed)
  1278. _setFanSpeed = ShmFanModuleData->SetFan1Speed;
  1279. //printf("_setFanSpeed = %d \n", _setFanSpeed);
  1280. _fanSpeed.speed[0] = _setFanSpeed;
  1281. _fanSpeed.speed[1] = _setFanSpeed;
  1282. _fanSpeed.speed[2] = _setFanSpeed;
  1283. _fanSpeed.speed[3] = _setFanSpeed;
  1284. if (Config_Fan_Speed(Uart5Fd, Addr.Fan, &_fanSpeed) == PASS)
  1285. {
  1286. //PRINTF_FUNC("successfully Fan\n");
  1287. }
  1288. }
  1289. }
  1290. //==========================================
  1291. // Common Function
  1292. //==========================================
  1293. void SetK1K2RelayStatus(byte index)
  1294. {
  1295. if (ShmPsuData->Work_Step >= _TEST_MODE && ShmPsuData->Work_Step <= _TEST_MODE)
  1296. {
  1297. if(regRelay.relay_event.bits.Gun1_N == NO)
  1298. outputRelay.relay_event.bits.Gun1_N = YES;
  1299. else if (regRelay.relay_event.bits.Gun1_P == NO)
  1300. outputRelay.relay_event.bits.Gun1_P = YES;
  1301. return;
  1302. }
  1303. if (_chargingData[index]->SystemStatus < SYS_MODE_PREPARE_FOR_EVSE ||
  1304. _chargingData[index]->SystemStatus == SYS_MODE_FAULT ||
  1305. _chargingData[index]->GroundFaultStatus == GFD_FAIL)
  1306. {
  1307. if (_chargingData[index]->Evboard_id == 0x01)
  1308. {
  1309. outputRelay.relay_event.bits.Gun1_P = NO;
  1310. outputRelay.relay_event.bits.Gun1_N = NO;
  1311. }
  1312. else if (_chargingData[index]->Evboard_id == 0x02)
  1313. {
  1314. outputRelay.relay_event.bits.Gun2_P = NO;
  1315. outputRelay.relay_event.bits.Gun2_N = NO;
  1316. }
  1317. }
  1318. else if ((_chargingData[index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE &&
  1319. _chargingData[index]->SystemStatus <= SYS_MODE_CHARGING))
  1320. {
  1321. //if (_chargingData[index]->RelayWeldingCheck == YES)
  1322. {
  1323. if (_chargingData[index]->Evboard_id == 0x01)
  1324. {
  1325. if(regRelay.relay_event.bits.Gun1_N == NO)
  1326. outputRelay.relay_event.bits.Gun1_N = YES;
  1327. else if (regRelay.relay_event.bits.Gun1_P == NO)
  1328. outputRelay.relay_event.bits.Gun1_P = YES;
  1329. }
  1330. else if (_chargingData[index]->Evboard_id == 0x02)
  1331. {
  1332. if(regRelay.relay_event.bits.Gun2_N == NO)
  1333. outputRelay.relay_event.bits.Gun2_N = YES;
  1334. else if (regRelay.relay_event.bits.Gun2_P == NO)
  1335. outputRelay.relay_event.bits.Gun2_P = YES;
  1336. }
  1337. }
  1338. }
  1339. else if (_chargingData[index]->SystemStatus >= SYS_MODE_TERMINATING &&
  1340. _chargingData[index]->SystemStatus <= SYS_MODE_ALARM)
  1341. {
  1342. if ((_chargingData[index]->PresentChargingCurrent * 10) <= SEFETY_SWITCH_RELAY_CUR)
  1343. {
  1344. if (_chargingData[index]->Evboard_id == 0x01)
  1345. {
  1346. if(regRelay.relay_event.bits.Gun1_P == YES)
  1347. outputRelay.relay_event.bits.Gun1_P = NO;
  1348. else if (regRelay.relay_event.bits.Gun1_N == YES)
  1349. outputRelay.relay_event.bits.Gun1_N = NO;
  1350. }
  1351. else if (_chargingData[index]->Evboard_id == 0x02)
  1352. {
  1353. if(regRelay.relay_event.bits.Gun2_P == YES)
  1354. outputRelay.relay_event.bits.Gun2_P = NO;
  1355. else if (regRelay.relay_event.bits.Gun2_N == YES)
  1356. outputRelay.relay_event.bits.Gun2_N = NO;
  1357. }
  1358. }
  1359. }
  1360. else if (_chargingData[index]->SystemStatus == SYS_MODE_CCS_PRECHARGE_STEP0)
  1361. {
  1362. // if (_chargingData[index]->Evboard_id == 0x01)
  1363. // {
  1364. // if (_chargingData[index]->Type == _Type_CCS_2)
  1365. // {
  1366. // if (gunCount == 1)
  1367. // {
  1368. // if (regRelay.relay_event.bits.CCS_Precharge == NO)
  1369. // outputRelay.relay_event.bits.CCS_Precharge = YES;
  1370. // else if (regRelay.relay_event.bits.CCS_Precharge == YES)
  1371. // outputRelay.relay_event.bits.Gun1_P = NO;
  1372. // }
  1373. // }
  1374. // }
  1375. // else if (_chargingData[index]->Evboard_id == 0x02)
  1376. // {
  1377. // if (_chargingData[index]->Type == _Type_CCS_2)
  1378. // {
  1379. // if (regRelay.relay_event.bits.CCS_Precharge == NO)
  1380. // outputRelay.relay_event.bits.CCS_Precharge = YES;
  1381. // else if (regRelay.relay_event.bits.CCS_Precharge == YES)
  1382. // outputRelay.relay_event.bits.Gun2_P = NO;
  1383. // }
  1384. // }
  1385. }
  1386. else if (_chargingData[index]->SystemStatus == SYS_MODE_CCS_PRECHARGE_STEP1)
  1387. {
  1388. // if (_chargingData[index]->Evboard_id == 0x01)
  1389. // {
  1390. // if (_chargingData[index]->Type == _Type_CCS_2)
  1391. // {
  1392. // if (gunCount == 1)
  1393. // {
  1394. // if (regRelay.relay_event.bits.Gun1_P == NO)
  1395. // outputRelay.relay_event.bits.Gun1_P = YES;
  1396. // else if(regRelay.relay_event.bits.Gun1_P == YES)
  1397. // outputRelay.relay_event.bits.CCS_Precharge = NO;
  1398. // }
  1399. // }
  1400. // }
  1401. // else if (_chargingData[index]->Evboard_id == 0x02)
  1402. // {
  1403. // if (_chargingData[index]->Type == _Type_CCS_2)
  1404. // {
  1405. // if (regRelay.relay_event.bits.Gun2_P == NO)
  1406. // outputRelay.relay_event.bits.Gun2_P = YES;
  1407. // else if(regRelay.relay_event.bits.Gun2_P == YES)
  1408. // outputRelay.relay_event.bits.CCS_Precharge = NO;
  1409. // }
  1410. // }
  1411. }
  1412. }
  1413. void SetParalleRelayStatus()
  1414. {
  1415. // 之後雙槍單模機種,橋接都會上
  1416. if (gunCount >= 2 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  1417. {
  1418. if (_chargingData[0]->SystemStatus == SYS_MODE_BOOTING || _chargingData[1]->SystemStatus == SYS_MODE_BOOTING ||
  1419. ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL || !isSelftestComp ||
  1420. ((_chargingData[0]->SystemStatus == SYS_MODE_IDLE || _chargingData[0]->SystemStatus == SYS_MODE_MAINTAIN) &&
  1421. (_chargingData[1]->SystemStatus == SYS_MODE_IDLE || _chargingData[1]->SystemStatus == SYS_MODE_MAINTAIN)))
  1422. {
  1423. // 初始化~ 不搭橋接
  1424. }
  1425. else
  1426. {
  1427. if (ShmSmartBoxData->ParallelRelayStatus[_RELAY_SWITCH_NAME_R3])
  1428. {
  1429. if (regRelay.relay_event.bits.R3_BOTH_K1_K4 == NO)
  1430. outputRelay.relay_event.bits.R3_BOTH_K1_K4 = YES;
  1431. }
  1432. else
  1433. {
  1434. if (regRelay.relay_event.bits.R3_BOTH_K1_K4 == YES)
  1435. outputRelay.relay_event.bits.R3_BOTH_K1_K4 = NO;
  1436. }
  1437. if (ShmSmartBoxData->ParallelRelayStatus [_RELAY_SWITCH_NAME_R4])
  1438. {
  1439. if (regRelay.relay_event.bits.R4_BOTH_K2_K5 == NO)
  1440. outputRelay.relay_event.bits.R4_BOTH_K2_K5 = YES;
  1441. }
  1442. else
  1443. {
  1444. if (regRelay.relay_event.bits.R4_BOTH_K2_K5 == YES)
  1445. outputRelay.relay_event.bits.R4_BOTH_K2_K5 = NO;
  1446. }
  1447. if (ShmSmartBoxData->ParallelRelayStatus [_RELAY_SWITCH_NAME_R5])
  1448. {
  1449. if (regRelay.relay_event.bits.R5_BOTH_K3_K6 == NO)
  1450. outputRelay.relay_event.bits.R5_BOTH_K3_K6 = YES;
  1451. }
  1452. else
  1453. {
  1454. if (regRelay.relay_event.bits.R5_BOTH_K3_K6 == YES)
  1455. outputRelay.relay_event.bits.R5_BOTH_K3_K6 = NO;
  1456. }
  1457. }
  1458. }
  1459. }
  1460. unsigned char DetectBitValue(int _value, unsigned char _bit)
  1461. {
  1462. return ( _value & mask_table[_bit] ) != 0x00;
  1463. }
  1464. bool CheckAlarmOccur()
  1465. {
  1466. bool isErr = false;
  1467. for(byte count = 0; count < sizeof(mask_table)/sizeof(mask_table[0]); count++)
  1468. {
  1469. byte value = DetectBitValue(acAlarmCode.AcAlarmCode, count);
  1470. if (value == 1)
  1471. {
  1472. isErr = true;
  1473. switch(count)
  1474. {
  1475. case L1_AC_OVP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcSystemInputOVP = YES; break;
  1476. case L1_AC_UVP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcSystemInputUVP = YES; break;
  1477. case L1_AC_OCP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = YES; break;
  1478. case AC_OTP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAmbientOTP = YES; break;
  1479. case AC_GMI_FAULT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcGroundfaultFail = YES; break;
  1480. case AC_CP_ERROR: ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault = YES; break;
  1481. case AC_AC_LEAKAGE: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip = YES; break;
  1482. case AC_DC_LEAKAGE: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip = YES; break;
  1483. case AC_SYSTEM_SELFTEST_FAULT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.McuSelftestFail = YES; break;
  1484. case AC_HANDSHAKE_TIMEOUT: break;
  1485. case AC_EMC_STOP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES; break;
  1486. case AC_RELAY_WELDING: ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayWelding = YES; break;
  1487. case AC_LEAK_MODULE_FAULT: ShmStatusCodeData->FaultCode.FaultEvents.bits.RcdSelfTestFail = YES; break;
  1488. case AC_SHUTTER_FAULT: ShmStatusCodeData->FaultCode.FaultEvents.bits.ShutterFault = YES; break;
  1489. case AC_LOCKER_FAULT: ShmStatusCodeData->FaultCode.FaultEvents.bits.AcConnectorLockFail = YES; break;
  1490. case AC_POWER_DROP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = YES; break;
  1491. case L1_AC_CIRCUIT_SHORT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CircuitShort = YES; break;
  1492. case AC_ROTARY_SWITCH_FAULT: ShmStatusCodeData->FaultCode.FaultEvents.bits.RotarySwitchFault = YES; break;
  1493. case AC_RELAY_DRIVE_FAULT: ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayDrivingFault = YES;break;
  1494. case RESERVED_1: break;
  1495. case L2_AC_OVP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcSystemInputOVP = YES; break;
  1496. case L3_AC_OVP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcSystemInputOVP = YES; break;
  1497. case L2_AC_UVP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcSystemInputUVP = YES; break;
  1498. case L3_AC_UVP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcSystemInputUVP = YES; break;
  1499. case L2_AC_OCP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = YES; break;
  1500. case L3_AC_OCP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = YES; break;
  1501. case L2_AC_CIRCUIT_SHORT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CircuitShort = YES; break;
  1502. case L3_AC_CIRCUIT_SHORT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CircuitShort = YES; break;
  1503. case AC_METER_TIMEOUT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MeterCommTimeout = YES; break;
  1504. case AC_METER_IC_COMM_TIMEOUT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MeterIcCommTimeout = YES; break;
  1505. case AC_CP_NEGATIVE_ERR: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PilotNegativeError = YES; break;
  1506. case RESERVED_2: break;
  1507. }
  1508. }
  1509. }
  1510. if (!isErr)
  1511. {
  1512. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE ||
  1513. ac_chargingInfo [0]->SystemStatus == SYS_MODE_MAINTAIN ||
  1514. ac_chargingInfo [0]->SystemStatus == SYS_MODE_RESERVATION)
  1515. {
  1516. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcSystemInputOVP = NO;
  1517. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcSystemInputUVP = NO;
  1518. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = NO;
  1519. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAmbientOTP = NO;
  1520. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcGroundfaultFail = NO;
  1521. ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault = NO;
  1522. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip = NO;
  1523. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.McuSelftestFail = NO;
  1524. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO;
  1525. ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayWelding = NO;
  1526. ShmStatusCodeData->FaultCode.FaultEvents.bits.RcdSelfTestFail = NO;
  1527. ShmStatusCodeData->FaultCode.FaultEvents.bits.ShutterFault = NO;
  1528. ShmStatusCodeData->FaultCode.FaultEvents.bits.AcConnectorLockFail = NO;
  1529. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = NO;
  1530. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CircuitShort = NO;
  1531. ShmStatusCodeData->FaultCode.FaultEvents.bits.RotarySwitchFault = NO;
  1532. ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayDrivingFault = NO;
  1533. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MeterCommTimeout = NO;
  1534. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MeterIcCommTimeout = NO;
  1535. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PilotNegativeError = NO;
  1536. }
  1537. }
  1538. ac_chargingInfo[0]->IsErrorOccur = isErr;
  1539. return isErr;
  1540. }
  1541. bool IsNoneMatchLedColor()
  1542. {
  1543. bool result = false;
  1544. if (cur_led_color.Connect_1_Red != led_color.Connect_1_Red ||
  1545. cur_led_color.Connect_1_Green != led_color.Connect_1_Green ||
  1546. cur_led_color.Connect_1_Blue != led_color.Connect_1_Blue ||
  1547. cur_led_color.Connect_2_Red != led_color.Connect_2_Red ||
  1548. cur_led_color.Connect_2_Green != led_color.Connect_2_Green ||
  1549. cur_led_color.Connect_2_Blue != led_color.Connect_2_Blue)
  1550. {
  1551. result = true;
  1552. }
  1553. return result;
  1554. }
  1555. void SetLedColor(struct ChargingInfoData *chargingData_1, struct ChargingInfoData *chargingData_2)
  1556. {
  1557. byte _colorBuf = COLOR_MAX_LV * LED_INTENSITY_BRIGHTEST;
  1558. if (ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity == _LED_INTENSITY_DARKEST)
  1559. _colorBuf = COLOR_MAX_LV * LED_INTENSITY_DARKEST;
  1560. else if (ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity == _LED_INTENSITY_MEDIUM)
  1561. _colorBuf = COLOR_MAX_LV * LED_INTENSITY_MEDIUM;
  1562. if (ShmSysConfigAndInfo->SysWarningInfo.Level == _ALARM_LEVEL_CRITICAL ||
  1563. (chargingData_1->SystemStatus == SYS_MODE_UPDATE && chargingData_2->SystemStatus == SYS_MODE_UPDATE))
  1564. {
  1565. led_color.Connect_1_Green = COLOR_MIN_LV;
  1566. led_color.Connect_1_Blue = COLOR_MIN_LV;
  1567. led_color.Connect_1_Red = _colorBuf;
  1568. led_color.Connect_2_Green = COLOR_MIN_LV;
  1569. led_color.Connect_2_Blue = COLOR_MIN_LV;
  1570. led_color.Connect_2_Red = _colorBuf;
  1571. }
  1572. else
  1573. {
  1574. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  1575. {
  1576. if (ShmDcCommonData->_isPsuErrorOccur)
  1577. {
  1578. led_color.Connect_1_Green = COLOR_MIN_LV;
  1579. led_color.Connect_1_Blue = COLOR_MIN_LV;
  1580. led_color.Connect_1_Red = _colorBuf;
  1581. led_color.Connect_2_Green = COLOR_MIN_LV;
  1582. led_color.Connect_2_Blue = COLOR_MIN_LV;
  1583. led_color.Connect_2_Red = _colorBuf;
  1584. }
  1585. else if ((chargingData_1->SystemStatus == SYS_MODE_BOOTING || chargingData_1->SystemStatus == SYS_MODE_IDLE || chargingData_1->SystemStatus == SYS_MODE_RESERVATION || chargingData_1->SystemStatus == SYS_MODE_MAINTAIN) &&
  1586. (chargingData_2->SystemStatus == SYS_MODE_BOOTING || chargingData_2->SystemStatus == SYS_MODE_IDLE || chargingData_2->SystemStatus == SYS_MODE_RESERVATION || chargingData_2->SystemStatus == SYS_MODE_MAINTAIN))
  1587. {
  1588. led_color.Connect_1_Green = _colorBuf;
  1589. led_color.Connect_1_Blue = COLOR_MIN_LV;
  1590. led_color.Connect_1_Red = COLOR_MIN_LV;
  1591. led_color.Connect_2_Green = _colorBuf;
  1592. led_color.Connect_2_Blue = COLOR_MIN_LV;
  1593. led_color.Connect_2_Red = COLOR_MIN_LV;
  1594. if (chargingData_1->SystemStatus == SYS_MODE_RESERVATION ||
  1595. chargingData_2->SystemStatus == SYS_MODE_RESERVATION)
  1596. {
  1597. if (GetTimeoutValue(&_led_blink_time) > 3)
  1598. {
  1599. led_color.Connect_1_Green = COLOR_MIN_LV;
  1600. led_color.Connect_2_Green = COLOR_MIN_LV;
  1601. }
  1602. }
  1603. else if (chargingData_1->SystemStatus == SYS_MODE_MAINTAIN ||
  1604. chargingData_2->SystemStatus == SYS_MODE_MAINTAIN)
  1605. {
  1606. led_color.Connect_1_Green = COLOR_MIN_LV;
  1607. led_color.Connect_2_Green = COLOR_MIN_LV;
  1608. led_color.Connect_1_Red = _colorBuf;
  1609. led_color.Connect_2_Red = _colorBuf;
  1610. }
  1611. }
  1612. else if ((chargingData_1->SystemStatus >= SYS_MODE_AUTHORIZING && chargingData_1->SystemStatus <= SYS_MODE_COMPLETE) ||
  1613. (chargingData_1->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingData_1->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1) ||
  1614. (chargingData_2->SystemStatus >= SYS_MODE_AUTHORIZING && chargingData_2->SystemStatus <= SYS_MODE_COMPLETE) ||
  1615. (chargingData_2->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingData_2->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  1616. {
  1617. led_color.Connect_1_Green = COLOR_MIN_LV;
  1618. led_color.Connect_1_Blue = _colorBuf;
  1619. led_color.Connect_1_Red = COLOR_MIN_LV;
  1620. led_color.Connect_2_Green = COLOR_MIN_LV;
  1621. led_color.Connect_2_Blue = _colorBuf;
  1622. led_color.Connect_2_Red = COLOR_MIN_LV;
  1623. }
  1624. }
  1625. else
  1626. {
  1627. if (ShmDcCommonData->_isPsuErrorOccur)
  1628. {
  1629. if (chargingData_1->SystemStatus == SYS_MODE_BOOTING ||
  1630. chargingData_1->SystemStatus == SYS_MODE_IDLE ||
  1631. chargingData_1->SystemStatus == SYS_MODE_RESERVATION ||
  1632. chargingData_1->SystemStatus == SYS_MODE_MAINTAIN)
  1633. {
  1634. led_color.Connect_1_Green = COLOR_MIN_LV;
  1635. led_color.Connect_1_Blue = COLOR_MIN_LV;
  1636. led_color.Connect_1_Red = _colorBuf;
  1637. }
  1638. }
  1639. else if (chargingData_1->SystemStatus == SYS_MODE_BOOTING ||
  1640. chargingData_1->SystemStatus == SYS_MODE_IDLE ||
  1641. chargingData_1->SystemStatus == SYS_MODE_RESERVATION ||
  1642. chargingData_1->SystemStatus == SYS_MODE_MAINTAIN)
  1643. {
  1644. led_color.Connect_1_Green = _colorBuf;
  1645. led_color.Connect_1_Blue = COLOR_MIN_LV;
  1646. led_color.Connect_1_Red = COLOR_MIN_LV;
  1647. if (chargingData_1->SystemStatus == SYS_MODE_RESERVATION)
  1648. {
  1649. if (GetTimeoutValue(&_led_blink_time) > 3)
  1650. {
  1651. led_color.Connect_1_Green = COLOR_MIN_LV;
  1652. }
  1653. }
  1654. else if (chargingData_1->SystemStatus == SYS_MODE_MAINTAIN)
  1655. {
  1656. led_color.Connect_1_Green = COLOR_MIN_LV;
  1657. led_color.Connect_1_Red = _colorBuf;
  1658. }
  1659. }
  1660. else if ((chargingData_1->SystemStatus >= SYS_MODE_AUTHORIZING && chargingData_1->SystemStatus <= SYS_MODE_COMPLETE) ||
  1661. (chargingData_1->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingData_1->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  1662. {
  1663. led_color.Connect_1_Green = COLOR_MIN_LV;
  1664. led_color.Connect_1_Blue = _colorBuf;
  1665. led_color.Connect_1_Red = COLOR_MIN_LV;
  1666. }
  1667. // --------------------------------------------------------------------------
  1668. if (ShmDcCommonData->_isPsuErrorOccur)
  1669. {
  1670. if (chargingData_2->SystemStatus == SYS_MODE_BOOTING ||
  1671. chargingData_2->SystemStatus == SYS_MODE_IDLE ||
  1672. chargingData_2->SystemStatus == SYS_MODE_RESERVATION ||
  1673. chargingData_2->SystemStatus == SYS_MODE_MAINTAIN)
  1674. {
  1675. led_color.Connect_2_Green = COLOR_MIN_LV;
  1676. led_color.Connect_2_Blue = COLOR_MIN_LV;
  1677. led_color.Connect_2_Red = _colorBuf;
  1678. }
  1679. }
  1680. else if (chargingData_2->SystemStatus == SYS_MODE_BOOTING ||
  1681. chargingData_2->SystemStatus == SYS_MODE_IDLE ||
  1682. chargingData_2->SystemStatus == SYS_MODE_RESERVATION ||
  1683. chargingData_2->SystemStatus == SYS_MODE_MAINTAIN)
  1684. {
  1685. led_color.Connect_2_Green = _colorBuf;
  1686. led_color.Connect_2_Blue = COLOR_MIN_LV;
  1687. led_color.Connect_2_Red = COLOR_MIN_LV;
  1688. if (chargingData_2->SystemStatus == SYS_MODE_RESERVATION)
  1689. {
  1690. if (GetTimeoutValue(&_led_blink_time) > 3)
  1691. {
  1692. led_color.Connect_2_Green = COLOR_MIN_LV;
  1693. }
  1694. }
  1695. else if (chargingData_2->SystemStatus == SYS_MODE_MAINTAIN)
  1696. {
  1697. led_color.Connect_2_Green = COLOR_MIN_LV;
  1698. led_color.Connect_2_Red = _colorBuf;
  1699. }
  1700. }
  1701. else if ((chargingData_2->SystemStatus >= SYS_MODE_AUTHORIZING && chargingData_2->SystemStatus <= SYS_MODE_COMPLETE) ||
  1702. (chargingData_2->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && chargingData_2->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  1703. {
  1704. led_color.Connect_2_Green = COLOR_MIN_LV;
  1705. led_color.Connect_2_Blue = _colorBuf;
  1706. led_color.Connect_2_Red = COLOR_MIN_LV;
  1707. }
  1708. }
  1709. }
  1710. if (_checkLedChanged > 0)
  1711. {
  1712. if (Config_Led_Color(Uart5Fd, Addr.Led, &led_color) == PASS)
  1713. {
  1714. _checkLedChanged--;
  1715. cur_led_color.Connect_1_Red = led_color.Connect_1_Red;
  1716. cur_led_color.Connect_1_Green = led_color.Connect_1_Green;
  1717. cur_led_color.Connect_1_Blue = led_color.Connect_1_Blue;
  1718. cur_led_color.Connect_2_Red = led_color.Connect_2_Red;
  1719. cur_led_color.Connect_2_Green = led_color.Connect_2_Green;
  1720. cur_led_color.Connect_2_Blue = led_color.Connect_2_Blue;
  1721. }
  1722. }
  1723. else if (IsNoneMatchLedColor())
  1724. _checkLedChanged = 5;
  1725. }
  1726. //==========================================
  1727. // Init all share memory
  1728. //==========================================
  1729. int InitShareMemory()
  1730. {
  1731. int result = PASS;
  1732. int MeterSMId;
  1733. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  1734. {
  1735. #ifdef SystemLogMessage
  1736. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1737. #endif
  1738. result = FAIL;
  1739. }
  1740. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1741. {
  1742. #ifdef SystemLogMessage
  1743. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  1744. #endif
  1745. result = FAIL;
  1746. }
  1747. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  1748. {
  1749. #ifdef SystemLogMessage
  1750. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1751. #endif
  1752. result = FAIL;
  1753. }
  1754. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1755. {
  1756. #ifdef SystemLogMessage
  1757. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1758. #endif
  1759. result = FAIL;
  1760. }
  1761. if(CCS_QUANTITY > 0)
  1762. {
  1763. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  1764. {
  1765. #ifdef SystemLogMessage
  1766. DEBUG_ERROR("shmget ShmCcsData NG \n");
  1767. #endif
  1768. return FAIL;
  1769. }
  1770. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1771. {
  1772. #ifdef SystemLogMessage
  1773. DEBUG_ERROR("shmat ShmCcsData NG \n");
  1774. #endif
  1775. return FAIL;
  1776. }
  1777. }
  1778. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), 0777)) < 0)
  1779. {
  1780. #ifdef SystemLogMessage
  1781. DEBUG_ERROR("shmget ShmFanModuleData NG\n");
  1782. #endif
  1783. result = FAIL;
  1784. }
  1785. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1786. {
  1787. #ifdef SystemLogMessage
  1788. DEBUG_ERROR("shmat ShmFanModuleData NG\n");
  1789. #endif
  1790. result = FAIL;
  1791. }
  1792. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), 0777)) < 0)
  1793. {
  1794. #ifdef SystemLogMessage
  1795. DEBUG_ERROR("shmget ShmRelayModuleData NG\n");
  1796. #endif
  1797. result = FAIL;
  1798. }
  1799. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1800. {
  1801. #ifdef SystemLogMessage
  1802. DEBUG_ERROR("shmat ShmRelayModuleData NG\n");
  1803. #endif
  1804. result = FAIL;
  1805. }
  1806. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), 0777)) < 0)
  1807. {
  1808. #ifdef SystemLogMessage
  1809. DEBUG_ERROR("shmget ShmLedModuleData NG\n");
  1810. #endif
  1811. result = FAIL;
  1812. }
  1813. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1814. {
  1815. #ifdef SystemLogMessage
  1816. DEBUG_ERROR("shmat ShmLedModuleData NG\n");
  1817. #endif
  1818. result = FAIL;
  1819. }
  1820. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  1821. {
  1822. #ifdef SystemLogMessage
  1823. DEBUG_ERROR("shmget ShmPsuData NG \n");
  1824. #endif
  1825. result = FAIL;
  1826. }
  1827. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1828. {
  1829. #ifdef SystemLogMessage
  1830. DEBUG_ERROR("shmat ShmPsuData NG \n");
  1831. #endif
  1832. result = FAIL;
  1833. }
  1834. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  1835. {
  1836. #ifdef SystemLogMessage
  1837. DEBUG_ERROR("shmat ShmOCPP16Data NG \n");
  1838. #endif
  1839. result = FAIL;
  1840. }
  1841. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1842. {
  1843. #ifdef SystemLogMessage
  1844. DEBUG_ERROR("shmat ShmOCPP16Data NG \n");
  1845. #endif
  1846. result = FAIL;
  1847. }
  1848. if ((MeterSMId = shmget(ShmOcpp20ModuleKey, sizeof(struct OCPP20Data), IPC_CREAT | 0777)) < 0)
  1849. {
  1850. #ifdef SystemLogMessage
  1851. DEBUG_ERROR("shmat ShmOCPP20Data NG \n");
  1852. #endif
  1853. result = FAIL;
  1854. }
  1855. else if ((ShmOCPP20Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1856. {
  1857. #ifdef SystemLogMessage
  1858. DEBUG_ERROR("shmat ShmOCPP20Data NG \n");
  1859. #endif
  1860. result = FAIL;
  1861. }
  1862. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0)
  1863. {
  1864. #ifdef SystemLogMessage
  1865. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  1866. #endif
  1867. result = FAIL;
  1868. }
  1869. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1870. {
  1871. #ifdef ShmPrimaryMcuData
  1872. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  1873. #endif
  1874. result = FAIL;
  1875. }
  1876. if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
  1877. {
  1878. #ifdef SystemLogMessage
  1879. DEBUG_ERROR("shmget ShmCommonKey NG \n");
  1880. #endif
  1881. return 0;
  1882. }
  1883. else if ((ShmDcCommonData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1884. {
  1885. #ifdef SystemLogMessage
  1886. DEBUG_ERROR("shmat ShmCommonKey NG \n");
  1887. #endif
  1888. return 0;
  1889. }
  1890. if ((MeterSMId = shmget ( ShmSmartBoxKey, sizeof(struct SmartBoxData), IPC_CREAT | 0777 )) < 0)
  1891. {
  1892. #ifdef SystemLogMessage
  1893. DEBUG_ERROR("shmat ShmSmartBoxKey NG \n");
  1894. #endif
  1895. return 0;
  1896. }
  1897. else if ((ShmSmartBoxData = shmat ( MeterSMId, NULL, 0 )) == (void *) - 1)
  1898. {
  1899. #ifdef SystemLogMessage
  1900. DEBUG_ERROR("shmat ShmSmartBoxData NG \n");
  1901. #endif
  1902. return 0;
  1903. }
  1904. return result;
  1905. }
  1906. int InitComPort()
  1907. {
  1908. int fd;
  1909. struct termios tios;
  1910. fd = open(relayRs485PortName, O_RDWR);
  1911. if(fd <= 0)
  1912. {
  1913. #ifdef SystemLogMessage
  1914. DEBUG_ERROR("Module_InternalComm. InitComPort NG\n");
  1915. #endif
  1916. if(ShmStatusCodeData!=NULL)
  1917. {
  1918. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  1919. }
  1920. sleep(5);
  1921. return -1;
  1922. }
  1923. ioctl (fd, TCGETS, &tios);
  1924. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  1925. tios.c_lflag = 0;
  1926. tios.c_iflag = 0;
  1927. tios.c_oflag = 0;
  1928. tios.c_cc[VMIN]=0;
  1929. tios.c_cc[VTIME]=(byte)0; // timeout 0.5 second
  1930. tios.c_lflag=0;
  1931. tcflush(fd, TCIFLUSH);
  1932. ioctl (fd, TCSETS, &tios);
  1933. return fd;
  1934. }
  1935. //================================================
  1936. // Main process
  1937. //================================================
  1938. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  1939. {
  1940. for (byte index = 0; index < CHAdeMO_QUANTITY; index++) {
  1941. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index
  1942. == target) {
  1943. chargingData[target] =
  1944. &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  1945. return true;
  1946. }
  1947. }
  1948. for (byte index = 0; index < CCS_QUANTITY; index++) {
  1949. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index
  1950. == target) {
  1951. chargingData[target] =
  1952. &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  1953. return true;
  1954. }
  1955. }
  1956. for (byte index = 0; index < GB_QUANTITY; index++) {
  1957. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index
  1958. == target) {
  1959. chargingData[target] =
  1960. &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  1961. return true;
  1962. }
  1963. }
  1964. return false;
  1965. }
  1966. bool FindAcChargingInfoData(byte target, struct ChargingInfoData **acChargingData)
  1967. {
  1968. if (target < AC_QUANTITY)
  1969. {
  1970. acChargingData[target] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[target];
  1971. return true;
  1972. }
  1973. return false;
  1974. }
  1975. void Initialization()
  1976. {
  1977. bool isPass = false;
  1978. isAcModuleInitComp = false;
  1979. for (byte index = 0; index < ARRAY_SIZE(outputRelay.relay_event.relay_status); index++)
  1980. {
  1981. outputRelay.relay_event.relay_status[index] = 0x00;
  1982. }
  1983. while(!isPass)
  1984. {
  1985. isPass = true;
  1986. for (byte _index = 0; _index < gunCount; _index++)
  1987. {
  1988. if (!FindChargingInfoData(_index, &_chargingData[0]))
  1989. {
  1990. DEBUG_ERROR("InternalComm : FindChargingInfoData false (%d) \n", gunCount);
  1991. isPass = false;
  1992. break;
  1993. }
  1994. }
  1995. sleep(1);
  1996. }
  1997. isPass = false;
  1998. if (acgunCount > 0)
  1999. {
  2000. while(!isPass)
  2001. {
  2002. isPass = true;
  2003. for (byte _index = 0; _index < acgunCount; _index++)
  2004. {
  2005. if (!FindAcChargingInfoData(_index, &ac_chargingInfo[0]))
  2006. {
  2007. DEBUG_ERROR("EvComm : FindAcChargingInfoData false \n");
  2008. isPass = false;
  2009. break;
  2010. }
  2011. }
  2012. sleep(1);
  2013. }
  2014. }
  2015. }
  2016. void StartCheckRelayInfo(byte _chkIndex, byte toState)
  2017. {
  2018. if (ShmDcCommonData->RelayCheckStatus[_chkIndex] == STOP)
  2019. {
  2020. GetTimespecFunc(&_relayStateChkTimer[_chkIndex]);
  2021. ShmDcCommonData->RelayCheckStatus[_chkIndex] = START;
  2022. }
  2023. else
  2024. {
  2025. int _timebuf = GetTimeoutValue(&_relayStateChkTimer[_chkIndex]);
  2026. if (_timebuf < 0)
  2027. GetTimespecFunc(&_relayStateChkTimer[_chkIndex]);
  2028. else
  2029. {
  2030. if (_timebuf > RELAY_CHECK_TIME)
  2031. {
  2032. //PRINTF_FUNC ("relay welding or driving fault = %d \n", _chkIndex);
  2033. if (toState == 1)
  2034. ShmDcCommonData->RelayCheckStatus[_chkIndex] = RELAY_STATUS_ERROR_DRIVING;
  2035. else
  2036. ShmDcCommonData->RelayCheckStatus[_chkIndex] = RELAY_STATUS_ERROR_WELDING;
  2037. }
  2038. }
  2039. }
  2040. }
  2041. void StopCheckRelayInfo(byte _chkIndex)
  2042. {
  2043. if (ShmDcCommonData->RelayCheckStatus[_chkIndex] != STOP)
  2044. {
  2045. ShmDcCommonData->RelayCheckStatus[_chkIndex] = STOP;
  2046. }
  2047. }
  2048. bool IsNoneMatchRelayStatus()
  2049. {
  2050. bool result = false;
  2051. if ((regRelay.relay_event.bits.AC_Contactor != outputRelay.relay_event.bits.AC_Contactor) ||
  2052. (regRelay.relay_event.bits.R3_BOTH_K1_K4 != outputRelay.relay_event.bits.R3_BOTH_K1_K4) ||
  2053. (regRelay.relay_event.bits.Gun1_P != outputRelay.relay_event.bits.Gun1_P) ||
  2054. (regRelay.relay_event.bits.Gun1_N != outputRelay.relay_event.bits.Gun1_N) ||
  2055. (regRelay.relay_event.bits.Gun2_P != outputRelay.relay_event.bits.Gun2_P) ||
  2056. (regRelay.relay_event.bits.Gun2_N != outputRelay.relay_event.bits.Gun2_N) ||
  2057. (regRelay.relay_event.bits.R4_BOTH_K2_K5 != outputRelay.relay_event.bits.R4_BOTH_K2_K5) ||
  2058. (regRelay.relay_event.bits.R5_BOTH_K3_K6 != outputRelay.relay_event.bits.R5_BOTH_K3_K6))
  2059. {
  2060. result = true;
  2061. }
  2062. if (regRelay.relay_event.bits.AC_Contactor != outputRelay.relay_event.bits.AC_Contactor)
  2063. {
  2064. //PRINTF_FUNC("AC Contact Relay none match, try to switch to %d \n", outputRelay.relay_event.bits.AC_Contactor);
  2065. }
  2066. if (regRelay.relay_event.bits.Gun1_P != outputRelay.relay_event.bits.Gun1_P)
  2067. {
  2068. StartCheckRelayInfo(RELAY_SMR1_P_STATUS, outputRelay.relay_event.bits.Gun1_P);
  2069. //PRINTF_FUNC("SMR1:D+ Relay none match, try to switch to %d \n", outputRelay.relay_event.bits.Gun1_P);
  2070. }
  2071. else
  2072. {
  2073. StopCheckRelayInfo(RELAY_SMR1_P_STATUS);
  2074. //PRINTF_FUNC("SMR1:D+ Release %d \n");
  2075. }
  2076. if (regRelay.relay_event.bits.Gun1_N != outputRelay.relay_event.bits.Gun1_N)
  2077. {
  2078. StartCheckRelayInfo(RELAY_SMR1_N_STATUS, outputRelay.relay_event.bits.Gun1_N);
  2079. //PRINTF_FUNC("SMR1:D- Relay none match, try to switch to %d \n", outputRelay.relay_event.bits.Gun1_N);
  2080. }
  2081. else
  2082. {
  2083. StopCheckRelayInfo(RELAY_SMR1_N_STATUS);
  2084. }
  2085. if (regRelay.relay_event.bits.Gun2_P != outputRelay.relay_event.bits.Gun2_P)
  2086. {
  2087. StartCheckRelayInfo(RELAY_SMR2_P_STATUS, outputRelay.relay_event.bits.Gun2_P);
  2088. //PRINTF_FUNC("SMR2:D+ Relay none match, try to switch to %d \n", outputRelay.relay_event.bits.Gun2_P);
  2089. }
  2090. else
  2091. {
  2092. StopCheckRelayInfo(RELAY_SMR2_P_STATUS);
  2093. }
  2094. if (regRelay.relay_event.bits.Gun2_N != outputRelay.relay_event.bits.Gun2_N)
  2095. {
  2096. StartCheckRelayInfo(RELAY_SMR2_N_STATUS, outputRelay.relay_event.bits.Gun2_N);
  2097. //PRINTF_FUNC("SMR2:D- Relay none match, try to switch to %d \n", outputRelay.relay_event.bits.Gun2_N);
  2098. }
  2099. else
  2100. {
  2101. StopCheckRelayInfo(RELAY_SMR2_N_STATUS);
  2102. }
  2103. if (regRelay.relay_event.bits.R4_BOTH_K2_K5 != outputRelay.relay_event.bits.R4_BOTH_K2_K5)
  2104. {
  2105. StartCheckRelayInfo(RELAY_PARA_P_STATUS, outputRelay.relay_event.bits.R4_BOTH_K2_K5);
  2106. //PRINTF_FUNC("Parallel:D+ Relay none match, try to switch to %d \n", outputRelay.relay_event.bits.Gun1_Parallel_P);
  2107. }
  2108. else
  2109. {
  2110. StopCheckRelayInfo(RELAY_PARA_P_STATUS);
  2111. }
  2112. if (regRelay.relay_event.bits.R5_BOTH_K3_K6 != outputRelay.relay_event.bits.R5_BOTH_K3_K6)
  2113. {
  2114. StartCheckRelayInfo(RELAY_PARA_N_STATUS, outputRelay.relay_event.bits.R5_BOTH_K3_K6);
  2115. //PRINTF_FUNC("Parallel:D- Relay none match, try to switch to %d \n", outputRelay.relay_event.bits.Gun1_Parallel_N);
  2116. }
  2117. else
  2118. {
  2119. StopCheckRelayInfo(RELAY_PARA_N_STATUS);
  2120. }
  2121. if (regRelay.relay_event.bits.R3_BOTH_K1_K4 != outputRelay.relay_event.bits.R3_BOTH_K1_K4)
  2122. {
  2123. StartCheckRelayInfo(RELAY_PRE_STATUS, outputRelay.relay_event.bits.R3_BOTH_K1_K4);
  2124. //PRINTF_FUNC("Parallel:D- Relay none match, try to switch to %d \n", outputRelay.relay_event.bits.Gun1_Parallel_N);
  2125. }
  2126. else
  2127. {
  2128. StopCheckRelayInfo(RELAY_PRE_STATUS);
  2129. }
  2130. return result;
  2131. }
  2132. void MatchRelayStatus()
  2133. {
  2134. // 因為 AC Contactor 沒有 Feedback,所以暫時先這樣處理
  2135. //regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
  2136. ShmSysConfigAndInfo->SysInfo.AcContactorStatus = regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
  2137. regRelay.relay_event.bits.R5_BOTH_K3_K6 = outputRelay.relay_event.bits.R5_BOTH_K3_K6;
  2138. regRelay.relay_event.bits.Gun1_P = outputRelay.relay_event.bits.Gun1_P;
  2139. regRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_N;
  2140. regRelay.relay_event.bits.Gun2_P = outputRelay.relay_event.bits.Gun2_P;
  2141. regRelay.relay_event.bits.Gun2_N = outputRelay.relay_event.bits.Gun2_N;
  2142. regRelay.relay_event.bits.R4_BOTH_K2_K5 = outputRelay.relay_event.bits.R4_BOTH_K2_K5;
  2143. regRelay.relay_event.bits.R3_BOTH_K1_K4 = outputRelay.relay_event.bits.R3_BOTH_K1_K4;
  2144. }
  2145. void CheckRelayStatusByADC()
  2146. {
  2147. if (ShmRelayModuleData->Gun1FuseOutputVolt > 0 && ShmRelayModuleData->Gun1RelayOutputVolt > 0 &&
  2148. (ShmRelayModuleData->Gun1FuseOutputVolt == ShmRelayModuleData->Gun1RelayOutputVolt))
  2149. {
  2150. // Relay 前後電壓一致
  2151. _chargingData[0]->RelayK1K2Status = 0x01;
  2152. }
  2153. else
  2154. _chargingData[0]->RelayK1K2Status = 0x00;
  2155. if (ShmRelayModuleData->Gun2FuseOutputVolt > 0 && ShmRelayModuleData->Gun2RelayOutputVolt > 0 &&
  2156. (ShmRelayModuleData->Gun2FuseOutputVolt == ShmRelayModuleData->Gun2RelayOutputVolt))
  2157. {
  2158. // Relay 前後電壓一致
  2159. _chargingData[1]->RelayK1K2Status = 0x01;
  2160. }
  2161. else
  2162. _chargingData[1]->RelayK1K2Status = 0x00;
  2163. }
  2164. void SetGfdConfig(byte index, byte resister)
  2165. {
  2166. gfd_config.index = index;
  2167. gfd_config.state = resister;
  2168. //PRINTF_FUNC("************************GFD Vol = %d, GFD Res = %d \n", gfd_config.reqVol, gfd_config.resister);
  2169. if (Config_Gfd_Value(Uart5Fd, Addr.Relay, &gfd_config) == PASS)
  2170. {
  2171. // PRINTF_FUNC("Set reqVol = %f, resister = %d \n",
  2172. // gfd_config.reqVol,
  2173. // gfd_config.resister);
  2174. }
  2175. }
  2176. //===============================================
  2177. // Common Detect Chk - CCS
  2178. //===============================================
  2179. byte isPrechargeStatus_ccs(byte gunIndex)
  2180. {
  2181. byte result = 0x00;
  2182. if (ShmCcsData->CommProtocol == _CCS_COMM_V2GMessage_DIN70121)
  2183. {
  2184. result = ShmCcsData->V2GMessage_DIN70121[_chargingData[gunIndex]->type_index].PresentMsgFlowStatus;
  2185. }
  2186. return result;
  2187. }
  2188. void CableCheckDetected(byte index)
  2189. {
  2190. // Cable Check
  2191. // 當火線上的電壓 = 車端要求的電壓電流
  2192. // _chargingData[targetGun]->EvBatterytargetVoltage
  2193. // 主要驗證阻值為 100 與 500 當小於 100 * 950 時,為 Alarm,介於 100 * 950 與 500 * 950 時為 Warning
  2194. // 950 為系統最大電壓
  2195. // Alarm : Rgfd <= 95K
  2196. // Warning : 95K < Rgfd <= 475
  2197. // Normal : Rgfd > 475K
  2198. if ((_chargingData[index]->Type >= _Type_Chademo && _chargingData[index]->Type <= _Type_GB) ||
  2199. (_chargingData[index]->Type == 0x09 && ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag))
  2200. {
  2201. if ((_chargingData[index]->SystemStatus >= SYS_MODE_PREPARE_FOR_EVSE && _chargingData[index]->SystemStatus < SYS_MODE_TERMINATING) ||
  2202. (_chargingData[index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && _chargingData[index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1))
  2203. {
  2204. if (_chargingData[index]->SystemStatus == SYS_MODE_PREPARE_FOR_EVSE)
  2205. {
  2206. SetGfdConfig(index, GFD_CABLECHK);
  2207. }
  2208. else if (_chargingData[index]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 &&
  2209. _chargingData[index]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1 &&
  2210. _chargingData[index]->PresentChargingVoltage >= CCS_PRECHARGE_VOL &&
  2211. isPrechargeStatus_ccs(index) >= CCS_PRECHARGE_STARTU)
  2212. {
  2213. SetGfdConfig(index, GFD_PRECHARGE);
  2214. }
  2215. else if (_chargingData[index]->SystemStatus >= SYS_MODE_CHARGING &&
  2216. _chargingData[index]->SystemStatus < SYS_MODE_TERMINATING)
  2217. {
  2218. //if (_chargingData[index]->Type == _Type_GB || _chargingData[index]->Type == _Type_Chademo)
  2219. if (_chargingData[index]->Type == _Type_GB)
  2220. SetGfdConfig(index, GFD_IDLE);
  2221. else
  2222. SetGfdConfig(index, GFD_CHARGING);
  2223. }
  2224. }
  2225. else
  2226. {
  2227. SetGfdConfig(index, GFD_IDLE);
  2228. }
  2229. }
  2230. }
  2231. void CheckOutputPowerOverCarReq(byte index)
  2232. {
  2233. float fireV = _chargingData[index]->FireChargingVoltage;
  2234. float carV = _chargingData[index]->EvBatteryMaxVoltage * 10 > _chargingData[index]->ConnectorMaxVoltage ?
  2235. _chargingData[index]->ConnectorMaxVoltage : _chargingData[index]->EvBatteryMaxVoltage * 10;
  2236. if ((_chargingData[index]->EvBatterytargetVoltage * 10) > 1500 &&
  2237. (_chargingData[index]->Type == _Type_Chademo ||
  2238. _chargingData[index]->Type == _Type_CCS ||
  2239. _chargingData[index]->Type == _Type_GB))
  2240. {
  2241. if (fireV >= (carV + (carV * 0.02)))
  2242. {
  2243. if (!_isOvpChkTimeFlag[index])
  2244. {
  2245. if ((_chargingData[index]->PresentChargingVoltage * 10) >= VOUT_MIN_VOLTAGE * 10)
  2246. {
  2247. GetTimespecMFunc(&_checkOutputVolProtectTimer[index]);
  2248. _isOvpChkTimeFlag[index] = YES;
  2249. PRINTF_FUNC("First time : CheckOutputPowerOverCarReq NG : fire = %f, req = %f, max-battery = %f \n",
  2250. _chargingData[index]->FireChargingVoltage, (_chargingData[index]->EvBatterytargetVoltage * 10), carV);
  2251. }
  2252. }
  2253. else
  2254. {
  2255. int _timebuf = GetTimeoutMValue(&_checkOutputVolProtectTimer[index]);
  2256. if (_timebuf < 0)
  2257. GetTimespecMFunc(&_checkOutputVolProtectTimer[index]);
  2258. else
  2259. {
  2260. if (GetTimeoutMValue(&_checkOutputVolProtectTimer[index]) >= OUTPUT_VOL_CHK_TIME)
  2261. {
  2262. PRINTF_FUNC("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, req = %f, max-battery = %f \n",
  2263. _chargingData[index]->FireChargingVoltage, (_chargingData[index]->EvBatterytargetVoltage * 10), carV);
  2264. DEBUG_ERROR("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, req = %f, max-battery = %f \n",
  2265. _chargingData[index]->FireChargingVoltage, (_chargingData[index]->EvBatterytargetVoltage * 10), carV);
  2266. if (_chargingData[index]->Type == _Type_Chademo)
  2267. {
  2268. ShmDcCommonData->ConnectErrList[index].GunBits.ChaConnectOVP = YES;
  2269. if (strncmp((char *)_chargingData[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2270. memcpy(_chargingData[index]->ConnectorAlarmCode, "012217", 6);
  2271. }
  2272. else if (_chargingData[index]->Type == _Type_CCS)
  2273. {
  2274. ShmDcCommonData->ConnectErrList[index].GunBits.CCSConnectOVP = YES;
  2275. if (strncmp((char *)_chargingData[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2276. memcpy(_chargingData[index]->ConnectorAlarmCode, "012219", 6);
  2277. }
  2278. else if (_chargingData[index]->Type == _Type_GB)
  2279. {
  2280. ShmDcCommonData->ConnectErrList[index].GunBits.GBTConnectOVP = YES;
  2281. if (strncmp((char *)_chargingData[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2282. memcpy(_chargingData[index]->ConnectorAlarmCode, "012221", 6);
  2283. }
  2284. _chargingData[index]->StopChargeFlag = YES;
  2285. }
  2286. }
  2287. }
  2288. }
  2289. else
  2290. _isOvpChkTimeFlag[index] = NO;
  2291. }
  2292. }
  2293. void CheckOutputVolNoneMatchFire(byte index)
  2294. {
  2295. if (((_chargingData[index]->EvBatterytargetVoltage * 10) > 1500 ||
  2296. _chargingData[index]->RelayK1K2Status == NO) &&
  2297. (_chargingData[index]->Type == _Type_CCS))
  2298. {
  2299. if (((_chargingData[index]->PresentChargingVoltage * 10) < _chargingData[index]->FireChargingVoltage - 300) ||
  2300. ((_chargingData[index]->PresentChargingVoltage * 10) > _chargingData[index]->FireChargingVoltage + 300))
  2301. {
  2302. if (!_isOutputNoneMatch[index])
  2303. {
  2304. _isOutputNoneMatch[index] = YES;
  2305. GetTimespecFunc(&_checkOutputNoneMatchTimer[index]);
  2306. }
  2307. else
  2308. {
  2309. if (GetTimeoutValue(&_checkOutputNoneMatchTimer[index]) >= 2)
  2310. {
  2311. PRINTF_FUNC("Abnormal voltage on the Output at the stage of GFD. (%d) : pre = %.1f, fire = %.1f \n",
  2312. index, (_chargingData[index]->PresentChargingVoltage * 10), _chargingData[index]->FireChargingVoltage);
  2313. if (index == GUN_LEFT)
  2314. {
  2315. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_1 = YES;
  2316. if (strncmp((char *)_chargingData[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2317. memcpy(_chargingData[index]->ConnectorAlarmCode, "012324", 6);
  2318. }
  2319. else if (index == GUN_RIGHT)
  2320. {
  2321. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AbnormalVoltageOnOutputLine_2 = YES;
  2322. if (strncmp((char *)_chargingData[index]->ConnectorAlarmCode, "", 6) == EQUAL)
  2323. memcpy(_chargingData[index]->ConnectorAlarmCode, "012325", 6);
  2324. }
  2325. _chargingData[index]->StopChargeFlag = YES;
  2326. }
  2327. }
  2328. }
  2329. else
  2330. _isOutputNoneMatch[index] = NO;
  2331. }
  2332. }
  2333. void GetPsuTempForFanSpeed()
  2334. {
  2335. char temp = 0;
  2336. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  2337. {
  2338. for (byte count = 0; count < ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity; count++)
  2339. {
  2340. if (temp < ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp)
  2341. temp = ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp;
  2342. }
  2343. }
  2344. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = temp;
  2345. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  2346. {
  2347. if (ShmFanModuleData->TestFanSpeed == NORMAL_FAN_SPEED)
  2348. {
  2349. if (temp >= ENV_TEMP_MAX)
  2350. ShmFanModuleData->TestFanSpeed = MAX_FAN_SPEED;
  2351. }
  2352. else if (ShmFanModuleData->TestFanSpeed == MAX_FAN_SPEED)
  2353. {
  2354. if (temp <= ENV_TEMP_MIN)
  2355. ShmFanModuleData->TestFanSpeed = NORMAL_FAN_SPEED;
  2356. }
  2357. else
  2358. ShmFanModuleData->TestFanSpeed = NORMAL_FAN_SPEED;
  2359. }
  2360. }
  2361. void GetFanSpeedByFunction()
  2362. {
  2363. // 風控修改 :
  2364. // ******************************************************* //
  2365. //
  2366. // 當前PSU輸出總 KW PSU Temp
  2367. // 50 x -------------------- x ---------- + 0.5 x (PSU Temp - 70)
  2368. // 當前樁最大功率 KW 50
  2369. //
  2370. // ******************************************************* //
  2371. // 當前樁最大功率 KW : ShmPsuData->SystemAvailablePower
  2372. unsigned int _maxPower = ShmPsuData->SystemAvailablePower;
  2373. // 當前PSU輸出總 KW & PSU Temp :
  2374. unsigned char temp = 0;
  2375. float power = 0;
  2376. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  2377. {
  2378. for (byte count = 0; count < ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity; count++)
  2379. {
  2380. if (temp < ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp)
  2381. temp = ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp;
  2382. }
  2383. }
  2384. for (byte gunIndex = 0; gunIndex < gunCount; gunIndex++)
  2385. {
  2386. power += (_chargingData[gunIndex]->PresentChargingPower * 10);
  2387. }
  2388. double _pw_rate = 0;
  2389. if (_maxPower > 0)
  2390. _pw_rate = power / (double)_maxPower;
  2391. double _temp_rate = 0;
  2392. if (temp > 0)
  2393. _temp_rate = (double)temp / 50;
  2394. unsigned char _temp_diff = 0;
  2395. if (temp > 70)
  2396. _temp_diff = temp - 70;
  2397. // debug mode 直接印出資訊後離開
  2398. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  2399. {
  2400. // printf("Fan Speed Information: power = %f, _maxPower = %d, temp = %d -- _pw_rate = %f, _temp_rate = %f, _temp_diff = %d \n",
  2401. // power, _maxPower, temp, _pw_rate, _temp_rate, _temp_diff);
  2402. return;
  2403. }
  2404. ShmFanModuleData->TestFanSpeed = (((50 * _pw_rate * _temp_rate) + (0.5 * _temp_diff)) / 100) * MAX_FAN_SPEED;
  2405. // 日規 50KW 系統~ 只能限制 3000 轉輸出
  2406. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_JARI)
  2407. {
  2408. if (is50kwSystem)
  2409. {
  2410. // 50KW 系統,使用 14000 轉風扇
  2411. // 如果輸出大於 2.5KW
  2412. if (ShmFanModuleData->TestFanSpeed > 5000 ||
  2413. power >= 25)
  2414. ShmFanModuleData->TestFanSpeed = 5000;
  2415. }
  2416. else
  2417. {
  2418. // 日規其他系統,使用 4400 轉風扇
  2419. if (ShmFanModuleData->TestFanSpeed > 9545)
  2420. ShmFanModuleData->TestFanSpeed = 9545;
  2421. }
  2422. }
  2423. if (ShmFanModuleData->TestFanSpeed > MAX_FAN_SPEED)
  2424. ShmFanModuleData->TestFanSpeed = MAX_FAN_SPEED;
  2425. if (ShmFanModuleData->TestFanSpeed < 0)
  2426. ShmFanModuleData->TestFanSpeed = 0;
  2427. // -----------------------------------------------------------------------
  2428. // printf("power = %f \n", power);
  2429. // printf("_maxPower = %d \n", _maxPower);
  2430. // printf("temp = %d \n", temp);
  2431. //
  2432. // printf("_pw_rate = %f \n", _pw_rate);
  2433. // printf("_temp_rate = %f \n", _temp_rate);
  2434. // printf("_temp_diff = %d \n", _temp_diff);
  2435. // printf("fan rate = %f \n", (30 * _pw_rate * _temp_rate + 14 * _temp_diff));
  2436. // printf("ShmFanModuleData->TestFanSpeed = %d \n", ShmFanModuleData->TestFanSpeed);
  2437. }
  2438. bool GetAcStatus()
  2439. {
  2440. bool isPass = false;
  2441. if (Query_AC_Status(Uart5Fd, Addr.AcPlug, &acStatus) == PASS)
  2442. {
  2443. if (acStatus.CpStatus != AC_SYS_NONE)
  2444. {
  2445. ShmSysConfigAndInfo->SysConfig.AcRatingCurrent = (unsigned short)acStatus.MaxCurrent;
  2446. //if(ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent == 0)
  2447. //ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent = ShmSysConfigAndInfo->SysConfig.AcRatingCurrent;
  2448. AcChargingRatingCurrent = ShmSysConfigAndInfo->SysConfig.AcRatingCurrent;
  2449. ac_chargingInfo[0]->ConnectorPlugIn = acStatus.CpStatus;
  2450. ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PilotState = acStatus.CpStatus;
  2451. isPass = true;
  2452. }
  2453. // if (acStatus.CpStatus == AC_SYS_F)
  2454. // {
  2455. // printf("PilotVol_P = %d \n", acStatus.PilotVol_P);
  2456. // printf("PilotVol_N = %d \n", acStatus.PilotVol_N);
  2457. // }
  2458. // PRINTF_FUNC("CpStatus = %d \n", acStatus.CpStatus);
  2459. // printf("CurLimit = %d \n", acStatus.CurLimit);
  2460. // printf("PilotVol_P = %d \n", acStatus.PilotVol_P);
  2461. // printf("PilotVol_N = %d \n", acStatus.PilotVol_N);
  2462. // printf("LockStatus = %d \n", acStatus.LockStatus);
  2463. // printf("RelayStatus = %d \n", acStatus.RelayStatus);
  2464. // printf("ShutterStatus = %d \n", acStatus.ShutterStatus);
  2465. // printf("MeterStatus = %d \n", acStatus.MeterStatus);
  2466. // printf("PpStatus = %d \n", acStatus.PpStatus);
  2467. // printf("MaxCurrent = %d \n", acStatus.MaxCurrent);
  2468. // printf("RotateSwitchStatus = %d \n", acStatus.RelayStatus);
  2469. // printf("============================== \n");
  2470. //ac_chargingInfo[0]->SystemStatus = acStatus.CpStatus;
  2471. }
  2472. // else
  2473. // PRINTF_FUNC("GetAcStatus return fail. \n");
  2474. return isPass;
  2475. }
  2476. void GetAcAlarmCode()
  2477. {
  2478. if (Query_AC_Alarm_Code(Uart5Fd, Addr.AcPlug, &acAlarmCode) == PASS)
  2479. {
  2480. ocpp_ac_clear_errorCode_cmd(CheckAlarmOccur());
  2481. }
  2482. }
  2483. unsigned char GetChargingEnergy()
  2484. {
  2485. return Query_Charging_Energy(Uart5Fd, Addr.AcPlug, &acChargingEnergy);
  2486. }
  2487. unsigned char GetAcChargingVoltage()
  2488. {
  2489. return Query_Present_InputVoltage(Uart5Fd, Addr.AcPlug, &acChargingVoltage);
  2490. }
  2491. unsigned char GetAcChargingCurrent()
  2492. {
  2493. return Query_Charging_Current(Uart5Fd, Addr.AcPlug, &acChargingCurrent);
  2494. }
  2495. void ChangeLedStatus()
  2496. {
  2497. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE)
  2498. ledStatus.ActionMode = 1;
  2499. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_PREPARING)
  2500. ledStatus.ActionMode = 3;
  2501. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING)
  2502. ledStatus.ActionMode = 4;
  2503. Config_LED_Status(Uart5Fd, Addr.AcPlug, &ledStatus);
  2504. }
  2505. void SetLegacyReq(byte _switch)
  2506. {
  2507. if (Config_Legacy_Req(Uart5Fd, Addr.AcPlug, _switch))
  2508. {
  2509. PRINTF_FUNC("SetLegacyReq Pass. \n");
  2510. }
  2511. else
  2512. {
  2513. PRINTF_FUNC("SetLegacyReq Fail. \n");
  2514. }
  2515. }
  2516. void SetCpDuty(byte _value)
  2517. {
  2518. if (_ac_duty != _value)
  2519. {
  2520. _ac_duty = _value;
  2521. PRINTF_FUNC("********Duty : %d \n", _ac_duty);
  2522. }
  2523. Config_Ac_Duty(Uart5Fd, Addr.AcPlug, _value);
  2524. }
  2525. void ResetMcuReqAC()
  2526. {
  2527. Config_Reset_MCU(Uart5Fd, Addr.AcPlug);
  2528. }
  2529. void SetModelName_AC()
  2530. {
  2531. if (Config_Model_Name(Uart5Fd, Addr.AcPlug, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  2532. {
  2533. PRINTF_FUNC("Set Model name (AC) PASS = %s \n", ShmSysConfigAndInfo->SysConfig.ModelName);
  2534. }
  2535. }
  2536. void ChangeToCsuMode()
  2537. {
  2538. ac_chargingInfo[0]->IsModeChagned = Config_CSU_Mode(Uart5Fd, Addr.AcPlug);
  2539. // if (ac_chargingInfo[0]->IsModeChagned == PASS)
  2540. // {
  2541. // Config_Reset_MCU(Uart5Fd, Addr.AcPlug);
  2542. // }
  2543. }
  2544. void ChangeStartOrStopDateTime(byte isStart)
  2545. {
  2546. char cmdBuf[32];
  2547. struct timeb csuTime;
  2548. struct tm *tmCSU;
  2549. ftime(&csuTime);
  2550. tmCSU = localtime(&csuTime.time);
  2551. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  2552. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  2553. tmCSU->tm_sec);
  2554. if (isStart)
  2555. strcpy((char *)ac_chargingInfo[0]->StartDateTime, cmdBuf);
  2556. else
  2557. strcpy((char *)ac_chargingInfo[0]->StopDateTime, cmdBuf);
  2558. }
  2559. unsigned char isModeChange()
  2560. {
  2561. unsigned char result = NO;
  2562. if(ac_chargingInfo[0]->SystemStatus != ac_chargingInfo[0]->PreviousSystemStatus)
  2563. {
  2564. result = YES;
  2565. ac_chargingInfo[0]->PreviousSystemStatus = ac_chargingInfo[0]->SystemStatus;
  2566. }
  2567. return result;
  2568. }
  2569. void SetAcModuleRelay(byte value)
  2570. {
  2571. acOutputRelay.relay_event.bits.AC_L1_Relay = value;
  2572. acOutputRelay.relay_event.bits.AC_L2_Relay = value;
  2573. acOutputRelay.relay_event.bits.AC_L3_Relay = value;
  2574. Config_Relay_Output(Uart5Fd, Addr.AcPlug, &acOutputRelay);
  2575. }
  2576. //===============================================
  2577. // AC OCPP routine
  2578. //===============================================
  2579. void ocpp_ac_startTransation_cmd()
  2580. {
  2581. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2582. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2583. {
  2584. if(strcmp((char *)ac_chargingInfo[0]->StartUserId, "") == EQUAL)
  2585. strcpy((char *)ac_chargingInfo[0]->StartUserId, (char *)ShmOCPP16Data->StartTransaction[hasDc].IdTag);
  2586. else
  2587. strcpy((char *)ShmOCPP16Data->StartTransaction[hasDc].IdTag, (char *)ac_chargingInfo[0]->StartUserId);
  2588. PRINTF_FUNC("AC IdTag = %s \n", ShmOCPP16Data->StartTransaction[hasDc].IdTag);
  2589. ShmOCPP16Data->CpMsg.bits[hasDc].StartTransactionReq = YES;
  2590. }
  2591. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2592. {
  2593. if(strcmp((char *)ac_chargingInfo[0]->StartUserId, "") == EQUAL)
  2594. strcpy((char *)ShmOCPP20Data->TransactionEvent[hasDc].idToken.idToken, (char *)ShmOCPP20Data->TransactionEvent[hasDc].idToken.idToken);
  2595. else
  2596. strcpy((char *)ShmOCPP20Data->TransactionEvent[hasDc].idToken.idToken, (char *)ac_chargingInfo[0]->StartUserId);
  2597. PRINTF_FUNC("AC IdTag = %s \n", ShmOCPP20Data->TransactionEvent[hasDc].idToken.idToken);
  2598. ShmOCPP20Data->CpMsg.bits[hasDc].TransactionEventReq = YES;
  2599. }
  2600. }
  2601. void ocpp_ac_stopTransation_cmd()
  2602. {
  2603. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2604. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2605. {
  2606. if(strcmp((char *)ac_chargingInfo[0]->StartUserId, "") == EQUAL)
  2607. strcpy((char *)ShmOCPP16Data->StopTransaction[hasDc].IdTag, (char *)ShmOCPP16Data->StopTransaction[hasDc].IdTag);
  2608. else
  2609. strcpy((char *)ShmOCPP16Data->StopTransaction[hasDc].IdTag, (char *)ac_chargingInfo[0]->StartUserId);
  2610. PRINTF_FUNC("AC IdTag = %s \n", ShmOCPP16Data->StopTransaction[hasDc].IdTag);
  2611. ShmOCPP16Data->CpMsg.bits[hasDc].StopTransactionReq = YES;
  2612. }
  2613. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2614. {
  2615. if(strcmp((char *)ac_chargingInfo[0]->StartUserId, "") == EQUAL)
  2616. strcpy((char *)ShmOCPP20Data->TransactionEvent[hasDc].idToken.idToken, (char *)ShmOCPP20Data->TransactionEvent[hasDc].idToken.idToken);
  2617. else
  2618. strcpy((char *)ShmOCPP20Data->TransactionEvent[hasDc].idToken.idToken, (char *)ac_chargingInfo[0]->StartUserId);
  2619. PRINTF_FUNC("AC IdTag = %s \n", ShmOCPP20Data->TransactionEvent[hasDc].idToken.idToken);
  2620. ShmOCPP20Data->CpMsg.bits[hasDc].TransactionEventReq = YES;
  2621. }
  2622. }
  2623. bool ocpp_ac_remoteStopReq_cmd()
  2624. {
  2625. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2626. bool result = false;
  2627. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2628. {
  2629. result = ShmOCPP16Data->CsMsg.bits[hasDc].RemoteStopTransactionReq;
  2630. if (ShmOCPP16Data->CsMsg.bits[hasDc].RemoteStopTransactionReq == YES)
  2631. {
  2632. strcpy((char *)ShmOCPP16Data->StopTransaction[hasDc].StopReason, "Remote");
  2633. ShmOCPP16Data->CsMsg.bits[hasDc].RemoteStopTransactionReq = NO;
  2634. }
  2635. }
  2636. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2637. {
  2638. result = ShmOCPP20Data->CsMsg.bits[hasDc].RequestStopTransactionReq;
  2639. if (ShmOCPP20Data->CsMsg.bits[hasDc].RequestStopTransactionReq == YES)
  2640. {
  2641. strcpy((char *)ShmOCPP20Data->TransactionEvent[hasDc].transactionInfo.stoppedReason, "Remote");
  2642. ShmOCPP20Data->CsMsg.bits[hasDc].RequestStopTransactionReq = NO;
  2643. }
  2644. }
  2645. return result;
  2646. }
  2647. bool ocpp_ac_is_resPass_StartTransationConf()
  2648. {
  2649. bool result = false;
  2650. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2651. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2652. {
  2653. result = ShmOCPP16Data->CpMsg.bits[hasDc].StartTransactionConf;
  2654. }
  2655. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2656. {
  2657. result = ShmOCPP20Data->CpMsg.bits[hasDc].TransactionEventConf;
  2658. }
  2659. return result;
  2660. }
  2661. bool ocpp_ac_chk_invalid_id_cmd()
  2662. {
  2663. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2664. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2665. {
  2666. if (strstr((char *) ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnInvalidId].ItemData, "TRUE"))
  2667. {
  2668. if (strcmp((char *)ShmOCPP16Data->StartTransaction[hasDc].ResponseIdTagInfo.Status, "Blocked") == EQUAL ||
  2669. strcmp((char *)ShmOCPP16Data->StartTransaction[hasDc].ResponseIdTagInfo.Status, "Expired") == EQUAL ||
  2670. strcmp((char *)ShmOCPP16Data->StartTransaction[hasDc].ResponseIdTagInfo.Status, "Invalid") == EQUAL)
  2671. return true;
  2672. }
  2673. }
  2674. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2675. {
  2676. if (strstr((char *) ShmOCPP20Data->ControllerComponentVariable[TxCtrlr_StopTxOnInvalidId].variableAttribute[0].value, "TRUE"))
  2677. {
  2678. if (strcmp((char *)ShmOCPP20Data->TransactionEvent[hasDc].Response_idTokenInfo.status, "Blocked") == EQUAL ||
  2679. strcmp((char *)ShmOCPP20Data->TransactionEvent[hasDc].Response_idTokenInfo.status, "Expired") == EQUAL ||
  2680. strcmp((char *)ShmOCPP20Data->TransactionEvent[hasDc].Response_idTokenInfo.status, "Invalid") == EQUAL ||
  2681. strcmp((char *)ShmOCPP20Data->TransactionEvent[hasDc].Response_idTokenInfo.status, "NoCredit") == EQUAL ||
  2682. strcmp((char *)ShmOCPP20Data->TransactionEvent[hasDc].Response_idTokenInfo.status, "NotAllowedTypeEVSE") == EQUAL ||
  2683. strcmp((char *)ShmOCPP20Data->TransactionEvent[hasDc].Response_idTokenInfo.status, "NotAtThisLocation") == EQUAL ||
  2684. strcmp((char *)ShmOCPP20Data->TransactionEvent[hasDc].Response_idTokenInfo.status, "NotAtThisTime") == EQUAL ||
  2685. strcmp((char *)ShmOCPP20Data->TransactionEvent[hasDc].Response_idTokenInfo.status, "Unknown") == EQUAL)
  2686. return true;
  2687. }
  2688. }
  2689. return false;
  2690. }
  2691. bool ocpp_ac_get_startTransation_req()
  2692. {
  2693. bool result = false;
  2694. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2695. // 如果有 AC 槍,而現在是 DC 第二把槍進入充電
  2696. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2697. {
  2698. result = ShmOCPP16Data->CpMsg.bits[hasDc].StartTransactionReq;
  2699. }
  2700. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2701. {
  2702. result = ShmOCPP20Data->CpMsg.bits[hasDc].TransactionEventReq;
  2703. }
  2704. return result;
  2705. }
  2706. void ocpp_ac_set_stopReason_by_cmd(char *reason)
  2707. {
  2708. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2709. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2710. {
  2711. if (strcmp((char *)ShmOCPP16Data->StopTransaction[hasDc].StopReason, "") == EQUAL)
  2712. {
  2713. strcpy((char *)ShmOCPP16Data->StopTransaction[hasDc].StopReason, reason);
  2714. }
  2715. }
  2716. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2717. {
  2718. if (strcmp((char *)ShmOCPP20Data->TransactionEvent[hasDc].transactionInfo.stoppedReason, "") == EQUAL)
  2719. {
  2720. strcpy((char *)ShmOCPP20Data->TransactionEvent[hasDc].transactionInfo.stoppedReason, reason);
  2721. }
  2722. }
  2723. }
  2724. void ocpp_ac_auto_response_StartTransationConf()
  2725. {
  2726. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2727. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2728. {
  2729. if (ShmOCPP16Data->CpMsg.bits[hasDc].StartTransactionConf)
  2730. ShmOCPP16Data->CpMsg.bits[hasDc].StartTransactionConf = NO;
  2731. }
  2732. else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2733. {
  2734. if (ShmOCPP20Data->CpMsg.bits[hasDc].TransactionEventConf)
  2735. ShmOCPP20Data->CpMsg.bits[hasDc].TransactionEventConf = NO;
  2736. }
  2737. }
  2738. void ocpp_ac_set_errCode_cmd()
  2739. {
  2740. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2741. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2742. {
  2743. strcpy((char *)ShmOCPP16Data->StatusNotification[hasDc].ErrorCode, "InternalError");
  2744. strcpy((char *)ShmOCPP16Data->StatusNotification[hasDc].VendorErrorCode, (char *)ac_chargingInfo[0]->ConnectorAlarmCode);
  2745. }
  2746. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2747. {
  2748. }
  2749. }
  2750. void ocpp_ac_clear_errorCode_cmd(bool isError)
  2751. {
  2752. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2753. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2754. {
  2755. if (strcmp((char *)ShmOCPP16Data->StatusNotification[hasDc].ErrorCode, "NoError") != EQUAL)
  2756. strcpy((char *)ShmOCPP16Data->StatusNotification[hasDc].ErrorCode, "NoError");
  2757. if (strcmp((char *)ShmOCPP16Data->StatusNotification[hasDc].VendorErrorCode, "") != EQUAL)
  2758. strcpy((char *)ShmOCPP16Data->StatusNotification[hasDc].VendorErrorCode, "");
  2759. }
  2760. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2761. {
  2762. }
  2763. }
  2764. void ocpp_ac_clear_stopReason()
  2765. {
  2766. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2767. if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2768. strcpy((char *)ShmOCPP16Data->StopTransaction[hasDc].StopReason, "");
  2769. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2770. strcpy((char *)ShmOCPP20Data->TransactionEvent[hasDc].transactionInfo.stoppedReason, "");
  2771. }
  2772. void ocpp_ac_set_remoteStart_transaction_req(bool result)
  2773. {
  2774. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2775. if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
  2776. ShmOCPP16Data->CsMsg.bits [hasDc].RemoteStartTransactionReq = result;
  2777. else if (ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
  2778. ShmOCPP20Data->CsMsg.bits [hasDc].RequestStartTransactionReq = result;
  2779. }
  2780. //===============================================
  2781. // AC Main Process
  2782. //===============================================
  2783. byte _SystemStatus = 255;
  2784. byte _CpStatus = 255;
  2785. byte GetMaxChargingDuty()
  2786. {
  2787. // 一開始先設定最大電流為硬體最大可支援電流
  2788. byte result = AcChargingRatingCurrent;
  2789. // ChargingProfileCurrent => charging profile
  2790. // AcMaxChargingCurrent => 網頁
  2791. // AcChargingRatingCurrent => 硬體限制最大可輸出電流
  2792. if (ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent > 0)
  2793. {
  2794. if (ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent <= result)
  2795. result = ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent;
  2796. }
  2797. if (ac_chargingInfo[0]->ChargingProfileCurrent > 0)
  2798. {
  2799. if (ac_chargingInfo[0]->ChargingProfileCurrent <= result)
  2800. result = ac_chargingInfo[0]->ChargingProfileCurrent;
  2801. }
  2802. if (result >= 1 && result <= 5)
  2803. result = AC_MINIMUM_DUTY;
  2804. else if (result == 0)
  2805. result = AC_STOP_DUTY;
  2806. return result;
  2807. }
  2808. void AcChargeTypeProcess()
  2809. {
  2810. if (acgunCount > 0)
  2811. {
  2812. //ac_chargingInfo[0]->SelfTest_Comp = YES;
  2813. //ac_chargingInfo[0]->IsModeChagned = PASS;
  2814. //---------------------------------------------
  2815. if (ac_chargingInfo[0]->SelfTest_Comp == NO)
  2816. {
  2817. ac_chargingInfo[0]->IsModeChagned = NO;
  2818. ResetMcuReqAC();
  2819. SetModelName_AC();
  2820. GetFwVersion_AC();
  2821. GetAcModelName();
  2822. }
  2823. else if (ac_chargingInfo[0]->SelfTest_Comp == YES)
  2824. {
  2825. if (isAcModuleInitComp == NO)
  2826. {
  2827. isAcModuleInitComp = YES;
  2828. ac_chargingInfo[0]->SystemStatus = SYS_MODE_IDLE;
  2829. }
  2830. GetAcAlarmCode();
  2831. if (GetAcStatus())
  2832. {
  2833. byte _status = SYS_MODE_BOOTING;
  2834. if (ac_chargingInfo[0]->IsAvailable == NO)
  2835. {
  2836. _status = SYS_MODE_MAINTAIN;
  2837. }
  2838. //printf("SystemStatus = %d, err = %d \n", ac_chargingInfo[0]->SystemStatus, ac_chargingInfo[0]->IsErrorOccur);
  2839. if ((ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE || ac_chargingInfo[0]->SystemStatus == SYS_MODE_MAINTAIN) &&
  2840. ac_chargingInfo[0]->IsErrorOccur)
  2841. {
  2842. _status = SYS_MODE_FAULT;
  2843. }
  2844. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING &&
  2845. ac_chargingInfo[0]->IsErrorOccur)
  2846. {
  2847. _status = SYS_MODE_ALARM;
  2848. }
  2849. else if (_status == SYS_MODE_MAINTAIN)
  2850. {
  2851. // do nothing
  2852. }
  2853. else if (acStatus.CpStatus == AC_SYS_A)
  2854. {
  2855. if (ac_chargingInfo[0]->IsErrorOccur &&
  2856. (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING ||
  2857. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_CHARGING))
  2858. _status = SYS_MODE_ALARM;
  2859. else if (!ac_chargingInfo[0]->IsErrorOccur &&
  2860. (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_TERMINATING ||
  2861. ac_chargingInfo[0]->SystemStatus <= SYS_MODE_ALARM))
  2862. {
  2863. if (GetTimeoutValue(&_ac_charging_comp) >= 10 && acStatus.CpStatus == AC_SYS_A)
  2864. _status = SYS_MODE_IDLE;
  2865. }
  2866. }
  2867. else if (ac_chargingInfo[0]->SystemStatus >= SYS_MODE_PREPARING &&
  2868. ac_chargingInfo[0]->SystemStatus < SYS_MODE_CHARGING)
  2869. {
  2870. if (acStatus.CpStatus == AC_SYS_C)
  2871. _status = SYS_MODE_CHARGING;
  2872. else if (GetTimeoutValue(&_ac_preparing) >= 120)
  2873. _status = SYS_MODE_IDLE;
  2874. }
  2875. else if ((acStatus.CpStatus == AC_SYS_B || ac_chargingInfo[0]->ConnectorPlugIn == AC_SYS_B) &&
  2876. ac_chargingInfo[0]->IsAvailable &&
  2877. !ac_chargingInfo[0]->IsErrorOccur &&
  2878. ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE &&
  2879. (ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES ||
  2880. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE))
  2881. {
  2882. if (ac_chargingInfo[0]->RemoteStartFlag == YES)
  2883. {
  2884. PRINTF_FUNC("** AC Remote \n");
  2885. ac_chargingInfo[0]->RemoteStartFlag = NO;
  2886. ac_chargingInfo[0]->isRemoteStart = YES;
  2887. strcpy((char *)ac_chargingInfo[0]->StartUserId, "");
  2888. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  2889. ocpp_ac_set_remoteStart_transaction_req(NO);
  2890. _status = SYS_MODE_PREPARING;
  2891. }
  2892. else if (ac_chargingInfo[0]->ReservedStartFlag == YES)
  2893. {
  2894. ac_chargingInfo[0]->ReservedStartFlag = NO;
  2895. strcpy ( (char *) ac_chargingInfo[0]->StartUserId,
  2896. (char *) ShmDcCommonData->_reserved_ac_UserId [0] );
  2897. PRINTF_FUNC ( "AC Start Charging : Reserved CardNumber = %s \n", ac_chargingInfo[0]->StartUserId );
  2898. strcpy ( (char *) ShmSysConfigAndInfo->SysConfig.UserId, "" );
  2899. strcpy ( (char *) ShmDcCommonData->_reserved_ac_UserId [0], "" );
  2900. // 當前操作的槍號,進入 Preparing
  2901. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  2902. }
  2903. else if (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE)
  2904. {
  2905. PRINTF_FUNC ( "Conn AC Start Charging : Plug and charing \n" );
  2906. byte hasDc = ((ShmSysConfigAndInfo->SysConfig.TotalConnectorCount > 0) ? 1 : 0);
  2907. strncpy ( (char *) ShmOCPP16Data->StartTransaction [hasDc].IdTag,
  2908. (char *) ShmSysConfigAndInfo->SysConfig.SerialNumber,
  2909. sizeof(ShmOCPP16Data->StartTransaction [hasDc].IdTag) );
  2910. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2911. _status = SYS_MODE_PREPARING;
  2912. }
  2913. else if (ShmSysConfigAndInfo->SysInfo.OrderCharging == NO_DEFINE)
  2914. {
  2915. PRINTF_FUNC("** UserId = %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  2916. strcpy((char *)ac_chargingInfo[0]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  2917. PRINTF_FUNC("** CardNumber = %s \n", ac_chargingInfo[0]->StartUserId);
  2918. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2919. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  2920. _status = SYS_MODE_PREPARING;
  2921. }
  2922. }
  2923. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_PREPARING ||
  2924. ac_chargingInfo[0]->SystemStatus == SYS_MODE_CHARGING)
  2925. {
  2926. if (ocpp_ac_remoteStopReq_cmd())
  2927. {
  2928. ocpp_ac_set_stopReason_by_cmd("Remote");
  2929. _status = SYS_MODE_TERMINATING;
  2930. }
  2931. else if (ac_chargingInfo[0]->StopChargeFlag ||
  2932. acStatus.CpStatus == AC_SYS_F)
  2933. {
  2934. _status = SYS_MODE_TERMINATING;
  2935. }
  2936. }
  2937. if (_status != SYS_MODE_BOOTING && ac_chargingInfo[0]->SystemStatus != _status)
  2938. {
  2939. ac_chargingInfo[0]->SystemStatus = _status;
  2940. if (ac_chargingInfo [0]->SystemStatus != _SystemStatus)
  2941. {
  2942. PRINTF_FUNC ( "ac_chargingInfo[0]->SystemStatus = %d \n", ac_chargingInfo [0]->SystemStatus );
  2943. _SystemStatus = ac_chargingInfo [0]->SystemStatus;
  2944. }
  2945. }
  2946. if (acStatus.CpStatus != _CpStatus &&
  2947. _CpStatus != SYS_MODE_BOOTING)
  2948. {
  2949. PRINTF_FUNC("acStatus.CpStatus = %d", acStatus.CpStatus);
  2950. _CpStatus = acStatus.CpStatus;
  2951. }
  2952. // 設定限制最大充電電流 >= 6 ~ <= 32
  2953. switch(ac_chargingInfo[0]->SystemStatus)
  2954. {
  2955. case SYS_MODE_IDLE:
  2956. case SYS_MODE_MAINTAIN:
  2957. case SYS_MODE_RESERVATION:
  2958. case SYS_MODE_FAULT:
  2959. {
  2960. if (isModeChange())
  2961. {
  2962. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_IDLE)
  2963. {
  2964. PRINTF_FUNC("================== SYS_MODE_IDLE (AC Gun) ================ \n");
  2965. ac_chargingInfo [0]->ReservationId = -1;
  2966. }
  2967. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_MAINTAIN)
  2968. PRINTF_FUNC("================== SYS_MODE_MAINTAIN (AC Gun) ================ \n");
  2969. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_RESERVATION)
  2970. {
  2971. PRINTF_FUNC("================== SYS_MODE_RESERVATION (AC Gun) ================ \n");
  2972. PRINTF_FUNC("================= Id : %s ================ \n", ShmDcCommonData->_reserved_ac_UserId[0]);
  2973. }
  2974. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_FAULT)
  2975. PRINTF_FUNC("================== SYS_MODE_FAULT (AC Gun) ================ \n");
  2976. SetCpDuty(100);
  2977. ac_chargingInfo [0]->PresentChargedDuration = 0;
  2978. ac_chargingInfo [0]->RemainChargingDuration = 0;
  2979. ac_chargingInfo [0]->PresentChargingCurrent = 0;
  2980. ac_chargingInfo[0]->PresentChargedEnergy = 0.0;
  2981. ac_chargingInfo[0]->PresentChargingVoltage = 0;
  2982. ac_chargingInfo[0]->ChargingFee = 0.0;
  2983. ac_chargingInfo[0]->isRemoteStart = NO;
  2984. strcpy((char *)ac_chargingInfo[0]->StartDateTime, "");
  2985. strcpy((char *)ac_chargingInfo[0]->StopDateTime, "");
  2986. strcpy ( (char *) ac_chargingInfo [0]->StartUserId, "" );
  2987. _beforeChargingTotalEnergy = 0.0;
  2988. ocpp_ac_clear_stopReason();
  2989. ocpp_ac_auto_response_StartTransationConf();
  2990. ac_startTransationFlag = START_TRANSATION_STATUS_WAIT;
  2991. }
  2992. ac_chargingInfo[0]->ChargingProfilePower = -1;
  2993. ac_chargingInfo[0]->ChargingProfileCurrent = -1;
  2994. ac_chargingInfo[0]->StopChargeFlag = NO;
  2995. ChangeLedStatus();
  2996. }
  2997. break;
  2998. case SYS_MODE_PREPARING:
  2999. {
  3000. if (isModeChange())
  3001. {
  3002. PRINTF_FUNC("================== SYS_MODE_PREPARING (AC Gun) ================ \n");
  3003. SetLegacyReq(YES);
  3004. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  3005. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  3006. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  3007. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  3008. ocpp_ac_startTransation_cmd();
  3009. GetTimespecFunc(&_ac_preparing);
  3010. }
  3011. if (GetChargingEnergy() == PASS)
  3012. {
  3013. //ac_chargingInfo[0]->PresentChargedEnergy = acChargingEnergy.Energy / 100;
  3014. _beforeChargingTotalEnergy = ((float)acChargingEnergy.Energy) / 10000;
  3015. }
  3016. ac_startTransationFlag = ocpp_ac_is_resPass_StartTransationConf();
  3017. if (ac_startTransationFlag == START_TRANSATION_STATUS_PASS)
  3018. {
  3019. if (ocpp_ac_chk_invalid_id_cmd())
  3020. ac_startTransationFlag = START_TRANSATION_STATUS_FAIL;
  3021. }
  3022. else
  3023. {
  3024. if (ShmSysConfigAndInfo->SysInfo.OcppConnStatus == NO)
  3025. {
  3026. if (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == _OFFLINE_POLICY_FREE_CHARGING ||
  3027. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE ||
  3028. !ocpp_ac_get_startTransation_req() ||
  3029. strcmp((char *)ac_chargingInfo[0]->StartUserId, "") != EQUAL)
  3030. {
  3031. ac_startTransationFlag = START_TRANSATION_STATUS_PASS;
  3032. }
  3033. }
  3034. }
  3035. if (ac_startTransationFlag == START_TRANSATION_STATUS_PASS)
  3036. {
  3037. // 充電前須給 duty : 依照 Charging profile
  3038. SetCpDuty(GetMaxChargingDuty());
  3039. }
  3040. else if (ac_startTransationFlag == START_TRANSATION_STATUS_FAIL ||
  3041. (ac_startTransationFlag == START_TRANSATION_STATUS_WAIT && GetTimeoutValue(&_ac_preparing) > 10))
  3042. {
  3043. PRINTF_FUNC("StartTransaction Fail / Timeout. \n");
  3044. ac_chargingInfo[0]->StopChargeFlag = YES;
  3045. }
  3046. ChangeLedStatus();
  3047. }
  3048. break;
  3049. case SYS_MODE_CHARGING:
  3050. {
  3051. if (isModeChange())
  3052. {
  3053. PRINTF_FUNC("================== SYS_MODE_CHARGING (AC Gun) ================ \n");
  3054. ftime(&_ac_startChargingTime);
  3055. ChangeStartOrStopDateTime(YES);
  3056. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  3057. }
  3058. // 用以判斷是否有在輸出
  3059. ac_chargingInfo[0]->IsCharging = acStatus.RelayStatus;
  3060. ftime(&_ac_endChargingTime);
  3061. ac_chargingInfo[0]->PresentChargedDuration = DiffTimeb(_ac_startChargingTime, _ac_endChargingTime);
  3062. if (ac_chargingInfo[0]->IsCharging)
  3063. {
  3064. if (GetChargingEnergy() == PASS)
  3065. {
  3066. if ((acChargingEnergy.Energy - _beforeChargingTotalEnergy) > 0)
  3067. {
  3068. // 小數點取兩位
  3069. ac_chargingInfo[0]->PresentChargedEnergy += (((float)acChargingEnergy.Energy) / 10000 - _beforeChargingTotalEnergy);
  3070. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  3071. {
  3072. ac_chargingInfo[0]->ChargingFee += ac_chargingInfo[0]->PresentChargedEnergy * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  3073. }
  3074. }
  3075. _beforeChargingTotalEnergy = ((float)acChargingEnergy.Energy) / 10000;
  3076. }
  3077. //GetAcChargingPower();
  3078. GetAcChargingVoltage();
  3079. GetAcChargingCurrent();
  3080. {
  3081. // PRINTF_FUNC("L1N_L12 = %f, L1N_L12 = %f, L1N_L12 = %f \n",
  3082. // acChargingVoltage.L1N_L12,
  3083. // acChargingVoltage.L2N_L23,
  3084. // acChargingVoltage.L3N_L31);
  3085. // PRINTF_FUNC("c1 = %d, c2 = %d, c3 = %d \n",
  3086. // acChargingCurrent.OuputCurrentL1,
  3087. // acChargingCurrent.OuputCurrentL2,
  3088. // acChargingCurrent.OuputCurrentL3);
  3089. // 即時輸出功率
  3090. ac_chargingInfo[0]->PresentChargingPower =
  3091. (float)(acChargingVoltage.L1N_L12 * acChargingCurrent.OuputCurrentL1 +
  3092. acChargingVoltage.L2N_L23 * acChargingCurrent.OuputCurrentL2 +
  3093. acChargingVoltage.L3N_L31 * acChargingCurrent.OuputCurrentL3) / 1000;
  3094. }
  3095. // 累積輸出功率
  3096. //ac_chargingInfo[0]->PresentChargedEnergy
  3097. ac_chargingInfo[0]->PresentChargingVoltage = (float)acChargingVoltage.L1N_L12;
  3098. ac_chargingInfo[0]->PresentChargingCurrent = (float)(acChargingCurrent.OuputCurrentL1 + acChargingCurrent.OuputCurrentL2 + acChargingCurrent.OuputCurrentL3);
  3099. // 充電中須給 duty : 依照 Charging profile
  3100. SetCpDuty(GetMaxChargingDuty());
  3101. }
  3102. else
  3103. {
  3104. // 搭上 relay
  3105. if (acStatus.CpStatus == AC_SYS_C &&
  3106. !ac_chargingInfo[0]->IsErrorOccur)
  3107. {
  3108. PRINTF_FUNC("Set relay ON (AC Type). \n");
  3109. SetAcModuleRelay(YES);
  3110. }
  3111. }
  3112. ChangeLedStatus();
  3113. }
  3114. break;
  3115. case SYS_MODE_TERMINATING:
  3116. case SYS_MODE_ALARM:
  3117. {
  3118. //ocpp_set_errCode_cmd
  3119. if (isModeChange())
  3120. {
  3121. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_TERMINATING)
  3122. PRINTF_FUNC("================== SYS_MODE_TERMINATING (AC Gun) ================ \n");
  3123. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_ALARM)
  3124. PRINTF_FUNC("================== SYS_MODE_ALARM (AC Gun) ================ \n");
  3125. ChangeStartOrStopDateTime(NO);
  3126. GetTimespecFunc(&_ac_charging_comp);
  3127. ocpp_ac_set_stopReason_by_cmd("Local");
  3128. SetCpDuty(100);
  3129. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_TERMINATING)
  3130. PRINTF_FUNC("================== SYS_MODE_TERMINATING (AC Gun) ================ \n");
  3131. else if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_ALARM)
  3132. {
  3133. ftime(&_ac_endChargingTime);
  3134. ocpp_ac_set_errCode_cmd();
  3135. if (strcmp((char *)ac_chargingInfo[0]->StartDateTime, "") != EQUAL)
  3136. {
  3137. ocpp_ac_auto_response_StartTransationConf();
  3138. // AC 固定為第2把槍
  3139. ocpp_ac_stopTransation_cmd();
  3140. }
  3141. }
  3142. }
  3143. SetLegacyReq(NO);
  3144. if (acStatus.RelayStatus == NO)
  3145. {
  3146. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_TERMINATING)
  3147. ac_chargingInfo[0]->SystemStatus = SYS_MODE_COMPLETE;
  3148. }
  3149. else
  3150. {
  3151. // 搭上 relay
  3152. PRINTF_FUNC("Set relay OFF (AC Type). \n");
  3153. SetAcModuleRelay(NO);
  3154. }
  3155. if (ac_chargingInfo[0]->SystemStatus == SYS_MODE_ALARM)
  3156. ac_chargingInfo[0]->PresentChargedDuration = DiffTimeb(_ac_startChargingTime, _ac_endChargingTime);
  3157. }
  3158. break;
  3159. case SYS_MODE_COMPLETE:
  3160. {
  3161. if (isModeChange())
  3162. {
  3163. PRINTF_FUNC("================== SYS_MODE_COMPLETE (AC Gun) ================ \n");
  3164. GetTimespecFunc(&_ac_charging_comp);
  3165. ftime(&_ac_endChargingTime);
  3166. ocpp_ac_set_errCode_cmd();
  3167. if (strcmp((char *)ac_chargingInfo[0]->StartDateTime, "") != EQUAL)
  3168. {
  3169. ocpp_ac_auto_response_StartTransationConf();
  3170. // AC 固定為第2把槍
  3171. ocpp_ac_stopTransation_cmd();
  3172. }
  3173. ChangeStartOrStopDateTime(NO);
  3174. ac_chargingInfo[0]->PresentChargedDuration = DiffTimeb(_ac_startChargingTime, _ac_endChargingTime);
  3175. }
  3176. }
  3177. break;
  3178. }
  3179. }
  3180. }
  3181. }
  3182. }
  3183. bool Is50kwSystemChk()
  3184. {
  3185. bool result = false;
  3186. char EvsePower[2];
  3187. EvsePower[2] = '\0';
  3188. if (strlen ( (char *) ShmSysConfigAndInfo->SysConfig.ModelName ) >= 6)
  3189. {
  3190. strncpy ( EvsePower, (char *) (ShmSysConfigAndInfo->SysConfig.ModelName + 4), 2);
  3191. if (strcmp ( EvsePower, "50" ) == EQUAL)
  3192. {
  3193. result = true;
  3194. }
  3195. }
  3196. return result;
  3197. }
  3198. void RunForceStopProcess()
  3199. {
  3200. if (isCriticalStop == NO)
  3201. {
  3202. isCriticalStop = YES;
  3203. GetTimespecFunc(&_psuCriticalStop);
  3204. }
  3205. else
  3206. {
  3207. int _timebuf = GetTimeoutValue(&_psuCriticalStop);
  3208. if (_timebuf < 0)
  3209. GetTimespecFunc(&_psuCriticalStop);
  3210. else
  3211. {
  3212. if (_timebuf >= FORCE_STOP_TIME)
  3213. {
  3214. isCriticalStop = NO;
  3215. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm = NORMAL;
  3216. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU = NORMAL;
  3217. system("/usr/bin/run_evse_restart.sh");
  3218. }
  3219. }
  3220. }
  3221. }
  3222. int main(void)
  3223. {
  3224. if(InitShareMemory() == FAIL)
  3225. {
  3226. #ifdef SystemLogMessage
  3227. DEBUG_ERROR("InitShareMemory NG\n");
  3228. #endif
  3229. if(ShmStatusCodeData!=NULL)
  3230. {
  3231. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  3232. }
  3233. sleep(5);
  3234. return 0;
  3235. }
  3236. gunCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  3237. acgunCount = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  3238. // Open Uart5 for RB
  3239. Uart5Fd = InitComPort();
  3240. Initialization();
  3241. is50kwSystem = Is50kwSystemChk();
  3242. sleep(1);
  3243. if(Uart5Fd < 0)
  3244. {
  3245. PRINTF_FUNC("(Internal) open port error. \n");
  3246. return 0;
  3247. }
  3248. if (acgunCount > 0)
  3249. {
  3250. acOutputRelay.relay_event.bits.AC_L1_Relay = 0x00;
  3251. acOutputRelay.relay_event.bits.AC_L2_Relay = 0x00;
  3252. acOutputRelay.relay_event.bits.AC_L3_Relay = 0x00;
  3253. Config_Relay_Output(Uart5Fd, Addr.AcPlug, &acOutputRelay);
  3254. }
  3255. outputRelay.relay_event.bits.AC_Contactor = 0x00;
  3256. outputRelay.relay_event.bits.R5_BOTH_K3_K6 = 0x00; // K3 - K6 (R4)
  3257. outputRelay.relay_event.bits.R4_BOTH_K2_K5 = 0x00; // K2 - K5 (R4)
  3258. outputRelay.relay_event.bits.R3_BOTH_K1_K4 = 0x00; // K1 - K4 (R3)
  3259. outputRelay.relay_event.bits.Gun1_P = 0x00; // 左槍 D+ (R1 +)
  3260. outputRelay.relay_event.bits.Gun1_N = 0x00; // 左槍 D- (R1 -)
  3261. outputRelay.relay_event.bits.Gun2_N = 0x00; // 右槍 D- (R2 -)
  3262. outputRelay.relay_event.bits.Gun2_P = 0x00; // 右槍 D+ (R2 +)
  3263. if(Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay) != PASS)
  3264. PRINTF_FUNC("Config_Relay_Output fail \n");
  3265. // sleep(1);
  3266. // GetRelayOutputStatus();
  3267. // PRINTF_FUNC("Gun1_P = %d \n", outputRelay.relay_event.bits.Gun1_P);
  3268. // PRINTF_FUNC("Gun1_N = %d \n", outputRelay.relay_event.bits.Gun1_N);
  3269. // PRINTF_FUNC("Gun2_P = %d", outputRelay.relay_event.bits.Gun2_P);
  3270. // PRINTF_FUNC("Gun2_N = %d", outputRelay.relay_event.bits.Gun2_N);
  3271. // PRINTF_FUNC("R3 = %d \n", regRelay.relay_event.bits.R3_BOTH_K1_K4);
  3272. // PRINTF_FUNC("R4 = %d \n", regRelay.relay_event.bits.R4_BOTH_K2_K5);
  3273. // PRINTF_FUNC("R5 = %d \n", regRelay.relay_event.bits.R5_BOTH_K3_K6);
  3274. // return -1;
  3275. cur_led_color.Connect_1_Red = COLOR_MIN_LV;
  3276. cur_led_color.Connect_1_Green = COLOR_MIN_LV;
  3277. cur_led_color.Connect_1_Blue = COLOR_MIN_LV;
  3278. cur_led_color.Connect_2_Red = COLOR_MIN_LV;
  3279. cur_led_color.Connect_2_Green = COLOR_MIN_LV;
  3280. cur_led_color.Connect_2_Blue = COLOR_MIN_LV;
  3281. relayMatchFlag = NO;
  3282. isCriticalStop = NO;
  3283. isSelftestComp = NO;
  3284. GetTimespecFunc(&_led_blink_time);
  3285. for(;;)
  3286. {
  3287. bool isCharging = false;
  3288. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  3289. if (ShmRelayModuleData->SelfTest_Comp == NO)
  3290. {
  3291. GetFwAndHwVersion_Relay();
  3292. SetModelName_Relay();
  3293. SetRtcData_Relay();
  3294. sleep(1);
  3295. }
  3296. if (ShmFanModuleData->SelfTest_Comp == NO)
  3297. {
  3298. GetFwAndHwVersion_Fan();
  3299. SetModelName_Fan();
  3300. SetRtcData_Fan();
  3301. sleep(1);
  3302. GetTimespecFunc(&_priority_time);
  3303. }
  3304. // 自檢階段處理,自檢階段如果讀不到版號則代表該系統沒有掛燈板
  3305. if (ShmLedModuleData->SelfTest_Comp == NO)
  3306. {
  3307. // 自檢階段
  3308. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq <= _STEST_PSU_CAP)
  3309. {
  3310. GetFwAndHwVersion_Led();
  3311. sleep(1);
  3312. GetTimespecFunc(&_led_priority_time);
  3313. }
  3314. else
  3315. {
  3316. // 自檢階段沒有問到版號
  3317. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail == NO)
  3318. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = YES;
  3319. }
  3320. }
  3321. AcChargeTypeProcess();
  3322. if (ShmRelayModuleData->SelfTest_Comp == YES)
  3323. {
  3324. if (ShmSysConfigAndInfo->SysInfo.PageIndex == _LCM_IDLE && !isSelftestComp)
  3325. isSelftestComp = true;
  3326. // ==============優先權最高 10 ms ==============
  3327. // 輸出電壓
  3328. GetPersentOutputVol();
  3329. // 三相輸入電壓
  3330. GetPresentInputVol();
  3331. // 讀取當前 AC relay 狀態
  3332. regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  3333. GetRelayOutputStatus();
  3334. for (int i = 0; i < gunCount; i++)
  3335. {
  3336. // Cable check (Set)
  3337. CableCheckDetected(i);
  3338. // check k1 k2 relay 狀態
  3339. CheckK1K2RelayOutput(i);
  3340. // 依據當前各槍的狀態選擇 搭上/放開 Relay
  3341. SetK1K2RelayStatus(i);
  3342. if (_chargingData[i]->SystemStatus == SYS_MODE_IDLE ||
  3343. _chargingData[i]->SystemStatus == SYS_MODE_RESERVATION ||
  3344. _chargingData[i]->SystemStatus == SYS_MODE_MAINTAIN)
  3345. {
  3346. _isOvpChkTimeFlag[i] = NO;
  3347. }
  3348. if (_chargingData[i]->SystemStatus == SYS_MODE_BOOTING || _chargingData[i]->ConnectorPlugIn ||
  3349. (_chargingData[i]->SystemStatus >= SYS_MODE_MODE_REASSIGN_CHECK && _chargingData[i]->SystemStatus <= SYS_MODE_ALARM) ||
  3350. (_chargingData[i]->SystemStatus >= SYS_MODE_CCS_PRECHARGE_STEP0 && _chargingData[i]->SystemStatus <= SYS_MODE_CCS_PRECHARGE_STEP1) ||
  3351. ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES ||
  3352. (ShmSysConfigAndInfo->SysInfo.PageIndex >= _LCM_AUTHORIZING && ShmSysConfigAndInfo->SysInfo.PageIndex <= _LCM_WAIT_FOR_PLUG))
  3353. {
  3354. _chargingData[i]->IsReadyToCharging = YES;
  3355. isCharging = true;
  3356. if (_chargingData[i]->SystemStatus == SYS_MODE_CHARGING)
  3357. {
  3358. // OVP
  3359. CheckOutputPowerOverCarReq(i);
  3360. }
  3361. else if (_chargingData[i]->SystemStatus == SYS_MODE_PREPARE_FOR_EVSE)
  3362. {
  3363. // abnormal voltage at the stage of GFD
  3364. CheckOutputVolNoneMatchFire(i);
  3365. }
  3366. else
  3367. _isOutputNoneMatch[i] = NO;
  3368. }
  3369. else
  3370. _chargingData[i]->IsReadyToCharging = NO;
  3371. }
  3372. // Cable check (Get)
  3373. GetGfdAdc();
  3374. // 橋接 relay
  3375. SetParalleRelayStatus();
  3376. // 搭上 AC Contactor
  3377. // if (isCharging)
  3378. // outputRelay.relay_event.bits.AC_Contactor = YES;
  3379. // else
  3380. // outputRelay.relay_event.bits.AC_Contactor = NO;
  3381. if (isCharging || (ShmPsuData->Work_Step == _TEST_MODE))
  3382. {
  3383. isStopChargingCount = false;
  3384. outputRelay.relay_event.bits.AC_Contactor = YES;
  3385. }
  3386. else
  3387. {
  3388. if (!isStopChargingCount)
  3389. {
  3390. GetTimespecFunc(&_close_ac_contactor);
  3391. isStopChargingCount = true;
  3392. }
  3393. else
  3394. {
  3395. if ((outputRelay.relay_event.bits.AC_Contactor == YES && GetTimeoutValue(&_close_ac_contactor) >= TEN_MINUTES))
  3396. {
  3397. outputRelay.relay_event.bits.AC_Contactor = NO;
  3398. }
  3399. }
  3400. }
  3401. if ((ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL ||
  3402. ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL) &&
  3403. ShmDcCommonData->_tiltSensorStep == _TILT_SENSOR_STEP_NONE)
  3404. outputRelay.relay_event.bits.AC_Contactor = NO;
  3405. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm == ABNORMAL ||
  3406. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU == ABNORMAL)
  3407. {
  3408. RunForceStopProcess();
  3409. outputRelay.relay_event.bits.AC_Contactor = NO;
  3410. }
  3411. ShmDcCommonData->acContactSwitch = outputRelay.relay_event.bits.AC_Contactor;
  3412. if (ShmPsuData->Work_Step >= _TEST_MODE && ShmPsuData->Work_Step <= _TEST_MODE)
  3413. outputRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_P = YES;
  3414. // 搭上/鬆開 Relay
  3415. if(IsNoneMatchRelayStatus())
  3416. {
  3417. relayMatchFlag = NO;
  3418. if (Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay))
  3419. {
  3420. // regRelay.relay_event.bits.R5_BOTH_K3_K6 = outputRelay.relay_event.bits.R5_BOTH_K3_K6;
  3421. // regRelay.relay_event.bits.R4_BOTH_K2_K5 = outputRelay.relay_event.bits.R4_BOTH_K2_K5;
  3422. // regRelay.relay_event.bits.R3_BOTH_K1_K4 = outputRelay.relay_event.bits.R3_BOTH_K1_K4;
  3423. // regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  3424. // regRelay.relay_event.bits.CCS_Precharge = outputRelay.relay_event.bits.CCS_Precharge;
  3425. // regRelay.relay_event.bits.Gun1_P = outputRelay.relay_event.bits.Gun1_P;
  3426. // regRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_N;
  3427. // regRelay.relay_event.bits.Gun2_P = outputRelay.relay_event.bits.Gun2_P;
  3428. // regRelay.relay_event.bits.Gun2_N = outputRelay.relay_event.bits.Gun2_N;
  3429. // regRelay.relay_event.bits.Gun1_Parallel_P = outputRelay.relay_event.bits.Gun1_Parallel_P;
  3430. // regRelay.relay_event.bits.Gun1_Parallel_N = outputRelay.relay_event.bits.Gun1_Parallel_N;
  3431. }
  3432. }
  3433. else if (relayMatchFlag == NO)
  3434. {
  3435. relayMatchFlag = YES;
  3436. PRINTF_FUNC("======== Relay Status Start========\n");
  3437. if (regRelay.relay_event.bits.AC_Contactor == YES)
  3438. PRINTF_FUNC("AC Power : ON \n");
  3439. else
  3440. PRINTF_FUNC("AC Power : OFF \n");
  3441. if (regRelay.relay_event.bits.Gun1_P == YES)
  3442. PRINTF_FUNC("Conn1(+) : ON \n");
  3443. else
  3444. PRINTF_FUNC("Conn1(+) : OFF \n");
  3445. if (regRelay.relay_event.bits.Gun1_N == YES)
  3446. PRINTF_FUNC("Conn1(-) : ON \n");
  3447. else
  3448. PRINTF_FUNC("Conn1(-) : OFF \n");
  3449. if (regRelay.relay_event.bits.Gun2_P == YES)
  3450. PRINTF_FUNC("Conn2(+) : ON \n");
  3451. else
  3452. PRINTF_FUNC("Conn2(+) : OFF \n");
  3453. if (regRelay.relay_event.bits.Gun2_N == YES)
  3454. PRINTF_FUNC("Conn2(-) : ON \n");
  3455. else
  3456. PRINTF_FUNC("Conn2(-) : OFF \n");
  3457. if (regRelay.relay_event.bits.R3_BOTH_K1_K4 == YES)
  3458. PRINTF_FUNC("R3_BOTH_K1_K4 : ON \n");
  3459. else
  3460. PRINTF_FUNC("R3_BOTH_K1_K4 : OFF \n");
  3461. if (regRelay.relay_event.bits.R4_BOTH_K2_K5 == YES)
  3462. PRINTF_FUNC("R4_BOTH_K2_K5 : ON \n");
  3463. else
  3464. PRINTF_FUNC("R4_BOTH_K2_K5 : OFF \n");
  3465. if (regRelay.relay_event.bits.R5_BOTH_K3_K6 == YES)
  3466. PRINTF_FUNC("R5_BOTH_K3_K6 : ON \n");
  3467. else
  3468. PRINTF_FUNC("R5_BOTH_K3_K6 : OFF \n");
  3469. PRINTF_FUNC("======== Relay Status End========\n");
  3470. }
  3471. }
  3472. if (ShmFanModuleData->SelfTest_Comp == YES ||
  3473. strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  3474. ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  3475. {
  3476. ShmFanModuleData->SelfTest_Comp = YES;
  3477. int _timebuf = GetTimeoutValue(&_priority_time);
  3478. if (_timebuf < 0)
  3479. GetTimespecFunc(&_priority_time);
  3480. else
  3481. {
  3482. if (_timebuf >= 1)
  3483. {
  3484. //GetPsuTempForFanSpeed();
  3485. GetFanSpeedByFunction();
  3486. GetFanSpeed();
  3487. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = _setFanSpeed;
  3488. GetTimespecFunc(&_priority_time);
  3489. ShmFanModuleData->SetFan1Speed = ShmFanModuleData->TestFanSpeed;
  3490. ShmFanModuleData->SetFan2Speed = ShmFanModuleData->TestFanSpeed;
  3491. ShmFanModuleData->SetFan3Speed = ShmFanModuleData->TestFanSpeed;
  3492. ShmFanModuleData->SetFan4Speed = ShmFanModuleData->TestFanSpeed;
  3493. //PRINTF_FUNC("set fan = %d \n", ShmFanModuleData->SetFan1Speed);
  3494. SetFanModuleSpeed();
  3495. }
  3496. }
  3497. }
  3498. if (ShmLedModuleData->SelfTest_Comp == YES)
  3499. {
  3500. int _timebuf = GetTimeoutValue(&_led_priority_time);
  3501. if (_timebuf < 0)
  3502. GetTimespecFunc(&_led_priority_time);
  3503. else
  3504. {
  3505. int _ledTimebuf = GetTimeoutValue(&_led_blink_time);
  3506. if (_ledTimebuf > 6 ||_ledTimebuf < 0)
  3507. GetTimespecFunc(&_led_blink_time);
  3508. if (_timebuf >= 1)
  3509. {
  3510. if(gunCount == 1)
  3511. {
  3512. SetLedColor(_chargingData[0], _chargingData[0]);
  3513. }
  3514. else if (gunCount == 2)
  3515. {
  3516. SetLedColor(_chargingData[0], _chargingData[1]);
  3517. }
  3518. GetTimespecFunc(&_led_priority_time);
  3519. }
  3520. }
  3521. }
  3522. usleep(10000);
  3523. }
  3524. return FAIL;
  3525. }