Module_InternalComm.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  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. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  41. struct StatusCodeData *ShmStatusCodeData;
  42. struct FanModuleData *ShmFanModuleData;
  43. struct RelayModuleData *ShmRelayModuleData;
  44. struct CHAdeMOData *ShmCHAdeMOData;
  45. struct CcsData *ShmCcsData;
  46. struct PsuData *ShmPsuData;
  47. #define VIN_MAX_VOLTAGE 250 // 大於該值 : OVP
  48. #define VIN_MIN_VOLTAGE 150 // 小於該值 : UVP
  49. #define VIN_DROP_VOLTAGE 150 // 小於該值 : ac drop
  50. #define VOUT_MAX_VOLTAGE 750
  51. #define VOUT_MIN_VOLTAGE 150
  52. #define IOUT_MAX_CURRENT 50
  53. #define MAX_FAN_SPEED 13500
  54. #define MIN_FAN_SPEED 2800
  55. #define NORMAL_FAN_SPEED 7000
  56. // GFD Status
  57. #define GFD_IDLE 0
  58. #define GFD_CABLECHK 1
  59. #define GFD_PRECHARGE 2
  60. #define GFD_CHARGING 3
  61. // 最小切換 Relay 電壓
  62. #define SELF_TO_CHANGE_RELAY_STATUS 600
  63. // 透過電壓確認 Relay 是否搭上的依據電壓
  64. #define CHECK_RELAY_STATUS 300
  65. #define CHECK_RELAY_STATUS_GAP 100
  66. // 安全在停止充電程序中斷開 Relay 的電流
  67. #define SEFETY_SWITCH_RELAY_CUR 20
  68. // 確認 Relay Welding 電壓
  69. #define RELAY_WELDING_DET 300
  70. byte gunCount;
  71. // 槍資訊
  72. struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  73. bool _isOutputNoneMatch[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  74. struct timeval _checkOutputNoneMatchTimer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  75. bool _isRelayWelding[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  76. struct timeval _checkRelayWeldingTimer[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  77. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData);
  78. int Uart5Fd;
  79. char *relayRs485PortName = "/dev/ttyS5";
  80. unsigned short fanSpeedSmoothValue = 1000;
  81. bool isStopChargingCount = false;
  82. bool isSystemBooting = false;
  83. struct timeval _close_ac_contactor;
  84. struct timeval _priority_time;
  85. Ver ver;
  86. PresentInputVoltage inputVoltage;
  87. PresentOutputVoltage outputVoltage;
  88. FanSpeed fanSpeed;
  89. Temperature temperature;
  90. AuxPower auxPower;
  91. Gfd gfd_adc;
  92. Gfd_config gfd_config;
  93. Gpio_in gpio_in;
  94. Gpio_out gpio_out;
  95. Relay outputRelay;
  96. Relay regRelay;
  97. Rtc rtc;
  98. void PRINTF_FUNC(char *string, ...);
  99. int StoreLogMsg(const char *fmt, ...);
  100. unsigned long GetTimeoutValue(struct timeval _sour_time);
  101. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  102. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  103. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  104. unsigned long GetTimeoutValue(struct timeval _sour_time)
  105. {
  106. struct timeval _end_time;
  107. gettimeofday(&_end_time, NULL);
  108. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  109. }
  110. int StoreLogMsg(const char *fmt, ...)
  111. {
  112. char Buf[4096+256];
  113. char buffer[4096];
  114. time_t CurrentTime;
  115. struct tm *tm;
  116. va_list args;
  117. va_start(args, fmt);
  118. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  119. va_end(args);
  120. memset(Buf,0,sizeof(Buf));
  121. CurrentTime = time(NULL);
  122. tm=localtime(&CurrentTime);
  123. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  124. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  125. buffer,
  126. tm->tm_year+1900,tm->tm_mon+1);
  127. system(Buf);
  128. return rc;
  129. }
  130. int DiffTimeb(struct timeb ST, struct timeb ET)
  131. {
  132. //return milli-second
  133. unsigned int StartTime,StopTime;
  134. StartTime=(unsigned int)ST.time;
  135. StopTime=(unsigned int)ET.time;
  136. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  137. }
  138. unsigned short MaxValue(unsigned short value1, unsigned short value2)
  139. {
  140. return value1 >= value2 ? value1 : value2;
  141. }
  142. void PRINTF_FUNC(char *string, ...)
  143. {
  144. va_list args;
  145. char buffer[4096];
  146. va_start(args, string);
  147. vsnprintf(buffer, sizeof(buffer), string, args);
  148. va_end(args);
  149. if (DEBUG)
  150. printf("%s \n", buffer);
  151. else
  152. DEBUG_INFO("%s \n", buffer);
  153. }
  154. //==========================================
  155. // Communication Function
  156. //==========================================
  157. void GetFwAndHwVersion_Fan()
  158. {
  159. if(Query_FW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
  160. {
  161. // FanModuleData
  162. strcpy((char *) ShmFanModuleData->version, ver.Version_FW);
  163. // SystemInfo
  164. strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, ver.Version_FW);
  165. //PRINTF_FUNC("GetFwAndHwVersion_Fan s1 = %s \n", ver.Version_FW);
  166. }
  167. if (Query_HW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
  168. {
  169. // SystemInfo
  170. strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, ver.Version_FW);
  171. //PRINTF_FUNC("GetFwAndHwVersion_Fan s2 = %s \n", ver.Version_HW);
  172. }
  173. }
  174. void GetFwAndHwVersion_Relay()
  175. {
  176. if (Query_FW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
  177. {
  178. // FanModuleData
  179. strcpy((char *) ShmRelayModuleData->version, ver.Version_FW);
  180. // SystemInfo
  181. strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, ver.Version_FW);
  182. //PRINTF_FUNC("GetFwAndHwVersion_Relay s1 = %s \n", ver.Version_FW);
  183. }
  184. if (Query_HW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
  185. {
  186. // SystemInfo
  187. strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, ver.Version_FW);
  188. //PRINTF_FUNC("GetFwAndHwVersion_Relay s2 = %s \n", ver.Version_HW);
  189. }
  190. }
  191. void SetRtcData_Relay()
  192. {
  193. struct timeb csuTime;
  194. struct tm *tmCSU;
  195. ftime(&csuTime);
  196. tmCSU = localtime(&csuTime.time);
  197. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  198. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  199. // tmCSU->tm_sec);
  200. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  201. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  202. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  203. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  204. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  205. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  206. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  207. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  208. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  209. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  210. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  211. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  212. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  213. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  214. if (Config_Rtc_Data(Uart5Fd, Addr.Relay, &rtc) == PASS)
  215. {
  216. //PRINTF_FUNC("SetRtc (RB) sucessfully. \n");
  217. }
  218. }
  219. void SetRtcData_Fan()
  220. {
  221. struct timeb csuTime;
  222. struct tm *tmCSU;
  223. ftime(&csuTime);
  224. tmCSU = localtime(&csuTime.time);
  225. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  226. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  227. // tmCSU->tm_sec);
  228. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  229. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  230. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  231. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  232. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  233. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  234. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  235. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  236. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  237. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  238. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  239. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  240. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  241. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  242. if (Config_Rtc_Data(Uart5Fd, Addr.Fan, &rtc) == PASS)
  243. {
  244. //PRINTF_FUNC("SetRtc (FB) sucessfully. \n");
  245. }
  246. }
  247. void SetModelName_Fan()
  248. {
  249. if (Config_Model_Name(Uart5Fd, Addr.Fan, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  250. {
  251. PRINTF_FUNC("Set Model name PASS = %s \n", ShmSysConfigAndInfo->SysConfig.ModelName);
  252. }
  253. }
  254. // AC 三相輸入電壓
  255. void GetPresentInputVol()
  256. {
  257. if (Query_Present_InputVoltage(Uart5Fd, Addr.Relay, &inputVoltage) == PASS)
  258. {
  259. // resolution : 0.1
  260. ShmSysConfigAndInfo->SysInfo.InputVoltageR = ShmRelayModuleData->InputL1Volt = inputVoltage.L1N_L12;
  261. ShmSysConfigAndInfo->SysInfo.InputVoltageS = ShmRelayModuleData->InputL2Volt = inputVoltage.L2N_L23;
  262. ShmSysConfigAndInfo->SysInfo.InputVoltageT = ShmRelayModuleData->InputL3Volt = inputVoltage.L3N_L31;
  263. //********************************************************************************************************//
  264. // VIN < 170
  265. if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE)
  266. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = YES;
  267. else
  268. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = NO;
  269. if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE)
  270. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = YES;
  271. else
  272. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = NO;
  273. if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE)
  274. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = YES;
  275. else
  276. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = NO;
  277. //********************************************************************************************************//
  278. // VIN > 250
  279. if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE)
  280. {
  281. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = 0x01;
  282. }
  283. if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE)
  284. {
  285. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = 0x01;
  286. }
  287. if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE)
  288. {
  289. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = 0x01;
  290. }
  291. //********************************************************************************************************//
  292. // VIN < 150
  293. if (inputVoltage.L1N_L12 < VIN_DROP_VOLTAGE)
  294. {
  295. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = 0x01;
  296. }
  297. if (inputVoltage.L2N_L23 < VIN_DROP_VOLTAGE)
  298. {
  299. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputDrop = 0x01;
  300. }
  301. if (inputVoltage.L3N_L31 < VIN_DROP_VOLTAGE)
  302. {
  303. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputDrop = 0x01;
  304. }
  305. //********************************************************************************************************//
  306. // 150 <= VIN < 160
  307. // if (inputVoltage.L1N_L12 >= VIN_MIN_VOLTAGE && inputVoltage.L1N_L12 <= VIN_LOW_VOLTAGE)
  308. // {
  309. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = 0x00;
  310. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = 0x00;
  311. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = 0x00;
  312. // }
  313. // if (inputVoltage.L2N_L23 >= VIN_MIN_VOLTAGE && inputVoltage.L2N_L23 <= VIN_LOW_VOLTAGE)
  314. // {
  315. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = 0x00;
  316. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = 0x00;
  317. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputDrop = 0x00;
  318. // }
  319. // if (inputVoltage.L3N_L31 >= VIN_MIN_VOLTAGE && inputVoltage.L3N_L31 <= VIN_LOW_VOLTAGE)
  320. // {
  321. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = 0x00;
  322. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = 0x00;
  323. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputDrop = 0x00;
  324. // }
  325. }
  326. }
  327. // 左右槍的 Relay 前後的輸出電壓
  328. void GetPersentOutputVol()
  329. {
  330. if (Query_Present_OutputVoltage(Uart5Fd, Addr.Relay, &outputVoltage) == PASS)
  331. {
  332. // PRINTF_FUNC("Conn1 fuse 1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
  333. // PRINTF_FUNC("Conn1 relay 1 = %f \n", outputVoltage.behindRelay_Voltage_C1);
  334. // PRINTF_FUNC("Conn2 fuse 2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
  335. // PRINTF_FUNC("Conn2 relay 2 = %f \n", outputVoltage.behindRelay_Voltage_C2);
  336. //PRINTF_FUNC("outputVoltage.behindFuse_Voltage_C1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
  337. //PRINTF_FUNC("outputVoltage.behindFuse_Voltage_C2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
  338. ShmRelayModuleData->Gun1FuseOutputVolt = outputVoltage.behindFuse_Voltage_C1;
  339. ShmRelayModuleData->Gun1RelayOutputVolt = outputVoltage.behindRelay_Voltage_C1;
  340. ShmRelayModuleData->Gun2FuseOutputVolt = outputVoltage.behindFuse_Voltage_C2;
  341. ShmRelayModuleData->Gun2RelayOutputVolt = outputVoltage.behindRelay_Voltage_C2;
  342. for (int index = 0; index < gunCount; index++)
  343. {
  344. if (index == 0)
  345. {
  346. if (_chargingData[index]->Evboard_id == 0x01)
  347. {
  348. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun1FuseOutputVolt;
  349. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun1RelayOutputVolt;
  350. }
  351. else if (_chargingData[index]->Evboard_id == 0x02)
  352. {
  353. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
  354. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
  355. }
  356. }
  357. else if (index == 1)
  358. {
  359. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
  360. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
  361. }
  362. unsigned short Ovp = 0;
  363. unsigned short Ocp = 0;
  364. //Ovp = MIN [VOUT_MAX_VOLTAGE, EV_BATTERY_VOLTAGE] // 最大輸出電壓與電池電壓最大值
  365. //Ocp = MIN [IOUT_MAX_CURRENT, EV_CURRENT_REQ] // 最大輸出電流與需求電流最小值
  366. if (_chargingData[index]->Type == _Type_Chademo)
  367. {
  368. Ovp = MaxValue(_chargingData[index]->MaximumChargingVoltage, _chargingData[index]->EvBatteryMaxVoltage);
  369. Ocp = MaxValue(_chargingData[index]->PresentChargingCurrent, ShmCHAdeMOData->ev[_chargingData[index]->type_index].ChargingCurrentRequest);
  370. if (_chargingData[index]->PresentChargingVoltage >= Ovp)
  371. {
  372. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = 0x01;
  373. }
  374. if (_chargingData[index]->PresentChargingCurrent >= Ocp)
  375. {
  376. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOCP = 0x01;
  377. }
  378. }
  379. else if (_chargingData[index]->Type == _Type_CCS_2)
  380. {
  381. }
  382. }
  383. }
  384. }
  385. // 風扇速度
  386. void GetFanSpeed()
  387. {
  388. //PRINTF_FUNC("Get fan board speed \n");
  389. if (Query_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed) == PASS)
  390. {
  391. ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0];
  392. ShmFanModuleData->PresentFan2Speed = fanSpeed.speed[1];
  393. ShmFanModuleData->PresentFan3Speed = fanSpeed.speed[2];
  394. ShmFanModuleData->PresentFan4Speed = fanSpeed.speed[3];
  395. // PRINTF_FUNC("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
  396. // PRINTF_FUNC("SystemFanRotaSpeed_2 = %d \n", fanSpeed.speed[1]);
  397. // PRINTF_FUNC("SystemFanRotaSpeed_3 = %d \n", fanSpeed.speed[2]);
  398. // PRINTF_FUNC("SystemFanRotaSpeed_4 = %d \n", fanSpeed.speed[3]);
  399. // Config_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed[0]);
  400. //SysInfoData (SystemFanRotaSpeed)
  401. }
  402. }
  403. // 讀取 Relay 狀態
  404. void GetRelayOutputStatus()
  405. {
  406. if (Query_Relay_Output(Uart5Fd, Addr.Relay, &regRelay) == PASS)
  407. {
  408. regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  409. }
  410. }
  411. // 確認 K1 K2 relay 的狀態
  412. void CheckK1K2RelayOutput(byte index)
  413. {
  414. if (index == 0)
  415. {
  416. if (_chargingData[index]->Evboard_id == 0x01)
  417. {
  418. if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.Gun1_P == YES)
  419. _chargingData[index]->RelayK1K2Status = YES;
  420. else
  421. _chargingData[index]->RelayK1K2Status = NO;
  422. if(_chargingData[index]->Type == _Type_CCS_2)
  423. {
  424. if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  425. _chargingData[index]->RelayKPK2Status = YES;
  426. else
  427. _chargingData[index]->RelayKPK2Status = NO;
  428. }
  429. }
  430. else if (_chargingData[index]->Evboard_id == 0x02)
  431. {
  432. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
  433. _chargingData[index]->RelayK1K2Status = YES;
  434. else
  435. _chargingData[index]->RelayK1K2Status = NO;
  436. if(_chargingData[index]->Type == _Type_CCS_2)
  437. {
  438. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  439. _chargingData[index]->RelayKPK2Status = YES;
  440. else
  441. _chargingData[index]->RelayKPK2Status = NO;
  442. }
  443. }
  444. }
  445. else if (index == 1)
  446. {
  447. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
  448. _chargingData[index]->RelayK1K2Status = YES;
  449. else
  450. _chargingData[index]->RelayK1K2Status = NO;
  451. if(_chargingData[index]->Type == _Type_CCS_2)
  452. {
  453. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  454. _chargingData[index]->RelayKPK2Status = YES;
  455. else
  456. _chargingData[index]->RelayKPK2Status = NO;
  457. }
  458. }
  459. if (regRelay.relay_event.bits.Gun1_Parallel_N == YES && regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  460. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = YES;
  461. else
  462. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = NO;
  463. // PRINTF_FUNC("Check Relay Output. index = %d, RelayKPK2Status = %d, BridgeRelayStatus = %d \n",
  464. // index, _chargingData[index]->RelayKPK2Status, ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus);
  465. }
  466. void GetGfdAdc()
  467. {
  468. // define : 每 0.2 ~ 1 秒一次
  469. // occur : <= 75k 歐姆 @ 150 - 750 Vdc
  470. // warning : >= 100 歐姆 && <= 500 歐姆 @ 150-750 Vdc
  471. if (Query_Gfd_Adc(Uart5Fd, Addr.Relay, &gfd_adc) == PASS)
  472. {
  473. for (int i = 0; i < gunCount; i++)
  474. {
  475. if (i == 0)
  476. {
  477. _chargingData[i]->GroundFaultStatus = gfd_adc.result_conn1;
  478. if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
  479. {
  480. DEBUG_ERROR("GFD Fail. index = %d, R = %d, Vol = %d \n",
  481. i, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  482. PRINTF_FUNC("GFD Fail. index = %d, R = %d, Vol = %d \n",
  483. i, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  484. }
  485. }
  486. else if (i == 1)
  487. {
  488. _chargingData[i]->GroundFaultStatus = gfd_adc.result_conn2;
  489. if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
  490. {
  491. DEBUG_ERROR("GFD Fail. index = %d, R = %d, Vol = %d \n",
  492. i, gfd_adc.Resister_conn2, gfd_adc.voltage_conn2);
  493. PRINTF_FUNC("GFD Fail. index = %d, R = %d, Vol = %d \n",
  494. i, gfd_adc.Resister_conn2, gfd_adc.voltage_conn2);
  495. }
  496. }
  497. }
  498. //if (gfd_adc.result_conn1 != 0)
  499. {
  500. // PRINTF_FUNC("******************Resister_conn1 = %d, voltage_conn1 = %d, result_conn1 = %d, step = %d \n",
  501. // gfd_adc.Resister_conn1,
  502. // gfd_adc.voltage_conn1,
  503. // gfd_adc.result_conn1,
  504. // gfd_adc.rb_step_1);
  505. }
  506. }
  507. }
  508. void GetGpioInput()
  509. {
  510. if (Query_Gpio_Input(Uart5Fd, Addr.Aux, &gpio_in) == PASS)
  511. {
  512. // AC Contactor Status
  513. if (gpio_in.AC_MainBreaker == 1)
  514. {
  515. // AC Main Breaker ON
  516. PRINTF_FUNC("RB AC Main Breaker. \n");
  517. }
  518. if (gpio_in.SPD == 1)
  519. {
  520. // SPD (雷擊保護) ON
  521. PRINTF_FUNC("RB SPD. \n");
  522. }
  523. if (gpio_in.Door_Open == 1)
  524. {
  525. // Door Open
  526. PRINTF_FUNC("RB Door Open. \n");
  527. }
  528. if (gpio_in.GFD[0] == 1)
  529. {
  530. // GFD_1 Trigger
  531. }
  532. if (gpio_in.GFD[1] == 1)
  533. {
  534. // GFD_2 Trigger
  535. }
  536. if (gpio_in.AC_Drop == 1)
  537. {
  538. // AC Drop
  539. PRINTF_FUNC("RB AC Drop. \n");
  540. }
  541. if (gpio_in.Emergency_IO == 1)
  542. {
  543. // Emergency IO ON
  544. PRINTF_FUNC("RB Emergency IO ON. \n");
  545. }
  546. if (gpio_in.Button_Emergency_Press == 1)
  547. {
  548. // Emergency button Press
  549. }
  550. if (gpio_in.Button_On_Press == 1)
  551. {
  552. // On button Press
  553. }
  554. if (gpio_in.Button_Off_Press == 1)
  555. {
  556. // Off button Press
  557. }
  558. if (gpio_in.Key_1_Press == 1)
  559. {
  560. // key 1 press
  561. }
  562. if (gpio_in.Key_2_Press == 1)
  563. {
  564. // key 2 press
  565. }
  566. if (gpio_in.Key_3_Press == 1)
  567. {
  568. // key 3 press
  569. }
  570. if (gpio_in.Key_4_Press == 1)
  571. {
  572. // key 4 press
  573. }
  574. }
  575. }
  576. // 5V 12V 24V 48V
  577. void GetAuxPower()
  578. {
  579. if (Query_Aux_PowerVoltage(Uart5Fd, Addr.Fan, &auxPower) == PASS)
  580. {
  581. ShmSysConfigAndInfo->SysInfo.AuxPower48V = auxPower.voltage[0];
  582. ShmSysConfigAndInfo->SysInfo.AuxPower24V = auxPower.voltage[1];
  583. //ShmSysConfigAndInfo->SysInfo.AuxPower12V = auxPower.voltage[4];
  584. //ShmSysConfigAndInfo->SysInfo.AuxPower5V = auxPower.voltage[6];
  585. // aux power voltage
  586. //PRINTF_FUNC("aux1 = %x, \n", auxPower.voltage[0]);
  587. //PRINTF_FUNC("aux2 = %x, \n", auxPower.voltage[1]);
  588. }
  589. }
  590. void SetFanModuleSpeed()
  591. {
  592. // 調整風扇速度要漸進式 : 500 rpm/p
  593. if (ShmFanModuleData->PresentFan1Speed != ShmFanModuleData->SetFan1Speed ||
  594. ShmFanModuleData->PresentFan2Speed != ShmFanModuleData->SetFan2Speed ||
  595. ShmFanModuleData->PresentFan3Speed != ShmFanModuleData->SetFan3Speed ||
  596. ShmFanModuleData->PresentFan4Speed != ShmFanModuleData->SetFan4Speed)
  597. {
  598. FanSpeed _fanSpeed;
  599. unsigned short speed = ShmFanModuleData->PresentFan1Speed + fanSpeedSmoothValue;
  600. if (speed >= ShmFanModuleData->SetFan1Speed)
  601. speed = ShmFanModuleData->SetFan1Speed;
  602. _fanSpeed.speed[0] = speed;
  603. speed = ShmFanModuleData->PresentFan2Speed + fanSpeedSmoothValue;
  604. if (speed >= ShmFanModuleData->SetFan2Speed)
  605. speed = ShmFanModuleData->SetFan2Speed;
  606. _fanSpeed.speed[1] = speed;
  607. speed = ShmFanModuleData->PresentFan3Speed + fanSpeedSmoothValue;
  608. if (speed >= ShmFanModuleData->SetFan3Speed)
  609. speed = ShmFanModuleData->SetFan3Speed;
  610. _fanSpeed.speed[2] = speed;
  611. speed = ShmFanModuleData->PresentFan4Speed + fanSpeedSmoothValue;
  612. if (speed >= ShmFanModuleData->SetFan4Speed)
  613. speed = ShmFanModuleData->SetFan4Speed;
  614. _fanSpeed.speed[3] = speed;
  615. if (Config_Fan_Speed(Uart5Fd, Addr.Fan, &_fanSpeed) == PASS)
  616. {
  617. //PRINTF_FUNC("successfully Fan\n");
  618. }
  619. }
  620. }
  621. void SetRelayModuleFanSpeed()
  622. {
  623. // 調整風扇速度要漸進式 : 100 rpm/p
  624. if (ShmFanModuleData->PresentFan1Speed != ShmFanModuleData->SetFan1Speed)
  625. {
  626. FanSpeed _fanSpeed;
  627. unsigned short speed = 0;
  628. if (ShmFanModuleData->SetFan1Speed > ShmFanModuleData->PresentFan1Speed)
  629. {
  630. speed = ShmFanModuleData->PresentFan1Speed + fanSpeedSmoothValue;
  631. if (speed >= ShmFanModuleData->SetFan1Speed)
  632. speed = ShmFanModuleData->SetFan1Speed;
  633. }
  634. else
  635. {
  636. speed = ShmFanModuleData->PresentFan1Speed - fanSpeedSmoothValue;
  637. if (speed <= 0)
  638. speed = ShmFanModuleData->SetFan1Speed;
  639. }
  640. _fanSpeed.speed[0] = speed & 0xff;
  641. _fanSpeed.speed[1] = (speed >> 8) & 0xff;
  642. ShmFanModuleData->PresentFan1Speed = speed;
  643. Config_Fan_Speed(Uart5Fd, Addr.Relay, &_fanSpeed);
  644. }
  645. }
  646. void GetRelayModuleFanSpeed()
  647. {
  648. PRINTF_FUNC("Get fan board speed \n");
  649. if (Query_Fan_Speed(Uart5Fd, Addr.Relay, &fanSpeed) == PASS)
  650. {
  651. ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0] + (fanSpeed.speed[1] >> 8);
  652. PRINTF_FUNC("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
  653. }
  654. }
  655. //==========================================
  656. // Common Function
  657. //==========================================
  658. void SetK1K2RelayStatus(byte index)
  659. {
  660. if (_chargingData[index]->SystemStatus < S_PREPARING_FOR_EVSE)
  661. {
  662. if (_chargingData[index]->Evboard_id == 0x01)
  663. {
  664. if(regRelay.relay_event.bits.Gun1_P == YES)
  665. outputRelay.relay_event.bits.Gun1_P = NO;
  666. else if (regRelay.relay_event.bits.Gun1_N == YES)
  667. outputRelay.relay_event.bits.Gun1_N = NO;
  668. if (_chargingData[index]->Type == _Type_CCS_2)
  669. {
  670. if(regRelay.relay_event.bits.CCS_Precharge == YES)
  671. outputRelay.relay_event.bits.CCS_Precharge = NO;
  672. }
  673. }
  674. else if (_chargingData[index]->Evboard_id == 0x02)
  675. {
  676. if(regRelay.relay_event.bits.Gun2_P == YES)
  677. outputRelay.relay_event.bits.Gun2_P = NO;
  678. else if (regRelay.relay_event.bits.Gun2_N == YES)
  679. outputRelay.relay_event.bits.Gun2_N = NO;
  680. if (_chargingData[index]->Type == _Type_CCS_2)
  681. {
  682. if(regRelay.relay_event.bits.CCS_Precharge == YES)
  683. outputRelay.relay_event.bits.CCS_Precharge = NO;
  684. }
  685. }
  686. }
  687. else if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE &&
  688. _chargingData[index]->SystemStatus <= S_CHARGING))
  689. {
  690. if (_chargingData[index]->RelayWeldingCheck == YES)
  691. {
  692. if (_chargingData[index]->Evboard_id == 0x01)
  693. {
  694. if(regRelay.relay_event.bits.Gun1_N == NO)
  695. outputRelay.relay_event.bits.Gun1_N = YES;
  696. else if (regRelay.relay_event.bits.Gun1_P == NO)
  697. outputRelay.relay_event.bits.Gun1_P = YES;
  698. }
  699. else if (_chargingData[index]->Evboard_id == 0x02)
  700. {
  701. if(regRelay.relay_event.bits.Gun2_N == NO)
  702. outputRelay.relay_event.bits.Gun2_N = YES;
  703. else if (regRelay.relay_event.bits.Gun2_P == NO)
  704. outputRelay.relay_event.bits.Gun2_P = YES;
  705. }
  706. }
  707. }
  708. else if ((_chargingData[index]->SystemStatus >= S_TERMINATING && _chargingData[index]->SystemStatus <= S_COMPLETE))
  709. {
  710. if (_chargingData[index]->PresentChargingCurrent <= SEFETY_SWITCH_RELAY_CUR)
  711. {
  712. if (_chargingData[index]->Evboard_id == 0x01)
  713. {
  714. if(regRelay.relay_event.bits.Gun1_P == YES)
  715. outputRelay.relay_event.bits.Gun1_P = NO;
  716. else if (regRelay.relay_event.bits.Gun1_N == YES)
  717. outputRelay.relay_event.bits.Gun1_N = NO;
  718. }
  719. else if (_chargingData[index]->Evboard_id == 0x02)
  720. {
  721. if(regRelay.relay_event.bits.Gun2_P == YES)
  722. outputRelay.relay_event.bits.Gun2_P = NO;
  723. else if (regRelay.relay_event.bits.Gun2_N == YES)
  724. outputRelay.relay_event.bits.Gun2_N = NO;
  725. }
  726. }
  727. }
  728. else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST0)
  729. {
  730. if (_chargingData[index]->Evboard_id == 0x01)
  731. {
  732. if (_chargingData[index]->Type == _Type_CCS_2)
  733. {
  734. if (regRelay.relay_event.bits.CCS_Precharge == NO)
  735. outputRelay.relay_event.bits.CCS_Precharge = YES;
  736. else if (regRelay.relay_event.bits.CCS_Precharge == YES)
  737. outputRelay.relay_event.bits.Gun1_P = NO;
  738. }
  739. }
  740. else if (_chargingData[index]->Evboard_id == 0x02)
  741. {
  742. if (_chargingData[index]->Type == _Type_CCS_2)
  743. {
  744. if (regRelay.relay_event.bits.CCS_Precharge == NO)
  745. outputRelay.relay_event.bits.CCS_Precharge = YES;
  746. else if (regRelay.relay_event.bits.CCS_Precharge == YES)
  747. outputRelay.relay_event.bits.Gun2_P = NO;
  748. }
  749. }
  750. }
  751. else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST1)
  752. {
  753. if (_chargingData[index]->Evboard_id == 0x01)
  754. {
  755. if (_chargingData[index]->Type == _Type_CCS_2)
  756. {
  757. if (regRelay.relay_event.bits.Gun1_P == NO)
  758. outputRelay.relay_event.bits.Gun1_P = YES;
  759. else if(regRelay.relay_event.bits.Gun1_P == YES)
  760. outputRelay.relay_event.bits.CCS_Precharge = NO;
  761. }
  762. }
  763. else if (_chargingData[index]->Evboard_id == 0x02)
  764. {
  765. if (_chargingData[index]->Type == _Type_CCS_2)
  766. {
  767. if (regRelay.relay_event.bits.Gun2_P == NO)
  768. outputRelay.relay_event.bits.Gun2_P = YES;
  769. else if(regRelay.relay_event.bits.Gun2_P == YES)
  770. outputRelay.relay_event.bits.CCS_Precharge = NO;
  771. }
  772. }
  773. }
  774. }
  775. void CheckPhaseLossStatus(byte index)
  776. {
  777. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  778. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  779. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  780. {
  781. _chargingData[index]->StopChargeFlag = YES;
  782. }
  783. }
  784. void SetParalleRelayStatus()
  785. {
  786. if (gunCount >= 2 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  787. {
  788. if (_chargingData[0]->SystemStatus == S_BOOTING || _chargingData[1]->SystemStatus == S_BOOTING)
  789. {
  790. // 初始化~ 不搭橋接
  791. if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  792. outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
  793. else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
  794. outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
  795. }
  796. else
  797. {
  798. if (_chargingData[0]->IsReadyToCharging == YES ||
  799. _chargingData[1]->IsReadyToCharging == YES)
  800. {
  801. // ************需考慮在切換中 - 切開 relay 與搭回 relay 的時機點************
  802. if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_MAX)
  803. {
  804. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag < _REASSIGNED_RELAY_M_TO_A)
  805. {
  806. // 最大充 - 搭上橋接
  807. if (regRelay.relay_event.bits.Gun1_Parallel_N == NO)
  808. outputRelay.relay_event.bits.Gun1_Parallel_N = YES;
  809. else if (regRelay.relay_event.bits.Gun1_Parallel_P == NO)
  810. outputRelay.relay_event.bits.Gun1_Parallel_P = YES;
  811. }
  812. else
  813. {
  814. // 平均充 - 不搭
  815. if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  816. outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
  817. else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
  818. outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
  819. }
  820. }
  821. else if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_AVER)
  822. {
  823. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag < _REASSIGNED_RELAY_A_TO_M)
  824. {
  825. // 平均充 - 不搭
  826. if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  827. outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
  828. else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
  829. outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
  830. }
  831. else
  832. {
  833. // 最大充 - 搭上橋接
  834. if (regRelay.relay_event.bits.Gun1_Parallel_N == NO)
  835. outputRelay.relay_event.bits.Gun1_Parallel_N = YES;
  836. else if (regRelay.relay_event.bits.Gun1_Parallel_P == NO)
  837. outputRelay.relay_event.bits.Gun1_Parallel_P = YES;
  838. }
  839. }
  840. }
  841. }
  842. }
  843. }
  844. //==========================================
  845. // Init all share memory
  846. //==========================================
  847. int InitShareMemory()
  848. {
  849. int result = PASS;
  850. int MeterSMId;
  851. //creat ShmSysConfigAndInfo
  852. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  853. {
  854. #ifdef SystemLogMessage
  855. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  856. #endif
  857. result = FAIL;
  858. }
  859. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  860. {
  861. #ifdef SystemLogMessage
  862. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  863. #endif
  864. result = FAIL;
  865. }
  866. //creat ShmStatusCodeData
  867. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  868. {
  869. #ifdef SystemLogMessage
  870. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  871. #endif
  872. result = FAIL;
  873. }
  874. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  875. {
  876. #ifdef SystemLogMessage
  877. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  878. #endif
  879. result = FAIL;
  880. }
  881. //creat ShmFanModuleData
  882. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), 0777)) < 0)
  883. {
  884. #ifdef SystemLogMessage
  885. DEBUG_ERROR("shmget ShmFanModuleData NG\n");
  886. #endif
  887. result = FAIL;
  888. }
  889. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  890. {
  891. #ifdef SystemLogMessage
  892. DEBUG_ERROR("shmat ShmFanModuleData NG\n");
  893. #endif
  894. result = FAIL;
  895. }
  896. memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
  897. //creat ShmRelayModuleData
  898. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), 0777)) < 0)
  899. {
  900. #ifdef SystemLogMessage
  901. DEBUG_ERROR("shmget ShmRelayModuleData NG\n");
  902. #endif
  903. result = FAIL;
  904. }
  905. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  906. {
  907. #ifdef SystemLogMessage
  908. DEBUG_ERROR("shmat ShmRelayModuleData NG\n");
  909. #endif
  910. result = FAIL;
  911. }
  912. //creat ShmPsuData
  913. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  914. {
  915. #ifdef SystemLogMessage
  916. DEBUG_ERROR("shmget ShmPsuData NG \n");
  917. #endif
  918. result = FAIL;
  919. }
  920. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  921. {
  922. #ifdef SystemLogMessage
  923. DEBUG_ERROR("shmat ShmPsuData NG \n");
  924. #endif
  925. result = FAIL;
  926. }
  927. memset(ShmPsuData,0,sizeof(struct PsuData));
  928. if(CHAdeMO_QUANTITY > 0)
  929. {
  930. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  931. {
  932. #ifdef SystemLogMessage
  933. DEBUG_ERROR("[shmget ShmCHAdeMOData NG \n");
  934. #endif
  935. return FAIL;
  936. }
  937. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
  938. #ifdef SystemLogMessage
  939. DEBUG_ERROR("shmat ShmCHAdeMOData NG \n");
  940. #endif
  941. return FAIL;
  942. }
  943. }
  944. if(CCS_QUANTITY > 0)
  945. {
  946. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  947. {
  948. #ifdef SystemLogMessage
  949. DEBUG_ERROR("shmget ShmCcsData NG \n");
  950. #endif
  951. return FAIL;
  952. }
  953. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  954. {
  955. #ifdef SystemLogMessage
  956. DEBUG_ERROR("shmat ShmCcsData NG \n");
  957. #endif
  958. return FAIL;
  959. }
  960. }
  961. return result;
  962. }
  963. int InitComPort()
  964. {
  965. int fd;
  966. struct termios tios;
  967. fd = open(relayRs485PortName, O_RDWR);
  968. if(fd <= 0)
  969. {
  970. #ifdef SystemLogMessage
  971. DEBUG_ERROR("Module_InternalComm. InitComPort NG\n");
  972. #endif
  973. if(ShmStatusCodeData!=NULL)
  974. {
  975. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  976. }
  977. sleep(5);
  978. return -1;
  979. }
  980. ioctl (fd, TCGETS, &tios);
  981. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  982. tios.c_lflag = 0;
  983. tios.c_iflag = 0;
  984. tios.c_oflag = 0;
  985. tios.c_cc[VMIN]=0;
  986. tios.c_cc[VTIME]=(byte)0; // timeout 0.5 second
  987. tios.c_lflag=0;
  988. tcflush(fd, TCIFLUSH);
  989. ioctl (fd, TCSETS, &tios);
  990. return fd;
  991. }
  992. //================================================
  993. // Main process
  994. //================================================
  995. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  996. {
  997. for (byte index = 0; index < CHAdeMO_QUANTITY; index++) {
  998. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index
  999. == target) {
  1000. chargingData[target] =
  1001. &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  1002. return true;
  1003. }
  1004. }
  1005. for (byte index = 0; index < CCS_QUANTITY; index++) {
  1006. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index
  1007. == target) {
  1008. chargingData[target] =
  1009. &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  1010. return true;
  1011. }
  1012. }
  1013. for (byte index = 0; index < GB_QUANTITY; index++) {
  1014. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index
  1015. == target) {
  1016. chargingData[target] =
  1017. &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  1018. return true;
  1019. }
  1020. }
  1021. return false;
  1022. }
  1023. void Initialization()
  1024. {
  1025. bool isPass = false;
  1026. for (byte index = 0; index < ARRAY_SIZE(outputRelay.relay_event.relay_status); index++)
  1027. {
  1028. outputRelay.relay_event.relay_status[index] = 0x00;
  1029. }
  1030. while(!isPass)
  1031. {
  1032. isPass = true;
  1033. for (byte _index = 0; _index < gunCount; _index++)
  1034. {
  1035. if (!FindChargingInfoData(_index, &_chargingData[0]))
  1036. {
  1037. DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
  1038. isPass = false;
  1039. break;
  1040. }
  1041. }
  1042. }
  1043. }
  1044. bool IsNoneMatchRelayStatus()
  1045. {
  1046. bool result = false;
  1047. // PRINTF_FUNC("Real Relay, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
  1048. // regRelay.relay_event.bits.AC_Contactor,
  1049. // regRelay.relay_event.bits.Gun1_P,
  1050. // regRelay.relay_event.bits.Gun1_N,
  1051. // regRelay.relay_event.bits.Gun2_P,
  1052. // regRelay.relay_event.bits.Gun2_N,
  1053. // regRelay.relay_event.bits.CCS_Precharge,
  1054. // regRelay.relay_event.bits.Gun1_Parallel_P,
  1055. // regRelay.relay_event.bits.Gun1_Parallel_N);
  1056. if ((regRelay.relay_event.bits.AC_Contactor != outputRelay.relay_event.bits.AC_Contactor) ||
  1057. (regRelay.relay_event.bits.CCS_Precharge != outputRelay.relay_event.bits.CCS_Precharge) ||
  1058. (regRelay.relay_event.bits.Gun1_P != outputRelay.relay_event.bits.Gun1_P) ||
  1059. (regRelay.relay_event.bits.Gun1_N != outputRelay.relay_event.bits.Gun1_N) ||
  1060. (regRelay.relay_event.bits.Gun2_P != outputRelay.relay_event.bits.Gun2_P) ||
  1061. (regRelay.relay_event.bits.Gun2_N != outputRelay.relay_event.bits.Gun2_N) ||
  1062. (regRelay.relay_event.bits.Gun1_Parallel_P != outputRelay.relay_event.bits.Gun1_Parallel_P) ||
  1063. (regRelay.relay_event.bits.Gun1_Parallel_N != outputRelay.relay_event.bits.Gun1_Parallel_N))
  1064. {
  1065. result = true;
  1066. }
  1067. return result;
  1068. }
  1069. void MatchRelayStatus()
  1070. {
  1071. // 因為 AC Contactor 沒有 Feedback,所以暫時先這樣處理
  1072. //regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
  1073. ShmSysConfigAndInfo->SysInfo.AcContactorStatus = regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
  1074. regRelay.relay_event.bits.CCS_Precharge = outputRelay.relay_event.bits.CCS_Precharge;
  1075. regRelay.relay_event.bits.Gun1_P = outputRelay.relay_event.bits.Gun1_P;
  1076. regRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_N;
  1077. regRelay.relay_event.bits.Gun2_P = outputRelay.relay_event.bits.Gun2_P;
  1078. regRelay.relay_event.bits.Gun2_N = outputRelay.relay_event.bits.Gun2_N;
  1079. regRelay.relay_event.bits.Gun1_Parallel_P = outputRelay.relay_event.bits.Gun1_Parallel_P;
  1080. regRelay.relay_event.bits.Gun1_Parallel_N = outputRelay.relay_event.bits.Gun1_Parallel_N;
  1081. }
  1082. void CheckRelayStatusByADC()
  1083. {
  1084. if (ShmRelayModuleData->Gun1FuseOutputVolt > 0 && ShmRelayModuleData->Gun1RelayOutputVolt > 0 &&
  1085. (ShmRelayModuleData->Gun1FuseOutputVolt == ShmRelayModuleData->Gun1RelayOutputVolt))
  1086. {
  1087. // Relay 前後電壓一致
  1088. _chargingData[0]->RelayK1K2Status = 0x01;
  1089. }
  1090. else
  1091. _chargingData[0]->RelayK1K2Status = 0x00;
  1092. if (ShmRelayModuleData->Gun2FuseOutputVolt > 0 && ShmRelayModuleData->Gun2RelayOutputVolt > 0 &&
  1093. (ShmRelayModuleData->Gun2FuseOutputVolt == ShmRelayModuleData->Gun2RelayOutputVolt))
  1094. {
  1095. // Relay 前後電壓一致
  1096. _chargingData[1]->RelayK1K2Status = 0x01;
  1097. }
  1098. else
  1099. _chargingData[1]->RelayK1K2Status = 0x00;
  1100. }
  1101. void SetGfdConfig(byte index, byte resister)
  1102. {
  1103. gfd_config.index = index;
  1104. gfd_config.state = resister;
  1105. //PRINTF_FUNC("************************GFD Vol = %d, GFD Res = %d \n", gfd_config.reqVol, gfd_config.resister);
  1106. if (Config_Gfd_Value(Uart5Fd, Addr.Relay, &gfd_config) == PASS)
  1107. {
  1108. // PRINTF_FUNC("Set reqVol = %f, resister = %d \n",
  1109. // gfd_config.reqVol,
  1110. // gfd_config.resister);
  1111. }
  1112. }
  1113. void CableCheckDetected(byte index)
  1114. {
  1115. // Cable Check
  1116. // 當火線上的電壓 = 車端要求的電壓電流
  1117. // _chargingData[targetGun]->EvBatterytargetVoltage
  1118. // 才可以開始偵測 1s
  1119. // Warning : Rgfd <= 150 歐/V 假設電壓為 500V 則~ Rgfd <= 75000 歐
  1120. // Pre-Warning : 150 歐/V < Rgfd <= 500 歐/V 假設電壓為 500V 則 75000 歐 < Rgfd <= 250000
  1121. // SO Normal : Rgfd > 500 歐/V 假設電壓為 500 V 則 Rgfd > 250000 歐
  1122. if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE && _chargingData[index]->SystemStatus <= S_CHARGING) ||
  1123. (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1124. {
  1125. if (_chargingData[index]->SystemStatus == S_PREPARING_FOR_EVSE)
  1126. {
  1127. SetGfdConfig(index, GFD_CABLECHK);
  1128. }
  1129. else if (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1130. _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1)
  1131. {
  1132. SetGfdConfig(index, GFD_PRECHARGE);
  1133. }
  1134. else if (_chargingData[index]->SystemStatus <= S_CHARGING)
  1135. {
  1136. SetGfdConfig(index, GFD_CHARGING);
  1137. }
  1138. }
  1139. else if(_chargingData[index]->SystemStatus == S_COMPLETE || _chargingData[index]->SystemStatus == S_PREPARNING
  1140. || _chargingData[index]->SystemStatus == S_IDLE)
  1141. {
  1142. SetGfdConfig(index, GFD_IDLE);
  1143. }
  1144. }
  1145. void CheckOutputPowerOverCarReq(byte index)
  1146. {
  1147. float fireV = _chargingData[index]->FireChargingVoltage;
  1148. float carV = _chargingData[index]->EvBatterytargetVoltage;
  1149. if (_chargingData[index]->EvBatterytargetVoltage > 1500 &&
  1150. (_chargingData[index]->Type == _Type_Chademo ||
  1151. _chargingData[index]->Type == _Type_CCS_2 ||
  1152. _chargingData[index]->Type == _Type_GB))
  1153. {
  1154. if (fireV >= (carV + (carV * 0.1)))
  1155. {
  1156. PRINTF_FUNC("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f \n",
  1157. _chargingData[index]->FireChargingVoltage, _chargingData[index]->EvBatterytargetVoltage);
  1158. DEBUG_ERROR("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f \n",
  1159. _chargingData[index]->FireChargingVoltage, _chargingData[index]->EvBatterytargetVoltage);
  1160. _chargingData[index]->StopChargeFlag = YES;
  1161. }
  1162. }
  1163. }
  1164. void CheckOutputVolNoneMatchFire(byte index)
  1165. {
  1166. if (_chargingData[index]->EvBatterytargetVoltage > 1500 &&
  1167. (_chargingData[index]->Type == _Type_Chademo ||
  1168. _chargingData[index]->Type == _Type_CCS_2 ||
  1169. _chargingData[index]->Type == _Type_GB))
  1170. {
  1171. if ((_chargingData[index]->PresentChargingVoltage < _chargingData[index]->FireChargingVoltage - 300) ||
  1172. (_chargingData[index]->PresentChargingVoltage > _chargingData[index]->FireChargingVoltage + 300))
  1173. {
  1174. if (!_isOutputNoneMatch[index])
  1175. {
  1176. _isOutputNoneMatch[index] = YES;
  1177. gettimeofday(&_checkOutputNoneMatchTimer[index], NULL);
  1178. }
  1179. else
  1180. {
  1181. if ((GetTimeoutValue(_checkOutputNoneMatchTimer[index]) / 1000) >= 5000)
  1182. {
  1183. PRINTF_FUNC("[Module_InternalComm]CheckOutputVolNoneMatchFire NG (%d) : pre = %f, fire = %f \n",
  1184. index, _chargingData[index]->PresentChargingVoltage, _chargingData[index]->FireChargingVoltage);
  1185. DEBUG_ERROR("[Module_InternalComm]CheckOutputVolNoneMatchFire NG (%d): pre = %f, fire = %f \n",
  1186. index, _chargingData[index]->PresentChargingVoltage, _chargingData[index]->FireChargingVoltage);
  1187. _chargingData[index]->StopChargeFlag = YES;
  1188. }
  1189. }
  1190. }
  1191. else
  1192. _isOutputNoneMatch[index] = NO;
  1193. }
  1194. }
  1195. void CheckRelayWeldingStatus(byte index)
  1196. {
  1197. if (!_isRelayWelding[index])
  1198. {
  1199. if ( _chargingData[index]->PresentChargingVoltage >= 150)
  1200. {
  1201. gettimeofday(&_checkRelayWeldingTimer[index], NULL);
  1202. _isRelayWelding[index] = YES;
  1203. }
  1204. }
  1205. else
  1206. {
  1207. if ((GetTimeoutValue(_checkRelayWeldingTimer[index]) / 1000) >= 1000)
  1208. {
  1209. _chargingData[index]->RelayWeldingCheck = YES;
  1210. return;
  1211. }
  1212. if (_chargingData[index]->FireChargingVoltage >= 150)
  1213. {
  1214. if (_chargingData[index]->Type == _Type_Chademo)
  1215. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = YES;
  1216. else if (_chargingData[index]->Type == _Type_GB)
  1217. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = YES;
  1218. else if (_chargingData[index]->Type == _Type_CCS_2)
  1219. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = YES;
  1220. _chargingData[index]->StopChargeFlag = YES;
  1221. }
  1222. }
  1223. }
  1224. void GetPsuTempForFanSpeed()
  1225. {
  1226. char temp = 0;
  1227. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1228. {
  1229. for (byte count = 0; count < ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity; count++)
  1230. {
  1231. if (temp < ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp)
  1232. temp = ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp;
  1233. }
  1234. }
  1235. if (ShmFanModuleData->TestFanSpeed == NORMAL_FAN_SPEED)
  1236. {
  1237. if (temp >= ENV_TEMP_MAX)
  1238. ShmFanModuleData->TestFanSpeed = MAX_FAN_SPEED;
  1239. }
  1240. else if (ShmFanModuleData->TestFanSpeed == MAX_FAN_SPEED)
  1241. {
  1242. if (temp <= ENV_TEMP_MIN)
  1243. ShmFanModuleData->TestFanSpeed = NORMAL_FAN_SPEED;
  1244. }
  1245. else
  1246. ShmFanModuleData->TestFanSpeed = NORMAL_FAN_SPEED;
  1247. }
  1248. int main(void)
  1249. {
  1250. if(InitShareMemory() == FAIL)
  1251. {
  1252. #ifdef SystemLogMessage
  1253. DEBUG_ERROR("InitShareMemory NG\n");
  1254. #endif
  1255. if(ShmStatusCodeData!=NULL)
  1256. {
  1257. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1258. }
  1259. sleep(5);
  1260. return 0;
  1261. }
  1262. gunCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1263. // Open Uart5 for RB
  1264. Uart5Fd = InitComPort();
  1265. Initialization();
  1266. sleep(1);
  1267. if(Uart5Fd < 0)
  1268. {
  1269. PRINTF_FUNC("(Internal) open port error. \n");
  1270. return 0;
  1271. }
  1272. outputRelay.relay_event.bits.AC_Contactor = 0x00;
  1273. //outputRelay.relay_event.bits.CCS_Precharge = 0x00;
  1274. //outputRelay.relay_event.bits.Gun1_Parallel_P = 0x01;
  1275. //outputRelay.relay_event.bits.Gun1_Parallel_N = 0x01;
  1276. //outputRelay.relay_event.bits.Gun1_P = 0x01;
  1277. //outputRelay.relay_event.bits.Gun1_N = 0x01;
  1278. //outputRelay.relay_event.bits.Gun2_N = 0x01;
  1279. //outputRelay.relay_event.bits.Gun2_P = 0x01;
  1280. if(Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay) != PASS)
  1281. PRINTF_FUNC("Config_Relay_Output fail \n");
  1282. bool printRelayStatus = true;
  1283. //return 0;
  1284. for(;;)
  1285. {
  1286. bool isCharging = false;
  1287. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  1288. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1289. {
  1290. GetFwAndHwVersion_Relay();
  1291. SetRtcData_Relay();
  1292. sleep(1);
  1293. }
  1294. if (ShmFanModuleData->SelfTest_Comp == NO)
  1295. {
  1296. GetFwAndHwVersion_Fan();
  1297. SetModelName_Fan();
  1298. SetRtcData_Fan();
  1299. sleep(1);
  1300. gettimeofday(&_priority_time, NULL);
  1301. }
  1302. if (ShmRelayModuleData->SelfTest_Comp == YES)
  1303. {
  1304. // ==============優先權最高 10 ms ==============
  1305. // 輸出電壓
  1306. GetPersentOutputVol();
  1307. // 三相輸入電壓
  1308. GetPresentInputVol();
  1309. // 讀取當前 relay 狀態
  1310. GetRelayOutputStatus();
  1311. for (int i = 0; i < gunCount; i++)
  1312. {
  1313. // Cable check (Set)
  1314. CableCheckDetected(i);
  1315. // check k1 k2 relay 狀態
  1316. CheckK1K2RelayOutput(i);
  1317. // 依據當前各槍的狀態選擇 搭上/放開 Relay
  1318. SetK1K2RelayStatus(i);
  1319. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  1320. CheckPhaseLossStatus(i);
  1321. if (_chargingData[i]->SystemStatus == S_IDLE)
  1322. {
  1323. _chargingData[i]->RelayWeldingCheck = NO;
  1324. _isRelayWelding[i] = NO;
  1325. }
  1326. if (_chargingData[i]->SystemStatus == S_BOOTING ||
  1327. (_chargingData[i]->SystemStatus >= S_PREPARNING && _chargingData[i]->SystemStatus <= S_COMPLETE) ||
  1328. (_chargingData[i]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[i]->SystemStatus <= S_CCS_PRECHARGE_ST1) ||
  1329. (ShmSysConfigAndInfo->SysInfo.PageIndex >= _LCM_AUTHORIZING && ShmSysConfigAndInfo->SysInfo.PageIndex <= _LCM_WAIT_FOR_PLUG))
  1330. {
  1331. _chargingData[i]->IsReadyToCharging = YES;
  1332. isCharging = true;
  1333. if (_chargingData[i]->SystemStatus >= S_PREPARING_FOR_EVSE &&
  1334. _chargingData[i]->RelayWeldingCheck == NO)
  1335. CheckRelayWeldingStatus(i);
  1336. if (_chargingData[i]->SystemStatus == S_CHARGING)
  1337. {
  1338. CheckOutputPowerOverCarReq(i);
  1339. CheckOutputVolNoneMatchFire(i);
  1340. }
  1341. else
  1342. _isOutputNoneMatch[i] = NO;
  1343. }
  1344. else
  1345. _chargingData[i]->IsReadyToCharging = NO;
  1346. }
  1347. // Cable check (Get)
  1348. GetGfdAdc();
  1349. // 橋接 relay
  1350. SetParalleRelayStatus();
  1351. // 搭上 AC Contactor
  1352. if (isCharging)
  1353. outputRelay.relay_event.bits.AC_Contactor = YES;
  1354. else
  1355. outputRelay.relay_event.bits.AC_Contactor = NO;
  1356. if (isCharging)
  1357. {
  1358. isStopChargingCount = false;
  1359. outputRelay.relay_event.bits.AC_Contactor = YES;
  1360. }
  1361. else
  1362. {
  1363. if (!isStopChargingCount)
  1364. {
  1365. gettimeofday(&_close_ac_contactor, NULL);
  1366. isStopChargingCount = true;
  1367. }
  1368. else
  1369. {
  1370. if (!isSystemBooting ||
  1371. (outputRelay.relay_event.bits.AC_Contactor == YES && GetTimeoutValue(_close_ac_contactor) / 1000 >= (TEN_MINUTES * 1000)))
  1372. outputRelay.relay_event.bits.AC_Contactor = NO;
  1373. }
  1374. }
  1375. // 搭上/鬆開 Relay
  1376. if(IsNoneMatchRelayStatus())
  1377. {
  1378. if (printRelayStatus)
  1379. {
  1380. 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",
  1381. outputRelay.relay_event.bits.AC_Contactor,
  1382. outputRelay.relay_event.bits.Gun1_P,
  1383. outputRelay.relay_event.bits.Gun1_N,
  1384. outputRelay.relay_event.bits.Gun2_P,
  1385. outputRelay.relay_event.bits.Gun2_N,
  1386. outputRelay.relay_event.bits.CCS_Precharge,
  1387. outputRelay.relay_event.bits.Gun1_Parallel_P,
  1388. outputRelay.relay_event.bits.Gun1_Parallel_N);
  1389. }
  1390. printRelayStatus = false;
  1391. if (Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay))
  1392. {}
  1393. }
  1394. else
  1395. {
  1396. if (!printRelayStatus)
  1397. {
  1398. 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",
  1399. regRelay.relay_event.bits.AC_Contactor,
  1400. regRelay.relay_event.bits.Gun1_P,
  1401. regRelay.relay_event.bits.Gun1_N,
  1402. regRelay.relay_event.bits.Gun2_P,
  1403. regRelay.relay_event.bits.Gun2_N,
  1404. regRelay.relay_event.bits.CCS_Precharge,
  1405. regRelay.relay_event.bits.Gun1_Parallel_P,
  1406. regRelay.relay_event.bits.Gun1_Parallel_N);
  1407. }
  1408. printRelayStatus = true;
  1409. }
  1410. }
  1411. if (ShmFanModuleData->SelfTest_Comp == YES)
  1412. {
  1413. if (GetTimeoutValue(_priority_time) / 1000 >= 1000)
  1414. {
  1415. GetPsuTempForFanSpeed();
  1416. GetFanSpeed();
  1417. // printf("ShmFanModuleData->PresentFan1Speed = %d \n", ShmFanModuleData->PresentFan1Speed);
  1418. // printf("ShmFanModuleData->PresentFan2Speed = %d \n", ShmFanModuleData->PresentFan2Speed);
  1419. // printf("ShmFanModuleData->PresentFan3Speed = %d \n", ShmFanModuleData->PresentFan3Speed);
  1420. // printf("ShmFanModuleData->PresentFan4Speed = %d \n", ShmFanModuleData->PresentFan4Speed);
  1421. gettimeofday(&_priority_time, NULL);
  1422. if (isCharging)
  1423. {
  1424. if (ShmFanModuleData->PresentFan1Speed < MAX_FAN_SPEED ||
  1425. ShmFanModuleData->PresentFan2Speed < MAX_FAN_SPEED ||
  1426. ShmFanModuleData->PresentFan3Speed < MAX_FAN_SPEED ||
  1427. ShmFanModuleData->PresentFan4Speed < MAX_FAN_SPEED)
  1428. {
  1429. ShmFanModuleData->SetFan1Speed = MAX_FAN_SPEED;
  1430. ShmFanModuleData->SetFan2Speed = MAX_FAN_SPEED;
  1431. ShmFanModuleData->SetFan3Speed = MAX_FAN_SPEED;
  1432. ShmFanModuleData->SetFan4Speed = MAX_FAN_SPEED;
  1433. }
  1434. if (ShmFanModuleData->TestFanSpeed > 0)
  1435. {
  1436. ShmFanModuleData->SetFan1Speed = ShmFanModuleData->TestFanSpeed;
  1437. ShmFanModuleData->SetFan2Speed = ShmFanModuleData->TestFanSpeed;
  1438. ShmFanModuleData->SetFan3Speed = ShmFanModuleData->TestFanSpeed;
  1439. ShmFanModuleData->SetFan4Speed = ShmFanModuleData->TestFanSpeed;
  1440. }
  1441. }
  1442. else
  1443. {
  1444. if (ShmFanModuleData->PresentFan1Speed > MIN_FAN_SPEED ||
  1445. ShmFanModuleData->PresentFan2Speed < MAX_FAN_SPEED ||
  1446. ShmFanModuleData->PresentFan3Speed < MAX_FAN_SPEED ||
  1447. ShmFanModuleData->PresentFan4Speed < MAX_FAN_SPEED)
  1448. {
  1449. ShmFanModuleData->SetFan1Speed = MIN_FAN_SPEED;
  1450. ShmFanModuleData->SetFan2Speed = MIN_FAN_SPEED;
  1451. ShmFanModuleData->SetFan3Speed = MIN_FAN_SPEED;
  1452. ShmFanModuleData->SetFan4Speed = MIN_FAN_SPEED;
  1453. }
  1454. }
  1455. //PRINTF_FUNC("set fan = %d \n", ShmFanModuleData->SetFan1Speed);
  1456. SetFanModuleSpeed();
  1457. }
  1458. }
  1459. usleep(10000);
  1460. }
  1461. return FAIL;
  1462. }