Module_PrimaryComm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. Gpio_out *pGpio = (Gpio_out *)gpio;
  162. if ((strncmp((char *)&pSysConfig->ModelName[7], "V", 1) == 0) ||
  163. (strncmp((char *)&pSysConfig->ModelName[7], "F", 1) == 0)) {
  164. chillerCount++;
  165. }
  166. if ((strncmp((char *)&pSysConfig->ModelName[9], "V", 1) == 0) ||
  167. (strncmp((char *)&pSysConfig->ModelName[9], "F", 1) == 0)) {
  168. chillerCount++;
  169. }
  170. if (chillerCount == 0) {
  171. pGpio->AC_Connector = 0x00;
  172. return;
  173. }
  174. for (gunIndex = 0; gunIndex < chillerCount; gunIndex++) {
  175. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  176. pChillerInfo = (ChillerInfo *)&fChillerInfo[gunIndex];
  177. if((pDcChargingInfo->SystemStatus > S_AUTHORIZING && pDcChargingInfo->SystemStatus < S_TERMINATING) ||
  178. (pDcChargingInfo->SystemStatus >= S_CCS_PRECHARGE_ST0 && pDcChargingInfo->SystemStatus <= S_CCS_PRECHARGE_ST1))
  179. {
  180. pChillerInfo->ChillerSwitch = YES;
  181. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  182. }
  183. else
  184. {
  185. if(pChillerInfo->ChillerSwitch == YES)
  186. {
  187. //10分鐘後停止
  188. if ((time((time_t *)NULL) - pChillerInfo->ChillerOnTime) >= 600)
  189. {
  190. pChillerInfo->ChillerSwitch = NO;
  191. }
  192. }
  193. else
  194. {
  195. pChillerInfo->ChillerSwitch = NO;
  196. }
  197. }
  198. /*
  199. if ((pDcChargingInfo->PresentChargingCurrent) >= 150) { //當前電壓於150A,打開水冷機
  200. pChillerInfo->ChillerSwitch = YES;
  201. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  202. } else {
  203. if (pChillerInfo->ChillerSwitch == YES) {
  204. if ((pDcChargingInfo->PresentChargingCurrent) >= 100) { //判斷如果還是大於100A不變動
  205. pChillerInfo->ChillerSwitch = YES;
  206. pChillerInfo->ChillerOnTime = time((time_t *)NULL);
  207. } else {
  208. if ((time((time_t *)NULL) - pChillerInfo->ChillerOnTime) >= 600) { //5分鐘後停止
  209. pChillerInfo->ChillerSwitch = NO;
  210. } else {
  211. pChillerInfo->ChillerSwitch = YES;
  212. }
  213. }
  214. } else {
  215. pChillerInfo->ChillerSwitch = NO;
  216. }
  217. }
  218. */
  219. }
  220. uint8_t _chillerNeedOn = NO;
  221. for (gunIndex = 0; gunIndex < chillerCount; gunIndex++)
  222. {
  223. pChillerInfo = (ChillerInfo *)&fChillerInfo[gunIndex];
  224. if(pChillerInfo->ChillerSwitch == YES)
  225. {
  226. _chillerNeedOn = YES;
  227. }
  228. }
  229. if(_chiller.ChillerSwitch != _chillerNeedOn)
  230. {
  231. log_info("Chiller Need Turn %s", _chillerNeedOn == YES ? "ON" : "OFF");
  232. }
  233. _chiller.ChillerSwitch = _chillerNeedOn;
  234. pGpio->AC_Connector = _chiller.ChillerSwitch;//Chiller ON/OFF Control, "0: Chiller disable, 1: Chiller enable"
  235. }
  236. void SetOutputGpio(int fd, uint8_t outputValue)
  237. {
  238. Gpio_out gpio;
  239. LedConfig *pLedConfig = (LedConfig *)&outputValue;
  240. static uint8_t flash = NO;
  241. if (strcmp((char *)pSysInfo->LcmHwRev, " ") == 0x00) {
  242. if (flash == NO) {
  243. flash = YES;
  244. } else {
  245. flash = NO;
  246. }
  247. } else {
  248. if (flash == NO) {
  249. flash = YES;
  250. }
  251. }
  252. pLedConfig->LeftButtonLed = flash;
  253. pLedConfig->RightButtonLed = flash;
  254. gpio.Button_LED[0] = pLedConfig->LeftButtonLed;
  255. gpio.Button_LED[1] = pLedConfig->RightButtonLed;
  256. gpio.System_LED[0] = pLedConfig->GreenLED;
  257. gpio.System_LED[1] = pLedConfig->YellowLED;
  258. gpio.System_LED[2] = pLedConfig->RedLED;
  259. gpio.System_LED[3] = 0x00;
  260. checkChillerStatus(&gpio);
  261. gpio.AC_Breaker = 0x00;
  262. Config_Gpio_Output(fd, OP_ADDR_IO_EXTEND, &gpio);
  263. }
  264. void SetRtcData(int fd)
  265. {
  266. Rtc rtc = {0};
  267. struct timeb csuTime;
  268. struct tm *tmCSU;
  269. ftime(&csuTime);
  270. tmCSU = localtime(&csuTime.time);
  271. //log_info("Time : %04d-%02d-%02d %02d:%02d:%02d ",
  272. // tmCSU->tm_year + 1900,
  273. // tmCSU->tm_mon + 1,
  274. // tmCSU->tm_mday,
  275. // tmCSU->tm_hour,
  276. // tmCSU->tm_min,
  277. // tmCSU->tm_sec);
  278. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  279. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  280. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  281. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  282. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  283. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  284. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  285. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  286. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  287. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  288. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  289. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  290. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  291. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  292. if (Config_Rtc_Data(fd, OP_ADDR_IO_EXTEND, &rtc) == PASS) {
  293. //log_info("SetRtc sucessfully. ");
  294. } else {
  295. //log_info("SetRtc fail. ");
  296. }
  297. }
  298. void SetModelName(int fd)
  299. {
  300. if (Config_Model_Name(fd, OP_ADDR_IO_EXTEND, pSysConfig->ModelName) == PASS) {
  301. }
  302. }
  303. //================================================
  304. // Main process
  305. //================================================
  306. int InitComPort()
  307. {
  308. int fd;
  309. struct termios tios;
  310. fd = open(priPortName, O_RDWR);
  311. if (fd <= 0) {
  312. log_error("open 407 Communication port NG ");
  313. return -1;
  314. }
  315. ioctl (fd, TCGETS, &tios);
  316. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  317. tios.c_lflag = 0;
  318. tios.c_iflag = 0;
  319. tios.c_oflag = 0;
  320. tios.c_cc[VMIN] = 0;
  321. tios.c_cc[VTIME] = (uint8_t)1;
  322. tios.c_lflag = 0;
  323. tcflush(fd, TCIFLUSH);
  324. ioctl (fd, TCSETS, &tios);
  325. return fd;
  326. }
  327. unsigned long GetTimeoutValue(struct timeval _sour_time)
  328. {
  329. struct timeval _end_time;
  330. gettimeofday(&_end_time, NULL);
  331. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  332. }
  333. static bool IsPrimaryProcessNeedPause(void)
  334. {
  335. bool _pause = false;
  336. static bool isPause = false;
  337. struct ChargingInfoData *pDcChargingInfo = NULL;
  338. for (uint8_t i = 0; i < pSysConfig->TotalConnectorCount; i++)
  339. {
  340. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  341. if(pDcChargingInfo->SystemStatus == S_UPDATE)
  342. {
  343. _pause = true;
  344. }
  345. }
  346. if(isPause != _pause)
  347. {
  348. log_info("Primary Process Now Is %s ", _pause == true ? "Paused" : "Continued");
  349. }
  350. isPause = _pause;
  351. return _pause;
  352. }
  353. int main(void)
  354. {
  355. int Uart1Fd = -1;
  356. //if (InitShareMemory() == FAIL) {
  357. // log_error("InitShareMemory NG");
  358. // if (ShmStatusCodeData != NULL) {
  359. // pAlarmCode->AlarmEvents.bits.FailToCreateShareMemory = 1;
  360. // }
  361. // sleep(5);
  362. // return 0;
  363. //}
  364. if (CreateAllCsuShareMemory() == FAIL) {
  365. log_error("create share memory error");
  366. return FAIL;
  367. }
  368. MappingGunChargingInfo("Primary Task");
  369. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  370. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  371. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  372. pFaultCode = (struct FaultCodeData *)GetShmFaultCodeData();
  373. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  374. Uart1Fd = InitComPort();
  375. //log_info("407 Port id = %d ", Uart1Fd);
  376. if (Uart1Fd < 0) {
  377. log_error("InitComPort (Uart1 : AM3352 - STM32) NG");
  378. if (pAlarmCode != NULL) {
  379. pAlarmCode->AlarmEvents.bits.CsuInitFailed = 1;
  380. }
  381. sleep(5);
  382. return 0;
  383. }
  384. SetRtcData(Uart1Fd);
  385. SetModelName(Uart1Fd);
  386. gun_count = pSysConfig->TotalConnectorCount;
  387. //Initialization();
  388. for (;;) {
  389. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  390. // 模組更新 FW 後,需重新做
  391. if(IsPrimaryProcessNeedPause() == true)
  392. {
  393. sleep(1);
  394. continue;
  395. }
  396. if (ShmPrimaryMcuData->SelfTest_Comp != PASS) {
  397. //log_info("(407) Get Fw and Hw Ver. ");
  398. GetFwAndHwVersion(Uart1Fd);
  399. sleep(1);
  400. ShmPrimaryMcuData->SelfTest_Comp = PASS;
  401. } else {
  402. SetOutputGpio(Uart1Fd, ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0]);
  403. GetInputGpioStatus(Uart1Fd);
  404. }
  405. usleep(50000);
  406. }
  407. return FAIL;
  408. }