#include "HW_Config.h"
#include "uart_fec_std_drv.h"
#include "CnCpp.h"

#define FEC_STD_UART 	4

#define FEC_STD_RX_TIMEOUT 	25//12:19200 25:9600  //9600 19byte:20ms

T_FEC_STD_RX_Prm fec_std_rx_prm;

unsigned char fec_std_uart_tx[32];

static unsigned int set_crc_error_flag = 0;

unsigned int fec_std_tx_is_busy(void)//1:is busy 0:no busy
{
	if(!get_uart_tx_complete_delay_flag(FEC_STD_UART))return 1;
	
	return 0;
}

void set_fec_std_tx_data(unsigned char * pdata)//fec格式，加上后面crc 2byte 与end
{
	unsigned int i = 0;
	unsigned int len = 0;
	
	len = pdata[7];
	if(len > 8)return;
	len += 8;
	for(i=0;i<len;i++){
		fec_std_uart_tx[i] = pdata[i];
	}
	
	fec_std_uart_tx[len] = pdata[16];
	fec_std_uart_tx[len+1] = pdata[17];
	fec_std_uart_tx[len+2] = pdata[18];
	
	len+=3;
	
	uart_tx_over(FEC_STD_UART, fec_std_uart_tx, len);
}

void fec_std_rx_timeout_tick(void)//1ms in irq
{
	if(fec_std_rx_prm.starttime_flag){
		fec_std_rx_prm.timecount++;
		if(fec_std_rx_prm.timecount > 30){//9600 19byte:20ms
			fec_std_rx_prm.state = 0;
			fec_std_rx_prm.starttime_flag = 0;
			fec_std_rx_prm.timecount = 0;
		}
	}
}
	
void fec_std_rx_irq(void)
{
	unsigned short cc;
	
	if(LL_USART_IsActiveFlag_RXNE(UART4))
	{
		cc = LL_USART_ReceiveData8(UART4);
		
		switch(fec_std_rx_prm.state){
			case 0 :
								if(FEC_STD_UART_HEAD == cc){
									uart_info_8bit[FEC_STD_UART].rx_index = 0;
									uart_info_8bit[FEC_STD_UART].rx_buf[uart_info_8bit[FEC_STD_UART].rx_index] = cc;
									uart_info_8bit[FEC_STD_UART].rx_len = 25;//>19
									fec_std_rx_prm.starttime_flag = 1;
									fec_std_rx_prm.state++;
									set_crc_error_flag = 0;
								}
								break;
								
			case 1:
								uart_info_8bit[FEC_STD_UART].rx_index++;
								uart_info_8bit[FEC_STD_UART].rx_buf[uart_info_8bit[FEC_STD_UART].rx_index] = cc;
								if(7 == uart_info_8bit[FEC_STD_UART].rx_index){
									uart_info_8bit[FEC_STD_UART].rx_len = 11 + cc;
									if(uart_info_8bit[FEC_STD_UART].rx_len > 19){
										fec_std_rx_prm.state = 0;
										fec_std_rx_prm.starttime_flag = 0;
										fec_std_rx_prm.timecount = 0;
									}else{
										fec_std_rx_prm.state++;
									}
								}else
								if(uart_info_8bit[FEC_STD_UART].rx_index > 7){
									fec_std_rx_prm.state = 0;
									fec_std_rx_prm.starttime_flag = 0;
									fec_std_rx_prm.timecount = 0;
								}
								break;
			
			case 2:
								uart_info_8bit[FEC_STD_UART].rx_index++;
								uart_info_8bit[FEC_STD_UART].rx_buf[uart_info_8bit[FEC_STD_UART].rx_index] = cc;
								if(((uart_info_8bit[FEC_STD_UART].rx_len - 1) == uart_info_8bit[FEC_STD_UART].rx_index)&&(fec_std_rx_prm.timecount < FEC_STD_RX_TIMEOUT)&&(cc == FEC_STD_UART_END)){
									uart_info_8bit[FEC_STD_UART].rx_complete = 1;
									fec_std_rx_prm.state = 0;
									fec_std_rx_prm.starttime_flag = 0;
									fec_std_rx_prm.timecount = 0;
								}else
								if(uart_info_8bit[FEC_STD_UART].rx_index > 19){
									fec_std_rx_prm.state = 0;
									fec_std_rx_prm.starttime_flag = 0;
									fec_std_rx_prm.timecount = 0;
								}
								break;
			
			default:
								fec_std_rx_prm.state = 0;
								fec_std_rx_prm.starttime_flag = 0;
								fec_std_rx_prm.timecount = 0;
								break;
		}
	}
}

void uart4_rx_task(void)
{
	if(uart_info_8bit[FEC_STD_UART].rx_complete){
		uart_info_8bit[FEC_STD_UART].rx_complete = 0;
		if(!check_crc(uart_info_8bit[FEC_STD_UART].rx_buf,(uart_info_8bit[FEC_STD_UART].rx_len-1))){
			LoadStdViaFECBus(uart_info_8bit[FEC_STD_UART].rx_buf);
		}else{
			if((FEC_STD_UART_HEAD == uart_info_8bit[FEC_STD_UART].rx_buf[0])&&\
				(FEC_STD_UART_END == uart_info_8bit[FEC_STD_UART].rx_buf[uart_info_8bit[FEC_STD_UART].rx_len-1])&&\
				(0 == uart_info_8bit[FEC_STD_UART].rx_buf[1])){
				
				set_crc_error_flag = 1;
			}
		}
		
		if((set_crc_error_flag)&&(0 == uart_info_8bit[FEC_STD_UART].rx_buf[6])){
			set_crc_error_flag = 0;
			SetRpyFECStdCRCError();
		}
	}
}

void uart_fec_std_task(void)
{
	uart4_rx_task();
}

