Module_PrimaryComm.c 15 KB

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