drv_uart_v2.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-04-08 Hydevcode the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <rtdbg.h>
  13. #include "board.h"
  14. #include "drv_uart_v2.h"
  15. #include "mmu.h"
  16. #ifdef RT_USING_SERIAL_V2
  17. #include <rtdbg.h>
  18. #if !defined(BSP_USING_UART0) && !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3)
  19. #error "Please define at least one BSP_USING_UARTx"
  20. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  21. #endif
  22. struct hw_uart_device
  23. {
  24. rt_uint32_t hw_base;
  25. rt_uint32_t irqno;
  26. struct rt_serial_device *serial;
  27. char *device_name;
  28. };
  29. #define UART_DR(base) __REG32(base + 0x00)
  30. #define UART_FR(base) __REG32(base + 0x18)
  31. #define UART_CR(base) __REG32(base + 0x30)
  32. #define UART_IMSC(base) __REG32(base + 0x38)
  33. #define UART_ICR(base) __REG32(base + 0x44)
  34. #define UART_IFLS(base) __REG32(base + 0x34)
  35. #define UART_TCR(base) __REG32(base + 0x80)
  36. #define UART_ITOP(base) __REG32(base + 0x88)
  37. #define UART_LCR_H(base) __REG32(base + 0x2C)
  38. #define UART_DMACR(base) __REG32(base + 0x48)
  39. #define UARTITOP_RXINTR 0x400
  40. #define UARTLCR_H_FEN 0x10
  41. #define UARTFR_RXFE 0x10
  42. #define UARTFR_TXFF 0x20
  43. #define UARTFR_RXFF 0x40
  44. #define UARTFR_TXFE 0x80
  45. #define UARTFR_BUSY 0x08
  46. #define UARTICR_RXIC 0x10
  47. #define UARTICR_TXIC 0x20
  48. #define UARTIMSC_RXIM 0x10
  49. #define UARTIMSC_RTIM 0x40
  50. #define UARTIMSC_TXIM 0x20
  51. #if defined(BSP_USING_UART0)
  52. struct rt_serial_device serial0;
  53. #endif
  54. #if defined(BSP_USING_UART1)
  55. struct rt_serial_device serial1;
  56. #endif
  57. #if defined(BSP_USING_UART2)
  58. struct rt_serial_device serial2;
  59. #endif
  60. #if defined(BSP_USING_UART3)
  61. struct rt_serial_device serial3;
  62. #endif
  63. static struct hw_uart_device _uart_device[] = {
  64. #if defined(BSP_USING_UART0)
  65. {
  66. REALVIEW_UART0_BASE,
  67. IRQ_PBA8_UART0,
  68. &serial0,
  69. "uart0",
  70. },
  71. #endif
  72. #if defined(BSP_USING_UART1)
  73. {
  74. REALVIEW_UART1_BASE,
  75. IRQ_PBA8_UART1,
  76. &serial1,
  77. "uart1",
  78. },
  79. #endif
  80. #if defined(BSP_USING_UART2)
  81. {
  82. REALVIEW_UART2_BASE,
  83. IRQ_PBA8_UART2,
  84. &serial2,
  85. "uart2",
  86. },
  87. #endif
  88. #if defined(BSP_USING_UART3)
  89. {
  90. REALVIEW_UART3_BASE,
  91. IRQ_PBA8_UART3,
  92. &serial3,
  93. "uart3",
  94. },
  95. #endif
  96. };
  97. /**
  98. * @brief UART common interrupt process. This
  99. *
  100. * @param serial Serial device
  101. */
  102. static void rt_hw_uart_isr(int irqno, void *param)
  103. {
  104. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  105. RT_ASSERT(serial != RT_NULL);
  106. struct hw_uart_device *uart;
  107. uart = (struct hw_uart_device *)serial->parent.user_data;
  108. RT_ASSERT(uart != RT_NULL);
  109. if((UART_FR(uart->hw_base) & UARTFR_RXFF) && (UART_IMSC(uart->hw_base) & UARTIMSC_RXIM))
  110. {
  111. struct rt_serial_rx_fifo *rx_fifo;
  112. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  113. RT_ASSERT(rx_fifo != RT_NULL);
  114. char rec_ch;
  115. while (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
  116. {
  117. rec_ch = UART_DR(uart->hw_base) & 0xff;
  118. rt_hw_serial_control_isr(serial, RT_HW_SERIAL_CTRL_PUTC, &rec_ch);
  119. }
  120. rt_hw_serial_isr(serial,RT_SERIAL_EVENT_RX_IND);
  121. }
  122. else if(UART_IMSC(uart->hw_base) & UARTIMSC_RTIM)
  123. {
  124. struct rt_serial_rx_fifo *rx_fifo;
  125. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  126. RT_ASSERT(rx_fifo != RT_NULL);
  127. char rec_ch;
  128. while (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
  129. {
  130. rec_ch = UART_DR(uart->hw_base) & 0xff;
  131. rt_hw_serial_control_isr(serial, RT_HW_SERIAL_CTRL_PUTC, &rec_ch);
  132. }
  133. rt_hw_serial_isr(serial,RT_SERIAL_EVENT_RX_IND);
  134. }
  135. else if((UART_IMSC(uart->hw_base) & UARTIMSC_TXIM))
  136. {
  137. UART_ICR(uart->hw_base) |= UARTICR_TXIC;
  138. rt_hw_serial_isr(serial,RT_SERIAL_EVENT_TX_DONE);
  139. }
  140. }
  141. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  142. {
  143. return RT_EOK;
  144. }
  145. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  146. {
  147. struct hw_uart_device *uart;
  148. RT_ASSERT(serial != RT_NULL);
  149. uart = (struct hw_uart_device *)serial->parent.user_data;
  150. rt_ubase_t ctrl_arg = (rt_ubase_t) arg;
  151. if(ctrl_arg & (RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_RX_NON_BLOCKING))
  152. {
  153. ctrl_arg = RT_DEVICE_FLAG_INT_RX;
  154. }
  155. else if(ctrl_arg & (RT_DEVICE_FLAG_TX_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING))
  156. {
  157. ctrl_arg = RT_DEVICE_FLAG_INT_TX;
  158. }
  159. switch (cmd)
  160. {
  161. case RT_DEVICE_CTRL_CLR_INT:
  162. if (ctrl_arg == RT_DEVICE_FLAG_INT_RX)
  163. {
  164. /* disable rx irq */
  165. UART_IMSC(uart->hw_base) &= ~UARTIMSC_RXIM;
  166. UART_IMSC(uart->hw_base) &= ~UARTIMSC_RTIM;
  167. }
  168. else if (ctrl_arg == RT_DEVICE_FLAG_INT_TX)
  169. {
  170. /* disable tx irq */
  171. UART_IMSC(uart->hw_base) &= ~UARTIMSC_TXIM;
  172. }
  173. break;
  174. case RT_DEVICE_CTRL_SET_INT:
  175. if (ctrl_arg == RT_DEVICE_FLAG_INT_RX)
  176. {
  177. /* enable rx irq */
  178. UART_IMSC(uart->hw_base) |= UARTIMSC_RXIM;
  179. UART_IMSC(uart->hw_base) |= UARTIMSC_RTIM;
  180. rt_hw_interrupt_umask(uart->irqno);
  181. } else if (ctrl_arg == RT_DEVICE_FLAG_INT_TX)
  182. {
  183. /* enable tx irq */
  184. UART_IMSC(uart->hw_base) |= UARTIMSC_TXIM;
  185. rt_hw_interrupt_umask(uart->irqno);
  186. }
  187. break;
  188. case RT_DEVICE_CTRL_CONFIG:
  189. uart_control(serial, RT_DEVICE_CTRL_SET_INT, (void *)ctrl_arg);
  190. break;
  191. }
  192. return RT_EOK;
  193. }
  194. static int uart_putc(struct rt_serial_device *serial, char ch)
  195. {
  196. struct hw_uart_device *uart;
  197. RT_ASSERT(serial != RT_NULL);
  198. uart = (struct hw_uart_device *)serial->parent.user_data;
  199. while (UART_FR(uart->hw_base) & UARTFR_TXFF);
  200. UART_DR(uart->hw_base) = ch;
  201. return 1;
  202. }
  203. static int uart_getc(struct rt_serial_device *serial)
  204. {
  205. int ch;
  206. struct hw_uart_device *uart;
  207. RT_ASSERT(serial != RT_NULL);
  208. uart = (struct hw_uart_device *)serial->parent.user_data;
  209. ch = -1;
  210. if (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
  211. {
  212. ch = UART_DR(uart->hw_base) & 0xff;
  213. }
  214. return ch;
  215. }
  216. static rt_ssize_t uart_transmit(struct rt_serial_device *serial,
  217. rt_uint8_t *buf,
  218. rt_size_t size,
  219. rt_uint32_t tx_flag)
  220. {
  221. RT_ASSERT(serial != RT_NULL);
  222. RT_ASSERT(buf != RT_NULL);
  223. RT_ASSERT(size);
  224. struct hw_uart_device *uart = (struct hw_uart_device *)serial->parent.user_data;
  225. struct rt_serial_tx_fifo *tx_fifo;
  226. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  227. uint8_t ch = 0;
  228. RT_ASSERT(tx_fifo != RT_NULL);
  229. if (size > 0)
  230. {
  231. if (UART_IMSC(uart->hw_base) & UARTIMSC_TXIM)
  232. {
  233. UART_IMSC(uart->hw_base) &= ~UARTIMSC_TXIM;
  234. if(rt_ringbuffer_getchar(&tx_fifo->rb, &ch))
  235. {
  236. while (UART_FR(uart->hw_base) & UARTFR_TXFF);
  237. UART_DR(uart->hw_base) = ch;
  238. }
  239. UART_IMSC(uart->hw_base) |= UARTIMSC_TXIM;
  240. }
  241. }
  242. return size;
  243. }
  244. static const struct rt_uart_ops _uart_ops = {
  245. .configure = uart_configure,
  246. .control = uart_control,
  247. .putc = uart_putc,
  248. .getc = uart_getc,
  249. .transmit = uart_transmit
  250. };
  251. static int uart_config(void)
  252. {
  253. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  254. #ifdef BSP_USING_UART0
  255. _uart_device[0].serial->config = config;
  256. _uart_device[0].serial->config.rx_bufsz = BSP_UART0_RX_BUFSIZE;
  257. _uart_device[0].serial->config.tx_bufsz = BSP_UART0_TX_BUFSIZE;
  258. #endif /* BSP_USING_UART0 */
  259. #ifdef BSP_USING_UART1
  260. _uart_device[1].serial->config = config;
  261. _uart_device[1].serial->config.rx_bufsz = BSP_UART1_RX_BUFSIZE;
  262. _uart_device[1].serial->config.tx_bufsz = BSP_UART1_TX_BUFSIZE;
  263. #endif /* BSP_USING_UART1 */
  264. #ifdef BSP_USING_UART2
  265. _uart_device[2].serial->config = config;
  266. _uart_device[2].serial->config.rx_bufsz = BSP_UART2_RX_BUFSIZE;
  267. _uart_device[2].serial->config.tx_bufsz = BSP_UART2_TX_BUFSIZE;
  268. #endif /* BSP_USING_UART2 */
  269. #ifdef BSP_USING_UART3
  270. _uart_device[3].serial->config = config;
  271. _uart_device[3].serial->config.rx_bufsz = BSP_UART3_RX_BUFSIZE;
  272. _uart_device[3].serial->config.tx_bufsz = BSP_UART3_TX_BUFSIZE;
  273. #endif /* BSP_USING_UART3 */
  274. return RT_EOK;
  275. }
  276. int rt_hw_uart_init(void)
  277. {
  278. rt_err_t err = RT_EOK;
  279. uart_config();
  280. for (uint32_t i = 0; i < sizeof(_uart_device) / sizeof(_uart_device[0]); i++)
  281. {
  282. #ifdef RT_USING_SMART
  283. _uart_device[i].hw_base = (uint32_t)rt_ioremap((void*)_uart_device[i].hw_base, 0x1000);
  284. #endif
  285. _uart_device[i].serial->ops = &_uart_ops;
  286. /* register UART device */
  287. err = rt_hw_serial_register(_uart_device[i].serial,
  288. _uart_device[i].device_name,
  289. RT_DEVICE_FLAG_RDWR,
  290. (void*)&_uart_device[i]);
  291. rt_hw_interrupt_install(_uart_device[i].irqno, rt_hw_uart_isr, _uart_device[i].serial, _uart_device[i].device_name);
  292. /* enable Rx and Tx of UART */
  293. UART_CR(_uart_device[i].hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  294. UART_LCR_H(_uart_device[i].hw_base) =(1 << 4);
  295. UART_IFLS(_uart_device[i].hw_base) =0;
  296. UART_IFLS(_uart_device[i].hw_base) =(1 << 3);
  297. }
  298. return err;
  299. }
  300. INIT_BOARD_EXPORT(rt_hw_uart_init);
  301. #endif /* RT_USING_SERIAL_V2 */