Module_InternalComm.c 91 KB

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