serial.h 4.2 KB

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