uart.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. * 2010-03-08 Bernard The first version for LPC17xx
  9. * 2010-05-02 Aozima update CMSIS to 130
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include "LPC17xx.h"
  14. #define IER_RBR 0x01
  15. #define IER_THRE 0x02
  16. #define IER_RLS 0x04
  17. #define IIR_PEND 0x01
  18. #define IIR_RLS 0x03
  19. #define IIR_RDA 0x02
  20. #define IIR_CTI 0x06
  21. #define IIR_THRE 0x01
  22. #define LSR_RDR 0x01
  23. #define LSR_OE 0x02
  24. #define LSR_PE 0x04
  25. #define LSR_FE 0x08
  26. #define LSR_BI 0x10
  27. #define LSR_THRE 0x20
  28. #define LSR_TEMT 0x40
  29. #define LSR_RXFE 0x80
  30. /**
  31. * @addtogroup LPC176x
  32. */
  33. /*@{*/
  34. #if defined(RT_USING_UART0) && defined(RT_USING_DEVICE)
  35. #define UART_BAUDRATE 115200
  36. #define LPC_UART LPC_UART0
  37. #define UART_IRQn UART0_IRQn
  38. struct rt_uart_lpc
  39. {
  40. struct rt_device parent;
  41. /* buffer for reception */
  42. rt_uint8_t read_index, save_index;
  43. rt_uint8_t rx_buffer[RT_UART_RX_BUFFER_SIZE];
  44. } uart_device;
  45. void UART0_IRQHandler(void)
  46. {
  47. rt_ubase_t level, iir;
  48. struct rt_uart_lpc *uart = &uart_device;
  49. /* enter interrupt */
  50. rt_interrupt_enter();
  51. /* read IIR and clear it */
  52. iir = LPC_UART->IIR;
  53. iir >>= 1; /* skip pending bit in IIR */
  54. iir &= 0x07; /* check bit 1~3, interrupt identification */
  55. if (iir == IIR_RDA) /* Receive Data Available */
  56. {
  57. /* Receive Data Available */
  58. uart->rx_buffer[uart->save_index] = LPC_UART->RBR;
  59. level = rt_hw_interrupt_disable();
  60. uart->save_index ++;
  61. if (uart->save_index >= RT_UART_RX_BUFFER_SIZE)
  62. uart->save_index = 0;
  63. rt_hw_interrupt_enable(level);
  64. /* invoke callback */
  65. if (uart->parent.rx_indicate != RT_NULL)
  66. {
  67. rt_size_t length;
  68. if (uart->read_index > uart->save_index)
  69. length = RT_UART_RX_BUFFER_SIZE - uart->read_index + uart->save_index;
  70. else
  71. length = uart->save_index - uart->read_index;
  72. uart->parent.rx_indicate(&uart->parent, length);
  73. }
  74. }
  75. else if (iir == IIR_RLS)
  76. {
  77. iir = LPC_UART->LSR; //oe pe fe oe read for clear interrupt
  78. }
  79. /* leave interrupt */
  80. rt_interrupt_leave();
  81. return;
  82. }
  83. static rt_err_t rt_uart_init(rt_device_t dev)
  84. {
  85. rt_uint32_t Fdiv;
  86. rt_uint32_t pclkdiv, pclk;
  87. /* Init UART Hardware */
  88. if (LPC_UART == LPC_UART0)
  89. {
  90. LPC_PINCON->PINSEL0 &= ~0x000000F0;
  91. LPC_PINCON->PINSEL0 |= 0x00000050; /* RxD0 is P0.3 and TxD0 is P0.2 */
  92. /* By default, the PCLKSELx value is zero, thus, the PCLK for
  93. all the peripherals is 1/4 of the SystemFrequency. */
  94. /* Bit 6~7 is for UART0 */
  95. pclkdiv = (LPC_SC->PCLKSEL0 >> 6) & 0x03;
  96. switch (pclkdiv)
  97. {
  98. case 0x00:
  99. default:
  100. pclk = SystemCoreClock / 4;
  101. break;
  102. case 0x01:
  103. pclk = SystemCoreClock;
  104. break;
  105. case 0x02:
  106. pclk = SystemCoreClock / 2;
  107. break;
  108. case 0x03:
  109. pclk = SystemCoreClock / 8;
  110. break;
  111. }
  112. LPC_UART0->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
  113. Fdiv = (pclk / 16) / UART_BAUDRATE; /*baud rate */
  114. LPC_UART0->DLM = Fdiv / 256;
  115. LPC_UART0->DLL = Fdiv % 256;
  116. LPC_UART0->LCR = 0x03; /* DLAB = 0 */
  117. LPC_UART0->FCR = 0x07; /* Enable and reset TX and RX FIFO. */
  118. }
  119. else if ((LPC_UART1_TypeDef *)LPC_UART == LPC_UART1)
  120. {
  121. LPC_PINCON->PINSEL4 &= ~0x0000000F;
  122. LPC_PINCON->PINSEL4 |= 0x0000000A; /* Enable RxD1 P2.1, TxD1 P2.0 */
  123. /* By default, the PCLKSELx value is zero, thus, the PCLK for
  124. all the peripherals is 1/4 of the SystemFrequency. */
  125. /* Bit 8,9 are for UART1 */
  126. pclkdiv = (LPC_SC->PCLKSEL0 >> 8) & 0x03;
  127. switch (pclkdiv)
  128. {
  129. case 0x00:
  130. default:
  131. pclk = SystemCoreClock / 4;
  132. break;
  133. case 0x01:
  134. pclk = SystemCoreClock;
  135. break;
  136. case 0x02:
  137. pclk = SystemCoreClock / 2;
  138. break;
  139. case 0x03:
  140. pclk = SystemCoreClock / 8;
  141. break;
  142. }
  143. LPC_UART1->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
  144. Fdiv = (pclk / 16) / UART_BAUDRATE ; /*baud rate */
  145. LPC_UART1->DLM = Fdiv / 256;
  146. LPC_UART1->DLL = Fdiv % 256;
  147. LPC_UART1->LCR = 0x03; /* DLAB = 0 */
  148. LPC_UART1->FCR = 0x07; /* Enable and reset TX and RX FIFO. */
  149. }
  150. /* Ensure a clean start, no data in either TX or RX FIFO. */
  151. while ((LPC_UART->LSR & (LSR_THRE | LSR_TEMT)) != (LSR_THRE | LSR_TEMT));
  152. while (LPC_UART->LSR & LSR_RDR)
  153. {
  154. Fdiv = LPC_UART->RBR; /* Dump data from RX FIFO */
  155. }
  156. LPC_UART->IER = IER_RBR | IER_THRE | IER_RLS; /* Enable UART interrupt */
  157. return RT_EOK;
  158. }
  159. static rt_err_t rt_uart_open(rt_device_t dev, rt_uint16_t oflag)
  160. {
  161. RT_ASSERT(dev != RT_NULL);
  162. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  163. {
  164. /* Enable the UART Interrupt */
  165. NVIC_EnableIRQ(UART_IRQn);
  166. }
  167. return RT_EOK;
  168. }
  169. static rt_err_t rt_uart_close(rt_device_t dev)
  170. {
  171. RT_ASSERT(dev != RT_NULL);
  172. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  173. {
  174. /* Disable the UART Interrupt */
  175. NVIC_DisableIRQ(UART_IRQn);
  176. }
  177. return RT_EOK;
  178. }
  179. static rt_ssize_t rt_uart_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  180. {
  181. rt_uint8_t *ptr;
  182. struct rt_uart_lpc *uart = (struct rt_uart_lpc *)dev;
  183. RT_ASSERT(uart != RT_NULL);
  184. /* point to buffer */
  185. ptr = (rt_uint8_t *) buffer;
  186. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  187. {
  188. while (size)
  189. {
  190. /* interrupt receive */
  191. rt_base_t level;
  192. /* disable interrupt */
  193. level = rt_hw_interrupt_disable();
  194. if (uart->read_index != uart->save_index)
  195. {
  196. *ptr = uart->rx_buffer[uart->read_index];
  197. uart->read_index ++;
  198. if (uart->read_index >= RT_UART_RX_BUFFER_SIZE)
  199. uart->read_index = 0;
  200. }
  201. else
  202. {
  203. /* no data in rx buffer */
  204. /* enable interrupt */
  205. rt_hw_interrupt_enable(level);
  206. break;
  207. }
  208. /* enable interrupt */
  209. rt_hw_interrupt_enable(level);
  210. ptr ++;
  211. size --;
  212. }
  213. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  214. }
  215. return 0;
  216. }
  217. static rt_ssize_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  218. {
  219. char *ptr;
  220. ptr = (char *)buffer;
  221. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  222. {
  223. /* stream mode */
  224. while (size)
  225. {
  226. if (*ptr == '\n')
  227. {
  228. /* THRE status, contain valid data */
  229. while (!(LPC_UART->LSR & LSR_THRE));
  230. /* write data */
  231. LPC_UART->THR = '\r';
  232. }
  233. /* THRE status, contain valid data */
  234. while (!(LPC_UART->LSR & LSR_THRE));
  235. /* write data */
  236. LPC_UART->THR = *ptr;
  237. ptr ++;
  238. size --;
  239. }
  240. }
  241. else
  242. {
  243. while (size != 0)
  244. {
  245. /* THRE status, contain valid data */
  246. while (!(LPC_UART->LSR & LSR_THRE));
  247. /* write data */
  248. LPC_UART->THR = *ptr;
  249. ptr++;
  250. size--;
  251. }
  252. }
  253. return (rt_size_t) ptr - (rt_size_t) buffer;
  254. }
  255. void rt_hw_uart_init(void)
  256. {
  257. struct rt_uart_lpc *uart;
  258. /* get uart device */
  259. uart = &uart_device;
  260. /* device initialization */
  261. uart->parent.type = RT_Device_Class_Char;
  262. rt_memset(uart->rx_buffer, 0, sizeof(uart->rx_buffer));
  263. uart->read_index = uart->save_index = 0;
  264. /* device interface */
  265. uart->parent.init = rt_uart_init;
  266. uart->parent.open = rt_uart_open;
  267. uart->parent.close = rt_uart_close;
  268. uart->parent.read = rt_uart_read;
  269. uart->parent.write = rt_uart_write;
  270. uart->parent.control = RT_NULL;
  271. uart->parent.user_data = RT_NULL;
  272. rt_device_register(&uart->parent,
  273. "uart0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX);
  274. }
  275. #endif /* end of UART */
  276. /*@}*/