#include "common.h"
#include "delay.h"
#include "drv_usart.h"
#include "drv_usart_2.h"
#include "drv_io.h"
#include "drv_can.h"
#include "measure_temprature.h"
#include "app_rs485_1.h"
#include "app_rs485_2.h"
#include "app.h"
#include "app_can.h"
#include "app_bms_1.h"
#include "app_bms_2.h"
#include "app_end_ctr.h"
#include "app_adas.h"
#include "low_power.h"
#include "hardware_test.h"

static uint32_t delay = 0;
uint32_t utc_seconds = 1505216210;// 2017/9/12  19:37:50
uint16_t s_ms ;

void systick_close(void)
{
	//close tick
	
	SysTick->CTRL  &= ~SysTick_CTRL_TICKINT_Msk;
	SysTick->CTRL  &= ~SysTick_CTRL_ENABLE_Msk;

}

void delay_1us(uint16_t cnt)
{
	uint32_t  count = 3*cnt;
	while(count--); 
}

/*!
    \brief      configure systick
    \param[in]  none
    \param[out] none
    \retval     none
*/

void systick_config(void)
{
	if (SysTick_Config(SystemCoreClock / 1000))
	{ 
		/* Capture error */ 
		while (1);
	}
    NVIC_SetPriority(SysTick_IRQn, 0x00U);

	delay = 0;

	s_ms = 0;
}

/*!
    \brief      delay a time in milliseconds
    \param[in]  count: count in milliseconds
    \param[out] none
    \retval     none
*/
void delay_1ms(uint32_t count)
{
    delay = count;

    while(0 != delay);
}

#if 0
/*!
    \brief      delay decrement
    \param[in]  none
    \param[out] none
    \retval     none
*/
void delay_decrement(void)
{
    if (0 != delay){ 
        delay--;
    }
}
#endif

void SysTick_Handler(void)
{
	//uint8_t i,j;
	
	if(++s_ms >= 1000)
	{
		utc_seconds++;
		s_ms = 0;

		Measure_Temprature_Delay();

		Save_Param_Time_Out();

	}
		
	if (0 != delay){ 
        delay--;
    }

	ACC2_PWM();
	
	Uart1_Time_Out();

	Uart2_Time_Out();

	ACC12_OVER_Loader_Timeout();

	if(sub_rs485_time_out_1.set)
	{
		if(++sub_rs485_time_out_1.count >= RS485_COM_TIMEOUT)
		{
			
			RS485_busy_1 = 0;
			sub_rs485_time_out_1.set = 0;
			sub_rs485_time_out_1.count = 0;
			if(sub_rs485_time_out_1.com_err_flag == 0)
			{
				sub_rs485_time_out_1.com_err_flag = 1;
				sub_rs485_time_out_1.com_err_count = 1;
			}
			else
			{
				if(++sub_rs485_time_out_1.com_err_count >= RS485_COM_ERROR)
				{
					sub_rs485_time_out_1.com_err_count = 0;
					g_event |= SUB_BMS_1_RS485_DISC_EVENT;
				}
			}
		}
	}
	
	if(sub_rs485_time_out_2.set)
	{
		if(++sub_rs485_time_out_2.count >= RS485_COM_TIMEOUT)
		{
			
			RS485_busy_2 = 0;
			sub_rs485_time_out_2.set = 0;
			sub_rs485_time_out_2.count = 0;
			if(sub_rs485_time_out_2.com_err_flag == 0)
			{
				sub_rs485_time_out_2.com_err_flag = 1;
				sub_rs485_time_out_2.com_err_count = 1;
			}
			else
			{
				if(++sub_rs485_time_out_2.com_err_count >= RS485_COM_ERROR)
				{
					sub_rs485_time_out_2.com_err_count = 0;
					g_event |= SUB_BMS_2_RS485_DISC_EVENT;
				}
			}
		}
	}

#if 0
	Send_Sub_BMS_CMD_Delay();
#else
if(send_delay.set)
	{
		++send_delay.count;
		if(send_delay.count >= 400)
		{
			send_delay.count = 0;
			if(update_bat.ub_bat != UPDATE_BAT_2)
				g_event |= SUB_BMS_2_SEND_CMD_EVENT;
		}
		else if(send_delay.count == 200)
		{
			if(update_bat.ub_bat != UPDATE_BAT_1)
				g_event |= SUB_BMS_1_SEND_CMD_EVENT;
		}
	}
#endif
	//Check_CB_Oper_Sta_Delay();
	
	if(side_stay_dec_delay.set)
		side_stay_dec_delay.count++;
	if(soak_dec_delay.set)
		soak_dec_delay.count++;
	if(sti_dec_delay.set)
		sti_dec_delay.count++;
	if(repair_dec_delay.set)
		repair_dec_delay.count++;
	if(qd_dec_delay.set)
		qd_dec_delay.count++;
	if(xl_dec_delay.set)
		xl_dec_delay.count++;
	if(acc2_dec_delay.set)
		acc2_dec_delay.count++;
	
	
	
	if(left_light_delay.set)
	{
		++left_light_delay.count;
		if(left_light_delay.count >= ZONG_TIME)
		{
			Left_Light_Enable(1);
			left_light_delay.count = 0;
		}
		else if(left_light_delay.count >= LIANG_TIME)
		{
			Left_Light_Enable(0);
		}
	}
	if(right_light_delay.set)
	{
		++right_light_delay.count;
		if(right_light_delay.count >= ZONG_TIME)
		{
			Right_Light_Enable(1);
			right_light_delay.count = 0;
		}
		else if(right_light_delay.count >= LIANG_TIME)
		{
			Right_Light_Enable(0);
		}
	}



	Bms_1_Self_Send_Timeout();

	Bms_2_Self_Send_Timeout();

	End_Ctr_Self_Send_Timeout();

	ADAS_Timeout();

	Shield_XL_Timeout();

	Check_Charger_Timeout();

	Series_Delay_Timeout();

	Enter_Sleep_Delay_Timeout();

	HT_Reboot_Timeout();
	
}




