#ifndef USART_H
#define USART_H

#include "gd32f10x.h"

typedef struct usart_data_buf_s
{
	uint8_t data[256];
	uint16_t data_cnt;
} usart_data_buf_t;

#define DE485_PIN                        GPIO_PIN_8
#define DE485_GPIO_PORT                  GPIOA
#define DE485_GPIO_CLK                   RCU_GPIOB

#define EC800M_PER_PIN                   GPIO_PIN_1
#define EC800M_PER_GPIO_PORT             GPIOC
#define EC800M_PER_GPIO_CLK              RCU_GPIOC

#define EC800M_RST_PIN                   GPIO_PIN_8
#define EC800M_RST_GPIO_PORT             GPIOB
#define EC800M_RST_GPIO_CLK              RCU_GPIOB

#define COMn                             3U

#define COM_EC800                        USART0 //USART1 EC800
#define COM_EC800_CLK                    RCU_USART0
#define COM_EC800_TX_PIN                 GPIO_PIN_9
#define COM_EC800_RX_PIN                 GPIO_PIN_10
#define COM_EC800_GPIO_PORT              GPIOA
#define COM_EC800_GPIO_CLK               RCU_GPIOA
#define COM_EC800_IT_HANDLER             USART0_IRQn
#define COM_EC800_BAUDRATE							 115200

#define COM_485	                       USART1 //UART2 J2 RS485
#define COM_485_CLK                    RCU_USART1
#define COM_485_TX_PIN                 GPIO_PIN_2
#define COM_485_RX_PIN                 GPIO_PIN_3
#define COM_485_GPIO_PORT              GPIOA
#define COM_485_GPIO_CLK               RCU_GPIOA
#define COM_485_IT_HANDLER             USART1_IRQn
#define COM_485_BAUDRATE				 				2400

#define COM_232                        USART2 //UART3 J3
#define COM_232_CLK                    RCU_USART2
#define COM_232_TX_PIN                 GPIO_PIN_8
#define COM_232_RX_PIN                 GPIO_PIN_9
#define COM_232_GPIO_PORT              GPIOD
#define COM_232_GPIO_CLK               RCU_GPIOD
#define COM_232_IT_HANDLER             USART2_IRQn
#define COM_232_BAUDRATE							 115200

//extern usart_data_buf_t usart0_rx_buf;
//extern usart_data_buf_t usart0_tx_buf;

//extern usart_data_buf_t usart1_rx_buf;
//extern usart_data_buf_t usart1_tx_buf;

/* configure COM port */
void gd_com_init(uint32_t com);

void gd_EC800M_pin_init(void);

void gd_pull_EC800M_pwr_up(void);

void gd_pull_EC800M_pwr_down(void);

void gd_pull_EC800M_rst_up(void);

void gd_pull_EC800M_rst_down(void);

void dma_config(void);
void Clear_DMA_Buffer(void);
#define UART0_RX_LEN		1024 * 4	
extern uint8_t UART0_RX_BUF[UART0_RX_LEN];
extern uint8_t UART0_RX_STAT ;					
extern uint32_t UART0_RX_NUM ;	
extern uint8_t UART0_RX_MQTT_SUB_STAT;

	
void gd_com_sendByte( uint8_t com, uint8_t ch);
void gd_com_send( uint32_t com, uint8_t *Buf,uint16_t size);
void nvic_config(void);
void dma_config_change(char *dmaBuffer,uint32_t bufferSize);
#endif   
