/**
******************************************************************************************************************************************************************************************
*@file                       user_uart.h
*@author         
*@data                       
*@version                    v1.0
*@par Copyright(c):
*
*@par History:
*     version:author,date,desc
*     v1.0   :create this file
*
*******************************************************************************************************************************************************************************************
*/
#ifndef _UI_UART_H_
#define _UI_UART_H_
#include "sdk_include.h"

#if (is_uart_dev(APP_UART) && (UART_USE_NUM == 1))
	#define UART_SEND_BUF_SIZE		517
	#define UART_RECV_BUF_SIZE		517
#else
	#define UART_SEND_BUF_SIZE		256
	#define UART_RECV_BUF_SIZE		256
#endif

typedef void (*uart_interrupt_handler)(void *param,uint8_t ch);

typedef struct UART_PARAM_S
{
	uint8_t 	tx_transmit_stopped;
	struct{
		volatile uint32_t read_index;			
		volatile uint32_t write_index;				
		volatile uint32_t bytes;
		uint8_t buf[UART_SEND_BUF_SIZE + 1];
	}Send;
	struct{
		volatile uint32_t read_index;
		volatile uint32_t write_index;
		volatile uint32_t bytes;
		uint8_t buf[UART_RECV_BUF_SIZE + 1];
	}Recv;
	uint8_t init;
	uint32_t wait;
	uint8_t  dev;
    uart_interrupt_handler uart_handler;
} UART_PARAM;


typedef struct _uart_config
{
	uint32_t 	boaud_rate;
	uint8_t 	length_bit;		   
	uint8_t 	stop_bit; 		  
	uint8_t 	parity_bit;	
	uint8_t  	mode;

	gpio_pin 	rtsnPin;				//串口接收流控脚
	gpio_pin 	ctsnPin;				//串口发送流控脚
	gpio_pin 	rxPin;					//串口接收脚
	gpio_pin 	txPin;					//串口发送脚

} uart_config;

typedef struct
{
    uart_interrupt_handler uart_handler;
}user_uart_cfg_parameter_t;

extern uint32_t ui_get_uart_fifo_avaiable(ws_dev dev);
extern void dev_isr(uart_typedef_t* uartx,UART_PARAM * pUart_Param);
extern UART_PARAM *get_instance(ws_dev dev);
extern void reg_uart_rx_callback(ws_dev dev,user_uart_cfg_parameter_t *p_parameter);
extern void set_dev_recv_busy(ws_dev dev,uint8_t busy);
extern void set_uart_init(ws_dev dev);
extern void UART0_IRQHandler(void);
extern void UART1_IRQHandler(void);
extern int uart_block_putchar (ws_dev dev,int ch);
extern void ui_uart_block_writebuf(ws_dev dev,uint8_t * buf,uint32_t buf_len);
extern void ui_uart_configuration(ws_dev dev,uart_config *pConfig,uint8_t on);
extern void ui_uart_init(ws_dev dev);
extern uint32_t ui_uart_readbuf(ws_dev  dev,uint8_t *buf,uint32_t len);
extern uint32_t ui_uart_recvchar(ws_dev dev, uint8_t *ch);
extern void ui_uart_sendchar(ws_dev dev, uint8_t data);
extern void ui_uart_writebuf(ws_dev dev,uint8_t * buf,uint32_t buf_len);
extern void user_uart_config(ws_dev dev,user_uart_cfg_parameter_t *p_parameter);
void set_uart_recv_busy(ws_dev dev,uint8_t busy);
void ui_dbg_uart_writebuf(ws_dev dev,uint8_t * buf,uint32_t buf_len);
uint16_t get_ui_uart_data_size(ws_dev  dev);
uint32_t get_uart_busy(ws_dev dev);
uint8_t ui_uart_flush(ws_dev  dev);
void flush_uartx_2_usr_fifo(uint8_t type);

uint8_t get_uart_recv_busy(ws_dev dev);
uint8_t ui_uart_rx_fifo_empty(ws_dev dev);
void app_uart_configuration(uint8_t on);

#endif

