drv_uart.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <rthw.h>
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #include "drv_uart.h"
  14. #include <stdio.h>
  15. // #include "uart.h"
  16. #include "uarths.h"
  17. #include "plic.h"
  18. struct device_uart
  19. {
  20. rt_uint32_t hw_base;
  21. rt_uint32_t irqno;
  22. };
  23. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  24. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg);
  25. static int drv_uart_putc(struct rt_serial_device *serial, char c);
  26. static int drv_uart_getc(struct rt_serial_device *serial);
  27. static void uart_irq_handler(int irqno, void *param);
  28. const struct rt_uart_ops _uart_ops =
  29. {
  30. uart_configure,
  31. uart_control,
  32. drv_uart_putc,
  33. drv_uart_getc,
  34. RT_NULL
  35. };
  36. /*
  37. * UART Initiation
  38. */
  39. int rt_hw_uart_init(void)
  40. {
  41. struct rt_serial_device *serial;
  42. struct device_uart *uart;
  43. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  44. #ifdef BSP_USING_UART_HS
  45. {
  46. static struct rt_serial_device serial_hs;
  47. static struct device_uart uart_hs;
  48. serial = &serial_hs;
  49. uart = &uart_hs;
  50. serial->ops = &_uart_ops;
  51. serial->config = config;
  52. serial->config.baud_rate = 115200;
  53. uart->hw_base = UARTHS_BASE_ADDR;
  54. uart->irqno = IRQN_UARTHS_INTERRUPT;
  55. /* initialize UART HS */
  56. uarths_init();
  57. rt_hw_serial_register(serial,
  58. "uarths",
  59. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  60. uart);
  61. }
  62. #endif
  63. #ifdef BSP_USING_UART1
  64. {
  65. static struct rt_serial_device serial1;
  66. static struct device_uart uart1;
  67. serial = &serial1;
  68. uart = &uart1;
  69. serial->ops = &_uart_ops;
  70. serial->config = config;
  71. serial->config.baud_rate = 115200;
  72. uart->hw_base = UART1_BASE_ADDR;
  73. uart->irqno = IRQN_UART1_INTERRUPT;
  74. rt_hw_serial_register(serial,
  75. "uarths",
  76. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  77. uart);
  78. }
  79. #endif
  80. #ifdef BSP_USING_UART2
  81. #endif
  82. #ifdef BSP_USING_UART3
  83. #endif
  84. return 0;
  85. }
  86. /*
  87. * UART interface
  88. */
  89. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  90. {
  91. rt_uint32_t baud_div;
  92. struct device_uart *uart;
  93. RT_ASSERT(serial != RT_NULL);
  94. serial->config = *cfg;
  95. uart = serial->parent.user_data;
  96. RT_ASSERT(uart != RT_NULL);
  97. /* Init UART Hardware */
  98. /* Enable UART clock */
  99. /* Set both receiver and transmitter in UART mode (not SIR) */
  100. /* Set databits, stopbits and parity. (8-bit data, 1 stopbit, no parity) */
  101. /* set baudrate */
  102. return (RT_EOK);
  103. }
  104. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  105. {
  106. struct device_uart *uart;
  107. uart = serial->parent.user_data;
  108. RT_ASSERT(uart != RT_NULL);
  109. switch (cmd)
  110. {
  111. case RT_DEVICE_CTRL_CLR_INT:
  112. /* Disable the UART Interrupt */
  113. rt_hw_interrupt_mask(uart->irqno);
  114. break;
  115. case RT_DEVICE_CTRL_SET_INT:
  116. /* install interrupt */
  117. rt_hw_interrupt_install(uart->irqno, uart_irq_handler,
  118. serial, serial->parent.parent.name);
  119. rt_hw_interrupt_umask(uart->irqno);
  120. break;
  121. }
  122. return (RT_EOK);
  123. }
  124. static int drv_uart_putc(struct rt_serial_device *serial, char c)
  125. {
  126. struct device_uart *uart;
  127. uart = serial->parent.user_data;
  128. if (uart->hw_base == UARTHS_BASE_ADDR)
  129. {
  130. uarths_putchar(c);
  131. }
  132. else
  133. {
  134. /* other uart */
  135. }
  136. return (1);
  137. }
  138. static int drv_uart_getc(struct rt_serial_device *serial)
  139. {
  140. int ret = -1;
  141. struct device_uart *uart = serial->parent.user_data;
  142. if (uart->hw_base == UARTHS_BASE_ADDR)
  143. {
  144. ret = uarths_getc();
  145. if (ret != EOF) return ret;
  146. }
  147. /* Receive Data Available */
  148. return (-1);
  149. }
  150. /* UART ISR */
  151. static void uart_irq_handler(int irqno, void *param)
  152. {
  153. rt_ubase_t isr;
  154. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  155. struct device_uart *uart = serial->parent.user_data;
  156. /* read interrupt status and clear it */
  157. if (uart->hw_base == UARTHS_BASE_ADDR)
  158. {
  159. if (uarths->ip.rxwm)
  160. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  161. }
  162. }