functional_safety_key.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #include "tasks.h"
  2. #include "usart.h"
  3. #include "var.h"
  4. #include "functional_safety_key.h"
  5. Key_Flag_Struct_t Key_Flag;
  6. Key_Status_Struct_t Key_Recv_Status;
  7. Key_Status_Struct_t Key_Scan_Status;
  8. PBU_TE_STATUS PBU_TE_Status=HANDSHAKE_NO;
  9. void Scan_Key(void)
  10. {
  11. static uint32_t DelayTimeCnt_power=0,DelayTimeCnt_walk=0,DelayTimeCnt_dec=0,DelayTimeCnt_add=0,DelayTimeCnt_start=0;
  12. static uint8_t Flag_key_power = 0;
  13. static uint8_t Flag_key_walk = 0;
  14. static uint8_t Flag_key_dec = 0;
  15. static uint8_t Flag_key_add = 0;
  16. static uint8_t Flag_Start = 0;
  17. if(Scan_Key_Start == 0x01)
  18. {
  19. if(Flag_Start == 0)
  20. {
  21. DelayTimeCnt_start = SysTime_5ms;
  22. Flag_Start = 1;
  23. }
  24. if(TimeCntDiff_5ms(DelayTimeCnt_start) > 4)
  25. {
  26. Scan_Key_Start = 0x00;
  27. Flag_Start = 0;
  28. SendKeyStatusToTE();
  29. }
  30. /* Processing power button */
  31. if( *(Key_On_Off.ADC_result)<1000 )
  32. {
  33. if(Flag_key_power == 0)
  34. {
  35. DelayTimeCnt_power = SysTime_5ms;
  36. Flag_key_power = 1;
  37. }
  38. if(TimeCntDiff_5ms(DelayTimeCnt_power) > 1)
  39. {
  40. if( *(Key_On_Off.ADC_result)<1000 )
  41. {
  42. Key_Scan_Status.Status_Bit.Power = 0x00;
  43. Flag_key_power = 0;
  44. }
  45. }
  46. }
  47. else if(*(Key_On_Off.ADC_result)>3000)
  48. {
  49. Key_Scan_Status.Status_Bit.Power = 0x01;
  50. Flag_key_power = 0;
  51. }
  52. /* Processing Walk button */
  53. if( *(Key_Walk.ADC_result)<1000 )
  54. {
  55. if(Flag_key_walk == 0)
  56. {
  57. DelayTimeCnt_walk = SysTime_5ms;
  58. Flag_key_walk = 1;
  59. }
  60. if(TimeCntDiff_5ms(DelayTimeCnt_walk) > 1)
  61. {
  62. if(*(Key_Walk.ADC_result)<1000)
  63. {
  64. Key_Scan_Status.Status_Bit.Walk = 0x00;
  65. Flag_key_walk = 0;
  66. }
  67. }
  68. }
  69. else if(*(Key_Walk.ADC_result)>3000)
  70. {
  71. Key_Scan_Status.Status_Bit.Walk = 0x01;
  72. Flag_key_walk = 0;
  73. }
  74. /* Processing DEC button */
  75. if( *(Key_Down.ADC_result)<1000 )
  76. {
  77. if(Flag_key_dec == 0)
  78. {
  79. DelayTimeCnt_dec = SysTime_5ms;
  80. Flag_key_dec = 1;
  81. }
  82. if(TimeCntDiff_5ms(DelayTimeCnt_dec) > 1)
  83. {
  84. if( *(Key_Down.ADC_result)<1000 )
  85. {
  86. Key_Scan_Status.Status_Bit.Dec = 0x00;
  87. Flag_key_dec = 0;
  88. }
  89. }
  90. }
  91. else if(*(Key_Down.ADC_result)>3000)
  92. {
  93. Key_Scan_Status.Status_Bit.Dec = 0x01;
  94. Flag_key_dec = 0;
  95. }
  96. /* Processing ADD button */
  97. if( *(Key_Up.ADC_result)<1000 )
  98. {
  99. if(Flag_key_add == 0)
  100. {
  101. DelayTimeCnt_add = SysTime_5ms;
  102. Flag_key_add = 1;
  103. }
  104. if(TimeCntDiff_5ms(DelayTimeCnt_add) > 1)
  105. {
  106. if(*(Key_Up.ADC_result)<1000)
  107. {
  108. Key_Scan_Status.Status_Bit.Add = 0x00;
  109. Flag_key_add = 0;
  110. }
  111. }
  112. }
  113. else if(*(Key_Up.ADC_result)>3000)
  114. {
  115. Key_Scan_Status.Status_Bit.Add = 0x01;
  116. Flag_key_add = 0;
  117. }
  118. }
  119. }
  120. void SendKeyStatusToTE(void)
  121. {
  122. uint8_t send_buffer[256];
  123. uint8_t mode;
  124. mode = 0x0C; //0x11:read,0x16:write,0x0C:report
  125. /* Add command word */
  126. send_buffer[0] = 0x11;
  127. send_buffer[1] = 0x01;
  128. /* Add data */
  129. send_buffer[2] = Key_Scan_Status.Status;
  130. /* Send data */
  131. SendCmdData(mode, send_buffer, 3);
  132. }
  133. void SendMotorEnableToTE(void)
  134. {
  135. uint8_t send_buffer[3];
  136. uint8_t mode;
  137. mode = 0x0C; //0x11:read,0x16:write,0x0C:report
  138. /* Shut down information command word */
  139. send_buffer[0] = 0x44;
  140. send_buffer[1] = 0x01;
  141. /* Add data */
  142. send_buffer[2] = 0x01;
  143. /* Send data */
  144. SendCmdData(mode, send_buffer, 3);
  145. }
  146. void SendMotorDisableToTE(void)
  147. {
  148. uint8_t send_buffer[3];
  149. uint8_t mode;
  150. mode = 0x0C; //0x11:read,0x16:write,0x0C:report
  151. /* Shut down information command word */
  152. send_buffer[0] = 0x44;
  153. send_buffer[1] = 0x01;
  154. /* Add data */
  155. send_buffer[2] = 0x00;
  156. /* Send data */
  157. SendCmdData(mode, send_buffer, 3);
  158. }
  159. void Error_Key_Process(void)
  160. {
  161. static uint32_t LongDelayTimeCnt_On_Off = 0;
  162. static uint32_t LongDelayTimeCnt_Walk = 0;
  163. static uint32_t LongDelayTimeCnt_Down = 0;
  164. static uint32_t LongDelayTimeCnt_Up = 0;
  165. static uint32_t LongDelayTimeCnt_Light = 0;
  166. static uint8_t Flag1_1 = 0;
  167. static uint8_t Flag2_1 = 0;
  168. static uint8_t Flag3_1 = 0;
  169. static uint8_t Flag4_1 = 0;
  170. static uint8_t Flag6_1 = 0;
  171. /* Processing power key */
  172. #if 0
  173. if(*(Key_On_Off.ADC_result)<1000)
  174. {
  175. if(Flag1_1 == 0)
  176. {
  177. LongDelayTimeCnt_On_Off = SysTime_5ms;
  178. Flag1_1 = 1;
  179. }
  180. if(TimeCntDiff_5ms(LongDelayTimeCnt_On_Off) >= FAILURE_TIME)
  181. {
  182. if(*(Key_On_Off.ADC_result)<1000)
  183. {
  184. Key_Flag.power_long_press = 1;
  185. PBU_hardwareErrorCode.ERROR_Bit.Key_Power = 0x01;
  186. Flag1_1 = 0;
  187. }
  188. }
  189. }
  190. else if(*(Key_On_Off.ADC_result)>3000)
  191. {
  192. Key_Flag.power_long_press = 0;
  193. Flag1_1 = 0;
  194. }
  195. #endif
  196. /* Processing Walk key */
  197. if(*(Key_Walk.ADC_result)<1000)
  198. {
  199. if(Flag2_1 == 0)
  200. {
  201. LongDelayTimeCnt_Walk = SysTime_5ms;
  202. Flag2_1 = 1;
  203. }
  204. if(TimeCntDiff_5ms(LongDelayTimeCnt_Walk) >= FAILURE_TIME)
  205. {
  206. if(*(Key_Walk.ADC_result)<1000)
  207. {
  208. Key_Flag.walk_long_press = 1;
  209. PBU_hardwareErrorCode.ERROR_Bit.Key_Walk = 0x01;
  210. Flag2_1 = 0;
  211. }
  212. }
  213. }
  214. else if(*(Key_Walk.ADC_result)>3000)
  215. {
  216. Key_Flag.walk_long_press = 0;
  217. PBU_hardwareErrorCode.ERROR_Bit.Key_Walk = 0x00;
  218. Flag2_1 = 0;
  219. }
  220. /* Processing Down key */
  221. if(*(Key_Down.ADC_result)<1000)
  222. {
  223. if(Flag3_1 == 0)
  224. {
  225. LongDelayTimeCnt_Down = SysTime_5ms;
  226. Flag3_1 = 1;
  227. }
  228. if(TimeCntDiff_5ms(LongDelayTimeCnt_Down) >= FAILURE_TIME)
  229. {
  230. if(*(Key_Down.ADC_result)<1000)
  231. {
  232. Key_Flag.dec_long_press = 1;
  233. PBU_hardwareErrorCode.ERROR_Bit.Key_Dec = 0x01;
  234. Flag3_1 = 0;
  235. }
  236. }
  237. }
  238. else if(*(Key_Down.ADC_result)>3000)
  239. {
  240. Key_Flag.dec_long_press = 0;
  241. PBU_hardwareErrorCode.ERROR_Bit.Key_Dec = 0x00;
  242. Flag3_1 = 0;
  243. }
  244. /* Processing ADD key */
  245. if(*(Key_Up.ADC_result)<1000)
  246. {
  247. if(Flag4_1 == 0)
  248. {
  249. LongDelayTimeCnt_Up = SysTime_5ms;
  250. Flag4_1 = 1;
  251. }
  252. if((PBU_AssitMode_Status.PBU_AssistMode_Flag == PBU_AssistMode_Flag_Walk)||(PBU_AssitMode_Status.PBU_AssistMode_Flag == PBU_AssistMode_Flag_WalkSby))
  253. LongDelayTimeCnt_Up = SysTime_5ms;
  254. if(TimeCntDiff_5ms(LongDelayTimeCnt_Up) >= FAILURE_TIME)
  255. {
  256. if(*(Key_Up.ADC_result)<1000)
  257. {
  258. Key_Flag.add_long_press = 1;
  259. PBU_hardwareErrorCode.ERROR_Bit.Key_Add = 0x01;
  260. Flag4_1 = 0;
  261. }
  262. }
  263. }
  264. else if(*(Key_Up.ADC_result)>3000)
  265. {
  266. Key_Flag.add_long_press = 0;
  267. PBU_hardwareErrorCode.ERROR_Bit.Key_Add = 0x00;
  268. Flag4_1 = 0;
  269. }
  270. /* Processing LIGHT key */
  271. if(*(Key_Light.ADC_result)<1000)
  272. {
  273. if(Flag6_1 == 0)
  274. {
  275. LongDelayTimeCnt_Light = SysTime_5ms;
  276. Flag6_1 = 1;
  277. }
  278. /* Handle long press 10s */
  279. if(TimeCntDiff_5ms(LongDelayTimeCnt_Light) >= FAILURE_TIME)
  280. {
  281. if(*(Key_Light.ADC_result)<1000)
  282. {
  283. Key_Flag.light_long_press = 1;
  284. PBU_hardwareErrorCode.ERROR_Bit.Key_Light = 0x01;
  285. Flag6_1 = 0;
  286. }
  287. }
  288. }
  289. else if(*(Key_Light.ADC_result)>3000)
  290. {
  291. Key_Flag.light_long_press = 0;
  292. PBU_hardwareErrorCode.ERROR_Bit.Key_Light = 0x00;
  293. Flag6_1 = 0;
  294. }
  295. }
  296. void Scan_Key_ADC_Error(void)
  297. {
  298. static uint16_t On_Off_error_cnt = 0;
  299. static uint16_t Walk_error_cnt = 0;
  300. static uint16_t Up_error_cnt = 0;
  301. static uint16_t Down_error_cnt = 0;
  302. static uint16_t Light_error_cnt = 0;
  303. #if 0
  304. ////////////On_Off判断////////////////////////
  305. if((*(Key_On_Off.ADC_result)>1000)&&(*(Key_On_Off.ADC_result)<3000))
  306. {
  307. On_Off_error_cnt++;
  308. }
  309. else
  310. {
  311. On_Off_error_cnt=0;
  312. }
  313. if(On_Off_error_cnt>5000)
  314. {
  315. PBU_hardwareErrorCode.ERROR_Bit.Key_Power = 0x01;
  316. }
  317. #endif
  318. ////////////Walk判断////////////////////////
  319. if((*(Key_Walk.ADC_result)>1000)&&(*(Key_Walk.ADC_result)<3000))
  320. {
  321. Walk_error_cnt++;
  322. }
  323. else
  324. {
  325. Walk_error_cnt=0;
  326. }
  327. if(Walk_error_cnt>5000)
  328. {
  329. PBU_hardwareErrorCode.ERROR_Bit.Key_Walk = 0x01;
  330. }
  331. ////////////Add判断////////////////////////
  332. if((*(Key_Up.ADC_result)>1000)&&(*(Key_Up.ADC_result)<3000))
  333. {
  334. Up_error_cnt++;
  335. }
  336. else
  337. {
  338. Up_error_cnt=0;
  339. }
  340. if(Up_error_cnt>5000)
  341. {
  342. PBU_hardwareErrorCode.ERROR_Bit.Key_Add = 0x01;
  343. }
  344. ////////////Down判断////////////////////////
  345. if((*(Key_Down.ADC_result)>1000)&&(*(Key_Down.ADC_result)<3000))
  346. {
  347. Down_error_cnt++;
  348. }
  349. else
  350. {
  351. Down_error_cnt=0;
  352. }
  353. if(Down_error_cnt>5000)
  354. {
  355. PBU_hardwareErrorCode.ERROR_Bit.Key_Dec = 0x01;
  356. }
  357. ////////////Light判断////////////////////////
  358. if((*(Key_Light.ADC_result)>1000)&&(*(Key_Light.ADC_result)<3000))
  359. {
  360. Light_error_cnt++;
  361. }
  362. else
  363. {
  364. Light_error_cnt=0;
  365. }
  366. if(Light_error_cnt>5000)
  367. {
  368. PBU_hardwareErrorCode.ERROR_Bit.Key_Light = 0x01;
  369. }
  370. }
  371. void Power_Mos_Check(void)
  372. {
  373. static uint16_t Power_Mos_error_cnt = 0;
  374. if((ADC_Result[0]>2000)&&(*(Key_On_Off.ADC_result)>3000))
  375. {
  376. Power_Mos_error_cnt ++;
  377. }
  378. else
  379. Power_Mos_error_cnt=0;
  380. if(Power_Mos_error_cnt>3)
  381. {
  382. Power_Mos_error_cnt=0;
  383. PBU_ErrorCode.ERROR_Bit.MOS_Fault =1;
  384. }
  385. }
  386. void Vin_test_Check(void)
  387. {
  388. if((ADC_Result[0]<3000)&&(ADC_Result[0]>1000))
  389. {
  390. Vin_test_TimeCnt=SysTime_5ms;
  391. }
  392. if(TimeCntDiff_5ms(Vin_test_TimeCnt) >= 400)
  393. {
  394. PBU_ErrorCode.ERROR_Bit.Hardware_Fault =1;
  395. }
  396. }