serial.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_38400 38400
  34. #define BAUD_RATE_57600 57600
  35. #define BAUD_RATE_115200 115200
  36. #define BAUD_RATE_230400 230400
  37. #define BAUD_RATE_460800 460800
  38. #define BAUD_RATE_921600 921600
  39. #define DATA_BITS_5 5
  40. #define DATA_BITS_6 6
  41. #define DATA_BITS_7 7
  42. #define DATA_BITS_8 8
  43. #define DATA_BITS_9 9
  44. #define STOP_BITS_1 0
  45. #define STOP_BITS_2 1
  46. #define STOP_BITS_3 2
  47. #define STOP_BITS_4 3
  48. #define PARITY_NONE 0
  49. #define PARITY_ODD 1
  50. #define PARITY_EVEN 2
  51. #define BIT_ORDER_LSB 0
  52. #define BIT_ORDER_MSB 1
  53. #define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
  54. #define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
  55. #ifndef RT_SERIAL_RB_BUFSZ
  56. #define RT_SERIAL_RB_BUFSZ 64
  57. #endif
  58. #define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
  59. #define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */
  60. #define RT_SERIAL_EVENT_RX_DMADONE 0x03 /* Rx DMA transfer done */
  61. #define RT_SERIAL_EVENT_TX_DMADONE 0x04 /* Tx DMA transfer done */
  62. #define RT_SERIAL_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
  63. #define RT_SERIAL_DMA_RX 0x01
  64. #define RT_SERIAL_DMA_TX 0x02
  65. #define RT_SERIAL_RX_INT 0x01
  66. #define RT_SERIAL_TX_INT 0x02
  67. #define RT_SERIAL_ERR_OVERRUN 0x01
  68. #define RT_SERIAL_ERR_FRAMING 0x02
  69. #define RT_SERIAL_ERR_PARITY 0x03
  70. #define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
  71. #define RT_SERIAL_TX_DATAQUEUE_LWM 30
  72. /* Default config for serial_configure structure */
  73. #define RT_SERIAL_CONFIG_DEFAULT \
  74. { \
  75. BAUD_RATE_115200, /* 115200 bits/s */ \
  76. DATA_BITS_8, /* 8 databits */ \
  77. STOP_BITS_1, /* 1 stopbit */ \
  78. PARITY_NONE, /* No parity */ \
  79. BIT_ORDER_LSB, /* LSB first sent */ \
  80. NRZ_NORMAL, /* Normal mode */ \
  81. RT_SERIAL_RB_BUFSZ, /* Buffer size */ \
  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 bufsz :16;
  93. rt_uint32_t reserved :4;
  94. };
  95. /*
  96. * Serial FIFO mode
  97. */
  98. struct rt_serial_rx_fifo
  99. {
  100. /* software fifo */
  101. rt_uint8_t *buffer;
  102. rt_uint16_t put_index, get_index;
  103. };
  104. struct rt_serial_tx_fifo
  105. {
  106. struct rt_completion completion;
  107. };
  108. /*
  109. * Serial DMA mode
  110. */
  111. struct rt_serial_rx_dma
  112. {
  113. rt_bool_t activated;
  114. };
  115. struct rt_serial_tx_dma
  116. {
  117. rt_bool_t activated;
  118. struct rt_data_queue data_queue;
  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. typedef struct rt_serial_device rt_serial_t;
  129. /**
  130. * uart operators
  131. */
  132. struct rt_uart_ops
  133. {
  134. rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
  135. rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
  136. int (*putc)(struct rt_serial_device *serial, char c);
  137. int (*getc)(struct rt_serial_device *serial);
  138. rt_size_t (*dma_transmit)(struct rt_serial_device *serial, const rt_uint8_t *buf, rt_size_t size, int direction);
  139. };
  140. void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
  141. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  142. const char *name,
  143. rt_uint32_t flag,
  144. void *data);
  145. #endif