drv_uart.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 <sysctl.h>
  16. // #include "uart.h"
  17. #include "uarths.h"
  18. #include "plic.h"
  19. static volatile uarths_t *const _uarths = (volatile uarths_t *)UARTHS_BASE_ADDR;
  20. struct device_uart
  21. {
  22. rt_uint32_t hw_base;
  23. rt_uint32_t irqno;
  24. };
  25. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  26. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg);
  27. static int drv_uart_putc(struct rt_serial_device *serial, char c);
  28. static int drv_uart_getc(struct rt_serial_device *serial);
  29. static void uart_irq_handler(int irqno, void *param);
  30. const struct rt_uart_ops _uart_ops =
  31. {
  32. uart_configure,
  33. uart_control,
  34. drv_uart_putc,
  35. drv_uart_getc,
  36. RT_NULL
  37. };
  38. /*
  39. * UART Initiation
  40. */
  41. int rt_hw_uart_init(void)
  42. {
  43. struct rt_serial_device *serial;
  44. struct device_uart *uart;
  45. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  46. #ifdef BSP_USING_UART_HS
  47. {
  48. static struct rt_serial_device serial_hs;
  49. static struct device_uart uart_hs;
  50. serial = &serial_hs;
  51. uart = &uart_hs;
  52. serial->ops = &_uart_ops;
  53. serial->config = config;
  54. serial->config.baud_rate = 115200;
  55. uart->hw_base = UARTHS_BASE_ADDR;
  56. uart->irqno = IRQN_UARTHS_INTERRUPT;
  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. uint32_t freq = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU);
  94. uint16_t div = freq / cfg->baud_rate - 1;
  95. RT_ASSERT(serial != RT_NULL);
  96. serial->config = *cfg;
  97. uart = serial->parent.user_data;
  98. RT_ASSERT(uart != RT_NULL);
  99. if (uart->hw_base == UARTHS_BASE_ADDR)
  100. {
  101. _uarths->div.div = div;
  102. _uarths->txctrl.txen = 1;
  103. _uarths->rxctrl.rxen = 1;
  104. _uarths->txctrl.txcnt = 0;
  105. _uarths->rxctrl.rxcnt = 0;
  106. _uarths->ip.txwm = 1;
  107. _uarths->ip.rxwm = 1;
  108. _uarths->ie.txwm = 0;
  109. _uarths->ie.rxwm = 1;
  110. }
  111. else
  112. {
  113. /* other uart */
  114. }
  115. return (RT_EOK);
  116. }
  117. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  118. {
  119. struct device_uart *uart;
  120. uart = serial->parent.user_data;
  121. RT_ASSERT(uart != RT_NULL);
  122. switch (cmd)
  123. {
  124. case RT_DEVICE_CTRL_CLR_INT:
  125. /* Disable the UART Interrupt */
  126. rt_hw_interrupt_mask(uart->irqno);
  127. break;
  128. case RT_DEVICE_CTRL_SET_INT:
  129. /* install interrupt */
  130. rt_hw_interrupt_install(uart->irqno, uart_irq_handler,
  131. serial, serial->parent.parent.name);
  132. rt_hw_interrupt_umask(uart->irqno);
  133. break;
  134. }
  135. return (RT_EOK);
  136. }
  137. static int drv_uart_putc(struct rt_serial_device *serial, char c)
  138. {
  139. struct device_uart *uart;
  140. uart = serial->parent.user_data;
  141. if (uart->hw_base == UARTHS_BASE_ADDR)
  142. {
  143. while (_uarths->txdata.full);
  144. _uarths->txdata.data = (uint8_t)c;
  145. }
  146. else
  147. {
  148. /* other uart */
  149. }
  150. return (1);
  151. }
  152. static int drv_uart_getc(struct rt_serial_device *serial)
  153. {
  154. int ret = -1;
  155. struct device_uart *uart = serial->parent.user_data;
  156. if (uart->hw_base == UARTHS_BASE_ADDR)
  157. {
  158. uarths_rxdata_t recv = _uarths->rxdata;
  159. if (recv.empty)
  160. return EOF;
  161. else
  162. return (recv.data & 0xff);
  163. }
  164. /* Receive Data Available */
  165. return (-1);
  166. }
  167. /* UART ISR */
  168. static void uart_irq_handler(int irqno, void *param)
  169. {
  170. rt_ubase_t isr;
  171. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  172. struct device_uart *uart = serial->parent.user_data;
  173. /* read interrupt status and clear it */
  174. if (uart->hw_base == UARTHS_BASE_ADDR)
  175. {
  176. if (_uarths->ip.rxwm)
  177. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  178. }
  179. }
  180. /* WEAK for SDK 0.5.6 */
  181. RT_WEAK void uart_debug_init(int uart_channel)
  182. {
  183. }