drv_uart.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-08 zhuangwei the first version
  9. * 2021-02-02 michael5hzg@gmail.com adapt to ls1b
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <rthw.h>
  14. #include "drv_uart.h"
  15. #include "ls1b_pin.h"
  16. #include "ls1b_uart.h"
  17. /* STM32 uart driver */
  18. struct rt_uart_ls1b
  19. {
  20. ls1b_uart_t UARTx;
  21. rt_uint32_t IRQ;
  22. };
  23. static rt_err_t ls1b_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  24. {
  25. struct rt_uart_ls1b *uart_dev = RT_NULL;
  26. ls1b_uart_info_t uart_info = {0};
  27. RT_ASSERT(serial != RT_NULL);
  28. RT_ASSERT(cfg != RT_NULL);
  29. uart_dev = (struct rt_uart_ls1b *)serial->parent.user_data;
  30. // 初始化串口
  31. uart_info.UARTx = uart_dev->UARTx;
  32. uart_info.baudrate = cfg->baud_rate;
  33. uart_info.rx_enable = TRUE;
  34. uart_init(&uart_info);
  35. return RT_EOK;
  36. }
  37. static rt_err_t ls1b_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  38. {
  39. struct rt_uart_ls1b *uart_dev = RT_NULL;
  40. RT_ASSERT(serial != RT_NULL);
  41. uart_dev = (struct rt_uart_ls1b *)serial->parent.user_data;
  42. switch (cmd)
  43. {
  44. case RT_DEVICE_CTRL_CLR_INT: /* disable rx irq */
  45. rt_hw_interrupt_mask(uart_dev->IRQ);
  46. break;
  47. case RT_DEVICE_CTRL_SET_INT: /* enable rx irq */
  48. rt_hw_interrupt_umask(uart_dev->IRQ);
  49. break;
  50. default:
  51. break;
  52. }
  53. return RT_EOK;
  54. }
  55. static int ls1b_uart_putc(struct rt_serial_device *serial, char c)
  56. {
  57. struct rt_uart_ls1b *uart_dev = RT_NULL;
  58. RT_ASSERT(serial != RT_NULL);
  59. uart_dev = (struct rt_uart_ls1b *)serial->parent.user_data;
  60. uart_putc(uart_dev->UARTx, c);
  61. return 1;
  62. }
  63. static int ls1b_uart_getc(struct rt_serial_device *serial)
  64. {
  65. struct rt_uart_ls1b *uart_dev = RT_NULL;
  66. RT_ASSERT(serial != RT_NULL);
  67. uart_dev = (struct rt_uart_ls1b *)serial->parent.user_data;
  68. void *uart_base = uart_get_base(uart_dev->UARTx);
  69. if (LSR_RXRDY & reg_read_8(uart_base + LS1B_UART_LSR_OFFSET))
  70. {
  71. return reg_read_8(uart_base + LS1B_UART_DAT_OFFSET);
  72. }
  73. return -1;
  74. }
  75. /* UART interrupt handler */
  76. static void uart_irq_handler(int vector, void *param)
  77. {
  78. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  79. struct rt_uart_ls1b *uart_dev = RT_NULL;
  80. RT_ASSERT(serial != RT_NULL);
  81. uart_dev = (struct rt_uart_ls1b *)serial->parent.user_data;
  82. void *uart_base = uart_get_base(uart_dev->UARTx);
  83. unsigned char iir = reg_read_8(uart_base + LS1B_UART_IIR_OFFSET);
  84. // 判断是否为接收超时或接收到有效数据
  85. if ((IIR_RXTOUT & iir) || (IIR_RXRDY & iir))
  86. {
  87. rt_interrupt_enter();
  88. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  89. rt_interrupt_leave();
  90. }
  91. }
  92. static const struct rt_uart_ops ls1b_uart_ops =
  93. {
  94. ls1b_uart_configure,
  95. ls1b_uart_control,
  96. ls1b_uart_putc,
  97. ls1b_uart_getc,
  98. };
  99. #if defined(RT_USING_UART1)
  100. struct rt_uart_ls1b uart1 =
  101. {
  102. LS1B_UART1,
  103. LS1B_UART1_IRQ,
  104. };
  105. struct rt_serial_device serial1;
  106. #endif /* RT_USING_UART1 */
  107. #if defined(RT_USING_UART2)
  108. struct rt_uart_ls1b uart2 =
  109. {
  110. LS1B_UART2,
  111. LS1B_UART2_IRQ,
  112. };
  113. struct rt_serial_device serial2;
  114. #endif /* RT_USING_UART2 */
  115. #if defined(RT_USING_UART3)
  116. struct rt_uart_ls1b uart3 =
  117. {
  118. LS1B_UART3,
  119. LS1B_UART3_IRQ,
  120. };
  121. struct rt_serial_device serial3;
  122. #endif /* RT_USING_UART3 */
  123. #if defined(RT_USING_UART4)
  124. struct rt_uart_ls1b uart4 =
  125. {
  126. LS1B_UART4,
  127. LS1B_UART4_IRQ,
  128. };
  129. struct rt_serial_device serial4;
  130. #endif /* RT_USING_UART4 */
  131. #if defined(RT_USING_UART5)
  132. struct rt_uart_ls1b uart5 =
  133. {
  134. LS1B_UART5,
  135. LS1B_UART5_IRQ,
  136. };
  137. struct rt_serial_device serial5;
  138. #endif /* RT_USING_UART5 */
  139. void rt_hw_uart_init(void)
  140. {
  141. struct rt_uart_ls1b *uart;
  142. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  143. #ifdef RT_USING_UART5
  144. uart = &uart5;
  145. serial5.ops = &ls1b_uart_ops;
  146. serial5.config = config;
  147. rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial5, "UART5");
  148. /* register UART5 device */
  149. rt_hw_serial_register(&serial5,
  150. "uart5",
  151. //RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX,
  152. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  153. uart);
  154. #endif /* RT_USING_UART5 */
  155. #ifdef RT_USING_UART2
  156. uart = &uart2;
  157. serial2.ops = &ls1b_uart_ops;
  158. serial2.config = config;
  159. pin_set_purpose(36, PIN_PURPOSE_OTHER);
  160. pin_set_purpose(37, PIN_PURPOSE_OTHER);
  161. pin_set_remap(36, PIN_REMAP_SECOND);
  162. pin_set_remap(37, PIN_REMAP_SECOND);
  163. rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial2, "UART2");
  164. /* register UART2 device */
  165. rt_hw_serial_register(&serial2,
  166. "uart2",
  167. //RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX,
  168. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  169. uart);
  170. #endif /* RT_USING_UART2 */
  171. }