Module_PrimaryComm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. bool _isNeedChkTilt = FALSE;
  52. struct timespec _tilt_time;
  53. uint8_t tilt = NO;
  54. bool _chargeSwitch;
  55. //================================================
  56. // Function
  57. //================================================
  58. bool GetFwAndHwVersion(int fd)
  59. {
  60. Ver ver = {0};
  61. if (Query_FW_Ver(fd, OP_ADDR_IO_EXTEND, &ver) == PASS) {
  62. //log_info("Primary FW Rev = %s ", ver.Version_FW);
  63. strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
  64. strcpy((char *) pSysInfo->CsuPrimFwRev, ver.Version_FW);
  65. return TRUE;
  66. }
  67. return FALSE;
  68. //if (Query_HW_Ver(fd, OP_ADDR_IO_EXTEND, &ver) == PASS)
  69. // ;//log_info("Primary HW Rev = %s ", ver.Version_HW);
  70. }
  71. void GetInputGpioStatus(int fd)
  72. {
  73. uint8_t dispenserSwTmp = 0;
  74. Gpio_in gpio_in = {0};
  75. static uint8_t dispenserSw = 0;
  76. //log_info("GetInputGpioStatus ");
  77. if (Query_Gpio_Input(fd, OP_ADDR_IO_EXTEND, &gpio_in) != PASS) {
  78. return;
  79. }
  80. ShmPrimaryMcuData->InputDet.bits.SpdDetec = gpio_in.SPD;
  81. //log_info("gpio_in.AC_Drop:%d", gpio_in.AC_Drop);
  82. if ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  83. (strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  84. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0) ||
  85. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)
  86. ) {
  87. ShmPrimaryMcuData->InputDet.bits.Ac_Drop = gpio_in.AC_Drop; // Chiller Alarm ping
  88. }
  89. #ifdef DD360ComBox
  90. EmgBtn_flag = 0;
  91. #else
  92. if (gpio_in.Emergency_Btn && (EmgBtn_flag != gpio_in.Emergency_Btn))
  93. {
  94. EmgBtn_count++;
  95. if (EmgBtn_count > SensorTrigCount) {
  96. EmgBtn_flag = 1;
  97. EmgBtn_count = 0; // Avoid Overflow
  98. }
  99. } else if (EmgBtn_flag != gpio_in.Emergency_Btn ) {
  100. EmgBtn_count++;
  101. if (EmgBtn_count > SensorTrigCount) {
  102. EmgBtn_flag = 0;
  103. EmgBtn_count = 0;
  104. }
  105. }
  106. #endif
  107. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = EmgBtn_flag;
  108. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key0);
  109. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key1 << 1);
  110. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key2 << 2);
  111. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key3 << 3);
  112. if (dispenserSwTmp != dispenserSw) {
  113. dispenserSw = dispenserSwTmp;
  114. log_info("Dispenser switch number = %d, bit = %d, %d, %d, %d",
  115. dispenserSw,
  116. ShmPrimaryMcuData->InputDet.bits.Key3,
  117. ShmPrimaryMcuData->InputDet.bits.Key2,
  118. ShmPrimaryMcuData->InputDet.bits.Key1,
  119. ShmPrimaryMcuData->InputDet.bits.Key0);
  120. }
  121. ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
  122. ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
  123. //#if defined DD360 || defined DD360Audi || defined DD360ComBox
  124. if ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  125. (strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  126. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0) ||
  127. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)
  128. ) {
  129. pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = ~gpio_in.AC_Connector;
  130. pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault = ~gpio_in.AC_MainBreaker;
  131. } else {
  132. //pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = gpio_in.AC_Connector;
  133. //pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault = gpio_in.AC_MainBreaker;
  134. }
  135. #if defined DD360ComBox
  136. Door_flag = 0;
  137. #else
  138. if (Door_flag == gpio_in.Door_Open) {
  139. Door_count++;
  140. if (Door_count == 3 ) {
  141. Door_count = 0;
  142. Door_flag = gpio_in.Door_Open;
  143. }
  144. } else {
  145. Door_flag = gpio_in.Door_Open;
  146. Door_count = 0;
  147. }
  148. /*
  149. if (gpio_in.Door_Open == 0 && (Door_flag == gpio_in.Door_Open))
  150. {
  151. Door_count++;
  152. if (Door_count > SensorTrigCount) {
  153. Door_flag = 1;
  154. Door_count = 0; // Avoid Overflow
  155. }
  156. } else if (gpio_in.Door_Open && Door_flag) {
  157. Door_count++;
  158. if (Door_count > SensorTrigCount) {
  159. Door_flag = 0;
  160. Door_count = 0;
  161. }
  162. }
  163. */
  164. #endif
  165. #if defined DD360ComBox
  166. ShmPrimaryMcuData->InputDet.bits.DoorOpen = Door_flag;
  167. #else
  168. ShmPrimaryMcuData->InputDet.bits.DoorOpen = ~Door_flag;
  169. #endif
  170. /*
  171. log_info("Emergency Button Count = %d , Emergency flag = %d",
  172. EmgBtn_count,EmgBtn_flag);
  173. log_info("Door Sensor Count = %d , Door Sensor flag = %d",
  174. Door_count,Door_flag);
  175. */
  176. ShmPrimaryMcuData->InputDet.bits.Key0 = ~gpio_in.Key[0] & 0x01;
  177. ShmPrimaryMcuData->InputDet.bits.Key1 = ~gpio_in.Key[1] & 0x01;
  178. ShmPrimaryMcuData->InputDet.bits.Key2 = ~gpio_in.Key[2] & 0x01;
  179. ShmPrimaryMcuData->InputDet.bits.Key3 = ~gpio_in.Key[3] & 0x01;
  180. return;
  181. }
  182. static void checkChillerStatus(Gpio_out *gpio)
  183. {
  184. uint8_t gunIndex = 0;
  185. uint8_t chillerCount = 0;
  186. struct ChargingInfoData *pDcChargingInfo = NULL;
  187. static ChillerInfo fChillerInfo[2] = {0}, *pChillerInfo = NULL;
  188. static ChillerInfo _chiller;
  189. struct FanModuleData* ShmFanModuleData = (struct FanModuleData*)GetShmFanModuleData();
  190. DcCommonInfo* ShmDcCommonData = (DcCommonInfo*)GetShmDcCommonData();
  191. Gpio_out *pGpio = (Gpio_out *)gpio;
  192. if ((strncmp((char*)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  193. (strncmp((char*)&pSysConfig->ModelName[7], "F", 1) == 0)) {
  194. chillerCount++;
  195. ShmDcCommonData->pGunInfo[0].withChiller = TRUE;
  196. }
  197. if ((strncmp((char*)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  198. (strncmp((char*)&pSysConfig->ModelName[9], "F", 1) == 0)) {
  199. chillerCount++;
  200. ShmDcCommonData->pGunInfo[1].withChiller = TRUE;
  201. }
  202. if (chillerCount == 0) {
  203. pGpio->AC_Connector = 0x00;
  204. return;
  205. }
  206. // 設定chiller 開關
  207. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  208. if (!ShmDcCommonData->pGunInfo[gunIndex].withChiller)
  209. continue;
  210. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  211. pChillerInfo = (ChillerInfo *)&fChillerInfo[gunIndex];
  212. if((pDcChargingInfo->SystemStatus > S_IDLE && pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  213. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 && pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  214. pChillerInfo->ChillerSwitch = YES;
  215. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  216. } else {
  217. if(pChillerInfo->ChillerSwitch == YES) {
  218. //10分鐘後停止
  219. if ((time((time_t *)NULL) - pChillerInfo->ChillerOnTime) >= 600) {
  220. pChillerInfo->ChillerSwitch = NO;
  221. ShmDcCommonData->FanOnTime = time((time_t*)NULL);
  222. }
  223. } else {
  224. pChillerInfo->ChillerSwitch = NO;
  225. ShmFanModuleData->SetFan1Speed = 0;
  226. }
  227. }
  228. // 檢查Chiller溫度點,若小於零下時開啟heater,大於10度時關閉heater
  229. if (pDcChargingInfo->ChillerTemp < 70) {
  230. pGpio->AC_Breaker = YES;
  231. } else if(pDcChargingInfo->ChillerTemp > 75) {
  232. pGpio->AC_Breaker = NO;
  233. }
  234. //log_info("Gun%d Set Heater %s", gunIndex, pGpio->AC_Breaker ? "ON" : "OFF");
  235. }
  236. uint8_t _chillerNeedOn = NO;
  237. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++)
  238. {
  239. pChillerInfo = (ChillerInfo*)&fChillerInfo[gunIndex];
  240. if (pChillerInfo->ChillerSwitch == YES) {
  241. _chillerNeedOn = YES;
  242. ShmFanModuleData->SetFan1Speed = 7000;
  243. }
  244. }
  245. /*
  246. if (ShmDcCommonData->debugflag == YES)
  247. _chillerNeedOn = ShmDcCommonData->chillerCtrl;
  248. */
  249. if (ShmPrimaryMcuData->InputDet.bits.Ac_Drop == ABNORMAL) {
  250. _chillerNeedOn = NO;
  251. }
  252. if(_chiller.ChillerSwitch != _chillerNeedOn) {
  253. log_info("Chiller Need Turn %s", _chillerNeedOn == YES ? "ON" : "OFF");
  254. }
  255. _chiller.ChillerSwitch = _chillerNeedOn;
  256. pGpio->AC_Connector = _chiller.ChillerSwitch;//Chiller ON/OFF Control, "0: Chiller disable, 1: Chiller enable"
  257. }
  258. void SetOutputGpio(int fd, uint8_t outputValue)
  259. {
  260. Gpio_out gpio;
  261. LedConfig *pLedConfig = (LedConfig *)&outputValue;
  262. static uint8_t flash = NO;
  263. if (strcmp((char *)pSysInfo->LcmHwRev, " ") == 0x00) {
  264. if (flash == NO) {
  265. flash = YES;
  266. } else {
  267. flash = NO;
  268. }
  269. } else {
  270. if (flash == NO) {
  271. flash = YES;
  272. }
  273. }
  274. pLedConfig->LeftButtonLed = flash;
  275. pLedConfig->RightButtonLed = flash;
  276. gpio.Button_LED[0] = pLedConfig->LeftButtonLed;
  277. gpio.Button_LED[1] = pLedConfig->RightButtonLed;
  278. #if defined DD360ComBox
  279. gpio.System_LED[0] = pLedConfig->GreenLED;
  280. gpio.System_LED[1] = pLedConfig->YellowLED;
  281. gpio.System_LED[2] = pLedConfig->RedLED;
  282. gpio.System_LED[3] = 0x00;
  283. #else
  284. if (_isNeedChkTilt) {
  285. gpio.System_LED[0] = 0x00;
  286. gpio.System_LED[1] = 0x00;
  287. gpio.System_LED[2] = tilt;
  288. gpio.System_LED[3] = tilt;
  289. }
  290. #endif
  291. checkChillerStatus(&gpio);
  292. //gpio.AC_Breaker = 0x01;
  293. Config_Gpio_Output(fd, OP_ADDR_IO_EXTEND, &gpio);
  294. }
  295. void SetRtcData(int fd)
  296. {
  297. Rtc rtc = {0};
  298. struct timeb csuTime;
  299. struct tm *tmCSU;
  300. ftime(&csuTime);
  301. tmCSU = localtime(&csuTime.time);
  302. //log_info("Time : %04d-%02d-%02d %02d:%02d:%02d ",
  303. // tmCSU->tm_year + 1900,
  304. // tmCSU->tm_mon + 1,
  305. // tmCSU->tm_mday,
  306. // tmCSU->tm_hour,
  307. // tmCSU->tm_min,
  308. // tmCSU->tm_sec);
  309. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  310. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  311. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  312. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  313. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  314. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  315. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  316. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  317. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  318. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  319. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  320. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  321. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  322. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  323. if (Config_Rtc_Data(fd, OP_ADDR_IO_EXTEND, &rtc) == PASS) {
  324. //log_info("SetRtc sucessfully. ");
  325. } else {
  326. //log_info("SetRtc fail. ");
  327. }
  328. }
  329. void SetModelName(int fd)
  330. {
  331. if (Config_Model_Name(fd, OP_ADDR_IO_EXTEND, pSysConfig->ModelName) == PASS) {
  332. }
  333. }
  334. //================================================
  335. // Main process
  336. //================================================
  337. int InitComPort()
  338. {
  339. int fd;
  340. struct termios tios;
  341. fd = open(priPortName, O_RDWR);
  342. if (fd <= 0) {
  343. #ifdef SystemLogMessage
  344. log_error("open 407 Communication port NG ");
  345. #endif
  346. return -1;
  347. }
  348. ioctl (fd, TCGETS, &tios);
  349. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  350. tios.c_lflag = 0;
  351. tios.c_iflag = 0;
  352. tios.c_oflag = 0;
  353. tios.c_cc[VMIN] = 0;
  354. tios.c_cc[VTIME] = (uint8_t)1;
  355. tios.c_lflag = 0;
  356. tcflush(fd, TCIFLUSH);
  357. ioctl (fd, TCSETS, &tios);
  358. return fd;
  359. }
  360. int GetTimeoutValue(struct timespec *startTime)
  361. {
  362. struct timespec endTime;
  363. clock_gettime(CLOCK_MONOTONIC_COARSE, &endTime);
  364. return endTime.tv_sec - startTime->tv_sec;
  365. }
  366. void GetTimespecFunc(struct timespec *time)
  367. {
  368. clock_gettime(CLOCK_MONOTONIC_COARSE, time);
  369. }
  370. static bool IsPrimaryProcessNeedPause(void)
  371. {
  372. bool _pause = false;
  373. static bool isPause = false;
  374. struct ChargingInfoData *pDcChargingInfo = NULL;
  375. for (uint8_t i = 0; i < pSysConfig->TotalConnectorCount; i++)
  376. {
  377. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  378. if(pDcChargingInfo->SystemStatus == S_UPDATE)
  379. {
  380. _pause = true;
  381. }
  382. }
  383. if(isPause != _pause)
  384. {
  385. log_info("Primary Process Now Is %s ", _pause == true ? "Paused" : "Continued");
  386. }
  387. isPause = _pause;
  388. return _pause;
  389. }
  390. bool CheckCustomer()
  391. {
  392. bool result = false;
  393. char _buf[3] = {0};
  394. memcpy(_buf, &pSysConfig->ModelName[12], 2);
  395. if (strcmp(_buf, "OL") == EQUAL)
  396. result = true;
  397. return result;
  398. }
  399. void TiltSensorSwitch(bool _switch)
  400. {
  401. if (tilt != _switch)
  402. tilt = _switch;
  403. }
  404. bool isChargingStatus(unsigned char status)
  405. {
  406. if ((status >= S_REASSIGN_CHECK && status <= S_ALARM) ||
  407. status == S_CCS_PRECHARGE_ST0 || status == S_CCS_PRECHARGE_ST1 ) {
  408. return TRUE;
  409. } else {
  410. return FALSE;
  411. }
  412. }
  413. void ChkTiltSensor(fd)
  414. {
  415. struct ChargingInfoData *pDcChargingInfo = NULL;
  416. bool isCharing = NO;
  417. Gpio_in gpio_in = {0};
  418. if (Query_Gpio_Input(fd, OP_ADDR_IO_EXTEND, &gpio_in) != PASS) {
  419. return;
  420. }
  421. for (uint8_t i = 0; i < pSysConfig->TotalConnectorCount; i++)
  422. {
  423. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  424. if (isChargingStatus(pDcChargingInfo->SystemStatus) || ShmPrimaryMcuData->SelfTest_Comp != PASS) {
  425. isCharing = YES;
  426. }
  427. }
  428. if (isCharing == YES)
  429. {
  430. if (_chargeSwitch == NO)
  431. {
  432. if (ShmDcCommonData->_tiltSensorStep == _TILT_SENSOR_STEP_NONE)
  433. {
  434. log_info("---- Tilt Sensor : _TILT_SENSOR_STEP_PIN_ON ---- ");
  435. ShmDcCommonData->_tiltSensorStep = _TILT_SENSOR_STEP_PIN_ON;
  436. _chargeSwitch = YES;
  437. }
  438. }
  439. else
  440. {
  441. switch (ShmDcCommonData->_tiltSensorStep)
  442. {
  443. case _TILT_SENSOR_STEP_PIN_ON:
  444. {
  445. TiltSensorSwitch(YES);
  446. GetTimespecFunc(&_tilt_time);
  447. log_info("---- Tilt Sensor : _TILT_SENSOR_STEP_PIN_WAIT ---- ");
  448. ShmDcCommonData->_tiltSensorStep = _TILT_SENSOR_STEP_PIN_WAIT;
  449. }
  450. break;
  451. case _TILT_SENSOR_STEP_PIN_WAIT:
  452. {
  453. int _time = GetTimeoutValue(&_tilt_time);
  454. if (_time < 0)
  455. GetTimespecFunc(&_tilt_time);
  456. else if (_time > 1)
  457. {
  458. if (gpio_in.Door_Open == NO)
  459. pAlarmCode->AlarmEvents.bits.TiltSensorStestFail = YES;
  460. else
  461. pAlarmCode->AlarmEvents.bits.TiltSensorStestFail = NO;
  462. if (_time > TILTSENSORDET)
  463. {
  464. GetTimespecFunc(&_tilt_time);
  465. log_info("---- Tilt Sensor : _TILT_SENSOR_STEP_PIN_OFF ---- ");
  466. log_info("---- Tilt Sensor : _TILT_SENSOR_TEST %s ---- ",
  467. pAlarmCode->AlarmEvents.bits.TiltSensorStestFail ? "FAIL" : "PASS");
  468. ShmDcCommonData->_tiltSensorStep = _TILT_SENSOR_STEP_PIN_OFF;
  469. }
  470. }
  471. }
  472. break;
  473. case _TILT_SENSOR_STEP_PIN_OFF:
  474. {
  475. TiltSensorSwitch(NO);
  476. GetTimespecFunc(&_tilt_time);
  477. log_info("---- Tilt Sensor : _TILT_SENSOR_STEP_PIN_FINISH ---- ");
  478. ShmDcCommonData->_tiltSensorStep = _TILT_SENSOR_STEP_PIN_FINISH;
  479. }
  480. break;
  481. case _TILT_SENSOR_STEP_PIN_FINISH :
  482. {
  483. int _time = GetTimeoutValue(&_tilt_time);
  484. if (_time < 0)
  485. GetTimespecFunc(&_tilt_time);
  486. else if (_time > TILTSENSORDET)
  487. {
  488. log_info("---- Tilt Sensor : _TILT_SENSOR_STEP_NONE ---- ");
  489. ShmDcCommonData->_tiltSensorStep = _TILT_SENSOR_STEP_NONE;
  490. ShmPrimaryMcuData->SelfTest_Comp = PASS;
  491. }
  492. }
  493. break;
  494. }
  495. }
  496. }
  497. else
  498. {
  499. TiltSensorSwitch(NO);
  500. ShmDcCommonData->_tiltSensorStep = _TILT_SENSOR_STEP_NONE;
  501. _chargeSwitch = NO;
  502. //log_info("ShmPrimaryMcuData->SelfTest_Comp:%d",ShmPrimaryMcuData->SelfTest_Comp);
  503. }
  504. return;
  505. }
  506. int main(void)
  507. {
  508. int Uart1Fd = -1;
  509. //if (InitShareMemory() == FAIL) {
  510. // log_error("InitShareMemory NG");
  511. // if (ShmStatusCodeData != NULL) {
  512. // pAlarmCode->AlarmEvents.bits.FailToCreateShareMemory = 1;
  513. // }
  514. // sleep(5);
  515. // return 0;
  516. //}
  517. if (CreateAllCsuShareMemory() == FAIL) {
  518. log_error("create share memory error");
  519. return FAIL;
  520. }
  521. MappingGunChargingInfo("Primary Task");
  522. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  523. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  524. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  525. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  526. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  527. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  528. Uart1Fd = InitComPort();
  529. //log_info("407 Port id = %d ", Uart1Fd);
  530. if (Uart1Fd < 0) {
  531. log_error("InitComPort (Uart1 : AM3352 - STM32) NG");
  532. if (pAlarmCode != NULL) {
  533. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  534. }
  535. sleep(5);
  536. return 0;
  537. }
  538. SetRtcData(Uart1Fd);
  539. SetModelName(Uart1Fd);
  540. gun_count = pSysConfig->TotalConnectorCount;
  541. //Initialization();
  542. // 需要判斷這個的客戶碼 : OL
  543. //_isNeedChkTilt = CheckCustomer();
  544. for (;;) {
  545. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  546. // 模組更新 FW 後,需重新做
  547. if(IsPrimaryProcessNeedPause() == true)
  548. {
  549. sleep(1);
  550. continue;
  551. }
  552. if (ShmPrimaryMcuData->SelfTest_Comp != PASS) {
  553. //log_info("(407) Get Fw and Hw Ver. ");
  554. if (GetFwAndHwVersion(Uart1Fd) != PASS ) {
  555. sleep(1);
  556. continue;
  557. }
  558. if (_isNeedChkTilt) {
  559. ChkTiltSensor(Uart1Fd);
  560. } else {
  561. ShmPrimaryMcuData->SelfTest_Comp = PASS;
  562. }
  563. } else {
  564. if (_isNeedChkTilt) {
  565. ChkTiltSensor(Uart1Fd);
  566. }
  567. SetOutputGpio(Uart1Fd, ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0]);
  568. GetInputGpioStatus(Uart1Fd);
  569. }
  570. usleep(50000);
  571. }
  572. return FAIL;
  573. }