drv_uart.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * File : drv_uart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-05-08 zhuangwei the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include <rthw.h>
  27. #include "drv_uart.h"
  28. #include "ls1c_pin.h"
  29. #include "ls1c_uart.h"
  30. /* STM32 uart driver */
  31. struct rt_uart_ls1c
  32. {
  33. ls1c_uart_t UARTx;
  34. rt_uint32_t IRQ;
  35. };
  36. static rt_err_t ls1c_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  37. {
  38. struct rt_uart_ls1c *uart_dev = RT_NULL;
  39. ls1c_uart_info_t uart_info = {0};
  40. RT_ASSERT(serial != RT_NULL);
  41. RT_ASSERT(cfg != RT_NULL);
  42. uart_dev = (struct rt_uart_ls1c *)serial->parent.user_data;
  43. // 初始化串口
  44. uart_info.UARTx = uart_dev->UARTx;
  45. uart_info.baudrate = cfg->baud_rate;
  46. uart_info.rx_enable = TRUE;
  47. uart_init(&uart_info);
  48. return RT_EOK;
  49. }
  50. static rt_err_t ls1c_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  51. {
  52. struct rt_uart_ls1c *uart_dev = RT_NULL;
  53. RT_ASSERT(serial != RT_NULL);
  54. uart_dev = (struct rt_uart_ls1c *)serial->parent.user_data;
  55. switch (cmd)
  56. {
  57. case RT_DEVICE_CTRL_CLR_INT: /* disable rx irq */
  58. rt_hw_interrupt_mask(uart_dev->IRQ);
  59. break;
  60. case RT_DEVICE_CTRL_SET_INT: /* enable rx irq */
  61. rt_hw_interrupt_umask(uart_dev->IRQ);
  62. break;
  63. default:
  64. break;
  65. }
  66. return RT_EOK;
  67. }
  68. static int ls1c_uart_putc(struct rt_serial_device *serial, char c)
  69. {
  70. struct rt_uart_ls1c *uart_dev = RT_NULL;
  71. RT_ASSERT(serial != RT_NULL);
  72. uart_dev = (struct rt_uart_ls1c *)serial->parent.user_data;
  73. uart_putc(uart_dev->UARTx, c);
  74. return 1;
  75. }
  76. static int ls1c_uart_getc(struct rt_serial_device *serial)
  77. {
  78. struct rt_uart_ls1c *uart_dev = RT_NULL;
  79. RT_ASSERT(serial != RT_NULL);
  80. uart_dev = (struct rt_uart_ls1c *)serial->parent.user_data;
  81. void *uart_base = uart_get_base(uart_dev->UARTx);
  82. if (LSR_RXRDY & reg_read_8(uart_base + LS1C_UART_LSR_OFFSET))
  83. {
  84. return reg_read_8(uart_base + LS1C_UART_DAT_OFFSET);
  85. }
  86. return -1;
  87. }
  88. /* UART interrupt handler */
  89. static void uart_irq_handler(int vector, void *param)
  90. {
  91. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  92. struct rt_uart_ls1c *uart_dev = RT_NULL;
  93. RT_ASSERT(serial != RT_NULL);
  94. uart_dev = (struct rt_uart_ls1c *)serial->parent.user_data;
  95. void *uart_base = uart_get_base(uart_dev->UARTx);
  96. unsigned char iir = reg_read_8(uart_base + LS1C_UART_IIR_OFFSET);
  97. // 判断是否为接收超时或接收到有效数据
  98. if ((IIR_RXTOUT & iir) || (IIR_RXRDY & iir))
  99. {
  100. rt_interrupt_enter();
  101. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  102. rt_interrupt_leave();
  103. }
  104. }
  105. static const struct rt_uart_ops stm32_uart_ops =
  106. {
  107. ls1c_uart_configure,
  108. ls1c_uart_control,
  109. ls1c_uart_putc,
  110. ls1c_uart_getc,
  111. };
  112. #if defined(RT_USING_UART2)
  113. struct rt_uart_ls1c uart2 =
  114. {
  115. LS1C_UART2,
  116. LS1C_UART2_IRQ,
  117. };
  118. struct rt_serial_device serial2;
  119. #endif /* RT_USING_UART1 */
  120. void rt_hw_uart_init(void)
  121. {
  122. struct rt_uart_ls1c *uart;
  123. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  124. #ifdef RT_USING_UART2
  125. uart = &uart2;
  126. serial2.ops = &stm32_uart_ops;
  127. serial2.config = config;
  128. pin_set_purpose(36, PIN_PURPOSE_OTHER);
  129. pin_set_purpose(37, PIN_PURPOSE_OTHER);
  130. pin_set_remap(36, PIN_REMAP_SECOND);
  131. pin_set_remap(37, PIN_REMAP_SECOND);
  132. rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial2, "UART2");
  133. /* register UART1 device */
  134. rt_hw_serial_register(&serial2,
  135. "uart2",
  136. //RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX,
  137. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  138. uart);
  139. #endif /* RT_USING_UART1 */
  140. }