Module_InternalComm.c 80 KB

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