Module_InternalComm.c 96 KB

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