delay.c 4.7 KB

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