Uart.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef __USART_H
  2. #define __USART_H
  3. #include "stdio.h"
  4. #include "stm32f10x.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_ME3616 = UART3_ID,
  14. UART_485 = UART3_ID,
  15. UART_MAX = 5
  16. };
  17. #define RS485_UART USART3
  18. #ifndef FALSE
  19. #define FALSE 0
  20. #endif
  21. #ifndef TRUE
  22. #define TRUE 1
  23. #endif
  24. #define UART_RCV_TEMPBUF_LEN 1024
  25. #define UART_RCV_PROCBUF_LEN 1024
  26. #define OTP_PACK_ALIGN(x) __attribute__((packed, aligned(x)))
  27. typedef int (*uart_rcv_func)(u8 uartid, void *arg, u8 *pbuf, u16 len, u8 version);
  28. /*定义串口处理的一些标志*/
  29. typedef struct _uart_buf_flag
  30. {
  31. u8 rsvd:7;
  32. u8 bufof:1;/*暂存表溢出*/
  33. } OTP_PACK_ALIGN(1)uart_buf_flag;
  34. /*暂存缓冲区*/
  35. typedef struct _uart_rcv_tempbuf
  36. {
  37. u16 pinput; /*接收缓冲输入的指针值*/
  38. u16 poutput; /*接收缓冲输出的指针值*/
  39. u8 buf[UART_RCV_TEMPBUF_LEN];
  40. }OTP_PACK_ALIGN(1)uart_rcv_tempbuf;
  41. /*接收处理缓冲区*/
  42. typedef struct _uart_rcv_procbuf
  43. {
  44. u16 outcnt; /*处理的字节数*/
  45. u16 pktlen; /*协议报文的长度*/
  46. u16 datalen; /*缓冲区的长度*/
  47. u8 pdata[UART_RCV_PROCBUF_LEN]; /*缓冲区指针*/
  48. uart_rcv_func rcv; /*接收报文处理函数*/
  49. }OTP_PACK_ALIGN(1)uart_rcv_procbuf;
  50. typedef struct _uart_rcv_info
  51. {
  52. u32 rtime; /*接收时间定义,记录上次字节接收时间*/
  53. uart_buf_flag flag;
  54. uart_rcv_tempbuf rtbuf; /*暂存缓冲区信息*/
  55. uart_rcv_procbuf rproc; /*报文处理缓冲区*/
  56. u16 errpkt; /*接收错误报文计数*/
  57. #ifdef INCLUDE_OPTTYPE
  58. uart_opttype_buf opttype;
  59. #endif
  60. }uart_rcv_info;
  61. /*发送缓冲区*/
  62. typedef struct _uart_send_info
  63. {
  64. int sending; /*表示是否开始发送缓冲区数据*/
  65. u16 pinput; /*接收缓冲输入的指针值;范围0~datalen-1*/
  66. u16 poutput; /*接收缓冲输入的指针值;范围0~datalen-1*/
  67. u16 datalen; /*缓冲区的长度*/
  68. u8 pdata[1024]; /*缓冲区指针*/
  69. uart_buf_flag flag;
  70. }uart_send_info;
  71. extern uint8_t UART3_RCV_PROT_BUF[UART_RCV_PROCBUF_LEN];
  72. //串口3接收中断处理
  73. typedef struct _uart_irq
  74. {
  75. uint16_t head_cnt;
  76. uint16_t tail_cnt;
  77. uint16_t data_len;
  78. uint16_t data_cnt;
  79. uint8_t code;
  80. uint8_t rcv_over;
  81. //
  82. uint16_t head_cnt2;
  83. uint16_t data_len2;
  84. }uart_irq;
  85. typedef struct _uart_info
  86. {
  87. uart_rcv_info rinfo;
  88. // uart_send_info sinfo;
  89. uart_irq irqinfo;
  90. u8 uartid;
  91. void *arg; /*自定义指针*/
  92. }uart_info;
  93. typedef struct _uart_fifo
  94. {
  95. uint8_t flag;
  96. uint8_t over;
  97. uint16_t len;
  98. uint8_t data[256];
  99. }uart_fifo_t;
  100. /*接口配置数据结构*/
  101. typedef struct _uart_config_struct
  102. {
  103. GPIO_TypeDef * rx_port;
  104. u16 rx_pin;
  105. GPIO_TypeDef * tx_port;
  106. u16 tx_pin;
  107. u32 remap;
  108. u8 irq_no;
  109. USART_TypeDef * uart_def;
  110. u32 uart_clk;
  111. u32 gpio_clk;
  112. } uart_config_struct;
  113. void uart_init(u8 uartid, u32 baud, u16 wordlen, u16 parity);
  114. void data_dump(const char *name, uint8_t *data, uint16_t length);
  115. int uart_msg_send(u8 uartid, const char *buf, u32 buflen);
  116. int uart_blocking_read(char *buffer, u8 uartid, u32 timeout);
  117. int uart_rs232_init(u8 uartid, uart_rcv_func rcv);
  118. int uart_rcv_process(u8 uartid);
  119. uint16_t uart_rcv_uart3(uint8_t *buffer);
  120. uint16_t uart_rcv_uart3_two(uint8_t *buffer1,uint16_t *buf_len1, uint8_t *buffer2, uint16_t *buf_len2);
  121. #endif