drv_uart.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. return (RT_EOK);
  44. }
  45. static rt_err_t _uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  46. {
  47. struct device_uart *uart = (struct device_uart*)serial->parent.user_data;
  48. switch (cmd)
  49. {
  50. case RT_DEVICE_CTRL_CLR_INT:
  51. if ((size_t)arg == RT_DEVICE_FLAG_INT_RX)
  52. {
  53. rt_uint8_t value = read8_uart0(UART_IER);
  54. write8_uart0(UART_IER, value & ~UART_IER_RX_ENABLE);
  55. }
  56. break;
  57. case RT_DEVICE_CTRL_SET_INT:
  58. if ((size_t)arg == RT_DEVICE_FLAG_INT_RX)
  59. {
  60. rt_uint8_t value = read8_uart0(UART_IER);
  61. write8_uart0(UART_IER, value | UART_IER_RX_ENABLE);
  62. }
  63. break;
  64. }
  65. return (RT_EOK);
  66. }
  67. static int _uart_putc(struct rt_serial_device *serial, char c)
  68. {
  69. struct device_uart *uart;
  70. uart = (struct device_uart*)serial->parent.user_data;
  71. // wait for Transmit Holding Empty to be set in LSR.
  72. while((read8_uart0(UART_LSR) & UART_LSR_TX_IDLE) == 0)
  73. ;
  74. write8_uart0(UART_THR, c);
  75. return (1);
  76. }
  77. static int _uart_getc(struct rt_serial_device *serial)
  78. {
  79. struct device_uart *uart;
  80. volatile rt_uint32_t lsr;
  81. int ch = -1;
  82. uart = (struct device_uart*)serial->parent.user_data;
  83. lsr = read8_uart0(UART_LSR);
  84. if (lsr & UART_LSR_RX_READY)
  85. {
  86. ch = read8_uart0(UART_RHR);
  87. }
  88. return ch;
  89. }
  90. const struct rt_uart_ops _uart_ops = {
  91. _uart_configure,
  92. _uart_control,
  93. _uart_putc,
  94. _uart_getc,
  95. // TODO: add DMA support
  96. RT_NULL};
  97. static void rt_hw_uart_isr(int irqno, void *param)
  98. {
  99. rt_ubase_t level = rt_hw_interrupt_disable();
  100. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  101. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  102. rt_hw_interrupt_enable(level);
  103. }
  104. /*
  105. * UART Initiation
  106. */
  107. int rt_hw_uart_init(void)
  108. {
  109. struct rt_serial_device *serial;
  110. struct device_uart *uart;
  111. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  112. #ifdef RT_USING_SMART
  113. uart0_base = rt_ioremap(uart0_base, 4096);
  114. #endif
  115. // register device
  116. serial = &serial0;
  117. uart = &uart0;
  118. serial->ops = &_uart_ops;
  119. serial->config = config;
  120. serial->config.baud_rate = UART_DEFAULT_BAUDRATE;
  121. uart->hw_base = (rt_ubase_t)uart0_base;
  122. uart->irqno = 0x0a;
  123. rt_hw_serial_register(serial,
  124. RT_CONSOLE_DEVICE_NAME,
  125. RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  126. uart);
  127. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, serial, RT_CONSOLE_DEVICE_NAME);
  128. rt_hw_interrupt_umask(uart->irqno);
  129. return 0;
  130. }
  131. /* WEAK for SDK 0.5.6 */
  132. rt_weak void uart_debug_init(int uart_channel)
  133. {
  134. }