serial.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-05-15 lgnq first version.
  13. * 2012-05-28 bernard change interfaces
  14. * 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define
  15. * the size of ring buffer.
  16. */
  17. #ifndef __SERIAL_H__
  18. #define __SERIAL_H__
  19. #include <rtthread.h>
  20. #define BAUD_RATE_4800 4800
  21. #define BAUD_RATE_9600 9600
  22. #define BAUD_RATE_115200 115200
  23. #define DATA_BITS_5 5
  24. #define DATA_BITS_6 6
  25. #define DATA_BITS_7 7
  26. #define DATA_BITS_8 8
  27. #define DATA_BITS_9 9
  28. #define STOP_BITS_1 0
  29. #define STOP_BITS_2 1
  30. #define STOP_BITS_3 2
  31. #define STOP_BITS_4 3
  32. #define PARITY_NONE 0
  33. #define PARITY_ODD 1
  34. #define PARITY_EVEN 2
  35. #define BIT_ORDER_LSB 0
  36. #define BIT_ORDER_MSB 1
  37. #define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
  38. #define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
  39. #ifndef RT_SERIAL_RB_BUFSZ
  40. #define RT_SERIAL_RB_BUFSZ 64
  41. #endif
  42. #define RT_DEVICE_CTRL_CONFIG 0x03 /* configure device */
  43. #define RT_DEVICE_CTRL_SET_INT 0x10 /* enable receive irq */
  44. #define RT_DEVICE_CTRL_CLR_INT 0x11 /* disable receive irq */
  45. #define RT_DEVICE_CTRL_GET_INT 0x12
  46. #define RT_SERIAL_RX_INT 0x01
  47. #define RT_SERIAL_TX_INT 0x02
  48. #define RT_SERIAL_ERR_OVERRUN 0x01
  49. #define RT_SERIAL_ERR_FRAMING 0x02
  50. #define RT_SERIAL_ERR_PARITY 0x03
  51. #define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
  52. #define RT_SERIAL_TX_DATAQUEUE_LWM 30
  53. /* Default config for serial_configure structure */
  54. #define RT_SERIAL_CONFIG_DEFAULT \
  55. { \
  56. BAUD_RATE_115200, /* 115200 bits/s */ \
  57. DATA_BITS_8, /* 8 databits */ \
  58. STOP_BITS_1, /* 1 stopbit */ \
  59. PARITY_NONE, /* No parity */ \
  60. BIT_ORDER_LSB, /* LSB first sent */ \
  61. NRZ_NORMAL, /* Normal mode */ \
  62. 0 \
  63. }
  64. struct serial_ringbuffer
  65. {
  66. rt_uint8_t buffer[RT_SERIAL_RB_BUFSZ];
  67. rt_uint16_t put_index, get_index;
  68. };
  69. struct serial_configure
  70. {
  71. rt_uint32_t baud_rate;
  72. rt_uint32_t data_bits :4;
  73. rt_uint32_t stop_bits :2;
  74. rt_uint32_t parity :2;
  75. rt_uint32_t bit_order :1;
  76. rt_uint32_t invert :1;
  77. rt_uint32_t reserved :20;
  78. };
  79. struct rt_serial_device
  80. {
  81. struct rt_device parent;
  82. const struct rt_uart_ops *ops;
  83. struct serial_configure config;
  84. /* rx structure */
  85. struct serial_ringbuffer *int_rx;
  86. /* tx structure */
  87. struct serial_ringbuffer *int_tx;
  88. struct rt_data_queue tx_dq; /* tx dataqueue */
  89. volatile rt_bool_t dma_flag; /* dma transfer flag */
  90. };
  91. typedef struct rt_serial_device rt_serial_t;
  92. /**
  93. * uart operators
  94. */
  95. struct rt_uart_ops
  96. {
  97. rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
  98. rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
  99. int (*putc)(struct rt_serial_device *serial, char c);
  100. int (*getc)(struct rt_serial_device *serial);
  101. rt_size_t (*dma_transmit)(struct rt_serial_device *serial, const char *buf, rt_size_t size);
  102. };
  103. void rt_hw_serial_isr(struct rt_serial_device *serial);
  104. void rt_hw_serial_dma_tx_isr(struct rt_serial_device *serial);
  105. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  106. const char *name,
  107. rt_uint32_t flag,
  108. void *data);
  109. #endif