Module_PrimaryComm.c 12 KB

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