#include "main.h"
#include "rtc.h"
#include "HW_config.h"
#include "w25qxx.h"
#include "load_gui_lib.h"
#include "fdcan_task.h"
#include "uart_printer_drv.h"
#include "uart_fecbus_drv.h"
#include "uart_fec_std_drv.h"
#include "uart_lp_test_drv.h"
#include "uart_task.h"
#include "relay_task.h"
#include "CnCpp.h"

unsigned int sys_tick_1ms_rdy = 0;
unsigned int sys_tick_10ms_rdy = 0;
unsigned int sys_tick_100ms_rdy = 0;
unsigned int sys_tick_200ms_rdy = 0;
unsigned int sys_tick_1s_rdy = 0;
unsigned int sys_tick_600S_rdy = 0;

volatile unsigned int sys_oldtime = 0;
volatile unsigned int sys_free_run_ms = 0;
volatile unsigned int sys_test_sp_time = 0;

unsigned int UserRequestReset = 0;
unsigned int SysLoopTick = 0;

unsigned char datatemp[128];
unsigned char tx_test_data[16] = {0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,\
																	0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};

void my_sys_tick(void)
{
	static unsigned int sys_tick_10ms_tick = 0;
	static unsigned int sys_tick_100ms_tick = 0;
	static unsigned int sys_tick_200ms_tick = 0;
	static unsigned int sys_tick_1s_tick = 0;
	static unsigned int sys_tick_600S_tick = 0;
	
	static unsigned int norflash_err_tick[2] = {0,0};

	sys_tick_1ms_rdy = 1;
	sys_free_run_ms++;
	
	fecbus_rx_timeout_tick();
	fec_std_rx_timeout_tick();
	test_rx_timeout_tick();
	
	Cpp_1Ms_Task();
	
	uart_tx_complete_delay();
	can_tx_complete_delay();
	
	if(norflash_get_busy_flag[0]){
		norflash_err_tick[0]++;
		if(norflash_err_tick[0] > 201000){
			norflash_err_tick[0] = 0;
			norflash_err_flag[0] = 1;
		}
	}else{
		norflash_err_tick[0] = 0;
		norflash_err_flag[0] = 0;
	}
	
	if(norflash_get_busy_flag[1]){
		norflash_err_tick[1]++;
		if(norflash_err_tick[1] > 201000){
			norflash_err_tick[1] = 0;
			norflash_err_flag[1] = 1;
		}
	}else{
		norflash_err_tick[1] = 0;
		norflash_err_flag[1] = 0;
	}
	
	sys_tick_10ms_tick++;
	if(sys_tick_10ms_tick > 9){
		sys_tick_10ms_tick = 0;
		sys_tick_10ms_rdy = 1;
	}
	
	sys_tick_100ms_tick++;
	if(sys_tick_100ms_tick > 99){
		sys_tick_100ms_tick = 0;
		sys_tick_100ms_rdy = 1;
		
		if(UserRequestReset != 0x12345678){
			SysLoopTick++;
			if(SysLoopTick < 100)
				LL_IWDG_ReloadCounter(IWDG1);
		}
		
		can_send_time_handle();
	}
	
	sys_tick_200ms_tick++;
	if(sys_tick_200ms_tick > 199){
		sys_tick_200ms_tick = 0;
		sys_tick_200ms_rdy = 1;
	}
	
	sys_tick_1s_tick++;
	if(sys_tick_1s_tick > 999){
		sys_tick_1s_tick = 0;
		sys_tick_1s_rdy = 1;
		
		sys_tick_600S_tick++;
		if(sys_tick_600S_tick > 599){
			sys_tick_600S_tick = 0;
			sys_tick_600S_rdy = 1;
		}
	}
}

void sys_error_led_task(void)
{
	if(!GetSystemErrorState()){
		//GPIOE->ODR ^=0x04;;
	}
}

void DoUpdataRtc()
{
	RTC_TimeShow();
}

void operation_finction(void)
{
	static unsigned int color_tick = 0;
	
	static unsigned int addr = 0;
	unsigned int i = 0;
	
	CppFreRun();
	
	if(sys_tick_1ms_rdy){
		sys_tick_1ms_rdy = 0;
		
		uart_interval_send();
	}
	
	if(sys_tick_10ms_rdy){
		sys_tick_10ms_rdy = 0;
		sys_error_led_task();
		Cpp_10Ms_Task();
	}
	
	if(sys_tick_100ms_rdy){
		sys_tick_100ms_rdy = 0;
		
		GPIOC->ODR ^=0x0020;
		
		Cpp_100Ms_Task();
		load_hzk_pic_task();
		
		relay_output_handle();
	}
	
	if(sys_tick_1s_rdy){
		sys_tick_1s_rdy = 0;
		Sys_1s_Load_Rdy();
		
		printer_task();
		DoUpdataRtc();
		
		sys_oldtime = sys_free_run_ms;
		CppIn();
		sys_test_sp_time = sys_free_run_ms - sys_oldtime; 
	}
	
	uart_task();
	can_task();
	
	if(sys_tick_600S_rdy){
		sys_tick_600S_rdy = 0;
	}
}

