drv_uart.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2013-05-18 Bernard The first version for LPC40xx
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "board.h"
  14. struct lpc_uart
  15. {
  16. LPC_UART_TypeDef *UART;
  17. IRQn_Type UART_IRQn;
  18. };
  19. static rt_err_t lpc_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  20. {
  21. struct lpc_uart *uart;
  22. uint32_t Fdiv = 0;
  23. RT_ASSERT(serial != RT_NULL);
  24. uart = (struct lpc_uart *)serial->parent.user_data;
  25. /* Initialize UART Configuration parameter structure to default state:
  26. * Baudrate = 115200 bps
  27. * 8 data bit
  28. * 1 Stop bit
  29. * None parity
  30. */
  31. /* set DLAB=1 */
  32. uart->UART->LCR |= 0x80;
  33. /* config uart baudrate */
  34. Fdiv = (PeripheralClock / 16) / cfg->baud_rate;
  35. uart->UART->DLM = Fdiv / 256;
  36. uart->UART->DLL = Fdiv % 256;
  37. /* set DLAB=0 */
  38. uart->UART->LCR &= ~0x80;
  39. /* config to 8 data bit,1 Stop bit,None parity */
  40. uart->UART->LCR |= 0x03;
  41. /*enable and reset FIFO*/
  42. uart->UART->FCR = 0x07;
  43. return RT_EOK;
  44. }
  45. static rt_err_t lpc_control(struct rt_serial_device *serial, int cmd, void *arg)
  46. {
  47. struct lpc_uart *uart;
  48. RT_ASSERT(serial != RT_NULL);
  49. uart = (struct lpc_uart *)serial->parent.user_data;
  50. switch (cmd)
  51. {
  52. case RT_DEVICE_CTRL_CLR_INT:
  53. /* disable rx irq */
  54. uart->UART->IER &= ~0x01;
  55. break;
  56. case RT_DEVICE_CTRL_SET_INT:
  57. /* enable rx irq */
  58. uart->UART->IER |= 0x01;
  59. break;
  60. }
  61. return RT_EOK;
  62. }
  63. static int lpc_putc(struct rt_serial_device *serial, char c)
  64. {
  65. struct lpc_uart *uart;
  66. uart = (struct lpc_uart *)serial->parent.user_data;
  67. while (!(uart->UART->LSR & 0x20));
  68. uart->UART->THR = c;
  69. return 1;
  70. }
  71. static int lpc_getc(struct rt_serial_device *serial)
  72. {
  73. struct lpc_uart *uart;
  74. uart = (struct lpc_uart *)serial->parent.user_data;
  75. if (uart->UART->LSR & 0x01)
  76. return (uart->UART->RBR);
  77. else
  78. return -1;
  79. }
  80. static const struct rt_uart_ops lpc_uart_ops =
  81. {
  82. lpc_configure,
  83. lpc_control,
  84. lpc_putc,
  85. lpc_getc,
  86. };
  87. #ifdef RT_USING_UART0
  88. /* UART0 device driver structure */
  89. #if RTTHREAD_VERSION < 20000 /* RT-Thread 1.x */
  90. struct serial_ringbuffer uart0_int_rx;
  91. #endif
  92. struct lpc_uart uart0 =
  93. {
  94. LPC_UART0,
  95. UART0_IRQn,
  96. };
  97. struct rt_serial_device serial0;
  98. void UART0_IRQHandler(void)
  99. {
  100. volatile uint32_t IIR, tmp;
  101. /* enter interrupt */
  102. rt_interrupt_enter();
  103. IIR = LPC_UART0->IIR;
  104. IIR &= 0x0e;
  105. switch (IIR)
  106. {
  107. case 0x04:
  108. case 0x0C:
  109. #if RTTHREAD_VERSION < 20000
  110. rt_hw_serial_isr(&serial0);
  111. #else
  112. rt_hw_serial_isr(&serial0, RT_SERIAL_EVENT_RX_IND);
  113. #endif
  114. break;
  115. case 0x06:
  116. tmp = LPC_UART0->LSR;
  117. break;
  118. default :
  119. tmp = LPC_UART0->LSR;
  120. break;
  121. }
  122. /* leave interrupt */
  123. rt_interrupt_leave();
  124. }
  125. #endif
  126. #ifdef RT_USING_UART2
  127. /* UART2 device driver structure */
  128. #if RTTHREAD_VERSION < 20000 /* RT-Thread 1.x */
  129. struct serial_ringbuffer uart2_int_rx;
  130. #endif
  131. struct lpc_uart uart2 =
  132. {
  133. LPC_UART2,
  134. UART2_IRQn,
  135. };
  136. struct rt_serial_device serial2;
  137. void UART2_IRQHandler(void)
  138. {
  139. volatile uint32_t IIR, tmp;
  140. /* enter interrupt */
  141. rt_interrupt_enter();
  142. IIR = LPC_UART2->IIR;
  143. IIR &= 0x0e;
  144. switch (IIR)
  145. {
  146. case 0x04:
  147. case 0x0C:
  148. #if RTTHREAD_VERSION < 20000
  149. rt_hw_serial_isr(&serial2);
  150. #else
  151. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  152. #endif
  153. break;
  154. case 0x06:
  155. tmp = LPC_UART2->LSR;
  156. break;
  157. default :
  158. tmp = LPC_UART2->LSR;
  159. break;
  160. }
  161. /* leave interrupt */
  162. rt_interrupt_leave();
  163. }
  164. #endif
  165. #ifdef RT_USING_UART4
  166. /* UART4 device driver structure */
  167. #if RTTHREAD_VERSION < 20000 /* RT-Thread 1.x */
  168. struct serial_ringbuffer uart4_int_rx;
  169. #endif
  170. struct lpc_uart uart4 =
  171. {
  172. LPC_UART4,
  173. UART4_IRQn,
  174. };
  175. struct rt_serial_device serial4;
  176. void UART4_IRQHandler(void)
  177. {
  178. volatile uint32_t IIR, tmp;
  179. /* enter interrupt */
  180. rt_interrupt_enter();
  181. IIR = LPC_UART4->IIR;
  182. IIR &= 0x0e;
  183. switch (IIR)
  184. {
  185. case 0x04:
  186. case 0x0C:
  187. #if RTTHREAD_VERSION < 20000
  188. rt_hw_serial_isr(&serial4);
  189. #else
  190. rt_hw_serial_isr(&serial4, RT_SERIAL_EVENT_RX_IND);
  191. #endif
  192. break;
  193. case 0x06:
  194. tmp = LPC_UART4->LSR;
  195. break;
  196. default :
  197. tmp = LPC_UART4->LSR;
  198. break;
  199. }
  200. /* leave interrupt */
  201. rt_interrupt_leave();
  202. }
  203. #endif
  204. void rt_hw_uart_init(void)
  205. {
  206. struct lpc_uart *uart;
  207. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  208. #ifdef RT_USING_UART0
  209. uart = &uart0;
  210. serial0.ops = &lpc_uart_ops;
  211. serial0.config = config;
  212. #if RTTHREAD_VERSION < 20000
  213. serial0.int_rx = &uart0_int_rx;
  214. #endif
  215. serial0.parent.user_data = uart;
  216. /*
  217. * Initialize UART0 pin connect
  218. * P0.2: U0_TXD
  219. * P0.3: U0_RXD
  220. */
  221. LPC_IOCON->P0_2 &= ~0x07;
  222. LPC_IOCON->P0_2 |= 0x01;
  223. LPC_IOCON->P0_3 &= ~0x07;
  224. LPC_IOCON->P0_3 |= 0x01;
  225. /* enable the uart0 power and clock */
  226. LPC_SC->PCONP |= 0x01 << 3;
  227. /* preemption = 1, sub-priority = 1 */
  228. NVIC_SetPriority(uart->UART_IRQn, ((0x01 << 3) | 0x01));
  229. /* Enable Interrupt for UART channel */
  230. NVIC_EnableIRQ(uart->UART_IRQn);
  231. /* register UART0 device */
  232. rt_hw_serial_register(&serial0, "uart0",
  233. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  234. uart);
  235. #endif
  236. #ifdef RT_USING_UART2
  237. uart = &uart2;
  238. serial2.ops = &lpc_uart_ops;
  239. serial2.config = config;
  240. #if RTTHREAD_VERSION < 20000
  241. serial2.int_rx = &uart2_int_rx;
  242. #endif
  243. serial2.parent.user_data = uart;
  244. /*
  245. * Initialize UART2 pin connect
  246. * P2.8: U2_TXD
  247. * P0.11: U2_RXD
  248. */
  249. LPC_IOCON->P2_8 &= ~0x07;
  250. LPC_IOCON->P0_11 &= ~0x07;
  251. LPC_IOCON->P2_8 |= 0x02;
  252. LPC_IOCON->P0_11 |= 0x01;
  253. /* enable the uart2 power and clock */
  254. LPC_SC->PCONP |= 0x01 << 24;
  255. /* preemption = 1, sub-priority = 1 */
  256. NVIC_SetPriority(uart->UART_IRQn, ((0x01 << 3) | 0x01));
  257. /* Enable Interrupt for UART channel */
  258. NVIC_EnableIRQ(uart->UART_IRQn);
  259. /* register UART2 device */
  260. rt_hw_serial_register(&serial2, "uart2",
  261. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  262. uart);
  263. #endif
  264. #ifdef RT_USING_UART4
  265. uart = &uart4;
  266. serial4.ops = &lpc_uart_ops;
  267. serial4.config = config;
  268. #if RTTHREAD_VERSION < 20000
  269. serial4.int_rx = &uart4_int_rx;
  270. #endif
  271. serial4.parent.user_data = uart;
  272. /*
  273. * Initialize UART2 pin connect
  274. * P5.4: U2_TXD
  275. * P5.3: U2_RXD
  276. */
  277. LPC_IOCON->P5_4 &= ~0x07;
  278. LPC_IOCON->P5_3 &= ~0x07;
  279. LPC_IOCON->P5_4 |= 0x04;
  280. LPC_IOCON->P5_3 |= 0x04;
  281. /* enable the uart4 power and clock */
  282. LPC_SC->PCONP |= 0x01 << 8;
  283. /* preemption = 1, sub-priority = 1 */
  284. NVIC_SetPriority(uart->UART_IRQn, ((0x01 << 3) | 0x01));
  285. /* Enable Interrupt for UART channel */
  286. NVIC_EnableIRQ(uart->UART_IRQn);
  287. /* register UART2 device */
  288. rt_hw_serial_register(&serial4, "uart4",
  289. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  290. uart);
  291. #endif
  292. }