Module_InternalComm.c 99 KB

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