Module_PrimaryComm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 <stdbool.h>
  16. #include <unistd.h>
  17. #include <stdarg.h>
  18. #include <stdio.h> /*標準輸入輸出定義*/
  19. #include <stdlib.h> /*標準函數庫定義*/
  20. #include <unistd.h> /*Unix 標準函數定義*/
  21. #include <fcntl.h> /*檔控制定義*/
  22. #include <termios.h> /*PPSIX 終端控制定義*/
  23. #include <errno.h> /*錯誤號定義*/
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include <ctype.h>
  28. #include <ifaddrs.h>
  29. #include <math.h>
  30. #include "../Log/log.h"
  31. #include "../Define/define.h"
  32. #include "../Config.h"
  33. #include "../ShareMemory/shmMem.h"
  34. #include "PrimaryComm.h"
  35. #include "Module_PrimaryComm.h"
  36. //------------------------------------------------------------------------------
  37. //struct SysConfigAndInfo *ShmSysConfigAndInfo;
  38. //struct StatusCodeData *ShmStatusCodeData;
  39. static struct SysConfigData *pSysConfig = NULL;
  40. static struct SysInfoData *pSysInfo = NULL;
  41. static struct AlarmCodeData *pAlarmCode = NULL;
  42. static struct FaultCodeData *pFaultCode = NULL;
  43. static struct PrimaryMcuData *ShmPrimaryMcuData;
  44. static DcCommonInfo *ShmDcCommonData = NULL;
  45. const char *priPortName = "/dev/ttyS1";
  46. uint8_t gun_count; //DS60-120 add
  47. uint8_t EmgBtn_count = 0;
  48. uint8_t Door_count = 0;
  49. uint8_t EmgBtn_flag = 0;
  50. uint8_t Door_flag = 0;
  51. //struct ChargingInfoData *ChargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  52. //------------------------------------------------------------------------------
  53. /*int StoreLogMsg(const char *fmt, ...)
  54. {
  55. char Buf[4096 + 256];
  56. char buffer[4096];
  57. va_list args;
  58. struct timeb SeqEndTime;
  59. struct tm *tm;
  60. va_start(args, fmt);
  61. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  62. va_end(args);
  63. memset(Buf, 0, sizeof(Buf));
  64. ftime(&SeqEndTime);
  65. SeqEndTime.time = time(NULL);
  66. tm = localtime(&SeqEndTime.time);
  67. if (pSysConfig->SwitchDebugFlag == YES) {
  68. sprintf(Buf, "%02d:%02d:%02d:%03d - %s",
  69. tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm, buffer);
  70. printf("%s ", Buf);
  71. } else {
  72. sprintf(Buf, "echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  73. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm,
  74. buffer,
  75. tm->tm_year + 1900, tm->tm_mon + 1);
  76. system(Buf);
  77. }
  78. return rc;
  79. }
  80. */
  81. #if 0 //non use
  82. int DiffTimeb(struct timeb ST, struct timeb ET)
  83. {
  84. //return milli-second
  85. unsigned int StartTime, StopTime;
  86. StartTime = (unsigned int)ST.time;
  87. StopTime = (unsigned int)ET.time;
  88. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  89. }
  90. //=================================
  91. // Common routine
  92. //=================================
  93. char *getTimeString(void)
  94. {
  95. char *result = malloc(21);
  96. time_t timep;
  97. struct tm *p;
  98. time(&timep);
  99. p = gmtime(&timep);
  100. sprintf(result, "[%04d-%02d-%02d %02d:%02d:%02d]",
  101. (1900 + p->tm_year),
  102. (1 + p->tm_mon),
  103. p->tm_mday,
  104. p->tm_hour,
  105. p->tm_hour,
  106. p->tm_sec);
  107. return result;
  108. }
  109. #endif //0
  110. //==========================================
  111. // Init all share memory
  112. //==========================================
  113. /*int InitShareMemory()
  114. {
  115. int result = PASS;
  116. int MeterSMId;
  117. //creat ShmSysConfigAndInfo
  118. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0) {
  119. result = FAIL;
  120. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  121. result = FAIL;
  122. }
  123. //creat ShmStatusCodeData
  124. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) {
  125. result = FAIL;
  126. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  127. result = FAIL;
  128. }
  129. //creat ShmStatusCodeData
  130. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0) {
  131. result = FAIL;
  132. } else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  133. result = FAIL;
  134. }
  135. return result;
  136. }
  137. */
  138. //================================================
  139. // Function
  140. //================================================
  141. void GetFwAndHwVersion(int fd)
  142. {
  143. Ver ver = {0};
  144. if (Query_FW_Ver(fd, OP_ADDR_IO_EXTEND, &ver) == PASS) {
  145. //log_info("Primary FW Rev = %s ", ver.Version_FW);
  146. strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
  147. strcpy((char *) pSysInfo->CsuPrimFwRev, ver.Version_FW);
  148. }
  149. //if (Query_HW_Ver(fd, OP_ADDR_IO_EXTEND, &ver) == PASS)
  150. // ;//log_info("Primary HW Rev = %s ", ver.Version_HW);
  151. }
  152. void GetInputGpioStatus(int fd)
  153. {
  154. uint8_t dispenserSwTmp = 0;
  155. Gpio_in gpio_in = {0};
  156. static uint8_t dispenserSw = 0;
  157. //log_info("GetInputGpioStatus ");
  158. if (Query_Gpio_Input(fd, OP_ADDR_IO_EXTEND, &gpio_in) != PASS) {
  159. return;
  160. }
  161. ShmPrimaryMcuData->InputDet.bits.SpdDetec = gpio_in.SPD;
  162. #ifdef DD360ComBox
  163. EmgBtn_flag = 0;
  164. #else
  165. if (gpio_in.Emergency_Btn && (EmgBtn_flag != gpio_in.Emergency_Btn))
  166. {
  167. EmgBtn_count++;
  168. if (EmgBtn_count > SensorTrigCount) {
  169. EmgBtn_flag = 1;
  170. EmgBtn_count = 0; // Avoid Overflow
  171. }
  172. } else if (EmgBtn_flag != gpio_in.Emergency_Btn ) {
  173. EmgBtn_count++;
  174. if (EmgBtn_count > SensorTrigCount) {
  175. EmgBtn_flag = 0;
  176. EmgBtn_count = 0;
  177. }
  178. }
  179. #endif
  180. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = EmgBtn_flag;
  181. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key0);
  182. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key1 << 1);
  183. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key2 << 2);
  184. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key3 << 3);
  185. if (dispenserSwTmp != dispenserSw) {
  186. dispenserSw = dispenserSwTmp;
  187. log_info("Dispenser switch number = %d, bit = %d, %d, %d, %d",
  188. dispenserSw,
  189. ShmPrimaryMcuData->InputDet.bits.Key3,
  190. ShmPrimaryMcuData->InputDet.bits.Key2,
  191. ShmPrimaryMcuData->InputDet.bits.Key1,
  192. ShmPrimaryMcuData->InputDet.bits.Key0);
  193. }
  194. ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
  195. ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
  196. #if defined DD360 || defined DD360Audi || defined DD360ComBox
  197. if ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  198. (strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  199. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0) ||
  200. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)
  201. ) {
  202. pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = ~gpio_in.AC_Connector;
  203. pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault = ~gpio_in.AC_MainBreaker;
  204. } else {
  205. pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = gpio_in.AC_Connector;
  206. pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault = gpio_in.AC_MainBreaker;
  207. }
  208. #if defined DD360ComBox
  209. Door_flag = 0;
  210. #else
  211. if (Door_flag == gpio_in.Door_Open) {
  212. Door_count++;
  213. if (Door_count == 3 ) {
  214. Door_count = 0;
  215. Door_flag = gpio_in.Door_Open;
  216. }
  217. } else {
  218. Door_flag = gpio_in.Door_Open;
  219. Door_count = 0;
  220. }
  221. /*
  222. if (gpio_in.Door_Open == 0 && (Door_flag == gpio_in.Door_Open))
  223. {
  224. Door_count++;
  225. if (Door_count > SensorTrigCount) {
  226. Door_flag = 1;
  227. Door_count = 0; // Avoid Overflow
  228. }
  229. } else if (gpio_in.Door_Open && Door_flag) {
  230. Door_count++;
  231. if (Door_count > SensorTrigCount) {
  232. Door_flag = 0;
  233. Door_count = 0;
  234. }
  235. }
  236. */
  237. #endif
  238. #if defined DD360ComBox
  239. ShmPrimaryMcuData->InputDet.bits.DoorOpen = Door_flag;
  240. #else
  241. ShmPrimaryMcuData->InputDet.bits.DoorOpen = ~Door_flag;
  242. #endif
  243. /*
  244. log_info("Emergency Button Count = %d , Emergency flag = %d",
  245. EmgBtn_count,EmgBtn_flag);
  246. log_info("Door Sensor Count = %d , Door Sensor flag = %d",
  247. Door_count,Door_flag);
  248. */
  249. ShmPrimaryMcuData->InputDet.bits.Key0 = ~gpio_in.Key[0] & 0x01;
  250. ShmPrimaryMcuData->InputDet.bits.Key1 = ~gpio_in.Key[1] & 0x01;
  251. ShmPrimaryMcuData->InputDet.bits.Key2 = ~gpio_in.Key[2] & 0x01;
  252. ShmPrimaryMcuData->InputDet.bits.Key3 = ~gpio_in.Key[3] & 0x01;
  253. return;
  254. #endif //defined DD360 || defined DD360Audi || defined DD360ComBox
  255. static uint8_t _curDeviceStatus[3] = {0};
  256. static uint8_t _reCheckCount[3] = {0};
  257. //DS60-120 add
  258. if (_curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] != gpio_in.AC_Connector) {
  259. if (_reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] >= 3) {
  260. _curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] = gpio_in.AC_Connector;
  261. pSysInfo->AcContactorStatus =
  262. ShmPrimaryMcuData->InputDet.bits.AcContactorDetec =
  263. gpio_in.AC_Connector;
  264. } else {
  265. _reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT]++;
  266. }
  267. } else {
  268. _reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] = 0;
  269. }
  270. if (_curDeviceStatus[_PRIMARY_CHECK_TAG_MAIN_BREAKER] != gpio_in.AC_MainBreaker) {
  271. if (_reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] >= 3) {
  272. _curDeviceStatus[_PRIMARY_CHECK_TAG_MAIN_BREAKER] = gpio_in.AC_MainBreaker;
  273. ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  274. } else {
  275. _reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER]++;
  276. }
  277. } else {
  278. _reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] = 0;
  279. }
  280. //pSysInfo->AcContactorStatus = ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = gpio_in.AC_Connector;
  281. //ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  282. ShmPrimaryMcuData->InputDet.bits.Key0 = gpio_in.Key[0] & 0x01;
  283. ShmPrimaryMcuData->InputDet.bits.Key1 = gpio_in.Key[1] & 0x01;
  284. ShmPrimaryMcuData->InputDet.bits.Key2 = gpio_in.Key[2] & 0x01;
  285. ShmPrimaryMcuData->InputDet.bits.Key3 = gpio_in.Key[3] & 0x01;
  286. ShmPrimaryMcuData->InputDet.bits.DoorOpen = gpio_in.Door_Open;
  287. /*printf(" gpio_in.Key[0]~ gpio_in.Key[3]=%d, %d, %d, %d",
  288. ShmPrimaryMcuData->InputDet.bits.Key0 , ShmPrimaryMcuData->InputDet.bits.Key1,
  289. ShmPrimaryMcuData->InputDet.bits.Key2,ShmPrimaryMcuData->InputDet.bits.Key3);
  290. printf("pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning=%d", pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning);
  291. printf("pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault=%d", pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault);
  292. */
  293. //log_info("left = %d ", ShmPrimaryMcuData->InputDet.bits.Button1);
  294. //log_info("right = %d ", ShmPrimaryMcuData->InputDet.bits.Button2);
  295. //log_info("pSysInfo->AcContactorStatus = %d ", pSysInfo->AcContactorStatus);
  296. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == YES) {
  297. log_error("AC Mainbreaker occur. ");
  298. }
  299. }
  300. static void checkChillerStatus(Gpio_out *gpio)
  301. {
  302. uint8_t gunIndex = 0;
  303. uint8_t chillerCount = 0;
  304. struct ChargingInfoData *pDcChargingInfo = NULL;
  305. static ChillerInfo fChillerInfo[2] = {0}, *pChillerInfo = NULL;
  306. static ChillerInfo _chiller;
  307. struct FanModuleData* ShmFanModuleData = (struct FanModuleData*)GetShmFanModuleData();;
  308. Gpio_out *pGpio = (Gpio_out *)gpio;
  309. if ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  310. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0)) {
  311. chillerCount++;
  312. }
  313. if ((strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  314. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)) {
  315. chillerCount++;
  316. }
  317. if (chillerCount == 0) {
  318. pGpio->AC_Connector = 0x00;
  319. return;
  320. }
  321. for (gunIndex = 0; gunIndex < chillerCount; gunIndex++) {
  322. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  323. pChillerInfo = (ChillerInfo *)&fChillerInfo[gunIndex];
  324. if((pDcChargingInfo->SystemStatus > S_IDLE && pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  325. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 && pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))
  326. {
  327. pChillerInfo->ChillerSwitch = YES;
  328. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  329. }
  330. else
  331. {
  332. if(pChillerInfo->ChillerSwitch == YES)
  333. {
  334. //10分鐘後停止
  335. if ((time((time_t *)NULL) - pChillerInfo->ChillerOnTime) >= 600)
  336. {
  337. pChillerInfo->ChillerSwitch = NO;
  338. ShmFanModuleData->SetFan1Speed = 0;
  339. }
  340. }
  341. else
  342. {
  343. pChillerInfo->ChillerSwitch = NO;
  344. }
  345. }
  346. #if 0
  347. if ((pDcChargingInfo->PresentChargingCurrent) >= 150) { //當前電壓於150A,打開水冷機
  348. pChillerInfo->ChillerSwitch = YES;
  349. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  350. } else {
  351. if (pChillerInfo->ChillerSwitch == YES) {
  352. if ((pDcChargingInfo->PresentChargingCurrent) >= 100) { //判斷如果還是大於100A不變動
  353. pChillerInfo->ChillerSwitch = YES;
  354. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  355. } else {
  356. if ((time((time_t *)NULL) - pChillerInfo->ChillerOnTime) >= 600) { //5分鐘後停止
  357. pChillerInfo->ChillerSwitch = NO;
  358. } else {
  359. pChillerInfo->ChillerSwitch = YES;
  360. }
  361. }
  362. } else {
  363. pChillerInfo->ChillerSwitch = NO;
  364. }
  365. }
  366. #endif
  367. }
  368. uint8_t _chillerNeedOn = NO;
  369. for (gunIndex = 0; gunIndex < chillerCount; gunIndex++)
  370. {
  371. pChillerInfo = (ChillerInfo *)&fChillerInfo[gunIndex];
  372. if(pChillerInfo->ChillerSwitch == YES)
  373. {
  374. _chillerNeedOn = YES;
  375. if (chillerCount >= 2) {
  376. ShmFanModuleData->SetFan1Speed = 7000;
  377. }
  378. }
  379. }
  380. if (ShmDcCommonData->debugflag == YES)
  381. _chillerNeedOn = ShmDcCommonData->chillerCtrl;
  382. if(_chiller.ChillerSwitch != _chillerNeedOn)
  383. {
  384. log_info("Chiller Need Turn %s", _chillerNeedOn == YES ? "ON" : "OFF");
  385. }
  386. _chiller.ChillerSwitch = _chillerNeedOn;
  387. pGpio->AC_Connector = _chiller.ChillerSwitch;//Chiller ON/OFF Control, "0: Chiller disable, 1: Chiller enable"
  388. }
  389. void SetOutputGpio(int fd, uint8_t outputValue)
  390. {
  391. Gpio_out gpio;
  392. LedConfig *pLedConfig = (LedConfig *)&outputValue;
  393. static uint8_t flash = NO;
  394. if (strcmp((char *)pSysInfo->LcmHwRev, " ") == 0x00) {
  395. if (flash == NO) {
  396. flash = YES;
  397. } else {
  398. flash = NO;
  399. }
  400. } else {
  401. if (flash == NO) {
  402. flash = YES;
  403. }
  404. }
  405. pLedConfig->LeftButtonLed = flash;
  406. pLedConfig->RightButtonLed = flash;
  407. gpio.Button_LED[0] = pLedConfig->LeftButtonLed;
  408. gpio.Button_LED[1] = pLedConfig->RightButtonLed;
  409. gpio.System_LED[0] = pLedConfig->GreenLED;
  410. gpio.System_LED[1] = pLedConfig->YellowLED;
  411. gpio.System_LED[2] = pLedConfig->RedLED;
  412. gpio.System_LED[3] = 0x00;
  413. checkChillerStatus(&gpio);
  414. gpio.AC_Breaker = 0x00;
  415. Config_Gpio_Output(fd, OP_ADDR_IO_EXTEND, &gpio);
  416. }
  417. void SetRtcData(int fd)
  418. {
  419. Rtc rtc = {0};
  420. struct timeb csuTime;
  421. struct tm *tmCSU;
  422. ftime(&csuTime);
  423. tmCSU = localtime(&csuTime.time);
  424. //log_info("Time : %04d-%02d-%02d %02d:%02d:%02d ",
  425. // tmCSU->tm_year + 1900,
  426. // tmCSU->tm_mon + 1,
  427. // tmCSU->tm_mday,
  428. // tmCSU->tm_hour,
  429. // tmCSU->tm_min,
  430. // tmCSU->tm_sec);
  431. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  432. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  433. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  434. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  435. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  436. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  437. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  438. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  439. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  440. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  441. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  442. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  443. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  444. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  445. if (Config_Rtc_Data(fd, OP_ADDR_IO_EXTEND, &rtc) == PASS) {
  446. //log_info("SetRtc sucessfully. ");
  447. } else {
  448. //log_info("SetRtc fail. ");
  449. }
  450. }
  451. void SetModelName(int fd)
  452. {
  453. if (Config_Model_Name(fd, OP_ADDR_IO_EXTEND, pSysConfig->ModelName) == PASS) {
  454. }
  455. }
  456. //================================================
  457. // Main process
  458. //================================================
  459. int InitComPort()
  460. {
  461. int fd;
  462. struct termios tios;
  463. fd = open(priPortName, O_RDWR);
  464. if (fd <= 0) {
  465. #ifdef SystemLogMessage
  466. log_error("open 407 Communication port NG ");
  467. #endif
  468. return -1;
  469. }
  470. ioctl (fd, TCGETS, &tios);
  471. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  472. tios.c_lflag = 0;
  473. tios.c_iflag = 0;
  474. tios.c_oflag = 0;
  475. tios.c_cc[VMIN] = 0;
  476. tios.c_cc[VTIME] = (uint8_t)1;
  477. tios.c_lflag = 0;
  478. tcflush(fd, TCIFLUSH);
  479. ioctl (fd, TCSETS, &tios);
  480. return fd;
  481. }
  482. unsigned long GetTimeoutValue(struct timeval _sour_time)
  483. {
  484. struct timeval _end_time;
  485. gettimeofday(&_end_time, NULL);
  486. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  487. }
  488. //int FindChargingInfoData(uint8_t target, struct ChargingInfoData **chargingData)
  489. //{
  490. // for (uint8_t index = 0; index < CHAdeMO_QUANTITY; index++) {
  491. // if (pSysInfo->ChademoChargingData[index].Index == target) {
  492. // chargingData[target] = &pSysInfo->ChademoChargingData[index];
  493. // return 1;
  494. // }
  495. // }
  496. //
  497. // for (uint8_t index = 0; index < CCS_QUANTITY; index++) {
  498. // if (pSysInfo->CcsChargingData[index].Index == target) {
  499. // chargingData[target] = &pSysInfo->CcsChargingData[index];
  500. // return 1;
  501. // }
  502. // }
  503. //
  504. // for (uint8_t index = 0; index < GB_QUANTITY; index++) {
  505. // if (pSysInfo->GbChargingData[index].Index == target) {
  506. // chargingData[target] = &pSysInfo->GbChargingData[index];
  507. // return 1;
  508. // }
  509. // }
  510. //
  511. // return 0;
  512. //}
  513. //void Initialization() //DS60-120 add
  514. //{
  515. // bool isPass = false;
  516. // while (!isPass) {
  517. // isPass = true;
  518. // for (uint8_t _index = 0; _index < gun_count; _index++) {
  519. // if (!FindChargingInfoData(_index, &ChargingData[0])) {
  520. // log_error("EvComm (main) : FindChargingInfoData false ");
  521. // isPass = false;
  522. // break;
  523. // }
  524. // }
  525. // sleep(1);
  526. // }
  527. //}
  528. static bool IsPrimaryProcessNeedPause(void)
  529. {
  530. bool _pause = false;
  531. static bool isPause = false;
  532. struct ChargingInfoData *pDcChargingInfo = NULL;
  533. for (uint8_t i = 0; i < pSysConfig->TotalConnectorCount; i++)
  534. {
  535. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  536. if(pDcChargingInfo->SystemStatus == S_UPDATE)
  537. {
  538. _pause = true;
  539. }
  540. }
  541. if(isPause != _pause)
  542. {
  543. log_info("Primary Process Now Is %s ", _pause == true ? "Paused" : "Continued");
  544. }
  545. isPause = _pause;
  546. return _pause;
  547. }
  548. int main(void)
  549. {
  550. int Uart1Fd = -1;
  551. //if (InitShareMemory() == FAIL) {
  552. // log_error("InitShareMemory NG");
  553. // if (ShmStatusCodeData != NULL) {
  554. // pAlarmCode->AlarmEvents.bits.FailToCreateShareMemory = 1;
  555. // }
  556. // sleep(5);
  557. // return 0;
  558. //}
  559. if (CreateAllCsuShareMemory() == FAIL) {
  560. log_error("create share memory error");
  561. return FAIL;
  562. }
  563. MappingGunChargingInfo("Primary Task");
  564. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  565. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  566. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  567. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  568. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  569. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  570. Uart1Fd = InitComPort();
  571. //log_info("407 Port id = %d ", Uart1Fd);
  572. if (Uart1Fd < 0) {
  573. log_error("InitComPort (Uart1 : AM3352 - STM32) NG");
  574. if (pAlarmCode != NULL) {
  575. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  576. }
  577. sleep(5);
  578. return 0;
  579. }
  580. SetRtcData(Uart1Fd);
  581. SetModelName(Uart1Fd);
  582. gun_count = pSysConfig->TotalConnectorCount;
  583. //Initialization();
  584. for (;;) {
  585. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  586. // 模組更新 FW 後,需重新做
  587. if(IsPrimaryProcessNeedPause() == true)
  588. {
  589. sleep(1);
  590. continue;
  591. }
  592. if (ShmPrimaryMcuData->SelfTest_Comp != PASS) {
  593. //log_info("(407) Get Fw and Hw Ver. ");
  594. GetFwAndHwVersion(Uart1Fd);
  595. sleep(1);
  596. ShmPrimaryMcuData->SelfTest_Comp = PASS;
  597. } else {
  598. SetOutputGpio(Uart1Fd, ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0]);
  599. GetInputGpioStatus(Uart1Fd);
  600. }
  601. usleep(50000);
  602. }
  603. return FAIL;
  604. }