Module_PrimaryComm.c 19 KB

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