Module_InternalComm.c 95 KB

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