#include "HW_Config.h"
#include "uart_fecbus_drv.h"
#include "CnCpp.h"

#define FECBUS_UART 	2

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

#define FECBUS_UART_HEAD 	0x7E
#define FECBUS_UART_END 	0x7E

T_FECBus_RX_Prm fecbus_rx_prm;

unsigned char fecbus_uart_tx[32];

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

void set_fecbus_tx_data(unsigned char * pdata)//fec??¨º?¡ê??¨®¨¦?o¨®??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++){
		fecbus_uart_tx[i] = pdata[i];
	}
	
	fecbus_uart_tx[len] = pdata[16];
	fecbus_uart_tx[len+1] = pdata[17];
	fecbus_uart_tx[len+2] = pdata[18];
	
	len+=3;
	
	uart_tx_over(FECBUS_UART, fecbus_uart_tx, len);
}

void fecbus_rx_timeout_tick(void)//1ms in irq
{
	if(fecbus_rx_prm.starttime_flag){
		fecbus_rx_prm.timecount++;
		if(fecbus_rx_prm.timecount > 30){//9600 19byte:20ms
			fecbus_rx_prm.state = 0;
			fecbus_rx_prm.starttime_flag = 0;
			fecbus_rx_prm.timecount = 0;
		}
	}
}
	
void fecbus_rx_irq(void)
{
	unsigned short cc;
	
	if(LL_USART_IsActiveFlag_RXNE(USART2))
	{
		cc = LL_USART_ReceiveData8(USART2);
		
		switch(fecbus_rx_prm.state){
			case 0 :
								if(0x7E == cc){
									uart_info_8bit[FECBUS_UART].rx_index = 0;
									uart_info_8bit[FECBUS_UART].rx_buf[uart_info_8bit[FECBUS_UART].rx_index] = cc;
									uart_info_8bit[FECBUS_UART].rx_len = 25;//>19
									fecbus_rx_prm.starttime_flag = 1;
									fecbus_rx_prm.state++;
								}
								break;
								
			case 1:
								uart_info_8bit[FECBUS_UART].rx_index++;
								uart_info_8bit[FECBUS_UART].rx_buf[uart_info_8bit[FECBUS_UART].rx_index] = cc;
								if(7 == uart_info_8bit[FECBUS_UART].rx_index){
									uart_info_8bit[FECBUS_UART].rx_len = 11 + cc;
									if(uart_info_8bit[FECBUS_UART].rx_len > 19){
										fecbus_rx_prm.state = 0;
										fecbus_rx_prm.starttime_flag = 0;
										fecbus_rx_prm.timecount = 0;
									}else{
										fecbus_rx_prm.state++;
									}
								}else
								if(uart_info_8bit[FECBUS_UART].rx_index > 7){
									fecbus_rx_prm.state = 0;
									fecbus_rx_prm.starttime_flag = 0;
									fecbus_rx_prm.timecount = 0;
								}
								break;
			
			case 2:
								uart_info_8bit[FECBUS_UART].rx_index++;
								uart_info_8bit[FECBUS_UART].rx_buf[uart_info_8bit[FECBUS_UART].rx_index] = cc;
								if(((uart_info_8bit[FECBUS_UART].rx_len - 1) == uart_info_8bit[FECBUS_UART].rx_index)&&(fecbus_rx_prm.timecount < FECBUS_RX_TIMEOUT)&&(cc == 0x7E)){
									uart_info_8bit[FECBUS_UART].rx_complete = 1;
									fecbus_rx_prm.state = 0;
									fecbus_rx_prm.starttime_flag = 0;
									fecbus_rx_prm.timecount = 0;
								}else
								if(uart_info_8bit[FECBUS_UART].rx_index > 19){
									fecbus_rx_prm.state = 0;
									fecbus_rx_prm.starttime_flag = 0;
									fecbus_rx_prm.timecount = 0;
								}
								break;
			
			default:
								fecbus_rx_prm.state = 0;
								fecbus_rx_prm.starttime_flag = 0;
								fecbus_rx_prm.timecount = 0;
								break;
		}
	}
}

void uart2_rx_task(void)
{
	static unsigned int set_crc_error_flag = 0;
	
	if(uart_info_8bit[FECBUS_UART].rx_complete){
		uart_info_8bit[FECBUS_UART].rx_complete = 0;
		if(!check_crc(uart_info_8bit[FECBUS_UART].rx_buf,(uart_info_8bit[FECBUS_UART].rx_len-1))){
			LoadCRTViaFECBbus(uart_info_8bit[FECBUS_UART].rx_buf);
		}else{
			if((0x7E == uart_info_8bit[FECBUS_UART].rx_buf[0])&&\
				(0x7E == uart_info_8bit[FECBUS_UART].rx_buf[uart_info_8bit[FECBUS_UART].rx_len-1])&&\
				(0 == uart_info_8bit[FECBUS_UART].rx_buf[1])){
				
				set_crc_error_flag = 0;
			}
		}
		
		if((set_crc_error_flag)&&(0 == uart_info_8bit[FECBUS_UART].rx_buf[6])){
			set_crc_error_flag = 0;
			//SetRpyFECBusCRCError();
		}
	}
}
void uart_fecbus_task(void)
{
	uart2_rx_task();
}

