Module_InternalComm.c 45 KB

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