Module_PrimaryComm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h> /*標準輸入輸出定義*/
  18. #include <stdlib.h> /*標準函數庫定義*/
  19. #include <unistd.h> /*Unix 標準函數定義*/
  20. #include <fcntl.h> /*檔控制定義*/
  21. #include <termios.h> /*PPSIX 終端控制定義*/
  22. #include <errno.h> /*錯誤號定義*/
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include "../../define.h"
  30. #include "PrimaryComm.h"
  31. #include <stdbool.h>
  32. #include "Config.h"
  33. #include "Common.h"
  34. #define COMM_FAIL_COUNT 10
  35. #define STATE_CHANGE_COUNT 3
  36. typedef unsigned char byte;
  37. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  38. struct StatusCodeData *ShmStatusCodeData;
  39. struct PrimaryMcuData *ShmPrimaryMcuData;
  40. ChargerInfoData *ShmChargerInfo;
  41. void trim(char *s);
  42. int mystrcmp(char *p1,char *p2);
  43. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  44. void split(char **arr, char *str, const char *del);
  45. int Uart1Fd;
  46. char *priPortName = "/dev/ttyS1";
  47. Ver ver;
  48. Gpio_in gpio_in;
  49. Rtc rtc;
  50. byte _OutputDrv = 0;
  51. byte _acStatus = 0;
  52. byte _acChkCount = 0;
  53. int _CommFailCount = 0;
  54. int _CabinetSwitch = -1;
  55. int _TempSwitch = -1;
  56. int _TempSPD = -1;
  57. int _TempDoor = -1;
  58. int _TempEmg = -1;
  59. byte _SwitchCnt = 0;
  60. byte _SPDCnt = 0;
  61. byte _DoorCnt = 0;
  62. byte _EmgBtnCnt = 0;
  63. int DiffTimeb(struct timeb ST, struct timeb ET)
  64. {
  65. //return milli-second
  66. unsigned int StartTime,StopTime;
  67. StartTime=(unsigned int)ST.time;
  68. StopTime=(unsigned int)ET.time;
  69. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  70. }
  71. //=================================
  72. // Common routine
  73. //=================================
  74. char* getTimeString(void)
  75. {
  76. char *result=malloc(21);
  77. time_t timep;
  78. struct tm *p;
  79. time(&timep);
  80. p=gmtime(&timep);
  81. sprintf(result, "[%04d-%02d-%02d %02d:%02d:%02d]", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_hour, p->tm_sec);
  82. return result;
  83. }
  84. void trim(char *s)
  85. {
  86. int i=0, j, k, l=0;
  87. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  88. i++;
  89. j = strlen(s)-1;
  90. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  91. j--;
  92. if(i==0 && j==strlen(s)-1) { }
  93. else if(i==0) s[j+1] = '\0';
  94. else {
  95. for(k=i; k<=j; k++) s[l++] = s[k];
  96. s[l] = '\0';
  97. }
  98. }
  99. int mystrcmp(char *p1,char *p2)
  100. {
  101. while(*p1==*p2)
  102. {
  103. if(*p1=='\0' || *p2=='\0')
  104. break;
  105. p1++;
  106. p2++;
  107. }
  108. if(*p1=='\0' && *p2=='\0')
  109. return(PASS);
  110. else
  111. return(FAIL);
  112. }
  113. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  114. {
  115. strncpy(dest, src + start, cnt);
  116. dest[cnt] = 0;
  117. }
  118. void split(char **arr, char *str, const char *del)
  119. {
  120. char *s = strtok(str, del);
  121. while(s != NULL)
  122. {
  123. *arr++ = s;
  124. s = strtok(NULL, del);
  125. }
  126. }
  127. //==========================================
  128. // Init all share memory
  129. //==========================================
  130. int InitShareMemory()
  131. {
  132. int result = PASS;
  133. int MeterSMId;
  134. //creat ShmSysConfigAndInfo
  135. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  136. {
  137. #ifdef SystemLogMessage
  138. LOG_ERROR("shmget ShmSysConfigAndInfo NG");
  139. #endif
  140. result = FAIL;
  141. }
  142. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  143. {
  144. #ifdef SystemLogMessage
  145. LOG_ERROR("shmat ShmSysConfigAndInfo NG");
  146. #endif
  147. result = FAIL;
  148. }
  149. //creat ShmStatusCodeData
  150. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  151. {
  152. #ifdef SystemLogMessage
  153. LOG_ERROR("shmget ShmStatusCodeData NG");
  154. #endif
  155. result = FAIL;
  156. }
  157. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  158. {
  159. #ifdef SystemLogMessage
  160. LOG_ERROR("shmat ShmStatusCodeData NG");
  161. #endif
  162. result = FAIL;
  163. }
  164. //creat ShmStatusCodeData
  165. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0)
  166. {
  167. #ifdef SystemLogMessage
  168. LOG_ERROR("shmget ShmPrimaryMcuData NG");
  169. #endif
  170. result = FAIL;
  171. }
  172. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  173. {
  174. #ifdef SystemLogMessage
  175. LOG_ERROR("shmat ShmPrimaryMcuData NG");
  176. #endif
  177. result = FAIL;
  178. }
  179. if((MeterSMId = shmget(SM_ChargerInfoKey, sizeof(ChargerInfoData), 0777)) < 0)
  180. {
  181. #ifdef SystemLogMessage
  182. LOG_ERROR("shmat ChargerInfoData NG");
  183. #endif
  184. result = FAIL;
  185. }
  186. else if((ShmChargerInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  187. {
  188. #ifdef SystemLogMessage
  189. LOG_ERROR("shmat ChargerInfoData NG");
  190. #endif
  191. result = FAIL;
  192. }
  193. return result;
  194. }
  195. //================================================
  196. // Function
  197. //================================================
  198. void GetFwAndHwVersion()
  199. {
  200. if(Query_FW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
  201. {
  202. LOG_INFO("s1 = %s", ver.Version_FW);
  203. strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
  204. strcpy((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ver.Version_FW);
  205. }
  206. else
  207. {
  208. _CommFailCount++;
  209. }
  210. if (Query_HW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
  211. {
  212. LOG_INFO("s2 = %s", ver.Version_HW);
  213. }
  214. }
  215. void GetInputGpioStatus()
  216. {
  217. //LOG_INFO("GetInputGpioStatus");
  218. if (Query_Gpio_Input(Uart1Fd, Addr.IoExtend, &gpio_in) == PASS)
  219. {
  220. if (_acStatus != gpio_in.AC_Connector)
  221. {
  222. if (_acChkCount >= 3)
  223. {
  224. _acStatus = gpio_in.AC_Connector;
  225. // DO360 AC_Connector Status is the inverse of DS's
  226. if(ShmChargerInfo->Control.PrimaryCtrl.bits.AcContactorReverse)
  227. {
  228. ShmSysConfigAndInfo->SysInfo.AcContactorStatus = gpio_in.AC_Connector ? 0 : 1;
  229. ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = gpio_in.AC_Connector ? 0 : 1;
  230. }
  231. else
  232. {
  233. ShmSysConfigAndInfo->SysInfo.AcContactorStatus = gpio_in.AC_Connector ? 1 : 0;
  234. ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = gpio_in.AC_Connector ? 1 : 0;
  235. }
  236. LOG_INFO("Ac Contactor Status %s", ShmPrimaryMcuData->InputDet.bits.AcContactorDetec > 0 ? "On" : "Off");
  237. }
  238. else
  239. _acChkCount++;
  240. }
  241. else
  242. _acChkCount = 0;
  243. ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  244. if(_TempSPD != gpio_in.SPD)
  245. {
  246. _SPDCnt++;
  247. if(_SPDCnt >= STATE_CHANGE_COUNT || _TempSPD < 0)
  248. {
  249. _TempSPD = gpio_in.SPD;
  250. _SPDCnt = 0;
  251. }
  252. }
  253. if(_TempSPD >= 0)
  254. {
  255. ShmPrimaryMcuData->InputDet.bits.SpdDetec = _TempSPD;
  256. }
  257. #if 0
  258. ShmPrimaryMcuData->InputDet.bits.SpdDetec = gpio_in.SPD;
  259. #endif
  260. // DO360 Door Status is the inverse of DS's
  261. #if 0
  262. if(ShmChargerInfo->Control.PrimaryCtrl.bits.DoorSensorReverse)
  263. {
  264. ShmPrimaryMcuData->InputDet.bits.DoorOpen = gpio_in.Door_Open ? 0 : 1;
  265. }
  266. else
  267. {
  268. ShmPrimaryMcuData->InputDet.bits.DoorOpen = gpio_in.Door_Open ? 1 : 0;
  269. }
  270. #endif
  271. if(_TempDoor != gpio_in.Door_Open)
  272. {
  273. _DoorCnt++;
  274. if(_DoorCnt >= STATE_CHANGE_COUNT || _TempDoor < 0)
  275. {
  276. _TempDoor = gpio_in.Door_Open;
  277. _DoorCnt = 0;
  278. }
  279. }
  280. if(_TempDoor >= 0)
  281. {
  282. if(ShmChargerInfo->Control.PrimaryCtrl.bits.DoorSensorReverse)
  283. {
  284. ShmPrimaryMcuData->InputDet.bits.DoorOpen = _TempDoor > 0 ? 0 : 1;
  285. }
  286. else
  287. {
  288. ShmPrimaryMcuData->InputDet.bits.DoorOpen = _TempDoor > 0 ? 1 : 0;
  289. }
  290. }
  291. // Bypass door open
  292. //ShmPrimaryMcuData->InputDet.bits.DoorOpen = 0;
  293. ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
  294. ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
  295. if(_TempEmg != gpio_in.Emergency_Btn)
  296. {
  297. _EmgBtnCnt++;
  298. if(_EmgBtnCnt >= STATE_CHANGE_COUNT || _TempEmg < 0)
  299. {
  300. _TempEmg = gpio_in.Emergency_Btn;
  301. _EmgBtnCnt = 0;
  302. }
  303. }
  304. if(_TempEmg >= 0)
  305. {
  306. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = _TempEmg > 0 ? 1 : 0;
  307. }
  308. #if 0
  309. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = gpio_in.Emergency_Btn;
  310. #endif
  311. //LOG_INFO("left = %d", ShmPrimaryMcuData->InputDet.bits.Button1);
  312. //LOG_INFO("right = %d", ShmPrimaryMcuData->InputDet.bits.Button2);
  313. //LOG_INFO("ShmSysConfigAndInfo->SysInfo.AcContactorStatus = %d", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
  314. //if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == YES)
  315. // LOG_INFO("AC Mainbreaker occur.");
  316. ShmPrimaryMcuData->InputDet.bits.Key0 = gpio_in.Key[0] ? 0 : 1;
  317. ShmPrimaryMcuData->InputDet.bits.Key1 = gpio_in.Key[1] ? 0 : 1;
  318. ShmPrimaryMcuData->InputDet.bits.Key2 = gpio_in.Key[2] ? 0 : 1;
  319. ShmPrimaryMcuData->InputDet.bits.Key3 = gpio_in.Key[3] ? 0 : 1;
  320. char _SwValue = (ShmPrimaryMcuData->InputDet.bits.Key0 << 0) |
  321. (ShmPrimaryMcuData->InputDet.bits.Key1 << 1) |
  322. (ShmPrimaryMcuData->InputDet.bits.Key2 << 2) |
  323. (ShmPrimaryMcuData->InputDet.bits.Key3 << 3);
  324. if(_TempSwitch != _SwValue)
  325. {
  326. _SwitchCnt++;
  327. if(_SwitchCnt >= STATE_CHANGE_COUNT)
  328. {
  329. _TempSwitch = _SwValue;
  330. _SwitchCnt = 0;
  331. }
  332. }
  333. if(_CabinetSwitch != _TempSwitch)
  334. {
  335. LOG_INFO("Switch: %d, Key3: %d, Key2: %d, Key1: %d, Key0: %d",
  336. _TempSwitch,
  337. ShmPrimaryMcuData->InputDet.bits.Key3,
  338. ShmPrimaryMcuData->InputDet.bits.Key2,
  339. ShmPrimaryMcuData->InputDet.bits.Key1,
  340. ShmPrimaryMcuData->InputDet.bits.Key0);
  341. }
  342. _CabinetSwitch = _TempSwitch;
  343. if(_CabinetSwitch >= 0)
  344. {
  345. if(ShmChargerInfo->Control.CabinetSwitch != _CabinetSwitch)
  346. {
  347. LOG_INFO("Set Cabinet Switch: %d", _CabinetSwitch);
  348. }
  349. ShmChargerInfo->Control.CabinetSwitch = _CabinetSwitch;
  350. }
  351. }
  352. }
  353. void SetOutputGpio(byte flash)
  354. {
  355. Gpio_out gpio;
  356. if(_OutputDrv != flash)
  357. {
  358. gpio.Button_LED[0] = (flash & 0x01) > 0 ? 1 : 0;
  359. gpio.Button_LED[1] = (flash & 0x02) > 0 ? 1 : 0;
  360. gpio.System_LED[0] = (flash & 0x04) > 0 ? 1 : 0;
  361. gpio.System_LED[1] = (flash & 0x08) > 0 ? 1 : 0;
  362. gpio.System_LED[2] = (flash & 0x10) > 0 ? 1 : 0;
  363. gpio.System_LED[3] = (flash & 0x20) > 0 ? 1 : 0;
  364. gpio.AC_Connector = (flash & 0x40) > 0 ? 1 : 0;
  365. gpio.AC_Breaker = (flash & 0x80) > 0 ? 1 : 0;
  366. if (Config_Gpio_Output(Uart1Fd, Addr.IoExtend, &gpio) == PASS)
  367. {
  368. _OutputDrv = flash;
  369. }
  370. }
  371. else
  372. {
  373. // do nothing when no change
  374. }
  375. }
  376. void SetRtcData()
  377. {
  378. struct timeb csuTime;
  379. struct tm *tmCSU;
  380. ftime(&csuTime);
  381. tmCSU = localtime(&csuTime.time);
  382. // LOG_INFO("Time : %04d-%02d-%02d %02d:%02d:%02d", tmCSU->tm_year + 1900,
  383. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  384. // tmCSU->tm_sec);
  385. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  386. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  387. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  388. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  389. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  390. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  391. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  392. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  393. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  394. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  395. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  396. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  397. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  398. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  399. if (Config_Rtc_Data(Uart1Fd, Addr.IoExtend, &rtc) == PASS)
  400. {
  401. //LOG_INFO("SetRtc sucessfully.");
  402. }
  403. else
  404. {
  405. //LOG_INFO("SetRtc fail.");
  406. }
  407. }
  408. //================================================
  409. // Main process
  410. //================================================
  411. int InitComPort()
  412. {
  413. int fd;
  414. struct termios tios;
  415. fd = open(priPortName, O_RDWR);
  416. if(fd<=0)
  417. {
  418. #ifdef SystemLogMessage
  419. LOG_ERROR("open 407 Communication port NG");
  420. #endif
  421. return -1;
  422. }
  423. ioctl (fd, TCGETS, &tios);
  424. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  425. tios.c_lflag = 0;
  426. tios.c_iflag = 0;
  427. tios.c_oflag = 0;
  428. tios.c_cc[VMIN]=0;
  429. tios.c_cc[VTIME]=(unsigned char)1;
  430. tios.c_lflag=0;
  431. tcflush(fd, TCIFLUSH);
  432. ioctl (fd, TCSETS, &tios);
  433. return fd;
  434. }
  435. int main(void)
  436. {
  437. if(InitShareMemory() == FAIL)
  438. {
  439. #ifdef SystemLogMessage
  440. LOG_ERROR("InitShareMemory NG");
  441. #endif
  442. if(ShmStatusCodeData!=NULL)
  443. {
  444. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  445. }
  446. sleep(5);
  447. return 0;
  448. }
  449. Uart1Fd = InitComPort();
  450. LOG_INFO("407 Port id = %d", Uart1Fd);
  451. if(Uart1Fd < 0)
  452. {
  453. #ifdef SystemLogMessage
  454. LOG_ERROR("InitComPort (Uart1 : AM3352 - STM32) NG");
  455. #endif
  456. if (ShmStatusCodeData != NULL)
  457. {
  458. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  459. }
  460. sleep(5);
  461. return 0;
  462. }
  463. SetRtcData();
  464. // update ac contact status
  465. //_acStatus = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  466. _acStatus = ShmSysConfigAndInfo->SysInfo.AcContactorStatus > 0 ? 0 : 1;
  467. // set wrong value, it will update at the first time
  468. _OutputDrv = ~ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0];
  469. for(;;)
  470. {
  471. if(!ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  472. {
  473. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  474. // 模組更新 FW 後,需重新做
  475. if(ShmPrimaryMcuData->SelfTest_Comp != PASS)
  476. {
  477. memset(ShmPrimaryMcuData->version, 0x00, 16);
  478. GetFwAndHwVersion();
  479. sleep(1);
  480. if(strlen((char *)ShmPrimaryMcuData->version) != 0)
  481. {
  482. ShmPrimaryMcuData->SelfTest_Comp = YES;
  483. }
  484. if(_CommFailCount >= COMM_FAIL_COUNT)
  485. {
  486. LOG_INFO("Primary MCU Communication Fail: %d", _CommFailCount);
  487. _CommFailCount = 0;
  488. }
  489. }
  490. else
  491. {
  492. GetInputGpioStatus();
  493. //LOG_INFO("Input Status: %02X %02X", ShmPrimaryMcuData->InputDet.InputDetValue[1], ShmPrimaryMcuData->InputDet.InputDetValue[0]);
  494. SetOutputGpio(ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0]);
  495. }
  496. }
  497. usleep(100000);
  498. }
  499. return FAIL;
  500. }