serial_v2.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. /* 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_RX_MINBUFSZ, /* rxBuf size */ \
  78. RT_SERIAL_TX_MINBUFSZ, /* txBuf size */ \
  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 rx_bufsz :16;
  90. rt_uint32_t tx_bufsz :16;
  91. rt_uint32_t reserved :6;
  92. };
  93. /*
  94. * Serial Receive FIFO mode
  95. */
  96. struct rt_serial_rx_fifo
  97. {
  98. struct rt_ringbuffer rb;
  99. struct rt_completion rx_cpt;
  100. rt_uint16_t rx_cpt_index;
  101. /* software fifo */
  102. rt_uint8_t buffer[];
  103. };
  104. /*
  105. * Serial Transmit FIFO mode
  106. */
  107. struct rt_serial_tx_fifo
  108. {
  109. struct rt_ringbuffer rb;
  110. rt_size_t put_size;
  111. rt_bool_t activated;
  112. struct rt_completion tx_cpt;
  113. /* software fifo */
  114. rt_uint8_t buffer[];
  115. };
  116. struct rt_serial_device
  117. {
  118. struct rt_device parent;
  119. const struct rt_uart_ops *ops;
  120. struct serial_configure config;
  121. void *serial_rx;
  122. void *serial_tx;
  123. };
  124. /**
  125. * uart operators
  126. */
  127. struct rt_uart_ops
  128. {
  129. rt_err_t (*configure)(struct rt_serial_device *serial,
  130. struct serial_configure *cfg);
  131. rt_err_t (*control)(struct rt_serial_device *serial,
  132. int cmd,
  133. void *arg);
  134. int (*putc)(struct rt_serial_device *serial, char c);
  135. int (*getc)(struct rt_serial_device *serial);
  136. rt_size_t (*transmit)(struct rt_serial_device *serial,
  137. rt_uint8_t *buf,
  138. rt_size_t size,
  139. rt_uint32_t tx_flag);
  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