Module_PrimaryComm.c 15 KB

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