functional_safety_key.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. Flag2_1 = 0;
  218. }
  219. /* Processing Down key */
  220. if(*(Key_Down.ADC_result)<1000)
  221. {
  222. if(Flag3_1 == 0)
  223. {
  224. LongDelayTimeCnt_Down = SysTime_5ms;
  225. Flag3_1 = 1;
  226. }
  227. if(TimeCntDiff_5ms(LongDelayTimeCnt_Down) >= FAILURE_TIME)
  228. {
  229. if(*(Key_Down.ADC_result)<1000)
  230. {
  231. Key_Flag.dec_long_press = 1;
  232. PBU_hardwareErrorCode.ERROR_Bit.Key_Dec = 0x01;
  233. Flag3_1 = 0;
  234. }
  235. }
  236. }
  237. else if(*(Key_Down.ADC_result)>3000)
  238. {
  239. Key_Flag.dec_long_press = 0;
  240. Flag3_1 = 0;
  241. }
  242. /* Processing ADD key */
  243. if(*(Key_Up.ADC_result)<1000)
  244. {
  245. if(Flag4_1 == 0)
  246. {
  247. LongDelayTimeCnt_Up = SysTime_5ms;
  248. Flag4_1 = 1;
  249. }
  250. if((PBU_AssitMode_Status.PBU_AssistMode_Flag == PBU_AssistMode_Flag_Walk)||(PBU_AssitMode_Status.PBU_AssistMode_Flag == PBU_AssistMode_Flag_WalkSby))
  251. LongDelayTimeCnt_Up = SysTime_5ms;
  252. if(TimeCntDiff_5ms(LongDelayTimeCnt_Up) >= FAILURE_TIME)
  253. {
  254. if(*(Key_Up.ADC_result)<1000)
  255. {
  256. Key_Flag.add_long_press = 1;
  257. PBU_hardwareErrorCode.ERROR_Bit.Key_Add = 0x01;
  258. Flag4_1 = 0;
  259. }
  260. }
  261. }
  262. else if(*(Key_Up.ADC_result)>3000)
  263. {
  264. Key_Flag.add_long_press = 0;
  265. Flag4_1 = 0;
  266. }
  267. /* Processing LIGHT key */
  268. if(*(Key_Light.ADC_result)<1000)
  269. {
  270. if(Flag6_1 == 0)
  271. {
  272. LongDelayTimeCnt_Light = SysTime_5ms;
  273. Flag6_1 = 1;
  274. }
  275. /* Handle long press 10s */
  276. if(TimeCntDiff_5ms(LongDelayTimeCnt_Light) >= FAILURE_TIME)
  277. {
  278. if(*(Key_Light.ADC_result)<1000)
  279. {
  280. Key_Flag.light_long_press = 1;
  281. PBU_hardwareErrorCode.ERROR_Bit.Key_Light = 0x01;
  282. Flag6_1 = 0;
  283. }
  284. }
  285. }
  286. else if(*(Key_Light.ADC_result)>3000)
  287. {
  288. Key_Flag.light_long_press = 0;
  289. Flag6_1 = 0;
  290. }
  291. }
  292. void Scan_Key_ADC_Error(void)
  293. {
  294. static uint16_t On_Off_error_cnt = 0;
  295. static uint16_t Walk_error_cnt = 0;
  296. static uint16_t Up_error_cnt = 0;
  297. static uint16_t Down_error_cnt = 0;
  298. static uint16_t Light_error_cnt = 0;
  299. #if 0
  300. ////////////On_Off判断////////////////////////
  301. if((*(Key_On_Off.ADC_result)>1000)&&(*(Key_On_Off.ADC_result)<3000))
  302. {
  303. On_Off_error_cnt++;
  304. }
  305. else
  306. {
  307. On_Off_error_cnt=0;
  308. }
  309. if(On_Off_error_cnt>5000)
  310. {
  311. PBU_hardwareErrorCode.ERROR_Bit.Key_Power = 0x01;
  312. }
  313. #endif
  314. ////////////Walk判断////////////////////////
  315. if((*(Key_Walk.ADC_result)>1000)&&(*(Key_Walk.ADC_result)<3000))
  316. {
  317. Walk_error_cnt++;
  318. }
  319. else
  320. {
  321. Walk_error_cnt=0;
  322. }
  323. if(Walk_error_cnt>5000)
  324. {
  325. PBU_hardwareErrorCode.ERROR_Bit.Key_Walk = 0x01;
  326. }
  327. ////////////Add判断////////////////////////
  328. if((*(Key_Up.ADC_result)>1000)&&(*(Key_Up.ADC_result)<3000))
  329. {
  330. Up_error_cnt++;
  331. }
  332. else
  333. {
  334. Up_error_cnt=0;
  335. }
  336. if(Up_error_cnt>5000)
  337. {
  338. PBU_hardwareErrorCode.ERROR_Bit.Key_Add = 0x01;
  339. }
  340. ////////////Down判断////////////////////////
  341. if((*(Key_Down.ADC_result)>1000)&&(*(Key_Down.ADC_result)<3000))
  342. {
  343. Down_error_cnt++;
  344. }
  345. else
  346. {
  347. Down_error_cnt=0;
  348. }
  349. if(Down_error_cnt>5000)
  350. {
  351. PBU_hardwareErrorCode.ERROR_Bit.Key_Dec = 0x01;
  352. }
  353. ////////////Light判断////////////////////////
  354. if((*(Key_Light.ADC_result)>1000)&&(*(Key_Light.ADC_result)<3000))
  355. {
  356. Light_error_cnt++;
  357. }
  358. else
  359. {
  360. Light_error_cnt=0;
  361. }
  362. if(Light_error_cnt>5000)
  363. {
  364. PBU_hardwareErrorCode.ERROR_Bit.Key_Light = 0x01;
  365. }
  366. }
  367. void Power_Mos_Check(void)
  368. {
  369. static uint16_t Power_Mos_error_cnt = 0;
  370. if((ADC_Result[0]>2000)&&(*(Key_On_Off.ADC_result)>3000))
  371. {
  372. Power_Mos_error_cnt ++;
  373. }
  374. else
  375. Power_Mos_error_cnt=0;
  376. if(Power_Mos_error_cnt>3)
  377. {
  378. Power_Mos_error_cnt=0;
  379. PBU_ErrorCode.ERROR_Bit.MOS_Fault =1;
  380. }
  381. }
  382. void Vin_test_Check(void)
  383. {
  384. if((ADC_Result[0]<3000)&&(ADC_Result[0]>1000))
  385. {
  386. Vin_test_TimeCnt=SysTime_5ms;
  387. }
  388. if(TimeCntDiff_5ms(Vin_test_TimeCnt) >= 400)
  389. {
  390. PBU_ErrorCode.ERROR_Bit.Hardware_Fault =1;
  391. }
  392. }