#ifndef __USART_H #define __USART_H #include "stdio.h" #include "includes.h" /*端口号*/ enum { UART1_ID = 0, UART2_ID = 1, UART3_ID = 2, UART4_ID = 3, UART5_ID = 4, UART_DEBUG = UART1_ID, UART_4G = UART3_ID, UART_MAX = 5 }; #define RS485_UART UART2_ID #ifndef FALSE #define FALSE 0 #endif #ifndef TRUE #define TRUE 1 #endif #define UART_RCV_TEMPBUF_LEN 2048 #define UART_RCV_PROCBUF_LEN 2048 #define USART_RECEIVE_OVERTIME 20 //字节超时时间, 单位为ms #define SYSTEMTICK_PERIOD_MS 1 //滴答时基,单位ms,缺省值10ms #define OTP_PACK_ALIGN(x) __attribute__((packed, aligned(x))) typedef int (*uart_rcv_func)(uint8_t uartid, void *arg, uint8_t *pbuf, uint16_t len, uint8_t version); #define P_BUF 1 #define P_DATA 2 #define P_READ 1 #define P_WRITE 2 #define UART_RCV_LEN_SMALL 1024 #define UART_RCV_LEN_BIG 2100 typedef struct _uart_rcv_buf_t { uint8_t uart1_buf[UART_RCV_LEN_SMALL]; uint8_t uart1_pdata[UART_RCV_LEN_SMALL]; uint8_t uart2_buf[UART_RCV_LEN_SMALL]; uint8_t uart2_pdata[UART_RCV_LEN_SMALL]; uint8_t uart3_buf[UART_RCV_LEN_BIG]; uint8_t uart3_pdata[UART_RCV_LEN_BIG]; uint8_t uart4_buf[UART_RCV_LEN_BIG]; uint8_t uart4_pdata[UART_RCV_LEN_BIG]; uint8_t uart5_buf[UART_RCV_LEN_SMALL]; uint8_t uart5_pdata[UART_RCV_LEN_SMALL]; }OTP_PACK_ALIGN(1) uart_rcv_buf_t; typedef struct _uart_buf_temp { uint8_t buff[UART_RCV_LEN_BIG]; }OTP_PACK_ALIGN(1) uart_buf_temp; /*定义串口处理的一些标志*/ typedef struct _uart_buf_flag { uint8_t rsvd:7; uint8_t bufof:1;/*暂存表溢出*/ } OTP_PACK_ALIGN(1)uart_buf_flag; /*暂存缓冲区*/ typedef struct _uart_rcv_tempbuf { uint16_t pinput; /*接收缓冲输入的指针值*/ uint16_t poutput; /*接收缓冲输出的指针值*/ // uint8_t buf[UART_RCV_TEMPBUF_LEN]; }OTP_PACK_ALIGN(1)uart_rcv_tempbuf; /*接收处理缓冲区*/ typedef struct _uart_rcv_procbuf { uint16_t outcnt; /*处理的字节数*/ uint16_t pktlen; /*协议报文的长度*/ uint16_t datalen; /*缓冲区的长度*/ // uint8_t pdata[UART_RCV_PROCBUF_LEN]; /*缓冲区指针*/ uart_rcv_func rcv; /*接收报文处理函数*/ }OTP_PACK_ALIGN(1)uart_rcv_procbuf; typedef struct _uart_rcv_info { uint32_t rtime; /*接收时间定义,记录上次字节接收时间*/ uart_buf_flag flag; uart_rcv_tempbuf rtbuf; /*暂存缓冲区信息*/ uart_rcv_procbuf rproc; /*报文处理缓冲区*/ uint16_t errpkt; /*接收错误报文计数*/ #ifdef INCLUDE_OPTTYPE uart_opttype_buf opttype; #endif }uart_rcv_info; /*发送缓冲区*/ typedef struct _uart_send_info { int sending; /*表示是否开始发送缓冲区数据*/ uint16_t pinput; /*接收缓冲输入的指针值;范围0~datalen-1*/ uint16_t poutput; /*接收缓冲输入的指针值;范围0~datalen-1*/ uint16_t datalen; /*缓冲区的长度*/ uint8_t pdata[2048]; /*缓冲区指针*/ uart_buf_flag flag; }uart_send_info; typedef struct _uart_info { uart_rcv_info rinfo; // uart_send_info sinfo; uint8_t uartid; uint16_t USART_ReceiveTimeCounter; uint8_t Rcv_Over; void *arg; /*自定义指针*/ }uart_info; extern uart_info g_uart_info[UART_MAX]; /*接口配置数据结构*/ typedef struct _uart_config_struct { // GPIO_TypeDef * rx_port; uint32_t rx_port; uint16_t rx_pin; // GPIO_TypeDef * tx_port; uint32_t tx_port; uint16_t tx_pin; uint32_t remap; uint8_t irq_no; // USART_TypeDef * uart_def; uint32_t uart_def; uint32_t uart_clk; uint32_t gpio_clk; uint32_t gpio_clk1; uint32_t gpio_clk2; } uart_config_struct; void uart_init(uint8_t uartid, uint32_t baud, uint32_t wordlen, uint32_t parity); void data_dump(const char *name, uint8_t *data, uint16_t length); int uart_msg_send(uint8_t uartid, const char *buf, uint32_t buflen); int uart_blocking_read(char *buffer, uint8_t uartid, uint32_t timeout); int uart_rs232_init(uint8_t uartid, uart_rcv_func rcv); int uart_rcv_process(uint8_t uartid); void USART_ReceiveOvertimeProcess(void); void uart_rcv_wait(uint8_t uartid, uint32_t timeout); uint32_t uart_blocking_read_over(char *buffer, uint8_t uartid, uint32_t timeout); #endif