#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			100
#define USART_SEND_SIZE				100
#define READ_RXLEVEL_STATE			GPIO_Read_Status(GPIOB0,5)		//RCU通讯485总线RX引脚

#define UART_SEND_BUFFER_NUM        10
#define UART_SEND_BUFFER_SIZE       20

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

#define BUSSend_ValidTime1			60000	//数据有效期
#define BUSSend_ValidTime2			1000	//数据有效期
#define BUSSend_ValidTime3			300		//数据有效期

#define BUSSend_Cnt					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)			//UARTTxByte(UART1,data)
#define DBG_Printf	
#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     Receiving;

    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[3];
extern U32_T Dbg_Switch;

extern U8_T	DealBuffer[USART_BUFFER_SIZE];	//数据处理缓冲
extern U16_T DealLen;

extern volatile int RS485_Comm_Flag,RS485_Comm_Start,RS485_Comm_End,RS485_Comming;
extern volatile int RS485_Comm_Flag1,RS485_Comm_Start1,RS485_Comm_End1,RS485_Comming1;

void UARTx_Init(UART_IDX uart_id, Uart_prt prt_cf);
void UART1_RecvINT_Processing(char data);
void UART1_TASK(void);
void UART2_TASK(void);
void UART0_RecvINT_Processing(char data);
void UART1_RecvINT_Processing(char data);
void UART2_RecvINT_Processing(char data);
void MCU485_SendData(U8_T *buff,U16_T len);
void Controller485_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);
U8_T Get_BUS485_Send_State(void);
void BUS485_Jump_Boot(U8_T jump);

#endif

