serial.h 5.1 KB

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