drv_uart.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2006-2020, 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 <rtdevice.h>
  11. #include <rtthread.h>
  12. #include "board.h"
  13. #include "drv_uart.h"
  14. #include <stdio.h>
  15. #ifdef RT_USING_SMART
  16. #include <ioremap.h>
  17. #endif
  18. #include "sbi.h"
  19. struct device_uart
  20. {
  21. rt_ubase_t hw_base;
  22. rt_uint32_t irqno;
  23. };
  24. void *uart0_base = (void*)0x10000000;
  25. struct rt_serial_device serial0;
  26. struct device_uart uart0;
  27. void uart_init(void)
  28. {
  29. rt_uint32_t div = UART_REFERENCE_CLOCK / (UART_DEFAULT_BAUDRATE * 16);
  30. write8_uart0(UART_IER, 0x00);
  31. write8_uart0(UART_LCR, UART_LCR_BAUD_LATCH);
  32. // LSB
  33. write8_uart0(0, div & 0xff);
  34. // MSB
  35. write8_uart0(1, (div >> 8) & 0xff);
  36. // set word length to 8 bits, no parity
  37. write8_uart0(UART_LCR, UART_LCR_EIGHT_BITS);
  38. write8_uart0(UART_FCR, UART_FCR_FIFO_ENABLE | UART_FCR_FIFO_CLEAR);
  39. return;
  40. }
  41. static rt_err_t _uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  42. {
  43. uart_init();
  44. return (RT_EOK);
  45. }
  46. static rt_err_t _uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  47. {
  48. struct device_uart *uart = (struct device_uart*)serial->parent.user_data;
  49. switch (cmd)
  50. {
  51. case RT_DEVICE_CTRL_CLR_INT:
  52. if ((size_t)arg == RT_DEVICE_FLAG_INT_RX)
  53. {
  54. rt_uint8_t value = read8_uart0(UART_IER);
  55. write8_uart0(UART_IER, value & ~UART_IER_RX_ENABLE);
  56. }
  57. break;
  58. case RT_DEVICE_CTRL_SET_INT:
  59. if ((size_t)arg == RT_DEVICE_FLAG_INT_RX)
  60. {
  61. rt_uint8_t value = read8_uart0(UART_IER);
  62. write8_uart0(UART_IER, value | UART_IER_RX_ENABLE);
  63. }
  64. break;
  65. }
  66. return (RT_EOK);
  67. }
  68. static int _uart_putc(struct rt_serial_device *serial, char c)
  69. {
  70. struct device_uart *uart;
  71. uart = (struct device_uart*)serial->parent.user_data;
  72. // wait for Transmit Holding Empty to be set in LSR.
  73. while((read8_uart0(UART_LSR) & UART_LSR_TX_IDLE) == 0)
  74. ;
  75. write8_uart0(UART_THR, c);
  76. return (1);
  77. }
  78. static int _uart_getc(struct rt_serial_device *serial)
  79. {
  80. struct device_uart *uart;
  81. volatile rt_uint32_t lsr;
  82. int ch = -1;
  83. uart = (struct device_uart*)serial->parent.user_data;
  84. lsr = read8_uart0(UART_LSR);
  85. if (lsr & UART_LSR_RX_READY)
  86. {
  87. ch = read8_uart0(UART_RHR);
  88. }
  89. return ch;
  90. }
  91. const struct rt_uart_ops _uart_ops = {
  92. _uart_configure,
  93. _uart_control,
  94. _uart_putc,
  95. _uart_getc,
  96. // TODO: add DMA support
  97. RT_NULL};
  98. static void rt_hw_uart_isr(int irqno, void *param)
  99. {
  100. rt_ubase_t level = rt_hw_interrupt_disable();
  101. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  102. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  103. rt_hw_interrupt_enable(level);
  104. }
  105. /*
  106. * UART Initiation
  107. */
  108. int rt_hw_uart_init(void)
  109. {
  110. struct rt_serial_device *serial;
  111. struct device_uart *uart;
  112. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  113. #ifdef RT_USING_SMART
  114. uart0_base = rt_ioremap(uart0_base, 4096);
  115. #endif
  116. // register device
  117. serial = &serial0;
  118. uart = &uart0;
  119. serial->ops = &_uart_ops;
  120. serial->config = config;
  121. serial->config.baud_rate = UART_DEFAULT_BAUDRATE;
  122. uart->hw_base = (rt_ubase_t)uart0_base;
  123. uart->irqno = 0x0a;
  124. rt_hw_serial_register(serial,
  125. RT_CONSOLE_DEVICE_NAME,
  126. RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  127. uart);
  128. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, serial, RT_CONSOLE_DEVICE_NAME);
  129. rt_hw_interrupt_umask(uart->irqno);
  130. return 0;
  131. }
  132. /* WEAK for SDK 0.5.6 */
  133. rt_weak void uart_debug_init(int uart_channel)
  134. {
  135. }