Module_InternalComm.c 66 KB

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