serial.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_9600 9600
  19. #define BAUD_RATE_115200 115200
  20. #define DATA_BITS_5 5
  21. #define DATA_BITS_6 6
  22. #define DATA_BITS_7 7
  23. #define DATA_BITS_8 8
  24. #define DATA_BITS_9 9
  25. #define STOP_BITS_1 0
  26. #define STOP_BITS_2 1
  27. #define STOP_BITS_3 2
  28. #define STOP_BITS_4 3
  29. #define PARITY_NONE 0
  30. #define PARITY_ODD 1
  31. #define PARITY_EVEN 2
  32. #define BIT_ORDER_LSB 0
  33. #define BIT_ORDER_MSB 1
  34. #define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
  35. #define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
  36. #define UART_RX_BUFFER_SIZE 64
  37. #define UART_TX_BUFFER_SIZE 64
  38. #define SERIAL_RBUFFER_SIZE 64
  39. #define RT_DEVICE_CTRL_CONFIG 0x03 /* configure device */
  40. #define RT_DEVICE_CTRL_SET_INT 0x10 /* enable receive irq */
  41. #define RT_DEVICE_CTRL_CLR_INT 0x11 /* disable receive irq */
  42. #define RT_DEVICE_CTRL_GET_INT 0x12
  43. #define RT_SERIAL_RX_INT 0x01
  44. #define RT_SERIAL_TX_INT 0x02
  45. #define RT_SERIAL_ERR_OVERRUN 0x01
  46. #define RT_SERIAL_ERR_FRAMING 0x02
  47. #define RT_SERIAL_ERR_PARITY 0x03
  48. struct serial_ringbuffer
  49. {
  50. rt_uint8_t buffer[SERIAL_RBUFFER_SIZE];
  51. rt_uint16_t put_index, get_index;
  52. };
  53. struct serial_configure
  54. {
  55. rt_uint32_t baud_rate;
  56. rt_uint32_t data_bits :4;
  57. rt_uint32_t stop_bits :2;
  58. rt_uint32_t parity :2;
  59. rt_uint32_t bit_order :1;
  60. rt_uint32_t invert :1;
  61. rt_uint32_t reserved :20;
  62. };
  63. struct rt_serial_device
  64. {
  65. struct rt_device parent;
  66. const struct rt_uart_ops *ops;
  67. struct serial_configure config;
  68. /* rx structure */
  69. struct serial_ringbuffer *int_rx;
  70. /* tx structure */
  71. struct serial_ringbuffer *int_tx;
  72. };
  73. typedef struct rt_serial_device rt_serial_t;
  74. /**
  75. * uart operators
  76. */
  77. struct rt_uart_ops
  78. {
  79. rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
  80. rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
  81. int (*putc)(struct rt_serial_device *serial, char c);
  82. int (*getc)(struct rt_serial_device *serial);
  83. };
  84. void rt_hw_serial_isr(struct rt_serial_device *serial);
  85. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  86. const char *name,
  87. rt_uint32_t flag,
  88. void *data);
  89. #endif