drv_usart.c 5.9 KB

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