drv_usart.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. * 2021-03-04 Carl the first version
  9. */
  10. #include "board.h"
  11. #include "drv_usart.h"
  12. #include "interrupt.h"
  13. #include "serial.h"
  14. #include "rtconfig.h"
  15. #ifdef RT_USING_SERIAL
  16. extern u32 FUart_GetInterruptMask(Ft_Uart *uart_ptr);
  17. static void Ft_Os_Uart_Callback(void *Args, u32 Event, u32 EventData);
  18. static void rt_hw_uart_isr(int irqno, void *param)
  19. {
  20. Ft_Uart *uart_ptr = (Ft_Uart *)param;
  21. FUart_InterruptHandler(uart_ptr);
  22. }
  23. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  24. {
  25. struct drv_usart *uart = RT_NULL;
  26. Ft_Uart *uart_ptr = RT_NULL;
  27. u32 RegTemp;
  28. u32 ret;
  29. RT_ASSERT(serial != RT_NULL);
  30. RT_ASSERT(cfg != RT_NULL);
  31. uart = rt_container_of(serial, struct drv_usart, serial);
  32. uart_ptr = uart->handle;
  33. RT_ASSERT(FUart_CfgInitialize(uart_ptr, FUart_LookupConfig(uart_ptr->Config.InstanceId)) == FST_SUCCESS);
  34. FUart_SetHandler(uart_ptr, Ft_Os_Uart_Callback, serial);
  35. rt_hw_interrupt_install(uart_ptr->Config.IsrNum, rt_hw_uart_isr, uart_ptr, "uart");
  36. rt_hw_interrupt_umask(uart_ptr->Config.IsrNum);
  37. //<! 设置波特率
  38. ret = FUart_SetBaudRate(uart_ptr, cfg->baud_rate);
  39. RT_ASSERT(ret == FST_SUCCESS);
  40. //<! 打开接收中断
  41. RegTemp = FUart_GetInterruptMask(uart_ptr);
  42. RegTemp |= UARTMIS_RTMIS;
  43. FUart_SetInterruptMask(uart_ptr, RegTemp);
  44. FUart_SetOptions(uart_ptr, FUART_OPTION_UARTEN | FUART_OPTION_RXEN | FUART_OPTION_TXEN | FUART_OPTION_FIFOEN);
  45. return RT_EOK;
  46. }
  47. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  48. {
  49. struct drv_usart *uart = RT_NULL;
  50. Ft_Uart *uart_ptr = RT_NULL;
  51. RT_ASSERT(serial != RT_NULL);
  52. uart = rt_container_of(serial, struct drv_usart, serial);
  53. uart_ptr = uart->handle;
  54. switch (cmd)
  55. {
  56. case RT_DEVICE_CTRL_CLR_INT:
  57. /* disable rx irq */
  58. rt_hw_interrupt_mask(uart_ptr->Config.IsrNum);
  59. break;
  60. case RT_DEVICE_CTRL_SET_INT:
  61. /* enable rx irq */
  62. rt_hw_interrupt_umask(uart_ptr->Config.IsrNum);
  63. break;
  64. }
  65. return RT_EOK;
  66. }
  67. static void Ft_Os_Uart_Callback(void *Args, u32 Event, u32 EventData)
  68. {
  69. struct rt_serial_device *serial = (struct rt_serial_device *)Args;
  70. if (FUART_EVENT_RECV_DATA == Event || FUART_EVENT_RECV_TOUT == Event)
  71. {
  72. if (serial->serial_rx)
  73. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  74. }
  75. else if (FUART_EVENT_RECV_ERROR == Event)
  76. {
  77. }
  78. else if (FUART_EVENT_SENT_DATA == Event)
  79. {
  80. }
  81. else if (FUART_EVENT_PARE_FRAME_BRKE == Event)
  82. {
  83. }
  84. else if (FUART_EVENT_RECV_ORERR == Event)
  85. {
  86. }
  87. if (FUART_EVENT_SENT_DATA == Event)
  88. {
  89. }
  90. else
  91. {
  92. }
  93. }
  94. static int uart_putc(struct rt_serial_device *serial, char c)
  95. {
  96. struct drv_usart *uart = RT_NULL;
  97. Ft_Uart *uart_ptr = RT_NULL;
  98. RT_ASSERT(serial != RT_NULL);
  99. uart = rt_container_of(serial, struct drv_usart, serial);
  100. uart_ptr = uart->handle;
  101. FUart_SendByte(uart_ptr->Config.BaseAddress, c);
  102. return 1;
  103. }
  104. static int uart_getc(struct rt_serial_device *serial)
  105. {
  106. int ch;
  107. struct drv_usart *uart = RT_NULL;
  108. Ft_Uart *uart_ptr = RT_NULL;
  109. RT_ASSERT(serial != RT_NULL);
  110. uart = rt_container_of(serial, struct drv_usart, serial);
  111. uart_ptr = uart->handle;
  112. ch = FUart_GetChar(uart_ptr->Config.BaseAddress);
  113. if (ch == 0xff)
  114. ch = -1;
  115. return ch;
  116. }
  117. static const struct rt_uart_ops _uart_ops =
  118. {
  119. uart_configure,
  120. uart_control,
  121. uart_putc,
  122. uart_getc,
  123. };
  124. #ifdef RT_USING_UART0
  125. static Ft_Uart Ft_Uart0;
  126. static struct drv_usart _RtUart0;
  127. #endif
  128. #ifdef RT_USING_UART1
  129. static Ft_Uart Ft_Uart1;
  130. static struct drv_usart _RtUart1;
  131. #endif
  132. int rt_hw_uart_init(void)
  133. {
  134. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  135. #ifdef RT_USING_UART0
  136. config.bufsz = RT_SERIAL_RB_BUFSZ;
  137. _RtUart0.serial.ops = &_uart_ops;
  138. _RtUart0.serial.config = config;
  139. Ft_Uart0.Config.InstanceId = FT_UART0_ID;
  140. _RtUart0.Handle = &Ft_Uart0;
  141. rt_hw_serial_register(&_RtUart0.serial, "uart0",
  142. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  143. &_RtUart0);
  144. #endif
  145. #ifdef RT_USING_UART1
  146. config.bufsz = RT_SERIAL_RB_BUFSZ;
  147. _RtUart1.serial.ops = &_uart_ops;
  148. _RtUart1.serial.config = config;
  149. Ft_Uart1.Config.InstanceId = FT_UART1_ID;
  150. _RtUart1.handle = &Ft_Uart1;
  151. rt_hw_serial_register(&_RtUart1.serial, "uart1",
  152. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  153. &_RtUart1);
  154. #endif
  155. return 0;
  156. }
  157. INIT_BOARD_EXPORT(rt_hw_uart_init);
  158. #endif /* RT_USING_SERIAL */