serial.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2006-2018, 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_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_RB_BUFSZ
  49. #define RT_SERIAL_RB_BUFSZ 64
  50. #endif
  51. #define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
  52. #define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */
  53. #define RT_SERIAL_EVENT_RX_DMADONE 0x03 /* Rx DMA transfer done */
  54. #define RT_SERIAL_EVENT_TX_DMADONE 0x04 /* Tx DMA transfer done */
  55. #define RT_SERIAL_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
  56. #define RT_SERIAL_DMA_RX 0x01
  57. #define RT_SERIAL_DMA_TX 0x02
  58. #define RT_SERIAL_RX_INT 0x01
  59. #define RT_SERIAL_TX_INT 0x02
  60. #define RT_SERIAL_ERR_OVERRUN 0x01
  61. #define RT_SERIAL_ERR_FRAMING 0x02
  62. #define RT_SERIAL_ERR_PARITY 0x03
  63. #define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
  64. #define RT_SERIAL_TX_DATAQUEUE_LWM 30
  65. /* Default config for serial_configure structure */
  66. #define RT_SERIAL_CONFIG_DEFAULT \
  67. { \
  68. BAUD_RATE_115200, /* 115200 bits/s */ \
  69. DATA_BITS_8, /* 8 databits */ \
  70. STOP_BITS_1, /* 1 stopbit */ \
  71. PARITY_NONE, /* No parity */ \
  72. BIT_ORDER_LSB, /* LSB first sent */ \
  73. NRZ_NORMAL, /* Normal mode */ \
  74. RT_SERIAL_RB_BUFSZ, /* Buffer size */ \
  75. 0 \
  76. }
  77. struct serial_configure
  78. {
  79. rt_uint32_t baud_rate;
  80. rt_uint32_t data_bits :4;
  81. rt_uint32_t stop_bits :2;
  82. rt_uint32_t parity :2;
  83. rt_uint32_t bit_order :1;
  84. rt_uint32_t invert :1;
  85. rt_uint32_t bufsz :16;
  86. rt_uint32_t reserved :6;
  87. };
  88. /*
  89. * Serial FIFO mode
  90. */
  91. struct rt_serial_rx_fifo
  92. {
  93. /* software fifo */
  94. rt_uint8_t *buffer;
  95. rt_uint16_t put_index, get_index;
  96. rt_bool_t is_full;
  97. };
  98. struct rt_serial_tx_fifo
  99. {
  100. struct rt_completion completion;
  101. };
  102. /*
  103. * Serial DMA mode
  104. */
  105. struct rt_serial_rx_dma
  106. {
  107. rt_bool_t activated;
  108. };
  109. struct rt_serial_tx_dma
  110. {
  111. rt_bool_t activated;
  112. struct rt_data_queue data_queue;
  113. };
  114. struct rt_serial_device
  115. {
  116. struct rt_device parent;
  117. const struct rt_uart_ops *ops;
  118. struct serial_configure config;
  119. void *serial_rx;
  120. void *serial_tx;
  121. };
  122. typedef struct rt_serial_device rt_serial_t;
  123. /**
  124. * uart operators
  125. */
  126. struct rt_uart_ops
  127. {
  128. rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
  129. rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
  130. int (*putc)(struct rt_serial_device *serial, char c);
  131. int (*getc)(struct rt_serial_device *serial);
  132. rt_size_t (*dma_transmit)(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);
  133. };
  134. void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
  135. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  136. const char *name,
  137. rt_uint32_t flag,
  138. void *data);
  139. #endif