serial.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #define PARITY_NONE 0
  52. #define PARITY_ODD 1
  53. #define PARITY_EVEN 2
  54. #define BIT_ORDER_LSB 0
  55. #define BIT_ORDER_MSB 1
  56. #define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
  57. #define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
  58. #ifndef RT_SERIAL_RB_BUFSZ
  59. #define RT_SERIAL_RB_BUFSZ 64
  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. /* Default config for serial_configure structure */
  76. #define RT_SERIAL_CONFIG_DEFAULT \
  77. { \
  78. BAUD_RATE_115200, /* 115200 bits/s */ \
  79. DATA_BITS_8, /* 8 databits */ \
  80. STOP_BITS_1, /* 1 stopbit */ \
  81. PARITY_NONE, /* No parity */ \
  82. BIT_ORDER_LSB, /* LSB first sent */ \
  83. NRZ_NORMAL, /* Normal mode */ \
  84. RT_SERIAL_RB_BUFSZ, /* Buffer size */ \
  85. 0 \
  86. }
  87. struct serial_configure
  88. {
  89. rt_uint32_t baud_rate;
  90. rt_uint32_t data_bits :4;
  91. rt_uint32_t stop_bits :2;
  92. rt_uint32_t parity :2;
  93. rt_uint32_t bit_order :1;
  94. rt_uint32_t invert :1;
  95. rt_uint32_t bufsz :16;
  96. rt_uint32_t reserved :4;
  97. };
  98. /*
  99. * Serial FIFO mode
  100. */
  101. struct rt_serial_rx_fifo
  102. {
  103. /* software fifo */
  104. rt_uint8_t *buffer;
  105. rt_uint16_t put_index, get_index;
  106. };
  107. struct rt_serial_tx_fifo
  108. {
  109. struct rt_completion completion;
  110. };
  111. /*
  112. * Serial DMA mode
  113. */
  114. struct rt_serial_rx_dma
  115. {
  116. rt_bool_t activated;
  117. };
  118. struct rt_serial_tx_dma
  119. {
  120. rt_bool_t activated;
  121. struct rt_data_queue data_queue;
  122. };
  123. struct rt_serial_device
  124. {
  125. struct rt_device parent;
  126. const struct rt_uart_ops *ops;
  127. struct serial_configure config;
  128. void *serial_rx;
  129. void *serial_tx;
  130. };
  131. typedef struct rt_serial_device rt_serial_t;
  132. /**
  133. * uart operators
  134. */
  135. struct rt_uart_ops
  136. {
  137. rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
  138. rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
  139. int (*putc)(struct rt_serial_device *serial, char c);
  140. int (*getc)(struct rt_serial_device *serial);
  141. rt_size_t (*dma_transmit)(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);
  142. };
  143. void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
  144. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  145. const char *name,
  146. rt_uint32_t flag,
  147. void *data);
  148. #endif