Module_PrimaryComm.c 15 KB

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