drv_uart.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #ifdef RT_USING_SERIAL
  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 BSP_USING_UART0
  88. struct lpc_uart uart0 =
  89. {
  90. LPC_UART0,
  91. UART0_IRQn,
  92. };
  93. struct rt_serial_device serial0;
  94. void UART0_IRQHandler(void)
  95. {
  96. volatile uint32_t IIR, tmp;
  97. /* enter interrupt */
  98. rt_interrupt_enter();
  99. IIR = LPC_UART0->IIR;
  100. IIR &= 0x0e;
  101. switch (IIR)
  102. {
  103. case 0x04:
  104. case 0x0C:
  105. rt_hw_serial_isr(&serial0, RT_SERIAL_EVENT_RX_IND);
  106. break;
  107. case 0x06:
  108. tmp = LPC_UART0->LSR;
  109. break;
  110. default :
  111. tmp = LPC_UART0->LSR;
  112. break;
  113. }
  114. /* leave interrupt */
  115. rt_interrupt_leave();
  116. }
  117. #endif
  118. #ifdef BSP_USING_UART2
  119. struct lpc_uart uart2 =
  120. {
  121. LPC_UART2,
  122. UART2_IRQn,
  123. };
  124. struct rt_serial_device serial2;
  125. void UART2_IRQHandler(void)
  126. {
  127. volatile uint32_t IIR, tmp;
  128. /* enter interrupt */
  129. rt_interrupt_enter();
  130. IIR = LPC_UART2->IIR;
  131. IIR &= 0x0e;
  132. switch (IIR)
  133. {
  134. case 0x04:
  135. case 0x0C:
  136. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  137. break;
  138. case 0x06:
  139. tmp = LPC_UART2->LSR;
  140. break;
  141. default :
  142. tmp = LPC_UART2->LSR;
  143. break;
  144. }
  145. /* leave interrupt */
  146. rt_interrupt_leave();
  147. }
  148. #endif
  149. int rt_hw_uart_init(void)
  150. {
  151. rt_err_t ret = RT_EOK;
  152. struct lpc_uart *uart;
  153. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  154. #ifdef BSP_USING_UART0
  155. uart = &uart0;
  156. serial0.ops = &lpc_uart_ops;
  157. serial0.config = config;
  158. serial0.parent.user_data = uart;
  159. /*
  160. * Initialize UART0 pin connect
  161. * P0.2: U0_TXD
  162. * P0.3: U0_RXD
  163. */
  164. LPC_IOCON->P0_2 &= ~0x07;
  165. LPC_IOCON->P0_2 |= 0x01;
  166. LPC_IOCON->P0_3 &= ~0x07;
  167. LPC_IOCON->P0_3 |= 0x01;
  168. /* enable the uart0 power and clock */
  169. LPC_SC->PCONP |= 0x01 << 3;
  170. /* preemption = 1, sub-priority = 1 */
  171. NVIC_SetPriority(uart->UART_IRQn, ((0x01 << 3) | 0x01));
  172. /* Enable Interrupt for UART channel */
  173. NVIC_EnableIRQ(uart->UART_IRQn);
  174. /* register UART0 device */
  175. ret = rt_hw_serial_register(&serial0, "uart0",
  176. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  177. uart);
  178. #endif
  179. #ifdef BSP_USING_UART2
  180. uart = &uart2;
  181. serial2.ops = &lpc_uart_ops;
  182. serial2.config = config;
  183. serial2.parent.user_data = uart;
  184. /*
  185. * Initialize UART2 pin connect
  186. * P2.8: U2_TXD
  187. * P0.11: U2_RXD
  188. */
  189. LPC_IOCON->P2_8 &= ~0x07;
  190. LPC_IOCON->P0_11 &= ~0x07;
  191. LPC_IOCON->P2_8 |= 0x02;
  192. LPC_IOCON->P0_11 |= 0x01;
  193. /* enable the uart2 power and clock */
  194. LPC_SC->PCONP |= 0x01 << 24;
  195. /* preemption = 1, sub-priority = 1 */
  196. NVIC_SetPriority(uart->UART_IRQn, ((0x01 << 3) | 0x01));
  197. /* Enable Interrupt for UART channel */
  198. NVIC_EnableIRQ(uart->UART_IRQn);
  199. /* register UART2 device */
  200. ret = rt_hw_serial_register(&serial2, "uart2",
  201. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  202. uart);
  203. #endif
  204. return ret;
  205. }
  206. INIT_BOARD_EXPORT(rt_hw_uart_init);
  207. #endif /* RT_USING_SERIAL */