#include "common.h" 
#include "drv_usart.h"

 /* transmit buffer and receive buffer */
static uint8_t tx_buffer[TX_BUFFER_SIZE];
static uint8_t rx_buffer[RX_BUFFER_SIZE];
/* counter of transmit buffer and receive buffer */
static uint16_t tx_count = 0, rx_count = 0;
/* size of transmit buffer and receive buffer */
static uint32_t /*rx_buffer_size ,*/ tx_buffer_size;

DELAY_COMMON uart1_tout;


void Enable_Uart1_Timer(uint8_t enable,uint16_t timeout)
{
	if(enable)
	{
		uart1_tout.set = 1;
		uart1_tout.count = timeout;
	}
	else
	memset(&uart1_tout,0x00,sizeof(uart1_tout));
	
}

static void Reset_RX_Buffer(void)
{
	memset(rx_buffer,0x00,sizeof(rx_buffer));
	rx_count = 0;
}

uint16_t Get_RS485_Data(uint8_t * dbuf,uint16_t dbuf_len)
{
	if(dbuf == NULL || dbuf_len < rx_count)
		return 0;

	memcpy(dbuf,rx_buffer,rx_count);
	dbuf_len = rx_count;
	Reset_RX_Buffer();
	
	return dbuf_len;
}



int8_t Send_Data_RS485(uint8_t*data,uint16_t size)
{
	if(data == NULL || size == 0 || size > sizeof(tx_buffer))
		return 0;

	
	memcpy(tx_buffer,data,size);
	tx_count = 0;
	tx_buffer_size = size;
	
	//
#if 0

	do
	{
		while(RESET == usart_flag_get(USART0, USART_FLAG_TC));
		usart_data_transmit(USART0,tx_buffer[tx_count++]);
	}while(tx_count < tx_buffer_size);
#else
	//while(RESET == USART_GetFlagStatus(USART1, USART_FLAG_TC));
	//USART_SendData(USART1,tx_buffer[tx_count++]);
	usart_interrupt_enable(USART0, USART_INT_TC);
	usart_interrupt_flag_get(USART0, USART_INT_TC);
#endif	
	return 1;
}



/*
 * 函数名：USART1_Config
 * 描述  ：USART1 GPIO 配置,工作模式配置。9600 8-N-1
 * 输入  ：无
 * 输出  : 无
 * 调用  ：外部调用
 */
void Usart1_Initial(void)
{

	
	/* enable GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_AF);
	rcu_periph_clock_enable(RCU_USART0);
    /* connect port to USARTx_Tx */
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);

    /* connect port to USARTx_Rx */
    gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);

    /* USART configure */
	/* enable USART clock */
    
    usart_deinit(USART0);
    usart_baudrate_set(USART0, USART_0_BAUND);
    usart_word_length_set(USART0, USART_WL_8BIT);
    usart_stop_bit_set(USART0, USART_STB_1BIT);
    usart_parity_config(USART0, USART_PM_NONE);
    usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
    usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
    usart_receive_config(USART0, USART_RECEIVE_ENABLE);
    usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
	nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
	nvic_irq_enable(USART0_IRQn, 1, 0);
	usart_interrupt_enable(USART0, USART_INT_RBNE);

#if UART_0_IDAR
	/*USART_ClockInitTypeDef  USART_ClockInitStruct; 

	USART_ClockInitStruct.USART_Clock = USART_Clock_Disable;
	USART_ClockInitStruct.USART_CPOL = USART_CPOL_Low;
	USART_ClockInitStruct.USART_CPHA = USART_CPHA_2Edge;
	USART_ClockInitStruct.USART_LastBit = USART_LastBit_Disable;
	
	USART_ClockInit(USART1,  &USART_ClockInitStruct);*/

	
	usart_enable(USART0); 

	usart_prescaler_config(USART0, 0x01);
	usart_irda_lowpower_config(USART0, USART_IRLP_NORMAL);
	usart_irda_mode_enable(USART0);
#else
	usart_enable(USART0);
#endif
    
	
	
	
}



/*******************************************************************************
* Function Name  : USART1_IRQHandler
* Description    : This function handles USART1 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART0_IRQHandler(void)
{
	uint8_t c;
	
	/* 串口接收 */
	//if(usart_interrupt_flag_get(USART0, USART_FLAG_RBNE) == SET)
	if(usart_flag_get(USART0, USART_FLAG_RBNE) == SET)	
	{
		c = usart_data_receive(USART0);
		if (rx_count >= sizeof(rx_buffer))
		{
		    return;
		}
		//USART_ReceiverTimeOutCmd(USART1,DISABLE);
		Enable_Uart1_Timer(0,0);
		/* Read one byte from the receive data register */
		rx_buffer[rx_count++] = c;
				
		Enable_Uart1_Timer(1,5);
	}
	/* 发送 */
	//if(usart_interrupt_flag_get(USART0, USART_FLAG_TC) == SET)
	if(usart_flag_get(USART0, USART_FLAG_TC) == SET)
	{
		//USART_ClearFlag(USART1, USART_FLAG_TC);
		if (tx_count >= tx_buffer_size)
		{
			 /* Disable the EVAL_COM1 Transmit interrupt */
		  	usart_interrupt_disable(USART0,USART_INT_TC);
			//USART_ClearFlag(USART1, USART_FLAG_TC);
			return;
		}
		/* Write one byte to the transmit data register */
		usart_data_transmit(USART0,tx_buffer[tx_count++]);
	}

}




