delay.c 4.9 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. void systick_close(void)
  22. {
  23. //close tick
  24. SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
  25. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  26. }
  27. void delay_1us(uint16_t cnt)
  28. {
  29. uint32_t count = 3*cnt;
  30. while(count--);
  31. }
  32. /*!
  33. \brief configure systick
  34. \param[in] none
  35. \param[out] none
  36. \retval none
  37. */
  38. void systick_config(void)
  39. {
  40. if (SysTick_Config(SystemCoreClock / 1000))
  41. {
  42. /* Capture error */
  43. while (1);
  44. }
  45. NVIC_SetPriority(SysTick_IRQn, 0x00U);
  46. s_ms = 0;
  47. }
  48. /*!
  49. \brief delay a time in milliseconds
  50. \param[in] count: count in milliseconds
  51. \param[out] none
  52. \retval none
  53. */
  54. void delay_1ms(uint32_t count)
  55. {
  56. uint64_t time = shark_mseconds + count;
  57. while (time > shark_mseconds);
  58. }
  59. #if 0
  60. /*!
  61. \brief delay decrement
  62. \param[in] none
  63. \param[out] none
  64. \retval none
  65. */
  66. void delay_decrement(void)
  67. {
  68. if (0 != delay){
  69. delay--;
  70. }
  71. }
  72. #endif
  73. void SysTick_Handler(void)
  74. {
  75. shark_mseconds++;
  76. //uint8_t i,j;
  77. if (BAT1_IS_OPEN() && sub_bms_info_1.open_times < 0xFFFF) {
  78. sub_bms_info_1.open_times++;
  79. }
  80. if (BAT2_IS_OPEN() && sub_bms_info_2.open_times < 0xFFFF) {
  81. sub_bms_info_2.open_times++;
  82. }
  83. if(++s_ms >= 1000)
  84. {
  85. utc_seconds++;
  86. s_ms = 0;
  87. Measure_Temprature_Delay();
  88. Save_Param_Time_Out();
  89. }
  90. ACC2_PWM();
  91. Uart1_Time_Out();
  92. Uart2_Time_Out();
  93. ACC12_OVER_Loader_Timeout();
  94. if(sub_rs485_time_out_1.set)
  95. {
  96. if(++sub_rs485_time_out_1.count >= RS485_COM_TIMEOUT)
  97. {
  98. RS485_busy_1 = 0;
  99. sub_rs485_time_out_1.set = 0;
  100. sub_rs485_time_out_1.count = 0;
  101. if(sub_rs485_time_out_1.com_err_flag == 0)
  102. {
  103. sub_rs485_time_out_1.com_err_flag = 1;
  104. sub_rs485_time_out_1.com_err_count = 1;
  105. }
  106. else
  107. {
  108. if(++sub_rs485_time_out_1.com_err_count >= RS485_COM_ERROR)
  109. {
  110. sub_rs485_time_out_1.com_err_count = 0;
  111. g_event |= SUB_BMS_1_RS485_DISC_EVENT;
  112. // if(test_info.ti_set)
  113. test_info.ti_bms_1.bms_err_cnt++;
  114. }
  115. }
  116. // if(test_info.ti_set)
  117. test_info.ti_bms_1.bms_err_timeout_cnt++;
  118. }
  119. }
  120. if(sub_rs485_time_out_2.set)
  121. {
  122. if(++sub_rs485_time_out_2.count >= RS485_COM_TIMEOUT)
  123. {
  124. RS485_busy_2 = 0;
  125. sub_rs485_time_out_2.set = 0;
  126. sub_rs485_time_out_2.count = 0;
  127. if(sub_rs485_time_out_2.com_err_flag == 0)
  128. {
  129. sub_rs485_time_out_2.com_err_flag = 1;
  130. sub_rs485_time_out_2.com_err_count = 1;
  131. }
  132. else
  133. {
  134. if(++sub_rs485_time_out_2.com_err_count >= RS485_COM_ERROR)
  135. {
  136. sub_rs485_time_out_2.com_err_count = 0;
  137. g_event |= SUB_BMS_2_RS485_DISC_EVENT;
  138. // if(test_info.ti_set)
  139. test_info.ti_bms_2.bms_err_cnt++;
  140. }
  141. }
  142. // if(test_info.ti_set)
  143. test_info.ti_bms_2.bms_err_timeout_cnt++;
  144. }
  145. }
  146. #if 0
  147. Send_Sub_BMS_CMD_Delay();
  148. #else
  149. if(send_delay.set)
  150. {
  151. ++send_delay.count;
  152. if(send_delay.count >= 400)
  153. {
  154. send_delay.count = 0;
  155. if(update_bat.ub_bat != UPDATE_BAT_2)
  156. g_event |= SUB_BMS_2_SEND_CMD_EVENT;
  157. }
  158. else if(send_delay.count == 200)
  159. {
  160. if(update_bat.ub_bat != UPDATE_BAT_1)
  161. g_event |= SUB_BMS_1_SEND_CMD_EVENT;
  162. }
  163. }
  164. #endif
  165. //Check_CB_Oper_Sta_Delay();
  166. if(side_stay_dec_delay.set)
  167. side_stay_dec_delay.count++;
  168. if(soak_dec_delay.set)
  169. soak_dec_delay.count++;
  170. if(sti_dec_delay.set)
  171. sti_dec_delay.count++;
  172. if(repair_dec_delay.set)
  173. repair_dec_delay.count++;
  174. if(qd_dec_delay.set)
  175. qd_dec_delay.count++;
  176. if(xl_dec_delay.set)
  177. xl_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. Shield_XL_Timeout();
  211. Check_Charger_Timeout();
  212. Series_Delay_Timeout();
  213. Enter_Sleep_Delay_Timeout();
  214. HT_Reboot_Timeout();
  215. }