Module_PrimaryComm.c 12 KB

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