delay.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "common.h"
  2. #include "delay.h"
  3. #include "drv_usart.h"
  4. #include "drv_usart_2.h"
  5. #include "drv_io.h"
  6. #include "drv_can.h"
  7. #include "measure_temprature.h"
  8. #include "app_rs485_1.h"
  9. #include "app_rs485_2.h"
  10. #include "app.h"
  11. #include "app_can.h"
  12. #include "app_bms_1.h"
  13. #include "app_bms_2.h"
  14. #include "app_end_ctr.h"
  15. #include "app_adas.h"
  16. #include "low_power.h"
  17. #include "hardware_test.h"
  18. #include "shark_xl.h"
  19. u64 shark_mseconds;
  20. uint32_t utc_seconds = 1505216210;// 2017/9/12 19:37:50
  21. uint16_t s_ms ;
  22. u64 shark_get_time_safe(void)
  23. {
  24. u64 time = shark_mseconds;
  25. fwdgt_counter_reload();
  26. Handle_Can_Data();
  27. return time;
  28. }
  29. void systick_close(void)
  30. {
  31. //close tick
  32. SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
  33. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  34. }
  35. void delay_1us(uint16_t cnt)
  36. {
  37. uint32_t count = 3*cnt;
  38. while(count--);
  39. }
  40. /*!
  41. \brief configure systick
  42. \param[in] none
  43. \param[out] none
  44. \retval none
  45. */
  46. void systick_config(void)
  47. {
  48. if (SysTick_Config(SystemCoreClock / 1000))
  49. {
  50. /* Capture error */
  51. while (1);
  52. }
  53. NVIC_SetPriority(SysTick_IRQn, 0x00U);
  54. s_ms = 0;
  55. }
  56. /*!
  57. \brief delay a time in milliseconds
  58. \param[in] count: count in milliseconds
  59. \param[out] none
  60. \retval none
  61. */
  62. void delay_1ms(uint32_t count)
  63. {
  64. uint64_t time = shark_mseconds + count;
  65. while (time > shark_mseconds) {
  66. fwdgt_counter_reload();
  67. }
  68. }
  69. #if 0
  70. /*!
  71. \brief delay decrement
  72. \param[in] none
  73. \param[out] none
  74. \retval none
  75. */
  76. void delay_decrement(void)
  77. {
  78. if (0 != delay){
  79. delay--;
  80. }
  81. }
  82. #endif
  83. void SysTick_Handler(void)
  84. {
  85. shark_mseconds++;
  86. if(++s_ms >= 1000)
  87. {
  88. utc_seconds++;
  89. s_ms = 0;
  90. Measure_Temprature_Delay();
  91. Save_Param_Time_Out();
  92. }
  93. shark_xl_tick();
  94. ACC2_PWM();
  95. Uart1_Time_Out();
  96. Uart2_Time_Out();
  97. if(sub_rs485_time_out_1.set)
  98. {
  99. if(++sub_rs485_time_out_1.count >= RS485_COM_TIMEOUT)
  100. {
  101. RS485_busy_1 = 0;
  102. sub_rs485_time_out_1.set = 0;
  103. sub_rs485_time_out_1.count = 0;
  104. if(sub_rs485_time_out_1.com_err_flag == 0)
  105. {
  106. sub_rs485_time_out_1.com_err_flag = 1;
  107. sub_rs485_time_out_1.com_err_count = 1;
  108. }
  109. else
  110. {
  111. if(++sub_rs485_time_out_1.com_err_count >= RS485_COM_ERROR)
  112. {
  113. sub_rs485_time_out_1.com_err_count = 0;
  114. g_event |= SUB_BMS_1_RS485_DISC_EVENT;
  115. // if(test_info.ti_set)
  116. test_info.ti_bms_1.bms_err_cnt++;
  117. }
  118. }
  119. // if(test_info.ti_set)
  120. test_info.ti_bms_1.bms_err_timeout_cnt++;
  121. }
  122. }
  123. if(sub_rs485_time_out_2.set)
  124. {
  125. if(++sub_rs485_time_out_2.count >= RS485_COM_TIMEOUT)
  126. {
  127. RS485_busy_2 = 0;
  128. sub_rs485_time_out_2.set = 0;
  129. sub_rs485_time_out_2.count = 0;
  130. if(sub_rs485_time_out_2.com_err_flag == 0)
  131. {
  132. sub_rs485_time_out_2.com_err_flag = 1;
  133. sub_rs485_time_out_2.com_err_count = 1;
  134. }
  135. else
  136. {
  137. if(++sub_rs485_time_out_2.com_err_count >= RS485_COM_ERROR)
  138. {
  139. sub_rs485_time_out_2.com_err_count = 0;
  140. g_event |= SUB_BMS_2_RS485_DISC_EVENT;
  141. // if(test_info.ti_set)
  142. test_info.ti_bms_2.bms_err_cnt++;
  143. }
  144. }
  145. // if(test_info.ti_set)
  146. test_info.ti_bms_2.bms_err_timeout_cnt++;
  147. }
  148. }
  149. #if 0
  150. Send_Sub_BMS_CMD_Delay();
  151. #else
  152. if(send_delay.set)
  153. {
  154. ++send_delay.count;
  155. if(send_delay.count >= 400)
  156. {
  157. send_delay.count = 0;
  158. g_event |= SUB_BMS_2_SEND_CMD_EVENT;
  159. }
  160. else if(send_delay.count == 200)
  161. {
  162. g_event |= SUB_BMS_1_SEND_CMD_EVENT;
  163. }
  164. }
  165. #endif
  166. //Check_CB_Oper_Sta_Delay();
  167. if(side_stay_dec_delay.set)
  168. side_stay_dec_delay.count++;
  169. if(soak_dec_delay.set)
  170. soak_dec_delay.count++;
  171. if(sti_dec_delay.set)
  172. sti_dec_delay.count++;
  173. if(repair_dec_delay.set)
  174. repair_dec_delay.count++;
  175. if(qd_dec_delay.set)
  176. qd_dec_delay.count++;
  177. if(acc2_dec_delay.set)
  178. acc2_dec_delay.count++;
  179. if(left_light_delay.set)
  180. {
  181. ++left_light_delay.count;
  182. if(left_light_delay.count >= ZONG_TIME)
  183. {
  184. Left_Light_Enable(1);
  185. left_light_delay.count = 0;
  186. }
  187. else if(left_light_delay.count >= LIANG_TIME)
  188. {
  189. Left_Light_Enable(0);
  190. }
  191. }
  192. if(right_light_delay.set)
  193. {
  194. ++right_light_delay.count;
  195. if(right_light_delay.count >= ZONG_TIME)
  196. {
  197. Right_Light_Enable(1);
  198. right_light_delay.count = 0;
  199. }
  200. else if(right_light_delay.count >= LIANG_TIME)
  201. {
  202. Right_Light_Enable(0);
  203. }
  204. }
  205. Bms_1_Self_Send_Timeout();
  206. Bms_2_Self_Send_Timeout();
  207. End_Ctr_Self_Send_Timeout();
  208. ADAS_Timeout();
  209. Check_Charger_Timeout();
  210. Series_Delay_Timeout();
  211. Enter_Sleep_Delay_Timeout();
  212. HT_Reboot_Timeout();
  213. }