drv_uart.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-04-05 bigmagic Initial version
  9. */
  10. /**
  11. * @addtogroup ls2k
  12. */
  13. /*@{*/
  14. #include <rtthread.h>
  15. #include <rtdevice.h>
  16. #include <rthw.h>
  17. #include "drv_uart.h"
  18. #define TRUE 1
  19. #define FALSE 0
  20. struct rt_uart_ls2k
  21. {
  22. void *base;
  23. rt_uint32_t IRQ;
  24. };
  25. static rt_err_t ls2k_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  26. {
  27. struct rt_uart_ls2k *uart_dev = RT_NULL;
  28. RT_ASSERT(serial != RT_NULL);
  29. RT_ASSERT(cfg != RT_NULL);
  30. uart_dev = (struct rt_uart_ls2k *)serial->parent.user_data;
  31. UART_IER(uart_dev->base) = 0; /* clear interrupt */
  32. UART_FCR(uart_dev->base) = 0xc1; /* reset UART Rx/Tx */
  33. /* set databits, stopbits and parity. (8-bit data, 1 stopbit, no parity) */
  34. UART_LCR(uart_dev->base) = 0x3;
  35. UART_MCR(uart_dev->base) = 0x3;
  36. UART_LSR(uart_dev->base) = 0x60;
  37. UART_MSR(uart_dev->base) = 0xb0;
  38. return RT_EOK;
  39. }
  40. static rt_err_t ls2k_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  41. {
  42. struct rt_uart_ls2k *uart_dev = RT_NULL;
  43. RT_ASSERT(serial != RT_NULL);
  44. uart_dev = (struct rt_uart_ls2k *)serial->parent.user_data;
  45. switch (cmd)
  46. {
  47. case RT_DEVICE_CTRL_CLR_INT: /* Disable RX IRQ */
  48. rt_hw_interrupt_mask(uart_dev->IRQ);
  49. break;
  50. case RT_DEVICE_CTRL_SET_INT: /* Enable RX IRQ */
  51. rt_hw_interrupt_umask(uart_dev->IRQ);
  52. UART_IER(uart_dev->base) |= (IER_IRxE|IER_ILE);
  53. break;
  54. default:
  55. break;
  56. }
  57. return RT_EOK;
  58. }
  59. static rt_bool_t uart_is_transmit_empty(struct rt_uart_ls2k *uart_dev)
  60. {
  61. unsigned char status = UART_LSR(uart_dev->base);
  62. if (status & (UARTLSR_TE | UARTLSR_TFE))
  63. {
  64. return TRUE;
  65. }
  66. else
  67. {
  68. return FALSE;
  69. }
  70. }
  71. static int ls2k_uart_putc(struct rt_serial_device *serial, char c)
  72. {
  73. struct rt_uart_ls2k *uart_dev = RT_NULL;
  74. RT_ASSERT(serial != RT_NULL);
  75. uart_dev = (struct rt_uart_ls2k *)serial->parent.user_data;
  76. while (FALSE == uart_is_transmit_empty(uart_dev))
  77. ;
  78. UART_DAT(uart_dev->base) = c;
  79. return 1;
  80. }
  81. static int ls2k_uart_getc(struct rt_serial_device *serial)
  82. {
  83. struct rt_uart_ls2k *uart_dev = RT_NULL;
  84. RT_ASSERT(serial != RT_NULL);
  85. uart_dev = (struct rt_uart_ls2k *)serial->parent.user_data;
  86. if (LSR_RXRDY & UART_LSR(uart_dev->base))
  87. {
  88. return UART_DAT(uart_dev->base);
  89. }
  90. return -1;
  91. }
  92. /* UART interrupt handler */
  93. static void uart_irq_handler(int vector, void *param)
  94. {
  95. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  96. struct rt_uart_ls2k *uart_dev = RT_NULL;
  97. RT_ASSERT(serial != RT_NULL);
  98. uart_dev = (struct rt_uart_ls2k *)serial->parent.user_data;
  99. unsigned char iir = UART_IIR(uart_dev->base);
  100. /* Find out interrupt reason */
  101. if ((IIR_RXTOUT & iir) || (IIR_RXRDY & iir))
  102. {
  103. rt_interrupt_enter();
  104. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  105. rt_interrupt_leave();
  106. }
  107. }
  108. static const struct rt_uart_ops ls2k_uart_ops =
  109. {
  110. ls2k_uart_configure,
  111. ls2k_uart_control,
  112. ls2k_uart_putc,
  113. ls2k_uart_getc,
  114. };
  115. struct rt_uart_ls2k uart_dev0 =
  116. {
  117. (void *)UARTx_BASE(0),
  118. LS2K_UART_0_1_2_3_IRQ,
  119. };
  120. struct rt_serial_device serial;
  121. void rt_hw_uart_init(void)
  122. {
  123. struct rt_uart_ls2k *uart;
  124. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  125. uart = &uart_dev0;
  126. serial.ops = &ls2k_uart_ops;
  127. serial.config = config;
  128. rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial, "UART");
  129. /* register UART device */
  130. rt_hw_serial_register(&serial,
  131. "uart",
  132. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  133. uart);
  134. }
  135. /*@}*/