serial.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #ifdef RT_USING_VMM
  33. #include <vmm.h>
  34. #endif
  35. struct hw_uart_device
  36. {
  37. rt_uint32_t hw_base;
  38. rt_uint32_t irqno;
  39. };
  40. #define UART_DR(base) __REG32(base + 0x00)
  41. #define UART_FR(base) __REG32(base + 0x18)
  42. #define UART_CR(base) __REG32(base + 0x30)
  43. #define UART_IMSC(base) __REG32(base + 0x38)
  44. #define UART_ICR(base) __REG32(base + 0x44)
  45. #define UARTFR_RXFE 0x10
  46. #define UARTFR_TXFF 0x20
  47. #define UARTIMSC_RXIM 0x10
  48. #define UARTIMSC_TXIM 0x20
  49. #define UARTICR_RXIC 0x10
  50. #define UARTICR_TXIC 0x20
  51. static void rt_hw_uart_isr(int irqno, void *param)
  52. {
  53. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  54. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  55. }
  56. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  57. {
  58. return RT_EOK;
  59. }
  60. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  61. {
  62. struct hw_uart_device *uart;
  63. RT_ASSERT(serial != RT_NULL);
  64. uart = (struct hw_uart_device *)serial->parent.user_data;
  65. switch (cmd)
  66. {
  67. case RT_DEVICE_CTRL_CLR_INT:
  68. /* disable rx irq */
  69. UART_IMSC(uart->hw_base) &= ~UARTIMSC_RXIM;
  70. break;
  71. case RT_DEVICE_CTRL_SET_INT:
  72. /* enable rx irq */
  73. UART_IMSC(uart->hw_base) |= UARTIMSC_RXIM;
  74. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, serial, "uart");
  75. rt_hw_interrupt_umask(uart->irqno);
  76. break;
  77. }
  78. return RT_EOK;
  79. }
  80. static int uart_putc(struct rt_serial_device *serial, char c)
  81. {
  82. struct hw_uart_device *uart;
  83. RT_ASSERT(serial != RT_NULL);
  84. uart = (struct hw_uart_device *)serial->parent.user_data;
  85. while (UART_FR(uart->hw_base) & UARTFR_TXFF);
  86. UART_DR(uart->hw_base) = c;
  87. return 1;
  88. }
  89. static int uart_getc(struct rt_serial_device *serial)
  90. {
  91. int ch;
  92. struct hw_uart_device *uart;
  93. RT_ASSERT(serial != RT_NULL);
  94. uart = (struct hw_uart_device *)serial->parent.user_data;
  95. ch = -1;
  96. if (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
  97. {
  98. ch = UART_DR(uart->hw_base) & 0xff;
  99. }
  100. return ch;
  101. }
  102. static const struct rt_uart_ops _uart_ops =
  103. {
  104. uart_configure,
  105. uart_control,
  106. uart_putc,
  107. uart_getc,
  108. };
  109. #ifdef RT_USING_UART0
  110. /* UART device driver structure */
  111. static struct hw_uart_device _uart0_device =
  112. {
  113. REALVIEW_UART0_BASE,
  114. IRQ_PBA8_UART0,
  115. };
  116. static struct rt_serial_device _serial0;
  117. #endif
  118. #ifdef RT_USING_UART1
  119. /* UART1 device driver structure */
  120. static struct hw_uart_device _uart1_device =
  121. {
  122. REALVIEW_UART1_BASE,
  123. IRQ_PBA8_UART1,
  124. };
  125. static struct rt_serial_device _serial1;
  126. #endif
  127. int uart_isr_test(void)
  128. {
  129. return uart_getc(&_serial1);
  130. }
  131. int rt_hw_uart_init(void)
  132. {
  133. struct hw_uart_device *uart;
  134. struct serial_configure config;
  135. config.baud_rate = BAUD_RATE_115200;
  136. config.bit_order = BIT_ORDER_LSB;
  137. config.data_bits = DATA_BITS_8;
  138. config.parity = PARITY_NONE;
  139. config.stop_bits = STOP_BITS_1;
  140. config.invert = NRZ_NORMAL;
  141. config.bufsz = RT_SERIAL_RB_BUFSZ;
  142. #ifdef RT_USING_UART0
  143. uart = &_uart0_device;
  144. #ifdef RT_USING_VMM
  145. uart->hw_base = vmm_find_iomap("UART0");
  146. #endif
  147. _serial0.ops = &_uart_ops;
  148. _serial0.config = config;
  149. /* register UART1 device */
  150. rt_hw_serial_register(&_serial0, "uart0",
  151. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  152. uart);
  153. /* enable Rx and Tx of UART */
  154. UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  155. #endif
  156. #ifdef RT_USING_UART1
  157. uart = &_uart1_device;
  158. #ifdef RT_USING_VMM
  159. uart->hw_base = vmm_find_iomap("UART1");
  160. #endif
  161. _serial1.ops = &_uart_ops;
  162. _serial1.config = config;
  163. /* register UART1 device */
  164. rt_hw_serial_register(&_serial1, "uart1",
  165. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
  166. /* enable Rx and Tx of UART */
  167. UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  168. #endif
  169. return 0;
  170. }
  171. INIT_BOARD_EXPORT(rt_hw_uart_init);