Module_InternalComm.c 80 KB

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