Module_InternalComm.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/wireless.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h> /*標準輸入輸出定義*/
  18. #include <stdlib.h> /*標準函數庫定義*/
  19. #include <unistd.h> /*Unix 標準函數定義*/
  20. #include <fcntl.h> /*檔控制定義*/
  21. #include <termios.h> /*PPSIX 終端控制定義*/
  22. #include <errno.h> /*錯誤號定義*/
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include "../../define.h"
  30. #include "internalComm.h"
  31. #include <stdbool.h>
  32. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  33. #define PASS 1
  34. #define FAIL -1
  35. #define YES 1
  36. #define NO 0
  37. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  38. struct StatusCodeData *ShmStatusCodeData;
  39. struct FanModuleData *ShmFanModuleData;
  40. struct RelayModuleData *ShmRelayModuleData;
  41. struct CHAdeMOData *ShmCHAdeMOData;
  42. struct CcsData *ShmCcsData;
  43. #define VIN_MAX_VOLTAGE 250 // 大於該值 : OVP
  44. #define VIN_MIN_VOLTAGE 170 // 小於該值 : UVP
  45. #define VIN_DROP_VOLTAGE 150 // 小於該值 : ac drop
  46. #define VOUT_MAX_VOLTAGE 750
  47. #define VOUT_MIN_VOLTAGE 150
  48. #define IOUT_MAX_CURRENT 50
  49. #define MAX_FAN_SPEED 13500
  50. #define MIN_FAN_SPEED 2800
  51. // GFD Status
  52. #define GFD_IDLE 0
  53. #define GFD_CABLECHK 1
  54. #define GFD_PRECHARGE 2
  55. #define GFD_CHARGING 3
  56. // 最小切換 Relay 電壓
  57. #define SELF_TO_CHANGE_RELAY_STATUS 600
  58. // 透過電壓確認 Relay 是否搭上的依據電壓
  59. #define CHECK_RELAY_STATUS 300
  60. #define CHECK_RELAY_STATUS_GAP 100
  61. // 安全在停止充電程序中斷開 Relay 的電流
  62. #define SEFETY_SWITCH_RELAY_CUR 20
  63. // 確認 Relay Welding 電壓
  64. #define RELAY_WELDING_DET 300
  65. byte gunCount = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
  66. // 槍資訊
  67. struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  68. byte gfdChkFailCount[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  69. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData);
  70. int Uart5Fd;
  71. char *relayRs485PortName = "/dev/ttyS5";
  72. unsigned short fanSpeedSmoothValue = 1000;
  73. struct timeval _priority_time;
  74. Ver ver;
  75. PresentInputVoltage inputVoltage;
  76. PresentOutputVoltage outputVoltage;
  77. FanSpeed fanSpeed;
  78. Temperature temperature;
  79. AuxPower auxPower;
  80. Gfd gfd_adc;
  81. Gfd_config gfd_config;
  82. Gpio_in gpio_in;
  83. Gpio_out gpio_out;
  84. Relay outputRelay;
  85. Relay regRelay;
  86. Rtc rtc;
  87. void PRINTF_FUNC(char *string, ...);
  88. int StoreLogMsg(const char *fmt, ...);
  89. unsigned long GetTimeoutValue(struct timeval _sour_time);
  90. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  91. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  92. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  93. unsigned long GetTimeoutValue(struct timeval _sour_time)
  94. {
  95. struct timeval _end_time;
  96. gettimeofday(&_end_time, NULL);
  97. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  98. }
  99. int StoreLogMsg(const char *fmt, ...)
  100. {
  101. char Buf[4096+256];
  102. char buffer[4096];
  103. time_t CurrentTime;
  104. struct tm *tm;
  105. va_list args;
  106. va_start(args, fmt);
  107. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  108. va_end(args);
  109. memset(Buf,0,sizeof(Buf));
  110. CurrentTime = time(NULL);
  111. tm=localtime(&CurrentTime);
  112. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  113. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  114. buffer,
  115. tm->tm_year+1900,tm->tm_mon+1);
  116. system(Buf);
  117. return rc;
  118. }
  119. int DiffTimeb(struct timeb ST, struct timeb ET)
  120. {
  121. //return milli-second
  122. unsigned int StartTime,StopTime;
  123. StartTime=(unsigned int)ST.time;
  124. StopTime=(unsigned int)ET.time;
  125. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  126. }
  127. unsigned short MaxValue(unsigned short value1, unsigned short value2)
  128. {
  129. return value1 >= value2 ? value1 : value2;
  130. }
  131. void PRINTF_FUNC(char *string, ...)
  132. {
  133. if (DEBUG)
  134. {
  135. va_list args;
  136. char buffer[4096];
  137. va_start(args, string);
  138. vsnprintf(buffer, sizeof(buffer), string, args);
  139. va_end(args);
  140. printf("%s \n", buffer);
  141. }
  142. }
  143. //==========================================
  144. // Communication Function
  145. //==========================================
  146. void GetFwAndHwVersion_Fan()
  147. {
  148. if(Query_FW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
  149. {
  150. // FanModuleData
  151. strcpy((char *) ShmFanModuleData->version, ver.Version_FW);
  152. // SystemInfo
  153. strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, ver.Version_FW);
  154. //PRINTF_FUNC("GetFwAndHwVersion_Fan s1 = %s \n", ver.Version_FW);
  155. }
  156. if (Query_HW_Ver(Uart5Fd, Addr.Fan, &ver) == PASS)
  157. {
  158. // SystemInfo
  159. strcpy((char *) ShmSysConfigAndInfo->SysInfo.FanModuleHwRev, ver.Version_FW);
  160. //PRINTF_FUNC("GetFwAndHwVersion_Fan s2 = %s \n", ver.Version_HW);
  161. }
  162. }
  163. void GetFwAndHwVersion_Relay()
  164. {
  165. if (Query_FW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
  166. {
  167. // FanModuleData
  168. strcpy((char *) ShmRelayModuleData->version, ver.Version_FW);
  169. // SystemInfo
  170. strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, ver.Version_FW);
  171. //PRINTF_FUNC("GetFwAndHwVersion_Relay s1 = %s \n", ver.Version_FW);
  172. }
  173. if (Query_HW_Ver(Uart5Fd, Addr.Relay, &ver) == PASS)
  174. {
  175. // SystemInfo
  176. strcpy((char *) ShmSysConfigAndInfo->SysInfo.RelayModuleHwRev, ver.Version_FW);
  177. //PRINTF_FUNC("GetFwAndHwVersion_Relay s2 = %s \n", ver.Version_HW);
  178. }
  179. }
  180. void SetRtcData_Relay()
  181. {
  182. struct timeb csuTime;
  183. struct tm *tmCSU;
  184. ftime(&csuTime);
  185. tmCSU = localtime(&csuTime.time);
  186. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  187. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  188. // tmCSU->tm_sec);
  189. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  190. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  191. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  192. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  193. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  194. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  195. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  196. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  197. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  198. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  199. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  200. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  201. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  202. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  203. if (Config_Rtc_Data(Uart5Fd, Addr.Relay, &rtc) == PASS)
  204. PRINTF_FUNC("SetRtc (RB) sucessfully. \n");
  205. else
  206. PRINTF_FUNC("SetRtc (RB) fail. \n");
  207. }
  208. void SetRtcData_Fan()
  209. {
  210. struct timeb csuTime;
  211. struct tm *tmCSU;
  212. ftime(&csuTime);
  213. tmCSU = localtime(&csuTime.time);
  214. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  215. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  216. // tmCSU->tm_sec);
  217. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  218. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  219. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  220. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  221. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  222. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  223. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  224. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  225. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  226. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  227. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  228. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  229. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  230. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  231. if (Config_Rtc_Data(Uart5Fd, Addr.Fan, &rtc) == PASS)
  232. PRINTF_FUNC("SetRtc (FB) sucessfully. \n");
  233. else
  234. PRINTF_FUNC("SetRtc (FB) fail. \n");
  235. }
  236. void SetModelName_Fan()
  237. {
  238. if (Config_Model_Name(Uart5Fd, Addr.Fan, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
  239. {
  240. PRINTF_FUNC("Set Model name PASS = %s \n", ShmSysConfigAndInfo->SysConfig.ModelName);
  241. }
  242. }
  243. // AC 三相輸入電壓
  244. void GetPresentInputVol()
  245. {
  246. if (Query_Present_InputVoltage(Uart5Fd, Addr.Relay, &inputVoltage) == PASS)
  247. {
  248. // resolution : 0.1
  249. //PRINTF_FUNC("InputVoltageR = %f \n", inputVoltage.L1N_L12);
  250. //PRINTF_FUNC("InputVoltageS = %f \n", inputVoltage.L2N_L23);
  251. //PRINTF_FUNC("InputVoltageT = %f \n", inputVoltage.L3N_L31);
  252. ShmRelayModuleData->InputL1Volt = inputVoltage.L1N_L12;
  253. ShmRelayModuleData->InputL2Volt = inputVoltage.L2N_L23;
  254. ShmRelayModuleData->InputL3Volt = inputVoltage.L3N_L31;
  255. //********************************************************************************************************//
  256. // VIN < 170
  257. if (inputVoltage.L1N_L12 < VIN_MIN_VOLTAGE)
  258. {
  259. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = 0x01;
  260. }
  261. if (inputVoltage.L2N_L23 < VIN_MIN_VOLTAGE)
  262. {
  263. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = 0x01;
  264. }
  265. if (inputVoltage.L3N_L31 < VIN_MIN_VOLTAGE)
  266. {
  267. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = 0x01;
  268. }
  269. //********************************************************************************************************//
  270. // VIN > 250
  271. if (inputVoltage.L1N_L12 > VIN_MAX_VOLTAGE)
  272. {
  273. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = 0x01;
  274. }
  275. if (inputVoltage.L2N_L23 > VIN_MAX_VOLTAGE)
  276. {
  277. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = 0x01;
  278. }
  279. if (inputVoltage.L3N_L31 > VIN_MAX_VOLTAGE)
  280. {
  281. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = 0x01;
  282. }
  283. //********************************************************************************************************//
  284. // VIN < 150
  285. if (inputVoltage.L1N_L12 < VIN_DROP_VOLTAGE)
  286. {
  287. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = 0x01;
  288. }
  289. if (inputVoltage.L2N_L23 < VIN_DROP_VOLTAGE)
  290. {
  291. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputDrop = 0x01;
  292. }
  293. if (inputVoltage.L3N_L31 < VIN_DROP_VOLTAGE)
  294. {
  295. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputDrop = 0x01;
  296. }
  297. //********************************************************************************************************//
  298. // 150 <= VIN < 160
  299. // if (inputVoltage.L1N_L12 >= VIN_MIN_VOLTAGE && inputVoltage.L1N_L12 <= VIN_LOW_VOLTAGE)
  300. // {
  301. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputUVP = 0x00;
  302. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputOVP = 0x00;
  303. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL1InputDrop = 0x00;
  304. // }
  305. // if (inputVoltage.L2N_L23 >= VIN_MIN_VOLTAGE && inputVoltage.L2N_L23 <= VIN_LOW_VOLTAGE)
  306. // {
  307. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputUVP = 0x00;
  308. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputOVP = 0x00;
  309. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL2InputDrop = 0x00;
  310. // }
  311. // if (inputVoltage.L3N_L31 >= VIN_MIN_VOLTAGE && inputVoltage.L3N_L31 <= VIN_LOW_VOLTAGE)
  312. // {
  313. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputUVP = 0x00;
  314. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputOVP = 0x00;
  315. // ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemL3InputDrop = 0x00;
  316. // }
  317. }
  318. }
  319. // 左右槍的 Relay 前後的輸出電壓
  320. void GetPersentOutputVol()
  321. {
  322. if (Query_Present_OutputVoltage(Uart5Fd, Addr.Relay, &outputVoltage) == PASS)
  323. {
  324. // PRINTF_FUNC("Conn1 fuse 1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
  325. // PRINTF_FUNC("Conn1 relay 1 = %f \n", outputVoltage.behindRelay_Voltage_C1);
  326. // PRINTF_FUNC("Conn2 fuse 2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
  327. // PRINTF_FUNC("Conn2 relay 2 = %f \n", outputVoltage.behindRelay_Voltage_C2);
  328. //PRINTF_FUNC("outputVoltage.behindFuse_Voltage_C1 = %f \n", outputVoltage.behindFuse_Voltage_C1);
  329. //PRINTF_FUNC("outputVoltage.behindFuse_Voltage_C2 = %f \n", outputVoltage.behindFuse_Voltage_C2);
  330. ShmRelayModuleData->Gun1FuseOutputVolt = outputVoltage.behindFuse_Voltage_C1;
  331. ShmRelayModuleData->Gun1RelayOutputVolt = outputVoltage.behindRelay_Voltage_C1;
  332. ShmRelayModuleData->Gun2FuseOutputVolt = outputVoltage.behindFuse_Voltage_C2;
  333. ShmRelayModuleData->Gun2RelayOutputVolt = outputVoltage.behindRelay_Voltage_C2;
  334. for (int index = 0; index < gunCount; index++)
  335. {
  336. if (index == 0)
  337. {
  338. if (_chargingData[index]->Evboard_id == 0x01)
  339. {
  340. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun1FuseOutputVolt;
  341. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun1RelayOutputVolt;
  342. }
  343. else if (_chargingData[index]->Evboard_id == 0x02)
  344. {
  345. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
  346. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
  347. }
  348. }
  349. else if (index == 1)
  350. {
  351. _chargingData[index]->FuseChargingVoltage = ShmRelayModuleData->Gun2FuseOutputVolt;
  352. _chargingData[index]->FireChargingVoltage = ShmRelayModuleData->Gun2RelayOutputVolt;
  353. }
  354. unsigned short Ovp = 0;
  355. unsigned short Ocp = 0;
  356. //Ovp = MIN [VOUT_MAX_VOLTAGE, EV_BATTERY_VOLTAGE] // 最大輸出電壓與電池電壓最大值
  357. //Ocp = MIN [IOUT_MAX_CURRENT, EV_CURRENT_REQ] // 最大輸出電流與需求電流最小值
  358. if (_chargingData[index]->Type == _Type_Chademo)
  359. {
  360. Ovp = MaxValue(_chargingData[index]->MaximumChargingVoltage, _chargingData[index]->EvBatteryMaxVoltage);
  361. Ocp = MaxValue(_chargingData[index]->PresentChargingCurrent, ShmCHAdeMOData->ev[_chargingData[index]->type_index].ChargingCurrentRequest);
  362. if (_chargingData[index]->PresentChargingVoltage >= Ovp)
  363. {
  364. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOVP = 0x01;
  365. }
  366. if (_chargingData[index]->PresentChargingCurrent >= Ocp)
  367. {
  368. //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemChademoOutputOCP = 0x01;
  369. }
  370. }
  371. else if (_chargingData[index]->Type == _Type_CCS_2)
  372. {
  373. }
  374. }
  375. }
  376. }
  377. // 風扇速度
  378. void GetFanSpeed()
  379. {
  380. //PRINTF_FUNC("Get fan board speed \n");
  381. if (Query_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed) == PASS)
  382. {
  383. ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0];
  384. ShmFanModuleData->PresentFan2Speed = fanSpeed.speed[1];
  385. ShmFanModuleData->PresentFan3Speed = fanSpeed.speed[2];
  386. ShmFanModuleData->PresentFan4Speed = fanSpeed.speed[3];
  387. // PRINTF_FUNC("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
  388. // PRINTF_FUNC("SystemFanRotaSpeed_2 = %d \n", fanSpeed.speed[1]);
  389. // PRINTF_FUNC("SystemFanRotaSpeed_3 = %d \n", fanSpeed.speed[2]);
  390. // PRINTF_FUNC("SystemFanRotaSpeed_4 = %d \n", fanSpeed.speed[3]);
  391. // Config_Fan_Speed(Uart5Fd, Addr.Fan, &fanSpeed[0]);
  392. //SysInfoData (SystemFanRotaSpeed)
  393. }
  394. }
  395. // 讀取 Relay 狀態
  396. void GetRelayOutputStatus()
  397. {
  398. if (Query_Relay_Output(Uart5Fd, Addr.Relay, &regRelay) == PASS)
  399. {
  400. regRelay.relay_event.bits.AC_Contactor = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  401. }
  402. }
  403. // 確認 K1 K2 relay 的狀態
  404. void CheckK1K2RelayOutput(byte index)
  405. {
  406. if (index == 0)
  407. {
  408. if (_chargingData[index]->Evboard_id == 0x01)
  409. {
  410. if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.Gun1_P == YES)
  411. _chargingData[index]->RelayK1K2Status = YES;
  412. else
  413. _chargingData[index]->RelayK1K2Status = NO;
  414. if(_chargingData[index]->Type == _Type_CCS_2)
  415. {
  416. if (regRelay.relay_event.bits.Gun1_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  417. _chargingData[index]->RelayKPK2Status = YES;
  418. else
  419. _chargingData[index]->RelayKPK2Status = NO;
  420. }
  421. }
  422. else if (_chargingData[index]->Evboard_id == 0x02)
  423. {
  424. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
  425. _chargingData[index]->RelayK1K2Status = YES;
  426. else
  427. _chargingData[index]->RelayK1K2Status = NO;
  428. if(_chargingData[index]->Type == _Type_CCS_2)
  429. {
  430. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  431. _chargingData[index]->RelayKPK2Status = YES;
  432. else
  433. _chargingData[index]->RelayKPK2Status = NO;
  434. }
  435. }
  436. }
  437. else if (index == 1)
  438. {
  439. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.Gun2_P == YES)
  440. _chargingData[index]->RelayK1K2Status = YES;
  441. else
  442. _chargingData[index]->RelayK1K2Status = NO;
  443. if(_chargingData[index]->Type == _Type_CCS_2)
  444. {
  445. if (regRelay.relay_event.bits.Gun2_N == YES && regRelay.relay_event.bits.CCS_Precharge == YES)
  446. _chargingData[index]->RelayKPK2Status = YES;
  447. else
  448. _chargingData[index]->RelayKPK2Status = NO;
  449. }
  450. }
  451. if (regRelay.relay_event.bits.Gun1_Parallel_N == YES && regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  452. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = YES;
  453. else
  454. ShmSysConfigAndInfo->SysInfo.BridgeRelayStatus = NO;
  455. }
  456. void GetGfdAdc()
  457. {
  458. // define : 每 0.2 ~ 1 秒一次
  459. // occur : <= 75k 歐姆 @ 150 - 750 Vdc
  460. // warning : >= 100 歐姆 && <= 500 歐姆 @ 150-750 Vdc
  461. if (Query_Gfd_Adc(Uart5Fd, Addr.Relay, &gfd_adc) == PASS)
  462. {
  463. for (int i = 0; i < gunCount; i++)
  464. {
  465. if (i == 0)
  466. {
  467. _chargingData[i]->GroundFaultStatus = gfd_adc.result_conn1;
  468. if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
  469. {
  470. DEBUG_ERROR("GFD Fail. index = %d, R = %d, Vol = %d \n",
  471. i, gfd_adc.Resister_conn1, gfd_adc.voltage_conn1);
  472. }
  473. }
  474. else if (i == 1)
  475. {
  476. _chargingData[i]->GroundFaultStatus = gfd_adc.result_conn2;
  477. if (_chargingData[i]->GroundFaultStatus == GFD_FAIL)
  478. {
  479. DEBUG_ERROR("GFD Fail. index = %d, R = %d, Vol = %d \n",
  480. i, gfd_adc.Resister_conn2, gfd_adc.voltage_conn2);
  481. }
  482. }
  483. }
  484. //if (gfd_adc.result_conn1 != 0)
  485. {
  486. // PRINTF_FUNC("******************Resister_conn1 = %d, voltage_conn1 = %d, result_conn1 = %d, step = %d \n",
  487. // gfd_adc.Resister_conn1,
  488. // gfd_adc.voltage_conn1,
  489. // gfd_adc.result_conn1,
  490. // gfd_adc.rb_step_1);
  491. }
  492. }
  493. }
  494. void GetGpioInput()
  495. {
  496. if (Query_Gpio_Input(Uart5Fd, Addr.Aux, &gpio_in) == PASS)
  497. {
  498. // AC Contactor Status
  499. if (gpio_in.AC_MainBreaker == 1)
  500. {
  501. // AC Main Breaker ON
  502. PRINTF_FUNC("RB AC Main Breaker. \n");
  503. }
  504. if (gpio_in.SPD == 1)
  505. {
  506. // SPD (雷擊保護) ON
  507. PRINTF_FUNC("RB SPD. \n");
  508. }
  509. if (gpio_in.Door_Open == 1)
  510. {
  511. // Door Open
  512. PRINTF_FUNC("RB Door Open. \n");
  513. }
  514. if (gpio_in.GFD[0] == 1)
  515. {
  516. // GFD_1 Trigger
  517. }
  518. if (gpio_in.GFD[1] == 1)
  519. {
  520. // GFD_2 Trigger
  521. }
  522. if (gpio_in.AC_Drop == 1)
  523. {
  524. // AC Drop
  525. PRINTF_FUNC("RB AC Drop. \n");
  526. }
  527. if (gpio_in.Emergency_IO == 1)
  528. {
  529. // Emergency IO ON
  530. PRINTF_FUNC("RB Emergency IO ON. \n");
  531. }
  532. if (gpio_in.Button_Emergency_Press == 1)
  533. {
  534. // Emergency button Press
  535. }
  536. if (gpio_in.Button_On_Press == 1)
  537. {
  538. // On button Press
  539. }
  540. if (gpio_in.Button_Off_Press == 1)
  541. {
  542. // Off button Press
  543. }
  544. if (gpio_in.Key_1_Press == 1)
  545. {
  546. // key 1 press
  547. }
  548. if (gpio_in.Key_2_Press == 1)
  549. {
  550. // key 2 press
  551. }
  552. if (gpio_in.Key_3_Press == 1)
  553. {
  554. // key 3 press
  555. }
  556. if (gpio_in.Key_4_Press == 1)
  557. {
  558. // key 4 press
  559. }
  560. }
  561. }
  562. // 5V 12V 24V 48V
  563. void GetAuxPower()
  564. {
  565. if (Query_Aux_PowerVoltage(Uart5Fd, Addr.Fan, &auxPower) == PASS)
  566. {
  567. ShmSysConfigAndInfo->SysInfo.AuxPower48V = auxPower.voltage[0];
  568. ShmSysConfigAndInfo->SysInfo.AuxPower24V = auxPower.voltage[1];
  569. //ShmSysConfigAndInfo->SysInfo.AuxPower12V = auxPower.voltage[4];
  570. //ShmSysConfigAndInfo->SysInfo.AuxPower5V = auxPower.voltage[6];
  571. // aux power voltage
  572. //PRINTF_FUNC("aux1 = %x, \n", auxPower.voltage[0]);
  573. //PRINTF_FUNC("aux2 = %x, \n", auxPower.voltage[1]);
  574. }
  575. }
  576. void SetFanModuleSpeed()
  577. {
  578. // 調整風扇速度要漸進式 : 500 rpm/p
  579. if (ShmFanModuleData->PresentFan1Speed != ShmFanModuleData->SetFan1Speed ||
  580. ShmFanModuleData->PresentFan2Speed != ShmFanModuleData->SetFan2Speed ||
  581. ShmFanModuleData->PresentFan3Speed != ShmFanModuleData->SetFan3Speed ||
  582. ShmFanModuleData->PresentFan4Speed != ShmFanModuleData->SetFan4Speed)
  583. {
  584. FanSpeed _fanSpeed;
  585. unsigned short speed = ShmFanModuleData->PresentFan1Speed + fanSpeedSmoothValue;
  586. if (speed >= ShmFanModuleData->SetFan1Speed)
  587. speed = ShmFanModuleData->SetFan1Speed;
  588. _fanSpeed.speed[0] = speed;
  589. speed = ShmFanModuleData->PresentFan2Speed + fanSpeedSmoothValue;
  590. if (speed >= ShmFanModuleData->SetFan2Speed)
  591. speed = ShmFanModuleData->SetFan2Speed;
  592. _fanSpeed.speed[1] = speed;
  593. speed = ShmFanModuleData->PresentFan3Speed + fanSpeedSmoothValue;
  594. if (speed >= ShmFanModuleData->SetFan3Speed)
  595. speed = ShmFanModuleData->SetFan3Speed;
  596. _fanSpeed.speed[2] = speed;
  597. speed = ShmFanModuleData->PresentFan4Speed + fanSpeedSmoothValue;
  598. if (speed >= ShmFanModuleData->SetFan4Speed)
  599. speed = ShmFanModuleData->SetFan4Speed;
  600. _fanSpeed.speed[3] = speed;
  601. if (Config_Fan_Speed(Uart5Fd, Addr.Fan, &_fanSpeed) == PASS)
  602. {
  603. //PRINTF_FUNC("successfully Fan\n");
  604. }
  605. }
  606. }
  607. void SetRelayModuleFanSpeed()
  608. {
  609. // 調整風扇速度要漸進式 : 100 rpm/p
  610. if (ShmFanModuleData->PresentFan1Speed != ShmFanModuleData->SetFan1Speed)
  611. {
  612. FanSpeed _fanSpeed;
  613. unsigned short speed = 0;
  614. if (ShmFanModuleData->SetFan1Speed > ShmFanModuleData->PresentFan1Speed)
  615. {
  616. speed = ShmFanModuleData->PresentFan1Speed + fanSpeedSmoothValue;
  617. if (speed >= ShmFanModuleData->SetFan1Speed)
  618. speed = ShmFanModuleData->SetFan1Speed;
  619. }
  620. else
  621. {
  622. speed = ShmFanModuleData->PresentFan1Speed - fanSpeedSmoothValue;
  623. if (speed <= 0)
  624. speed = ShmFanModuleData->SetFan1Speed;
  625. }
  626. _fanSpeed.speed[0] = speed & 0xff;
  627. _fanSpeed.speed[1] = (speed >> 8) & 0xff;
  628. ShmFanModuleData->PresentFan1Speed = speed;
  629. Config_Fan_Speed(Uart5Fd, Addr.Relay, &_fanSpeed);
  630. }
  631. }
  632. void GetRelayModuleFanSpeed()
  633. {
  634. PRINTF_FUNC("Get fan board speed \n");
  635. if (Query_Fan_Speed(Uart5Fd, Addr.Relay, &fanSpeed) == PASS)
  636. {
  637. ShmFanModuleData->PresentFan1Speed = fanSpeed.speed[0] + (fanSpeed.speed[1] >> 8);
  638. PRINTF_FUNC("SystemFanRotaSpeed_1 = %d \n", fanSpeed.speed[0]);
  639. }
  640. }
  641. //==========================================
  642. // Common Function
  643. //==========================================
  644. void SetK1K2RelayStatus(byte index)
  645. {
  646. if (_chargingData[index]->SystemStatus < S_PREPARING_FOR_EVSE)
  647. {
  648. if (_chargingData[index]->Evboard_id == 0x01)
  649. {
  650. if(regRelay.relay_event.bits.Gun1_P == YES)
  651. outputRelay.relay_event.bits.Gun1_P = NO;
  652. else if (regRelay.relay_event.bits.Gun1_N == YES)
  653. outputRelay.relay_event.bits.Gun1_N = NO;
  654. if (_chargingData[index]->Type == _Type_CCS_2)
  655. {
  656. if(regRelay.relay_event.bits.CCS_Precharge == YES)
  657. outputRelay.relay_event.bits.CCS_Precharge = NO;
  658. }
  659. }
  660. else if (_chargingData[index]->Evboard_id == 0x02)
  661. {
  662. if(regRelay.relay_event.bits.Gun2_P == YES)
  663. outputRelay.relay_event.bits.Gun2_P = NO;
  664. else if (regRelay.relay_event.bits.Gun2_N == YES)
  665. outputRelay.relay_event.bits.Gun2_N = NO;
  666. if (_chargingData[index]->Type == _Type_CCS_2)
  667. {
  668. if(regRelay.relay_event.bits.CCS_Precharge == YES)
  669. outputRelay.relay_event.bits.CCS_Precharge = NO;
  670. }
  671. }
  672. }
  673. else if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE && _chargingData[index]->SystemStatus <= S_CHARGING))
  674. {
  675. if (_chargingData[index]->Evboard_id == 0x01)
  676. {
  677. if(regRelay.relay_event.bits.Gun1_N == NO)
  678. outputRelay.relay_event.bits.Gun1_N = YES;
  679. else if (regRelay.relay_event.bits.Gun1_P == NO)
  680. outputRelay.relay_event.bits.Gun1_P = YES;
  681. }
  682. else if (_chargingData[index]->Evboard_id == 0x02)
  683. {
  684. if(regRelay.relay_event.bits.Gun2_N == NO)
  685. outputRelay.relay_event.bits.Gun2_N = YES;
  686. else if (regRelay.relay_event.bits.Gun2_P == NO)
  687. outputRelay.relay_event.bits.Gun2_P = YES;
  688. }
  689. }
  690. else if (_chargingData[index]->SystemStatus == S_COMPLETE)
  691. {
  692. if (_chargingData[index]->PresentChargingCurrent <= SEFETY_SWITCH_RELAY_CUR)
  693. {
  694. if (_chargingData[index]->Evboard_id == 0x01)
  695. {
  696. if(regRelay.relay_event.bits.Gun1_P == YES)
  697. outputRelay.relay_event.bits.Gun1_P = NO;
  698. else if (regRelay.relay_event.bits.Gun1_N == YES)
  699. outputRelay.relay_event.bits.Gun1_N = NO;
  700. }
  701. else if (_chargingData[index]->Evboard_id == 0x02)
  702. {
  703. if(regRelay.relay_event.bits.Gun2_P == YES)
  704. outputRelay.relay_event.bits.Gun2_P = NO;
  705. else if (regRelay.relay_event.bits.Gun2_N == YES)
  706. outputRelay.relay_event.bits.Gun2_N = NO;
  707. }
  708. }
  709. }
  710. else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST0)
  711. {
  712. if (_chargingData[index]->Evboard_id == 0x01)
  713. {
  714. if (_chargingData[index]->Type == _Type_CCS_2)
  715. {
  716. if (regRelay.relay_event.bits.CCS_Precharge == NO)
  717. outputRelay.relay_event.bits.CCS_Precharge = YES;
  718. else if (regRelay.relay_event.bits.CCS_Precharge == YES)
  719. outputRelay.relay_event.bits.Gun1_P = NO;
  720. }
  721. }
  722. else if (_chargingData[index]->Evboard_id == 0x02)
  723. {
  724. if (_chargingData[index]->Type == _Type_CCS_2)
  725. {
  726. if (regRelay.relay_event.bits.CCS_Precharge == NO)
  727. outputRelay.relay_event.bits.CCS_Precharge = YES;
  728. else if (regRelay.relay_event.bits.CCS_Precharge == YES)
  729. outputRelay.relay_event.bits.Gun2_P = NO;
  730. }
  731. }
  732. }
  733. else if (_chargingData[index]->SystemStatus == S_CCS_PRECHARGE_ST1)
  734. {
  735. if (_chargingData[index]->Evboard_id == 0x01)
  736. {
  737. if (_chargingData[index]->Type == _Type_CCS_2)
  738. {
  739. if (regRelay.relay_event.bits.Gun1_P == NO)
  740. outputRelay.relay_event.bits.Gun1_P = YES;
  741. else if(regRelay.relay_event.bits.Gun1_P == YES)
  742. outputRelay.relay_event.bits.CCS_Precharge = NO;
  743. }
  744. }
  745. else if (_chargingData[index]->Evboard_id == 0x02)
  746. {
  747. if (_chargingData[index]->Type == _Type_CCS_2)
  748. {
  749. if (regRelay.relay_event.bits.Gun2_P == NO)
  750. outputRelay.relay_event.bits.Gun2_P = YES;
  751. else if(regRelay.relay_event.bits.Gun2_P == YES)
  752. outputRelay.relay_event.bits.CCS_Precharge = NO;
  753. }
  754. }
  755. }
  756. }
  757. void SetParalleRelayStatus()
  758. {
  759. if (gunCount >= 2 && ONE_CONNECTOR_USE == 0)
  760. {
  761. if (_chargingData[0]->SystemStatus == S_BOOTING || _chargingData[1]->SystemStatus == S_BOOTING)
  762. {
  763. // 初始化~ 不搭橋接
  764. if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  765. outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
  766. else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
  767. outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
  768. }
  769. else
  770. {
  771. // 單槍充電中 - 搭上橋接
  772. if((_chargingData[0]->IsReadyToCharging == YES && _chargingData[1]->IsReadyToCharging == NO) ||
  773. (_chargingData[0]->IsReadyToCharging == NO && _chargingData[1]->IsReadyToCharging == YES))
  774. {
  775. if (ShmSysConfigAndInfo->SysInfo.ReAssignedFlag >= _REASSIGNED_RELAY)
  776. {
  777. if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  778. outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
  779. else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
  780. outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
  781. }
  782. else
  783. {
  784. if (regRelay.relay_event.bits.Gun1_Parallel_N == NO)
  785. outputRelay.relay_event.bits.Gun1_Parallel_N = YES;
  786. else if (regRelay.relay_event.bits.Gun1_Parallel_P == NO)
  787. outputRelay.relay_event.bits.Gun1_Parallel_P = YES;
  788. }
  789. }
  790. else
  791. {
  792. // 雙槍充電中~ 不搭橋接
  793. if (regRelay.relay_event.bits.Gun1_Parallel_P == YES)
  794. outputRelay.relay_event.bits.Gun1_Parallel_P = NO;
  795. else if (regRelay.relay_event.bits.Gun1_Parallel_N == YES)
  796. outputRelay.relay_event.bits.Gun1_Parallel_N = NO;
  797. }
  798. }
  799. }
  800. }
  801. //==========================================
  802. // Init all share memory
  803. //==========================================
  804. int InitShareMemory()
  805. {
  806. int result = PASS;
  807. int MeterSMId;
  808. //creat ShmSysConfigAndInfo
  809. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  810. {
  811. #ifdef SystemLogMessage
  812. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  813. #endif
  814. result = FAIL;
  815. }
  816. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  817. {
  818. #ifdef SystemLogMessage
  819. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  820. #endif
  821. result = FAIL;
  822. }
  823. //creat ShmStatusCodeData
  824. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  825. {
  826. #ifdef SystemLogMessage
  827. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  828. #endif
  829. result = FAIL;
  830. }
  831. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  832. {
  833. #ifdef SystemLogMessage
  834. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  835. #endif
  836. result = FAIL;
  837. }
  838. //creat ShmFanModuleData
  839. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), 0777)) < 0)
  840. {
  841. #ifdef SystemLogMessage
  842. DEBUG_ERROR("shmget ShmFanModuleData NG\n");
  843. #endif
  844. result = FAIL;
  845. }
  846. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  847. {
  848. #ifdef SystemLogMessage
  849. DEBUG_ERROR("shmat ShmFanModuleData NG\n");
  850. #endif
  851. result = FAIL;
  852. }
  853. memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
  854. //creat ShmRelayModuleData
  855. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), 0777)) < 0)
  856. {
  857. #ifdef SystemLogMessage
  858. DEBUG_ERROR("shmget ShmRelayModuleData NG\n");
  859. #endif
  860. result = FAIL;
  861. }
  862. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  863. {
  864. #ifdef SystemLogMessage
  865. DEBUG_ERROR("shmat ShmRelayModuleData NG\n");
  866. #endif
  867. result = FAIL;
  868. }
  869. if(CHAdeMO_QUANTITY > 0)
  870. {
  871. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  872. {
  873. #ifdef SystemLogMessage
  874. DEBUG_ERROR("[shmget ShmCHAdeMOData NG \n");
  875. #endif
  876. return FAIL;
  877. }
  878. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
  879. #ifdef SystemLogMessage
  880. DEBUG_ERROR("shmat ShmCHAdeMOData NG \n");
  881. #endif
  882. return FAIL;
  883. }
  884. }
  885. if(CCS_QUANTITY > 0)
  886. {
  887. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  888. {
  889. #ifdef SystemLogMessage
  890. DEBUG_ERROR("shmget ShmCcsData NG \n");
  891. #endif
  892. return FAIL;
  893. }
  894. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  895. {
  896. #ifdef SystemLogMessage
  897. DEBUG_ERROR("shmat ShmCcsData NG \n");
  898. #endif
  899. return FAIL;
  900. }
  901. }
  902. return result;
  903. }
  904. int InitComPort()
  905. {
  906. int fd;
  907. struct termios tios;
  908. fd = open(relayRs485PortName, O_RDWR);
  909. if(fd <= 0)
  910. {
  911. #ifdef SystemLogMessage
  912. DEBUG_ERROR("Module_InternalComm. InitComPort NG\n");
  913. #endif
  914. if(ShmStatusCodeData!=NULL)
  915. {
  916. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  917. }
  918. sleep(5);
  919. return -1;
  920. }
  921. ioctl (fd, TCGETS, &tios);
  922. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  923. tios.c_lflag = 0;
  924. tios.c_iflag = 0;
  925. tios.c_oflag = 0;
  926. tios.c_cc[VMIN]=0;
  927. tios.c_cc[VTIME]=(byte)0; // timeout 0.5 second
  928. tios.c_lflag=0;
  929. tcflush(fd, TCIFLUSH);
  930. ioctl (fd, TCSETS, &tios);
  931. return fd;
  932. }
  933. //================================================
  934. // Main process
  935. //================================================
  936. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  937. {
  938. for (byte index = 0; index < CHAdeMO_QUANTITY; index++) {
  939. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index
  940. == target) {
  941. chargingData[target] =
  942. &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  943. return true;
  944. }
  945. }
  946. for (byte index = 0; index < CCS_QUANTITY; index++) {
  947. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index
  948. == target) {
  949. chargingData[target] =
  950. &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  951. return true;
  952. }
  953. }
  954. for (byte index = 0; index < GB_QUANTITY; index++) {
  955. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index
  956. == target) {
  957. chargingData[target] =
  958. &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  959. return true;
  960. }
  961. }
  962. return false;
  963. }
  964. void Initialization()
  965. {
  966. bool isPass = false;
  967. for (byte index = 0; index < ARRAY_SIZE(outputRelay.relay_event.relay_status); index++)
  968. {
  969. outputRelay.relay_event.relay_status[index] = 0x00;
  970. }
  971. while(!isPass)
  972. {
  973. isPass = true;
  974. for (byte _index = 0; _index < gunCount; _index++)
  975. {
  976. if (!FindChargingInfoData(_index, &_chargingData[0]))
  977. {
  978. DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
  979. isPass = false;
  980. break;
  981. }
  982. }
  983. }
  984. }
  985. bool IsNoneMatchRelayStatus()
  986. {
  987. bool result = false;
  988. // PRINTF_FUNC("Real Relay, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
  989. // regRelay.relay_event.bits.AC_Contactor,
  990. // regRelay.relay_event.bits.Gun1_P,
  991. // regRelay.relay_event.bits.Gun1_N,
  992. // regRelay.relay_event.bits.Gun2_P,
  993. // regRelay.relay_event.bits.Gun2_N,
  994. // regRelay.relay_event.bits.CCS_Precharge,
  995. // regRelay.relay_event.bits.Gun1_Parallel_P,
  996. // regRelay.relay_event.bits.Gun1_Parallel_N);
  997. if ((regRelay.relay_event.bits.AC_Contactor != outputRelay.relay_event.bits.AC_Contactor) ||
  998. (regRelay.relay_event.bits.CCS_Precharge != outputRelay.relay_event.bits.CCS_Precharge) ||
  999. (regRelay.relay_event.bits.Gun1_P != outputRelay.relay_event.bits.Gun1_P) ||
  1000. (regRelay.relay_event.bits.Gun1_N != outputRelay.relay_event.bits.Gun1_N) ||
  1001. (regRelay.relay_event.bits.Gun2_P != outputRelay.relay_event.bits.Gun2_P) ||
  1002. (regRelay.relay_event.bits.Gun2_N != outputRelay.relay_event.bits.Gun2_N) ||
  1003. (regRelay.relay_event.bits.Gun1_Parallel_P != outputRelay.relay_event.bits.Gun1_Parallel_P) ||
  1004. (regRelay.relay_event.bits.Gun1_Parallel_N != outputRelay.relay_event.bits.Gun1_Parallel_N))
  1005. {
  1006. result = true;
  1007. }
  1008. return result;
  1009. }
  1010. void MatchRelayStatus()
  1011. {
  1012. // 因為 AC Contactor 沒有 Feedback,所以暫時先這樣處理
  1013. //regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
  1014. ShmSysConfigAndInfo->SysInfo.AcContactorStatus = regRelay.relay_event.bits.AC_Contactor = outputRelay.relay_event.bits.AC_Contactor;
  1015. regRelay.relay_event.bits.CCS_Precharge = outputRelay.relay_event.bits.CCS_Precharge;
  1016. regRelay.relay_event.bits.Gun1_P = outputRelay.relay_event.bits.Gun1_P;
  1017. regRelay.relay_event.bits.Gun1_N = outputRelay.relay_event.bits.Gun1_N;
  1018. regRelay.relay_event.bits.Gun2_P = outputRelay.relay_event.bits.Gun2_P;
  1019. regRelay.relay_event.bits.Gun2_N = outputRelay.relay_event.bits.Gun2_N;
  1020. regRelay.relay_event.bits.Gun1_Parallel_P = outputRelay.relay_event.bits.Gun1_Parallel_P;
  1021. regRelay.relay_event.bits.Gun1_Parallel_N = outputRelay.relay_event.bits.Gun1_Parallel_N;
  1022. }
  1023. void CheckRelayStatusByADC()
  1024. {
  1025. if (ShmRelayModuleData->Gun1FuseOutputVolt > 0 && ShmRelayModuleData->Gun1RelayOutputVolt > 0 &&
  1026. (ShmRelayModuleData->Gun1FuseOutputVolt == ShmRelayModuleData->Gun1RelayOutputVolt))
  1027. {
  1028. // Relay 前後電壓一致
  1029. _chargingData[0]->RelayK1K2Status = 0x01;
  1030. }
  1031. else
  1032. _chargingData[0]->RelayK1K2Status = 0x00;
  1033. if (ShmRelayModuleData->Gun2FuseOutputVolt > 0 && ShmRelayModuleData->Gun2RelayOutputVolt > 0 &&
  1034. (ShmRelayModuleData->Gun2FuseOutputVolt == ShmRelayModuleData->Gun2RelayOutputVolt))
  1035. {
  1036. // Relay 前後電壓一致
  1037. _chargingData[1]->RelayK1K2Status = 0x01;
  1038. }
  1039. else
  1040. _chargingData[1]->RelayK1K2Status = 0x00;
  1041. }
  1042. void SetGfdConfig(byte index, byte resister)
  1043. {
  1044. gfd_config.index = index;
  1045. gfd_config.state = resister;
  1046. //PRINTF_FUNC("************************GFD Vol = %d, GFD Res = %d \n", gfd_config.reqVol, gfd_config.resister);
  1047. if (Config_Gfd_Value(Uart5Fd, Addr.Relay, &gfd_config) == PASS)
  1048. {
  1049. // PRINTF_FUNC("Set reqVol = %f, resister = %d \n",
  1050. // gfd_config.reqVol,
  1051. // gfd_config.resister);
  1052. }
  1053. }
  1054. void CableCheckDetected(byte index)
  1055. {
  1056. // Cable Check
  1057. // 當火線上的電壓 = 車端要求的電壓電流
  1058. // _chargingData[targetGun]->EvBatterytargetVoltage
  1059. // 才可以開始偵測 1s
  1060. // Warning : Rgfd <= 150 歐/V 假設電壓為 500V 則~ Rgfd <= 75000 歐
  1061. // Pre-Warning : 150 歐/V < Rgfd <= 500 歐/V 假設電壓為 500V 則 75000 歐 < Rgfd <= 250000
  1062. // SO Normal : Rgfd > 500 歐/V 假設電壓為 500 V 則 Rgfd > 250000 歐
  1063. if ((_chargingData[index]->SystemStatus >= S_PREPARING_FOR_EVSE && _chargingData[index]->SystemStatus <= S_CHARGING) ||
  1064. (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1065. {
  1066. if (_chargingData[index]->SystemStatus == S_PREPARING_FOR_EVSE)
  1067. {
  1068. SetGfdConfig(index, GFD_CABLECHK);
  1069. }
  1070. else if (_chargingData[index]->SystemStatus >= S_CCS_PRECHARGE_ST0 &&
  1071. _chargingData[index]->SystemStatus <= S_CCS_PRECHARGE_ST1)
  1072. {
  1073. SetGfdConfig(index, GFD_PRECHARGE);
  1074. }
  1075. else if (_chargingData[index]->SystemStatus <= S_CHARGING)
  1076. {
  1077. SetGfdConfig(index, GFD_CHARGING);
  1078. }
  1079. }
  1080. else if(_chargingData[index]->SystemStatus == S_COMPLETE || _chargingData[index]->SystemStatus == S_PREPARNING
  1081. || _chargingData[index]->SystemStatus == S_IDLE)
  1082. {
  1083. SetGfdConfig(index, GFD_IDLE);
  1084. }
  1085. }
  1086. void CheckOutputPowerOverCarReq(byte index)
  1087. {
  1088. float fireV = _chargingData[index]->FireChargingVoltage;
  1089. float carV = _chargingData[index]->EvBatterytargetVoltage;
  1090. if (_chargingData[index]->EvBatterytargetVoltage > 1500 &&
  1091. (_chargingData[index]->Type == _Type_Chademo || _chargingData[index]->Type == _Type_CCS_2 || _chargingData[index]->Type == _Type_GB))
  1092. {
  1093. if (fireV >= (carV + (carV * 0.1)))
  1094. {
  1095. PRINTF_FUNC("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f \n",
  1096. _chargingData[index]->FireChargingVoltage, _chargingData[index]->EvBatterytargetVoltage);
  1097. DEBUG_ERROR("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f \n",
  1098. _chargingData[index]->FireChargingVoltage, _chargingData[index]->EvBatterytargetVoltage);
  1099. _chargingData[index]->StopChargeFlag = YES;
  1100. }
  1101. }
  1102. }
  1103. void CheckOutputVolNoneMatchFire(byte index)
  1104. {
  1105. if (_chargingData[index]->EvBatterytargetVoltage > 1500)
  1106. {
  1107. if ((_chargingData[index]->PresentChargingVoltage < _chargingData[index]->FireChargingVoltage - 300) ||
  1108. (_chargingData[index]->PresentChargingVoltage > _chargingData[index]->FireChargingVoltage + 300))
  1109. {
  1110. PRINTF_FUNC("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f \n",
  1111. _chargingData[index]->FireChargingVoltage, _chargingData[index]->EvBatterytargetVoltage);
  1112. DEBUG_ERROR("[Module_InternalComm]CheckOutputPowerOverCarReq NG : fire = %f, battery = %f \n",
  1113. _chargingData[index]->FireChargingVoltage, _chargingData[index]->EvBatterytargetVoltage);
  1114. _chargingData[index]->StopChargeFlag = YES;
  1115. }
  1116. }
  1117. }
  1118. int main(void)
  1119. {
  1120. if(InitShareMemory() == FAIL)
  1121. {
  1122. #ifdef SystemLogMessage
  1123. DEBUG_ERROR("InitShareMemory NG\n");
  1124. #endif
  1125. if(ShmStatusCodeData!=NULL)
  1126. {
  1127. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1128. }
  1129. sleep(5);
  1130. return 0;
  1131. }
  1132. // Open Uart5 for RB
  1133. Uart5Fd = InitComPort();
  1134. Initialization();
  1135. sleep(1);
  1136. if(Uart5Fd < 0)
  1137. {
  1138. printf ("open port error. \n");
  1139. return 0;
  1140. }
  1141. outputRelay.relay_event.bits.AC_Contactor = 0x00;
  1142. //outputRelay.relay_event.bits.CCS_Precharge = 0x00;
  1143. //outputRelay.relay_event.bits.Gun1_Parallel_P = 0x01;
  1144. //outputRelay.relay_event.bits.Gun1_Parallel_N = 0x01;
  1145. //outputRelay.relay_event.bits.Gun1_P = 0x01;
  1146. //outputRelay.relay_event.bits.Gun1_N = 0x01;
  1147. //outputRelay.relay_event.bits.Gun2_N = 0x01;
  1148. //outputRelay.relay_event.bits.Gun2_P = 0x01;
  1149. if(Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay) != PASS)
  1150. PRINTF_FUNC("Config_Relay_Output fail \n");
  1151. bool printRelayStatus = false;
  1152. //return 0;
  1153. for(;;)
  1154. {
  1155. bool isCharging = false;
  1156. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  1157. if (ShmRelayModuleData->SelfTest_Comp == NO)
  1158. {
  1159. GetFwAndHwVersion_Relay();
  1160. //SetRtcData_Relay();
  1161. sleep(1);
  1162. }
  1163. if (ShmFanModuleData->SelfTest_Comp == NO)
  1164. {
  1165. GetFwAndHwVersion_Fan();
  1166. SetModelName_Fan();
  1167. //SetRtcData_Fan();
  1168. sleep(1);
  1169. gettimeofday(&_priority_time, NULL);
  1170. }
  1171. if (ShmRelayModuleData->SelfTest_Comp == YES)
  1172. {
  1173. // ==============優先權最高 10 ms ==============
  1174. // 輸出電壓
  1175. GetPersentOutputVol();
  1176. // 三相輸入電壓
  1177. GetPresentInputVol();
  1178. // 讀取當前 relay 狀態
  1179. GetRelayOutputStatus();
  1180. for (int i = 0; i < gunCount; i++)
  1181. {
  1182. // Cable check (Set)
  1183. CableCheckDetected(i);
  1184. // check k1 k2 relay 狀態
  1185. CheckK1K2RelayOutput(i);
  1186. // 依據當前各槍的狀態選擇 搭上/放開 Relay
  1187. SetK1K2RelayStatus(i);
  1188. if (_chargingData[i]->SystemStatus == S_IDLE)
  1189. gfdChkFailCount[i] = 0;
  1190. if (_chargingData[i]->SystemStatus == S_BOOTING ||
  1191. (_chargingData[i]->SystemStatus >= S_PREPARNING && _chargingData[i]->SystemStatus <= S_COMPLETE) ||
  1192. (_chargingData[i]->SystemStatus >= S_CCS_PRECHARGE_ST0 && _chargingData[i]->SystemStatus <= S_CCS_PRECHARGE_ST1))
  1193. {
  1194. _chargingData[i]->IsReadyToCharging = YES;
  1195. isCharging = true;
  1196. if (_chargingData[i]->SystemStatus == S_CHARGING)
  1197. CheckOutputPowerOverCarReq(i);
  1198. CheckOutputVolNoneMatchFire(i);
  1199. }
  1200. else
  1201. _chargingData[i]->IsReadyToCharging = NO;
  1202. }
  1203. // Cable check (Get)
  1204. GetGfdAdc();
  1205. // 橋接 relay
  1206. if (gunCount > 1 && ONE_CONNECTOR_USE == NO)
  1207. SetParalleRelayStatus();
  1208. // 搭上 AC Contactor
  1209. if (isCharging)
  1210. outputRelay.relay_event.bits.AC_Contactor = YES;
  1211. else
  1212. outputRelay.relay_event.bits.AC_Contactor = NO;
  1213. // 搭上/鬆開 Relay
  1214. // 放開 Relay 之前要先確認輸出的電壓電流是否已經降到某個值
  1215. if(IsNoneMatchRelayStatus())
  1216. {
  1217. if (!printRelayStatus)
  1218. {
  1219. PRINTF_FUNC("Match Relay Target, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
  1220. outputRelay.relay_event.bits.AC_Contactor,
  1221. outputRelay.relay_event.bits.Gun1_P,
  1222. outputRelay.relay_event.bits.Gun1_N,
  1223. outputRelay.relay_event.bits.Gun2_P,
  1224. outputRelay.relay_event.bits.Gun2_N,
  1225. outputRelay.relay_event.bits.CCS_Precharge,
  1226. outputRelay.relay_event.bits.Gun1_Parallel_P,
  1227. outputRelay.relay_event.bits.Gun1_Parallel_N);
  1228. }
  1229. printRelayStatus = false;
  1230. if (Config_Relay_Output(Uart5Fd, Addr.Relay, &outputRelay))
  1231. {}
  1232. }
  1233. else
  1234. {
  1235. if (!printRelayStatus)
  1236. {
  1237. PRINTF_FUNC("Match Relay, AC = %x, g1_p = %x, g1_n = %x, g2_p = %x, g2_n = %x, pre = %x, bri_p = %x, bri_n = %x \n",
  1238. regRelay.relay_event.bits.AC_Contactor,
  1239. regRelay.relay_event.bits.Gun1_P,
  1240. regRelay.relay_event.bits.Gun1_N,
  1241. regRelay.relay_event.bits.Gun2_P,
  1242. regRelay.relay_event.bits.Gun2_N,
  1243. regRelay.relay_event.bits.CCS_Precharge,
  1244. regRelay.relay_event.bits.Gun1_Parallel_P,
  1245. regRelay.relay_event.bits.Gun1_Parallel_N);
  1246. }
  1247. printRelayStatus = true;
  1248. }
  1249. }
  1250. if (ShmFanModuleData->SelfTest_Comp == YES)
  1251. {
  1252. if (GetTimeoutValue(_priority_time) / 1000 >= 1000)
  1253. {
  1254. GetFanSpeed();
  1255. gettimeofday(&_priority_time, NULL);
  1256. if (isCharging)
  1257. {
  1258. if (ShmFanModuleData->PresentFan1Speed < MAX_FAN_SPEED ||
  1259. ShmFanModuleData->PresentFan2Speed < MAX_FAN_SPEED ||
  1260. ShmFanModuleData->PresentFan3Speed < MAX_FAN_SPEED ||
  1261. ShmFanModuleData->PresentFan4Speed < MAX_FAN_SPEED)
  1262. {
  1263. ShmFanModuleData->SetFan1Speed = MAX_FAN_SPEED;
  1264. ShmFanModuleData->SetFan2Speed = MAX_FAN_SPEED;
  1265. ShmFanModuleData->SetFan3Speed = MAX_FAN_SPEED;
  1266. ShmFanModuleData->SetFan4Speed = MAX_FAN_SPEED;
  1267. }
  1268. }
  1269. else
  1270. {
  1271. if (ShmFanModuleData->PresentFan1Speed > MIN_FAN_SPEED ||
  1272. ShmFanModuleData->PresentFan2Speed < MAX_FAN_SPEED ||
  1273. ShmFanModuleData->PresentFan3Speed < MAX_FAN_SPEED ||
  1274. ShmFanModuleData->PresentFan4Speed < MAX_FAN_SPEED)
  1275. {
  1276. ShmFanModuleData->SetFan1Speed = MIN_FAN_SPEED;
  1277. ShmFanModuleData->SetFan2Speed = MIN_FAN_SPEED;
  1278. ShmFanModuleData->SetFan3Speed = MIN_FAN_SPEED;
  1279. ShmFanModuleData->SetFan4Speed = MIN_FAN_SPEED;
  1280. }
  1281. }
  1282. if (ShmFanModuleData->TestFanSpeed > 0)
  1283. {
  1284. ShmFanModuleData->SetFan1Speed = ShmFanModuleData->TestFanSpeed;
  1285. ShmFanModuleData->SetFan2Speed = ShmFanModuleData->TestFanSpeed;
  1286. ShmFanModuleData->SetFan3Speed = ShmFanModuleData->TestFanSpeed;
  1287. ShmFanModuleData->SetFan4Speed = ShmFanModuleData->TestFanSpeed;
  1288. }
  1289. //PRINTF_FUNC("set fan = %d \n", ShmFanModuleData->SetFan1Speed);
  1290. SetFanModuleSpeed();
  1291. }
  1292. }
  1293. usleep(10000);
  1294. }
  1295. return FAIL;
  1296. }