
#include "uart.h"
#include "data.h"
#include "lcd.h"
#include "uart_drv.h"
#include "sys.h"
#include "stdio.h"
#include "pit.h"
#include "pit_drv.h"
#include "ioctrl_drv.h"

UINT8 Frame_AA_OK = 0;
UINT16 Frame_count = 0;

UINT8 Rx_Buffer_short[256]={0};
UINT8 Rx_Buffer_long[1024+2]={0};

UINT16  Rx_Count_short=0;
UINT16 Rx_Count_long=0;
UINT16 Rx_Num =0;
UINT16 Respond_Num = 0;
UINT16 Respond_Count= 0;

UINT8 Respond_AA_OK = 0;
//UINT8  gRxBuf[RX_SIZE] ;
UINT16 gRxfist ;
UINT8 Frameend[4] = {0xE4,0x1B,0x11,0xEE};

extern void UART_Init(UART_TypeDef *UARTx,u32 baudrate,u8 parity,u8 intMode);
//void Uart_Init(UINT32 pclk,UINT32 bound)
{	
	UINT32 band_rate=0;
	//ÉèÖÃ²¨ÌØÂÊ£º115200
	SCI3->SCIBRDF=(((pclk*8/bound)+1)/2)&0x003f;
	band_rate =(pclk*4/bound)>>6;
	SCI3->SCIBDH  =(UINT8)((band_rate>>8)&0x00ff);
	SCI3->SCIBDL  =(UINT8)(band_rate&0x00ff);

	SCI3->SCICR2 |= (1<<2)|(1<<3)|(1<<5);
	NVIC_Init(0, 0, SCI3_IRQn, 2);
}

void SCI3_IRQHandler(void)
{
	UINT8 i = 0;
/*
	if(FF_Update_Flag==0)
	{
		if((SCI3->SCISR1 & 0x20)== 0x20)
		{
			i = SCI3->SCIDRL;
//			SCI3->SCIDRL = i;
			if(!Frame_AA_OK && i == 0xaa)
			{
				Frame_AA_OK = 1;
			}

			if(Frame_AA_OK)
			{
				if(Rx_Count_long>= 1024+2)
				{
					Rx_Count_long = 0;
				}
				Rx_Buffer_short[Rx_Count_short++]= i;
				Rx_Buffer_long[Rx_Count_long++]= i;
				Rx_Num++;
				

				if(Rx_Count_short > 7 && Rx_Buffer_short[Rx_Count_short - 4] == Frameend[0] && Rx_Buffer_short[Rx_Count_short - 3] == Frameend[1] && Rx_Buffer_short[Rx_Count_short - 2] == Frameend[2] && Rx_Buffer_short[Rx_Count_short - 1] == Frameend[3])
				{			
					Frame_AA_OK = 0;
					Rx_Count_short = 0;
				}
			}
			if(Frame_AA_OK && Rx_Count_short >= 255)
			{
				Frame_AA_OK = 0;
				Rx_Count_short = 0;
			}

		}
	}
	else if(FF_Update_Flag==1)
	{
		if((SCI3->SCISR1 & 0x20)== 0x20)
		{
			gUsartRx.Buf[gUsartRx.Count++] = SCI3->SCIDRL;

			if(gUsartRx.Count>=RX_SIZE)
			{
				LT_UsartTimeDisable();
				PIT1->PCSR |=0x04;
				gUsartRx.Flag=1;
			}
			else LT_UsartTimeSetCounter(35156);	//15ms
		}
	}
	*/
}

void SerialOutChar(char c)
{
	if (c == '\n')
		SerialOutChar('\r');

    while((SCI3->SCISR1 & 0x80) == 0x00);
	SCI3->SCIDRL = c;
	while((SCI3->SCISR1 & 0x40) == 0x00);
}

void SerialOutString(char *c)
{
	char *p = c;
	while(*p)
		SerialOutChar(*p++);
}


void SendData(UINT16 data)
{
	SCI3->SCIDRL =(UINT8)data;
}


void LT_UsartRxDisable(void)
{
	SCI3->SCICR2 |= 0x20;
}

void LT_UsartRxEnable(void)
{
	SCI3->SCICR2 &=~(0x20);
}

void LT_ClearRxBuf(void)
{
	UINT16 i = 0;
	for(i = 0 ; i < RX_SIZE ; i++)
	{
		gUsartRx.Buf[i] = 0;
	}
}

void LT_SingleByteToPc(UINT8 val)
{
	SCI3->SCIDRL =val;
	while ((SCI3->SCISR1 & 0x40) == 0x00){};
}

//void LT_DataToPc(UINT8 *buf,UINT8 len)
//{
//	UINT16 i = 0;
//
//		for(i = 0 ; i < len ; i++)
//		{
//			 SendData((UINT8) buf[i]);
//			while (UART_GetFlagStatus(UART_FLAG_TC) == 0){};
//		}
//}

void LT_DataToPc(UINT8 *buf,UINT8 len)
{
	UINT16 i = 0;

	UINT16 TxToPc_crc;
	UINT8 crc[2] = {0};
	UINT8 buff[50] = {0};
	TxToPc_crc = Rx_CRC_CCITT(buf,len);
	crc[0] = (UINT8)((TxToPc_crc >> 8) & 0x00ff);
	crc[1] = (UINT8)(TxToPc_crc & 0x00ff);
	buff[0] = 0xAA;
	for(i = 1 ; i <= len ; i++)
	{
		buff[i] = buf[i-1];
	}
	buff[len+1] = crc[0];
	buff[len + 2] = crc[1];
	buff[len + 3] = 0xE4;
	buff[len + 4] = 0x1B;
	buff[len + 5] = 0x11;
	buff[len + 6] = 0xEE;


		for(i = 0 ; i < len +7; i++)
		{
			SCI3->SCIDRL = buff[i];
			while ((SCI3->SCISR1 & 0x40) == 0x00){};
		}

}

void LT_DataToPc_NoFrame(UINT8 *buf,UINT8 len)
{
	UINT16 i = 0;

	UINT16 TxToPc_crc;
	UINT8 crc[2] = {0};
	UINT8 buff[30] = {0};
	TxToPc_crc = Rx_CRC_CCITT(buf,len);
	crc[0] = (UINT8)((TxToPc_crc >> 8) & 0x00ff);
	crc[1] = (UINT8)(TxToPc_crc & 0x00ff);
	for(i = 0 ; i < len ; i++)
	{
		buff[i] = buf[i];
	}
	buff[len] = crc[0];
	buff[len + 1] = crc[1];

	for(i = 0 ; i < len +2; i++)
	{
		SendData((UINT8) buff[i]);
		while ((SCI3->SCISR1 & 0x40) == 0x00){};
	}
}


