uart.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef DEVICE_UART_H
  2. #define DEVICE_UART_H
  3. #include "typedef.h"
  4. #include "device/device.h"
  5. #include "generic/ioctl.h"
  6. #include "system/task.h"
  7. #define UART_DISABLE 0x00000000
  8. #define UART_DMA_SUPPORT 0x00000001
  9. #define UART_TX_USE_DMA 0x00000003
  10. #define UART_RX_USE_DMA 0x00000005
  11. #define UART_DEBUG 0x00000008
  12. struct uart_outport {
  13. u8 tx_pin;
  14. u8 rx_pin;
  15. u16 value;
  16. };
  17. extern void putbyte(char a);
  18. enum uart_clk_src {
  19. LSB_CLK,
  20. OSC_CLK,
  21. PLL_48M,
  22. };
  23. enum _uart_port_out {
  24. //uart0
  25. PORTC_0_1 = 0x00001000,
  26. PORTG_6_7 = 0x00002000,
  27. PORTH_12_13 = 0x00003000,
  28. PORTB_14_15 = 0x00004000,
  29. //uart1
  30. PORTC_2_3 = 0x00005000,
  31. PORTH_2_5 = 0x00006000,
  32. PORTH_14_15 = 0x00007000,
  33. PORTC_6_7 = 0x00008000,
  34. //uart3
  35. PORTE_0_1 = 0x00009000,
  36. PORTB_4_3 = 0x0000A000,
  37. PORTD_9_10 = 0x0000B000,
  38. PORTD_14_15 = 0x0000C000,
  39. PORT_REMAP = 0x0000D000,
  40. };
  41. struct uart_platform_data {
  42. u8 *name;
  43. u8 irq;
  44. u8 tx_pin;
  45. u8 rx_pin;
  46. u32 flags;
  47. u32 baudrate;
  48. enum _uart_port_out port;
  49. void (*port_remap_func)(void);
  50. u32 max_continue_recv_cnt;
  51. u32 idle_sys_clk_cnt;
  52. enum uart_clk_src clk_src;
  53. };
  54. enum {
  55. UART_CIRCULAR_BUFFER_WRITE_OVERLAY = -1,
  56. UART_RECV_TIMEOUT = -2,
  57. UART_RECV_EXIT = -3,
  58. };
  59. #define UART_MAGIC 'U'
  60. #define UART_FLUSH _IO(UART_MAGIC,1)
  61. #define UART_SET_RECV_ALL _IOW(UART_MAGIC,2,bool)
  62. #define UART_SET_RECV_BLOCK _IOW(UART_MAGIC,3,bool)
  63. #define UART_SET_RECV_TIMEOUT _IOW(UART_MAGIC,4,u32)
  64. #define UART_SET_RECV_TIMEOUT_CB _IOW(UART_MAGIC,5,int (*)(void))
  65. #define UART_GET_RECV_CNT _IOR(UART_MAGIC,6,u32)
  66. #define UART_START _IO(UART_MAGIC,7)
  67. #define UART_SET_CIRCULAR_BUFF_ADDR _IOW(UART_MAGIC,8,void *)
  68. #define UART_SET_CIRCULAR_BUFF_LENTH _IOW(UART_MAGIC,9,u32)
  69. #define UART_PLATFORM_DATA_BEGIN(data) \
  70. static const struct uart_platform_data data = {
  71. #define UART_PLATFORM_DATA_END() \
  72. };
  73. struct uart_device {
  74. char *name;
  75. const struct uart_operations *ops;
  76. struct device dev;
  77. const struct uart_platform_data *priv;
  78. OS_MUTEX mutex;
  79. };
  80. struct uart_operations {
  81. int (*init)(struct uart_device *);
  82. int (*read)(struct uart_device *, void *buf, u32 len);
  83. int (*write)(struct uart_device *, void *buf, u16 len);
  84. int (*ioctl)(struct uart_device *, u32 cmd, u32 arg);
  85. int (*close)(struct uart_device *);
  86. };
  87. #define REGISTER_UART_DEVICE(dev) \
  88. static struct uart_device dev sec(.uart)
  89. extern struct uart_device uart_device_begin[], uart_device_end[];
  90. #define list_for_each_uart_device(p) \
  91. for (p=uart_device_begin; p<uart_device_end; p++)
  92. extern const struct device_operations uart_dev_ops;
  93. #endif