serial.h 5.8 KB

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