Module_InternalComm.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  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 (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  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. }
  371. else if (_chargingData[index]->Type == _Type_CCS_2)
  372. {
  373. }
  374. }
  375. }
  376. }
  377. // 風扇速度
  378. void GetFanSpeed()
  379. {
  380. //PRINTF_FUNC("Get fan board speed \n");
  381. if (Query_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed) == PASS)
  382. {
  383. ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0];
  384. ShmFanModuleData->PresentFan2Speed = fanSpeed.speed[1];
  385. ShmFanModuleData->PresentFan3Speed = fanSpeed.speed[2];
  386. ShmFanModuleData->PresentFan4Speed = fanSpeed.speed[3];
  387. // PRINTF_FUNC("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
  388. // PRINTF_FUNC("SystemFanRotaSpeed_2 = %d \n", fanSpeed.speed[1]);
  389. // PRINTF_FUNC("SystemFanRotaSpeed_3 = %d \n", fanSpeed.speed[2]);
  390. // PRINTF_FUNC("SystemFanRotaSpeed_4 = %d \n", fanSpeed.speed[3]);
  391. // Config_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed[0]);
  392. //SysInfoData (SystemFanRotaSpeed)
  393. }
  394. }
  395. // 讀取 Relay 狀態
  396. void GetRelayOutputStatus()
  397. {
  398. if (Query_Relay_Output(Uart5Fd, Addr.Relay, &regRelay) == PASS)
  399. {
  400. regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  401. }
  402. }
  403. // 確認 K1 K2 relay 的狀態
  404. void CheckK1K2RelayOutput(byte index)
  405. {
  406. if (index == 0)
  407. {
  408. if (_chargingData[index]->Evboard_id == 0x01)
  409. {
  410. if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.Gun1_P == YES)
  411. _chargingData[index]->RelayK1K2Status = YES;
  412. else
  413. _chargingData[index]->RelayK1K2Status = NO;
  414. if(_chargingData[index]->Type == _Type_CCS_2)
  415. {
  416. if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  417. _chargingData[index]->RelayKPK2Status = YES;
  418. else
  419. _chargingData[index]->RelayKPK2Status = NO;
  420. }
  421. }
  422. else if (_chargingData[index]->Evboard_id == 0x02)
  423. {
  424. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
  425. _chargingData[index]->RelayK1K2Status = YES;
  426. else
  427. _chargingData[index]->RelayK1K2Status = NO;
  428. if(_chargingData[index]->Type == _Type_CCS_2)
  429. {
  430. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  431. _chargingData[index]->RelayKPK2Status = YES;
  432. else
  433. _chargingData[index]->RelayKPK2Status = NO;
  434. }
  435. }
  436. }
  437. else if (index == 1)
  438. {
  439. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
  440. _chargingData[index]->RelayK1K2Status = YES;
  441. else
  442. _chargingData[index]->RelayK1K2Status = NO;
  443. if(_chargingData[index]->Type == _Type_CCS_2)
  444. {
  445. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  446. _chargingData[index]->RelayKPK2Status = YES;
  447. else
  448. _chargingData[index]->RelayKPK2Status = NO;
  449. }
  450. }
  451. if (regRelay.relay_event.bits.Gun1_Parallel_N == YES && regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  452. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = YES;
  453. else
  454. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = NO;
  455. // PRINTF_FUNC("Check Relay Output. index = %d, RelayKPK2Status = %d, BridgeRelayStatus = %d \n",
  456. // index, _chargingData[index]->RelayKPK2Status, ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus);
  457. }
  458. void GetGfdAdc()
  459. {
  460. // define : 每 0.2 ~ 1 秒一次
  461. // occur : <= 75k 歐姆 @ 150 - 750 Vdc
  462. // warning : >= 100 歐姆 && <= 500 歐姆 @ 150-750 Vdc
  463. if (Query_Gfd_Adc(Uart5Fd, Addr.Relay, &gfd_adc) == PASS)
  464. {
  465. for (int i = 0; i < gunCount; i++)
  466. {
  467. if (_chargingData[i]->Type == 9)
  468. {
  469. if (_chargingData[i]->PresentChargingVoltage >= VOUT_MIN_VOLTAGE)
  470. _chargingData[i]->GroundFaultStatus = GFD_PASS;
  471. continue;
  472. }
  473. if (i == 0)
  474. {
  475. _chargingData[i]->GroundFaultStatus = gfd_adc.result_conn1;
  476. if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
  477. {
  478. PRINTF_FUNC("GFD Fail. index = %d, Step = %d, R = %d, Vol = %d \n",
  479. i, gfd_adc.rb_step_1, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  480. }
  481. else if (_chargingData[i]->GroundFaultStatus == GFD_PASS ||
  482. _chargingData[i]->GroundFaultStatus == GFD_WARNING)
  483. {
  484. // PRINTF_FUNC("GFD Result. index = %d, Result = %d, R = %d, Vol = %d \n",
  485. // i, _chargingData[i]->GroundFaultStatus, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  486. }
  487. }
  488. else if (i == 1)
  489. {
  490. _chargingData[i]->GroundFaultStatus = gfd_adc.result_conn2;
  491. if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
  492. {
  493. PRINTF_FUNC("GFD Fail. index = %d, Step = %d, R = %d, Vol = %d \n",
  494. i, gfd_adc.rb_step_2, gfd_adc.Resister_conn2, gfd_adc.voltage_conn2);
  495. }
  496. else if (_chargingData[i]->GroundFaultStatus == GFD_PASS ||
  497. _chargingData[i]->GroundFaultStatus == GFD_WARNING)
  498. {
  499. // PRINTF_FUNC("GFD Result. index = %d, Result = %d, R = %d, Vol = %d \n",
  500. // i, _chargingData[i]->GroundFaultStatus, gfd_adc.Resister_conn2, gfd_adc.voltage_conn2);
  501. }
  502. }
  503. }
  504. }
  505. }
  506. void GetGpioInput()
  507. {
  508. if (Query_Gpio_Input(Uart5Fd, Addr.Aux, &gpio_in) == PASS)
  509. {
  510. // AC Contactor Status
  511. if (gpio_in.AC_MainBreaker == 1)
  512. {
  513. // AC Main Breaker ON
  514. PRINTF_FUNC("RB AC Main Breaker. \n");
  515. }
  516. if (gpio_in.SPD == 1)
  517. {
  518. // SPD (雷擊保護) ON
  519. PRINTF_FUNC("RB SPD. \n");
  520. }
  521. if (gpio_in.Door_Open == 1)
  522. {
  523. // Door Open
  524. PRINTF_FUNC("RB Door Open. \n");
  525. }
  526. if (gpio_in.GFD[0] == 1)
  527. {
  528. // GFD_1 Trigger
  529. }
  530. if (gpio_in.GFD[1] == 1)
  531. {
  532. // GFD_2 Trigger
  533. }
  534. if (gpio_in.AC_Drop == 1)
  535. {
  536. // AC Drop
  537. PRINTF_FUNC("RB AC Drop. \n");
  538. }
  539. if (gpio_in.Emergency_IO == 1)
  540. {
  541. // Emergency IO ON
  542. PRINTF_FUNC("RB Emergency IO ON. \n");
  543. }
  544. if (gpio_in.Button_Emergency_Press == 1)
  545. {
  546. // Emergency button Press
  547. }
  548. if (gpio_in.Button_On_Press == 1)
  549. {
  550. // On button Press
  551. }
  552. if (gpio_in.Button_Off_Press == 1)
  553. {
  554. // Off button Press
  555. }
  556. if (gpio_in.Key_1_Press == 1)
  557. {
  558. // key 1 press
  559. }
  560. if (gpio_in.Key_2_Press == 1)
  561. {
  562. // key 2 press
  563. }
  564. if (gpio_in.Key_3_Press == 1)
  565. {
  566. // key 3 press
  567. }
  568. if (gpio_in.Key_4_Press == 1)
  569. {
  570. // key 4 press
  571. }
  572. }
  573. }
  574. // 5V 12V 24V 48V
  575. void GetAuxPower()
  576. {
  577. if (Query_Aux_PowerVoltage(Uart5Fd, Addr.Fan, &auxPower) == PASS)
  578. {
  579. ShmSysConfigAndInfo->SysInfo.AuxPower48V = auxPower.voltage[0];
  580. ShmSysConfigAndInfo->SysInfo.AuxPower24V = auxPower.voltage[1];
  581. //ShmSysConfigAndInfo->SysInfo.AuxPower12V = auxPower.voltage[4];
  582. //ShmSysConfigAndInfo->SysInfo.AuxPower5V = auxPower.voltage[6];
  583. // aux power voltage
  584. //PRINTF_FUNC("aux1 = %x, \n", auxPower.voltage[0]);
  585. //PRINTF_FUNC("aux2 = %x, \n", auxPower.voltage[1]);
  586. }
  587. }
  588. void SetFanModuleSpeed()
  589. {
  590. // 調整風扇速度要漸進式 : 500 rpm/p
  591. if (ShmFanModuleData->PresentFan1Speed != ShmFanModuleData->SetFan1Speed ||
  592. ShmFanModuleData->PresentFan2Speed != ShmFanModuleData->SetFan2Speed ||
  593. ShmFanModuleData->PresentFan3Speed != ShmFanModuleData->SetFan3Speed ||
  594. ShmFanModuleData->PresentFan4Speed != ShmFanModuleData->SetFan4Speed)
  595. {
  596. //printf("ShmFanModuleData->SetFan1Speed = %d \n", ShmFanModuleData->SetFan1Speed);
  597. FanSpeed _fanSpeed;
  598. unsigned short speed = ShmFanModuleData->PresentFan1Speed + fanSpeedSmoothValue;
  599. if (speed >= ShmFanModuleData->SetFan1Speed)
  600. speed = ShmFanModuleData->SetFan1Speed;
  601. _fanSpeed.speed[0] = speed;
  602. speed = ShmFanModuleData->PresentFan2Speed + fanSpeedSmoothValue;
  603. if (speed >= ShmFanModuleData->SetFan2Speed)
  604. speed = ShmFanModuleData->SetFan2Speed;
  605. _fanSpeed.speed[1] = speed;
  606. speed = ShmFanModuleData->PresentFan3Speed + fanSpeedSmoothValue;
  607. if (speed >= ShmFanModuleData->SetFan3Speed)
  608. speed = ShmFanModuleData->SetFan3Speed;
  609. _fanSpeed.speed[2] = speed;
  610. speed = ShmFanModuleData->PresentFan4Speed + fanSpeedSmoothValue;
  611. if (speed >= ShmFanModuleData->SetFan4Speed)
  612. speed = ShmFanModuleData->SetFan4Speed;
  613. _fanSpeed.speed[3] = speed;
  614. if (Config_Fan_Speed(Uart5Fd, Addr.Fan, &_fanSpeed) == PASS)
  615. {
  616. //PRINTF_FUNC("successfully Fan\n");
  617. }
  618. }
  619. }
  620. void SetRelayModuleFanSpeed()
  621. {
  622. // 調整風扇速度要漸進式 : 100 rpm/p
  623. if (ShmFanModuleData->PresentFan1Speed != ShmFanModuleData->SetFan1Speed)
  624. {
  625. FanSpeed _fanSpeed;
  626. unsigned short speed = 0;
  627. if (ShmFanModuleData->SetFan1Speed > ShmFanModuleData->PresentFan1Speed)
  628. {
  629. speed = ShmFanModuleData->PresentFan1Speed + fanSpeedSmoothValue;
  630. if (speed >= ShmFanModuleData->SetFan1Speed)
  631. speed = ShmFanModuleData->SetFan1Speed;
  632. }
  633. else
  634. {
  635. speed = ShmFanModuleData->PresentFan1Speed - fanSpeedSmoothValue;
  636. if (speed <= 0)
  637. speed = ShmFanModuleData->SetFan1Speed;
  638. }
  639. _fanSpeed.speed[0] = speed & 0xff;
  640. _fanSpeed.speed[1] = (speed >> 8) & 0xff;
  641. ShmFanModuleData->PresentFan1Speed = speed;
  642. Config_Fan_Speed(Uart5Fd, Addr.Relay, &_fanSpeed);
  643. }
  644. }
  645. void GetRelayModuleFanSpeed()
  646. {
  647. PRINTF_FUNC("Get fan board speed \n");
  648. if (Query_Fan_Speed(Uart5Fd, Addr.Relay, &fanSpeed) == PASS)
  649. {
  650. ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0] + (fanSpeed.speed[1] >> 8);
  651. PRINTF_FUNC("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
  652. }
  653. }
  654. //==========================================
  655. // Common Function
  656. //==========================================
  657. void SetK1K2RelayStatus(byte index)
  658. {
  659. if (_chargingData[index]->SystemStatus < S_PREPARING_FOR_EVSE)
  660. {
  661. if (_chargingData[index]->Evboard_id == 0x01)
  662. {
  663. if(regRelay.relay_event.bits.Gun1_P == YES)
  664. outputRelay.relay_event.bits.Gun1_P = NO;
  665. else if (regRelay.relay_event.bits.Gun1_N == YES)
  666. outputRelay.relay_event.bits.Gun1_N = NO;
  667. if (_chargingData[index]->Type == _Type_CCS_2)
  668. {
  669. if(regRelay.relay_event.bits.CCS_Precharge == YES)
  670. outputRelay.relay_event.bits.CCS_Precharge = NO;
  671. }
  672. }
  673. else if (_chargingData[index]->Evboard_id == 0x02)
  674. {
  675. if(regRelay.relay_event.bits.Gun2_P == YES)
  676. outputRelay.relay_event.bits.Gun2_P = NO;
  677. else if (regRelay.relay_event.bits.Gun2_N == YES)
  678. outputRelay.relay_event.bits.Gun2_N = NO;
  679. if (_chargingData[index]->Type == _Type_CCS_2)
  680. {
  681. if(regRelay.relay_event.bits.CCS_Precharge == YES)
  682. outputRelay.relay_event.bits.CCS_Precharge = NO;
  683. }
  684. }
  685. }
  686. else if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE &&
  687. _chargingData[index]->SystemStatus <= S_CHARGING))
  688. {
  689. if (_chargingData[index]->RelayWeldingCheck == YES)
  690. {
  691. if (_chargingData[index]->Evboard_id == 0x01)
  692. {
  693. if(regRelay.relay_event.bits.Gun1_N == NO)
  694. outputRelay.relay_event.bits.Gun1_N = YES;
  695. else if (regRelay.relay_event.bits.Gun1_P == NO)
  696. outputRelay.relay_event.bits.Gun1_P = YES;
  697. }
  698. else if (_chargingData[index]->Evboard_id == 0x02)
  699. {
  700. if(regRelay.relay_event.bits.Gun2_N == NO)
  701. outputRelay.relay_event.bits.Gun2_N = YES;
  702. else if (regRelay.relay_event.bits.Gun2_P == NO)
  703. outputRelay.relay_event.bits.Gun2_P = YES;
  704. }
  705. }
  706. }
  707. else if ((_chargingData[index]->SystemStatus >= S_TERMINATING && _chargingData[index]->SystemStatus <= S_COMPLETE))
  708. {
  709. if (_chargingData[index]->PresentChargingCurrent <= SEFETY_SWITCH_RELAY_CUR)
  710. {
  711. if (_chargingData[index]->Evboard_id == 0x01)
  712. {
  713. if(regRelay.relay_event.bits.Gun1_P == YES)
  714. outputRelay.relay_event.bits.Gun1_P = NO;
  715. else if (regRelay.relay_event.bits.Gun1_N == YES)
  716. outputRelay.relay_event.bits.Gun1_N = NO;
  717. }
  718. else if (_chargingData[index]->Evboard_id == 0x02)
  719. {
  720. if(regRelay.relay_event.bits.Gun2_P == YES)
  721. outputRelay.relay_event.bits.Gun2_P = NO;
  722. else if (regRelay.relay_event.bits.Gun2_N == YES)
  723. outputRelay.relay_event.bits.Gun2_N = NO;
  724. }
  725. }
  726. }
  727. else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST0)
  728. {
  729. if (_chargingData[index]->Evboard_id == 0x01)
  730. {
  731. if (_chargingData[index]->Type == _Type_CCS_2)
  732. {
  733. if (regRelay.relay_event.bits.CCS_Precharge == NO)
  734. outputRelay.relay_event.bits.CCS_Precharge = YES;
  735. else if (regRelay.relay_event.bits.CCS_Precharge == YES)
  736. outputRelay.relay_event.bits.Gun1_P = NO;
  737. }
  738. }
  739. else if (_chargingData[index]->Evboard_id == 0x02)
  740. {
  741. if (_chargingData[index]->Type == _Type_CCS_2)
  742. {
  743. if (regRelay.relay_event.bits.CCS_Precharge == NO)
  744. outputRelay.relay_event.bits.CCS_Precharge = YES;
  745. else if (regRelay.relay_event.bits.CCS_Precharge == YES)
  746. outputRelay.relay_event.bits.Gun2_P = NO;
  747. }
  748. }
  749. }
  750. else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST1)
  751. {
  752. if (_chargingData[index]->Evboard_id == 0x01)
  753. {
  754. if (_chargingData[index]->Type == _Type_CCS_2)
  755. {
  756. if (regRelay.relay_event.bits.Gun1_P == NO)
  757. outputRelay.relay_event.bits.Gun1_P = YES;
  758. else if(regRelay.relay_event.bits.Gun1_P == YES)
  759. outputRelay.relay_event.bits.CCS_Precharge = NO;
  760. }
  761. }
  762. else if (_chargingData[index]->Evboard_id == 0x02)
  763. {
  764. if (_chargingData[index]->Type == _Type_CCS_2)
  765. {
  766. if (regRelay.relay_event.bits.Gun2_P == NO)
  767. outputRelay.relay_event.bits.Gun2_P = YES;
  768. else if(regRelay.relay_event.bits.Gun2_P == YES)
  769. outputRelay.relay_event.bits.CCS_Precharge = NO;
  770. }
  771. }
  772. }
  773. }
  774. void CheckPhaseLossStatus(byte index)
  775. {
  776. if (ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP == YES ||
  777. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP == YES ||
  778. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP == YES)
  779. {
  780. _chargingData[index]->StopChargeFlag = YES;
  781. }
  782. }
  783. void SetParalleRelayStatus()
  784. {
  785. if (gunCount >= 2 && ShmSysConfigAndInfo->SysInfo.IsAlternatvieConf == NO)
  786. {
  787. if (_chargingData[0]->SystemStatus == S_BOOTING || _chargingData[1]->SystemStatus == S_BOOTING ||
  788. (_chargingData[0]->SystemStatus == S_IDLE && _chargingData[1]->SystemStatus == S_IDLE))
  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. if ((regRelay.relay_event.bits.AC_Contactor != outputRelay.relay_event.bits.AC_Contactor) ||
  1048. (regRelay.relay_event.bits.CCS_Precharge != outputRelay.relay_event.bits.CCS_Precharge) ||
  1049. (regRelay.relay_event.bits.Gun1_P != outputRelay.relay_event.bits.Gun1_P) ||
  1050. (regRelay.relay_event.bits.Gun1_N != outputRelay.relay_event.bits.Gun1_N) ||
  1051. (regRelay.relay_event.bits.Gun2_P != outputRelay.relay_event.bits.Gun2_P) ||
  1052. (regRelay.relay_event.bits.Gun2_N != outputRelay.relay_event.bits.Gun2_N) ||
  1053. (regRelay.relay_event.bits.Gun1_Parallel_P != outputRelay.relay_event.bits.Gun1_Parallel_P) ||
  1054. (regRelay.relay_event.bits.Gun1_Parallel_N != outputRelay.relay_event.bits.Gun1_Parallel_N))
  1055. {
  1056. if (regRelay.relay_event.bits.AC_Contactor != outputRelay.relay_event.bits.AC_Contactor)
  1057. PRINTF_FUNC("AC Contact Relay none match. \n");
  1058. if (regRelay.relay_event.bits.CCS_Precharge != outputRelay.relay_event.bits.CCS_Precharge)
  1059. PRINTF_FUNC("CCS Precharge Relay none match. \n");
  1060. if (regRelay.relay_event.bits.Gun1_P != outputRelay.relay_event.bits.Gun1_P)
  1061. PRINTF_FUNC("SMR1:D+ Relay none match. \n");
  1062. if (regRelay.relay_event.bits.Gun1_N != outputRelay.relay_event.bits.Gun1_N)
  1063. PRINTF_FUNC("SMR1:D- Relay none match. \n");
  1064. if (regRelay.relay_event.bits.Gun2_P != outputRelay.relay_event.bits.Gun2_P)
  1065. PRINTF_FUNC("SMR2:D+ Relay none match. \n");
  1066. if (regRelay.relay_event.bits.Gun2_N != outputRelay.relay_event.bits.Gun2_N)
  1067. PRINTF_FUNC("SMR2:D- Relay none match. \n");
  1068. if (regRelay.relay_event.bits.Gun1_Parallel_P != outputRelay.relay_event.bits.Gun1_Parallel_P)
  1069. PRINTF_FUNC("Parallel:D+ Relay none match. \n");
  1070. if (regRelay.relay_event.bits.Gun1_Parallel_N != outputRelay.relay_event.bits.Gun1_Parallel_N)
  1071. PRINTF_FUNC("Parallel:D- Relay none match. \n");
  1072. result = true;
  1073. }
  1074. return result;
  1075. }
  1076. void MatchRelayStatus()
  1077. {
  1078. // 因為 AC Contactor 沒有 Feedback,所以暫時先這樣處理
  1079. //regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
  1080. ShmSysConfigAndInfo->SysInfo.AcContactorStatus = regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
  1081. regRelay.relay_event.bits.CCS_Precharge = outputRelay.relay_event.bits.CCS_Precharge;
  1082. regRelay.relay_event.bits.Gun1_P = outputRelay.relay_event.bits.Gun1_P;
  1083. regRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_N;
  1084. regRelay.relay_event.bits.Gun2_P = outputRelay.relay_event.bits.Gun2_P;
  1085. regRelay.relay_event.bits.Gun2_N = outputRelay.relay_event.bits.Gun2_N;
  1086. regRelay.relay_event.bits.Gun1_Parallel_P = outputRelay.relay_event.bits.Gun1_Parallel_P;
  1087. regRelay.relay_event.bits.Gun1_Parallel_N = outputRelay.relay_event.bits.Gun1_Parallel_N;
  1088. }
  1089. void CheckRelayStatusByADC()
  1090. {
  1091. if (ShmRelayModuleData->Gun1FuseOutputVolt > 0 && ShmRelayModuleData->Gun1RelayOutputVolt > 0 &&
  1092. (ShmRelayModuleData->Gun1FuseOutputVolt == ShmRelayModuleData->Gun1RelayOutputVolt))
  1093. {
  1094. // Relay 前後電壓一致
  1095. _chargingData[0]->RelayK1K2Status = 0x01;
  1096. }
  1097. else
  1098. _chargingData[0]->RelayK1K2Status = 0x00;
  1099. if (ShmRelayModuleData->Gun2FuseOutputVolt > 0 && ShmRelayModuleData->Gun2RelayOutputVolt > 0 &&
  1100. (ShmRelayModuleData->Gun2FuseOutputVolt == ShmRelayModuleData->Gun2RelayOutputVolt))
  1101. {
  1102. // Relay 前後電壓一致
  1103. _chargingData[1]->RelayK1K2Status = 0x01;
  1104. }
  1105. else
  1106. _chargingData[1]->RelayK1K2Status = 0x00;
  1107. }
  1108. void SetGfdConfig(byte index, byte resister)
  1109. {
  1110. gfd_config.index = index;
  1111. gfd_config.state = resister;
  1112. //PRINTF_FUNC("************************GFD Vol = %d, GFD Res = %d \n", gfd_config.reqVol, gfd_config.resister);
  1113. if (Config_Gfd_Value(Uart5Fd, Addr.Relay, &gfd_config) == PASS)
  1114. {
  1115. // PRINTF_FUNC("Set reqVol = %f, resister = %d \n",
  1116. // gfd_config.reqVol,
  1117. // gfd_config.resister);
  1118. }
  1119. }
  1120. void CableCheckDetected(byte index)
  1121. {
  1122. // Cable Check
  1123. // 當火線上的電壓 = 車端要求的電壓電流
  1124. // _chargingData[targetGun]->EvBatterytargetVoltage
  1125. // 才可以開始偵測 1s
  1126. // Warning : Rgfd <= 150 歐/V 假設電壓為 500V 則~ Rgfd <= 75000 歐
  1127. // Pre-Warning : 150 歐/V < Rgfd <= 500 歐/V 假設電壓為 500V 則 75000 歐 < Rgfd <= 250000
  1128. // SO Normal : Rgfd > 500 歐/V 假設電壓為 500 V 則 Rgfd > 250000 歐
  1129. if (_chargingData[index]->Type >= _Type_Chademo && _chargingData[index]->Type <= _Type_GB)
  1130. {
  1131. if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE && _chargingData[index]->SystemStatus <= S_CHARGING) ||
  1132. (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1133. {
  1134. if (_chargingData[index]->SystemStatus == S_PREPARING_FOR_EVSE &&
  1135. _chargingData[index]->RelayWeldingCheck == YES)
  1136. {
  1137. SetGfdConfig(index, GFD_CABLECHK);
  1138. }
  1139. else if (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1140. _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1)
  1141. {
  1142. SetGfdConfig(index, GFD_PRECHARGE);
  1143. }
  1144. else if (_chargingData[index]->SystemStatus <= S_CHARGING)
  1145. {
  1146. if (_chargingData[index]->Type == _Type_GB)
  1147. SetGfdConfig(index, GFD_IDLE);
  1148. else
  1149. SetGfdConfig(index, GFD_CHARGING);
  1150. }
  1151. }
  1152. else if(_chargingData[index]->SystemStatus == S_COMPLETE || _chargingData[index]->SystemStatus == S_PREPARNING
  1153. || _chargingData[index]->SystemStatus == S_IDLE)
  1154. {
  1155. SetGfdConfig(index, GFD_IDLE);
  1156. }
  1157. }
  1158. }
  1159. void CheckOutputPowerOverCarReq(byte index)
  1160. {
  1161. float fireV = _chargingData[index]->FireChargingVoltage;
  1162. float carV = _chargingData[index]->EvBatterytargetVoltage;
  1163. if (_chargingData[index]->EvBatterytargetVoltage > 1500 &&
  1164. (_chargingData[index]->Type == _Type_Chademo ||
  1165. _chargingData[index]->Type == _Type_CCS_2 ||
  1166. _chargingData[index]->Type == _Type_GB))
  1167. {
  1168. if (fireV >= (carV + (carV * 0.1)))
  1169. {
  1170. PRINTF_FUNC("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f \n",
  1171. _chargingData[index]->FireChargingVoltage, _chargingData[index]->EvBatterytargetVoltage);
  1172. DEBUG_ERROR("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f \n",
  1173. _chargingData[index]->FireChargingVoltage, _chargingData[index]->EvBatterytargetVoltage);
  1174. _chargingData[index]->StopChargeFlag = YES;
  1175. }
  1176. }
  1177. }
  1178. void CheckOutputVolNoneMatchFire(byte index)
  1179. {
  1180. if (_chargingData[index]->EvBatterytargetVoltage > 1500 &&
  1181. (_chargingData[index]->Type == _Type_Chademo ||
  1182. _chargingData[index]->Type == _Type_CCS_2 ||
  1183. _chargingData[index]->Type == _Type_GB))
  1184. {
  1185. if (((_chargingData[index]->PresentChargingVoltage * 10) < _chargingData[index]->FireChargingVoltage - 300) ||
  1186. ((_chargingData[index]->PresentChargingVoltage * 10) > _chargingData[index]->FireChargingVoltage + 300))
  1187. {
  1188. if (!_isOutputNoneMatch[index])
  1189. {
  1190. _isOutputNoneMatch[index] = YES;
  1191. gettimeofday(&_checkOutputNoneMatchTimer[index], NULL);
  1192. }
  1193. else
  1194. {
  1195. if ((GetTimeoutValue(_checkOutputNoneMatchTimer[index]) / 1000) >= 5000)
  1196. {
  1197. PRINTF_FUNC("[Module_InternalComm]CheckOutputVolNoneMatchFire NG (%d) : pre = %f, fire = %f \n",
  1198. index, _chargingData[index]->PresentChargingVoltage, _chargingData[index]->FireChargingVoltage);
  1199. DEBUG_ERROR("[Module_InternalComm]CheckOutputVolNoneMatchFire NG (%d): pre = %f, fire = %f \n",
  1200. index, _chargingData[index]->PresentChargingVoltage, _chargingData[index]->FireChargingVoltage);
  1201. _chargingData[index]->StopChargeFlag = YES;
  1202. }
  1203. }
  1204. }
  1205. else
  1206. _isOutputNoneMatch[index] = NO;
  1207. }
  1208. }
  1209. void CheckRelayWeldingStatus(byte index)
  1210. {
  1211. if (!_isRelayWelding[index])
  1212. {
  1213. if (_chargingData[index]->PresentChargingVoltage >= VOUT_MIN_VOLTAGE)
  1214. {
  1215. gettimeofday(&_checkRelayWeldingTimer[index], NULL);
  1216. _isRelayWelding[index] = YES;
  1217. }
  1218. }
  1219. else
  1220. {
  1221. if ((GetTimeoutValue(_checkRelayWeldingTimer[index]) / 1000) >= 1000)
  1222. {
  1223. _chargingData[index]->RelayWeldingCheck = YES;
  1224. return;
  1225. }
  1226. if (_chargingData[index]->FireChargingVoltage >= VOUT_MIN_VOLTAGE)
  1227. {
  1228. if (_chargingData[index]->Type == _Type_Chademo)
  1229. ShmStatusCodeData->FaultCode.FaultEvents.bits.ChademoOutputRelayWelding = YES;
  1230. else if (_chargingData[index]->Type == _Type_GB)
  1231. ShmStatusCodeData->FaultCode.FaultEvents.bits.GbOutputRelayWelding = YES;
  1232. else if (_chargingData[index]->Type == _Type_CCS_2)
  1233. ShmStatusCodeData->FaultCode.FaultEvents.bits.CcsOutputRelayWelding = YES;
  1234. PRINTF_FUNC("CheckRelayWeldingStatus : fail \n");
  1235. _chargingData[index]->StopChargeFlag = YES;
  1236. }
  1237. }
  1238. }
  1239. void GetPsuTempForFanSpeed()
  1240. {
  1241. char temp = 0;
  1242. for (byte index = 0; index < ShmPsuData->GroupCount; index++)
  1243. {
  1244. for (byte count = 0; count < ShmPsuData->PsuGroup[index].GroupPresentPsuQuantity; count++)
  1245. {
  1246. if (temp < ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp)
  1247. temp = ShmPsuData->PsuGroup[index].PsuModule[count].ExletTemp;
  1248. }
  1249. }
  1250. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == NO)
  1251. {
  1252. if (ShmFanModuleData->TestFanSpeed == NORMAL_FAN_SPEED)
  1253. {
  1254. if (temp >= ENV_TEMP_MAX)
  1255. ShmFanModuleData->TestFanSpeed = MAX_FAN_SPEED;
  1256. }
  1257. else if (ShmFanModuleData->TestFanSpeed == MAX_FAN_SPEED)
  1258. {
  1259. if (temp <= ENV_TEMP_MIN)
  1260. ShmFanModuleData->TestFanSpeed = NORMAL_FAN_SPEED;
  1261. }
  1262. else
  1263. ShmFanModuleData->TestFanSpeed = NORMAL_FAN_SPEED;
  1264. }
  1265. }
  1266. int main(void)
  1267. {
  1268. if(InitShareMemory() == FAIL)
  1269. {
  1270. #ifdef SystemLogMessage
  1271. DEBUG_ERROR("InitShareMemory NG\n");
  1272. #endif
  1273. if(ShmStatusCodeData!=NULL)
  1274. {
  1275. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1276. }
  1277. sleep(5);
  1278. return 0;
  1279. }
  1280. gunCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1281. // Open Uart5 for RB
  1282. Uart5Fd = InitComPort();
  1283. Initialization();
  1284. sleep(1);
  1285. if(Uart5Fd < 0)
  1286. {
  1287. PRINTF_FUNC("(Internal) open port error. \n");
  1288. return 0;
  1289. }
  1290. outputRelay.relay_event.bits.AC_Contactor = 0x00;
  1291. outputRelay.relay_event.bits.CCS_Precharge = 0x00;
  1292. outputRelay.relay_event.bits.Gun1_Parallel_P = 0x00;
  1293. outputRelay.relay_event.bits.Gun1_Parallel_N = 0x00;
  1294. outputRelay.relay_event.bits.Gun1_P = 0x00;
  1295. outputRelay.relay_event.bits.Gun1_N = 0x00;
  1296. outputRelay.relay_event.bits.Gun2_N = 0x00;
  1297. outputRelay.relay_event.bits.Gun2_P = 0x00;
  1298. if(Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay) != PASS)
  1299. PRINTF_FUNC("Config_Relay_Output fail \n");
  1300. bool printRelayStatus = true;
  1301. for(;;)
  1302. {
  1303. bool isCharging = false;
  1304. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  1305. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1306. {
  1307. GetFwAndHwVersion_Relay();
  1308. SetRtcData_Relay();
  1309. sleep(1);
  1310. }
  1311. if (ShmFanModuleData->SelfTest_Comp == NO)
  1312. {
  1313. GetFwAndHwVersion_Fan();
  1314. SetModelName_Fan();
  1315. SetRtcData_Fan();
  1316. sleep(1);
  1317. gettimeofday(&_priority_time, NULL);
  1318. }
  1319. if (ShmRelayModuleData->SelfTest_Comp == YES)
  1320. {
  1321. // ==============優先權最高 10 ms ==============
  1322. // 輸出電壓
  1323. GetPersentOutputVol();
  1324. // 三相輸入電壓
  1325. GetPresentInputVol();
  1326. // 讀取當前 relay 狀態
  1327. //GetRelayOutputStatus();
  1328. for (int i = 0; i < gunCount; i++)
  1329. {
  1330. // Cable check (Set)
  1331. CableCheckDetected(i);
  1332. // check k1 k2 relay 狀態
  1333. CheckK1K2RelayOutput(i);
  1334. // 依據當前各槍的狀態選擇 搭上/放開 Relay
  1335. SetK1K2RelayStatus(i);
  1336. if (ShmSysConfigAndInfo->SysConfig.PhaseLossPolicy == YES)
  1337. CheckPhaseLossStatus(i);
  1338. if (_chargingData[i]->SystemStatus == S_IDLE)
  1339. {
  1340. _chargingData[i]->RelayWeldingCheck = NO;
  1341. _isRelayWelding[i] = NO;
  1342. }
  1343. if (_chargingData[i]->SystemStatus == S_BOOTING ||
  1344. (_chargingData[i]->SystemStatus >= S_PREPARNING && _chargingData[i]->SystemStatus <= S_COMPLETE) ||
  1345. (_chargingData[i]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[i]->SystemStatus <= S_CCS_PRECHARGE_ST1) ||
  1346. (ShmSysConfigAndInfo->SysInfo.PageIndex >= _LCM_AUTHORIZING && ShmSysConfigAndInfo->SysInfo.PageIndex <= _LCM_WAIT_FOR_PLUG))
  1347. {
  1348. _chargingData[i]->IsReadyToCharging = YES;
  1349. isCharging = true;
  1350. if (_chargingData[i]->SystemStatus >= S_PREPARING_FOR_EVSE &&
  1351. _chargingData[i]->RelayWeldingCheck == NO)
  1352. CheckRelayWeldingStatus(i);
  1353. if (_chargingData[i]->SystemStatus == S_CHARGING)
  1354. {
  1355. CheckOutputPowerOverCarReq(i);
  1356. CheckOutputVolNoneMatchFire(i);
  1357. }
  1358. else
  1359. _isOutputNoneMatch[i] = NO;
  1360. }
  1361. else
  1362. _chargingData[i]->IsReadyToCharging = NO;
  1363. }
  1364. // Cable check (Get)
  1365. GetGfdAdc();
  1366. // 橋接 relay
  1367. SetParalleRelayStatus();
  1368. // 搭上 AC Contactor
  1369. if (isCharging)
  1370. outputRelay.relay_event.bits.AC_Contactor = YES;
  1371. else
  1372. outputRelay.relay_event.bits.AC_Contactor = NO;
  1373. if (isCharging)
  1374. {
  1375. isStopChargingCount = false;
  1376. outputRelay.relay_event.bits.AC_Contactor = YES;
  1377. }
  1378. else
  1379. {
  1380. if (!isStopChargingCount)
  1381. {
  1382. gettimeofday(&_close_ac_contactor, NULL);
  1383. isStopChargingCount = true;
  1384. }
  1385. else
  1386. {
  1387. if (!isSystemBooting ||
  1388. (outputRelay.relay_event.bits.AC_Contactor == YES && GetTimeoutValue(_close_ac_contactor) / 1000 >= (TEN_MINUTES * 1000)))
  1389. outputRelay.relay_event.bits.AC_Contactor = NO;
  1390. }
  1391. }
  1392. // 搭上/鬆開 Relay
  1393. if(IsNoneMatchRelayStatus())
  1394. {
  1395. if (Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay))
  1396. {
  1397. regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  1398. regRelay.relay_event.bits.CCS_Precharge = outputRelay.relay_event.bits.CCS_Precharge;
  1399. regRelay.relay_event.bits.Gun1_P = outputRelay.relay_event.bits.Gun1_P;
  1400. regRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_N;
  1401. regRelay.relay_event.bits.Gun2_P = outputRelay.relay_event.bits.Gun2_P;
  1402. regRelay.relay_event.bits.Gun2_N = outputRelay.relay_event.bits.Gun2_N;
  1403. regRelay.relay_event.bits.Gun1_Parallel_P = outputRelay.relay_event.bits.Gun1_Parallel_P;
  1404. regRelay.relay_event.bits.Gun1_Parallel_N = outputRelay.relay_event.bits.Gun1_Parallel_N;
  1405. 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",
  1406. regRelay.relay_event.bits.AC_Contactor,
  1407. regRelay.relay_event.bits.Gun1_P,
  1408. regRelay.relay_event.bits.Gun1_N,
  1409. regRelay.relay_event.bits.Gun2_P,
  1410. regRelay.relay_event.bits.Gun2_N,
  1411. regRelay.relay_event.bits.CCS_Precharge,
  1412. regRelay.relay_event.bits.Gun1_Parallel_P,
  1413. regRelay.relay_event.bits.Gun1_Parallel_N);
  1414. }
  1415. printRelayStatus = true;
  1416. }
  1417. }
  1418. if (ShmFanModuleData->SelfTest_Comp == YES)
  1419. {
  1420. if (GetTimeoutValue(_priority_time) / 1000 >= 1000)
  1421. {
  1422. GetPsuTempForFanSpeed();
  1423. GetFanSpeed();
  1424. gettimeofday(&_priority_time, NULL);
  1425. if (isCharging)
  1426. {
  1427. if (ShmFanModuleData->PresentFan1Speed < MAX_FAN_SPEED ||
  1428. ShmFanModuleData->PresentFan2Speed < MAX_FAN_SPEED ||
  1429. ShmFanModuleData->PresentFan3Speed < MAX_FAN_SPEED ||
  1430. ShmFanModuleData->PresentFan4Speed < MAX_FAN_SPEED)
  1431. {
  1432. ShmFanModuleData->SetFan1Speed = MAX_FAN_SPEED;
  1433. ShmFanModuleData->SetFan2Speed = MAX_FAN_SPEED;
  1434. ShmFanModuleData->SetFan3Speed = MAX_FAN_SPEED;
  1435. ShmFanModuleData->SetFan4Speed = MAX_FAN_SPEED;
  1436. }
  1437. if (ShmFanModuleData->TestFanSpeed > 0)
  1438. {
  1439. ShmFanModuleData->SetFan1Speed = ShmFanModuleData->TestFanSpeed;
  1440. ShmFanModuleData->SetFan2Speed = ShmFanModuleData->TestFanSpeed;
  1441. ShmFanModuleData->SetFan3Speed = ShmFanModuleData->TestFanSpeed;
  1442. ShmFanModuleData->SetFan4Speed = ShmFanModuleData->TestFanSpeed;
  1443. }
  1444. }
  1445. else
  1446. {
  1447. if (ShmFanModuleData->PresentFan1Speed > MIN_FAN_SPEED ||
  1448. ShmFanModuleData->PresentFan2Speed > MIN_FAN_SPEED ||
  1449. ShmFanModuleData->PresentFan3Speed > MIN_FAN_SPEED ||
  1450. ShmFanModuleData->PresentFan4Speed > MIN_FAN_SPEED)
  1451. {
  1452. ShmFanModuleData->SetFan1Speed = MIN_FAN_SPEED;
  1453. ShmFanModuleData->SetFan2Speed = MIN_FAN_SPEED;
  1454. ShmFanModuleData->SetFan3Speed = MIN_FAN_SPEED;
  1455. ShmFanModuleData->SetFan4Speed = MIN_FAN_SPEED;
  1456. }
  1457. // 停止時,如溫度還是很高,則需要維持該轉速直到溫度降低
  1458. if (ShmFanModuleData->TestFanSpeed >= MAX_FAN_SPEED)
  1459. {
  1460. ShmFanModuleData->SetFan1Speed = ShmFanModuleData->TestFanSpeed;
  1461. ShmFanModuleData->SetFan2Speed = ShmFanModuleData->TestFanSpeed;
  1462. ShmFanModuleData->SetFan3Speed = ShmFanModuleData->TestFanSpeed;
  1463. ShmFanModuleData->SetFan4Speed = ShmFanModuleData->TestFanSpeed;
  1464. }
  1465. }
  1466. //PRINTF_FUNC("set fan = %d \n", ShmFanModuleData->SetFan1Speed);
  1467. SetFanModuleSpeed();
  1468. }
  1469. }
  1470. usleep(10000);
  1471. }
  1472. return FAIL;
  1473. }