serialX.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * 2022-04-10 THEWON serialX first version
  9. * 2022-06-08 THEWON add No TX Empty interrupt support
  10. * 2023-02-15 THEWON add init ops
  11. */
  12. #ifndef __SERIALX_H__
  13. #define __SERIALX_H__
  14. #include <rtthread.h>
  15. #include <rtdevice.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_3000000 3000000
  28. #define DATA_BITS_5 5
  29. #define DATA_BITS_6 6
  30. #define DATA_BITS_7 7
  31. #define DATA_BITS_8 8
  32. #define DATA_BITS_9 9
  33. #define STOP_BITS_1 0
  34. #define STOP_BITS_2 1
  35. #define STOP_BITS_3 2
  36. #define STOP_BITS_4 3
  37. #ifdef _WIN32
  38. #include <windows.h>
  39. #else
  40. #define PARITY_NONE 0
  41. #define PARITY_ODD 1
  42. #define PARITY_EVEN 2
  43. #endif
  44. #define BIT_ORDER_LSB 0
  45. #define BIT_ORDER_MSB 1
  46. #define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
  47. #define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
  48. #ifndef RT_SERIAL_FIFO_BUFSZ
  49. #define RT_SERIAL_FIFO_BUFSZ 512
  50. #endif
  51. #ifndef RT_SERIAL_DMA_BUFSZ
  52. #define RT_SERIAL_DMA_BUFSZ 32
  53. #endif
  54. #if RT_SERIAL_DMA_BUFSZ < 32
  55. #define RT_SERIAL_DMA_BUFSZ 32
  56. #endif
  57. #if RT_SERIAL_FIFO_BUFSZ < (RT_SERIAL_DMA_BUFSZ*2)
  58. #undef RT_SERIAL_FIFO_BUFSZ
  59. #define RT_SERIAL_FIFO_BUFSZ RT_SERIAL_DMA_BUFSZ*2
  60. #endif
  61. #define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
  62. #define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */
  63. #define RT_SERIAL_EVENT_RX_DMADONE 0x03 /* Rx DMA transfer done */
  64. #define RT_SERIAL_EVENT_TX_DMADONE 0x04 /* Tx DMA transfer done */
  65. #define RT_SERIAL_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
  66. #define RT_SERIAL_DMA_RX 0x01
  67. #define RT_SERIAL_DMA_TX 0x02
  68. #define RT_SERIAL_RX_INT 0x01
  69. #define RT_SERIAL_TX_INT 0x02
  70. #define RT_SERIAL_ERR_OVERRUN 0x01
  71. #define RT_SERIAL_ERR_FRAMING 0x02
  72. #define RT_SERIAL_ERR_PARITY 0x03
  73. #define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
  74. #define RT_SERIAL_TX_DATAQUEUE_LWM 30
  75. #define RT_SERIAL_FLOWCONTROL_CTSRTS 1
  76. #define RT_SERIAL_FLOWCONTROL_NONE 0
  77. /* Default config for serial_configure structure */
  78. #define RT_SERIAL_CONFIG_DEFAULT \
  79. { \
  80. BAUD_RATE_115200, /* 115200 bits/s */ \
  81. DATA_BITS_8, /* 8 databits */ \
  82. STOP_BITS_1, /* 1 stopbit */ \
  83. PARITY_NONE, /* No parity */ \
  84. BIT_ORDER_LSB, /* LSB first sent */ \
  85. NRZ_NORMAL, /* Normal mode */ \
  86. RT_SERIAL_FIFO_BUFSZ, /* Buffer size */ \
  87. RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
  88. 0 \
  89. }
  90. #define RT_SERIAL_EVENT_TXDONE (1 << 0)
  91. #define RT_SERIAL_EVENT_RXDONE (1 << 1)
  92. //#define RT_SERIAL_USE_EVENT // Just for test, maybe using event in the future
  93. struct rt_serial_device;
  94. typedef int (*cb_serial_tx)(struct rt_serial_device *serial, const rt_uint8_t *data, int length);
  95. typedef int (*cb_serial_rx)(struct rt_serial_device *serial, rt_uint8_t *data, int length);
  96. struct serial_configure
  97. {
  98. rt_uint32_t baud_rate;
  99. rt_uint32_t data_bits :4;
  100. rt_uint32_t stop_bits :2;
  101. rt_uint32_t parity :2;
  102. rt_uint32_t bit_order :1;
  103. rt_uint32_t invert :1;
  104. rt_uint32_t bufsz :16; // unused, move it to struct rt_serial_device
  105. rt_uint32_t flowcontrol :1;
  106. rt_uint32_t reserved :5;
  107. };
  108. /*
  109. * Serial FIFO mode
  110. */
  111. struct rt_serial_fifo
  112. {
  113. rt_uint32_t buf_sz;
  114. /* software fifo */
  115. rt_uint8_t *buffer;
  116. rt_uint16_t put_index, get_index;
  117. rt_bool_t is_full;
  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. rt_uint32_t bufsz;
  125. rt_tick_t timeout_tick; /**< timeout tick */
  126. void *serial_rx;
  127. void *serial_tx;
  128. #ifdef RT_SERIAL_USING_DMA
  129. rt_size_t dma_idx_rx;
  130. rt_uint8_t serial_dma_rx[RT_SERIAL_DMA_BUFSZ];
  131. rt_uint8_t serial_dma_tx[RT_SERIAL_DMA_BUFSZ];
  132. #endif
  133. cb_serial_rx _cb_rx;
  134. cb_serial_tx _cb_tx;
  135. #ifndef RT_SERIAL_USE_EVENT
  136. struct rt_completion completion_rx;
  137. struct rt_completion completion_tx;
  138. #else
  139. rt_event_t rx_done;
  140. rt_event_t tx_done;
  141. #endif
  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 (*init)(struct rt_serial_device *serial);
  150. rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
  151. rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
  152. int (*putc)(struct rt_serial_device *serial, char c);
  153. int (*getc)(struct rt_serial_device *serial);
  154. int (*flush)(struct rt_serial_device *serial);
  155. #if defined (RT_SERIAL_NO_TXEIT)
  156. rt_bool_t (*is_int_txing)(struct rt_serial_device *serial);
  157. void (*start_tx)(struct rt_serial_device *serial, rt_uint8_t ch);
  158. #else
  159. void (*start_tx)(struct rt_serial_device *serial);
  160. #endif
  161. void (*stop_tx)(struct rt_serial_device *serial);
  162. #ifdef RT_SERIAL_USING_DMA
  163. rt_bool_t (*is_dma_txing)(struct rt_serial_device *serial);
  164. void (*start_dma_tx)(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size);
  165. void (*stop_dma_tx)(struct rt_serial_device *serial);
  166. #endif
  167. void (*enable_interrupt)(struct rt_serial_device *serial);
  168. void (*disable_interrupt)(struct rt_serial_device *serial);
  169. };
  170. void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
  171. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  172. const char *name,
  173. rt_uint32_t flag,
  174. void *data);
  175. #endif