1
0

uart.c 8.6 KB

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