delay.c 4.8 KB

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