Module_InternalComm.c 95 KB

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