drv_uart.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2006-2025 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 version
  9. * 2025-04-08 Hydevcode second version
  10. *
  11. */
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include <rtdbg.h>
  15. #include "drv_uart.h"
  16. #include "board.h"
  17. #include "mmu.h"
  18. #ifdef RT_USING_SERIAL_V1
  19. struct hw_uart_device
  20. {
  21. rt_uint32_t hw_base;
  22. rt_uint32_t irqno;
  23. struct rt_serial_device *serial;
  24. char *device_name;
  25. };
  26. #define UART_DR(base) __REG32(base + 0x00)
  27. #define UART_FR(base) __REG32(base + 0x18)
  28. #define UART_CR(base) __REG32(base + 0x30)
  29. #define UART_IMSC(base) __REG32(base + 0x38)
  30. #define UART_ICR(base) __REG32(base + 0x44)
  31. #define UARTFR_RXFE 0x10
  32. #define UARTFR_TXFF 0x20
  33. #define UARTIMSC_RXIM 0x10
  34. #define UARTIMSC_TXIM 0x20
  35. #define UARTICR_RXIC 0x10
  36. #define UARTICR_TXIC 0x20
  37. #if defined(BSP_USING_UART0)
  38. struct rt_serial_device serial0;
  39. #endif
  40. #if defined(BSP_USING_UART1)
  41. struct rt_serial_device serial1;
  42. #endif
  43. #if defined(BSP_USING_UART2)
  44. struct rt_serial_device serial2;
  45. #endif
  46. #if defined(BSP_USING_UART3)
  47. struct rt_serial_device serial3;
  48. #endif
  49. static struct hw_uart_device _uart_device[] = {
  50. #if defined(BSP_USING_UART0)
  51. {
  52. REALVIEW_UART0_BASE,
  53. IRQ_PBA8_UART0,
  54. &serial0,
  55. "uart0",
  56. },
  57. #endif
  58. #if defined(BSP_USING_UART1)
  59. {
  60. REALVIEW_UART1_BASE,
  61. IRQ_PBA8_UART1,
  62. &serial1,
  63. "uart1",
  64. },
  65. #endif
  66. #if defined(BSP_USING_UART2)
  67. {
  68. REALVIEW_UART2_BASE,
  69. IRQ_PBA8_UART2,
  70. &serial2,
  71. "uart2",
  72. },
  73. #endif
  74. #if defined(BSP_USING_UART3)
  75. {
  76. REALVIEW_UART3_BASE,
  77. IRQ_PBA8_UART3,
  78. &serial3,
  79. "uart3",
  80. },
  81. #endif
  82. };
  83. /**
  84. * @brief UART common interrupt process. This
  85. *
  86. * @param serial Serial device
  87. */
  88. static void rt_hw_uart_isr(int irqno, void *param)
  89. {
  90. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  91. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  92. }
  93. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  94. {
  95. return RT_EOK;
  96. }
  97. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  98. {
  99. struct hw_uart_device *uart;
  100. RT_ASSERT(serial != RT_NULL);
  101. uart = (struct hw_uart_device *)serial->parent.user_data;
  102. switch (cmd)
  103. {
  104. case RT_DEVICE_CTRL_CLR_INT:
  105. /* disable rx irq */
  106. UART_IMSC(uart->hw_base) &= ~UARTIMSC_RXIM;
  107. break;
  108. case RT_DEVICE_CTRL_SET_INT:
  109. /* enable rx irq */
  110. UART_IMSC(uart->hw_base) |= UARTIMSC_RXIM;
  111. rt_hw_interrupt_umask(uart->irqno);
  112. break;
  113. }
  114. return RT_EOK;
  115. }
  116. static int uart_putc(struct rt_serial_device *serial, char ch)
  117. {
  118. struct hw_uart_device *uart;
  119. RT_ASSERT(serial != RT_NULL);
  120. uart = (struct hw_uart_device *)serial->parent.user_data;
  121. while (UART_FR(uart->hw_base) & UARTFR_TXFF);
  122. UART_DR(uart->hw_base) = ch;
  123. return 1;
  124. }
  125. static int uart_getc(struct rt_serial_device *serial)
  126. {
  127. int ch;
  128. struct hw_uart_device *uart;
  129. RT_ASSERT(serial != RT_NULL);
  130. uart = (struct hw_uart_device *)serial->parent.user_data;
  131. ch = -1;
  132. if (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
  133. {
  134. ch = UART_DR(uart->hw_base) & 0xff;
  135. }
  136. return ch;
  137. }
  138. static const struct rt_uart_ops _uart_ops = {
  139. uart_configure,
  140. uart_control,
  141. uart_putc,
  142. uart_getc,
  143. };
  144. int rt_hw_uart_init(void)
  145. {
  146. rt_err_t err = RT_EOK;
  147. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  148. for (uint32_t i = 0; i < sizeof(_uart_device) / sizeof(_uart_device[0]); i++)
  149. {
  150. #ifdef RT_USING_SMART
  151. _uart_device[i].hw_base = (uint32_t)rt_ioremap((void*)_uart_device[i].hw_base, 0x1000);
  152. #endif
  153. _uart_device[i].serial->ops = &_uart_ops;
  154. _uart_device[i].serial->config = config;
  155. /* register UART device */
  156. err = rt_hw_serial_register(_uart_device[i].serial,
  157. _uart_device[i].device_name,
  158. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  159. (void*)&_uart_device[i]);
  160. rt_hw_interrupt_install(_uart_device[i].irqno, rt_hw_uart_isr, _uart_device[i].serial, _uart_device[i].device_name);
  161. /* enable Rx and Tx of UART */
  162. UART_CR(_uart_device[i].hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  163. }
  164. return err;
  165. }
  166. INIT_BOARD_EXPORT(rt_hw_uart_init);
  167. #endif /* RT_USING_SERIAL */