uart.c 7.5 KB

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