#include "common.h" 
#include "drv_io.h"
#include "hardware_test.h"
#include "app_rs485_1.h"

CHECK_CHARGER check_charger;
DELAY_COMMON side_stay_dec_delay;
DELAY_COMMON sti_dec_delay;
DELAY_COMMON repair_dec_delay;
DELAY_COMMON qd_dec_delay;
DELAY_COMMON xl_dec_delay;
DELAY_COMMON acc2_dec_delay;
DELAY_COMMON acc2_delay;

DELAY_COMMON left_light_delay;
DELAY_COMMON right_light_delay;

DELAY_COMMON shield_xl;

DELAY_COMMON charge_delay;

CTR_DELAY_COMMON soak_dec_delay;

uint32_t xl_count = 0;

#define ACC2_USE_PWM (1)

uint8_t battery_charged_full = 0;
uint8_t QD_switch_from = 0;


extern void S11_FL_On(uint8_t on);
extern uint8_t S11_May_Operate(void);

void Check_S11_May_Operate(void)
{
	if(QD_Dect() == 0 && ACC2_Is_On() == 0 && S11_May_Operate())
		S11_FL_On(0);
}
void Check_S11(uint8_t on,uint8_t from)
{
	if(on)
	{
		if(S11_May_Operate())
			S11_FL_On(1);
			
	}
	else
	{
		if(S11_May_Operate())
		{
			switch(from)
			{
				case FROM_QD:
					if(ACC2_Is_On() == 0)
						S11_FL_On(0);	
					break;
				case FROM_ACC12:
					if(QD_Dect() == 0)
						S11_FL_On(0);	
					break;
				default:
					return;
			} 
		}
	}
}
void QD_Enable_From(uint8_t on,uint8_t from)
{
	Check_S11(on,FROM_QD);
	QD_Enable(on);
	QD_switch_from = from;
}

uint8_t ACC2_Is_On(void)
{
	if(acc2_delay.set == 0&&(gpio_output_bit_get(GPIOC,GPIO_PIN_15) == 0|ACC2_Over_Loader_Dect()== 0))
		return 0;
	else
		return 1;

}

void ACC2_PWM(void)
{
	if(acc2_delay.set)
	{
		++acc2_delay.count;
		if(acc2_delay.count >= ACC2_ENABLE_TIMEOUT)
		{
			gpio_bit_write(GPIOC,GPIO_PIN_15,(bit_status)0x01);
			memset(&acc2_delay,0x00,sizeof(acc2_delay));
		}
		else
			gpio_bit_write(GPIOC,GPIO_PIN_15,(bit_status)(acc2_delay.count&0x01));
	}
	
}
void ACC2_Enable(uint8_t on)
{
#if ACC2_USE_PWM
	Check_S11(on,FROM_ACC12);
	
	if(on)
	{
		acc2_delay.set = 1;
		acc2_delay.count = 0;
		FL_Enable(1);
	}
	else
	{
		memset(&acc2_delay,0x00,sizeof(acc2_delay));
		gpio_bit_write(GPIOC,GPIO_PIN_15,(bit_status)0x00);
	}
#else
	gpio_bit_write(GPIOC,GPIO_PIN_15,(bit_status)(on));
#endif
}
void E_CTR_Dec_IRQ_Initial(void)
{
	rcu_periph_clock_enable(RCU_AF);
	
	nvic_irq_enable(EXTI10_15_IRQn,1,0);
	
	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOC, GPIO_PIN_SOURCE_10);
	exti_init(EXTI_10, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    exti_interrupt_flag_clear(EXTI_10);
	
	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOC, GPIO_PIN_SOURCE_11);
	exti_init(EXTI_11, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    exti_interrupt_flag_clear(EXTI_11);
	
	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOC, GPIO_PIN_SOURCE_12);
	exti_init(EXTI_12, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    exti_interrupt_flag_clear(EXTI_12);

	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOC, GPIO_PIN_SOURCE_14);
	exti_init(EXTI_14, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
	exti_interrupt_flag_clear(EXTI_14);

	//QD
	nvic_irq_enable(EXTI5_9_IRQn,1,0);
	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_6);
	exti_init(EXTI_6, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    exti_interrupt_flag_clear(EXTI_6);

	//ACC2
	nvic_irq_enable(EXTI0_IRQn,1,0);
	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOD, GPIO_PIN_SOURCE_0);
	exti_init(EXTI_0, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    exti_interrupt_flag_clear(EXTI_0);

	//XL
	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_7);
	exti_init(EXTI_7, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
    exti_interrupt_flag_clear(EXTI_7);

	
}
void E_CTR_Initial(void)
{
	/* enable the clock of peripherals */
    rcu_periph_clock_enable(RCU_GPIOC);
	gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_15);
	ACC2_Enable(0);
	
	gpio_init(GPIOC,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_14);

	rcu_periph_clock_enable(RCU_GPIOD);
	rcu_periph_clock_enable(RCU_AF);
	gpio_pin_remap_config(GPIO_PD01_REMAP,ENABLE);
	gpio_init(GPIOD,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
	//
	gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6|GPIO_PIN_7);
	Left_Light_Enable(0);
	Right_Light_Enable(0);

	rcu_periph_clock_enable(RCU_GPIOB);
	gpio_init(GPIOB,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_0|GPIO_PIN_1);
	Tail_Light_Enable(0);
	Carpet_Light_Enable(0);

	//
	gpio_init(GPIOC,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13);

	//
	rcu_periph_clock_enable(RCU_GPIOB);
	//gpio_pin_remap_config(GPIO_SWJ_NONJTRST_REMAP,ENABLE);
	gpio_init(GPIOB,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_5|GPIO_PIN_4);
	Lock_Enable(0);
	//QD_Enable(0);
	QD_Enable_From(0,5);
	gpio_init(GPIOB,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_6|GPIO_PIN_7);

	E_CTR_Dec_IRQ_Initial();

	side_stay_dec_delay.set = 1;
	side_stay_dec_delay.count = SIDE_STAY_DEC_TIMEOUT;
	soak_dec_delay.enable = 1;
	soak_dec_delay.set = 1;
	soak_dec_delay.count = SOAK_DEC_TIMEOUT;
	sti_dec_delay.set = 1;
	sti_dec_delay.count = STI_DEC_TIMEOUT;
	repair_dec_delay.set = 1;
	repair_dec_delay.count = REPAIR_DEC_TIMEOUT;
	qd_dec_delay.set = 1;
	qd_dec_delay.count = QD_DEC_TIMEOUT;
	xl_dec_delay.set = 1;
	xl_dec_delay.count = XL_DEC_TIMEOUT;
	acc2_dec_delay.set = 1;
	acc2_dec_delay.count = ACC2_DEC_TIMEOUT;
	

	memset(&left_light_delay,0x00,sizeof(DELAY_COMMON));
	memset(&right_light_delay,0x00,sizeof(DELAY_COMMON));

	memset(&shield_xl,0x00,sizeof(DELAY_COMMON));
	
}

void _CB_Operate_Initial(void)
{
	rcu_periph_clock_enable(RCU_GPIOB);
	gpio_init(GPIOB,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_15|GPIO_PIN_13|GPIO_PIN_14);
	
}

void Charge_Dec_IRQ_Initial(void)
{
	rcu_periph_clock_enable(RCU_AF);
	
	nvic_irq_enable(EXTI4_IRQn,1,0);
	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOC, GPIO_PIN_SOURCE_4);
	exti_init(EXTI_4, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    exti_interrupt_flag_clear(EXTI_4);
	
}
void GPIO_Initial(void)
{
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_GPIOB);
	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_AF);
	
	gpio_init(GPIOC,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
	gpio_init(GPIOC,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_1|GPIO_PIN_2);

	//
	gpio_init(GPIOA,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_12);

#if 0
#if 1
	gpio_bit_reset(GPIOA,GPIO_PIN_12);
	gpio_bit_reset(GPIOA,GPIO_PIN_8);

#else	
	gpio_bit_set(GPIOA,GPIO_PIN_12);
	gpio_bit_set(GPIOA,GPIO_PIN_8);
#endif
#else
	//test-start
	Can_Power_Enable(0);
	//test-end
#endif

	
	//
	gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP,ENABLE);
	gpio_init(GPIOA,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_15);
	gpio_init(GPIOB,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_3);
	Enable_12V(0);
	
	//CHARGER
	
	gpio_init(GPIOC,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_4);
	
	gpio_init(GPIOB,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_12);

	Charge_Dec_IRQ_Initial();



	//
	gpio_init(GPIOB,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_2);
	FL_Enable(0);
	
	
}

void Set_Charger_In(uint8_t value)
{
	if(battery_charged_full == 0)
	{
		check_charger.cc_is_charger_in = value;
		if(value)
			cang_wei = CW_CHE_SHANG_CHARGER;
		else
			cang_wei = CW_CHE_SHANG_NO_CHARGER;
	}
}
uint8_t IS_CHARGE_IN(void)
{
	return check_charger.cc_is_charger_in;
}

void Check_Charger_Timeout(void)
{
#if 0
	if(check_charger.cc_set)
	{
		if(++check_charger.cc_time_count >= CC_TIME_OUT_COUNT)
		{
			if(check_charger.cc_pulse_count >= CC_PULSE_MIN_COUNT)
			{
				check_charger.cc_is_charger_in = 1;	
			}
			else if(check_charger.cc_pull_out_set == 0)
			{
				check_charger.cc_is_charger_in = 1;	
			}
			else 
			{
				check_charger.cc_is_charger_in = 0;	
			}
			check_charger.cc_set = 0;
			check_charger.cc_time_count = 0;
			check_charger.cc_pulse_count = 0;
		}
	}
#endif

	if(charge_delay.set)
		charge_delay.count++;
	
#if 0
	if(check_charger.cc_pull_out_set)
	{
		if(++check_charger.cc_pull_out_time_count >= CC_PULL_OUT_TIME_OUT_COUNT)
		{
			memset(&check_charger, 0x00,sizeof(check_charger));	
		}
	}
#endif
}

void EXTI0_IRQHandler(void)
{
	if (RESET != exti_interrupt_flag_get(EXTI_0))
	{  
        exti_interrupt_flag_clear(EXTI_0);
		acc2_dec_delay.set = 1;
		acc2_dec_delay.count = 0;
    }
}
void EXTI1_IRQHandler(void)
{

	if (RESET != exti_interrupt_flag_get(EXTI_1))
	{  
        exti_interrupt_flag_clear(EXTI_1);
    }
}
void EXTI3_IRQHandler(void)
{
	if (RESET != exti_interrupt_flag_get(EXTI_3))
	{  
        exti_interrupt_flag_clear(EXTI_3);
    }
}
void EXTI4_IRQHandler(void)
{
	if (RESET != exti_interrupt_flag_get(EXTI_4))
	{  
        exti_interrupt_flag_clear(EXTI_4);

		if(gpio_input_bit_get(GPIOC,GPIO_PIN_4) == 0)
		{
#if 0
			if( check_charger.cc_is_charger_in)
				return;
			
			if(check_charger.cc_set == 0)
			{
				check_charger.cc_set = 1;
				check_charger.cc_time_count = 0;
				check_charger.cc_pulse_count = 0;
			}
			
			check_charger.cc_pulse_count++;
#else
		Set_Charger_In(1);
#endif
		}
#if 0
		else
		{
			if( check_charger.cc_is_charger_in == 0)
				return;

			check_charger.cc_pull_out_set = 1;
			check_charger.cc_pull_out_time_count = 0;
		}
#endif		
    }
}

void EXTI5_9_IRQHandler(void)
{
	if (RESET != exti_interrupt_flag_get(EXTI_5))
	{  
        exti_interrupt_flag_clear(EXTI_5);
    }

	if (RESET != exti_interrupt_flag_get(EXTI_6))
	{  
        exti_interrupt_flag_clear(EXTI_6);
		qd_dec_delay.set = 1;
		qd_dec_delay.count = 0;
    }

	if (RESET != exti_interrupt_flag_get(EXTI_7))
	{  
        exti_interrupt_flag_clear(EXTI_7);
		if(shield_xl.set == 0)
			++xl_count;
		else
			xl_count = 0;
    }
	
	if (RESET != exti_interrupt_flag_get(EXTI_8))
	{  
        exti_interrupt_flag_clear(EXTI_8);
		extern void ADAS_Measure_Ju_Li_1(void);
		ADAS_Measure_Ju_Li_1();
    }
	if (RESET != exti_interrupt_flag_get(EXTI_9))
	{  
        exti_interrupt_flag_clear(EXTI_9);
		extern void ADAS_Measure_Ju_Li_2(void);
		ADAS_Measure_Ju_Li_2();
    }

	
}

void EXTI10_15_IRQHandler(void)
{
	if (RESET != exti_interrupt_flag_get(EXTI_10))
	{  
        exti_interrupt_flag_clear(EXTI_10);
		side_stay_dec_delay.set = 1;
		side_stay_dec_delay.count = 0;
    }
	if (RESET != exti_interrupt_flag_get(EXTI_11))
	{  
        exti_interrupt_flag_clear(EXTI_11);
		if(soak_dec_delay.enable)
		{
			soak_dec_delay.set = 1;
			soak_dec_delay.count = 0;
		}
    }
	if (RESET != exti_interrupt_flag_get(EXTI_12))
	{  
        exti_interrupt_flag_clear(EXTI_12);
		sti_dec_delay.set = 1;
		sti_dec_delay.count = 0;
    }

	if (RESET != exti_interrupt_flag_get(EXTI_13))
	{  
        exti_interrupt_flag_clear(EXTI_13);
		
    }
	
	if (RESET != exti_interrupt_flag_get(EXTI_14))
	{  
        exti_interrupt_flag_clear(EXTI_14);

		if(ht_mode)
		{
			over_12V = 1;
			return;
		}
		extern void ACC12_OVER_Loader_Interrupt(void);
		ACC12_OVER_Loader_Interrupt();
	
		
    }
	if (RESET != exti_interrupt_flag_get(EXTI_15))
	{  
        exti_interrupt_flag_clear(EXTI_15);
    }
}

