Module_PrimaryComm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. const char *priPortName = "/dev/ttyS1";
  45. uint8_t gun_count; //DS60-120 add
  46. //struct ChargingInfoData *ChargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  47. //------------------------------------------------------------------------------
  48. /*int StoreLogMsg(const char *fmt, ...)
  49. {
  50. char Buf[4096 + 256];
  51. char buffer[4096];
  52. va_list args;
  53. struct timeb SeqEndTime;
  54. struct tm *tm;
  55. va_start(args, fmt);
  56. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  57. va_end(args);
  58. memset(Buf, 0, sizeof(Buf));
  59. ftime(&SeqEndTime);
  60. SeqEndTime.time = time(NULL);
  61. tm = localtime(&SeqEndTime.time);
  62. if (pSysConfig->SwitchDebugFlag == YES) {
  63. sprintf(Buf, "%02d:%02d:%02d:%03d - %s",
  64. tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm, buffer);
  65. printf("%s \n", Buf);
  66. } else {
  67. sprintf(Buf, "echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  68. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm,
  69. buffer,
  70. tm->tm_year + 1900, tm->tm_mon + 1);
  71. system(Buf);
  72. }
  73. return rc;
  74. }
  75. */
  76. #if 0 //non use
  77. int DiffTimeb(struct timeb ST, struct timeb ET)
  78. {
  79. //return milli-second
  80. unsigned int StartTime, StopTime;
  81. StartTime = (unsigned int)ST.time;
  82. StopTime = (unsigned int)ET.time;
  83. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  84. }
  85. //=================================
  86. // Common routine
  87. //=================================
  88. char *getTimeString(void)
  89. {
  90. char *result = malloc(21);
  91. time_t timep;
  92. struct tm *p;
  93. time(&timep);
  94. p = gmtime(&timep);
  95. sprintf(result, "[%04d-%02d-%02d %02d:%02d:%02d]",
  96. (1900 + p->tm_year),
  97. (1 + p->tm_mon),
  98. p->tm_mday,
  99. p->tm_hour,
  100. p->tm_hour,
  101. p->tm_sec);
  102. return result;
  103. }
  104. #endif //0
  105. //==========================================
  106. // Init all share memory
  107. //==========================================
  108. /*int InitShareMemory()
  109. {
  110. int result = PASS;
  111. int MeterSMId;
  112. //creat ShmSysConfigAndInfo
  113. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0) {
  114. result = FAIL;
  115. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  116. result = FAIL;
  117. }
  118. //creat ShmStatusCodeData
  119. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) {
  120. result = FAIL;
  121. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  122. result = FAIL;
  123. }
  124. //creat ShmStatusCodeData
  125. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0) {
  126. result = FAIL;
  127. } else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  128. result = FAIL;
  129. }
  130. return result;
  131. }
  132. */
  133. //================================================
  134. // Function
  135. //================================================
  136. void GetFwAndHwVersion(int fd)
  137. {
  138. Ver ver = {0};
  139. if (Query_FW_Ver(fd, OP_ADDR_IO_EXTEND, &ver) == PASS) {
  140. //log_info("Primary FW Rev = %s \n", ver.Version_FW);
  141. strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
  142. strcpy((char *) pSysInfo->CsuPrimFwRev, ver.Version_FW);
  143. }
  144. //if (Query_HW_Ver(fd, OP_ADDR_IO_EXTEND, &ver) == PASS)
  145. // ;//log_info("Primary HW Rev = %s \n", ver.Version_HW);
  146. }
  147. void GetInputGpioStatus(int fd)
  148. {
  149. uint8_t dispenserSwTmp = 0;
  150. Gpio_in gpio_in = {0};
  151. static uint8_t dispenserSw = 0;
  152. //log_info("GetInputGpioStatus \n");
  153. if (Query_Gpio_Input(fd, OP_ADDR_IO_EXTEND, &gpio_in) != PASS) {
  154. return;
  155. }
  156. ShmPrimaryMcuData->InputDet.bits.SpdDetec = gpio_in.SPD;
  157. #if defined DD360ComBox
  158. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = ~gpio_in.Emergency_Btn;
  159. #else
  160. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = gpio_in.Emergency_Btn;
  161. #endif //defined DD360ComBox
  162. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key0);
  163. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key1 << 1);
  164. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key2 << 2);
  165. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key3 << 3);
  166. if (dispenserSwTmp != dispenserSw) {
  167. dispenserSw = dispenserSwTmp;
  168. log_info("Dispenser switch number = %d, bit = %d, %d, %d, %d\r\n",
  169. dispenserSw,
  170. ShmPrimaryMcuData->InputDet.bits.Key3,
  171. ShmPrimaryMcuData->InputDet.bits.Key2,
  172. ShmPrimaryMcuData->InputDet.bits.Key1,
  173. ShmPrimaryMcuData->InputDet.bits.Key0);
  174. }
  175. ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
  176. ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
  177. #if defined DD360 || defined DD360Audi || defined DD360ComBox
  178. if ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  179. (strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  180. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0) ||
  181. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)
  182. ) {
  183. pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = ~gpio_in.AC_Connector;
  184. pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault = ~gpio_in.AC_MainBreaker;
  185. } else {
  186. pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = gpio_in.AC_Connector;
  187. pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault = gpio_in.AC_MainBreaker;
  188. }
  189. #if defined DD360ComBox
  190. ShmPrimaryMcuData->InputDet.bits.DoorOpen = gpio_in.Door_Open;
  191. #else
  192. ShmPrimaryMcuData->InputDet.bits.DoorOpen = ~gpio_in.Door_Open;
  193. #endif //defined DD360ComBox
  194. ShmPrimaryMcuData->InputDet.bits.Key0 = ~gpio_in.Key[0] & 0x01;
  195. ShmPrimaryMcuData->InputDet.bits.Key1 = ~gpio_in.Key[1] & 0x01;
  196. ShmPrimaryMcuData->InputDet.bits.Key2 = ~gpio_in.Key[2] & 0x01;
  197. ShmPrimaryMcuData->InputDet.bits.Key3 = ~gpio_in.Key[3] & 0x01;
  198. return;
  199. #endif //defined DD360 || defined DD360Audi || defined DD360ComBox
  200. static uint8_t _curDeviceStatus[3] = {0};
  201. static uint8_t _reCheckCount[3] = {0};
  202. //DS60-120 add
  203. if (_curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] != gpio_in.AC_Connector) {
  204. if (_reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] >= 3) {
  205. _curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] = gpio_in.AC_Connector;
  206. pSysInfo->AcContactorStatus =
  207. ShmPrimaryMcuData->InputDet.bits.AcContactorDetec =
  208. gpio_in.AC_Connector;
  209. } else {
  210. _reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT]++;
  211. }
  212. } else {
  213. _reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] = 0;
  214. }
  215. if (_curDeviceStatus[_PRIMARY_CHECK_TAG_MAIN_BREAKER] != gpio_in.AC_MainBreaker) {
  216. if (_reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] >= 3) {
  217. _curDeviceStatus[_PRIMARY_CHECK_TAG_MAIN_BREAKER] = gpio_in.AC_MainBreaker;
  218. ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  219. } else {
  220. _reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER]++;
  221. }
  222. } else {
  223. _reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] = 0;
  224. }
  225. //pSysInfo->AcContactorStatus = ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = gpio_in.AC_Connector;
  226. //ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  227. ShmPrimaryMcuData->InputDet.bits.Key0 = gpio_in.Key[0] & 0x01;
  228. ShmPrimaryMcuData->InputDet.bits.Key1 = gpio_in.Key[1] & 0x01;
  229. ShmPrimaryMcuData->InputDet.bits.Key2 = gpio_in.Key[2] & 0x01;
  230. ShmPrimaryMcuData->InputDet.bits.Key3 = gpio_in.Key[3] & 0x01;
  231. ShmPrimaryMcuData->InputDet.bits.DoorOpen = gpio_in.Door_Open;
  232. /*printf(" gpio_in.Key[0]~ gpio_in.Key[3]=%d, %d, %d, %d\n",
  233. ShmPrimaryMcuData->InputDet.bits.Key0 , ShmPrimaryMcuData->InputDet.bits.Key1,
  234. ShmPrimaryMcuData->InputDet.bits.Key2,ShmPrimaryMcuData->InputDet.bits.Key3);
  235. printf("pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning=%d\n", pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning);
  236. printf("pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault=%d\n", pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault);
  237. */
  238. //log_info("left = %d \n", ShmPrimaryMcuData->InputDet.bits.Button1);
  239. //log_info("right = %d \n", ShmPrimaryMcuData->InputDet.bits.Button2);
  240. //log_info("pSysInfo->AcContactorStatus = %d \n", pSysInfo->AcContactorStatus);
  241. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == YES) {
  242. log_error("AC Mainbreaker occur. \n");
  243. }
  244. }
  245. static void checkChillerStatus(Gpio_out *gpio)
  246. {
  247. uint8_t gunIndex = 0;
  248. uint8_t chillerCount = 0;
  249. struct ChargingInfoData *pDcChargingInfo = NULL;
  250. static ChillerInfo fChillerInfo[2] = {0}, *pChillerInfo = NULL;
  251. Gpio_out *pGpio = (Gpio_out *)gpio;
  252. if ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  253. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0)) {
  254. chillerCount++;
  255. }
  256. if ((strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  257. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)) {
  258. chillerCount++;
  259. }
  260. if (chillerCount == 0) {
  261. pGpio->AC_Connector = 0x00;
  262. return;
  263. }
  264. for (gunIndex = 0; gunIndex < chillerCount; gunIndex++) {
  265. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  266. pChillerInfo = (ChillerInfo *)&fChillerInfo[gunIndex];
  267. if ((pDcChargingInfo->PresentChargingCurrent) >= 150) { //當前電壓於150A,打開水冷機
  268. pChillerInfo->ChillerSwitch = YES;
  269. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  270. } else {
  271. if (pChillerInfo->ChillerSwitch == YES) {
  272. if ((pDcChargingInfo->PresentChargingCurrent) >= 100) { //判斷如果還是大於100A不變動
  273. pChillerInfo->ChillerSwitch = YES;
  274. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  275. } else {
  276. if ((time((time_t *)NULL) - pChillerInfo->ChillerOnTime) >= 600) { //5分鐘後停止
  277. pChillerInfo->ChillerSwitch = NO;
  278. } else {
  279. pChillerInfo->ChillerSwitch = YES;
  280. }
  281. }
  282. } else {
  283. pChillerInfo->ChillerSwitch = NO;
  284. }
  285. }
  286. }
  287. pGpio->AC_Connector = pChillerInfo->ChillerSwitch;//Chiller ON/OFF Control, "0: Chiller disable, 1: Chiller enable"
  288. }
  289. void SetOutputGpio(int fd, uint8_t outputValue)
  290. {
  291. Gpio_out gpio;
  292. LedConfig *pLedConfig = (LedConfig *)&outputValue;
  293. static uint8_t flash = NO;
  294. if (strcmp((char *)pSysInfo->LcmHwRev, " ") == 0x00) {
  295. if (flash == NO) {
  296. flash = YES;
  297. } else {
  298. flash = NO;
  299. }
  300. } else {
  301. if (flash == NO) {
  302. flash = YES;
  303. }
  304. }
  305. pLedConfig->LeftButtonLed = flash;
  306. pLedConfig->RightButtonLed = flash;
  307. gpio.Button_LED[0] = pLedConfig->LeftButtonLed;
  308. gpio.Button_LED[1] = pLedConfig->RightButtonLed;
  309. gpio.System_LED[0] = pLedConfig->GreenLED;
  310. gpio.System_LED[1] = pLedConfig->YellowLED;
  311. gpio.System_LED[2] = pLedConfig->RedLED;
  312. gpio.System_LED[3] = 0x00;
  313. checkChillerStatus(&gpio);
  314. gpio.AC_Breaker = 0x00;
  315. Config_Gpio_Output(fd, OP_ADDR_IO_EXTEND, &gpio);
  316. }
  317. void SetRtcData(int fd)
  318. {
  319. Rtc rtc = {0};
  320. struct timeb csuTime;
  321. struct tm *tmCSU;
  322. ftime(&csuTime);
  323. tmCSU = localtime(&csuTime.time);
  324. //log_info("Time : %04d-%02d-%02d %02d:%02d:%02d \n",
  325. // tmCSU->tm_year + 1900,
  326. // tmCSU->tm_mon + 1,
  327. // tmCSU->tm_mday,
  328. // tmCSU->tm_hour,
  329. // tmCSU->tm_min,
  330. // tmCSU->tm_sec);
  331. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  332. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  333. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  334. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  335. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  336. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  337. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  338. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  339. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  340. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  341. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  342. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  343. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  344. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  345. if (Config_Rtc_Data(fd, OP_ADDR_IO_EXTEND, &rtc) == PASS) {
  346. //log_info("SetRtc sucessfully. \n");
  347. } else {
  348. //log_info("SetRtc fail. \n");
  349. }
  350. }
  351. void SetModelName(int fd)
  352. {
  353. if (Config_Model_Name(fd, OP_ADDR_IO_EXTEND, pSysConfig->ModelName) == PASS) {
  354. }
  355. }
  356. //================================================
  357. // Main process
  358. //================================================
  359. int InitComPort()
  360. {
  361. int fd;
  362. struct termios tios;
  363. fd = open(priPortName, O_RDWR);
  364. if (fd <= 0) {
  365. #ifdef SystemLogMessage
  366. log_error("open 407 Communication port NG \n");
  367. #endif
  368. return -1;
  369. }
  370. ioctl (fd, TCGETS, &tios);
  371. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  372. tios.c_lflag = 0;
  373. tios.c_iflag = 0;
  374. tios.c_oflag = 0;
  375. tios.c_cc[VMIN] = 0;
  376. tios.c_cc[VTIME] = (uint8_t)1;
  377. tios.c_lflag = 0;
  378. tcflush(fd, TCIFLUSH);
  379. ioctl (fd, TCSETS, &tios);
  380. return fd;
  381. }
  382. unsigned long GetTimeoutValue(struct timeval _sour_time)
  383. {
  384. struct timeval _end_time;
  385. gettimeofday(&_end_time, NULL);
  386. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  387. }
  388. //int FindChargingInfoData(uint8_t target, struct ChargingInfoData **chargingData)
  389. //{
  390. // for (uint8_t index = 0; index < CHAdeMO_QUANTITY; index++) {
  391. // if (pSysInfo->ChademoChargingData[index].Index == target) {
  392. // chargingData[target] = &pSysInfo->ChademoChargingData[index];
  393. // return 1;
  394. // }
  395. // }
  396. //
  397. // for (uint8_t index = 0; index < CCS_QUANTITY; index++) {
  398. // if (pSysInfo->CcsChargingData[index].Index == target) {
  399. // chargingData[target] = &pSysInfo->CcsChargingData[index];
  400. // return 1;
  401. // }
  402. // }
  403. //
  404. // for (uint8_t index = 0; index < GB_QUANTITY; index++) {
  405. // if (pSysInfo->GbChargingData[index].Index == target) {
  406. // chargingData[target] = &pSysInfo->GbChargingData[index];
  407. // return 1;
  408. // }
  409. // }
  410. //
  411. // return 0;
  412. //}
  413. //void Initialization() //DS60-120 add
  414. //{
  415. // bool isPass = false;
  416. // while (!isPass) {
  417. // isPass = true;
  418. // for (uint8_t _index = 0; _index < gun_count; _index++) {
  419. // if (!FindChargingInfoData(_index, &ChargingData[0])) {
  420. // log_error("EvComm (main) : FindChargingInfoData false \n");
  421. // isPass = false;
  422. // break;
  423. // }
  424. // }
  425. // sleep(1);
  426. // }
  427. //}
  428. int main(void)
  429. {
  430. int Uart1Fd = -1;
  431. //if (InitShareMemory() == FAIL) {
  432. // log_error("InitShareMemory NG\r\n");
  433. // if (ShmStatusCodeData != NULL) {
  434. // pAlarmCode->AlarmEvents.bits.FailToCreateShareMemory = 1;
  435. // }
  436. // sleep(5);
  437. // return 0;
  438. //}
  439. if (CreateAllCsuShareMemory() == FAIL) {
  440. log_error("create share memory error\r\n");
  441. return FAIL;
  442. }
  443. MappingGunChargingInfo("Primary Task");
  444. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  445. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  446. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  447. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  448. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  449. Uart1Fd = InitComPort();
  450. //log_info("407 Port id = %d \n", Uart1Fd);
  451. if (Uart1Fd < 0) {
  452. log_error("InitComPort (Uart1 : AM3352 - STM32) NG");
  453. if (pAlarmCode != NULL) {
  454. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  455. }
  456. sleep(5);
  457. return 0;
  458. }
  459. SetRtcData(Uart1Fd);
  460. SetModelName(Uart1Fd);
  461. gun_count = pSysConfig->TotalConnectorCount;
  462. //Initialization();
  463. for (;;) {
  464. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  465. // 模組更新 FW 後,需重新做
  466. if (ShmPrimaryMcuData->SelfTest_Comp != PASS) {
  467. //log_info("(407) Get Fw and Hw Ver. \n");
  468. GetFwAndHwVersion(Uart1Fd);
  469. sleep(1);
  470. ShmPrimaryMcuData->SelfTest_Comp = PASS;
  471. } else {
  472. SetOutputGpio(Uart1Fd, ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0]);
  473. GetInputGpioStatus(Uart1Fd);
  474. }
  475. usleep(50000);
  476. }
  477. return FAIL;
  478. }