serial.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * 2013-03-30 Bernard the first verion
  9. */
  10. #include <rthw.h>
  11. #include <rtdevice.h>
  12. #include "serial.h"
  13. struct hw_uart_device
  14. {
  15. rt_uint32_t hw_base;
  16. rt_uint32_t irqno;
  17. };
  18. #define UART_DR(base) __REG32(base + 0x00)
  19. #define UART_FR(base) __REG32(base + 0x18)
  20. #define UART_CR(base) __REG32(base + 0x30)
  21. #define UART_IMSC(base) __REG32(base + 0x38)
  22. #define UART_ICR(base) __REG32(base + 0x44)
  23. #define UARTFR_RXFE 0x10
  24. #define UARTFR_TXFF 0x20
  25. #define UARTIMSC_RXIM 0x10
  26. #define UARTIMSC_TXIM 0x20
  27. #define UARTICR_RXIC 0x10
  28. #define UARTICR_TXIC 0x20
  29. static void rt_hw_uart_isr(int irqno, void *param)
  30. {
  31. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  32. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  33. }
  34. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  35. {
  36. return RT_EOK;
  37. }
  38. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  39. {
  40. struct hw_uart_device *uart;
  41. RT_ASSERT(serial != RT_NULL);
  42. uart = (struct hw_uart_device *)serial->parent.user_data;
  43. switch (cmd)
  44. {
  45. case RT_DEVICE_CTRL_CLR_INT:
  46. /* disable rx irq */
  47. UART_IMSC(uart->hw_base) &= ~UARTIMSC_RXIM;
  48. break;
  49. case RT_DEVICE_CTRL_SET_INT:
  50. /* enable rx irq */
  51. UART_IMSC(uart->hw_base) |= UARTIMSC_RXIM;
  52. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, serial, "uart");
  53. rt_hw_interrupt_umask(uart->irqno);
  54. break;
  55. }
  56. return RT_EOK;
  57. }
  58. static int uart_putc(struct rt_serial_device *serial, char c)
  59. {
  60. struct hw_uart_device *uart;
  61. RT_ASSERT(serial != RT_NULL);
  62. uart = (struct hw_uart_device *)serial->parent.user_data;
  63. while (UART_FR(uart->hw_base) & UARTFR_TXFF);
  64. UART_DR(uart->hw_base) = c;
  65. return 1;
  66. }
  67. static int uart_getc(struct rt_serial_device *serial)
  68. {
  69. int ch;
  70. struct hw_uart_device *uart;
  71. RT_ASSERT(serial != RT_NULL);
  72. uart = (struct hw_uart_device *)serial->parent.user_data;
  73. ch = -1;
  74. if (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
  75. {
  76. ch = UART_DR(uart->hw_base) & 0xff;
  77. }
  78. return ch;
  79. }
  80. static const struct rt_uart_ops _uart_ops =
  81. {
  82. uart_configure,
  83. uart_control,
  84. uart_putc,
  85. uart_getc,
  86. };
  87. #ifdef RT_USING_UART0
  88. /* UART device driver structure */
  89. static struct hw_uart_device _uart0_device =
  90. {
  91. REALVIEW_UART0_BASE,
  92. IRQ_PBA8_UART0,
  93. };
  94. static struct rt_serial_device _serial0;
  95. #endif
  96. #ifdef RT_USING_UART1
  97. /* UART1 device driver structure */
  98. static struct hw_uart_device _uart1_device =
  99. {
  100. REALVIEW_UART1_BASE,
  101. IRQ_PBA8_UART1,
  102. };
  103. static struct rt_serial_device _serial1;
  104. #endif
  105. int uart_isr_test(void)
  106. {
  107. return uart_getc(&_serial1);
  108. }
  109. int rt_hw_uart_init(void)
  110. {
  111. struct hw_uart_device *uart;
  112. struct serial_configure config;
  113. config.baud_rate = BAUD_RATE_115200;
  114. config.bit_order = BIT_ORDER_LSB;
  115. config.data_bits = DATA_BITS_8;
  116. config.parity = PARITY_NONE;
  117. config.stop_bits = STOP_BITS_1;
  118. config.invert = NRZ_NORMAL;
  119. config.bufsz = RT_SERIAL_RB_BUFSZ;
  120. #ifdef RT_USING_UART0
  121. uart = &_uart0_device;
  122. _serial0.ops = &_uart_ops;
  123. _serial0.config = config;
  124. /* register UART1 device */
  125. rt_hw_serial_register(&_serial0, "uart0",
  126. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  127. uart);
  128. /* enable Rx and Tx of UART */
  129. UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  130. #endif
  131. #ifdef RT_USING_UART1
  132. uart = &_uart1_device;
  133. _serial1.ops = &_uart_ops;
  134. _serial1.config = config;
  135. /* register UART1 device */
  136. rt_hw_serial_register(&_serial1, "uart1",
  137. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
  138. /* enable Rx and Tx of UART */
  139. UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  140. #endif
  141. return 0;
  142. }
  143. INIT_BOARD_EXPORT(rt_hw_uart_init);