Module_PrimaryComm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. //================================================
  52. // Function
  53. //================================================
  54. void GetFwAndHwVersion(int fd)
  55. {
  56. Ver ver = {0};
  57. if (Query_FW_Ver(fd, OP_ADDR_IO_EXTEND, &ver) == PASS) {
  58. //log_info("Primary FW Rev = %s ", ver.Version_FW);
  59. strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
  60. strcpy((char *) pSysInfo->CsuPrimFwRev, ver.Version_FW);
  61. }
  62. //if (Query_HW_Ver(fd, OP_ADDR_IO_EXTEND, &ver) == PASS)
  63. // ;//log_info("Primary HW Rev = %s ", ver.Version_HW);
  64. }
  65. void GetInputGpioStatus(int fd)
  66. {
  67. uint8_t dispenserSwTmp = 0;
  68. Gpio_in gpio_in = {0};
  69. static uint8_t dispenserSw = 0;
  70. //log_info("GetInputGpioStatus ");
  71. if (Query_Gpio_Input(fd, OP_ADDR_IO_EXTEND, &gpio_in) != PASS) {
  72. return;
  73. }
  74. ShmPrimaryMcuData->InputDet.bits.SpdDetec = gpio_in.SPD;
  75. //log_info("gpio_in.AC_Drop:%d", gpio_in.AC_Drop);
  76. ShmPrimaryMcuData->InputDet.bits.Ac_Drop = gpio_in.AC_Drop; // Chiller Alarm ping
  77. #ifdef DD360ComBox
  78. EmgBtn_flag = 0;
  79. #else
  80. if (gpio_in.Emergency_Btn && (EmgBtn_flag != gpio_in.Emergency_Btn))
  81. {
  82. EmgBtn_count++;
  83. if (EmgBtn_count > SensorTrigCount) {
  84. EmgBtn_flag = 1;
  85. EmgBtn_count = 0; // Avoid Overflow
  86. }
  87. } else if (EmgBtn_flag != gpio_in.Emergency_Btn ) {
  88. EmgBtn_count++;
  89. if (EmgBtn_count > SensorTrigCount) {
  90. EmgBtn_flag = 0;
  91. EmgBtn_count = 0;
  92. }
  93. }
  94. #endif
  95. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = EmgBtn_flag;
  96. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key0);
  97. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key1 << 1);
  98. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key2 << 2);
  99. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key3 << 3);
  100. if (dispenserSwTmp != dispenserSw) {
  101. dispenserSw = dispenserSwTmp;
  102. log_info("Dispenser switch number = %d, bit = %d, %d, %d, %d",
  103. dispenserSw,
  104. ShmPrimaryMcuData->InputDet.bits.Key3,
  105. ShmPrimaryMcuData->InputDet.bits.Key2,
  106. ShmPrimaryMcuData->InputDet.bits.Key1,
  107. ShmPrimaryMcuData->InputDet.bits.Key0);
  108. }
  109. ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
  110. ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
  111. //#if defined DD360 || defined DD360Audi || defined DD360ComBox
  112. if ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  113. (strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  114. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0) ||
  115. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)
  116. ) {
  117. pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = ~gpio_in.AC_Connector;
  118. pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault = ~gpio_in.AC_MainBreaker;
  119. } else {
  120. pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = gpio_in.AC_Connector;
  121. pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault = gpio_in.AC_MainBreaker;
  122. }
  123. #if defined DD360ComBox
  124. Door_flag = 0;
  125. #else
  126. if (Door_flag == gpio_in.Door_Open) {
  127. Door_count++;
  128. if (Door_count == 3 ) {
  129. Door_count = 0;
  130. Door_flag = gpio_in.Door_Open;
  131. }
  132. } else {
  133. Door_flag = gpio_in.Door_Open;
  134. Door_count = 0;
  135. }
  136. /*
  137. if (gpio_in.Door_Open == 0 && (Door_flag == gpio_in.Door_Open))
  138. {
  139. Door_count++;
  140. if (Door_count > SensorTrigCount) {
  141. Door_flag = 1;
  142. Door_count = 0; // Avoid Overflow
  143. }
  144. } else if (gpio_in.Door_Open && Door_flag) {
  145. Door_count++;
  146. if (Door_count > SensorTrigCount) {
  147. Door_flag = 0;
  148. Door_count = 0;
  149. }
  150. }
  151. */
  152. #endif
  153. #if defined DD360ComBox
  154. ShmPrimaryMcuData->InputDet.bits.DoorOpen = Door_flag;
  155. #else
  156. ShmPrimaryMcuData->InputDet.bits.DoorOpen = ~Door_flag;
  157. #endif
  158. /*
  159. log_info("Emergency Button Count = %d , Emergency flag = %d",
  160. EmgBtn_count,EmgBtn_flag);
  161. log_info("Door Sensor Count = %d , Door Sensor flag = %d",
  162. Door_count,Door_flag);
  163. */
  164. ShmPrimaryMcuData->InputDet.bits.Key0 = ~gpio_in.Key[0] & 0x01;
  165. ShmPrimaryMcuData->InputDet.bits.Key1 = ~gpio_in.Key[1] & 0x01;
  166. ShmPrimaryMcuData->InputDet.bits.Key2 = ~gpio_in.Key[2] & 0x01;
  167. ShmPrimaryMcuData->InputDet.bits.Key3 = ~gpio_in.Key[3] & 0x01;
  168. return;
  169. }
  170. static void checkChillerStatus(Gpio_out *gpio)
  171. {
  172. uint8_t gunIndex = 0;
  173. uint8_t chillerCount = 0;
  174. struct ChargingInfoData *pDcChargingInfo = NULL;
  175. static ChillerInfo fChillerInfo[2] = {0}, *pChillerInfo = NULL;
  176. static ChillerInfo _chiller;
  177. struct FanModuleData* ShmFanModuleData = (struct FanModuleData*)GetShmFanModuleData();
  178. DcCommonInfo* ShmDcCommonData = (DcCommonInfo*)GetShmDcCommonData();
  179. Gpio_out *pGpio = (Gpio_out *)gpio;
  180. if ((strncmp((char*)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  181. (strncmp((char*)&pSysConfig->ModelName[7], "F", 1) == 0)) {
  182. chillerCount++;
  183. ShmDcCommonData->pGunInfo[0].withChiller = TRUE;
  184. }
  185. if ((strncmp((char*)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  186. (strncmp((char*)&pSysConfig->ModelName[9], "F", 1) == 0)) {
  187. chillerCount++;
  188. ShmDcCommonData->pGunInfo[1].withChiller = TRUE;
  189. }
  190. if (chillerCount == 0) {
  191. pGpio->AC_Connector = 0x00;
  192. return;
  193. }
  194. // 設定chiller 開關
  195. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  196. if (!ShmDcCommonData->pGunInfo[gunIndex].withChiller)
  197. continue;
  198. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  199. pChillerInfo = (ChillerInfo *)&fChillerInfo[gunIndex];
  200. if((pDcChargingInfo->SystemStatus > S_IDLE && pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  201. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 && pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1)) {
  202. pChillerInfo->ChillerSwitch = YES;
  203. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  204. } else {
  205. if(pChillerInfo->ChillerSwitch == YES) {
  206. //10分鐘後停止
  207. if ((time((time_t *)NULL) - pChillerInfo->ChillerOnTime) >= 600) {
  208. pChillerInfo->ChillerSwitch = NO;
  209. ShmDcCommonData->FanOnTime = time((time_t*)NULL);
  210. }
  211. } else {
  212. pChillerInfo->ChillerSwitch = NO;
  213. ShmFanModuleData->SetFan1Speed = 0;
  214. }
  215. }
  216. // 檢查Chiller溫度點,若小於零下時開啟heater,大於10度時關閉heater
  217. if (pDcChargingInfo->ChillerTemp < 70) {
  218. pGpio->AC_Breaker = YES;
  219. } else if(pDcChargingInfo->ChillerTemp > 75) {
  220. pGpio->AC_Breaker = NO;
  221. }
  222. //log_info("Gun%d Set Heater %s", gunIndex, pGpio->AC_Breaker ? "ON" : "OFF");
  223. }
  224. uint8_t _chillerNeedOn = NO;
  225. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++)
  226. {
  227. pChillerInfo = (ChillerInfo*)&fChillerInfo[gunIndex];
  228. if (pChillerInfo->ChillerSwitch == YES) {
  229. _chillerNeedOn = YES;
  230. ShmFanModuleData->SetFan1Speed = 7000;
  231. }
  232. }
  233. /*
  234. if (ShmDcCommonData->debugflag == YES)
  235. _chillerNeedOn = ShmDcCommonData->chillerCtrl;
  236. */
  237. if (ShmPrimaryMcuData->InputDet.bits.Ac_Drop == ABNORMAL) {
  238. _chillerNeedOn = NO;
  239. }
  240. if(_chiller.ChillerSwitch != _chillerNeedOn) {
  241. log_info("Chiller Need Turn %s", _chillerNeedOn == YES ? "ON" : "OFF");
  242. }
  243. _chiller.ChillerSwitch = _chillerNeedOn;
  244. pGpio->AC_Connector = _chiller.ChillerSwitch;//Chiller ON/OFF Control, "0: Chiller disable, 1: Chiller enable"
  245. }
  246. void SetOutputGpio(int fd, uint8_t outputValue)
  247. {
  248. Gpio_out gpio;
  249. LedConfig *pLedConfig = (LedConfig *)&outputValue;
  250. static uint8_t flash = NO;
  251. if (strcmp((char *)pSysInfo->LcmHwRev, " ") == 0x00) {
  252. if (flash == NO) {
  253. flash = YES;
  254. } else {
  255. flash = NO;
  256. }
  257. } else {
  258. if (flash == NO) {
  259. flash = YES;
  260. }
  261. }
  262. pLedConfig->LeftButtonLed = flash;
  263. pLedConfig->RightButtonLed = flash;
  264. gpio.Button_LED[0] = pLedConfig->LeftButtonLed;
  265. gpio.Button_LED[1] = pLedConfig->RightButtonLed;
  266. gpio.System_LED[0] = pLedConfig->GreenLED;
  267. gpio.System_LED[1] = pLedConfig->YellowLED;
  268. gpio.System_LED[2] = pLedConfig->RedLED;
  269. gpio.System_LED[3] = 0x00;
  270. checkChillerStatus(&gpio);
  271. //gpio.AC_Breaker = 0x01;
  272. Config_Gpio_Output(fd, OP_ADDR_IO_EXTEND, &gpio);
  273. }
  274. void SetRtcData(int fd)
  275. {
  276. Rtc rtc = {0};
  277. struct timeb csuTime;
  278. struct tm *tmCSU;
  279. ftime(&csuTime);
  280. tmCSU = localtime(&csuTime.time);
  281. //log_info("Time : %04d-%02d-%02d %02d:%02d:%02d ",
  282. // tmCSU->tm_year + 1900,
  283. // tmCSU->tm_mon + 1,
  284. // tmCSU->tm_mday,
  285. // tmCSU->tm_hour,
  286. // tmCSU->tm_min,
  287. // tmCSU->tm_sec);
  288. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  289. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  290. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  291. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  292. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  293. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  294. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  295. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  296. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  297. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  298. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  299. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  300. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  301. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  302. if (Config_Rtc_Data(fd, OP_ADDR_IO_EXTEND, &rtc) == PASS) {
  303. //log_info("SetRtc sucessfully. ");
  304. } else {
  305. //log_info("SetRtc fail. ");
  306. }
  307. }
  308. void SetModelName(int fd)
  309. {
  310. if (Config_Model_Name(fd, OP_ADDR_IO_EXTEND, pSysConfig->ModelName) == PASS) {
  311. }
  312. }
  313. //================================================
  314. // Main process
  315. //================================================
  316. int InitComPort()
  317. {
  318. int fd;
  319. struct termios tios;
  320. fd = open(priPortName, O_RDWR);
  321. if (fd <= 0) {
  322. #ifdef SystemLogMessage
  323. log_error("open 407 Communication port NG ");
  324. #endif
  325. return -1;
  326. }
  327. ioctl (fd, TCGETS, &tios);
  328. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  329. tios.c_lflag = 0;
  330. tios.c_iflag = 0;
  331. tios.c_oflag = 0;
  332. tios.c_cc[VMIN] = 0;
  333. tios.c_cc[VTIME] = (uint8_t)1;
  334. tios.c_lflag = 0;
  335. tcflush(fd, TCIFLUSH);
  336. ioctl (fd, TCSETS, &tios);
  337. return fd;
  338. }
  339. unsigned long GetTimeoutValue(struct timeval _sour_time)
  340. {
  341. struct timeval _end_time;
  342. gettimeofday(&_end_time, NULL);
  343. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  344. }
  345. unsigned long GetClockTimeoutValue(struct timespec _start_time)
  346. {
  347. struct timespec ts_end;
  348. unsigned long ret = 0;
  349. clock_gettime(CLOCK_MONOTONIC, &ts_end);
  350. ret = ((unsigned long)(ts_end.tv_sec - _start_time.tv_sec) * 1000000) + ((unsigned long)((ts_end.tv_nsec / 1000) - (_start_time.tv_nsec / 1000)));
  351. return ret;
  352. }
  353. static bool IsPrimaryProcessNeedPause(void)
  354. {
  355. bool _pause = false;
  356. static bool isPause = false;
  357. struct ChargingInfoData *pDcChargingInfo = NULL;
  358. for (uint8_t i = 0; i < pSysConfig->TotalConnectorCount; i++)
  359. {
  360. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  361. if(pDcChargingInfo->SystemStatus == S_UPDATE)
  362. {
  363. _pause = true;
  364. }
  365. }
  366. if(isPause != _pause)
  367. {
  368. log_info("Primary Process Now Is %s ", _pause == true ? "Paused" : "Continued");
  369. }
  370. isPause = _pause;
  371. return _pause;
  372. }
  373. int main(void)
  374. {
  375. int Uart1Fd = -1;
  376. //if (InitShareMemory() == FAIL) {
  377. // log_error("InitShareMemory NG");
  378. // if (ShmStatusCodeData != NULL) {
  379. // pAlarmCode->AlarmEvents.bits.FailToCreateShareMemory = 1;
  380. // }
  381. // sleep(5);
  382. // return 0;
  383. //}
  384. if (CreateAllCsuShareMemory() == FAIL) {
  385. log_error("create share memory error");
  386. return FAIL;
  387. }
  388. MappingGunChargingInfo("Primary Task");
  389. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  390. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  391. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  392. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  393. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  394. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  395. Uart1Fd = InitComPort();
  396. //log_info("407 Port id = %d ", Uart1Fd);
  397. if (Uart1Fd < 0) {
  398. log_error("InitComPort (Uart1 : AM3352 - STM32) NG");
  399. if (pAlarmCode != NULL) {
  400. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  401. }
  402. sleep(5);
  403. return 0;
  404. }
  405. SetRtcData(Uart1Fd);
  406. SetModelName(Uart1Fd);
  407. gun_count = pSysConfig->TotalConnectorCount;
  408. //Initialization();
  409. for (;;) {
  410. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  411. // 模組更新 FW 後,需重新做
  412. if(IsPrimaryProcessNeedPause() == true)
  413. {
  414. sleep(1);
  415. continue;
  416. }
  417. if (ShmPrimaryMcuData->SelfTest_Comp != PASS) {
  418. //log_info("(407) Get Fw and Hw Ver. ");
  419. GetFwAndHwVersion(Uart1Fd);
  420. sleep(1);
  421. ShmPrimaryMcuData->SelfTest_Comp = PASS;
  422. } else {
  423. SetOutputGpio(Uart1Fd, ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0]);
  424. GetInputGpioStatus(Uart1Fd);
  425. }
  426. usleep(50000);
  427. }
  428. return FAIL;
  429. }