functional_safety_key.c 8.4 KB

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