drv_uart.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * serial.c UART driver
  3. * Copyright (c) 2006-2021, RT-Thread Development Team
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2013-03-30 Bernard the first verion
  10. */
  11. #include <rthw.h>
  12. #include <rtdevice.h>
  13. #include "board.h"
  14. #include "mmu.h"
  15. struct hw_uart_device
  16. {
  17. rt_size_t hw_base;
  18. rt_size_t irqno;
  19. };
  20. #define UART_DR(base) __REG32(base + 0x00)
  21. #define UART_FR(base) __REG32(base + 0x18)
  22. #define UART_CR(base) __REG32(base + 0x30)
  23. #define UART_IMSC(base) __REG32(base + 0x38)
  24. #define UART_ICR(base) __REG32(base + 0x44)
  25. #define UARTFR_RXFE 0x10
  26. #define UARTFR_TXFF 0x20
  27. #define UARTIMSC_RXIM 0x10
  28. #define UARTIMSC_TXIM 0x20
  29. #define UARTICR_RXIC 0x10
  30. #define UARTICR_TXIC 0x20
  31. static void rt_hw_uart_isr(int irqno, void *param)
  32. {
  33. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  34. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  35. }
  36. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  37. {
  38. return RT_EOK;
  39. }
  40. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  41. {
  42. struct hw_uart_device *uart;
  43. RT_ASSERT(serial != RT_NULL);
  44. uart = (struct hw_uart_device *)serial->parent.user_data;
  45. switch (cmd)
  46. {
  47. case RT_DEVICE_CTRL_CLR_INT:
  48. /* disable rx irq */
  49. UART_IMSC(uart->hw_base) &= ~UARTIMSC_RXIM;
  50. break;
  51. case RT_DEVICE_CTRL_SET_INT:
  52. /* enable rx irq */
  53. UART_IMSC(uart->hw_base) |= UARTIMSC_RXIM;
  54. rt_hw_interrupt_umask(uart->irqno);
  55. break;
  56. }
  57. return RT_EOK;
  58. }
  59. static int uart_putc(struct rt_serial_device *serial, char c)
  60. {
  61. struct hw_uart_device *uart;
  62. RT_ASSERT(serial != RT_NULL);
  63. uart = (struct hw_uart_device *)serial->parent.user_data;
  64. while (UART_FR(uart->hw_base) & UARTFR_TXFF);
  65. UART_DR(uart->hw_base) = c;
  66. return 1;
  67. }
  68. static int uart_getc(struct rt_serial_device *serial)
  69. {
  70. int ch;
  71. struct hw_uart_device *uart;
  72. RT_ASSERT(serial != RT_NULL);
  73. uart = (struct hw_uart_device *)serial->parent.user_data;
  74. ch = -1;
  75. if (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
  76. {
  77. ch = UART_DR(uart->hw_base) & 0xff;
  78. }
  79. return ch;
  80. }
  81. static const struct rt_uart_ops _uart_ops =
  82. {
  83. uart_configure,
  84. uart_control,
  85. uart_putc,
  86. uart_getc,
  87. };
  88. #ifdef RT_USING_UART0
  89. /* UART device driver structure */
  90. static struct hw_uart_device _uart0_device =
  91. {
  92. PL011_UART0_BASE,
  93. PL011_UART0_IRQNUM,
  94. };
  95. static struct rt_serial_device _serial0;
  96. #endif
  97. int rt_hw_uart_init(void)
  98. {
  99. struct hw_uart_device *uart;
  100. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  101. #ifdef RT_USING_UART0
  102. _uart0_device.hw_base = (rt_size_t)rt_ioremap((void*)_uart0_device.hw_base, PL011_UART0_SIZE);
  103. uart = &_uart0_device;
  104. _serial0.ops = &_uart_ops;
  105. _serial0.config = config;
  106. /* register UART1 device */
  107. rt_hw_serial_register(&_serial0, "uart0",
  108. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  109. uart);
  110. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial0, "uart0");
  111. /* enable Rx and Tx of UART */
  112. UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  113. #endif
  114. return 0;
  115. }