1
0

drv_uart.c 8.9 KB

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