#include "bl_common.h" 
#include "bl_drv_usart_2.h"

 /* transmit buffer and receive buffer */
static uint8_t tx_buffer[TX_2_BUFFER_SIZE];
static uint8_t rx_buffer[RX_2_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 uart2_tout;

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

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

uint16_t Get_RS485_2_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_2_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(USART1, USART_FLAG_TC));
		usart_data_transmit(USART1,tx_buffer[tx_count++]);
	}while(tx_count < tx_buffer_size);
#else
	//while(RESET == USART_GetFlagStatus(USART2, USART_FLAG_TC));
	//USART_SendData(USART2,tx_buffer[tx_count++]);

	usart_interrupt_enable(USART1, USART_INT_TC);
#endif	
	return 1;
}



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

	
	/* enable GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_AF);

    /* connect port to USARTx_Tx */
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);

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

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

	
#if UART_1_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(USART1); 

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


}



/*******************************************************************************
* Function Name  : USART2_IRQHandler
* Description    : This function handles USART2 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART1_IRQHandler(void)
{
	uint8_t c;
	
	/* 串口接收 */
	if(usart_flag_get(USART1, USART_FLAG_RBNE) == SET)
	{
		c = usart_data_receive(USART1);
		if (rx_count >= sizeof(rx_buffer))
		{
		    return;
		}
		//USART_ReceiverTimeOutCmd(USART2,DISABLE);
		Enable_Uart2_Timer(0,0);
		/* Read one byte from the receive data register */
		rx_buffer[rx_count++] = c;
				
		Enable_Uart2_Timer(1,5);
	}
	/* 发送 */
	if(usart_flag_get(USART1, 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(USART1,USART_INT_TC);
			
			//USART_ClearFlag(USART1, USART_FLAG_TC);
			return;
		}
		/* Write one byte to the transmit data register */
		usart_data_transmit(USART1,tx_buffer[tx_count++]);
	}
	
}

#if 0
// 
//用于系统调用printf
//
#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

PUTCHAR_PROTOTYPE
{
	// RS485发送使能
	gpio_bit_set(GPIOA, GPIO_PIN_4);
	
	usart_data_transmit(USART1,(uint8_t) ch);
	while(!(usart_flag_get(USART1, USART_FLAG_TC) == SET));
	// RS485接收使能

	gpio_bit_reset(GPIOA, GPIO_PIN_4);
	
	return ch;
}
#endif



