drv_uart.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018/5/5 Bernard The first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "raspi.h"
  14. #include "board.h"
  15. #include "drv_uart.h"
  16. #define AUX_BASE (0x3F000000 + 0x215000)
  17. struct hw_uart_device
  18. {
  19. rt_ubase_t hw_base;
  20. rt_uint32_t irqno;
  21. };
  22. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  23. {
  24. struct hw_uart_device *uart;
  25. RT_ASSERT(serial != RT_NULL);
  26. uart = (struct hw_uart_device *)serial->parent.user_data;
  27. if (uart->hw_base == AUX_BASE)
  28. {
  29. rt_uint32_t value;
  30. /* GPIO function set */
  31. value = BCM283X_GPIO_GPFSEL(1);
  32. value &= ~(7 << 12); /* GPIO14 */
  33. value |= 2 << 12 ; /* ALT5 */
  34. value &= ~(7 << 15); /* GPIO15 */
  35. value |= 2 << 15 ; /* ALT5 */
  36. BCM283X_GPIO_GPFSEL(1) = value;
  37. BCM283X_GPIO_GPPUD = 0;
  38. BCM283X_GPIO_GPPUDCLK(0) = (1 << 14) | (1 << 15);
  39. BCM283X_GPIO_GPPUDCLK(0) = 0;
  40. AUX_ENABLES(uart->hw_base) = 1; /* Enable UART1 */
  41. AUX_MU_IER_REG(uart->hw_base) = 0; /* Disable interrupt */
  42. AUX_MU_CNTL_REG(uart->hw_base) = 0; /* Disable Transmitter and Receiver */
  43. AUX_MU_LCR_REG(uart->hw_base) = 3; /* Works in 8-bit mode */
  44. AUX_MU_MCR_REG(uart->hw_base) = 0; /* Disable RTS */
  45. AUX_MU_IIR_REG(uart->hw_base) = 0xC6; /* Enable FIFO, Clear FIFO */
  46. AUX_MU_BAUD_REG(uart->hw_base) = 270; /* 115200 = system clock 250MHz / (8 * (baud + 1)), baud = 270 */
  47. AUX_MU_CNTL_REG(uart->hw_base) = 3; /* Enable Transmitter and Receiver */
  48. }
  49. return RT_EOK;
  50. }
  51. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  52. {
  53. struct hw_uart_device *uart;
  54. RT_ASSERT(serial != RT_NULL);
  55. uart = (struct hw_uart_device *)serial->parent.user_data;
  56. switch (cmd)
  57. {
  58. case RT_DEVICE_CTRL_CLR_INT:
  59. /* disable rx irq */
  60. AUX_MU_IER_REG(uart->hw_base) = 0x0;
  61. rt_hw_interrupt_mask(uart->irqno);
  62. break;
  63. case RT_DEVICE_CTRL_SET_INT:
  64. /* enable rx irq */
  65. AUX_MU_IER_REG(uart->hw_base) = 0x1;
  66. rt_hw_interrupt_umask(uart->irqno);
  67. break;
  68. }
  69. return RT_EOK;
  70. }
  71. static int uart_putc(struct rt_serial_device *serial, char c)
  72. {
  73. struct hw_uart_device *uart;
  74. RT_ASSERT(serial != RT_NULL);
  75. uart = (struct hw_uart_device *)serial->parent.user_data;
  76. while (!(AUX_MU_LSR_REG(uart->hw_base) & 0x20));
  77. AUX_MU_IO_REG(uart->hw_base) = c;
  78. return 1;
  79. }
  80. static int uart_getc(struct rt_serial_device *serial)
  81. {
  82. int ch = -1;
  83. struct hw_uart_device *uart;
  84. RT_ASSERT(serial != RT_NULL);
  85. uart = (struct hw_uart_device *)serial->parent.user_data;
  86. if ((AUX_MU_LSR_REG(uart->hw_base) & 0x01))
  87. {
  88. ch = AUX_MU_IO_REG(uart->hw_base) & 0xff;
  89. }
  90. return ch;
  91. }
  92. static const struct rt_uart_ops _uart_ops =
  93. {
  94. uart_configure,
  95. uart_control,
  96. uart_putc,
  97. uart_getc,
  98. };
  99. static void rt_hw_uart_isr(int irqno, void *param)
  100. {
  101. struct rt_serial_device *serial = (struct rt_serial_device*)param;
  102. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  103. }
  104. #ifdef RT_USING_UART0
  105. /* UART device driver structure */
  106. static struct hw_uart_device _uart0_device =
  107. {
  108. RPI_UART0_BASE,
  109. IRQ_PBA8_UART0,
  110. };
  111. static struct rt_serial_device _serial0;
  112. #endif
  113. #ifdef RT_USING_UART1
  114. /* UART1 device driver structure */
  115. static struct hw_uart_device _uart1_device =
  116. {
  117. AUX_BASE,
  118. IRQ_AUX,
  119. };
  120. static struct rt_serial_device _serial1;
  121. #endif
  122. int rt_hw_uart_init(void)
  123. {
  124. struct hw_uart_device *uart;
  125. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  126. #ifdef RT_USING_UART0
  127. uart = &_uart0_device;
  128. _serial0.ops = &_uart_ops;
  129. _serial0.config = config;
  130. /* register UART1 device */
  131. rt_hw_serial_register(&_serial0, "uart0",
  132. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  133. uart);
  134. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial0, "uart0");
  135. #endif
  136. #ifdef RT_USING_UART1
  137. uart = &_uart1_device;
  138. _serial1.ops = &_uart_ops;
  139. _serial1.config = config;
  140. /* register UART1 device */
  141. rt_hw_serial_register(&_serial1, "uart1",
  142. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
  143. /* enable Rx and Tx of UART */
  144. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial1, "uart1");
  145. #endif
  146. return 0;
  147. }