Uart.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_ME3616 = UART3_ID,
  14. UART_MAX = 5
  15. };
  16. #define RS485_UART USART2
  17. #ifndef FALSE
  18. #define FALSE 0
  19. #endif
  20. #ifndef TRUE
  21. #define TRUE 1
  22. #endif
  23. #define UART_RCV_TEMPBUF_LEN 1024
  24. #define UART_RCV_PROCBUF_LEN 1024
  25. #define OTP_PACK_ALIGN(x) __attribute__((packed, aligned(x)))
  26. typedef int (*uart_rcv_func)(u8 uartid, void *arg, u8 *pbuf, u16 len, u8 version);
  27. /*定义串口处理的一些标志*/
  28. typedef struct _uart_buf_flag
  29. {
  30. u8 rsvd:7;
  31. u8 bufof:1;/*暂存表溢出*/
  32. } OTP_PACK_ALIGN(1)uart_buf_flag;
  33. /*暂存缓冲区*/
  34. typedef struct _uart_rcv_tempbuf
  35. {
  36. u16 pinput; /*接收缓冲输入的指针值*/
  37. u16 poutput; /*接收缓冲输出的指针值*/
  38. u8 buf[UART_RCV_TEMPBUF_LEN];
  39. }OTP_PACK_ALIGN(1)uart_rcv_tempbuf;
  40. /*接收处理缓冲区*/
  41. typedef struct _uart_rcv_procbuf
  42. {
  43. u16 outcnt; /*处理的字节数*/
  44. u16 pktlen; /*协议报文的长度*/
  45. u16 datalen; /*缓冲区的长度*/
  46. u8 pdata[UART_RCV_PROCBUF_LEN]; /*缓冲区指针*/
  47. uart_rcv_func rcv; /*接收报文处理函数*/
  48. }OTP_PACK_ALIGN(1)uart_rcv_procbuf;
  49. typedef struct _uart_rcv_info
  50. {
  51. u32 rtime; /*接收时间定义,记录上次字节接收时间*/
  52. uart_buf_flag flag;
  53. uart_rcv_tempbuf rtbuf; /*暂存缓冲区信息*/
  54. uart_rcv_procbuf rproc; /*报文处理缓冲区*/
  55. u16 errpkt; /*接收错误报文计数*/
  56. #ifdef INCLUDE_OPTTYPE
  57. uart_opttype_buf opttype;
  58. #endif
  59. }uart_rcv_info;
  60. /*发送缓冲区*/
  61. typedef struct _uart_send_info
  62. {
  63. int sending; /*表示是否开始发送缓冲区数据*/
  64. u16 pinput; /*接收缓冲输入的指针值;范围0~datalen-1*/
  65. u16 poutput; /*接收缓冲输入的指针值;范围0~datalen-1*/
  66. u16 datalen; /*缓冲区的长度*/
  67. u8 pdata[1024]; /*缓冲区指针*/
  68. uart_buf_flag flag;
  69. }uart_send_info;
  70. typedef struct _uart_info
  71. {
  72. uart_rcv_info rinfo;
  73. uart_send_info sinfo;
  74. u8 uartid;
  75. void *arg; /*自定义指针*/
  76. }uart_info;
  77. /*接口配置数据结构*/
  78. typedef struct _uart_config_struct
  79. {
  80. GPIO_TypeDef * rx_port;
  81. u16 rx_pin;
  82. GPIO_TypeDef * tx_port;
  83. u16 tx_pin;
  84. u32 remap;
  85. u8 irq_no;
  86. USART_TypeDef * uart_def;
  87. u32 uart_clk;
  88. u32 gpio_clk;
  89. } uart_config_struct;
  90. void uart_init(u8 uartid, u32 baud, u16 wordlen, u16 parity);
  91. int uart_msg_send(u8 uartid, const char *buf, u32 buflen);
  92. int uart_blocking_read(char *buffer, u8 uartid, u32 timeout);
  93. int uart_rs232_init(u8 uartid, uart_rcv_func rcv);
  94. int uart_rcv_process(u8 uartid);
  95. extern int uart_blocking_read_len(char *buffer, u8 uartid);
  96. #endif