serial.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_4800 4800
  31. #define BAUD_RATE_9600 9600
  32. #define BAUD_RATE_115200 115200
  33. #define DATA_BITS_5 5
  34. #define DATA_BITS_6 6
  35. #define DATA_BITS_7 7
  36. #define DATA_BITS_8 8
  37. #define DATA_BITS_9 9
  38. #define STOP_BITS_1 0
  39. #define STOP_BITS_2 1
  40. #define STOP_BITS_3 2
  41. #define STOP_BITS_4 3
  42. #define PARITY_NONE 0
  43. #define PARITY_ODD 1
  44. #define PARITY_EVEN 2
  45. #define BIT_ORDER_LSB 0
  46. #define BIT_ORDER_MSB 1
  47. #define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
  48. #define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
  49. #ifndef RT_SERIAL_RB_BUFSZ
  50. #define RT_SERIAL_RB_BUFSZ 64
  51. #endif
  52. #define RT_DEVICE_CTRL_CONFIG 0x03 /* configure device */
  53. #define RT_DEVICE_CTRL_SET_INT 0x10 /* enable receive irq */
  54. #define RT_DEVICE_CTRL_CLR_INT 0x11 /* disable receive irq */
  55. #define RT_DEVICE_CTRL_GET_INT 0x12
  56. #define RT_SERIAL_RX_INT 0x01
  57. #define RT_SERIAL_TX_INT 0x02
  58. #define RT_SERIAL_ERR_OVERRUN 0x01
  59. #define RT_SERIAL_ERR_FRAMING 0x02
  60. #define RT_SERIAL_ERR_PARITY 0x03
  61. #define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
  62. #define RT_SERIAL_TX_DATAQUEUE_LWM 30
  63. /* Default config for serial_configure structure */
  64. #define RT_SERIAL_CONFIG_DEFAULT \
  65. { \
  66. BAUD_RATE_115200, /* 115200 bits/s */ \
  67. DATA_BITS_8, /* 8 databits */ \
  68. STOP_BITS_1, /* 1 stopbit */ \
  69. PARITY_NONE, /* No parity */ \
  70. BIT_ORDER_LSB, /* LSB first sent */ \
  71. NRZ_NORMAL, /* Normal mode */ \
  72. 0 \
  73. }
  74. struct serial_ringbuffer
  75. {
  76. rt_uint8_t buffer[RT_SERIAL_RB_BUFSZ];
  77. rt_uint16_t put_index, get_index;
  78. };
  79. struct serial_configure
  80. {
  81. rt_uint32_t baud_rate;
  82. rt_uint32_t data_bits :4;
  83. rt_uint32_t stop_bits :2;
  84. rt_uint32_t parity :2;
  85. rt_uint32_t bit_order :1;
  86. rt_uint32_t invert :1;
  87. rt_uint32_t reserved :20;
  88. };
  89. struct rt_serial_device
  90. {
  91. struct rt_device parent;
  92. const struct rt_uart_ops *ops;
  93. struct serial_configure config;
  94. /* rx structure */
  95. struct serial_ringbuffer *int_rx;
  96. /* tx structure */
  97. struct serial_ringbuffer *int_tx;
  98. struct rt_data_queue tx_dq; /* tx dataqueue */
  99. volatile rt_bool_t dma_flag; /* dma transfer flag */
  100. };
  101. typedef struct rt_serial_device rt_serial_t;
  102. /**
  103. * uart operators
  104. */
  105. struct rt_uart_ops
  106. {
  107. rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
  108. rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
  109. int (*putc)(struct rt_serial_device *serial, char c);
  110. int (*getc)(struct rt_serial_device *serial);
  111. rt_size_t (*dma_transmit)(struct rt_serial_device *serial, const char *buf, rt_size_t size);
  112. };
  113. void rt_hw_serial_isr(struct rt_serial_device *serial);
  114. void rt_hw_serial_dma_tx_isr(struct rt_serial_device *serial);
  115. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  116. const char *name,
  117. rt_uint32_t flag,
  118. void *data);
  119. #endif