serial_v2.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * 2021-06-01 KyleChan first version
  9. */
  10. #ifndef __SERIAL_V2_H__
  11. #define __SERIAL_V2_H__
  12. #include <rtthread.h>
  13. #define BAUD_RATE_2400 2400
  14. #define BAUD_RATE_4800 4800
  15. #define BAUD_RATE_9600 9600
  16. #define BAUD_RATE_19200 19200
  17. #define BAUD_RATE_38400 38400
  18. #define BAUD_RATE_57600 57600
  19. #define BAUD_RATE_115200 115200
  20. #define BAUD_RATE_230400 230400
  21. #define BAUD_RATE_460800 460800
  22. #define BAUD_RATE_921600 921600
  23. #define BAUD_RATE_2000000 2000000
  24. #define BAUD_RATE_3000000 3000000
  25. #define DATA_BITS_5 5
  26. #define DATA_BITS_6 6
  27. #define DATA_BITS_7 7
  28. #define DATA_BITS_8 8
  29. #define DATA_BITS_9 9
  30. #define STOP_BITS_1 0
  31. #define STOP_BITS_2 1
  32. #define STOP_BITS_3 2
  33. #define STOP_BITS_4 3
  34. #ifdef _WIN32
  35. #include <windows.h>
  36. #else
  37. #define PARITY_NONE 0
  38. #define PARITY_ODD 1
  39. #define PARITY_EVEN 2
  40. #endif
  41. #define BIT_ORDER_LSB 0
  42. #define BIT_ORDER_MSB 1
  43. #define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
  44. #define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
  45. #define RT_DEVICE_FLAG_RX_BLOCKING 0x1000
  46. #define RT_DEVICE_FLAG_RX_NON_BLOCKING 0x2000
  47. #define RT_DEVICE_FLAG_TX_BLOCKING 0x4000
  48. #define RT_DEVICE_FLAG_TX_NON_BLOCKING 0x8000
  49. #define RT_SERIAL_RX_BLOCKING RT_DEVICE_FLAG_RX_BLOCKING
  50. #define RT_SERIAL_RX_NON_BLOCKING RT_DEVICE_FLAG_RX_NON_BLOCKING
  51. #define RT_SERIAL_TX_BLOCKING RT_DEVICE_FLAG_TX_BLOCKING
  52. #define RT_SERIAL_TX_NON_BLOCKING RT_DEVICE_FLAG_TX_NON_BLOCKING
  53. #define RT_DEVICE_CHECK_OPTMODE 0x20
  54. #define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
  55. #define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */
  56. #define RT_SERIAL_EVENT_RX_DMADONE 0x03 /* Rx DMA transfer done */
  57. #define RT_SERIAL_EVENT_TX_DMADONE 0x04 /* Tx DMA transfer done */
  58. #define RT_SERIAL_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
  59. #define RT_SERIAL_ERR_OVERRUN 0x01
  60. #define RT_SERIAL_ERR_FRAMING 0x02
  61. #define RT_SERIAL_ERR_PARITY 0x03
  62. #define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
  63. #define RT_SERIAL_TX_DATAQUEUE_LWM 30
  64. #define RT_SERIAL_RX_MINBUFSZ 64
  65. #define RT_SERIAL_TX_MINBUFSZ 64
  66. #define RT_SERIAL_TX_BLOCKING_BUFFER 1
  67. #define RT_SERIAL_TX_BLOCKING_NO_BUFFER 0
  68. #define RT_SERIAL_FLOWCONTROL_CTSRTS 1
  69. #define RT_SERIAL_FLOWCONTROL_NONE 0
  70. /* Default config for serial_configure structure */
  71. #define RT_SERIAL_CONFIG_DEFAULT \
  72. { \
  73. BAUD_RATE_115200, /* 115200 bits/s */ \
  74. DATA_BITS_8, /* 8 databits */ \
  75. STOP_BITS_1, /* 1 stopbit */ \
  76. PARITY_NONE, /* No parity */ \
  77. BIT_ORDER_LSB, /* LSB first sent */ \
  78. NRZ_NORMAL, /* Normal mode */ \
  79. RT_SERIAL_RX_MINBUFSZ, /* rxBuf size */ \
  80. RT_SERIAL_TX_MINBUFSZ, /* txBuf size */ \
  81. RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
  82. 0 \
  83. }
  84. struct serial_configure
  85. {
  86. rt_uint32_t baud_rate;
  87. rt_uint32_t data_bits :4;
  88. rt_uint32_t stop_bits :2;
  89. rt_uint32_t parity :2;
  90. rt_uint32_t bit_order :1;
  91. rt_uint32_t invert :1;
  92. rt_uint32_t rx_bufsz :16;
  93. rt_uint32_t tx_bufsz :16;
  94. rt_uint32_t flowcontrol :1;
  95. rt_uint32_t reserved :5;
  96. };
  97. /*
  98. * Serial Receive FIFO mode
  99. */
  100. struct rt_serial_rx_fifo
  101. {
  102. struct rt_ringbuffer rb;
  103. struct rt_completion rx_cpt;
  104. rt_uint16_t rx_cpt_index;
  105. /* software fifo */
  106. rt_uint8_t buffer[];
  107. };
  108. /*
  109. * Serial Transmit FIFO mode
  110. */
  111. struct rt_serial_tx_fifo
  112. {
  113. struct rt_ringbuffer rb;
  114. rt_size_t put_size;
  115. rt_bool_t activated;
  116. struct rt_completion tx_cpt;
  117. /* software fifo */
  118. rt_uint8_t buffer[];
  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. };
  128. /**
  129. * uart operators
  130. */
  131. struct rt_uart_ops
  132. {
  133. rt_err_t (*configure)(struct rt_serial_device *serial,
  134. struct serial_configure *cfg);
  135. rt_err_t (*control)(struct rt_serial_device *serial,
  136. int cmd,
  137. void *arg);
  138. int (*putc)(struct rt_serial_device *serial, char c);
  139. int (*getc)(struct rt_serial_device *serial);
  140. rt_size_t (*transmit)(struct rt_serial_device *serial,
  141. rt_uint8_t *buf,
  142. rt_size_t size,
  143. rt_uint32_t tx_flag);
  144. };
  145. void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
  146. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  147. const char *name,
  148. rt_uint32_t flag,
  149. void *data);
  150. #endif