Module_PrimaryComm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. if (gpio_in.Emergency_Btn && (EmgBtn_flag != gpio_in.Emergency_Btn))
  76. {
  77. EmgBtn_count++;
  78. if (EmgBtn_count > SensorTrigCount) {
  79. EmgBtn_flag = 1;
  80. EmgBtn_count = 0; // Avoid Overflow
  81. }
  82. } else if (EmgBtn_flag != gpio_in.Emergency_Btn ) {
  83. EmgBtn_count++;
  84. if (EmgBtn_count > SensorTrigCount) {
  85. EmgBtn_flag = 0;
  86. EmgBtn_count = 0;
  87. }
  88. }
  89. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = EmgBtn_flag;
  90. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key0);
  91. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key1 << 1);
  92. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key2 << 2);
  93. dispenserSwTmp |= (ShmPrimaryMcuData->InputDet.bits.Key3 << 3);
  94. if (dispenserSwTmp != dispenserSw) {
  95. dispenserSw = dispenserSwTmp;
  96. log_info("Dispenser switch number = %d, bit = %d, %d, %d, %d",
  97. dispenserSw,
  98. ShmPrimaryMcuData->InputDet.bits.Key3,
  99. ShmPrimaryMcuData->InputDet.bits.Key2,
  100. ShmPrimaryMcuData->InputDet.bits.Key1,
  101. ShmPrimaryMcuData->InputDet.bits.Key0);
  102. }
  103. ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
  104. ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
  105. if ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  106. (strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  107. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0) ||
  108. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)
  109. ) {
  110. pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = ~gpio_in.AC_Connector;
  111. pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault = ~gpio_in.AC_MainBreaker;
  112. } else {
  113. pAlarmCode->AlarmEvents.bits.CcsLiquidChillerWaterLevelWarning = gpio_in.AC_Connector;
  114. pFaultCode->FaultEvents.bits.CcsLiquidChillerWaterLevelFault = gpio_in.AC_MainBreaker;
  115. }
  116. if (Door_flag == gpio_in.Door_Open) {
  117. Door_count++;
  118. if (Door_count == 3 ) {
  119. Door_count = 0;
  120. Door_flag = gpio_in.Door_Open;
  121. }
  122. } else {
  123. Door_flag = gpio_in.Door_Open;
  124. Door_count = 0;
  125. }
  126. /*
  127. if (gpio_in.Door_Open == 0 && (Door_flag == gpio_in.Door_Open))
  128. {
  129. Door_count++;
  130. if (Door_count > SensorTrigCount) {
  131. Door_flag = 1;
  132. Door_count = 0; // Avoid Overflow
  133. }
  134. } else if (gpio_in.Door_Open && Door_flag) {
  135. Door_count++;
  136. if (Door_count > SensorTrigCount) {
  137. Door_flag = 0;
  138. Door_count = 0;
  139. }
  140. }
  141. */
  142. ShmPrimaryMcuData->InputDet.bits.DoorOpen = ~Door_flag;
  143. /*
  144. log_info("Emergency Button Count = %d , Emergency flag = %d",
  145. EmgBtn_count,EmgBtn_flag);
  146. log_info("Door Sensor Count = %d , Door Sensor flag = %d",
  147. Door_count,Door_flag);
  148. */
  149. ShmPrimaryMcuData->InputDet.bits.Key0 = ~gpio_in.Key[0] & 0x01;
  150. ShmPrimaryMcuData->InputDet.bits.Key1 = ~gpio_in.Key[1] & 0x01;
  151. ShmPrimaryMcuData->InputDet.bits.Key2 = ~gpio_in.Key[2] & 0x01;
  152. ShmPrimaryMcuData->InputDet.bits.Key3 = ~gpio_in.Key[3] & 0x01;
  153. return;
  154. }
  155. static void checkChillerStatus(Gpio_out *gpio)
  156. {
  157. uint8_t gunIndex = 0;
  158. uint8_t chillerCount = 0;
  159. struct ChargingInfoData *pDcChargingInfo = NULL;
  160. static ChillerInfo fChillerInfo[2] = {0}, *pChillerInfo = NULL;
  161. static ChillerInfo _chiller;
  162. struct FanModuleData* ShmFanModuleData = (struct FanModuleData*)GetShmFanModuleData();
  163. DcCommonInfo* ShmDcCommonData = (DcCommonInfo*)GetShmDcCommonData();
  164. Gpio_out *pGpio = (Gpio_out *)gpio;
  165. if ((strncmp((char*)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  166. (strncmp((char*)&pSysConfig->ModelName[7], "F", 1) == 0)) {
  167. chillerCount++;
  168. ShmDcCommonData->pGunInfo[0].withChiller = TRUE;
  169. }
  170. if ((strncmp((char*)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  171. (strncmp((char*)&pSysConfig->ModelName[9], "F", 1) == 0)) {
  172. chillerCount++;
  173. ShmDcCommonData->pGunInfo[1].withChiller = TRUE;
  174. }
  175. if (chillerCount == 0) {
  176. pGpio->AC_Connector = 0x00;
  177. return;
  178. }
  179. for (gunIndex = 0; gunIndex < chillerCount; gunIndex++) {
  180. if (!ShmDcCommonData->pGunInfo[gunIndex].withChiller)
  181. continue;
  182. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  183. pChillerInfo = (ChillerInfo *)&fChillerInfo[gunIndex];
  184. if((pDcChargingInfo->SystemStatus > S_AUTHORIZING && pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  185. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 && pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))
  186. {
  187. pChillerInfo->ChillerSwitch = YES;
  188. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  189. }
  190. else
  191. {
  192. if(pChillerInfo->ChillerSwitch == YES)
  193. {
  194. //10分鐘後停止
  195. if ((time((time_t *)NULL) - pChillerInfo->ChillerOnTime) >= 600)
  196. {
  197. pChillerInfo->ChillerSwitch = NO;
  198. ShmFanModuleData->SetFan1Speed = 0;
  199. }
  200. }
  201. else
  202. {
  203. pChillerInfo->ChillerSwitch = NO;
  204. ShmFanModuleData->SetFan1Speed = 0;
  205. }
  206. }
  207. /*
  208. if ((pDcChargingInfo->PresentChargingCurrent) >= 150) { //當前電壓於150A,打開水冷機
  209. pChillerInfo->ChillerSwitch = YES;
  210. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  211. } else {
  212. if (pChillerInfo->ChillerSwitch == YES) {
  213. if ((pDcChargingInfo->PresentChargingCurrent) >= 100) { //判斷如果還是大於100A不變動
  214. pChillerInfo->ChillerSwitch = YES;
  215. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  216. } else {
  217. if ((time((time_t *)NULL) - pChillerInfo->ChillerOnTime) >= 600) { //5分鐘後停止
  218. pChillerInfo->ChillerSwitch = NO;
  219. } else {
  220. pChillerInfo->ChillerSwitch = YES;
  221. }
  222. }
  223. } else {
  224. pChillerInfo->ChillerSwitch = NO;
  225. }
  226. }
  227. */
  228. }
  229. uint8_t _chillerNeedOn = NO;
  230. for (gunIndex = 0; gunIndex < chillerCount; gunIndex++)
  231. {
  232. pChillerInfo = (ChillerInfo *)&fChillerInfo[gunIndex];
  233. if(pChillerInfo->ChillerSwitch == YES)
  234. {
  235. _chillerNeedOn = YES;
  236. ShmFanModuleData->SetFan1Speed = 7000;
  237. }
  238. }
  239. if(_chiller.ChillerSwitch != _chillerNeedOn)
  240. {
  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 = 0x00;
  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. log_error("open 407 Communication port NG ");
  323. return -1;
  324. }
  325. ioctl (fd, TCGETS, &tios);
  326. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  327. tios.c_lflag = 0;
  328. tios.c_iflag = 0;
  329. tios.c_oflag = 0;
  330. tios.c_cc[VMIN] = 0;
  331. tios.c_cc[VTIME] = (uint8_t)1;
  332. tios.c_lflag = 0;
  333. tcflush(fd, TCIFLUSH);
  334. ioctl (fd, TCSETS, &tios);
  335. return fd;
  336. }
  337. unsigned long GetTimeoutValue(struct timeval _sour_time)
  338. {
  339. struct timeval _end_time;
  340. gettimeofday(&_end_time, NULL);
  341. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  342. }
  343. unsigned long GetClockTimeoutValue(struct timespec _start_time)
  344. {
  345. struct timespec ts_end;
  346. unsigned long ret = 0;
  347. clock_gettime(CLOCK_MONOTONIC, &ts_end);
  348. ret = ((unsigned long)(ts_end.tv_sec - _start_time.tv_sec) * 1000000) + ((unsigned long)((ts_end.tv_nsec / 1000) - (_start_time.tv_nsec / 1000)));
  349. return ret;
  350. }
  351. static bool IsPrimaryProcessNeedPause(void)
  352. {
  353. bool _pause = false;
  354. static bool isPause = false;
  355. struct ChargingInfoData *pDcChargingInfo = NULL;
  356. for (uint8_t i = 0; i < pSysConfig->TotalConnectorCount; i++)
  357. {
  358. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  359. if(pDcChargingInfo->SystemStatus == S_UPDATE)
  360. {
  361. _pause = true;
  362. }
  363. }
  364. if(isPause != _pause)
  365. {
  366. log_info("Primary Process Now Is %s ", _pause == true ? "Paused" : "Continued");
  367. }
  368. isPause = _pause;
  369. return _pause;
  370. }
  371. int main(void)
  372. {
  373. int Uart1Fd = -1;
  374. //if (InitShareMemory() == FAIL) {
  375. // log_error("InitShareMemory NG");
  376. // if (ShmStatusCodeData != NULL) {
  377. // pAlarmCode->AlarmEvents.bits.FailToCreateShareMemory = 1;
  378. // }
  379. // sleep(5);
  380. // return 0;
  381. //}
  382. if (CreateAllCsuShareMemory() == FAIL) {
  383. log_error("create share memory error");
  384. return FAIL;
  385. }
  386. MappingGunChargingInfo("Primary Task");
  387. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  388. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  389. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  390. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  391. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  392. ShmDcCommonData = (DcCommonInfo*)GetShmDcCommonData();
  393. Uart1Fd = InitComPort();
  394. //log_info("407 Port id = %d ", Uart1Fd);
  395. if (Uart1Fd < 0) {
  396. log_error("InitComPort (Uart1 : AM3352 - STM32) NG");
  397. if (pAlarmCode != NULL) {
  398. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  399. }
  400. sleep(5);
  401. return 0;
  402. }
  403. SetRtcData(Uart1Fd);
  404. SetModelName(Uart1Fd);
  405. gun_count = pSysConfig->TotalConnectorCount;
  406. //Initialization();
  407. for (;;) {
  408. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  409. // 模組更新 FW 後,需重新做
  410. if(IsPrimaryProcessNeedPause() == true)
  411. {
  412. sleep(1);
  413. continue;
  414. }
  415. if (ShmPrimaryMcuData->SelfTest_Comp != PASS) {
  416. //log_info("(407) Get Fw and Hw Ver. ");
  417. GetFwAndHwVersion(Uart1Fd);
  418. sleep(1);
  419. ShmPrimaryMcuData->SelfTest_Comp = PASS;
  420. } else {
  421. SetOutputGpio(Uart1Fd, ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0]);
  422. GetInputGpioStatus(Uart1Fd);
  423. }
  424. usleep(50000);
  425. }
  426. return FAIL;
  427. }