drv_uart.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "board.h"
  14. #include "drv_uart.h"
  15. #include <rtdevice.h>
  16. #define AUX_BASE (0x3F000000 + 0x215000)
  17. struct hw_uart_device
  18. {
  19. rt_uint32_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. uint32_t value;
  30. /* GPIO function set */
  31. value = GPIO_GPFSEL1;
  32. value &= ~(7<<12); /* GPIO14 */
  33. value |= 2<<12 ; /* ALT5 */
  34. value &= ~(7<<15); /* GPIO15 */
  35. value |= 2<<15 ; /* ALT5 */
  36. GPIO_GPFSEL1 = value;
  37. /* PullUD disable */
  38. GPIO_GPPUD = 0;
  39. GPIO_GPPUDCLK0 = (1 << 14) | (1 << 15);
  40. GPIO_GPPUDCLK0 = 0;
  41. AUX_ENABLES(uart->hw_base) = 1; /* Enable UART1 */
  42. AUX_MU_IER_REG(uart->hw_base) = 0; /* Disable interrupt */
  43. AUX_MU_CNTL_REG(uart->hw_base) = 0; /* Disable Transmitter and Receiver */
  44. AUX_MU_LCR_REG(uart->hw_base) = 3; /* Works in 8-bit mode */
  45. AUX_MU_MCR_REG(uart->hw_base) = 0; /* Disable RTS */
  46. AUX_MU_IIR_REG(uart->hw_base) = 0xC6; /* Enable FIFO, Clear FIFO */
  47. AUX_MU_BAUD_REG(uart->hw_base) = 270; /* 115200 = system clock 250MHz / (8 * (baud + 1)), baud = 270 */
  48. AUX_MU_CNTL_REG(uart->hw_base) = 3; /* Enable Transmitter and Receiver */
  49. }
  50. return RT_EOK;
  51. }
  52. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  53. {
  54. struct hw_uart_device *uart;
  55. RT_ASSERT(serial != RT_NULL);
  56. uart = (struct hw_uart_device *)serial->parent.user_data;
  57. switch (cmd)
  58. {
  59. case RT_DEVICE_CTRL_CLR_INT:
  60. /* disable rx irq */
  61. AUX_MU_IER_REG(uart->hw_base) = 0x0;
  62. rt_hw_interrupt_mask(uart->irqno);
  63. break;
  64. case RT_DEVICE_CTRL_SET_INT:
  65. /* enable rx irq */
  66. AUX_MU_IER_REG(uart->hw_base) = 0x1;
  67. rt_hw_interrupt_umask(uart->irqno);
  68. break;
  69. }
  70. return RT_EOK;
  71. }
  72. static int uart_putc(struct rt_serial_device *serial, char c)
  73. {
  74. struct hw_uart_device *uart;
  75. RT_ASSERT(serial != RT_NULL);
  76. uart = (struct hw_uart_device *)serial->parent.user_data;
  77. while (!(AUX_MU_LSR_REG(uart->hw_base) & 0x20));
  78. AUX_MU_IO_REG(uart->hw_base) = c;
  79. return 1;
  80. }
  81. static int uart_getc(struct rt_serial_device *serial)
  82. {
  83. int ch = -1;
  84. struct hw_uart_device *uart;
  85. RT_ASSERT(serial != RT_NULL);
  86. uart = (struct hw_uart_device *)serial->parent.user_data;
  87. if ((AUX_MU_LSR_REG(uart->hw_base) & 0x01))
  88. {
  89. ch = AUX_MU_IO_REG(uart->hw_base) & 0xff;
  90. }
  91. return ch;
  92. }
  93. static const struct rt_uart_ops _uart_ops =
  94. {
  95. uart_configure,
  96. uart_control,
  97. uart_putc,
  98. uart_getc,
  99. };
  100. static void rt_hw_uart_isr(int irqno, void *param)
  101. {
  102. struct rt_serial_device *serial = (struct rt_serial_device*)param;
  103. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  104. }
  105. #ifdef RT_USING_UART0
  106. /* UART device driver structure */
  107. static struct hw_uart_device _uart0_device =
  108. {
  109. RPI_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. AUX_BASE,
  119. IRQ_AUX,
  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. #endif
  137. #ifdef RT_USING_UART1
  138. uart = &_uart1_device;
  139. _serial1.ops = &_uart_ops;
  140. _serial1.config = config;
  141. /* register UART1 device */
  142. rt_hw_serial_register(&_serial1, "uart1",
  143. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
  144. /* enable Rx and Tx of UART */
  145. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial1, "uart1");
  146. #endif
  147. return 0;
  148. }