drv_uart.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * 2020-04-16 bigmagic 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 "drv_gpio.h"
  16. struct hw_uart_device
  17. {
  18. rt_ubase_t hw_base;
  19. rt_uint32_t irqno;
  20. };
  21. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  22. {
  23. struct hw_uart_device *uart;
  24. uint32_t bauddiv = (UART_REFERENCE_CLOCK / cfg->baud_rate)* 1000 / 16;
  25. uint32_t ibrd = bauddiv / 1000;
  26. RT_ASSERT(serial != RT_NULL);
  27. uart = (struct hw_uart_device *)serial->parent.user_data;
  28. if(uart->hw_base == PL011_BASE)
  29. {
  30. uint32_t gpfsel = 0;
  31. gpfsel &= ~((uint32_t)(0x07 << (4 * 3)));
  32. gpfsel |= (uint32_t)(ALT0 << (4 * 3));
  33. GPIO_REG_GPFSEL1(GPIO_BASE) = gpfsel;
  34. gpfsel &= ~((uint32_t)(0x07 << (5 * 3)));
  35. gpfsel |= (uint32_t)(ALT0 << (5 * 3));
  36. GPIO_REG_GPFSEL1(GPIO_BASE) = gpfsel;
  37. PL011_REG_CR(uart->hw_base) = 0;/*Clear UART setting*/
  38. PL011_REG_LCRH(uart->hw_base) = 0;/*disable FIFO*/
  39. PL011_REG_IBRD(uart->hw_base) = ibrd;
  40. PL011_REG_FBRD(uart->hw_base) = (((bauddiv - ibrd * 1000) * 64 + 500) / 1000);
  41. PL011_REG_LCRH(uart->hw_base) = PL011_LCRH_WLEN_8;/*FIFO*/
  42. PL011_REG_CR(uart->hw_base) = PL011_CR_UARTEN | PL011_CR_TXE | PL011_CR_RXE;/*art enable, TX/RX enable*/
  43. }
  44. return RT_EOK;
  45. }
  46. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  47. {
  48. struct hw_uart_device *uart;
  49. RT_ASSERT(serial != RT_NULL);
  50. uart = (struct hw_uart_device *)serial->parent.user_data;
  51. switch (cmd)
  52. {
  53. case RT_DEVICE_CTRL_CLR_INT:
  54. /* disable rx irq */
  55. PL011_REG_IMSC(uart->hw_base) &= ~((uint32_t)PL011_IMSC_RXIM);
  56. rt_hw_interrupt_mask(uart->irqno);
  57. break;
  58. case RT_DEVICE_CTRL_SET_INT:
  59. /* enable rx irq */
  60. PL011_REG_IMSC(uart->hw_base) |= PL011_IMSC_RXIM;
  61. rt_hw_interrupt_umask(uart->irqno);
  62. break;
  63. }
  64. return RT_EOK;
  65. }
  66. static int uart_putc(struct rt_serial_device *serial, char c)
  67. {
  68. struct hw_uart_device *uart;
  69. RT_ASSERT(serial != RT_NULL);
  70. uart = (struct hw_uart_device *)serial->parent.user_data;
  71. while ((PL011_REG_FR(uart->hw_base) & PL011_FR_TXFF));
  72. PL011_REG_DR(uart->hw_base) = (uint8_t)c;
  73. return 1;
  74. }
  75. static int uart_getc(struct rt_serial_device *serial)
  76. {
  77. int ch = -1;
  78. struct hw_uart_device *uart;
  79. RT_ASSERT(serial != RT_NULL);
  80. uart = (struct hw_uart_device *)serial->parent.user_data;
  81. if((PL011_REG_FR(uart->hw_base) & PL011_FR_RXFE) == 0)
  82. {
  83. ch = PL011_REG_DR(uart->hw_base) & 0xff;
  84. }
  85. return ch;
  86. }
  87. static const struct rt_uart_ops _uart_ops =
  88. {
  89. uart_configure,
  90. uart_control,
  91. uart_putc,
  92. uart_getc,
  93. };
  94. static void rt_hw_uart_isr(int irqno, void *param)
  95. {
  96. struct rt_serial_device *serial = (struct rt_serial_device*)param;
  97. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  98. PL011_REG_ICR(UART0_BASE) = PL011_INTERRUPT_RECEIVE;
  99. }
  100. /* UART device driver structure */
  101. static struct hw_uart_device _uart0_device =
  102. {
  103. PL011_BASE,
  104. IRQ_PL011,
  105. };
  106. static struct rt_serial_device _serial0;
  107. int rt_hw_uart_init(void)
  108. {
  109. struct hw_uart_device *uart;
  110. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  111. uart = &_uart0_device;
  112. _serial0.ops = &_uart_ops;
  113. _serial0.config = config;
  114. /* register UART1 device */
  115. rt_hw_serial_register(&_serial0, "uart",
  116. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  117. uart);
  118. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial0, "uart");
  119. return 0;
  120. }