drv_uart.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. default:
  57. return -1;
  58. }
  59. return RT_EOK;
  60. }
  61. static int uart_putc(struct rt_serial_device *serial, char c)
  62. {
  63. struct hw_uart_device *uart;
  64. RT_ASSERT(serial != RT_NULL);
  65. uart = (struct hw_uart_device *)serial->parent.user_data;
  66. while (UART_FR(uart->hw_base) & UARTFR_TXFF);
  67. UART_DR(uart->hw_base) = c;
  68. return 1;
  69. }
  70. static int uart_getc(struct rt_serial_device *serial)
  71. {
  72. int ch;
  73. struct hw_uart_device *uart;
  74. RT_ASSERT(serial != RT_NULL);
  75. uart = (struct hw_uart_device *)serial->parent.user_data;
  76. ch = -1;
  77. if (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
  78. {
  79. ch = UART_DR(uart->hw_base) & 0xff;
  80. }
  81. return ch;
  82. }
  83. static const struct rt_uart_ops _uart_ops =
  84. {
  85. uart_configure,
  86. uart_control,
  87. uart_putc,
  88. uart_getc,
  89. };
  90. #ifdef RT_USING_UART0
  91. /* UART device driver structure */
  92. static struct hw_uart_device _uart0_device =
  93. {
  94. PL011_UART0_BASE,
  95. PL011_UART0_IRQNUM,
  96. };
  97. static struct rt_serial_device _serial0;
  98. #endif
  99. int rt_hw_uart_init(void)
  100. {
  101. struct hw_uart_device *uart;
  102. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  103. #ifdef RT_USING_UART0
  104. _uart0_device.hw_base = (rt_size_t)rt_ioremap((void*)_uart0_device.hw_base, PL011_UART0_SIZE);
  105. uart = &_uart0_device;
  106. _serial0.ops = &_uart_ops;
  107. _serial0.config = config;
  108. /* register UART1 device */
  109. rt_hw_serial_register(&_serial0, "uart0",
  110. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  111. uart);
  112. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial0, "uart0");
  113. /* enable Rx and Tx of UART */
  114. UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  115. #endif
  116. return 0;
  117. }