serial.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-05-15 lgnq first version.
  9. * 2012-05-28 bernard change interfaces
  10. * 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define
  11. * the size of ring buffer.
  12. */
  13. #ifndef __SERIAL_H__
  14. #define __SERIAL_H__
  15. #include <rtthread.h>
  16. #define BAUD_RATE_2400 2400
  17. #define BAUD_RATE_4800 4800
  18. #define BAUD_RATE_9600 9600
  19. #define BAUD_RATE_19200 19200
  20. #define BAUD_RATE_38400 38400
  21. #define BAUD_RATE_57600 57600
  22. #define BAUD_RATE_115200 115200
  23. #define BAUD_RATE_230400 230400
  24. #define BAUD_RATE_460800 460800
  25. #define BAUD_RATE_921600 921600
  26. #define BAUD_RATE_2000000 2000000
  27. #define BAUD_RATE_2500000 2500000
  28. #define BAUD_RATE_3000000 3000000
  29. #define DATA_BITS_5 5
  30. #define DATA_BITS_6 6
  31. #define DATA_BITS_7 7
  32. #define DATA_BITS_8 8
  33. #define DATA_BITS_9 9
  34. #define STOP_BITS_1 0
  35. #define STOP_BITS_2 1
  36. #define STOP_BITS_3 2
  37. #define STOP_BITS_4 3
  38. #ifdef _WIN32
  39. #include <windows.h>
  40. #else
  41. #define PARITY_NONE 0
  42. #define PARITY_ODD 1
  43. #define PARITY_EVEN 2
  44. #endif
  45. #define BIT_ORDER_LSB 0
  46. #define BIT_ORDER_MSB 1
  47. #define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
  48. #define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
  49. #ifndef RT_SERIAL_RB_BUFSZ
  50. #define RT_SERIAL_RB_BUFSZ 64
  51. #endif
  52. #define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
  53. #define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */
  54. #define RT_SERIAL_EVENT_RX_DMADONE 0x03 /* Rx DMA transfer done */
  55. #define RT_SERIAL_EVENT_TX_DMADONE 0x04 /* Tx DMA transfer done */
  56. #define RT_SERIAL_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
  57. #define RT_SERIAL_DMA_RX 0x01
  58. #define RT_SERIAL_DMA_TX 0x02
  59. #define RT_SERIAL_RX_INT 0x01
  60. #define RT_SERIAL_TX_INT 0x02
  61. #define RT_SERIAL_ERR_OVERRUN 0x01
  62. #define RT_SERIAL_ERR_FRAMING 0x02
  63. #define RT_SERIAL_ERR_PARITY 0x03
  64. #define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
  65. #define RT_SERIAL_TX_DATAQUEUE_LWM 30
  66. #define RT_SERIAL_FLOWCONTROL_CTSRTS 1
  67. #define RT_SERIAL_FLOWCONTROL_NONE 0
  68. /* Default config for serial_configure structure */
  69. #define RT_SERIAL_CONFIG_DEFAULT \
  70. { \
  71. BAUD_RATE_115200, /* 115200 bits/s */ \
  72. DATA_BITS_8, /* 8 databits */ \
  73. STOP_BITS_1, /* 1 stopbit */ \
  74. PARITY_NONE, /* No parity */ \
  75. BIT_ORDER_LSB, /* LSB first sent */ \
  76. NRZ_NORMAL, /* Normal mode */ \
  77. RT_SERIAL_RB_BUFSZ, /* Buffer size */ \
  78. RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
  79. 0 \
  80. }
  81. struct serial_configure
  82. {
  83. rt_uint32_t baud_rate;
  84. rt_uint32_t data_bits :4;
  85. rt_uint32_t stop_bits :2;
  86. rt_uint32_t parity :2;
  87. rt_uint32_t bit_order :1;
  88. rt_uint32_t invert :1;
  89. rt_uint32_t bufsz :16;
  90. rt_uint32_t flowcontrol :1;
  91. rt_uint32_t reserved :5;
  92. };
  93. /*
  94. * Serial FIFO mode
  95. */
  96. struct rt_serial_rx_fifo
  97. {
  98. /* software fifo */
  99. rt_uint8_t *buffer;
  100. rt_uint16_t put_index, get_index;
  101. rt_bool_t is_full;
  102. };
  103. struct rt_serial_tx_fifo
  104. {
  105. struct rt_completion completion;
  106. };
  107. /*
  108. * Serial DMA mode
  109. */
  110. struct rt_serial_rx_dma
  111. {
  112. rt_bool_t activated;
  113. };
  114. struct rt_serial_tx_dma
  115. {
  116. rt_bool_t activated;
  117. struct rt_data_queue data_queue;
  118. };
  119. struct rt_serial_device
  120. {
  121. struct rt_device parent;
  122. const struct rt_uart_ops *ops;
  123. struct serial_configure config;
  124. void *serial_rx;
  125. void *serial_tx;
  126. };
  127. typedef struct rt_serial_device rt_serial_t;
  128. /**
  129. * uart operators
  130. */
  131. struct rt_uart_ops
  132. {
  133. rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
  134. rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
  135. int (*putc)(struct rt_serial_device *serial, char c);
  136. int (*getc)(struct rt_serial_device *serial);
  137. rt_size_t (*dma_transmit)(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);
  138. };
  139. void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
  140. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  141. const char *name,
  142. rt_uint32_t flag,
  143. void *data);
  144. #endif