serial.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * File : serial.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-05-15 lgnq first version.
  23. * 2012-05-28 bernard change interfaces
  24. * 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define
  25. * the size of ring buffer.
  26. */
  27. #ifndef __SERIAL_H__
  28. #define __SERIAL_H__
  29. #include <rtthread.h>
  30. #define BAUD_RATE_2400 2400
  31. #define BAUD_RATE_4800 4800
  32. #define BAUD_RATE_9600 9600
  33. #define BAUD_RATE_19200 19200
  34. #define BAUD_RATE_38400 38400
  35. #define BAUD_RATE_57600 57600
  36. #define BAUD_RATE_115200 115200
  37. #define BAUD_RATE_230400 230400
  38. #define BAUD_RATE_460800 460800
  39. #define BAUD_RATE_921600 921600
  40. #define BAUD_RATE_2000000 2000000
  41. #define BAUD_RATE_3000000 3000000
  42. #define DATA_BITS_5 5
  43. #define DATA_BITS_6 6
  44. #define DATA_BITS_7 7
  45. #define DATA_BITS_8 8
  46. #define DATA_BITS_9 9
  47. #define STOP_BITS_1 0
  48. #define STOP_BITS_2 1
  49. #define STOP_BITS_3 2
  50. #define STOP_BITS_4 3
  51. #ifdef _WIN32
  52. #include <windows.h>
  53. #else
  54. #define PARITY_NONE 0
  55. #define PARITY_ODD 1
  56. #define PARITY_EVEN 2
  57. #endif
  58. #define BIT_ORDER_LSB 0
  59. #define BIT_ORDER_MSB 1
  60. #define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
  61. #define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
  62. #ifndef RT_SERIAL_RB_BUFSZ
  63. #define RT_SERIAL_RB_BUFSZ 64
  64. #endif
  65. #define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
  66. #define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */
  67. #define RT_SERIAL_EVENT_RX_DMADONE 0x03 /* Rx DMA transfer done */
  68. #define RT_SERIAL_EVENT_TX_DMADONE 0x04 /* Tx DMA transfer done */
  69. #define RT_SERIAL_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
  70. #define RT_SERIAL_DMA_RX 0x01
  71. #define RT_SERIAL_DMA_TX 0x02
  72. #define RT_SERIAL_RX_INT 0x01
  73. #define RT_SERIAL_TX_INT 0x02
  74. #define RT_SERIAL_ERR_OVERRUN 0x01
  75. #define RT_SERIAL_ERR_FRAMING 0x02
  76. #define RT_SERIAL_ERR_PARITY 0x03
  77. #define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
  78. #define RT_SERIAL_TX_DATAQUEUE_LWM 30
  79. /* Default config for serial_configure structure */
  80. #define RT_SERIAL_CONFIG_DEFAULT \
  81. { \
  82. BAUD_RATE_115200, /* 115200 bits/s */ \
  83. DATA_BITS_8, /* 8 databits */ \
  84. STOP_BITS_1, /* 1 stopbit */ \
  85. PARITY_NONE, /* No parity */ \
  86. BIT_ORDER_LSB, /* LSB first sent */ \
  87. NRZ_NORMAL, /* Normal mode */ \
  88. RT_SERIAL_RB_BUFSZ, /* Buffer size */ \
  89. 0 \
  90. }
  91. struct serial_configure
  92. {
  93. rt_uint32_t baud_rate;
  94. rt_uint32_t data_bits :4;
  95. rt_uint32_t stop_bits :2;
  96. rt_uint32_t parity :2;
  97. rt_uint32_t bit_order :1;
  98. rt_uint32_t invert :1;
  99. rt_uint32_t bufsz :16;
  100. rt_uint32_t reserved :4;
  101. };
  102. /*
  103. * Serial FIFO mode
  104. */
  105. struct rt_serial_rx_fifo
  106. {
  107. /* software fifo */
  108. rt_uint8_t *buffer;
  109. rt_uint16_t put_index, get_index;
  110. rt_bool_t is_full;
  111. };
  112. struct rt_serial_tx_fifo
  113. {
  114. struct rt_completion completion;
  115. };
  116. /*
  117. * Serial DMA mode
  118. */
  119. struct rt_serial_rx_dma
  120. {
  121. rt_bool_t activated;
  122. };
  123. struct rt_serial_tx_dma
  124. {
  125. rt_bool_t activated;
  126. struct rt_data_queue data_queue;
  127. };
  128. struct rt_serial_device
  129. {
  130. struct rt_device parent;
  131. const struct rt_uart_ops *ops;
  132. struct serial_configure config;
  133. void *serial_rx;
  134. void *serial_tx;
  135. };
  136. typedef struct rt_serial_device rt_serial_t;
  137. /**
  138. * uart operators
  139. */
  140. struct rt_uart_ops
  141. {
  142. rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
  143. rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
  144. int (*putc)(struct rt_serial_device *serial, char c);
  145. int (*getc)(struct rt_serial_device *serial);
  146. rt_size_t (*dma_transmit)(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);
  147. };
  148. void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
  149. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  150. const char *name,
  151. rt_uint32_t flag,
  152. void *data);
  153. #endif