Module_PrimaryComm.c 20 KB

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