serial.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_install(uart->irqno, rt_hw_uart_isr, serial, "uart");
  72. rt_hw_interrupt_umask(uart->irqno);
  73. break;
  74. }
  75. return RT_EOK;
  76. }
  77. static int uart_putc(struct rt_serial_device *serial, char c)
  78. {
  79. struct hw_uart_device *uart;
  80. RT_ASSERT(serial != RT_NULL);
  81. uart = (struct hw_uart_device *)serial->parent.user_data;
  82. while (UART_FR(uart->hw_base) & UARTFR_TXFF);
  83. UART_DR(uart->hw_base) = c;
  84. return 1;
  85. }
  86. static int uart_getc(struct rt_serial_device *serial)
  87. {
  88. int ch;
  89. struct hw_uart_device *uart;
  90. RT_ASSERT(serial != RT_NULL);
  91. uart = (struct hw_uart_device *)serial->parent.user_data;
  92. ch = -1;
  93. if (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
  94. {
  95. ch = UART_DR(uart->hw_base) & 0xff;
  96. }
  97. return ch;
  98. }
  99. static const struct rt_uart_ops _uart_ops =
  100. {
  101. uart_configure,
  102. uart_control,
  103. uart_putc,
  104. uart_getc,
  105. };
  106. #ifdef RT_USING_UART0
  107. /* UART device driver structure */
  108. static struct hw_uart_device _uart0_device =
  109. {
  110. REALVIEW_UART0_BASE,
  111. IRQ_PBA8_UART0,
  112. };
  113. static struct rt_serial_device _serial0;
  114. #endif
  115. #ifdef RT_USING_UART1
  116. /* UART1 device driver structure */
  117. static struct hw_uart_device _uart1_device =
  118. {
  119. REALVIEW_UART1_BASE,
  120. IRQ_PBA8_UART1,
  121. };
  122. static struct rt_serial_device _serial1;
  123. #endif
  124. int uart_isr_test(void)
  125. {
  126. return uart_getc(&_serial1);
  127. }
  128. int rt_hw_uart_init(void)
  129. {
  130. struct hw_uart_device *uart;
  131. struct serial_configure config;
  132. config.baud_rate = BAUD_RATE_115200;
  133. config.bit_order = BIT_ORDER_LSB;
  134. config.data_bits = DATA_BITS_8;
  135. config.parity = PARITY_NONE;
  136. config.stop_bits = STOP_BITS_1;
  137. config.invert = NRZ_NORMAL;
  138. config.bufsz = RT_SERIAL_RB_BUFSZ;
  139. #ifdef RT_USING_UART0
  140. uart = &_uart0_device;
  141. _serial0.ops = &_uart_ops;
  142. _serial0.config = config;
  143. /* register UART1 device */
  144. rt_hw_serial_register(&_serial0, "uart0",
  145. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  146. uart);
  147. /* enable Rx and Tx of UART */
  148. UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  149. #endif
  150. #ifdef RT_USING_UART1
  151. uart = &_uart1_device;
  152. _serial1.ops = &_uart_ops;
  153. _serial1.config = config;
  154. /* register UART1 device */
  155. rt_hw_serial_register(&_serial1, "uart1",
  156. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
  157. /* enable Rx and Tx of UART */
  158. UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  159. #endif
  160. return 0;
  161. }
  162. INIT_BOARD_EXPORT(rt_hw_uart_init);