delay.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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()) {
  78. if (sub_bms_info_1.open_times < 0xFFFF) {
  79. sub_bms_info_1.open_times++;
  80. }
  81. } else if (sub_bms_info_1.open_times > 0) {
  82. sub_bms_info_1.open_times--;
  83. }
  84. if (BAT2_IS_OPEN()) {
  85. if (sub_bms_info_2.open_times < 0xFFFF) {
  86. sub_bms_info_2.open_times++;
  87. }
  88. } else if (sub_bms_info_2.open_times > 0) {
  89. sub_bms_info_2.open_times--;
  90. }
  91. if(++s_ms >= 1000)
  92. {
  93. utc_seconds++;
  94. s_ms = 0;
  95. Measure_Temprature_Delay();
  96. Save_Param_Time_Out();
  97. }
  98. ACC2_PWM();
  99. Uart1_Time_Out();
  100. Uart2_Time_Out();
  101. ACC12_OVER_Loader_Timeout();
  102. if(sub_rs485_time_out_1.set)
  103. {
  104. if(++sub_rs485_time_out_1.count >= RS485_COM_TIMEOUT)
  105. {
  106. RS485_busy_1 = 0;
  107. sub_rs485_time_out_1.set = 0;
  108. sub_rs485_time_out_1.count = 0;
  109. if(sub_rs485_time_out_1.com_err_flag == 0)
  110. {
  111. sub_rs485_time_out_1.com_err_flag = 1;
  112. sub_rs485_time_out_1.com_err_count = 1;
  113. }
  114. else
  115. {
  116. if(++sub_rs485_time_out_1.com_err_count >= RS485_COM_ERROR)
  117. {
  118. sub_rs485_time_out_1.com_err_count = 0;
  119. g_event |= SUB_BMS_1_RS485_DISC_EVENT;
  120. // if(test_info.ti_set)
  121. test_info.ti_bms_1.bms_err_cnt++;
  122. }
  123. }
  124. // if(test_info.ti_set)
  125. test_info.ti_bms_1.bms_err_timeout_cnt++;
  126. }
  127. }
  128. if(sub_rs485_time_out_2.set)
  129. {
  130. if(++sub_rs485_time_out_2.count >= RS485_COM_TIMEOUT)
  131. {
  132. RS485_busy_2 = 0;
  133. sub_rs485_time_out_2.set = 0;
  134. sub_rs485_time_out_2.count = 0;
  135. if(sub_rs485_time_out_2.com_err_flag == 0)
  136. {
  137. sub_rs485_time_out_2.com_err_flag = 1;
  138. sub_rs485_time_out_2.com_err_count = 1;
  139. }
  140. else
  141. {
  142. if(++sub_rs485_time_out_2.com_err_count >= RS485_COM_ERROR)
  143. {
  144. sub_rs485_time_out_2.com_err_count = 0;
  145. g_event |= SUB_BMS_2_RS485_DISC_EVENT;
  146. // if(test_info.ti_set)
  147. test_info.ti_bms_2.bms_err_cnt++;
  148. }
  149. }
  150. // if(test_info.ti_set)
  151. test_info.ti_bms_2.bms_err_timeout_cnt++;
  152. }
  153. }
  154. #if 0
  155. Send_Sub_BMS_CMD_Delay();
  156. #else
  157. if(send_delay.set)
  158. {
  159. ++send_delay.count;
  160. if(send_delay.count >= 400)
  161. {
  162. send_delay.count = 0;
  163. if(update_bat.ub_bat != UPDATE_BAT_2)
  164. g_event |= SUB_BMS_2_SEND_CMD_EVENT;
  165. }
  166. else if(send_delay.count == 200)
  167. {
  168. if(update_bat.ub_bat != UPDATE_BAT_1)
  169. g_event |= SUB_BMS_1_SEND_CMD_EVENT;
  170. }
  171. }
  172. #endif
  173. //Check_CB_Oper_Sta_Delay();
  174. if(side_stay_dec_delay.set)
  175. side_stay_dec_delay.count++;
  176. if(soak_dec_delay.set)
  177. soak_dec_delay.count++;
  178. if(sti_dec_delay.set)
  179. sti_dec_delay.count++;
  180. if(repair_dec_delay.set)
  181. repair_dec_delay.count++;
  182. if(qd_dec_delay.set)
  183. qd_dec_delay.count++;
  184. if(xl_dec_delay.set)
  185. xl_dec_delay.count++;
  186. if(acc2_dec_delay.set)
  187. acc2_dec_delay.count++;
  188. if(left_light_delay.set)
  189. {
  190. ++left_light_delay.count;
  191. if(left_light_delay.count >= ZONG_TIME)
  192. {
  193. Left_Light_Enable(1);
  194. left_light_delay.count = 0;
  195. }
  196. else if(left_light_delay.count >= LIANG_TIME)
  197. {
  198. Left_Light_Enable(0);
  199. }
  200. }
  201. if(right_light_delay.set)
  202. {
  203. ++right_light_delay.count;
  204. if(right_light_delay.count >= ZONG_TIME)
  205. {
  206. Right_Light_Enable(1);
  207. right_light_delay.count = 0;
  208. }
  209. else if(right_light_delay.count >= LIANG_TIME)
  210. {
  211. Right_Light_Enable(0);
  212. }
  213. }
  214. Bms_1_Self_Send_Timeout();
  215. Bms_2_Self_Send_Timeout();
  216. End_Ctr_Self_Send_Timeout();
  217. ADAS_Timeout();
  218. Shield_XL_Timeout();
  219. Check_Charger_Timeout();
  220. Series_Delay_Timeout();
  221. Enter_Sleep_Delay_Timeout();
  222. HT_Reboot_Timeout();
  223. }