drv_usart.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2022-10-26 huanghe first commit
  11. * 2023-04-27 huanghe support RT-Smart
  12. */
  13. #include "rtconfig.h"
  14. #ifdef BSP_USING_UART
  15. #include "board.h"
  16. #include <mmu.h>
  17. #include "drv_usart.h"
  18. #include "interrupt.h"
  19. #include "fpl011.h"
  20. extern u32 FUart_GetInterruptMask(FPl011 *uart_ptr);
  21. static void Ft_Os_Uart_Callback(void *Args, u32 Event, u32 EventData);
  22. static void rt_hw_uart_isr(int irqno, void *param)
  23. {
  24. FPl011InterruptHandler(irqno, param);
  25. }
  26. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  27. {
  28. struct drv_usart *uart = RT_NULL;
  29. FPl011 *uart_hw = RT_NULL;
  30. u32 intr_mask;
  31. FPl011Config config;
  32. RT_ASSERT(serial != RT_NULL);
  33. RT_ASSERT(cfg != RT_NULL);
  34. uart = rt_container_of(serial, struct drv_usart, serial);
  35. uart_hw = uart->handle;
  36. config = *(const FPl011Config *)FPl011LookupConfig(uart->config.uart_instance);
  37. #ifdef RT_USING_SMART
  38. config.base_address = (uintptr)rt_ioremap((void *)config.base_address, 0x1000);
  39. #endif
  40. RT_ASSERT(FPl011CfgInitialize(uart_hw, &config) == FT_SUCCESS);
  41. FPl011SetHandler(uart_hw, Ft_Os_Uart_Callback, serial);
  42. FPl011SetRxFifoThreadhold(uart_hw, FPL011IFLS_RXIFLSEL_1_4);
  43. FPl011SetTxFifoThreadHold(uart_hw, FPL011IFLS_TXIFLSEL_1_2);
  44. //<! 打开接收中断
  45. intr_mask = uart->config.isr_event_mask;
  46. FPl011SetInterruptMask(uart_hw, intr_mask);
  47. FPl011SetOptions(uart_hw, FPL011_OPTION_UARTEN | FPL011_OPTION_RXEN | FPL011_OPTION_TXEN | FPL011_OPTION_FIFOEN);
  48. rt_hw_interrupt_set_priority(uart_hw->config.irq_num, uart->config.isr_priority);
  49. rt_hw_interrupt_install(uart_hw->config.irq_num, rt_hw_uart_isr, uart_hw, "uart");
  50. rt_hw_interrupt_umask(uart_hw->config.irq_num);
  51. return RT_EOK;
  52. }
  53. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  54. {
  55. struct drv_usart *uart = RT_NULL;
  56. FPl011 *uart_ptr = RT_NULL;
  57. RT_ASSERT(serial != RT_NULL);
  58. uart = rt_container_of(serial, struct drv_usart, serial);
  59. uart_ptr = uart->handle;
  60. switch (cmd)
  61. {
  62. case RT_DEVICE_CTRL_CLR_INT:
  63. /* disable rx irq */
  64. rt_hw_interrupt_mask(uart_ptr->config.irq_num);
  65. break;
  66. case RT_DEVICE_CTRL_SET_INT:
  67. /* enable rx irq */
  68. rt_hw_interrupt_umask(uart_ptr->config.irq_num);
  69. break;
  70. }
  71. return RT_EOK;
  72. }
  73. static void Ft_Os_Uart_Callback(void *Args, u32 Event, u32 EventData)
  74. {
  75. struct rt_serial_device *serial = (struct rt_serial_device *)Args;
  76. if (FPL011_EVENT_RECV_DATA == Event || FPL011_EVENT_RECV_TOUT == Event)
  77. {
  78. if (serial->serial_rx)
  79. {
  80. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  81. }
  82. }
  83. else if (FPL011_EVENT_RECV_ERROR == Event)
  84. {
  85. }
  86. else if (FPL011_EVENT_SENT_DATA == Event)
  87. {
  88. }
  89. else if (FPL011_EVENT_PARE_FRAME_BRKE == Event)
  90. {
  91. }
  92. else if (FPL011_EVENT_RECV_ORERR == Event)
  93. {
  94. }
  95. if (FPL011_EVENT_SENT_DATA == Event)
  96. {
  97. }
  98. else
  99. {
  100. }
  101. }
  102. static int uart_putc(struct rt_serial_device *serial, char c)
  103. {
  104. struct drv_usart *uart = RT_NULL;
  105. FPl011 *uart_ptr = RT_NULL;
  106. RT_ASSERT(serial != RT_NULL);
  107. uart = rt_container_of(serial, struct drv_usart, serial);
  108. uart_ptr = uart->handle;
  109. FPl011SendByte(uart_ptr->config.base_address, c);
  110. return 1;
  111. }
  112. u32 FPl011RecvByteNoBlocking(uintptr addr)
  113. {
  114. u32 recieved_byte;
  115. while (FUART_RECEIVEDATAEMPTY(addr))
  116. {
  117. return 0xffff;
  118. }
  119. recieved_byte = FUART_READREG32(addr, FPL011DR_OFFSET);
  120. return recieved_byte;
  121. }
  122. static int uart_getc(struct rt_serial_device *serial)
  123. {
  124. int ch;
  125. struct drv_usart *uart = RT_NULL;
  126. FPl011 *uart_ptr = RT_NULL;
  127. RT_ASSERT(serial != RT_NULL);
  128. uart = rt_container_of(serial, struct drv_usart, serial);
  129. uart_ptr = uart->handle;
  130. ch = FPl011RecvByteNoBlocking(uart_ptr->config.base_address);
  131. if (ch == 0xffff)
  132. {
  133. ch = -1;
  134. }
  135. else
  136. {
  137. ch &= 0xff;
  138. }
  139. return ch;
  140. }
  141. static const struct rt_uart_ops _uart_ops =
  142. {
  143. uart_configure,
  144. uart_control,
  145. uart_putc,
  146. uart_getc,
  147. NULL
  148. };
  149. #define RT_USING_UART0
  150. #define RT_USING_UART1
  151. #define RT_USING_UART2
  152. #ifdef RT_USING_UART0
  153. static FPl011 Ft_Uart0;
  154. static struct drv_usart _RtUart0;
  155. #endif
  156. #ifdef RT_USING_UART1
  157. static FPl011 Ft_Uart1;
  158. static struct drv_usart _RtUart1;
  159. #endif
  160. #ifdef RT_USING_UART2
  161. static FPl011 Ft_Uart2;
  162. static struct drv_usart _RtUart2;
  163. #endif
  164. int rt_hw_uart_init(void)
  165. {
  166. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  167. #ifdef RT_USING_UART0
  168. config.bufsz = RT_SERIAL_RB_BUFSZ;
  169. _RtUart0.serial.ops = &_uart_ops;
  170. _RtUart0.serial.config = config;
  171. _RtUart0.handle = &Ft_Uart0;
  172. _RtUart0.config.uart_instance = FUART0_ID;
  173. _RtUart0.config.isr_priority = 0xd0;
  174. _RtUart0.config.isr_event_mask = (RTOS_UART_ISR_OEIM_MASK | RTOS_UART_ISR_BEIM_MASK | RTOS_UART_ISR_PEIM_MASK | RTOS_UART_ISR_FEIM_MASK | RTOS_UART_ISR_RTIM_MASK | RTOS_UART_ISR_RXIM_MASK);
  175. _RtUart0.config.uart_baudrate = 115200;
  176. rt_hw_serial_register(&_RtUart0.serial, "uart0",
  177. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  178. &_RtUart0);
  179. #endif
  180. #ifdef RT_USING_UART1
  181. config.bufsz = RT_SERIAL_RB_BUFSZ;
  182. _RtUart1.serial.ops = &_uart_ops;
  183. _RtUart1.serial.config = config;
  184. _RtUart1.handle = &Ft_Uart1;
  185. _RtUart1.config.uart_instance = FUART1_ID;
  186. _RtUart1.config.isr_priority = 0xd0;
  187. _RtUart1.config.isr_event_mask = (RTOS_UART_ISR_OEIM_MASK | RTOS_UART_ISR_BEIM_MASK | RTOS_UART_ISR_PEIM_MASK | RTOS_UART_ISR_FEIM_MASK | RTOS_UART_ISR_RTIM_MASK | RTOS_UART_ISR_RXIM_MASK);
  188. _RtUart1.config.uart_baudrate = 115200;
  189. rt_hw_serial_register(&_RtUart1.serial, "uart1",
  190. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  191. &_RtUart1);
  192. #endif
  193. #ifdef RT_USING_UART2
  194. config.bufsz = RT_SERIAL_RB_BUFSZ;
  195. _RtUart2.serial.ops = &_uart_ops;
  196. _RtUart2.serial.config = config;
  197. _RtUart2.handle = &Ft_Uart2;
  198. _RtUart2.config.uart_instance = FUART2_ID;
  199. _RtUart2.config.isr_priority = 0xd0;
  200. _RtUart2.config.isr_event_mask = (RTOS_UART_ISR_OEIM_MASK | RTOS_UART_ISR_BEIM_MASK | RTOS_UART_ISR_PEIM_MASK | RTOS_UART_ISR_FEIM_MASK | RTOS_UART_ISR_RTIM_MASK | RTOS_UART_ISR_RXIM_MASK);
  201. _RtUart2.config.uart_baudrate = 115200;
  202. rt_hw_serial_register(&_RtUart2.serial, "uart2",
  203. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  204. &_RtUart2);
  205. #endif
  206. return 0;
  207. }
  208. INIT_BOARD_EXPORT(rt_hw_uart_init);
  209. #endif