Module_InternalComm.c 98 KB

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