1
0

serial.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * serial.c UART driver
  3. *
  4. * COPYRIGHT (C) 2013, Shanghai Real-Thread Technology Co., Ltd
  5. *
  6. * This file is part of RT-Thread (http://www.rt-thread.org)
  7. * Maintainer: bernard.xiong <bernard.xiong at gmail.com>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. *
  25. * Change Logs:
  26. * Date Author Notes
  27. * 2013-03-30 Bernard the first verion
  28. */
  29. #include <rthw.h>
  30. #include <rtdevice.h>
  31. #include "serial.h"
  32. struct hw_uart_device
  33. {
  34. rt_uint32_t hw_base;
  35. rt_uint32_t irqno;
  36. };
  37. #define UART_DR(base) __REG32(base + 0x00)
  38. #define UART_FR(base) __REG32(base + 0x18)
  39. #define UART_CR(base) __REG32(base + 0x30)
  40. #define UART_IMSC(base) __REG32(base + 0x38)
  41. #define UART_ICR(base) __REG32(base + 0x44)
  42. #define UARTFR_RXFE 0x10
  43. #define UARTFR_TXFF 0x20
  44. #define UARTIMSC_RXIM 0x10
  45. #define UARTIMSC_TXIM 0x20
  46. #define UARTICR_RXIC 0x10
  47. #define UARTICR_TXIC 0x20
  48. static void rt_hw_uart_isr(int irqno, void *param)
  49. {
  50. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  51. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  52. }
  53. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  54. {
  55. return RT_EOK;
  56. }
  57. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  58. {
  59. struct hw_uart_device *uart;
  60. RT_ASSERT(serial != RT_NULL);
  61. uart = (struct hw_uart_device *)serial->parent.user_data;
  62. switch (cmd)
  63. {
  64. case RT_DEVICE_CTRL_CLR_INT:
  65. /* disable rx irq */
  66. UART_IMSC(uart->hw_base) &= ~UARTIMSC_RXIM;
  67. break;
  68. case RT_DEVICE_CTRL_SET_INT:
  69. /* enable rx irq */
  70. UART_IMSC(uart->hw_base) |= UARTIMSC_RXIM;
  71. rt_hw_interrupt_umask(uart->irqno);
  72. break;
  73. }
  74. return RT_EOK;
  75. }
  76. static int uart_putc(struct rt_serial_device *serial, char c)
  77. {
  78. struct hw_uart_device *uart;
  79. RT_ASSERT(serial != RT_NULL);
  80. uart = (struct hw_uart_device *)serial->parent.user_data;
  81. while (UART_FR(uart->hw_base) & UARTFR_TXFF);
  82. UART_DR(uart->hw_base) = c;
  83. return 1;
  84. }
  85. static int uart_getc(struct rt_serial_device *serial)
  86. {
  87. int ch;
  88. struct hw_uart_device *uart;
  89. RT_ASSERT(serial != RT_NULL);
  90. uart = (struct hw_uart_device *)serial->parent.user_data;
  91. ch = -1;
  92. if (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
  93. {
  94. ch = UART_DR(uart->hw_base) & 0xff;
  95. }
  96. return ch;
  97. }
  98. static const struct rt_uart_ops _uart_ops =
  99. {
  100. uart_configure,
  101. uart_control,
  102. uart_putc,
  103. uart_getc,
  104. };
  105. #ifdef RT_USING_UART0
  106. /* UART device driver structure */
  107. static struct hw_uart_device _uart0_device =
  108. {
  109. REALVIEW_UART0_BASE,
  110. IRQ_PBA8_UART0,
  111. };
  112. static struct rt_serial_device _serial0;
  113. #endif
  114. #ifdef RT_USING_UART1
  115. /* UART1 device driver structure */
  116. static struct hw_uart_device _uart1_device =
  117. {
  118. REALVIEW_UART1_BASE,
  119. IRQ_PBA8_UART1,
  120. };
  121. static struct rt_serial_device _serial1;
  122. #endif
  123. int rt_hw_uart_init(void)
  124. {
  125. struct hw_uart_device *uart;
  126. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  127. #ifdef RT_USING_UART0
  128. uart = &_uart0_device;
  129. _serial0.ops = &_uart_ops;
  130. _serial0.config = config;
  131. /* register UART1 device */
  132. rt_hw_serial_register(&_serial0, "uart0",
  133. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  134. uart);
  135. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial0, "uart0");
  136. /* enable Rx and Tx of UART */
  137. UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  138. #endif
  139. #ifdef RT_USING_UART1
  140. uart = &_uart1_device;
  141. _serial1.ops = &_uart_ops;
  142. _serial1.config = config;
  143. /* register UART1 device */
  144. rt_hw_serial_register(&_serial1, "uart1",
  145. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
  146. /* enable Rx and Tx of UART */
  147. UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  148. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial1, "uart1");
  149. #endif
  150. return 0;
  151. }
  152. INIT_BOARD_EXPORT(rt_hw_uart_init);