Primary.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #include <stdio.h> /*標準輸入輸出定義*/
  2. #include <stdlib.h> /*標準函數庫定義*/
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include "../Config.h"
  6. #include "../Log/log.h"
  7. #include "../Define/define.h"
  8. #include "../ShareMemory/shmMem.h"
  9. #include "../SelectGun/SelectGun.h"
  10. #include "main.h"
  11. #include "../timeout.h"
  12. struct SysInfoData *pSysInfo = NULL;
  13. struct ChargingInfoData *pDcChargingInfo = NULL;
  14. struct SysConfigData *pSysConfig = NULL;
  15. //------------------------------------------------------------------------------
  16. typedef struct StLedConfig {
  17. //OutputDrvValue[0]
  18. uint8_t LeftButtonLed: 1; //bit 0, H: ON, L:OFF
  19. uint8_t RightButtonLed: 1; //bit 1, H: ON, L:OFF
  20. uint8_t GreenLED: 1; //bit 2, H: ON, L:OFF
  21. uint8_t YellowLED: 1; //bit 3, H: ON, L:OFF
  22. uint8_t RedLED: 1; //bit 4, H: ON, L:OFF
  23. uint8_t SystemLed4: 1; //bit 5, H: ON, L:OFF
  24. uint8_t AcContactor: 1; //bit 6, H: ON, L:OFF
  25. uint8_t Reserved: 1; //bit 7 reserved
  26. } LedConfig;
  27. //------------------------------------------------------------------------------
  28. void ChangeLCM(uint8_t page)
  29. {
  30. pSysInfo->SystemPage = page;
  31. }
  32. // ***********************************
  33. // Enter LCM Page
  34. // ***********************************
  35. void EnterViewPage()
  36. {
  37. ChangeLCM(_LCM_VIEW);
  38. StopSystemTimeoutDet();
  39. }
  40. void EnterAuthorizePage(uint8_t gunIndex)
  41. {
  42. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  43. log_info("Select Gun(%d) in Authorizing",gunIndex);
  44. ClearDetectPluginFlag();
  45. ChangeGunSelectByIndex(gunIndex);
  46. confirmSelGun(gunIndex);
  47. ChangeLCM(_LCM_START_SCAN);
  48. strcpy((char *)pSysConfig->UserId, "");
  49. setChargerMode(gunIndex,MODE_AUTHORIZING);
  50. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  51. }
  52. void EnterLinkErrorPage()
  53. {
  54. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  55. //ClearDetectPluginFlag();
  56. StopSystemTimeoutDet();
  57. //StopGunInfoTimeoutDet(pSysInfo->CurGunSelected);
  58. ChangeLCM(_LCM_LINK_ERROR);
  59. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  60. //StartGunInfoTimeoutDet(pSysInfo->CurGunSelected,Timeout_LinkError);
  61. }
  62. void EnterDetailViewPage(uint8_t gunIndex)
  63. {
  64. log_info("Show Gun(%d) Detail View",gunIndex);
  65. ChangeGunSelectByIndex(gunIndex);
  66. ChangeLCM(_LCM_DETAIL_VIEW);
  67. StopGunInfoTimeoutDet(gunIndex);
  68. StartGunInfoTimeoutDet(gunIndex,Timeout_DetailView);
  69. }
  70. void EnterAuthorizeStopPage()
  71. {
  72. log_info("Select Gun(%d) in Stopping",pSysInfo->CurGunSelected);
  73. StopGunInfoTimeoutDet(pSysInfo->CurGunSelected);
  74. StopSystemTimeoutDet();
  75. strcpy((char *)pSysConfig->UserId, "");
  76. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  77. if (pDcChargingInfo->isRemoteStart) {
  78. pSysInfo->SystemPage = _LCM_STOP_APP;
  79. } else {
  80. pSysInfo->SystemPage = _LCM_STOP_RFID;
  81. }
  82. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  83. }
  84. void StopChargingBtn(uint8_t gunIndex)
  85. {
  86. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  87. pDcChargingInfo->SystemStatus = S_TERMINATING;
  88. log_info("Press Button to Stop Charging");
  89. }
  90. void EnterSummaryPage()
  91. {
  92. StopSystemTimeoutDet();
  93. log_info("Showing Summary Page");
  94. pSysInfo->SystemPage = _LCM_SUMMARY;
  95. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  96. }
  97. // *****************************
  98. // Enter System status
  99. // *****************************
  100. void ReturnIDLE(uint8_t gunIndex)
  101. {
  102. log_info("Gun(%d) return to IDLE",gunIndex);
  103. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  104. EnterViewPage();
  105. StopSystemTimeoutDet();
  106. StopGunInfoTimeoutDet(gunIndex);
  107. setChargerMode(gunIndex,MODE_IDLE);
  108. //destroySelGun(gunIndex);
  109. }
  110. void ReturnAuthorize()
  111. {
  112. StopSystemTimeoutDet();
  113. EnterAuthorizePage(pSysInfo->CurGunSelected);
  114. }
  115. void showConfirmStopPage(uint8_t gunIndex)
  116. {
  117. StopSystemTimeoutDet();
  118. ChangeGunSelectByIndex(gunIndex);
  119. log_info("Enter Confirm stop Page");
  120. pSysInfo->SystemPage = _LCM_CONFIRM_STOP;
  121. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  122. }
  123. void CheckLeftButton()
  124. {
  125. uint8_t gunIndex = 0;
  126. switch (pSysInfo->SystemPage) {
  127. case _LCM_VIEW:
  128. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(LEFT_GUN_NUM);
  129. pSysInfo->CurGunSelected = LEFT_GUN_NUM;
  130. if(pDcChargingInfo->SystemStatus == S_IDLE || pDcChargingInfo->SystemStatus == S_RESERVATION) {
  131. EnterAuthorizePage(LEFT_GUN_NUM);
  132. pSysInfo->SystemPage = _LCM_START_SCAN;
  133. } else if (pDcChargingInfo->SystemStatus == S_CHARGING) {
  134. //EnterDetailViewPage(LEFT_GUN_NUM);
  135. showConfirmStopPage(LEFT_GUN_NUM);
  136. }
  137. break;
  138. case _LCM_START_SCAN:
  139. case _LCM_START_AUTHORIZE_FAIL:
  140. case _LCM_LINK_ERROR:
  141. if (pSysInfo->CurGunSelected == LEFT_GUN_NUM) {
  142. ReturnIDLE(pSysInfo->CurGunSelected);
  143. }
  144. break;
  145. case _LCM_DETAIL_VIEW:
  146. case _LCM_STOP_RFID:
  147. case _LCM_STOP_APP:
  148. case _LCM_STOP_RFID_FAIL:
  149. EnterViewPage();
  150. break;
  151. case _LCM_SUMMARY:
  152. if (pSysInfo->CurGunSelected == RIGHT_GUN_NUM)
  153. break;
  154. log_info("Return View Page");
  155. ReturnIDLE(pSysInfo->CurGunSelected);
  156. break;
  157. case _LCM_ERROR:
  158. //EnterSummaryPage();
  159. if (pSysInfo->CurGunSelected == RIGHT_GUN_NUM)
  160. break;
  161. pSysInfo->SystemPage = _LCM_WAIT_PLUGOUT;
  162. //ReturnIDLE(pSysInfo->CurGunSelected);
  163. break;
  164. case _LCM_CONFIRM_STOP:
  165. if (pSysInfo->CurGunSelected == LEFT_GUN_NUM) {
  166. StopChargingBtn(LEFT_GUN_NUM);
  167. } else {
  168. EnterViewPage();
  169. }
  170. break;
  171. case _LCM_WAIT_PLUGIN:
  172. if (pSysInfo->CurGunSelected == LEFT_GUN_NUM)
  173. ReturnIDLE(LEFT_GUN_NUM);
  174. break;
  175. case _LCM_WAIT_PLUGOUT:
  176. EnterSummaryPage();
  177. break;
  178. }
  179. }
  180. void CheckRightButton()
  181. {
  182. uint8_t gunIndex = 0;
  183. switch (pSysInfo->SystemPage) {
  184. case _LCM_VIEW:
  185. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(RIGHT_GUN_NUM);
  186. pSysInfo->CurGunSelected = RIGHT_GUN_NUM;
  187. if(pDcChargingInfo->SystemStatus == S_IDLE || pDcChargingInfo->SystemStatus == S_RESERVATION) {
  188. EnterAuthorizePage(RIGHT_GUN_NUM);
  189. pSysInfo->SystemPage = _LCM_START_SCAN;
  190. } else if (pDcChargingInfo->SystemStatus == S_CHARGING) {
  191. //EnterDetailViewPage(RIGHT_GUN_NUM);
  192. showConfirmStopPage(RIGHT_GUN_NUM);
  193. }
  194. break;
  195. case _LCM_START_SCAN:
  196. if (pSysInfo->CurGunSelected == RIGHT_GUN_NUM)
  197. ReturnIDLE(pSysInfo->CurGunSelected);
  198. break;
  199. case _LCM_START_AUTHORIZE_FAIL:
  200. if (pSysInfo->SystemPage == _LCM_START_AUTHORIZE_FAIL && pSysInfo->CurGunSelected == LEFT_GUN_NUM)
  201. break;
  202. ReturnAuthorize();
  203. break;
  204. case _LCM_WAIT_PLUGIN:
  205. if (pSysInfo->CurGunSelected == RIGHT_GUN_NUM)
  206. ReturnIDLE(RIGHT_GUN_NUM);
  207. break;
  208. case _LCM_LINK_ERROR:
  209. if (pSysInfo->CurGunSelected == LEFT_GUN_NUM)
  210. break;
  211. ReturnAuthorize();
  212. break;
  213. case _LCM_DETAIL_VIEW:
  214. EnterAuthorizeStopPage();
  215. break;
  216. case _LCM_STOP_RFID_FAIL:
  217. EnterAuthorizeStopPage();
  218. break;
  219. case _LCM_WAIT_PLUGOUT:
  220. EnterSummaryPage();
  221. break;
  222. case _LCM_ERROR:
  223. if (pSysInfo->CurGunSelected == LEFT_GUN_NUM)
  224. break;
  225. pSysInfo->SystemPage = _LCM_WAIT_PLUGOUT;
  226. //EnterSummaryPage();
  227. break;
  228. case _LCM_CONFIRM_STOP:
  229. if (pSysInfo->CurGunSelected == RIGHT_GUN_NUM) {
  230. StopChargingBtn(RIGHT_GUN_NUM);
  231. } else {
  232. EnterViewPage();
  233. }
  234. break;
  235. case _LCM_SUMMARY:
  236. if (pSysInfo->CurGunSelected == LEFT_GUN_NUM)
  237. break;
  238. log_info("Return View Page");
  239. ReturnIDLE(pSysInfo->CurGunSelected);
  240. break;
  241. }
  242. }
  243. void CheckPrimaryWarm(void)
  244. {
  245. uint8_t i = 0;
  246. uint8_t Rtn = 0;
  247. struct WARNING_CODE_INFO *pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  248. struct PrimaryMcuData *ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  249. struct AlarmCodeData *pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  250. DcCommonInfo *ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  251. if (pSysWarning->WarningCount > 0) {
  252. Rtn = 0;
  253. for (i = 0; i < pSysWarning->WarningCount; i++) {
  254. if (memcmp(&pSysWarning->WarningCode[i][0], "042251", 6) == 0) {
  255. EmcOccureByString("042251");
  256. ShmDcCommonData->PowerAlarmState.StatusBit.EmergencyStop = YES;
  257. Rtn = 1;
  258. } else if (memcmp(&pSysWarning->WarningCode[i][0], "042252", 6) == 0) {
  259. ShmDcCommonData->PowerAlarmState.StatusBit.DoorOpen = YES;
  260. EmcOccureByString("042252");
  261. Rtn = 1;
  262. } else if (memcmp(&pSysWarning->WarningCode[i][0], "042327", 6) == 0) {
  263. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputOVP = YES;
  264. EmcOccureByString("042327");
  265. Rtn = 1;
  266. } else if (memcmp(&pSysWarning->WarningCode[i][0], "042328", 6) == 0) {
  267. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputUVP = YES;
  268. EmcOccureByString("042328");
  269. Rtn = 1;
  270. } else if (memcmp(&pSysWarning->WarningCode[i][0], "042200", 6) == 0) {
  271. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL1InputOVP = YES;
  272. EmcOccureByString("042200");
  273. Rtn = 1;
  274. } else if (memcmp(&pSysWarning->WarningCode[i][0], "042201", 6) == 0) {
  275. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL2InputOVP = YES;
  276. EmcOccureByString("042201");
  277. Rtn = 1;
  278. } else if (memcmp(&pSysWarning->WarningCode[i][0], "042202", 6) == 0) {
  279. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL3InputOVP = YES;
  280. EmcOccureByString("042202");
  281. Rtn = 1;
  282. } else if (memcmp(&pSysWarning->WarningCode[i][0], "042267", 6) == 0) {
  283. ShmDcCommonData->PowerAlarmState.StatusBit.PsuFailure = YES;
  284. EmcOccureByString("042267");
  285. Rtn = 1;
  286. } else if (memcmp(&pSysWarning->WarningCode[i][0], "012304", 6) == 0) {
  287. EmcOccureByString("012304");
  288. Rtn = 1;
  289. }
  290. }
  291. if (Rtn == 0) {
  292. ShmDcCommonData->PowerAlarmState.StatusBit.EmergencyStop = NO;
  293. ShmDcCommonData->PowerAlarmState.StatusBit.DoorOpen = NO;
  294. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputOVP = NO;
  295. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputUVP = NO;
  296. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL1InputOVP = NO;
  297. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL2InputOVP = NO;
  298. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL3InputOVP = NO;
  299. ShmDcCommonData->PowerAlarmState.StatusBit.PsuFailure = NO;
  300. ReleaseEmsOccureByString(0, "042251");
  301. ReleaseEmsOccureByString(0, "042252");
  302. ReleaseEmsOccureByString(0, "042200");
  303. ReleaseEmsOccureByString(0, "042327");
  304. ReleaseEmsOccureByString(0, "042328");
  305. ReleaseEmsOccureByString(0, "042201");
  306. ReleaseEmsOccureByString(0, "042202");
  307. ReleaseEmsOccureByString(0, "042267");
  308. ReleaseEmsOccureByString(0, "012304");
  309. }
  310. } else {
  311. ShmDcCommonData->PowerAlarmState.StatusBit.EmergencyStop = NO;
  312. ShmDcCommonData->PowerAlarmState.StatusBit.DoorOpen = NO;
  313. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputOVP = NO;
  314. ShmDcCommonData->PowerAlarmState.StatusBit.DcInputUVP = NO;
  315. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL1InputOVP = NO;
  316. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL2InputOVP = NO;
  317. ShmDcCommonData->PowerAlarmState.StatusBit.SystemL3InputOVP = NO;
  318. ShmDcCommonData->PowerAlarmState.StatusBit.PsuFailure = NO;
  319. ReleaseEmsOccureByString(0, "042251");
  320. ReleaseEmsOccureByString(0, "042252");
  321. ReleaseEmsOccureByString(0, "042200");
  322. ReleaseEmsOccureByString(0, "042327");
  323. ReleaseEmsOccureByString(0, "042328");
  324. ReleaseEmsOccureByString(0, "042201");
  325. ReleaseEmsOccureByString(0, "042202");
  326. ReleaseEmsOccureByString(0, "042267");
  327. ReleaseEmsOccureByString(0, "012304");
  328. }
  329. if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == ABNORMAL) {
  330. pAlarmCode->AlarmEvents.bits.EmergencyStopTrip = YES;
  331. EmcOccureByString("012251");
  332. } else {
  333. ReleaseEmsOccureByString(0, "012251");
  334. }
  335. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == ABNORMAL) {
  336. pAlarmCode->AlarmEvents.bits.MainPowerBreakerTrip = YES;
  337. EmcOccureByString("012238");
  338. } else {
  339. ReleaseEmsOccureByString(0, "012238");
  340. }
  341. if (ShmPrimaryMcuData->InputDet.bits.SpdDetec == ABNORMAL) {
  342. pAlarmCode->AlarmEvents.bits.SpdTrip = YES;
  343. } else {
  344. pAlarmCode->AlarmEvents.bits.SpdTrip = NO;
  345. }
  346. if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == ABNORMAL) {
  347. pAlarmCode->AlarmEvents.bits.DoorOpen = YES;
  348. EmcOccureByString("012252");
  349. } else {
  350. ReleaseEmsOccureByString(0, "012252");
  351. }
  352. }
  353. void ChkPrimaryStatus(void)
  354. {
  355. static bool leftBtnPush = false;
  356. static bool rightBtnPush = false;
  357. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  358. struct PrimaryMcuData *ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  359. struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  360. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  361. // Check Primary Board status
  362. CheckPrimaryWarm();
  363. if ((pDcChargingInfo->SystemStatus == S_IDLE || pDcChargingInfo->SystemStatus == S_RESERVATION)
  364. && pSysConfig->OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING) {
  365. log_info("Offline policy no charge");
  366. return;;
  367. }
  368. // Show Version
  369. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS &&
  370. ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS )
  371. pSysConfig->ShowInformation = YES;
  372. else
  373. pSysConfig->ShowInformation = NO;
  374. if (pSysConfig->ShowInformation)
  375. return;
  376. // Press Left Button
  377. if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_PRESS && !leftBtnPush ) {
  378. leftBtnPush = true;
  379. CheckLeftButton();
  380. log_info("left btn down...............................%d %d %d",
  381. pSysInfo->CurGunSelected,pDcChargingInfo->SystemStatus,pSysInfo->SystemPage);
  382. } else if (ShmPrimaryMcuData->InputDet.bits.Button1 == BTN_RELEASE && leftBtnPush) {
  383. leftBtnPush = false;
  384. }
  385. // Press Right Button
  386. if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_PRESS && !rightBtnPush ) {
  387. rightBtnPush = true;
  388. CheckRightButton();
  389. log_info("right btn down...............................%d %d %d",
  390. pSysInfo->CurGunSelected,pDcChargingInfo->SystemStatus, pSysInfo->SystemPage);
  391. } else if (ShmPrimaryMcuData->InputDet.bits.Button2 == BTN_RELEASE && rightBtnPush) {
  392. rightBtnPush = false;
  393. }
  394. }