drv_uart.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2019-2020, Xim
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. */
  7. #include <rthw.h>
  8. #include <rtdevice.h>
  9. #include "board.h"
  10. #include "drv_uart.h"
  11. #include <stdio.h>
  12. #include "sbi.h"
  13. #define UART_DEFAULT_BAUDRATE 115200
  14. struct device_uart
  15. {
  16. rt_ubase_t hw_base;
  17. rt_uint32_t irqno;
  18. };
  19. static rt_err_t rt_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  20. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg);
  21. static int drv_uart_putc(struct rt_serial_device *serial, char c);
  22. static int drv_uart_getc(struct rt_serial_device *serial);
  23. const struct rt_uart_ops _uart_ops =
  24. {
  25. rt_uart_configure,
  26. uart_control,
  27. drv_uart_putc,
  28. drv_uart_getc,
  29. //TODO: add DMA support
  30. RT_NULL
  31. };
  32. void uart_init(void)
  33. {
  34. return ;
  35. }
  36. struct rt_serial_device serial1;
  37. struct device_uart uart1;
  38. /*
  39. * UART interface
  40. */
  41. static rt_err_t rt_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  42. {
  43. struct device_uart *uart;
  44. RT_ASSERT(serial != RT_NULL);
  45. serial->config = *cfg;
  46. return (RT_EOK);
  47. }
  48. #define UART_LSR_DR 0x01 /* Data ready */
  49. #define UART_LSR_THRE 0x20 /* Xmit holding register empty */
  50. #define UART_RBR(hw) HWREG32(hw + 0x00)
  51. #define UART_IER(hw) HWREG32(hw + 0x04)
  52. #define UART_LSR(hw) HWREG32(hw + 0x14)
  53. static volatile uint64_t uart_hwbase = 0x10000000;
  54. void uart_putc(char c)
  55. {
  56. while ((UART_LSR(uart_hwbase) & UART_LSR_THRE) == 0);
  57. UART_RBR(uart_hwbase) = c;
  58. }
  59. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  60. {
  61. struct device_uart *uart;
  62. uart = serial->parent.user_data;
  63. rt_uint32_t channel = 1;
  64. RT_ASSERT(uart != RT_NULL);
  65. RT_ASSERT(channel != 3);
  66. switch (cmd)
  67. {
  68. case RT_DEVICE_CTRL_CLR_INT:
  69. break;
  70. case RT_DEVICE_CTRL_SET_INT:
  71. break;
  72. }
  73. return (RT_EOK);
  74. }
  75. static int drv_uart_putc(struct rt_serial_device *serial, char c)
  76. {
  77. sbi_console_putchar(c);
  78. return (1);
  79. }
  80. static int drv_uart_getc(struct rt_serial_device *serial)
  81. {
  82. return sbi_console_getchar();
  83. }
  84. char rt_hw_console_getchar(void)
  85. {
  86. return sbi_console_getchar();
  87. }
  88. static void uart_rx(void *param)
  89. {
  90. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  91. while(1)
  92. {
  93. rt_hw_serial_isr((struct rt_serial_device *)serial,RT_SERIAL_EVENT_RX_IND);
  94. rt_thread_mdelay(10);
  95. }
  96. }
  97. void rt_hw_uart_start_rx_thread()
  98. {
  99. rt_thread_t th;
  100. RT_ASSERT((th = rt_thread_create("uartrx",uart_rx,(void *)&serial1,8192,8,20)) != RT_NULL);
  101. RT_ASSERT(rt_thread_startup(th) == RT_EOK);
  102. }
  103. /*
  104. * UART Initiation
  105. */
  106. int rt_hw_uart_init(void)
  107. {
  108. struct rt_serial_device *serial;
  109. struct device_uart *uart;
  110. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  111. {
  112. serial = &serial1;
  113. uart = &uart1;
  114. serial->ops = &_uart_ops;
  115. serial->config = config;
  116. serial->config.baud_rate = UART_DEFAULT_BAUDRATE;
  117. uart->hw_base = 0x10000000;
  118. uart->irqno = 0xa;
  119. rt_hw_serial_register(serial,
  120. "uart",
  121. RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  122. uart);
  123. }
  124. return 0;
  125. }
  126. /* WEAK for SDK 0.5.6 */
  127. RT_WEAK void uart_debug_init(int uart_channel)
  128. {
  129. }