serial.c 3.8 KB

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