uart.c 7.3 KB

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