Uart.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef __USART_H
  2. #define __USART_H
  3. #include "stdio.h"
  4. #include "includes.h"
  5. /*端口号*/
  6. enum {
  7. UART1_ID = 0,
  8. UART2_ID = 1,
  9. UART3_ID = 2,
  10. UART4_ID = 3,
  11. UART5_ID = 4,
  12. UART_DEBUG = UART1_ID,
  13. UART_4G = UART3_ID,
  14. UART_MAX = 5
  15. };
  16. #define RS485_UART UART2_ID
  17. #ifndef FALSE
  18. #define FALSE 0
  19. #endif
  20. #ifndef TRUE
  21. #define TRUE 1
  22. #endif
  23. #define UART_RCV_TEMPBUF_LEN 2048
  24. #define UART_RCV_PROCBUF_LEN 2048
  25. #define USART_RECEIVE_OVERTIME 20 //字节超时时间, 单位为ms
  26. #define SYSTEMTICK_PERIOD_MS 1 //滴答时基,单位ms,缺省值10ms
  27. #define OTP_PACK_ALIGN(x) __attribute__((packed, aligned(x)))
  28. typedef int (*uart_rcv_func)(uint8_t uartid, void *arg, uint8_t *pbuf, uint16_t len, uint8_t version);
  29. #define P_BUF 1
  30. #define P_DATA 2
  31. #define P_READ 1
  32. #define P_WRITE 2
  33. #define UART_RCV_LEN_SMALL 1024
  34. #define UART_RCV_LEN_BIG 2100
  35. typedef struct _uart_rcv_buf_t
  36. {
  37. uint8_t uart1_buf[UART_RCV_LEN_SMALL];
  38. uint8_t uart1_pdata[UART_RCV_LEN_SMALL];
  39. uint8_t uart2_buf[UART_RCV_LEN_SMALL];
  40. uint8_t uart2_pdata[UART_RCV_LEN_SMALL];
  41. uint8_t uart3_buf[UART_RCV_LEN_BIG];
  42. uint8_t uart3_pdata[UART_RCV_LEN_BIG];
  43. uint8_t uart4_buf[UART_RCV_LEN_BIG];
  44. uint8_t uart4_pdata[UART_RCV_LEN_BIG];
  45. uint8_t uart5_buf[UART_RCV_LEN_SMALL];
  46. uint8_t uart5_pdata[UART_RCV_LEN_SMALL];
  47. }OTP_PACK_ALIGN(1) uart_rcv_buf_t;
  48. typedef struct _uart_buf_temp
  49. {
  50. uint8_t buff[UART_RCV_LEN_BIG];
  51. }OTP_PACK_ALIGN(1) uart_buf_temp;
  52. /*定义串口处理的一些标志*/
  53. typedef struct _uart_buf_flag
  54. {
  55. uint8_t rsvd:7;
  56. uint8_t bufof:1;/*暂存表溢出*/
  57. } OTP_PACK_ALIGN(1)uart_buf_flag;
  58. /*暂存缓冲区*/
  59. typedef struct _uart_rcv_tempbuf
  60. {
  61. uint16_t pinput; /*接收缓冲输入的指针值*/
  62. uint16_t poutput; /*接收缓冲输出的指针值*/
  63. // uint8_t buf[UART_RCV_TEMPBUF_LEN];
  64. }OTP_PACK_ALIGN(1)uart_rcv_tempbuf;
  65. /*接收处理缓冲区*/
  66. typedef struct _uart_rcv_procbuf
  67. {
  68. uint16_t outcnt; /*处理的字节数*/
  69. uint16_t pktlen; /*协议报文的长度*/
  70. uint16_t datalen; /*缓冲区的长度*/
  71. // uint8_t pdata[UART_RCV_PROCBUF_LEN]; /*缓冲区指针*/
  72. uart_rcv_func rcv; /*接收报文处理函数*/
  73. }OTP_PACK_ALIGN(1)uart_rcv_procbuf;
  74. typedef struct _uart_rcv_info
  75. {
  76. uint32_t rtime; /*接收时间定义,记录上次字节接收时间*/
  77. uart_buf_flag flag;
  78. uart_rcv_tempbuf rtbuf; /*暂存缓冲区信息*/
  79. uart_rcv_procbuf rproc; /*报文处理缓冲区*/
  80. uint16_t errpkt; /*接收错误报文计数*/
  81. #ifdef INCLUDE_OPTTYPE
  82. uart_opttype_buf opttype;
  83. #endif
  84. }uart_rcv_info;
  85. /*发送缓冲区*/
  86. typedef struct _uart_send_info
  87. {
  88. int sending; /*表示是否开始发送缓冲区数据*/
  89. uint16_t pinput; /*接收缓冲输入的指针值;范围0~datalen-1*/
  90. uint16_t poutput; /*接收缓冲输入的指针值;范围0~datalen-1*/
  91. uint16_t datalen; /*缓冲区的长度*/
  92. uint8_t pdata[2048]; /*缓冲区指针*/
  93. uart_buf_flag flag;
  94. }uart_send_info;
  95. typedef struct _uart_info
  96. {
  97. uart_rcv_info rinfo;
  98. // uart_send_info sinfo;
  99. uint8_t uartid;
  100. uint16_t USART_ReceiveTimeCounter;
  101. uint8_t Rcv_Over;
  102. void *arg; /*自定义指针*/
  103. }uart_info;
  104. extern uart_info g_uart_info[UART_MAX];
  105. /*接口配置数据结构*/
  106. typedef struct _uart_config_struct
  107. {
  108. // GPIO_TypeDef * rx_port;
  109. uint32_t rx_port;
  110. uint16_t rx_pin;
  111. // GPIO_TypeDef * tx_port;
  112. uint32_t tx_port;
  113. uint16_t tx_pin;
  114. uint32_t remap;
  115. uint8_t irq_no;
  116. // USART_TypeDef * uart_def;
  117. uint32_t uart_def;
  118. uint32_t uart_clk;
  119. uint32_t gpio_clk;
  120. uint32_t gpio_clk1;
  121. uint32_t gpio_clk2;
  122. } uart_config_struct;
  123. void uart_init(uint8_t uartid, uint32_t baud, uint32_t wordlen, uint32_t parity);
  124. void data_dump(const char *name, uint8_t *data, uint16_t length);
  125. int uart_msg_send(uint8_t uartid, const char *buf, uint32_t buflen);
  126. int uart_blocking_read(char *buffer, uint8_t uartid, uint32_t timeout);
  127. int uart_rs232_init(uint8_t uartid, uart_rcv_func rcv);
  128. int uart_rcv_process(uint8_t uartid);
  129. void USART_ReceiveOvertimeProcess(void);
  130. void uart_rcv_wait(uint8_t uartid, uint32_t timeout);
  131. uint32_t uart_blocking_read_over(char *buffer, uint8_t uartid, uint32_t timeout);
  132. #endif