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. 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. ACC2_PWM();
  93. Uart1_Time_Out();
  94. Uart2_Time_Out();
  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. }