Module_InternalComm.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712
  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/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/wireless.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h> /*標準輸入輸出定義*/
  18. #include <stdlib.h> /*標準函數庫定義*/
  19. #include <unistd.h> /*Unix 標準函數定義*/
  20. #include <fcntl.h> /*檔控制定義*/
  21. #include <termios.h> /*PPSIX 終端控制定義*/
  22. #include <errno.h> /*錯誤號定義*/
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include "../../define.h"
  30. #include "internalComm.h"
  31. #include <stdbool.h>
  32. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  33. #define PASS 1
  34. #define FAIL -1
  35. #define YES 1
  36. #define NO 0
  37. #define TEN_MINUTES 600
  38. #define ENV_TEMP_MIN 45
  39. #define ENV_TEMP_MAX 50
  40. #define DEFAULT_AC_INDEX 2
  41. #define EQUAL 0
  42. #define COLOR_MAX_LV 100
  43. #define COLOR_MIN_LV 0
  44. #define AC_DEFAULT_VOL 220
  45. #define NO_DEFINE 255
  46. #define NDEFAULT_AC_INDEX 2
  47. #define OVP_UVP_CHK_COUNT 3
  48. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  49. struct StatusCodeData *ShmStatusCodeData;
  50. struct FanModuleData *ShmFanModuleData;
  51. struct RelayModuleData *ShmRelayModuleData;
  52. struct LedModuleData *ShmLedModuleData;
  53. struct PsuData *ShmPsuData;
  54. struct OCPP16Data *ShmOCPP16Data;
  55. #define VIN_MAX_VOLTAGE_IEC 285 // 大於該值 : OVP
  56. #define VIN_MAX_REV_VOLTAGE_IEC 275 // 小於賦歸 OVP
  57. #define VIN_MIN_VOLTAGE_IEC 160 // 小於該值 : UVP
  58. #define VIN_MIN_REV_VOLTAGE_IEC 170 // 大於賦歸 UVP
  59. #define VIN_MAX_VOLTAGE_UL 315 // 大於該值 : OVP // 美規 (W)
  60. #define VIN_MAX_REV_VOLTAGE_UL 305 // 小於賦歸 OVP
  61. #define VIN_MIN_VOLTAGE_UL 210 // 小於該值 : UVP
  62. #define VIN_MIN_REV_VOLTAGE_UL 220 // 大於賦歸 UVP
  63. #define VIN_DROP_VOLTAGE 150 // 小於該值 : ac drop
  64. #define VOUT_MAX_VOLTAGE 995
  65. #define VOUT_MIN_VOLTAGE 150
  66. #define IOUT_MAX_CURRENT 50
  67. #define MAX_FAN_SPEED 14000
  68. #define MIN_FAN_SPEED 3000
  69. #define NORMAL_FAN_SPEED 7000
  70. // GFD Status
  71. #define GFD_IDLE 0
  72. #define GFD_CABLECHK 1
  73. #define GFD_PRECHARGE 2
  74. #define GFD_CHARGING 3
  75. // LED Intensity (rate)
  76. #define LED_INTENSITY_DARKEST 0.2
  77. #define LED_INTENSITY_MEDIUM 0.6
  78. #define LED_INTENSITY_BRIGHTEST 1
  79. // EE Spec
  80. #define LED_BRIGHTNESS_LV_HIGH 1
  81. #define LED_BRIGHTNESS_LV_MID 0.5
  82. #define LED_BRIGHTNESS_LV_LOW 0.2
  83. // 最小切換 Relay 電壓
  84. #define SELF_TO_CHANGE_RELAY_STATUS 600
  85. // 透過電壓確認 Relay 是否搭上的依據電壓
  86. #define CHECK_RELAY_STATUS 300
  87. #define CHECK_RELAY_STATUS_GAP 100
  88. // 安全在停止充電程序中斷開 Relay 的電流
  89. #define SEFETY_SWITCH_RELAY_CUR 50
  90. // 確認 Relay Welding 電壓
  91. #define RELAY_WELDING_DET 300
  92. byte gunCount;
  93. byte acgunCount;
  94. // 槍資訊
  95. struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  96. struct ChargingInfoData *ac_chargingInfo[AC_QUANTITY];
  97. bool _isOutputNoneMatch[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  98. struct timeval _checkOutputNoneMatchTimer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  99. bool _isRelayWelding[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  100. struct timeval _checkRelayWeldingTimer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  101. byte _threePhaseOvp[3] = {0, 0, 0};
  102. byte _threePhaseUvp[3] = {0, 0, 0};
  103. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData);
  104. int Uart5Fd;
  105. char *relayRs485PortName = "/dev/ttyS5";
  106. unsigned short fanSpeedSmoothValue = 500;
  107. bool isStopChargingCount = false;
  108. struct timeval _close_ac_contactor;
  109. struct timeval _priority_time;
  110. struct timeval _led_priority_time;
  111. struct timeval _ac_charging_comp;
  112. struct timeval _ac_preparing;
  113. struct timeb _ac_startChargingTime;
  114. struct timeb _ac_endChargingTime;
  115. unsigned short _setFanSpeed = 0;
  116. float _beforeChargingTotalEnergy = 0.0;
  117. byte _checkLedChanged = 3;
  118. Ver ver;
  119. PresentInputVoltage inputVoltage;
  120. PresentOutputVoltage outputVoltage;
  121. FanSpeed fanSpeed;
  122. Temperature temperature;
  123. AuxPower auxPower;
  124. Gfd gfd_adc;
  125. Gfd_config gfd_config;
  126. Gpio_in gpio_in;
  127. Gpio_out gpio_out;
  128. Relay outputRelay;
  129. Relay regRelay;
  130. Rtc rtc;
  131. Led_Color cur_led_color;
  132. Led_Color led_color;
  133. Ac_Status acStatus;
  134. Ac_Led_Status ledStatus;
  135. Ac_Alarm_code acAlarmCode;
  136. Ac_Charging_energy acChargingEnergy;
  137. Ac_Charging_current acChargingCurrent;
  138. #define AC_OVP 1
  139. #define AC_UVP 2
  140. #define AC_OCP 4
  141. #define AC_OTP 8
  142. #define AC_GMI_FAULT 16
  143. #define AC_CP_ERROR 32
  144. #define AC_AC_LEAKAGE 64
  145. #define AC_DC_LEAKAGE 128
  146. #define AC_SYSTEM_SELFTEST_FAULT 256
  147. #define AC_HANDSHAKE_TIMEOUT 512
  148. #define AC_EMC_STOP 1024
  149. #define AC_RELAY_WELDING 2048
  150. #define AC_GF_MODULE_FAULT 4096
  151. #define AC_SHUTTER_FAULT 8192
  152. #define AC_LOCKER_FAULT 16384
  153. #define AC_POWER_DROP 32768
  154. #define AC_CIRCUIT_SHORT 65536
  155. #define AC_ROTARY_SWITCH_FAULT 131072
  156. #define AC_RELAY_DRIVE_FAULT 262144
  157. int _alarm_code[] = {AC_OVP, AC_UVP, AC_OCP, AC_OTP, AC_GMI_FAULT, AC_CP_ERROR, AC_AC_LEAKAGE
  158. , AC_DC_LEAKAGE, AC_SYSTEM_SELFTEST_FAULT, AC_HANDSHAKE_TIMEOUT, AC_EMC_STOP, AC_RELAY_WELDING
  159. , AC_GF_MODULE_FAULT, AC_SHUTTER_FAULT, AC_LOCKER_FAULT, AC_POWER_DROP, AC_CIRCUIT_SHORT
  160. , AC_ROTARY_SWITCH_FAULT, AC_RELAY_DRIVE_FAULT};
  161. void PRINTF_FUNC(char *string, ...);
  162. int StoreLogMsg(const char *fmt, ...);
  163. unsigned long GetTimeoutValue(struct timeval _sour_time);
  164. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  165. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  166. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  167. unsigned long GetTimeoutValue(struct timeval _sour_time)
  168. {
  169. struct timeval _end_time;
  170. gettimeofday(&_end_time, NULL);
  171. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  172. }
  173. int StoreLogMsg(const char *fmt, ...)
  174. {
  175. char Buf[4096+256];
  176. char buffer[4096];
  177. va_list args;
  178. struct timeb SeqEndTime;
  179. struct tm *tm;
  180. va_start(args, fmt);
  181. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  182. va_end(args);
  183. memset(Buf,0,sizeof(Buf));
  184. ftime(&SeqEndTime);
  185. SeqEndTime.time = time(NULL);
  186. tm=localtime(&SeqEndTime.time);
  187. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  188. {
  189. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  190. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  191. printf("%s \n", Buf);
  192. }
  193. else
  194. {
  195. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  196. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  197. buffer,
  198. tm->tm_year+1900,tm->tm_mon+1);
  199. system(Buf);
  200. }
  201. return rc;
  202. }
  203. int DiffTimeb(struct timeb ST, struct timeb ET)
  204. {
  205. //return milli-second
  206. unsigned int StartTime, StopTime;
  207. StartTime = (unsigned int) ST.time;
  208. StopTime = (unsigned int) ET.time;
  209. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  210. return (StopTime - StartTime);
  211. }
  212. unsigned short MaxValue(unsigned short value1, unsigned short value2)
  213. {
  214. return value1 >= value2 ? value1 : value2;
  215. }
  216. void PRINTF_FUNC(char *string, ...)
  217. {
  218. va_list args;
  219. char buffer[4096];
  220. va_start(args, string);
  221. vsnprintf(buffer, sizeof(buffer), string, args);
  222. va_end(args);
  223. DEBUG_INFO("%s \n", buffer);
  224. }
  225. //==========================================
  226. // Communication Function
  227. //==========================================
  228. void GetFwAndHwVersion_Fan()
  229. {
  230. if(Query_FW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
  231. {
  232. // FanModuleData
  233. strcpy((char *) ShmFanModuleData->version, ver.Version_FW);
  234. // SystemInfo
  235. strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, ver.Version_FW);
  236. //PRINTF_FUNC("GetFwAndHwVersion_Fan s1 = %s \n", ver.Version_FW);
  237. }
  238. if (Query_HW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
  239. {
  240. // SystemInfo
  241. strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, ver.Version_FW);
  242. //PRINTF_FUNC("GetFwAndHwVersion_Fan s2 = %s \n", ver.Version_HW);
  243. }
  244. }
  245. void GetFwAndHwVersion_Relay()
  246. {
  247. if (Query_FW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
  248. {
  249. // RelayModuleData
  250. strcpy((char *) ShmRelayModuleData->version, ver.Version_FW);
  251. // SystemInfo
  252. strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, ver.Version_FW);
  253. //PRINTF_FUNC("GetFwAndHwVersion_Relay s1 = %s \n", ver.Version_FW);
  254. }
  255. if (Query_HW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
  256. {
  257. // SystemInfo
  258. strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, ver.Version_FW);
  259. //PRINTF_FUNC("GetFwAndHwVersion_Relay s2 = %s \n", ver.Version_HW);
  260. }
  261. }
  262. void GetFwAndHwVersion_Led()
  263. {
  264. if (Query_FW_Ver(Uart5Fd, Addr.Led, &ver) == PASS)
  265. {
  266. // LedModuleData
  267. strcpy((char *) ShmLedModuleData->version, ver.Version_FW);
  268. // SystemInfo
  269. strcpy((char *) ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, ver.Version_FW);
  270. PRINTF_FUNC("GetFwAndHwVersion_Led s1 = %s \n", ver.Version_FW);
  271. ShmLedModuleData->SelfTest_Comp = YES;
  272. }
  273. else
  274. {
  275. //PRINTF_FUNC("GetFwAndHwVersion_Led fail \n");
  276. }
  277. // if (Query_HW_Ver(Uart5Fd, Addr.Led, &ver) == PASS)
  278. // {
  279. // // SystemInfo
  280. // strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, ver.Version_FW);
  281. // //PRINTF_FUNC("GetFwAndHwVersion_Relay s2 = %s \n", ver.Version_HW);
  282. // }
  283. }
  284. void GetFwVersion_AC()
  285. {
  286. if (Query_FW_Ver(Uart5Fd, Addr.AcPlug, &ver) == PASS)
  287. {
  288. ac_chargingInfo[0]->SelfTest_Comp = YES;
  289. strcpy((char *) ac_chargingInfo[0]->version, ver.Version_FW);
  290. }
  291. }
  292. void GetAcModelName()
  293. {
  294. memset(ShmSysConfigAndInfo->SysConfig.AcModelName, 0x00, sizeof(ShmSysConfigAndInfo->SysConfig.AcModelName));
  295. if (Query_Model_Name(Uart5Fd, Addr.AcPlug, ShmSysConfigAndInfo->SysConfig.AcModelName) == PASS)
  296. {
  297. PRINTF_FUNC("ac model name = %s \n", ShmSysConfigAndInfo->SysConfig.AcModelName);
  298. }
  299. }
  300. void SetRtcData_Relay()
  301. {
  302. struct timeb csuTime;
  303. struct tm *tmCSU;
  304. ftime(&csuTime);
  305. tmCSU = localtime(&csuTime.time);
  306. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  307. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  308. // tmCSU->tm_sec);
  309. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  310. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  311. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  312. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  313. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  314. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  315. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  316. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  317. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  318. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  319. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  320. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  321. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  322. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  323. if (Config_Rtc_Data(Uart5Fd, Addr.Relay, &rtc) == PASS)
  324. {
  325. //PRINTF_FUNC("SetRtc (RB) sucessfully. \n");
  326. }
  327. }
  328. void SetModelName_Relay()
  329. {
  330. if (Config_Model_Name(Uart5Fd, Addr.Relay, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  331. {
  332. PRINTF_FUNC("Set Model name (RB) PASS = %s \n", ShmSysConfigAndInfo->SysConfig.ModelName);
  333. }
  334. }
  335. void SetRtcData_Fan()
  336. {
  337. struct timeb csuTime;
  338. struct tm *tmCSU;
  339. ftime(&csuTime);
  340. tmCSU = localtime(&csuTime.time);
  341. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  342. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  343. // tmCSU->tm_sec);
  344. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  345. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  346. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  347. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  348. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  349. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  350. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  351. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  352. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  353. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  354. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  355. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  356. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  357. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  358. if (Config_Rtc_Data(Uart5Fd, Addr.Fan, &rtc) == PASS)
  359. {
  360. //PRINTF_FUNC("SetRtc (FB) sucessfully. \n");
  361. }
  362. }
  363. void SetModelName_Fan()
  364. {
  365. if (Config_Model_Name(Uart5Fd, Addr.Fan, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  366. {
  367. PRINTF_FUNC("Set Model name (FAN) PASS = %s \n", ShmSysConfigAndInfo->SysConfig.ModelName);
  368. }
  369. }
  370. // AC 三相輸入電壓
  371. void GetPresentInputVol()
  372. {
  373. if (Query_Present_InputVoltage(Uart5Fd, Addr.Relay, &inputVoltage) == PASS)
  374. {
  375. // resolution : 0.1
  376. ShmSysConfigAndInfo->SysInfo.InputVoltageR = ShmRelayModuleData->InputL1Volt = inputVoltage.L1N_L12;
  377. ShmSysConfigAndInfo->SysInfo.InputVoltageS = ShmRelayModuleData->InputL2Volt = inputVoltage.L2N_L23;
  378. ShmSysConfigAndInfo->SysInfo.InputVoltageT = ShmRelayModuleData->InputL3Volt = inputVoltage.L3N_L31;
  379. //********************************************************************************************************//
  380. // Vin (UVP)
  381. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_IEC)
  382. {
  383. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == NO)
  384. {
  385. if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE_IEC)
  386. {
  387. PRINTF_FUNC("In Uvp L1N_L12 = %f \n", inputVoltage.L1N_L12);
  388. if (_threePhaseUvp[0] >= OVP_UVP_CHK_COUNT)
  389. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = YES;
  390. else
  391. _threePhaseUvp[0] += 1;
  392. }
  393. }
  394. else
  395. {
  396. if (inputVoltage.L1N_L12 > VIN_MIN_REV_VOLTAGE_IEC)
  397. {
  398. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = NO;
  399. _threePhaseUvp[0] = 0;
  400. }
  401. }
  402. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == NO)
  403. {
  404. if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE_IEC)
  405. {
  406. PRINTF_FUNC("In Uvp L2N_L23 = %f \n", inputVoltage.L2N_L23);
  407. if (_threePhaseUvp[1] >= OVP_UVP_CHK_COUNT)
  408. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = YES;
  409. else
  410. _threePhaseUvp[1] += 1;
  411. }
  412. }
  413. else
  414. {
  415. if (inputVoltage.L2N_L23 > VIN_MIN_REV_VOLTAGE_IEC)
  416. {
  417. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = NO;
  418. _threePhaseUvp[1] = 0;
  419. }
  420. }
  421. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == NO)
  422. {
  423. if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE_IEC)
  424. {
  425. PRINTF_FUNC("In Uvp L3N_L31 = %f \n", inputVoltage.L3N_L31);
  426. if (_threePhaseUvp[2] >= OVP_UVP_CHK_COUNT)
  427. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = YES;
  428. else
  429. _threePhaseUvp[2] += 1;
  430. }
  431. }
  432. else
  433. {
  434. if (inputVoltage.L3N_L31 > VIN_MIN_REV_VOLTAGE_IEC)
  435. {
  436. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = NO;
  437. _threePhaseUvp[2] = 0;
  438. }
  439. }
  440. }
  441. else if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_UL)
  442. {
  443. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == NO)
  444. {
  445. if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE_UL)
  446. {
  447. PRINTF_FUNC("In Uvp L1N_L12 = %f \n", inputVoltage.L1N_L12);
  448. if (_threePhaseUvp[0] >= OVP_UVP_CHK_COUNT)
  449. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = YES;
  450. else
  451. _threePhaseUvp[0] += 1;
  452. }
  453. }
  454. else
  455. {
  456. if (inputVoltage.L1N_L12 > VIN_MIN_REV_VOLTAGE_UL)
  457. {
  458. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = NO;
  459. _threePhaseUvp[0] = 0;
  460. }
  461. }
  462. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == NO)
  463. {
  464. if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE_UL)
  465. {
  466. PRINTF_FUNC("In Uvp L2N_L23 = %f \n", inputVoltage.L2N_L23);
  467. if (_threePhaseUvp[1] >= OVP_UVP_CHK_COUNT)
  468. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = YES;
  469. else
  470. _threePhaseUvp[1] += 1;
  471. }
  472. }
  473. else
  474. {
  475. if (inputVoltage.L2N_L23 > VIN_MIN_REV_VOLTAGE_UL)
  476. {
  477. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = NO;
  478. _threePhaseUvp[1] = 0;
  479. }
  480. }
  481. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == NO)
  482. {
  483. if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE_UL)
  484. {
  485. PRINTF_FUNC("In Uvp L3N_L31 = %f \n", inputVoltage.L3N_L31);
  486. if (_threePhaseUvp[2] >= OVP_UVP_CHK_COUNT)
  487. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = YES;
  488. else
  489. _threePhaseUvp[2] += 1;
  490. }
  491. }
  492. else
  493. {
  494. if (inputVoltage.L3N_L31 > VIN_MIN_REV_VOLTAGE_UL)
  495. {
  496. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = NO;
  497. _threePhaseUvp[2] = 0;
  498. }
  499. }
  500. }
  501. //********************************************************************************************************//
  502. // Vin (OVP)
  503. if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_IEC)
  504. {
  505. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == NO)
  506. {
  507. if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE_IEC)
  508. {
  509. PRINTF_FUNC("In Ovp L1N_L12 = %f \n", inputVoltage.L1N_L12);
  510. if (_threePhaseOvp[0] >= OVP_UVP_CHK_COUNT)
  511. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = YES;
  512. else
  513. _threePhaseOvp[0] += 1;
  514. }
  515. }
  516. else
  517. {
  518. if (inputVoltage.L1N_L12 < VIN_MAX_REV_VOLTAGE_IEC)
  519. {
  520. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = NO;
  521. _threePhaseOvp[0] = 0;
  522. }
  523. }
  524. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == NO)
  525. {
  526. if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE_IEC)
  527. {
  528. PRINTF_FUNC("In Ovp L2N_L23 = %f \n", inputVoltage.L2N_L23);
  529. if (_threePhaseOvp[1] >= OVP_UVP_CHK_COUNT)
  530. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = YES;
  531. else
  532. _threePhaseOvp[1] += 1;
  533. }
  534. }
  535. else
  536. {
  537. if (inputVoltage.L2N_L23 < VIN_MAX_REV_VOLTAGE_IEC)
  538. {
  539. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = NO;
  540. _threePhaseOvp[1] = 0;
  541. }
  542. }
  543. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == NO)
  544. {
  545. if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE_IEC)
  546. {
  547. PRINTF_FUNC("In Ovp L3N_L31 = %f \n", inputVoltage.L3N_L31);
  548. if (_threePhaseOvp[2] >= OVP_UVP_CHK_COUNT)
  549. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = YES;
  550. else
  551. _threePhaseOvp[2] += 1;
  552. }
  553. }
  554. else
  555. {
  556. if (inputVoltage.L3N_L31 < VIN_MAX_REV_VOLTAGE_IEC)
  557. {
  558. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = NO;
  559. _threePhaseOvp[2] = 0;
  560. }
  561. }
  562. }
  563. else if (ShmSysConfigAndInfo->SysInfo.ChargerType == _CHARGER_TYPE_UL)
  564. {
  565. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP == NO)
  566. {
  567. if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE_UL)
  568. {
  569. PRINTF_FUNC("In Ovp L1N_L12 = %f \n", inputVoltage.L1N_L12);
  570. if (_threePhaseOvp[0] >= OVP_UVP_CHK_COUNT)
  571. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = YES;
  572. else
  573. _threePhaseOvp[0] += 0;
  574. }
  575. }
  576. else
  577. {
  578. if (inputVoltage.L1N_L12 < VIN_MAX_REV_VOLTAGE_UL)
  579. {
  580. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = NO;
  581. _threePhaseOvp[0] = 0;
  582. }
  583. }
  584. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP == NO)
  585. {
  586. if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE_UL)
  587. {
  588. PRINTF_FUNC("In Ovp L2N_L23 = %f \n", inputVoltage.L2N_L23);
  589. if (_threePhaseOvp[1] >= OVP_UVP_CHK_COUNT)
  590. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = YES;
  591. else
  592. _threePhaseOvp[1] += 0;
  593. }
  594. }
  595. else
  596. {
  597. if (inputVoltage.L2N_L23 < VIN_MAX_REV_VOLTAGE_UL)
  598. {
  599. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = NO;
  600. _threePhaseOvp[1] = 0;
  601. }
  602. }
  603. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP == NO)
  604. {
  605. if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE_UL)
  606. {
  607. PRINTF_FUNC("In Ovp L3N_L31 = %f \n", inputVoltage.L3N_L31);
  608. if (_threePhaseOvp[2] >= OVP_UVP_CHK_COUNT)
  609. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = YES;
  610. else
  611. _threePhaseOvp[2] += 1;
  612. }
  613. }
  614. else
  615. {
  616. if (inputVoltage.L3N_L31 < VIN_MAX_REV_VOLTAGE_UL)
  617. {
  618. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = NO;
  619. _threePhaseOvp[2] = 0;
  620. }
  621. }
  622. }
  623. }
  624. }
  625. // 左右槍的 Relay 前後的輸出電壓
  626. void GetPersentOutputVol()
  627. {
  628. if (Query_Present_OutputVoltage(Uart5Fd, Addr.Relay, &outputVoltage) == PASS)
  629. {
  630. // PRINTF_FUNC("Conn1 fuse 1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
  631. // PRINTF_FUNC("Conn1 relay 1 = %f \n", outputVoltage.behindRelay_Voltage_C1);
  632. // PRINTF_FUNC("Conn2 fuse 2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
  633. // PRINTF_FUNC("Conn2 relay 2 = %f \n", outputVoltage.behindRelay_Voltage_C2);
  634. //PRINTF_FUNC("outputVoltage.behindFuse_Voltage_C1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
  635. //PRINTF_FUNC("outputVoltage.behindFuse_Voltage_C2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
  636. ShmRelayModuleData->Gun1FuseOutputVolt = outputVoltage.behindFuse_Voltage_C1;
  637. ShmRelayModuleData->Gun1RelayOutputVolt = outputVoltage.behindRelay_Voltage_C1;
  638. ShmRelayModuleData->Gun2FuseOutputVolt = outputVoltage.behindFuse_Voltage_C2;
  639. ShmRelayModuleData->Gun2RelayOutputVolt = outputVoltage.behindRelay_Voltage_C2;
  640. for (int index = 0; index < gunCount; index++)
  641. {
  642. if (index == 0)
  643. {
  644. if (_chargingData[index]->Evboard_id == 0x01)
  645. {
  646. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun1FuseOutputVolt;
  647. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun1RelayOutputVolt;
  648. }
  649. else if (_chargingData[index]->Evboard_id == 0x02)
  650. {
  651. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
  652. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
  653. }
  654. }
  655. else if (index == 1)
  656. {
  657. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
  658. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
  659. }
  660. //unsigned short Ovp = 0;
  661. //unsigned short Ocp = 0;
  662. //Ovp = MIN [VOUT_MAX_VOLTAGE, EV_BATTERY_VOLTAGE] // 最大輸出電壓與電池電壓最大值
  663. //Ocp = MIN [IOUT_MAX_CURRENT, EV_CURRENT_REQ] // 最大輸出電流與需求電流最小值
  664. if (_chargingData[index]->Type == _Type_Chademo)
  665. {
  666. //Ovp = MaxValue(_chargingData[index]->MaximumChargingVoltage, _chargingData[index]->EvBatteryMaxVoltage);
  667. //Ocp = MaxValue(_chargingData[index]->PresentChargingCurrent, ShmCHAdeMOData->ev[_chargingData[index]->type_index].ChargingCurrentRequest);
  668. }
  669. else if (_chargingData[index]->Type == _Type_CCS_2)
  670. {
  671. }
  672. }
  673. }
  674. }
  675. // 風扇速度
  676. void GetFanSpeed()
  677. {
  678. //PRINTF_FUNC("Get fan board speed \n");
  679. if (Query_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed) == PASS)
  680. {
  681. ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0];
  682. ShmFanModuleData->PresentFan2Speed = fanSpeed.speed[1];
  683. ShmFanModuleData->PresentFan3Speed = fanSpeed.speed[2];
  684. ShmFanModuleData->PresentFan4Speed = fanSpeed.speed[3];
  685. // PRINTF_FUNC("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
  686. // PRINTF_FUNC("SystemFanRotaSpeed_2 = %d \n", fanSpeed.speed[1]);
  687. // PRINTF_FUNC("SystemFanRotaSpeed_3 = %d \n", fanSpeed.speed[2]);
  688. // PRINTF_FUNC("SystemFanRotaSpeed_4 = %d \n", fanSpeed.speed[3]);
  689. // Config_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed[0]);
  690. //SysInfoData (SystemFanRotaSpeed)
  691. }
  692. }
  693. // 讀取 Relay 狀態
  694. void GetRelayOutputStatus()
  695. {
  696. if (Query_Relay_Output(Uart5Fd, Addr.Relay, &regRelay) == PASS)
  697. {
  698. regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  699. }
  700. }
  701. // 確認 K1 K2 relay 的狀態
  702. void CheckK1K2RelayOutput(byte index)
  703. {
  704. if (index == 0)
  705. {
  706. if (_chargingData[index]->Evboard_id == 0x01)
  707. {
  708. if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.Gun1_P == YES)
  709. _chargingData[index]->RelayK1K2Status = YES;
  710. else
  711. _chargingData[index]->RelayK1K2Status = NO;
  712. if(_chargingData[index]->Type == _Type_CCS_2)
  713. {
  714. if (gunCount == 1)
  715. {
  716. if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  717. _chargingData[index]->RelayKPK2Status = YES;
  718. else
  719. _chargingData[index]->RelayKPK2Status = NO;
  720. }
  721. else
  722. {
  723. if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST0)
  724. _chargingData[index]->RelayKPK2Status = YES;
  725. else
  726. _chargingData[index]->RelayKPK2Status = NO;
  727. }
  728. }
  729. }
  730. else if (_chargingData[index]->Evboard_id == 0x02)
  731. {
  732. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
  733. _chargingData[index]->RelayK1K2Status = YES;
  734. else
  735. _chargingData[index]->RelayK1K2Status = NO;
  736. if(_chargingData[index]->Type == _Type_CCS_2)
  737. {
  738. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  739. _chargingData[index]->RelayKPK2Status = YES;
  740. else
  741. _chargingData[index]->RelayKPK2Status = NO;
  742. }
  743. }
  744. }
  745. else if (index == 1)
  746. {
  747. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
  748. _chargingData[index]->RelayK1K2Status = YES;
  749. else
  750. _chargingData[index]->RelayK1K2Status = NO;
  751. if(_chargingData[index]->Type == _Type_CCS_2)
  752. {
  753. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  754. _chargingData[index]->RelayKPK2Status = YES;
  755. else
  756. _chargingData[index]->RelayKPK2Status = NO;
  757. }
  758. }
  759. if (regRelay.relay_event.bits.Gun1_Parallel_N == YES && regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  760. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = YES;
  761. else
  762. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = NO;
  763. // PRINTF_FUNC("Check Relay Output. index = %d, RelayKPK2Status = %d, BridgeRelayStatus = %d \n",
  764. // index, _chargingData[index]->RelayKPK2Status, ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus);
  765. }
  766. void GetGfdAdc()
  767. {
  768. // define : 每 0.2 ~ 1 秒一次
  769. // occur : <= 75k 歐姆 @ 150 - 750 Vdc
  770. // warning : >= 100 歐姆 && <= 500 歐姆 @ 150-750 Vdc
  771. if (Query_Gfd_Adc(Uart5Fd, Addr.Relay, &gfd_adc) == PASS)
  772. {
  773. for (int i = 0; i < gunCount; i++)
  774. {
  775. if (_chargingData[i]->Type == 0x09 && !ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag)
  776. {
  777. if ((_chargingData[i]->PresentChargingVoltage * 10) >= VOUT_MIN_VOLTAGE)
  778. _chargingData[i]->GroundFaultStatus = GFD_PASS;
  779. continue;
  780. }
  781. if (i == 0)
  782. {
  783. _chargingData[i]->GroundFaultStatus = gfd_adc.result_conn1;
  784. // PRINTF_FUNC("GFD ******** Result = %d, Step = %d, R = %d, Vol = %d \n",
  785. // _chargingData[i]->GroundFaultStatus, gfd_adc.rb_step_1, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  786. if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
  787. {
  788. PRINTF_FUNC("GFD Fail. index = %d, Step = %d, R = %d, Vol = %d \n",
  789. i, gfd_adc.rb_step_1, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  790. }
  791. else if (_chargingData[i]->GroundFaultStatus == GFD_PASS ||
  792. _chargingData[i]->GroundFaultStatus == GFD_WARNING)
  793. {
  794. if (_chargingData[i]->GroundFaultStatus == GFD_WARNING)
  795. {
  796. PRINTF_FUNC("GFD Warning. index = %d, Result = %d, R = %d, Vol = %d \n",
  797. i, _chargingData[i]->GroundFaultStatus, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  798. }
  799. }
  800. }
  801. else if (i == 1)
  802. {
  803. _chargingData[i]->GroundFaultStatus = gfd_adc.result_conn2;
  804. if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
  805. {
  806. PRINTF_FUNC("GFD Fail. index = %d, Step = %d, R = %d, Vol = %d \n",
  807. i, gfd_adc.rb_step_2, gfd_adc.Resister_conn2, gfd_adc.voltage_conn2);
  808. }
  809. else if (_chargingData[i]->GroundFaultStatus == GFD_PASS ||
  810. _chargingData[i]->GroundFaultStatus == GFD_WARNING)
  811. {
  812. if (_chargingData[i]->GroundFaultStatus == GFD_WARNING)
  813. {
  814. PRINTF_FUNC("GFD Warning. index = %d, Result = %d, R = %d, Vol = %d \n",
  815. i, _chargingData[i]->GroundFaultStatus, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  816. }
  817. }
  818. }
  819. }
  820. }
  821. }
  822. void GetGpioInput()
  823. {
  824. if (Query_Gpio_Input(Uart5Fd, Addr.Aux, &gpio_in) == PASS)
  825. {
  826. // AC Contactor Status
  827. if (gpio_in.AC_MainBreaker == 1)
  828. {
  829. // AC Main Breaker ON
  830. PRINTF_FUNC("RB AC Main Breaker. \n");
  831. }
  832. if (gpio_in.SPD == 1)
  833. {
  834. // SPD (雷擊保護) ON
  835. PRINTF_FUNC("RB SPD. \n");
  836. }
  837. if (gpio_in.Door_Open == 1)
  838. {
  839. // Door Open
  840. PRINTF_FUNC("RB Door Open. \n");
  841. }
  842. if (gpio_in.GFD[0] == 1)
  843. {
  844. // GFD_1 Trigger
  845. }
  846. if (gpio_in.GFD[1] == 1)
  847. {
  848. // GFD_2 Trigger
  849. }
  850. if (gpio_in.AC_Drop == 1)
  851. {
  852. // AC Drop
  853. PRINTF_FUNC("RB AC Drop. \n");
  854. }
  855. if (gpio_in.Emergency_IO == 1)
  856. {
  857. // Emergency IO ON
  858. PRINTF_FUNC("RB Emergency IO ON. \n");
  859. }
  860. if (gpio_in.Button_Emergency_Press == 1)
  861. {
  862. // Emergency button Press
  863. }
  864. if (gpio_in.Button_On_Press == 1)
  865. {
  866. // On button Press
  867. }
  868. if (gpio_in.Button_Off_Press == 1)
  869. {
  870. // Off button Press
  871. }
  872. if (gpio_in.Key_1_Press == 1)
  873. {
  874. // key 1 press
  875. }
  876. if (gpio_in.Key_2_Press == 1)
  877. {
  878. // key 2 press
  879. }
  880. if (gpio_in.Key_3_Press == 1)
  881. {
  882. // key 3 press
  883. }
  884. if (gpio_in.Key_4_Press == 1)
  885. {
  886. // key 4 press
  887. }
  888. }
  889. }
  890. // 5V 12V 24V 48V
  891. void GetAuxPower()
  892. {
  893. if (Query_Aux_PowerVoltage(Uart5Fd, Addr.Fan, &auxPower) == PASS)
  894. {
  895. ShmSysConfigAndInfo->SysInfo.AuxPower48V = auxPower.voltage[0];
  896. ShmSysConfigAndInfo->SysInfo.AuxPower24V = auxPower.voltage[1];
  897. //ShmSysConfigAndInfo->SysInfo.AuxPower12V = auxPower.voltage[4];
  898. //ShmSysConfigAndInfo->SysInfo.AuxPower5V = auxPower.voltage[6];
  899. // aux power voltage
  900. //PRINTF_FUNC("aux1 = %x, \n", auxPower.voltage[0]);
  901. //PRINTF_FUNC("aux2 = %x, \n", auxPower.voltage[1]);
  902. }
  903. }
  904. void SetFanModuleSpeed()
  905. {
  906. {
  907. FanSpeed _fanSpeed;
  908. _setFanSpeed += fanSpeedSmoothValue;
  909. if (_setFanSpeed >= ShmFanModuleData->SetFan1Speed)
  910. _setFanSpeed = ShmFanModuleData->SetFan1Speed;
  911. //printf("_setFanSpeed = %d \n", _setFanSpeed);
  912. _fanSpeed.speed[0] = _setFanSpeed;
  913. _fanSpeed.speed[1] = _setFanSpeed;
  914. _fanSpeed.speed[2] = _setFanSpeed;
  915. _fanSpeed.speed[3] = _setFanSpeed;
  916. if (Config_Fan_Speed(Uart5Fd, Addr.Fan, &_fanSpeed) == PASS)
  917. {
  918. //PRINTF_FUNC("successfully Fan\n");
  919. }
  920. }
  921. }
  922. //==========================================
  923. // Common Function
  924. //==========================================
  925. void SetK1K2RelayStatus(byte index)
  926. {
  927. if (ShmPsuData->Work_Step >= _TEST_MODE && ShmPsuData->Work_Step <= _TEST_MODE)
  928. {
  929. if(regRelay.relay_event.bits.Gun1_N == NO)
  930. outputRelay.relay_event.bits.Gun1_N = YES;
  931. else if (regRelay.relay_event.bits.Gun1_P == NO)
  932. outputRelay.relay_event.bits.Gun1_P = YES;
  933. return;
  934. }
  935. if (_chargingData[index]->SystemStatus < S_PREPARING_FOR_EVSE)
  936. {
  937. if (_chargingData[index]->Evboard_id == 0x01)
  938. {
  939. if(regRelay.relay_event.bits.Gun1_P == YES)
  940. outputRelay.relay_event.bits.Gun1_P = NO;
  941. else if (regRelay.relay_event.bits.Gun1_N == YES)
  942. outputRelay.relay_event.bits.Gun1_N = NO;
  943. if (gunCount == 1 && _chargingData[index]->Type == _Type_CCS_2)
  944. {
  945. if(regRelay.relay_event.bits.CCS_Precharge == YES)
  946. outputRelay.relay_event.bits.CCS_Precharge = NO;
  947. }
  948. }
  949. else if (_chargingData[index]->Evboard_id == 0x02)
  950. {
  951. if(regRelay.relay_event.bits.Gun2_P == YES)
  952. outputRelay.relay_event.bits.Gun2_P = NO;
  953. else if (regRelay.relay_event.bits.Gun2_N == YES)
  954. outputRelay.relay_event.bits.Gun2_N = NO;
  955. if (_chargingData[index]->Type == _Type_CCS_2)
  956. {
  957. if(regRelay.relay_event.bits.CCS_Precharge == YES)
  958. outputRelay.relay_event.bits.CCS_Precharge = NO;
  959. }
  960. }
  961. }
  962. else if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE &&
  963. _chargingData[index]->SystemStatus <= S_CHARGING))
  964. {
  965. if (_chargingData[index]->RelayWeldingCheck == YES)
  966. {
  967. if (_chargingData[index]->Evboard_id == 0x01)
  968. {
  969. if(regRelay.relay_event.bits.Gun1_N == NO)
  970. outputRelay.relay_event.bits.Gun1_N = YES;
  971. else if (regRelay.relay_event.bits.Gun1_P == NO)
  972. outputRelay.relay_event.bits.Gun1_P = YES;
  973. }
  974. else if (_chargingData[index]->Evboard_id == 0x02)
  975. {
  976. if(regRelay.relay_event.bits.Gun2_N == NO)
  977. outputRelay.relay_event.bits.Gun2_N = YES;
  978. else if (regRelay.relay_event.bits.Gun2_P == NO)
  979. outputRelay.relay_event.bits.Gun2_P = YES;
  980. }
  981. }
  982. }
  983. else if ((_chargingData[index]->SystemStatus >= S_TERMINATING &&
  984. _chargingData[index]->SystemStatus <= S_COMPLETE))
  985. {
  986. if ((_chargingData[index]->PresentChargingCurrent * 10) <= SEFETY_SWITCH_RELAY_CUR)
  987. {
  988. if (_chargingData[index]->Evboard_id == 0x01)
  989. {
  990. if(regRelay.relay_event.bits.Gun1_P == YES)
  991. outputRelay.relay_event.bits.Gun1_P = NO;
  992. else if (regRelay.relay_event.bits.Gun1_N == YES)
  993. outputRelay.relay_event.bits.Gun1_N = NO;
  994. }
  995. else if (_chargingData[index]->Evboard_id == 0x02)
  996. {
  997. if(regRelay.relay_event.bits.Gun2_P == YES)
  998. outputRelay.relay_event.bits.Gun2_P = NO;
  999. else if (regRelay.relay_event.bits.Gun2_N == YES)
  1000. outputRelay.relay_event.bits.Gun2_N = NO;
  1001. }
  1002. }
  1003. }
  1004. else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST0)
  1005. {
  1006. if (_chargingData[index]->Evboard_id == 0x01)
  1007. {
  1008. if (_chargingData[index]->Type == _Type_CCS_2)
  1009. {
  1010. if (gunCount == 1)
  1011. {
  1012. if (regRelay.relay_event.bits.CCS_Precharge == NO)
  1013. outputRelay.relay_event.bits.CCS_Precharge = YES;
  1014. else if (regRelay.relay_event.bits.CCS_Precharge == YES)
  1015. outputRelay.relay_event.bits.Gun1_P = NO;
  1016. }
  1017. }
  1018. }
  1019. else if (_chargingData[index]->Evboard_id == 0x02)
  1020. {
  1021. if (_chargingData[index]->Type == _Type_CCS_2)
  1022. {
  1023. if (regRelay.relay_event.bits.CCS_Precharge == NO)
  1024. outputRelay.relay_event.bits.CCS_Precharge = YES;
  1025. else if (regRelay.relay_event.bits.CCS_Precharge == YES)
  1026. outputRelay.relay_event.bits.Gun2_P = NO;
  1027. }
  1028. }
  1029. }
  1030. else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST1)
  1031. {
  1032. if (_chargingData[index]->Evboard_id == 0x01)
  1033. {
  1034. if (_chargingData[index]->Type == _Type_CCS_2)
  1035. {
  1036. if (gunCount == 1)
  1037. {
  1038. if (regRelay.relay_event.bits.Gun1_P == NO)
  1039. outputRelay.relay_event.bits.Gun1_P = YES;
  1040. else if(regRelay.relay_event.bits.Gun1_P == YES)
  1041. outputRelay.relay_event.bits.CCS_Precharge = NO;
  1042. }
  1043. }
  1044. }
  1045. else if (_chargingData[index]->Evboard_id == 0x02)
  1046. {
  1047. if (_chargingData[index]->Type == _Type_CCS_2)
  1048. {
  1049. if (regRelay.relay_event.bits.Gun2_P == NO)
  1050. outputRelay.relay_event.bits.Gun2_P = YES;
  1051. else if(regRelay.relay_event.bits.Gun2_P == YES)
  1052. outputRelay.relay_event.bits.CCS_Precharge = NO;
  1053. }
  1054. }
  1055. }
  1056. }
  1057. void SetParalleRelayStatus()
  1058. {
  1059. // 之後雙槍單模機種,橋接都會上
  1060. if (gunCount >= 2 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  1061. {
  1062. if (_chargingData[0]->SystemStatus == S_BOOTING || _chargingData[1]->SystemStatus == S_BOOTING ||
  1063. ((_chargingData[0]->SystemStatus == S_IDLE || _chargingData[0]->SystemStatus == S_MAINTAIN) &&
  1064. (_chargingData[1]->SystemStatus == S_IDLE || _chargingData[1]->SystemStatus == S_MAINTAIN)))
  1065. {
  1066. // 初始化~ 不搭橋接
  1067. if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  1068. outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
  1069. else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
  1070. outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
  1071. }
  1072. else
  1073. {
  1074. if (_chargingData[0]->IsReadyToCharging == YES ||
  1075. _chargingData[1]->IsReadyToCharging == YES)
  1076. {
  1077. // ************需考慮在切換中 - 切開 relay 與搭回 relay 的時機點************
  1078. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  1079. {
  1080. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag < _REASSIGNED_RELAY_M_TO_A)
  1081. {
  1082. // 最大充 - 搭上橋接
  1083. if (regRelay.relay_event.bits.Gun1_Parallel_N == NO)
  1084. outputRelay.relay_event.bits.Gun1_Parallel_N = YES;
  1085. else if (regRelay.relay_event.bits.Gun1_Parallel_P == NO)
  1086. outputRelay.relay_event.bits.Gun1_Parallel_P = YES;
  1087. }
  1088. else
  1089. {
  1090. // 平均充 - 不搭
  1091. if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  1092. outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
  1093. else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
  1094. outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
  1095. }
  1096. }
  1097. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  1098. {
  1099. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag < _REASSIGNED_RELAY_A_TO_M)
  1100. {
  1101. // 平均充 - 不搭
  1102. if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  1103. outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
  1104. else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
  1105. outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
  1106. }
  1107. else
  1108. {
  1109. // 最大充 - 搭上橋接
  1110. if (regRelay.relay_event.bits.Gun1_Parallel_N == NO)
  1111. outputRelay.relay_event.bits.Gun1_Parallel_N = YES;
  1112. else if (regRelay.relay_event.bits.Gun1_Parallel_P == NO)
  1113. outputRelay.relay_event.bits.Gun1_Parallel_P = YES;
  1114. }
  1115. }
  1116. }
  1117. }
  1118. }
  1119. }
  1120. void CheckAlarmOccur()
  1121. {
  1122. bool isErr = false;
  1123. for(byte count = 0; count < sizeof(_alarm_code)/sizeof(_alarm_code[0]); count++)
  1124. {
  1125. if (acAlarmCode.AcAlarmCode & _alarm_code[count])
  1126. {
  1127. isErr = true;
  1128. switch(_alarm_code[count])
  1129. {
  1130. case AC_OVP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcSystemInputOVP = YES; break;
  1131. case AC_UVP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcSystemInputUVP = YES; break;
  1132. case AC_OCP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = YES; break;
  1133. case AC_OTP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAmbientOTP = YES; break;
  1134. case AC_GMI_FAULT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcGroundfaultFail = YES; break;
  1135. case AC_CP_ERROR: ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault = YES; break;
  1136. case AC_AC_LEAKAGE: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip = YES; break;
  1137. case AC_DC_LEAKAGE: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip = YES; break;
  1138. case AC_SYSTEM_SELFTEST_FAULT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.McuSelftestFail = YES; break;
  1139. case AC_HANDSHAKE_TIMEOUT: break;
  1140. //case AC_EMC_STOP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = YES; break;
  1141. case AC_RELAY_WELDING: ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayWelding = YES; break;
  1142. case AC_GF_MODULE_FAULT: ShmStatusCodeData->FaultCode.FaultEvents.bits.RcdSelfTestFail = YES; break;
  1143. case AC_SHUTTER_FAULT: break;
  1144. case AC_LOCKER_FAULT: ShmStatusCodeData->FaultCode.FaultEvents.bits.AcConnectorLockFail = YES; break;
  1145. case AC_POWER_DROP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = YES; break;
  1146. case AC_CIRCUIT_SHORT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CircuitShort = YES; break;
  1147. case AC_ROTARY_SWITCH_FAULT: break;
  1148. case AC_RELAY_DRIVE_FAULT: ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayDrivingFault = YES; break;
  1149. }
  1150. }
  1151. else
  1152. {
  1153. switch(_alarm_code[count])
  1154. {
  1155. case AC_OVP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcSystemInputOVP = NO; break;
  1156. case AC_UVP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcSystemInputUVP = NO; break;
  1157. case AC_OCP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = NO; break;
  1158. case AC_OTP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAmbientOTP = NO; break;
  1159. case AC_GMI_FAULT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcGroundfaultFail = NO; break;
  1160. case AC_CP_ERROR: ShmStatusCodeData->InfoCode.InfoEvents.bits.PilotFault = NO; break;
  1161. case AC_AC_LEAKAGE: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip = NO; break;
  1162. case AC_DC_LEAKAGE: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RcdTrip = NO; break;
  1163. case AC_SYSTEM_SELFTEST_FAULT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.McuSelftestFail = NO; break;
  1164. case AC_HANDSHAKE_TIMEOUT: break;
  1165. //case AC_EMC_STOP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.EmergencyStopTrip = NO; break;
  1166. case AC_RELAY_WELDING: ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayWelding = NO; break;
  1167. case AC_GF_MODULE_FAULT: ShmStatusCodeData->FaultCode.FaultEvents.bits.RcdSelfTestFail = NO; break;
  1168. case AC_SHUTTER_FAULT: break;
  1169. case AC_LOCKER_FAULT: ShmStatusCodeData->FaultCode.FaultEvents.bits.AcConnectorLockFail = NO; break;
  1170. case AC_POWER_DROP: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = NO; break;
  1171. case AC_CIRCUIT_SHORT: ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CircuitShort = NO; break;
  1172. case AC_ROTARY_SWITCH_FAULT: break;
  1173. case AC_RELAY_DRIVE_FAULT: ShmStatusCodeData->FaultCode.FaultEvents.bits.AcOutputRelayDrivingFault = NO; break;
  1174. }
  1175. }
  1176. }
  1177. ac_chargingInfo[0]->IsErrorOccur = isErr;
  1178. }
  1179. bool IsNoneMatchLedColor()
  1180. {
  1181. bool result = false;
  1182. if (cur_led_color.Connect_1_Red != led_color.Connect_1_Red ||
  1183. cur_led_color.Connect_1_Green != led_color.Connect_1_Green ||
  1184. cur_led_color.Connect_1_Blue != led_color.Connect_1_Blue ||
  1185. cur_led_color.Connect_2_Red != led_color.Connect_2_Red ||
  1186. cur_led_color.Connect_2_Green != led_color.Connect_2_Green ||
  1187. cur_led_color.Connect_2_Blue != led_color.Connect_2_Blue)
  1188. {
  1189. result = true;
  1190. }
  1191. return result;
  1192. }
  1193. void SetLedColor(struct ChargingInfoData *chargingData_1, struct ChargingInfoData *chargingData_2)
  1194. {
  1195. byte _colorBuf = COLOR_MAX_LV * LED_INTENSITY_BRIGHTEST;
  1196. if (ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity == _LED_INTENSITY_DARKEST)
  1197. _colorBuf = COLOR_MAX_LV * LED_INTENSITY_DARKEST;
  1198. else if (ShmSysConfigAndInfo->SysConfig.LedInfo.Intensity == _LED_INTENSITY_MEDIUM)
  1199. _colorBuf = COLOR_MAX_LV * LED_INTENSITY_MEDIUM;
  1200. if (ShmSysConfigAndInfo->SysWarningInfo.Level == 2)
  1201. {
  1202. led_color.Connect_1_Green = COLOR_MIN_LV;
  1203. led_color.Connect_1_Blue = COLOR_MIN_LV;
  1204. led_color.Connect_1_Red = _colorBuf;
  1205. led_color.Connect_2_Green = COLOR_MIN_LV;
  1206. led_color.Connect_2_Blue = COLOR_MIN_LV;
  1207. led_color.Connect_2_Red = _colorBuf;
  1208. }
  1209. else
  1210. {
  1211. if (ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf)
  1212. {
  1213. if ((chargingData_1->SystemStatus == S_BOOTING || chargingData_1->SystemStatus == S_IDLE || chargingData_1->SystemStatus == S_RESERVATION) &&
  1214. (chargingData_2->SystemStatus == S_BOOTING || chargingData_2->SystemStatus == S_IDLE || chargingData_2->SystemStatus == S_RESERVATION))
  1215. {
  1216. led_color.Connect_1_Green = _colorBuf;
  1217. led_color.Connect_1_Blue = COLOR_MIN_LV;
  1218. led_color.Connect_1_Red = COLOR_MIN_LV;
  1219. led_color.Connect_2_Green = _colorBuf;
  1220. led_color.Connect_2_Blue = COLOR_MIN_LV;
  1221. led_color.Connect_2_Red = COLOR_MIN_LV;
  1222. }
  1223. else if ((chargingData_1->SystemStatus >= S_AUTHORIZING && chargingData_1->SystemStatus <= S_COMPLETE) ||
  1224. (chargingData_1->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingData_1->SystemStatus <= S_CCS_PRECHARGE_ST1) ||
  1225. (chargingData_2->SystemStatus >= S_AUTHORIZING && chargingData_2->SystemStatus <= S_COMPLETE) ||
  1226. (chargingData_2->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingData_2->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1227. {
  1228. led_color.Connect_1_Green = COLOR_MIN_LV;
  1229. led_color.Connect_1_Blue = _colorBuf;
  1230. led_color.Connect_1_Red = COLOR_MIN_LV;
  1231. led_color.Connect_2_Green = COLOR_MIN_LV;
  1232. led_color.Connect_2_Blue = _colorBuf;
  1233. led_color.Connect_2_Red = COLOR_MIN_LV;
  1234. }
  1235. }
  1236. else
  1237. {
  1238. if (chargingData_1->SystemStatus == S_BOOTING || chargingData_1->SystemStatus == S_IDLE || chargingData_1->SystemStatus == S_RESERVATION)
  1239. {
  1240. led_color.Connect_1_Green = _colorBuf;
  1241. led_color.Connect_1_Blue = COLOR_MIN_LV;
  1242. led_color.Connect_1_Red = COLOR_MIN_LV;
  1243. }
  1244. else if ((chargingData_1->SystemStatus >= S_AUTHORIZING && chargingData_1->SystemStatus <= S_COMPLETE) ||
  1245. (chargingData_1->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingData_1->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1246. {
  1247. led_color.Connect_1_Green = COLOR_MIN_LV;
  1248. led_color.Connect_1_Blue = _colorBuf;
  1249. led_color.Connect_1_Red = COLOR_MIN_LV;
  1250. }
  1251. // --------------------------------------------------------------------------
  1252. if (chargingData_2->SystemStatus == S_BOOTING || chargingData_2->SystemStatus == S_IDLE || chargingData_2->SystemStatus == S_RESERVATION)
  1253. {
  1254. led_color.Connect_2_Green = _colorBuf;
  1255. led_color.Connect_2_Blue = COLOR_MIN_LV;
  1256. led_color.Connect_2_Red = COLOR_MIN_LV;
  1257. }
  1258. else if ((chargingData_2->SystemStatus >= S_AUTHORIZING && chargingData_2->SystemStatus <= S_COMPLETE) ||
  1259. (chargingData_2->SystemStatus >= S_CCS_PRECHARGE_ST0 && chargingData_2->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1260. {
  1261. led_color.Connect_2_Green = COLOR_MIN_LV;
  1262. led_color.Connect_2_Blue = _colorBuf;
  1263. led_color.Connect_2_Red = COLOR_MIN_LV;
  1264. }
  1265. }
  1266. }
  1267. if (_checkLedChanged > 0)
  1268. {
  1269. if (Config_Led_Color(Uart5Fd, Addr.Led, &led_color) == PASS)
  1270. {
  1271. _checkLedChanged--;
  1272. cur_led_color.Connect_1_Red = led_color.Connect_1_Red;
  1273. cur_led_color.Connect_1_Green = led_color.Connect_1_Green;
  1274. cur_led_color.Connect_1_Blue = led_color.Connect_1_Blue;
  1275. cur_led_color.Connect_2_Red = led_color.Connect_2_Red;
  1276. cur_led_color.Connect_2_Green = led_color.Connect_2_Green;
  1277. cur_led_color.Connect_2_Blue = led_color.Connect_2_Blue;
  1278. }
  1279. }
  1280. else if (IsNoneMatchLedColor())
  1281. _checkLedChanged = 3;
  1282. }
  1283. //==========================================
  1284. // Init all share memory
  1285. //==========================================
  1286. int InitShareMemory()
  1287. {
  1288. int result = PASS;
  1289. int MeterSMId;
  1290. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  1291. {
  1292. #ifdef SystemLogMessage
  1293. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1294. #endif
  1295. result = FAIL;
  1296. }
  1297. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1298. {
  1299. #ifdef SystemLogMessage
  1300. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  1301. #endif
  1302. result = FAIL;
  1303. }
  1304. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  1305. {
  1306. #ifdef SystemLogMessage
  1307. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1308. #endif
  1309. result = FAIL;
  1310. }
  1311. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1312. {
  1313. #ifdef SystemLogMessage
  1314. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1315. #endif
  1316. result = FAIL;
  1317. }
  1318. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), 0777)) < 0)
  1319. {
  1320. #ifdef SystemLogMessage
  1321. DEBUG_ERROR("shmget ShmFanModuleData NG\n");
  1322. #endif
  1323. result = FAIL;
  1324. }
  1325. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1326. {
  1327. #ifdef SystemLogMessage
  1328. DEBUG_ERROR("shmat ShmFanModuleData NG\n");
  1329. #endif
  1330. result = FAIL;
  1331. }
  1332. memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
  1333. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), 0777)) < 0)
  1334. {
  1335. #ifdef SystemLogMessage
  1336. DEBUG_ERROR("shmget ShmRelayModuleData NG\n");
  1337. #endif
  1338. result = FAIL;
  1339. }
  1340. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1341. {
  1342. #ifdef SystemLogMessage
  1343. DEBUG_ERROR("shmat ShmRelayModuleData NG\n");
  1344. #endif
  1345. result = FAIL;
  1346. }
  1347. memset(ShmRelayModuleData,0,sizeof(struct RelayModuleData));
  1348. if ((MeterSMId = shmget(ShmLedBdKey, sizeof(struct LedModuleData), 0777)) < 0)
  1349. {
  1350. #ifdef SystemLogMessage
  1351. DEBUG_ERROR("shmget ShmLedModuleData NG\n");
  1352. #endif
  1353. result = FAIL;
  1354. }
  1355. else if ((ShmLedModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1356. {
  1357. #ifdef SystemLogMessage
  1358. DEBUG_ERROR("shmat ShmLedModuleData NG\n");
  1359. #endif
  1360. result = FAIL;
  1361. }
  1362. memset(ShmLedModuleData,0,sizeof(struct LedModuleData));
  1363. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  1364. {
  1365. #ifdef SystemLogMessage
  1366. DEBUG_ERROR("shmget ShmPsuData NG \n");
  1367. #endif
  1368. result = FAIL;
  1369. }
  1370. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1371. {
  1372. #ifdef SystemLogMessage
  1373. DEBUG_ERROR("shmat ShmPsuData NG \n");
  1374. #endif
  1375. result = FAIL;
  1376. }
  1377. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  1378. {
  1379. #ifdef SystemLogMessage
  1380. DEBUG_ERROR("shmat ShmOCPP16Data NG \n");
  1381. #endif
  1382. result = FAIL;
  1383. }
  1384. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  1385. {
  1386. #ifdef SystemLogMessage
  1387. DEBUG_ERROR("shmat ShmOCPP16Data NG \n");
  1388. #endif
  1389. result = FAIL;
  1390. }
  1391. return result;
  1392. }
  1393. int InitComPort()
  1394. {
  1395. int fd;
  1396. struct termios tios;
  1397. fd = open(relayRs485PortName, O_RDWR);
  1398. if(fd <= 0)
  1399. {
  1400. #ifdef SystemLogMessage
  1401. DEBUG_ERROR("Module_InternalComm. InitComPort NG\n");
  1402. #endif
  1403. if(ShmStatusCodeData!=NULL)
  1404. {
  1405. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1406. }
  1407. sleep(5);
  1408. return -1;
  1409. }
  1410. ioctl (fd, TCGETS, &tios);
  1411. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  1412. tios.c_lflag = 0;
  1413. tios.c_iflag = 0;
  1414. tios.c_oflag = 0;
  1415. tios.c_cc[VMIN]=0;
  1416. tios.c_cc[VTIME]=(byte)0; // timeout 0.5 second
  1417. tios.c_lflag=0;
  1418. tcflush(fd, TCIFLUSH);
  1419. ioctl (fd, TCSETS, &tios);
  1420. return fd;
  1421. }
  1422. //================================================
  1423. // Main process
  1424. //================================================
  1425. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  1426. {
  1427. for (byte index = 0; index < CHAdeMO_QUANTITY; index++) {
  1428. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index
  1429. == target) {
  1430. chargingData[target] =
  1431. &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  1432. return true;
  1433. }
  1434. }
  1435. for (byte index = 0; index < CCS_QUANTITY; index++) {
  1436. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index
  1437. == target) {
  1438. chargingData[target] =
  1439. &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  1440. return true;
  1441. }
  1442. }
  1443. for (byte index = 0; index < GB_QUANTITY; index++) {
  1444. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index
  1445. == target) {
  1446. chargingData[target] =
  1447. &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  1448. return true;
  1449. }
  1450. }
  1451. return false;
  1452. }
  1453. bool FindAcChargingInfoData(byte target, struct ChargingInfoData **acChargingData)
  1454. {
  1455. if (target < AC_QUANTITY)
  1456. {
  1457. acChargingData[target] = &ShmSysConfigAndInfo->SysInfo.AcChargingData[target];
  1458. return true;
  1459. }
  1460. return false;
  1461. }
  1462. void Initialization()
  1463. {
  1464. bool isPass = false;
  1465. for (byte index = 0; index < ARRAY_SIZE(outputRelay.relay_event.relay_status); index++)
  1466. {
  1467. outputRelay.relay_event.relay_status[index] = 0x00;
  1468. }
  1469. while(!isPass)
  1470. {
  1471. isPass = true;
  1472. for (byte _index = 0; _index < gunCount; _index++)
  1473. {
  1474. if (!FindChargingInfoData(_index, &_chargingData[0]))
  1475. {
  1476. DEBUG_ERROR("InternalComm : FindChargingInfoData false \n");
  1477. isPass = false;
  1478. break;
  1479. }
  1480. }
  1481. sleep(1);
  1482. }
  1483. isPass = false;
  1484. if (acgunCount > 0)
  1485. {
  1486. while(!isPass)
  1487. {
  1488. isPass = true;
  1489. for (byte _index = 0; _index < acgunCount; _index++)
  1490. {
  1491. if (!FindAcChargingInfoData(_index, &ac_chargingInfo[0]))
  1492. {
  1493. DEBUG_ERROR("EvComm : FindAcChargingInfoData false \n");
  1494. isPass = false;
  1495. break;
  1496. }
  1497. }
  1498. sleep(1);
  1499. }
  1500. }
  1501. }
  1502. bool IsNoneMatchRelayStatus()
  1503. {
  1504. bool result = false;
  1505. if ((regRelay.relay_event.bits.AC_Contactor != outputRelay.relay_event.bits.AC_Contactor) ||
  1506. (regRelay.relay_event.bits.CCS_Precharge != outputRelay.relay_event.bits.CCS_Precharge) ||
  1507. (regRelay.relay_event.bits.Gun1_P != outputRelay.relay_event.bits.Gun1_P) ||
  1508. (regRelay.relay_event.bits.Gun1_N != outputRelay.relay_event.bits.Gun1_N) ||
  1509. (regRelay.relay_event.bits.Gun2_P != outputRelay.relay_event.bits.Gun2_P) ||
  1510. (regRelay.relay_event.bits.Gun2_N != outputRelay.relay_event.bits.Gun2_N) ||
  1511. (regRelay.relay_event.bits.Gun1_Parallel_P != outputRelay.relay_event.bits.Gun1_Parallel_P) ||
  1512. (regRelay.relay_event.bits.Gun1_Parallel_N != outputRelay.relay_event.bits.Gun1_Parallel_N))
  1513. {
  1514. if (regRelay.relay_event.bits.AC_Contactor != outputRelay.relay_event.bits.AC_Contactor)
  1515. PRINTF_FUNC("AC Contact Relay none match. \n");
  1516. if (regRelay.relay_event.bits.CCS_Precharge != outputRelay.relay_event.bits.CCS_Precharge)
  1517. PRINTF_FUNC("CCS Precharge Relay none match. \n");
  1518. if (regRelay.relay_event.bits.Gun1_P != outputRelay.relay_event.bits.Gun1_P)
  1519. PRINTF_FUNC("SMR1:D+ Relay none match. \n");
  1520. if (regRelay.relay_event.bits.Gun1_N != outputRelay.relay_event.bits.Gun1_N)
  1521. PRINTF_FUNC("SMR1:D- Relay none match. \n");
  1522. if (regRelay.relay_event.bits.Gun2_P != outputRelay.relay_event.bits.Gun2_P)
  1523. PRINTF_FUNC("SMR2:D+ Relay none match. \n");
  1524. if (regRelay.relay_event.bits.Gun2_N != outputRelay.relay_event.bits.Gun2_N)
  1525. PRINTF_FUNC("SMR2:D- Relay none match. \n");
  1526. if (regRelay.relay_event.bits.Gun1_Parallel_P != outputRelay.relay_event.bits.Gun1_Parallel_P)
  1527. PRINTF_FUNC("Parallel:D+ Relay none match. \n");
  1528. if (regRelay.relay_event.bits.Gun1_Parallel_N != outputRelay.relay_event.bits.Gun1_Parallel_N)
  1529. PRINTF_FUNC("Parallel:D- Relay none match. \n");
  1530. result = true;
  1531. }
  1532. return result;
  1533. }
  1534. void MatchRelayStatus()
  1535. {
  1536. // 因為 AC Contactor 沒有 Feedback,所以暫時先這樣處理
  1537. //regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
  1538. ShmSysConfigAndInfo->SysInfo.AcContactorStatus = regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
  1539. regRelay.relay_event.bits.CCS_Precharge = outputRelay.relay_event.bits.CCS_Precharge;
  1540. regRelay.relay_event.bits.Gun1_P = outputRelay.relay_event.bits.Gun1_P;
  1541. regRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_N;
  1542. regRelay.relay_event.bits.Gun2_P = outputRelay.relay_event.bits.Gun2_P;
  1543. regRelay.relay_event.bits.Gun2_N = outputRelay.relay_event.bits.Gun2_N;
  1544. regRelay.relay_event.bits.Gun1_Parallel_P = outputRelay.relay_event.bits.Gun1_Parallel_P;
  1545. regRelay.relay_event.bits.Gun1_Parallel_N = outputRelay.relay_event.bits.Gun1_Parallel_N;
  1546. }
  1547. void CheckRelayStatusByADC()
  1548. {
  1549. if (ShmRelayModuleData->Gun1FuseOutputVolt > 0 && ShmRelayModuleData->Gun1RelayOutputVolt > 0 &&
  1550. (ShmRelayModuleData->Gun1FuseOutputVolt == ShmRelayModuleData->Gun1RelayOutputVolt))
  1551. {
  1552. // Relay 前後電壓一致
  1553. _chargingData[0]->RelayK1K2Status = 0x01;
  1554. }
  1555. else
  1556. _chargingData[0]->RelayK1K2Status = 0x00;
  1557. if (ShmRelayModuleData->Gun2FuseOutputVolt > 0 && ShmRelayModuleData->Gun2RelayOutputVolt > 0 &&
  1558. (ShmRelayModuleData->Gun2FuseOutputVolt == ShmRelayModuleData->Gun2RelayOutputVolt))
  1559. {
  1560. // Relay 前後電壓一致
  1561. _chargingData[1]->RelayK1K2Status = 0x01;
  1562. }
  1563. else
  1564. _chargingData[1]->RelayK1K2Status = 0x00;
  1565. }
  1566. void SetGfdConfig(byte index, byte resister)
  1567. {
  1568. gfd_config.index = index;
  1569. gfd_config.state = resister;
  1570. //PRINTF_FUNC("************************GFD Vol = %d, GFD Res = %d \n", gfd_config.reqVol, gfd_config.resister);
  1571. if (Config_Gfd_Value(Uart5Fd, Addr.Relay, &gfd_config) == PASS)
  1572. {
  1573. // PRINTF_FUNC("Set reqVol = %f, resister = %d \n",
  1574. // gfd_config.reqVol,
  1575. // gfd_config.resister);
  1576. }
  1577. }
  1578. void CableCheckDetected(byte index)
  1579. {
  1580. // Cable Check
  1581. // 當火線上的電壓 = 車端要求的電壓電流
  1582. // _chargingData[targetGun]->EvBatterytargetVoltage
  1583. // 才可以開始偵測 1s
  1584. // Warning : Rgfd <= 150 歐/V 假設電壓為 500V 則~ Rgfd <= 75000 歐
  1585. // Pre-Warning : 150 歐/V < Rgfd <= 500 歐/V 假設電壓為 500V 則 75000 歐 < Rgfd <= 250000
  1586. // SO Normal : Rgfd > 500 歐/V 假設電壓為 500 V 則 Rgfd > 250000 歐
  1587. if ((_chargingData[index]->Type >= _Type_Chademo && _chargingData[index]->Type <= _Type_GB) ||
  1588. (_chargingData[index]->Type == 0x09 && ShmSysConfigAndInfo->SysConfig.AlwaysGfdFlag))
  1589. {
  1590. if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE && _chargingData[index]->SystemStatus <= S_TERMINATING) ||
  1591. (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1592. {
  1593. if (_chargingData[index]->SystemStatus == S_PREPARING_FOR_EVSE &&
  1594. _chargingData[index]->RelayWeldingCheck == YES)
  1595. {
  1596. SetGfdConfig(index, GFD_CABLECHK);
  1597. }
  1598. else if (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1599. _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1)
  1600. {
  1601. SetGfdConfig(index, GFD_PRECHARGE);
  1602. }
  1603. else if (_chargingData[index]->SystemStatus >= S_CHARGING &&
  1604. _chargingData[index]->SystemStatus <= S_TERMINATING)
  1605. {
  1606. if (_chargingData[index]->Type == _Type_GB || _chargingData[index]->Type == _Type_Chademo)
  1607. SetGfdConfig(index, GFD_IDLE);
  1608. else
  1609. SetGfdConfig(index, GFD_CHARGING);
  1610. }
  1611. }
  1612. else if(_chargingData[index]->SystemStatus == S_COMPLETE || _chargingData[index]->SystemStatus == S_PREPARNING
  1613. || _chargingData[index]->SystemStatus == S_IDLE)
  1614. {
  1615. SetGfdConfig(index, GFD_IDLE);
  1616. }
  1617. }
  1618. }
  1619. void CheckOutputPowerOverCarReq(byte index)
  1620. {
  1621. float fireV = _chargingData[index]->FireChargingVoltage;
  1622. float carV = _chargingData[index]->EvBatterytargetVoltage * 10;
  1623. if ((_chargingData[index]->EvBatterytargetVoltage * 10) > 1500 &&
  1624. (_chargingData[index]->Type == _Type_Chademo ||
  1625. _chargingData[index]->Type == _Type_CCS_2 ||
  1626. _chargingData[index]->Type == _Type_GB))
  1627. {
  1628. if (fireV >= (carV + (carV * 0.1)))
  1629. {
  1630. PRINTF_FUNC("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f \n",
  1631. _chargingData[index]->FireChargingVoltage, (_chargingData[index]->EvBatterytargetVoltage * 10));
  1632. DEBUG_ERROR("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f \n",
  1633. _chargingData[index]->FireChargingVoltage, (_chargingData[index]->EvBatterytargetVoltage * 10));
  1634. if (_chargingData[index]->Type == _Type_Chademo)
  1635. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = YES;
  1636. else if (_chargingData[index]->Type == _Type_CCS_2)
  1637. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = YES;
  1638. else if (_chargingData[index]->Type == _Type_GB)
  1639. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = YES;
  1640. _chargingData[index]->StopChargeFlag = YES;
  1641. }
  1642. }
  1643. }
  1644. void CheckOutputVolNoneMatchFire(byte index)
  1645. {
  1646. if ((_chargingData[index]->EvBatterytargetVoltage * 10) > 1500 &&
  1647. (_chargingData[index]->Type == _Type_Chademo ||
  1648. _chargingData[index]->Type == _Type_CCS_2 ||
  1649. _chargingData[index]->Type == _Type_GB))
  1650. {
  1651. if (((_chargingData[index]->PresentChargingVoltage * 10) < _chargingData[index]->FireChargingVoltage - 300) ||
  1652. ((_chargingData[index]->PresentChargingVoltage * 10) > _chargingData[index]->FireChargingVoltage + 300))
  1653. {
  1654. if (!_isOutputNoneMatch[index])
  1655. {
  1656. _isOutputNoneMatch[index] = YES;
  1657. gettimeofday(&_checkOutputNoneMatchTimer[index], NULL);
  1658. }
  1659. else
  1660. {
  1661. if ((GetTimeoutValue(_checkOutputNoneMatchTimer[index]) / 1000) >= 5000)
  1662. {
  1663. PRINTF_FUNC("[Module_InternalComm]CheckOutputVolNoneMatchFire NG (%d) : pre = %f, fire = %f \n",
  1664. index, (_chargingData[index]->PresentChargingVoltage * 10), _chargingData[index]->FireChargingVoltage);
  1665. DEBUG_ERROR("[Module_InternalComm]CheckOutputVolNoneMatchFire NG (%d): pre = %f, fire = %f \n",
  1666. index, (_chargingData[index]->PresentChargingVoltage * 10), _chargingData[index]->FireChargingVoltage);
  1667. if (_chargingData[index]->Type == _Type_Chademo)
  1668. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = YES;
  1669. else if (_chargingData[index]->Type == _Type_GB)
  1670. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = YES;
  1671. else if (_chargingData[index]->Type == _Type_CCS_2)
  1672. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = YES;
  1673. _chargingData[index]->StopChargeFlag = YES;
  1674. }
  1675. }
  1676. }
  1677. else
  1678. _isOutputNoneMatch[index] = NO;
  1679. }
  1680. }
  1681. void CheckRelayWeldingStatus(byte index)
  1682. {
  1683. if (!_isRelayWelding[index])
  1684. {
  1685. if ((_chargingData[index]->PresentChargingVoltage * 10) >= VOUT_MIN_VOLTAGE * 10)
  1686. {
  1687. gettimeofday(&_checkRelayWeldingTimer[index], NULL);
  1688. _isRelayWelding[index] = YES;
  1689. }
  1690. }
  1691. else
  1692. {
  1693. if ((GetTimeoutValue(_checkRelayWeldingTimer[index]) / 1000) >= 1000)
  1694. {
  1695. _chargingData[index]->RelayWeldingCheck = YES;
  1696. return;
  1697. }
  1698. if (_chargingData[index]->FireChargingVoltage >= VOUT_MIN_VOLTAGE)
  1699. {
  1700. if (_chargingData[index]->Type == _Type_Chademo)
  1701. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = YES;
  1702. else if (_chargingData[index]->Type == _Type_GB)
  1703. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = YES;
  1704. else if (_chargingData[index]->Type == _Type_CCS_2)
  1705. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = YES;
  1706. PRINTF_FUNC("CheckRelayWeldingStatus : fail \n");
  1707. _chargingData[index]->StopChargeFlag = YES;
  1708. }
  1709. }
  1710. }
  1711. void GetPsuTempForFanSpeed()
  1712. {
  1713. char temp = 0;
  1714. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1715. {
  1716. for (byte count = 0; count < ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity; count++)
  1717. {
  1718. if (temp < ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp)
  1719. temp = ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp;
  1720. }
  1721. }
  1722. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = temp;
  1723. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  1724. {
  1725. if (ShmFanModuleData->TestFanSpeed == NORMAL_FAN_SPEED)
  1726. {
  1727. if (temp >= ENV_TEMP_MAX)
  1728. ShmFanModuleData->TestFanSpeed = MAX_FAN_SPEED;
  1729. }
  1730. else if (ShmFanModuleData->TestFanSpeed == MAX_FAN_SPEED)
  1731. {
  1732. if (temp <= ENV_TEMP_MIN)
  1733. ShmFanModuleData->TestFanSpeed = NORMAL_FAN_SPEED;
  1734. }
  1735. else
  1736. ShmFanModuleData->TestFanSpeed = NORMAL_FAN_SPEED;
  1737. }
  1738. }
  1739. void GetFanSpeedByFunction()
  1740. {
  1741. // if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  1742. // return;
  1743. //
  1744. // // 風控修改 :
  1745. // // ******************************************************* //
  1746. // //
  1747. // // 當前PSU輸出總 KW PSU Temp
  1748. // // 30 x -------------------- x ---------- + 14 x (PSU Temp - 45)
  1749. // // 當前樁最大功率 KW 45
  1750. // //
  1751. // // ******************************************************* //
  1752. //
  1753. // // 當前樁最大功率 KW : ShmPsuData->SystemAvailablePower
  1754. // unsigned int _maxPower = ShmPsuData->SystemAvailablePower;
  1755. // // 當前PSU輸出總 KW & PSU Temp :
  1756. // unsigned char temp = 0;
  1757. // float power = 0;
  1758. //
  1759. // for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1760. // {
  1761. // for (byte count = 0; count < ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity; count++)
  1762. // {
  1763. // if (temp < ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp)
  1764. // temp = ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp;
  1765. // }
  1766. // power += (_chargingData[index]->PresentChargingPower * 10);
  1767. // }
  1768. //
  1769. // double _pw_rate = 0;
  1770. // if (_maxPower > 0)
  1771. // _pw_rate = power / (double)_maxPower;
  1772. // double _temp_rate = 0;
  1773. // if (temp > 0)
  1774. // _temp_rate = (double)temp / 45;
  1775. // unsigned char _temp_diff = 0;
  1776. // if (temp > 45)
  1777. // _temp_diff = temp - 45;
  1778. //
  1779. // ShmFanModuleData->TestFanSpeed = ((30 * _pw_rate * _temp_rate + 14 * _temp_diff) / 100) * MAX_FAN_SPEED;
  1780. //
  1781. // if (ShmFanModuleData->TestFanSpeed > MAX_FAN_SPEED)
  1782. // ShmFanModuleData->TestFanSpeed = MAX_FAN_SPEED;
  1783. //
  1784. // if (ShmFanModuleData->TestFanSpeed < 0)
  1785. // ShmFanModuleData->TestFanSpeed = 0;
  1786. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  1787. return;
  1788. // 風控修改 :
  1789. // ******************************************************* //
  1790. //
  1791. // 當前PSU輸出總 KW PSU Temp
  1792. // 50 x -------------------- x ---------- + 0.5 x (PSU Temp - 70)
  1793. // 當前樁最大功率 KW 50
  1794. //
  1795. // ******************************************************* //
  1796. // 當前樁最大功率 KW : ShmPsuData->SystemAvailablePower
  1797. unsigned int _maxPower = ShmPsuData->SystemAvailablePower;
  1798. // 當前PSU輸出總 KW & PSU Temp :
  1799. unsigned char temp = 0;
  1800. float power = 0;
  1801. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1802. {
  1803. for (byte count = 0; count < ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity; count++)
  1804. {
  1805. if (temp < ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp)
  1806. temp = ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp;
  1807. }
  1808. power += (_chargingData[index]->PresentChargingPower * 10);
  1809. }
  1810. double _pw_rate = 0;
  1811. if (_maxPower > 0)
  1812. _pw_rate = power / (double)_maxPower;
  1813. double _temp_rate = 0;
  1814. if (temp > 0)
  1815. _temp_rate = (double)temp / 50;
  1816. unsigned char _temp_diff = 0;
  1817. if (temp > 70)
  1818. _temp_diff = temp - 70;
  1819. ShmFanModuleData->TestFanSpeed = (((50 * _pw_rate * _temp_rate) + (0.5 * _temp_diff)) / 100) * MAX_FAN_SPEED;
  1820. if (ShmFanModuleData->TestFanSpeed > MAX_FAN_SPEED)
  1821. ShmFanModuleData->TestFanSpeed = MAX_FAN_SPEED;
  1822. if (ShmFanModuleData->TestFanSpeed < 0)
  1823. ShmFanModuleData->TestFanSpeed = 0;
  1824. // -----------------------------------------------------------------------
  1825. // printf("power = %f \n", power);
  1826. // printf("_maxPower = %d \n", _maxPower);
  1827. // printf("temp = %d \n", temp);
  1828. //
  1829. // printf("_pw_rate = %f \n", _pw_rate);
  1830. // printf("_temp_rate = %f \n", _temp_rate);
  1831. // printf("_temp_diff = %d \n", _temp_diff);
  1832. // printf("fan rate = %f \n", (30 * _pw_rate * _temp_rate + 14 * _temp_diff));
  1833. // printf("ShmFanModuleData->TestFanSpeed = %d \n", ShmFanModuleData->TestFanSpeed);
  1834. }
  1835. void GetAcStatus()
  1836. {
  1837. if (Query_AC_Status(Uart5Fd, Addr.AcPlug, &acStatus) == PASS)
  1838. {
  1839. ShmSysConfigAndInfo->SysConfig.AcRatingCurrent = acStatus.MaxCurrent;
  1840. if(ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent == 0)
  1841. ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent = ShmSysConfigAndInfo->SysConfig.AcRatingCurrent;
  1842. ac_chargingInfo[0]->ConnectorPlugIn = acStatus.CpStatus;
  1843. // PRINTF_FUNC("CpStatus = %d \n", acStatus.CpStatus);
  1844. // printf("CurLimit = %d \n", acStatus.CurLimit);
  1845. // printf("PilotVol_P = %d \n", acStatus.PilotVol_P);
  1846. // printf("PilotVol_N = %d \n", acStatus.PilotVol_N);
  1847. // printf("LockStatus = %d \n", acStatus.LockStatus);
  1848. // printf("RelayStatus = %d \n", acStatus.RelayStatus);
  1849. // printf("ShutterStatus = %d \n", acStatus.ShutterStatus);
  1850. // printf("MeterStatus = %d \n", acStatus.MeterStatus);
  1851. // printf("PpStatus = %d \n", acStatus.PpStatus);
  1852. // printf("MaxCurrent = %d \n", acStatus.MaxCurrent);
  1853. // printf("RotateSwitchStatus = %d \n", acStatus.RelayStatus);
  1854. // printf("============================== \n");
  1855. //
  1856. // ac_chargingInfo[0]->SystemStatus = acStatus.CpStatus;
  1857. }
  1858. // else
  1859. // PRINTF_FUNC("GetAcStatus return fail. \n");
  1860. }
  1861. void GetAcAlarmCode()
  1862. {
  1863. if (Query_AC_Alarm_Code(Uart5Fd, Addr.AcPlug, &acAlarmCode) == PASS)
  1864. {
  1865. CheckAlarmOccur();
  1866. }
  1867. }
  1868. unsigned char GetChargingEnergy()
  1869. {
  1870. return Query_Charging_Energy(Uart5Fd, Addr.AcPlug, &acChargingEnergy);
  1871. }
  1872. unsigned char GetChargingCurrent()
  1873. {
  1874. return Query_Charging_Current(Uart5Fd, Addr.AcPlug, &acChargingCurrent);
  1875. }
  1876. void ChangeLedStatus()
  1877. {
  1878. if (ac_chargingInfo[0]->SystemStatus == S_IDLE)
  1879. ledStatus.ActionMode = 1;
  1880. else if (ac_chargingInfo[0]->SystemStatus == S_PREPARNING)
  1881. ledStatus.ActionMode = 3;
  1882. else if (ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  1883. ledStatus.ActionMode = 4;
  1884. Config_LED_Status(Uart5Fd, Addr.AcPlug, &ledStatus);
  1885. }
  1886. void SetLegacyReq(byte _switch)
  1887. {
  1888. Config_Legacy_Req(Uart5Fd, Addr.AcPlug, _switch);
  1889. }
  1890. void SetCpDuty(byte _value)
  1891. {
  1892. Config_Ac_Duty(Uart5Fd, Addr.AcPlug, _value);
  1893. }
  1894. void ChangeToCsuMode()
  1895. {
  1896. ac_chargingInfo[0]->IsModeChagned = Config_CSU_Mode(Uart5Fd, Addr.AcPlug);
  1897. // if (ac_chargingInfo[0]->IsModeChagned == PASS)
  1898. // {
  1899. // Config_Reset_MCU(Uart5Fd, Addr.AcPlug);
  1900. // }
  1901. }
  1902. void ChangeStartOrStopDateTime(byte isStart)
  1903. {
  1904. char cmdBuf[32];
  1905. struct timeb csuTime;
  1906. struct tm *tmCSU;
  1907. ftime(&csuTime);
  1908. tmCSU = localtime(&csuTime.time);
  1909. sprintf(cmdBuf, "%04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  1910. tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  1911. tmCSU->tm_sec);
  1912. if (isStart)
  1913. strcpy((char *)ac_chargingInfo[0]->StartDateTime, cmdBuf);
  1914. else
  1915. strcpy((char *)ac_chargingInfo[0]->StopDateTime, cmdBuf);
  1916. }
  1917. void OcppStartTransation(byte gunIndex)
  1918. {
  1919. if(strcmp((char *)ac_chargingInfo[0]->StartUserId, "") == EQUAL)
  1920. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag, (char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag);
  1921. else
  1922. strcpy((char *)ShmOCPP16Data->StartTransaction[gunIndex].IdTag, (char *)ac_chargingInfo[0]->StartUserId);
  1923. PRINTF_FUNC("AC IdTag = %s \n", ShmOCPP16Data->StartTransaction[gunIndex].IdTag);
  1924. ShmOCPP16Data->CpMsg.bits[gunIndex].StartTransactionReq = YES;
  1925. }
  1926. void OcppStopTransation(byte gunIndex)
  1927. {
  1928. if(strcmp((char *)ac_chargingInfo[0]->StartUserId, "") == EQUAL)
  1929. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag, (char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag);
  1930. else
  1931. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].IdTag, (char *)ac_chargingInfo[0]->StartUserId);
  1932. PRINTF_FUNC("AC IdTag = %s \n", ShmOCPP16Data->StopTransaction[gunIndex].IdTag);
  1933. ShmOCPP16Data->CpMsg.bits[gunIndex].StopTransactionReq = YES;
  1934. }
  1935. bool OcppRemoteStop(byte gunIndex)
  1936. {
  1937. bool result = ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq;
  1938. if (ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq == YES)
  1939. {
  1940. strcpy((char *)ShmOCPP16Data->StopTransaction[gunIndex].StopReason, "Remote");
  1941. ShmOCPP16Data->CsMsg.bits[gunIndex].RemoteStopTransactionReq = NO;
  1942. }
  1943. return result;
  1944. }
  1945. unsigned char isModeChange()
  1946. {
  1947. unsigned char result = NO;
  1948. if(ac_chargingInfo[0]->SystemStatus != ac_chargingInfo[0]->PreviousSystemStatus)
  1949. {
  1950. result = YES;
  1951. ac_chargingInfo[0]->PreviousSystemStatus = ac_chargingInfo[0]->SystemStatus;
  1952. }
  1953. return result;
  1954. }
  1955. void AcChargeTypeProcess()
  1956. {
  1957. if (acgunCount > 0)
  1958. {
  1959. //ac_chargingInfo[0]->SelfTest_Comp = YES;
  1960. //ac_chargingInfo[0]->IsModeChagned = PASS;
  1961. //---------------------------------------------
  1962. if (ac_chargingInfo[0]->SelfTest_Comp == NO)
  1963. {
  1964. ac_chargingInfo[0]->IsModeChagned = NO;
  1965. GetFwVersion_AC();
  1966. GetAcModelName();
  1967. }
  1968. else if (ac_chargingInfo[0]->SelfTest_Comp == YES)
  1969. {
  1970. if (ac_chargingInfo[0]->IsModeChagned != PASS)
  1971. {
  1972. ChangeToCsuMode();
  1973. return;
  1974. }
  1975. GetAcStatus();
  1976. GetAcAlarmCode();
  1977. byte _status = S_NONE;
  1978. if (ac_chargingInfo[0]->SystemStatus == S_IDLE && ac_chargingInfo[0]->IsErrorOccur)
  1979. {
  1980. _status = S_ALARM;
  1981. }
  1982. else if (acStatus.CpStatus == AC_SYS_A || ac_chargingInfo[0]->IsErrorOccur)
  1983. {
  1984. if (ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  1985. _status = S_TERMINATING;
  1986. else if (ac_chargingInfo[0]->SystemStatus >= S_TERMINATING)
  1987. {
  1988. if (GetTimeoutValue(_ac_charging_comp) >= 10000000 && acStatus.CpStatus == AC_SYS_A)
  1989. _status = S_IDLE;
  1990. }
  1991. else
  1992. _status = S_IDLE;
  1993. }
  1994. else if (ac_chargingInfo[0]->SystemStatus >= S_PREPARNING &&
  1995. ac_chargingInfo[0]->SystemStatus < S_CHARGING)
  1996. {
  1997. if (acStatus.CpStatus == AC_SYS_C && acStatus.RelayStatus == YES)
  1998. _status = S_CHARGING;
  1999. else if (GetTimeoutValue(_ac_preparing) >= 30000000)
  2000. _status = S_IDLE;
  2001. }
  2002. else if ((acStatus.CpStatus == AC_SYS_B || ac_chargingInfo[0]->ConnectorPlugIn == AC_SYS_B) &&
  2003. ac_chargingInfo[0]->IsAvailable &&
  2004. !ac_chargingInfo[0]->IsErrorOccur &&
  2005. (ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES ||
  2006. ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE))
  2007. {
  2008. if (ac_chargingInfo[0]->RemoteStartFlag == YES)
  2009. {
  2010. PRINTF_FUNC("** AC Remote \n");
  2011. ac_chargingInfo[0]->RemoteStartFlag = NO;
  2012. strcpy((char *)ac_chargingInfo[0]->StartUserId, "");
  2013. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  2014. _status = S_PREPARNING;
  2015. }
  2016. else if (ShmSysConfigAndInfo->SysInfo.OrderCharging == NO_DEFINE)
  2017. {
  2018. PRINTF_FUNC("** UserId = %s \n", ShmSysConfigAndInfo->SysConfig.UserId);
  2019. strcpy((char *)ac_chargingInfo[0]->StartUserId, (char *)ShmSysConfigAndInfo->SysConfig.UserId);
  2020. PRINTF_FUNC("** CardNumber = %s \n", ac_chargingInfo[0]->StartUserId);
  2021. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  2022. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  2023. _status = S_PREPARNING;
  2024. }
  2025. }
  2026. else if (ac_chargingInfo[0]->SystemStatus == S_CHARGING)
  2027. {
  2028. if (OcppRemoteStop(1))
  2029. _status = S_TERMINATING;
  2030. }
  2031. //printf("_status = %d \n", _status);
  2032. if (_status != S_NONE && ac_chargingInfo[0]->SystemStatus != _status)
  2033. {
  2034. ac_chargingInfo[0]->SystemStatus = _status;
  2035. }
  2036. // 設定限制最大充電電流 >= 6 ~ <= 32
  2037. switch(ac_chargingInfo[0]->SystemStatus)
  2038. {
  2039. case S_IDLE:
  2040. case S_ALARM:
  2041. {
  2042. if (isModeChange())
  2043. {
  2044. ac_chargingInfo[0]->PresentChargedEnergy = 0.0;
  2045. ac_chargingInfo[0]->PresentChargingVoltage = 0;
  2046. ac_chargingInfo[0]->ChargingFee = 0.0;
  2047. strcpy((char *)ac_chargingInfo[0]->StartDateTime, "");
  2048. strcpy((char *)ac_chargingInfo[0]->StopDateTime, "");
  2049. _beforeChargingTotalEnergy = 0.0;
  2050. }
  2051. ChangeLedStatus();
  2052. }
  2053. break;
  2054. case S_PREPARNING:
  2055. {
  2056. if (isModeChange())
  2057. {
  2058. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_NONE;
  2059. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2060. if (ShmSysConfigAndInfo->SysInfo.OrderCharging != NO_DEFINE)
  2061. ShmSysConfigAndInfo->SysInfo.OrderCharging = NO_DEFINE;
  2062. gettimeofday(&_ac_preparing, NULL);
  2063. }
  2064. if (GetChargingEnergy() == PASS)
  2065. {
  2066. //ac_chargingInfo[0]->PresentChargedEnergy = acChargingEnergy.Energy / 100;
  2067. _beforeChargingTotalEnergy = acChargingEnergy.Energy;
  2068. }
  2069. SetLegacyReq(YES);
  2070. ChangeLedStatus();
  2071. }
  2072. break;
  2073. case S_CHARGING:
  2074. {
  2075. if (isModeChange())
  2076. {
  2077. ftime(&_ac_startChargingTime);
  2078. OcppStartTransation(1);
  2079. ChangeStartOrStopDateTime(YES);
  2080. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = DEFAULT_AC_INDEX;
  2081. }
  2082. if (GetChargingEnergy() == PASS)
  2083. {
  2084. if ((acChargingEnergy.Energy - _beforeChargingTotalEnergy) > 0)
  2085. {
  2086. ac_chargingInfo[0]->PresentChargedEnergy += (acChargingEnergy.Energy - _beforeChargingTotalEnergy) / 100;
  2087. if (ShmSysConfigAndInfo->SysConfig.BillingData.isBilling)
  2088. {
  2089. ac_chargingInfo[0]->ChargingFee += ac_chargingInfo[0]->PresentChargedEnergy * ShmSysConfigAndInfo->SysConfig.BillingData.Cur_fee;
  2090. }
  2091. }
  2092. _beforeChargingTotalEnergy = acChargingEnergy.Energy;
  2093. }
  2094. if (GetChargingCurrent() == PASS)
  2095. ac_chargingInfo[0]->PresentChargingPower = (((float)(AC_DEFAULT_VOL * acChargingCurrent.OuputCurrentL1) / 10) / 1000);
  2096. ftime(&_ac_endChargingTime);
  2097. ac_chargingInfo[0]->PresentChargedDuration = DiffTimeb(_ac_startChargingTime, _ac_endChargingTime);
  2098. ac_chargingInfo[0]->PresentChargingVoltage = AC_DEFAULT_VOL;
  2099. ac_chargingInfo[0]->PresentChargingCurrent = ((float)acChargingCurrent.OuputCurrentL1 / 10);
  2100. // 用以判斷是否有在輸出
  2101. ac_chargingInfo[0]->IsCharging = acStatus.RelayStatus;
  2102. SetCpDuty(ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent);
  2103. ChangeLedStatus();
  2104. }
  2105. break;
  2106. case S_TERMINATING:
  2107. {
  2108. if (isModeChange())
  2109. {
  2110. ChangeStartOrStopDateTime(NO);
  2111. gettimeofday(&_ac_charging_comp, NULL);
  2112. }
  2113. SetLegacyReq(NO);
  2114. if (acStatus.RelayStatus == NO)
  2115. ac_chargingInfo[0]->SystemStatus = S_COMPLETE;
  2116. }
  2117. break;
  2118. case S_COMPLETE:
  2119. {
  2120. if (isModeChange())
  2121. {
  2122. gettimeofday(&_ac_charging_comp, NULL);
  2123. ftime(&_ac_endChargingTime);
  2124. if (strcmp((char *)ac_chargingInfo[0]->StartDateTime, "") != EQUAL)
  2125. {
  2126. // AC 固定為第2把槍
  2127. OcppStopTransation(1);
  2128. }
  2129. ChangeStartOrStopDateTime(NO);
  2130. ac_chargingInfo[0]->PresentChargedDuration = DiffTimeb(_ac_startChargingTime, _ac_endChargingTime);
  2131. }
  2132. }
  2133. break;
  2134. }
  2135. }
  2136. }
  2137. }
  2138. void ResetDetAlarmStatus(byte gun)
  2139. {
  2140. if (_chargingData[gun]->Type == _Type_Chademo)
  2141. {
  2142. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding == YES)
  2143. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = NO;
  2144. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault == YES)
  2145. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayDrivingFault = NO;
  2146. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP == YES)
  2147. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = NO;
  2148. }
  2149. else if (_chargingData[gun]->Type == _Type_GB)
  2150. {
  2151. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding == YES)
  2152. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = NO;
  2153. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault == YES)
  2154. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayDrivingFault = NO;
  2155. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP == YES)
  2156. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemGbOutputOVP = NO;
  2157. }
  2158. else if (_chargingData[gun]->Type == _Type_CCS_2)
  2159. {
  2160. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding == YES)
  2161. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = NO;
  2162. if (ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault == YES)
  2163. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayDrivingFault = NO;
  2164. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP == YES)
  2165. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemCcsOutputOVP = NO;
  2166. }
  2167. }
  2168. int main(void)
  2169. {
  2170. if(InitShareMemory() == FAIL)
  2171. {
  2172. #ifdef SystemLogMessage
  2173. DEBUG_ERROR("InitShareMemory NG\n");
  2174. #endif
  2175. if(ShmStatusCodeData!=NULL)
  2176. {
  2177. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  2178. }
  2179. sleep(5);
  2180. return 0;
  2181. }
  2182. gunCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  2183. acgunCount = ShmSysConfigAndInfo->SysConfig.AcConnectorCount;
  2184. // Open Uart5 for RB
  2185. Uart5Fd = InitComPort();
  2186. Initialization();
  2187. sleep(1);
  2188. if(Uart5Fd < 0)
  2189. {
  2190. PRINTF_FUNC("(Internal) open port error. \n");
  2191. return 0;
  2192. }
  2193. outputRelay.relay_event.bits.AC_Contactor = 0x00;
  2194. outputRelay.relay_event.bits.CCS_Precharge = 0x00;
  2195. outputRelay.relay_event.bits.Gun1_Parallel_P = 0x00;
  2196. outputRelay.relay_event.bits.Gun1_Parallel_N = 0x00;
  2197. outputRelay.relay_event.bits.Gun1_P = 0x00;
  2198. outputRelay.relay_event.bits.Gun1_N = 0x00;
  2199. outputRelay.relay_event.bits.Gun2_N = 0x00;
  2200. outputRelay.relay_event.bits.Gun2_P = 0x00;
  2201. if(Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay) != PASS)
  2202. PRINTF_FUNC("Config_Relay_Output fail \n");
  2203. cur_led_color.Connect_1_Red = COLOR_MIN_LV;
  2204. cur_led_color.Connect_1_Green = COLOR_MIN_LV;
  2205. cur_led_color.Connect_1_Blue = COLOR_MIN_LV;
  2206. cur_led_color.Connect_2_Red = COLOR_MIN_LV;
  2207. cur_led_color.Connect_2_Green = COLOR_MIN_LV;
  2208. cur_led_color.Connect_2_Blue = COLOR_MIN_LV;
  2209. //bool printRelayStatus = true;
  2210. for(;;)
  2211. {
  2212. bool isCharging = false;
  2213. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  2214. if (ShmRelayModuleData->SelfTest_Comp == NO)
  2215. {
  2216. GetFwAndHwVersion_Relay();
  2217. SetModelName_Relay();
  2218. SetRtcData_Relay();
  2219. sleep(1);
  2220. }
  2221. if (ShmFanModuleData->SelfTest_Comp == NO)
  2222. {
  2223. GetFwAndHwVersion_Fan();
  2224. SetModelName_Fan();
  2225. SetRtcData_Fan();
  2226. sleep(1);
  2227. gettimeofday(&_priority_time, NULL);
  2228. }
  2229. // 自檢階段處理,自檢階段如果讀不到版號則代表該系統沒有掛燈板
  2230. if (ShmLedModuleData->SelfTest_Comp == NO)
  2231. {
  2232. // 自檢階段
  2233. if (ShmSysConfigAndInfo->SysInfo.SelfTestSeq <= _STEST_PSU_CAP)
  2234. {
  2235. GetFwAndHwVersion_Led();
  2236. sleep(1);
  2237. gettimeofday(&_led_priority_time, NULL);
  2238. }
  2239. else
  2240. {
  2241. // 自檢階段沒有問到版號
  2242. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail == NO)
  2243. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.LedboardStestFail = YES;
  2244. }
  2245. }
  2246. AcChargeTypeProcess();
  2247. if (ShmRelayModuleData->SelfTest_Comp == YES)
  2248. {
  2249. // ==============優先權最高 10 ms ==============
  2250. // 輸出電壓
  2251. GetPersentOutputVol();
  2252. // 三相輸入電壓
  2253. GetPresentInputVol();
  2254. // 讀取當前 AC relay 狀態
  2255. regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  2256. //GetRelayOutputStatus();
  2257. for (int i = 0; i < gunCount; i++)
  2258. {
  2259. // Cable check (Set)
  2260. CableCheckDetected(i);
  2261. // check k1 k2 relay 狀態
  2262. CheckK1K2RelayOutput(i);
  2263. // 依據當前各槍的狀態選擇 搭上/放開 Relay
  2264. SetK1K2RelayStatus(i);
  2265. if (_chargingData[i]->SystemStatus == S_IDLE)
  2266. {
  2267. _chargingData[i]->RelayWeldingCheck = NO;
  2268. _isRelayWelding[i] = NO;
  2269. ResetDetAlarmStatus(i);
  2270. }
  2271. if (_chargingData[i]->SystemStatus == S_BOOTING ||
  2272. (_chargingData[i]->SystemStatus >= S_REASSIGN_CHECK && _chargingData[i]->SystemStatus <= S_COMPLETE) ||
  2273. (_chargingData[i]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[i]->SystemStatus <= S_CCS_PRECHARGE_ST1) ||
  2274. ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES ||
  2275. (ShmSysConfigAndInfo->SysInfo.PageIndex >= _LCM_AUTHORIZING && ShmSysConfigAndInfo->SysInfo.PageIndex <= _LCM_WAIT_FOR_PLUG))
  2276. {
  2277. _chargingData[i]->IsReadyToCharging = YES;
  2278. isCharging = true;
  2279. // 限定只有在槍類別為 GBT 的時候才做 relay welding 的判斷
  2280. if (_chargingData[i]->Type == _Type_GB)
  2281. {
  2282. if (_chargingData[i]->SystemStatus >= S_PREPARING_FOR_EVSE &&
  2283. _chargingData[i]->RelayWeldingCheck == NO)
  2284. CheckRelayWeldingStatus(i);
  2285. }
  2286. else
  2287. _chargingData[i]->RelayWeldingCheck = YES;
  2288. if (_chargingData[i]->SystemStatus == S_CHARGING)
  2289. {
  2290. CheckOutputPowerOverCarReq(i);
  2291. CheckOutputVolNoneMatchFire(i);
  2292. }
  2293. else
  2294. _isOutputNoneMatch[i] = NO;
  2295. }
  2296. else
  2297. _chargingData[i]->IsReadyToCharging = NO;
  2298. }
  2299. // Cable check (Get)
  2300. GetGfdAdc();
  2301. // 橋接 relay
  2302. SetParalleRelayStatus();
  2303. // 搭上 AC Contactor
  2304. // if (isCharging)
  2305. // outputRelay.relay_event.bits.AC_Contactor = YES;
  2306. // else
  2307. // outputRelay.relay_event.bits.AC_Contactor = NO;
  2308. if (isCharging ||
  2309. (ShmPsuData->Work_Step >= _TEST_MODE && ShmPsuData->Work_Step <= _TEST_MODE))
  2310. {
  2311. isStopChargingCount = false;
  2312. outputRelay.relay_event.bits.AC_Contactor = YES;
  2313. }
  2314. else
  2315. {
  2316. if (!isStopChargingCount)
  2317. {
  2318. gettimeofday(&_close_ac_contactor, NULL);
  2319. isStopChargingCount = true;
  2320. }
  2321. else
  2322. {
  2323. if ((outputRelay.relay_event.bits.AC_Contactor == YES && GetTimeoutValue(_close_ac_contactor) / 1000 >= (TEN_MINUTES * 1000)))
  2324. outputRelay.relay_event.bits.AC_Contactor = NO;
  2325. }
  2326. }
  2327. if (ShmPsuData->Work_Step >= _TEST_MODE && ShmPsuData->Work_Step <= _TEST_MODE)
  2328. outputRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_P = YES;
  2329. // 搭上/鬆開 Relay
  2330. if(IsNoneMatchRelayStatus())
  2331. {
  2332. if (Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay))
  2333. {
  2334. //regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  2335. regRelay.relay_event.bits.CCS_Precharge = outputRelay.relay_event.bits.CCS_Precharge;
  2336. regRelay.relay_event.bits.Gun1_P = outputRelay.relay_event.bits.Gun1_P;
  2337. regRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_N;
  2338. regRelay.relay_event.bits.Gun2_P = outputRelay.relay_event.bits.Gun2_P;
  2339. regRelay.relay_event.bits.Gun2_N = outputRelay.relay_event.bits.Gun2_N;
  2340. regRelay.relay_event.bits.Gun1_Parallel_P = outputRelay.relay_event.bits.Gun1_Parallel_P;
  2341. regRelay.relay_event.bits.Gun1_Parallel_N = outputRelay.relay_event.bits.Gun1_Parallel_N;
  2342. PRINTF_FUNC("Match Relay, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
  2343. regRelay.relay_event.bits.AC_Contactor,
  2344. regRelay.relay_event.bits.Gun1_P,
  2345. regRelay.relay_event.bits.Gun1_N,
  2346. regRelay.relay_event.bits.Gun2_P,
  2347. regRelay.relay_event.bits.Gun2_N,
  2348. regRelay.relay_event.bits.CCS_Precharge,
  2349. regRelay.relay_event.bits.Gun1_Parallel_P,
  2350. regRelay.relay_event.bits.Gun1_Parallel_N);
  2351. }
  2352. }
  2353. // if(IsNoneMatchRelayStatus())
  2354. // {
  2355. // if (printRelayStatus)
  2356. // {
  2357. //// PRINTF_FUNC("Match Relay Target, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
  2358. //// outputRelay.relay_event.bits.AC_Contactor,
  2359. //// outputRelay.relay_event.bits.Gun1_P,
  2360. //// outputRelay.relay_event.bits.Gun1_N,
  2361. //// outputRelay.relay_event.bits.Gun2_P,
  2362. //// outputRelay.relay_event.bits.Gun2_N,
  2363. //// outputRelay.relay_event.bits.CCS_Precharge,
  2364. //// outputRelay.relay_event.bits.Gun1_Parallel_P,
  2365. //// outputRelay.relay_event.bits.Gun1_Parallel_N);
  2366. // }
  2367. // printRelayStatus = false;
  2368. // if (Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay))
  2369. // {
  2370. // PRINTF_FUNC("Match Relay Target, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
  2371. // outputRelay.relay_event.bits.AC_Contactor,
  2372. // outputRelay.relay_event.bits.Gun1_P,
  2373. // outputRelay.relay_event.bits.Gun1_N,
  2374. // outputRelay.relay_event.bits.Gun2_P,
  2375. // outputRelay.relay_event.bits.Gun2_N,
  2376. // outputRelay.relay_event.bits.CCS_Precharge,
  2377. // outputRelay.relay_event.bits.Gun1_Parallel_P,
  2378. // outputRelay.relay_event.bits.Gun1_Parallel_N);
  2379. // }
  2380. // }
  2381. // else
  2382. // {
  2383. // if (!printRelayStatus)
  2384. // {
  2385. // PRINTF_FUNC("Match Relay, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
  2386. // regRelay.relay_event.bits.AC_Contactor,
  2387. // regRelay.relay_event.bits.Gun1_P,
  2388. // regRelay.relay_event.bits.Gun1_N,
  2389. // regRelay.relay_event.bits.Gun2_P,
  2390. // regRelay.relay_event.bits.Gun2_N,
  2391. // regRelay.relay_event.bits.CCS_Precharge,
  2392. // regRelay.relay_event.bits.Gun1_Parallel_P,
  2393. // regRelay.relay_event.bits.Gun1_Parallel_N);
  2394. // }
  2395. // printRelayStatus = true;
  2396. // }
  2397. }
  2398. if (ShmFanModuleData->SelfTest_Comp == YES ||
  2399. strlen((char *)ShmSysConfigAndInfo->SysInfo.FanModuleFwRev) != 0 ||
  2400. ShmSysConfigAndInfo->SysInfo.FanModuleFwRev[0] != '\0')
  2401. {
  2402. ShmFanModuleData->SelfTest_Comp = YES;
  2403. if (GetTimeoutValue(_priority_time) / 1000 >= 1000)
  2404. {
  2405. //GetPsuTempForFanSpeed();
  2406. GetFanSpeedByFunction();
  2407. GetFanSpeed();
  2408. ShmSysConfigAndInfo->SysInfo.SystemFanRotaSpeed = _setFanSpeed;
  2409. gettimeofday(&_priority_time, NULL);
  2410. ShmFanModuleData->SetFan1Speed = ShmFanModuleData->TestFanSpeed;
  2411. ShmFanModuleData->SetFan2Speed = ShmFanModuleData->TestFanSpeed;
  2412. ShmFanModuleData->SetFan3Speed = ShmFanModuleData->TestFanSpeed;
  2413. ShmFanModuleData->SetFan4Speed = ShmFanModuleData->TestFanSpeed;
  2414. //PRINTF_FUNC("set fan = %d \n", ShmFanModuleData->SetFan1Speed);
  2415. SetFanModuleSpeed();
  2416. }
  2417. }
  2418. if (ShmLedModuleData->SelfTest_Comp == YES)
  2419. {
  2420. if (GetTimeoutValue(_led_priority_time) / 1000 >= 1000)
  2421. {
  2422. if(gunCount == 1)
  2423. {
  2424. SetLedColor(_chargingData[0], _chargingData[0]);
  2425. }
  2426. else if (gunCount == 2)
  2427. {
  2428. SetLedColor(_chargingData[0], _chargingData[1]);
  2429. }
  2430. gettimeofday(&_led_priority_time, NULL);
  2431. }
  2432. }
  2433. usleep(10000);
  2434. }
  2435. return FAIL;
  2436. }