drv_uart.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * File : drv_uart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018/5/5 Bernard The first version
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. #include "board.h"
  28. #include "drv_uart.h"
  29. #include <rtdevice.h>
  30. #define AUX_BASE (0x3F000000 + 0x215000)
  31. struct hw_uart_device
  32. {
  33. rt_uint32_t hw_base;
  34. rt_uint32_t irqno;
  35. };
  36. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  37. {
  38. struct hw_uart_device *uart;
  39. RT_ASSERT(serial != RT_NULL);
  40. uart = (struct hw_uart_device *)serial->parent.user_data;
  41. if (uart->hw_base == AUX_BASE)
  42. {
  43. uint32_t value;
  44. /* GPIO function set */
  45. value = GPIO_GPFSEL1;
  46. value &= ~(7<<12); /* GPIO14 */
  47. value |= 2<<12 ; /* ALT5 */
  48. value &= ~(7<<15); /* GPIO15 */
  49. value |= 2<<15 ; /* ALT5 */
  50. GPIO_GPFSEL1 = value;
  51. /* PullUD disable */
  52. GPIO_GPPUD = 0;
  53. GPIO_GPPUDCLK0 = (1 << 14) | (1 << 15);
  54. GPIO_GPPUDCLK0 = 0;
  55. AUX_ENABLES(uart->hw_base) = 1; /* Enable UART1 */
  56. AUX_MU_IER_REG(uart->hw_base) = 0; /* Disable interrupt */
  57. AUX_MU_CNTL_REG(uart->hw_base) = 0; /* Disable Transmitter and Receiver */
  58. AUX_MU_LCR_REG(uart->hw_base) = 3; /* Works in 8-bit mode */
  59. AUX_MU_MCR_REG(uart->hw_base) = 0; /* Disable RTS */
  60. AUX_MU_IIR_REG(uart->hw_base) = 0xC6; /* Enable FIFO, Clear FIFO */
  61. AUX_MU_BAUD_REG(uart->hw_base) = 270; /* 115200 = system clock 250MHz / (8 * (baud + 1)), baud = 270 */
  62. AUX_MU_CNTL_REG(uart->hw_base) = 3; /* Enable Transmitter and Receiver */
  63. }
  64. return RT_EOK;
  65. }
  66. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  67. {
  68. struct hw_uart_device *uart;
  69. RT_ASSERT(serial != RT_NULL);
  70. uart = (struct hw_uart_device *)serial->parent.user_data;
  71. switch (cmd)
  72. {
  73. case RT_DEVICE_CTRL_CLR_INT:
  74. /* disable rx irq */
  75. AUX_MU_IER_REG(uart->hw_base) = 0x0;
  76. rt_hw_interrupt_mask(uart->irqno);
  77. break;
  78. case RT_DEVICE_CTRL_SET_INT:
  79. /* enable rx irq */
  80. AUX_MU_IER_REG(uart->hw_base) = 0x1;
  81. rt_hw_interrupt_umask(uart->irqno);
  82. break;
  83. }
  84. return RT_EOK;
  85. }
  86. static int uart_putc(struct rt_serial_device *serial, char c)
  87. {
  88. struct hw_uart_device *uart;
  89. RT_ASSERT(serial != RT_NULL);
  90. uart = (struct hw_uart_device *)serial->parent.user_data;
  91. while (!(AUX_MU_LSR_REG(uart->hw_base) & 0x20));
  92. AUX_MU_IO_REG(uart->hw_base) = c;
  93. return 1;
  94. }
  95. static int uart_getc(struct rt_serial_device *serial)
  96. {
  97. int ch = -1;
  98. struct hw_uart_device *uart;
  99. RT_ASSERT(serial != RT_NULL);
  100. uart = (struct hw_uart_device *)serial->parent.user_data;
  101. if ((AUX_MU_LSR_REG(uart->hw_base) & 0x01))
  102. {
  103. ch = AUX_MU_IO_REG(uart->hw_base) & 0xff;
  104. }
  105. return ch;
  106. }
  107. static const struct rt_uart_ops _uart_ops =
  108. {
  109. uart_configure,
  110. uart_control,
  111. uart_putc,
  112. uart_getc,
  113. };
  114. static void rt_hw_uart_isr(int irqno, void *param)
  115. {
  116. struct rt_serial_device *serial = (struct rt_serial_device*)param;
  117. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  118. }
  119. #ifdef RT_USING_UART0
  120. /* UART device driver structure */
  121. static struct hw_uart_device _uart0_device =
  122. {
  123. RPI_UART0_BASE,
  124. IRQ_PBA8_UART0,
  125. };
  126. static struct rt_serial_device _serial0;
  127. #endif
  128. #ifdef RT_USING_UART1
  129. /* UART1 device driver structure */
  130. static struct hw_uart_device _uart1_device =
  131. {
  132. AUX_BASE,
  133. IRQ_AUX,
  134. };
  135. static struct rt_serial_device _serial1;
  136. #endif
  137. int rt_hw_uart_init(void)
  138. {
  139. struct hw_uart_device *uart;
  140. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  141. #ifdef RT_USING_UART0
  142. uart = &_uart0_device;
  143. _serial0.ops = &_uart_ops;
  144. _serial0.config = config;
  145. /* register UART1 device */
  146. rt_hw_serial_register(&_serial0, "uart0",
  147. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  148. uart);
  149. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial0, "uart0");
  150. #endif
  151. #ifdef RT_USING_UART1
  152. uart = &_uart1_device;
  153. _serial1.ops = &_uart_ops;
  154. _serial1.config = config;
  155. /* register UART1 device */
  156. rt_hw_serial_register(&_serial1, "uart1",
  157. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
  158. /* enable Rx and Tx of UART */
  159. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial1, "uart1");
  160. #endif
  161. return 0;
  162. }