drv_uart.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. * 2020-04-16 bigmagic first version
  9. * 2020-05-26 bigmagic add other uart
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include "board.h"
  15. #include "drv_uart.h"
  16. #include "drv_gpio.h"
  17. #ifdef RT_USING_UART0
  18. static struct rt_serial_device _serial0;
  19. #endif
  20. #ifdef RT_USING_UART1
  21. static struct rt_serial_device _serial1;
  22. #endif
  23. #ifdef RT_USING_UART3
  24. static struct rt_serial_device _serial3;
  25. #endif
  26. #ifdef RT_USING_UART4
  27. static struct rt_serial_device _serial4;
  28. #endif
  29. #ifdef RT_USING_UART5
  30. static struct rt_serial_device _serial5;
  31. #endif
  32. struct hw_uart_device
  33. {
  34. rt_ubase_t hw_base;
  35. rt_uint32_t irqno;
  36. };
  37. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  38. {
  39. struct hw_uart_device *uart;
  40. uint32_t bauddiv = (UART_REFERENCE_CLOCK / cfg->baud_rate)* 1000 / 16;
  41. uint32_t ibrd = bauddiv / 1000;
  42. RT_ASSERT(serial != RT_NULL);
  43. uart = (struct hw_uart_device *)serial->parent.user_data;
  44. if(uart->hw_base == AUX_BASE)
  45. {
  46. prev_raspi_pin_mode(GPIO_PIN_14, ALT5);
  47. prev_raspi_pin_mode(GPIO_PIN_15, ALT5);
  48. AUX_ENABLES(uart->hw_base) = 1; /* Enable UART1 */
  49. AUX_MU_IER_REG(uart->hw_base) = 0; /* Disable interrupt */
  50. AUX_MU_CNTL_REG(uart->hw_base) = 0; /* Disable Transmitter and Receiver */
  51. AUX_MU_LCR_REG(uart->hw_base) = 3; /* Works in 8-bit mode */
  52. AUX_MU_MCR_REG(uart->hw_base) = 0; /* Disable RTS */
  53. AUX_MU_IIR_REG(uart->hw_base) = 0xC6; /* Enable FIFO, Clear FIFO */
  54. AUX_MU_BAUD_REG(uart->hw_base) = 541; /* 115200 = system clock 500MHz / (8 * (baud + 1)), baud = 541 */
  55. AUX_MU_CNTL_REG(uart->hw_base) = 3; /* Enable Transmitter and Receiver */
  56. return RT_EOK;
  57. }
  58. if(uart->hw_base == UART0_BASE)
  59. {
  60. #ifndef BSP_USING_BULETOOTH
  61. prev_raspi_pin_mode(GPIO_PIN_14, ALT0);
  62. prev_raspi_pin_mode(GPIO_PIN_15, ALT0);
  63. #else
  64. prev_raspi_pin_mode(GPIO_PIN_30, ALT3);
  65. prev_raspi_pin_mode(GPIO_PIN_31, ALT3);
  66. prev_raspi_pin_mode(GPIO_PIN_32, ALT3);
  67. prev_raspi_pin_mode(GPIO_PIN_33, ALT3);
  68. #endif
  69. }
  70. if(uart->hw_base == UART3_BASE)
  71. {
  72. prev_raspi_pin_mode(GPIO_PIN_4, ALT4);
  73. prev_raspi_pin_mode(GPIO_PIN_5, ALT4);
  74. }
  75. if(uart->hw_base == UART4_BASE)
  76. {
  77. prev_raspi_pin_mode(GPIO_PIN_8, ALT4);
  78. prev_raspi_pin_mode(GPIO_PIN_9, ALT4);
  79. }
  80. if(uart->hw_base == UART5_BASE)
  81. {
  82. prev_raspi_pin_mode(GPIO_PIN_12, ALT4);
  83. prev_raspi_pin_mode(GPIO_PIN_13, ALT4);
  84. }
  85. PL011_REG_IMSC(uart->hw_base) = 0; /* mask all interrupt */
  86. PL011_REG_ICR(uart->hw_base) = 0x7ff; /* clear all interrupt */
  87. //PL011 clock 480MHz 480x10^6/baudrate/16
  88. PL011_REG_IBRD(uart->hw_base) = ibrd;
  89. PL011_REG_FBRD(uart->hw_base) = (((bauddiv - ibrd * 1000) * 64 + 500) / 1000);
  90. #ifdef BSP_USING_BULETOOTH
  91. PL011_REG_IFLS(uart->hw_base) = 0x08;
  92. PL011_REG_LCRH(uart->hw_base) = 0x70;
  93. PL011_REG_CR(uart->hw_base) = PL011_CR_UARTEN | PL011_CR_TXE | PL011_CR_RXE | PL011_CR_RTS;
  94. #else
  95. PL011_REG_IFLS(uart->hw_base) = 0x0;
  96. PL011_REG_LCRH(uart->hw_base) = PL011_LCRH_WLEN_8;
  97. PL011_REG_CR(uart->hw_base) = PL011_CR_UARTEN | PL011_CR_TXE | PL011_CR_RXE;
  98. #endif
  99. PL011_REG_IMSC(uart->hw_base) = 0;
  100. return RT_EOK;
  101. }
  102. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  103. {
  104. struct hw_uart_device *uart;
  105. RT_ASSERT(serial != RT_NULL);
  106. uart = (struct hw_uart_device *)serial->parent.user_data;
  107. switch (cmd)
  108. {
  109. case RT_DEVICE_CTRL_CLR_INT:
  110. /* disable rx irq */
  111. if(uart->hw_base == AUX_BASE)
  112. {
  113. AUX_MU_IER_REG(uart->hw_base) = 0x0;
  114. }
  115. else
  116. {
  117. PL011_REG_IMSC(uart->hw_base) &= ~((uint32_t)PL011_IMSC_RXIM);
  118. }
  119. rt_hw_interrupt_mask(uart->irqno);
  120. break;
  121. case RT_DEVICE_CTRL_SET_INT:
  122. /* enable rx irq */
  123. if(uart->hw_base == AUX_BASE)
  124. {
  125. AUX_MU_IER_REG(uart->hw_base) = 0x1;
  126. }
  127. else
  128. {
  129. PL011_REG_IMSC(uart->hw_base) |= PL011_IMSC_RXIM;
  130. }
  131. rt_hw_interrupt_umask(uart->irqno);
  132. break;
  133. }
  134. return RT_EOK;
  135. }
  136. static int uart_putc(struct rt_serial_device *serial, char c)
  137. {
  138. struct hw_uart_device *uart;
  139. RT_ASSERT(serial != RT_NULL);
  140. uart = (struct hw_uart_device *)serial->parent.user_data;
  141. if(uart->hw_base == AUX_BASE)
  142. {
  143. while (!(AUX_MU_LSR_REG(uart->hw_base) & 0x20));
  144. AUX_MU_IO_REG(uart->hw_base) = c;
  145. }
  146. else
  147. {
  148. while ((PL011_REG_FR(uart->hw_base) & PL011_FR_TXFF));
  149. PL011_REG_DR(uart->hw_base) = (uint8_t)c;
  150. }
  151. return 1;
  152. }
  153. static int uart_getc(struct rt_serial_device *serial)
  154. {
  155. int ch = -1;
  156. struct hw_uart_device *uart;
  157. RT_ASSERT(serial != RT_NULL);
  158. uart = (struct hw_uart_device *)serial->parent.user_data;
  159. if(uart->hw_base == AUX_BASE)
  160. {
  161. if ((AUX_MU_LSR_REG(uart->hw_base) & 0x01))
  162. {
  163. ch = AUX_MU_IO_REG(uart->hw_base) & 0xff;
  164. }
  165. }
  166. else
  167. {
  168. if((PL011_REG_FR(uart->hw_base) & PL011_FR_RXFE) == 0)
  169. {
  170. ch = PL011_REG_DR(uart->hw_base) & 0xff;
  171. }
  172. }
  173. return ch;
  174. }
  175. static const struct rt_uart_ops _uart_ops =
  176. {
  177. uart_configure,
  178. uart_control,
  179. uart_putc,
  180. uart_getc,
  181. };
  182. #ifdef RT_USING_UART1
  183. static void rt_hw_aux_uart_isr(int irqno, void *param)
  184. {
  185. struct rt_serial_device *serial = (struct rt_serial_device*)param;
  186. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  187. }
  188. #endif
  189. static void rt_hw_uart_isr(int irqno, void *param)
  190. {
  191. #ifdef RT_USING_UART0
  192. if((PACTL_CS & IRQ_UART0) == IRQ_UART0)
  193. {
  194. PACTL_CS &= ~(IRQ_UART0);
  195. rt_hw_serial_isr(&_serial0, RT_SERIAL_EVENT_RX_IND);
  196. PL011_REG_ICR(UART0_BASE) = PL011_INTERRUPT_RECEIVE;
  197. }
  198. #endif
  199. #ifdef RT_USING_UART3
  200. if((PACTL_CS & IRQ_UART3) == IRQ_UART3)
  201. {
  202. PACTL_CS &= ~(IRQ_UART3);
  203. rt_hw_serial_isr(&_serial3, RT_SERIAL_EVENT_RX_IND);
  204. PL011_REG_ICR(UART3_BASE) = PL011_INTERRUPT_RECEIVE;
  205. }
  206. #endif
  207. #ifdef RT_USING_UART4
  208. if((PACTL_CS & IRQ_UART4) == IRQ_UART4)
  209. {
  210. PACTL_CS &= ~(IRQ_UART4);
  211. rt_hw_serial_isr(&_serial4, RT_SERIAL_EVENT_RX_IND);
  212. PL011_REG_ICR(UART4_BASE) = PL011_INTERRUPT_RECEIVE;
  213. }
  214. #endif
  215. #ifdef RT_USING_UART5
  216. if((PACTL_CS & IRQ_UART5) == IRQ_UART5)
  217. {
  218. PACTL_CS &= ~(IRQ_UART5);
  219. rt_hw_serial_isr(&_serial5, RT_SERIAL_EVENT_RX_IND);
  220. PL011_REG_ICR(UART5_BASE) = PL011_INTERRUPT_RECEIVE;
  221. }
  222. #endif
  223. }
  224. #ifdef RT_USING_UART0
  225. /* UART device driver structure */
  226. static struct hw_uart_device _uart0_device =
  227. {
  228. UART0_BASE,
  229. IRQ_PL011,
  230. };
  231. #endif
  232. #ifdef RT_USING_UART1
  233. /* UART device driver structure */
  234. static struct hw_uart_device _uart1_device =
  235. {
  236. AUX_BASE,
  237. IRQ_AUX_UART,
  238. };
  239. #endif
  240. #ifdef RT_USING_UART3
  241. static struct hw_uart_device _uart3_device =
  242. {
  243. UART3_BASE,
  244. IRQ_PL011,
  245. };
  246. #endif
  247. #ifdef RT_USING_UART4
  248. static struct hw_uart_device _uart4_device =
  249. {
  250. UART4_BASE,
  251. IRQ_PL011,
  252. };
  253. #endif
  254. #ifdef RT_USING_UART5
  255. static struct hw_uart_device _uart5_device =
  256. {
  257. UART5_BASE,
  258. IRQ_PL011,
  259. };
  260. #endif
  261. int rt_hw_uart_init(void)
  262. {
  263. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  264. #ifdef RT_USING_UART0
  265. struct hw_uart_device *uart0;
  266. uart0 = &_uart0_device;
  267. _serial0.ops = &_uart_ops;
  268. _serial0.config = config;
  269. /* register UART0 device */
  270. rt_hw_serial_register(&_serial0, "uart0",
  271. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  272. uart0);
  273. rt_hw_interrupt_install(uart0->irqno, rt_hw_uart_isr, &_serial0, "uart0");
  274. #endif
  275. #ifdef RT_USING_UART1
  276. struct hw_uart_device *uart1;
  277. uart1 = &_uart1_device;
  278. _serial1.ops = &_uart_ops;
  279. _serial1.config = config;
  280. /* register UART1 device */
  281. rt_hw_serial_register(&_serial1, "uart1",
  282. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  283. uart1);
  284. rt_hw_interrupt_install(uart1->irqno, rt_hw_aux_uart_isr, &_serial1, "uart1");
  285. #endif
  286. #ifdef RT_USING_UART3
  287. struct hw_uart_device *uart3;
  288. uart3 = &_uart3_device;
  289. _serial3.ops = &_uart_ops;
  290. _serial3.config = config;
  291. /* register UART3 device */
  292. rt_hw_serial_register(&_serial3, "uart3",
  293. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  294. uart3);
  295. rt_hw_interrupt_install(uart3->irqno, rt_hw_uart_isr, &_serial3, "uart3");
  296. #endif
  297. #ifdef RT_USING_UART4
  298. struct hw_uart_device *uart4;
  299. uart4 = &_uart4_device;
  300. _serial4.ops = &_uart_ops;
  301. _serial4.config = config;
  302. /* register UART4 device */
  303. rt_hw_serial_register(&_serial4, "uart4",
  304. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  305. uart4);
  306. rt_hw_interrupt_install(uart4->irqno, rt_hw_uart_isr, &_serial4, "uart4");
  307. #endif
  308. #ifdef RT_USING_UART5
  309. struct hw_uart_device *uart5;
  310. uart5 = &_uart5_device;
  311. _serial5.ops = &_uart_ops;
  312. _serial5.config = config;
  313. /* register UART5 device */
  314. rt_hw_serial_register(&_serial5, "uart5",
  315. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  316. uart5);
  317. rt_hw_interrupt_install(uart5->irqno, rt_hw_uart_isr, &_serial5, "uart5");
  318. #endif
  319. return 0;
  320. }