#ifndef _UART_H_
#define _UART_H_

#include "apt32f102.h"
#include "apt32f102_uart.h"

#define Recv_2400_TimeOut     	3       //ms
#define Recv_9600_TimeOut     	3       //ms
#define Recv_115200_TimeOut   	3       //ms

#define USART_BUFFER_NUM        3
#define USART_BUFFER_SIZE       70
#define USART_SEND_SIZE			128

#define UART_SEND_BUFFER_NUM    10
#define UART_SEND_BUFFER_SIZE   20

#define UART485_TX_PIN
#define UART485_RX_PIN			15		//PA0.15
#define UART485_DR_PIN			7		//PA0.7

#define READ_RX_LEVEL_STATE		GPIO_Read_Status(GPIOA0,UART485_RX_PIN)		//485总线RX引脚

#define WRITE_HIGH_DR			GPIO_Write_High(GPIOA0,UART485_DR_PIN)		//485 DR
#define WRITE_LOW_DR			GPIO_Write_Low(GPIOA0,UART485_DR_PIN)		//485 DR
#define REVERISE_DR				GPIO_Reverse(GPIOA0,UART485_DR_PIN)			//485 DR

#define LED_TX_PIN				0//PA0.0
#define LED_RX_PIN				1//PA0.1
#define LED_STATUS_PIN			4//PA0.4

#define TX_LED_ON				GPIO_Write_Low(GPIOA0,LED_TX_PIN)
#define TX_LED_OFF				GPIO_Write_High(GPIOA0,LED_TX_PIN)

#define RX_LED_ON				GPIO_Write_Low(GPIOA0,LED_RX_PIN)
#define RX_LED_OFF				GPIO_Write_High(GPIOA0,LED_RX_PIN)

#define STATUS_LED_ON			GPIO_Write_Low(GPIOA0,LED_STATUS_PIN)
#define STATUS_LED_OFF			GPIO_Write_High(GPIOA0,LED_STATUS_PIN)
#define REVERISE_STATUS			GPIO_Reverse(GPIOA0,LED_STATUS_PIN)	


#define UART_BUSBUSY			0x01	//总线繁忙
#define UART_BUSIDLE			0x00	//总线空闲

#define BUSSend_WaitTime1		300		//数据有效期：短按数据有效期
#define BUSSend_WaitTime2		3000	//数据有效期：
#define BUSSend_WaitTime3		60000	//数据有效期：

#define BUSSendCnt1				1				//点按数据上报次数
#define BUSSendCnt2				1//强电4，弱电1	//长按按下数据上报次数
#define BUSSendCnt3				1//强电8，弱电1	//长按松开数据上报次数
#define BUSSend_Tick			300		//数据发送间隔


/*调试信息相关定义*/
#ifndef DBG_LOG_EN
#define DBG_LOG_EN           		0      //DEBUG LOG 输出总开关
#endif

/*调试信息初始状态*/
#define DBG_OPT_Debug_STATUS        0       //临时调试信息打印开关
#define DBG_OPT_DEVICE_STATUS       0       //设备驱动层打印调试信息打印开关
#define DBG_OPT_SYS_STATUS          1      //系统调试信息打印开关

/*调试信息输出控制位*/
#define DBG_BIT_Debug_STATUS        2
#define DBG_BIT_DEVICE_STATUS       1
#define DBG_BIT_SYS_STATUS          0

#if DBG_LOG_EN
#define DBG_SendByte(data)			//UARTTxByte(UART2,data)
//#define DBG_Printf(data,len)		UARTTransmit(UART2,data,len)

#define DBG_Printf(data,len)		MCU485_SendData(data,len)
#else 
#define DBG_SendByte(data)			
#define DBG_Printf					//MCU485_SendData(data,len)
#endif	

typedef U8_T (*Uart_prt)(U8_T *,U16_T);

typedef enum
{
    UART_0,
    UART_1,
    UART_2,
    UART_3,
    UART_MAX,
}UART_IDX;

typedef enum
{
    BUSSEND_SUCC = 0x00,	//等待发送机会
    BUSSEND_WAIT,			//发送成功
    DATA_END,				//数据有效期结束
    RETRY_END,				//重发结束
    LEN_ERR,				//长度错误
}BUSSEND_REV;

typedef struct{

    U8_T     RecvBuffer[USART_BUFFER_SIZE];
	U8_T	 DealBuffer[USART_BUFFER_SIZE];	//数据处理缓冲
	
    U8_T     Receiving;
	U16_T	 DealLen;
	
    U16_T    RecvLen;
	
    U32_T    RecvTimeout;
    U32_T    RecvIdleTiming;

    Uart_prt    processing_cf;      //处理函数指针
}UART_t;


typedef struct{			//总线繁忙判断
	U8_T	SendBuffer[USART_SEND_SIZE];		//发送缓冲

	U8_T    BusState_Flag;		//总线繁忙标记位，  0x01:总线繁忙，0x00:总线空闲
	U8_T	HighBit_Flag;		//串口RX高电平标记位,默认是高电平0x01.
	U8_T	BUSBUSY_LOCK;		//锁定总线繁忙状态	
	
	U8_T	SendState;			//当前发送状态	
    U8_T	ResendCnt;			//当前发送次数
	U8_T	ASend_Flag;			//主动上报发送标记	
	U8_T	TotalCnt;			//发送总次数
	U8_T	SetBaudFlag;		//设置波特率	
	U8_T	Jump_Flag;			//跳转标志位
	
    U16_T	SendLen;			//发送缓冲区数据长度
	
	U32_T   Bus_DelayTime;		//总线繁忙转换到空闲状态的随机延时时间	
	U32_T   DataWait_Time;   	//上报数据间隔
	U32_T	DataValid_Time;		//上报数据有效期
	
	
	U32_T	BusState_Tick;		//总线繁忙状态判断时间戳
	U32_T	ASend_Tick;			//主动上报发送间隔判断时间戳
	U32_T	BusbusyTimeout;		//上报数据有效期判断时间戳
	
}MULIT_t;


extern  MULIT_t m_send;
extern UART_t g_uart;
extern U32_T Dbg_Switch;


extern volatile int RS485_Comm_Flag,RS485_Comm_Start,RS485_Comm_End,RS485_Comming;

void UARTx_Init(UART_IDX uart_id, Uart_prt prt_cf);
void UART0_RecvINT_Processing(char data);
void UART0_TASK(void);
void UART1_RecvINT_Processing(char data);
void UART1_TASK(void);
void UART2_RecvINT_Processing(char data);
void UART2_TASK(void);
void MCU485_SendData(U8_T *buff,U16_T len);

void Dbg_Print(int DbgOptBit, const char *cmd, ...);
void Dbg_Println(int DbgOptBit, const char *cmd, ...);
void Dbg_Print_Buff(int DbgOptBit, const char *cmd, U8_T *buff,U16_T len);

void BusIdle_Task(void);
void BusBusy_Task(void);
void BUS485Send_Task(void);
U8_T MultSend_Task(U8_T *buff,U16_T len,U8_T DatSd);
U8_T BUS485_Send(U8_T *buff,U16_T len);
void Set_GroupSend(U8_T *data,U16_T sled,U8_T SCnt,U32_T indate,U32_T tim_val);
void Clear_SendFlag(void);
void BUS485_Jump_Boot(U8_T jump);

#endif
