Module_InternalComm.c 101 KB

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