drv_usart.c 5.8 KB

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