#ifndef _USART_H_
#define _USART_H_

#include "stdint.h"

#if (USE_CORE_TYPE == 1)    //使用C1F核心板

	/*485控制引脚*/
	#define MCU485_PIN_1		GPIO_Pin_10		//PA10
	#define MCU485_PIN_2		GPIO_Pin_11		//PA11
	#define MCU485_PIN_3		GPIO_Pin_11		//PB11

	#define MCU485_EN1_H	GPIOA_SetBits(MCU485_PIN_1)
	#define MCU485_EN1_L	GPIOA_ResetBits(MCU485_PIN_1)
	#define MCU485_EN2_H	GPIOA_SetBits(MCU485_PIN_2)
	#define MCU485_EN2_L	GPIOA_ResetBits(MCU485_PIN_2)
	#define MCU485_EN3_H	GPIOB_SetBits(MCU485_PIN_3)
	#define MCU485_EN3_L	GPIOB_ResetBits(MCU485_PIN_3)
	
#elif (USE_CORE_TYPE == 2)   //使用C1核心板

	/*485控制引脚*/
	#define MCU485_PIN_1		GPIO_Pin_21		//PB21
	#define MCU485_PIN_2		GPIO_Pin_1		//PA1
	#define MCU485_PIN_3		GPIO_Pin_0		//PA0

	#define MCU485_EN1_H	GPIOB_SetBits(MCU485_PIN_1)
	#define MCU485_EN1_L	GPIOB_ResetBits(MCU485_PIN_1)
	#define MCU485_EN2_H	GPIOA_SetBits(MCU485_PIN_2)
	#define MCU485_EN2_L	GPIOA_ResetBits(MCU485_PIN_2)
	#define MCU485_EN3_H	GPIOA_SetBits(MCU485_PIN_3)
	#define MCU485_EN3_L	GPIOA_ResetBits(MCU485_PIN_3)
	
#endif  //USE_CORE_TYPE == CORE_TYPE_C1F


#define USART_RecvBuff_Size	1024	//串口接收数组定义大小
#define Recv_115200_TimeOut	3		//ms
#define UART_BUFFER_SIZE 		0x000400

/********************************************************/
//SRAM Uart缓冲区相关定义
#define SRAM_Uart_Buffer_Size		UART_BUFFER_SIZE		//串口缓存一包数据大小

#define SRAM_UART0_RecvBuffer_Start_Addr		0x010000		
#define SRAM_UART0_RecvBuffer_End_Addr			0x010FFF
#define SRAM_UART0_SendBuffer_Start_Addr		0x011000
#define SRAM_UART0_SendBuffer_End_Addr			0x011FFF

#define SRAM_UART1_RecvBuffer_Start_Addr		0x012000		
#define SRAM_UART1_RecvBuffer_End_Addr			0x012FFF
#define SRAM_UART1_SendBuffer_Start_Addr		0x013000
#define SRAM_UART1_SendBuffer_End_Addr			0x013FFF

#define SRAM_UART2_RecvBuffer_Start_Addr		0x014000		
#define SRAM_UART2_RecvBuffer_End_Addr			0x014FFF
#define SRAM_UART2_SendBuffer_Start_Addr		0x015000
#define SRAM_UART2_SendBuffer_End_Addr			0x015FFF

#define SRAM_UART3_RecvBuffer_Start_Addr		0x016000		
#define SRAM_UART3_RecvBuffer_End_Addr			0x016FFF
#define SRAM_UART3_SendBuffer_Start_Addr		0x017000
#define SRAM_UART3_SendBuffer_End_Addr			0x017FFF



/********************************************************/

typedef enum
{
	USART_0,
	USART_1,
	USART_2,
	USART_3,
	USART_NUM,
}USART_IDX;

#pragma pack(1)

typedef struct
{
	//uint8_t sn;
	uint8_t sendFlag;		//发送标志位
	uint8_t sendStatus;		//发送状态
	uint8_t recvFlag; 
	uint8_t recvBuffer[UART_BUFFER_SIZE];

	uint16_t recvLen;
	uint32_t recvTimeout;
	uint32_t recvIdleTiming;
	
	uint32_t TX_Buffer_WriteAddr;
	uint32_t TX_Buffer_ReadAddr;
	uint32_t RX_Buffer_WriteAddr;
	uint32_t RX_Buffer_ReadAddr;
}UART_t;

#pragma pack()

void USART_INIT(uint8_t usart,uint32_t baudrate);
void Set_Uart_recvTimeout(UART_t *set_uart,uint32_t baud);
uint8_t Uart0_GetStart(void);
uint8_t Uart1_GetStart(void);
uint8_t Uart2_GetStart(void);
uint8_t Uart3_GetStart(void);
void UART0_ChangeBaud(uint32_t baudrate);
void UART1_ChangeBaud(uint32_t baudrate);
void UART2_ChangeBaud(uint32_t baudrate);
void UART3_ChangeBaud(uint32_t baudrate);
void MCU485_SendString_0(uint8_t *buf, uint16_t l);
void MCU485_SendString_1(uint8_t *buf, uint16_t l);
void MCU485_SendString_2(uint8_t *buf, uint16_t l);
void MCU485_SendString_3(uint8_t *buf, uint16_t l);
void UART0_RECEIVE(void);
void UART1_RECEIVE(void);
void UART2_RECEIVE(void);
void UART3_RECEIVE(void);
void Uart2_RECEIVE_Process(void);

uint8_t Uart_Search_Cmd(uint8_t *buff,uint8_t len);
uint8_t Uart_Jump_Cmd(uint8_t *buff,uint8_t len);
uint8_t Uart_Search_Cmd_from_SRAM(uint32_t addr, uint8_t len);
uint8_t Uart_Jump_Cmd_from_SRAM(uint32_t addr, uint8_t len);
void Uart0_Flush(uint16_t over_time);
void Uart1_Flush(uint16_t over_time);
void Uart2_Flush(uint16_t over_time);
void Uart3_Flush(uint16_t over_time);

void Uart_SendString(uint8_t uart_id,uint8_t* buff,uint16_t len);
void MCU485_SendString(uint8_t uart_id,uint8_t* buff,uint16_t len);
void MCU485_SendSRAMData(uint8_t uart_id,uint32_t data_addr,uint16_t len);
void Write_Uart_SendBuff(uint8_t uart_id,uint8_t uart_outime,uint8_t* buff,uint16_t len);


extern UART_t g_Uart[4];


#endif
