drv_usart.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  73. }
  74. else if (FUART_EVENT_RECV_ERROR == Event)
  75. {
  76. }
  77. else if (FUART_EVENT_SENT_DATA == Event)
  78. {
  79. }
  80. else if (FUART_EVENT_PARE_FRAME_BRKE == Event)
  81. {
  82. }
  83. else if (FUART_EVENT_RECV_ORERR == Event)
  84. {
  85. }
  86. if (FUART_EVENT_SENT_DATA == Event)
  87. {
  88. }
  89. else
  90. {
  91. }
  92. }
  93. static int uart_putc(struct rt_serial_device *serial, char c)
  94. {
  95. struct drv_usart *uart = RT_NULL;
  96. Ft_Uart *uart_ptr = RT_NULL;
  97. RT_ASSERT(serial != RT_NULL);
  98. uart = rt_container_of(serial, struct drv_usart, serial);
  99. uart_ptr = uart->handle;
  100. FUart_SendByte(uart_ptr->Config.BaseAddress, c);
  101. return 1;
  102. }
  103. static int uart_getc(struct rt_serial_device *serial)
  104. {
  105. int ch;
  106. struct drv_usart *uart = RT_NULL;
  107. Ft_Uart *uart_ptr = RT_NULL;
  108. RT_ASSERT(serial != RT_NULL);
  109. uart = rt_container_of(serial, struct drv_usart, serial);
  110. uart_ptr = uart->handle;
  111. ch = FUart_GetChar(uart_ptr->Config.BaseAddress);
  112. if (ch == 0xff)
  113. ch = -1;
  114. return ch;
  115. }
  116. static const struct rt_uart_ops _uart_ops =
  117. {
  118. uart_configure,
  119. uart_control,
  120. uart_putc,
  121. uart_getc,
  122. };
  123. #ifdef RT_USING_UART0
  124. static Ft_Uart Ft_Uart0;
  125. static struct drv_usart _RtUart0;
  126. #endif
  127. #ifdef RT_USING_UART1
  128. static Ft_Uart Ft_Uart1;
  129. static struct drv_usart _RtUart1;
  130. #endif
  131. int rt_hw_uart_init(void)
  132. {
  133. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  134. #ifdef RT_USING_UART0
  135. config.bufsz = RT_SERIAL_RB_BUFSZ;
  136. _RtUart0.serial.ops = &_uart_ops;
  137. _RtUart0.serial.config = config;
  138. Ft_Uart0.Config.InstanceId = FT_UART0_ID;
  139. _RtUart0.Handle = &Ft_Uart0;
  140. rt_hw_serial_register(&_RtUart0.serial, "uart0",
  141. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  142. &_RtUart0);
  143. #endif
  144. #ifdef RT_USING_UART1
  145. config.bufsz = RT_SERIAL_RB_BUFSZ;
  146. _RtUart1.serial.ops = &_uart_ops;
  147. _RtUart1.serial.config = config;
  148. Ft_Uart1.Config.InstanceId = FT_UART1_ID;
  149. _RtUart1.handle = &Ft_Uart1;
  150. rt_hw_serial_register(&_RtUart1.serial, "uart1",
  151. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  152. &_RtUart1);
  153. #endif
  154. return 0;
  155. }
  156. INIT_BOARD_EXPORT(rt_hw_uart_init);
  157. #endif /* RT_USING_SERIAL */