uart.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "board.h"
  18. #include "LPC177x_8x.h"
  19. #include "lpc177x_8x_uart.h"
  20. #include "lpc177x_8x_pinsel.h"
  21. /**
  22. * @addtogroup LPC11xx
  23. */
  24. /*@{*/
  25. struct rt_uart_lpc
  26. {
  27. struct rt_device parent;
  28. LPC_UART_TypeDef * UART;
  29. IRQn_Type UART_IRQn;
  30. /* buffer for reception */
  31. rt_uint8_t read_index, save_index;
  32. rt_uint8_t rx_buffer[RT_UART_RX_BUFFER_SIZE];
  33. };
  34. #ifdef RT_USING_UART0
  35. struct rt_uart_lpc uart0_device;
  36. #endif
  37. #ifdef RT_USING_UART1
  38. struct rt_uart_lpc uart1_device;
  39. #endif
  40. #ifdef RT_USING_UART0
  41. void UART0_IRQHandler(void)
  42. {
  43. rt_ubase_t level, iir;
  44. struct rt_uart_lpc* uart = &uart0_device;
  45. /* enter interrupt */
  46. rt_interrupt_enter();
  47. /* read IIR and clear it */
  48. iir = uart->UART->IIR;
  49. if (iir == UART_IIR_INTID_RDA) /* Receive Data Available */
  50. {
  51. /* Receive Data Available */
  52. uart->rx_buffer[uart->save_index] = uart->UART->RBR;
  53. level = rt_hw_interrupt_disable();
  54. uart->save_index ++;
  55. if (uart->save_index >= RT_UART_RX_BUFFER_SIZE)
  56. uart->save_index = 0;
  57. rt_hw_interrupt_enable(level);
  58. /* invoke callback */
  59. if(uart->parent.rx_indicate != RT_NULL)
  60. {
  61. rt_size_t length;
  62. if (uart->read_index > uart->save_index)
  63. length = RT_UART_RX_BUFFER_SIZE - uart->read_index + uart->save_index;
  64. else
  65. length = uart->save_index - uart->read_index;
  66. uart->parent.rx_indicate(&uart->parent, length);
  67. }
  68. }
  69. /* leave interrupt */
  70. rt_interrupt_leave();
  71. return;
  72. }
  73. #endif
  74. #ifdef RT_USING_UART1
  75. void UART1_IRQHandler(void)
  76. {
  77. rt_ubase_t level, iir;
  78. struct rt_uart_lpc* uart = &uart1_device;
  79. /* enter interrupt */
  80. rt_interrupt_enter();
  81. /* read IIR and clear it */
  82. iir = uart->UART->IIR;
  83. if (iir == UART_IIR_INTID_RDA) /* Receive Data Available */
  84. {
  85. /* Receive Data Available */
  86. uart->rx_buffer[uart->save_index] = uart->UART->RBR;
  87. level = rt_hw_interrupt_disable();
  88. uart->save_index ++;
  89. if (uart->save_index >= RT_UART_RX_BUFFER_SIZE)
  90. uart->save_index = 0;
  91. rt_hw_interrupt_enable(level);
  92. /* invoke callback */
  93. if(uart->parent.rx_indicate != RT_NULL)
  94. {
  95. rt_size_t length;
  96. if (uart->read_index > uart->save_index)
  97. length = RT_UART_RX_BUFFER_SIZE - uart->read_index + uart->save_index;
  98. else
  99. length = uart->save_index - uart->read_index;
  100. uart->parent.rx_indicate(&uart->parent, length);
  101. }
  102. }
  103. /* leave interrupt */
  104. rt_interrupt_leave();
  105. return;
  106. }
  107. #endif
  108. static rt_err_t rt_uart_init (rt_device_t dev)
  109. {
  110. struct rt_uart_lpc *uart = (struct rt_uart_lpc*)dev;
  111. UART_CFG_Type UART_ConfigStruct;
  112. #ifdef RT_USING_UART0
  113. if( uart->UART == LPC_UART0 )
  114. {
  115. /*
  116. * Initialize UART0 pin connect
  117. * P0.2: TXD
  118. * P0.3: RXD
  119. */
  120. PINSEL_ConfigPin(0, 2, 1);
  121. PINSEL_ConfigPin(0, 3, 1);
  122. UART_ConfigStruct.Baud_rate = 115200;
  123. UART_ConfigStruct.Databits = UART_DATABIT_8;
  124. UART_ConfigStruct.Parity = UART_PARITY_NONE;
  125. UART_ConfigStruct.Stopbits = UART_STOPBIT_1;
  126. UART_Init( uart->UART, &UART_ConfigStruct);
  127. // Enable UART Transmit
  128. UART_TxCmd( uart->UART, ENABLE);
  129. UART_IntConfig( uart->UART, UART_INTCFG_RBR, ENABLE);
  130. }
  131. #endif
  132. #ifdef RT_USING_UART1
  133. if( ((LPC_UART1_TypeDef *)uart->UART) == LPC_UART1 )
  134. {
  135. /*
  136. * Initialize UART1 pin connect
  137. * P3.16: TXD
  138. * P3.17: RXD
  139. */
  140. PINSEL_ConfigPin(3, 16, 3);
  141. PINSEL_ConfigPin(3, 17, 3);
  142. UART_ConfigStruct.Baud_rate = 115200;
  143. UART_ConfigStruct.Databits = UART_DATABIT_8;
  144. UART_ConfigStruct.Parity = UART_PARITY_NONE;
  145. UART_ConfigStruct.Stopbits = UART_STOPBIT_1;
  146. UART_Init( uart->UART,&UART_ConfigStruct);
  147. // Enable UART Transmit
  148. UART_TxCmd( uart->UART, ENABLE);
  149. UART_IntConfig( uart->UART, UART_INTCFG_RBR, ENABLE);
  150. }
  151. #endif
  152. #ifdef RT_USING_UART2
  153. if( uart->UART == LPC_UART2 )
  154. {
  155. }
  156. #endif
  157. return RT_EOK;
  158. }
  159. static rt_err_t rt_uart_open(rt_device_t dev, rt_uint16_t oflag)
  160. {
  161. struct rt_uart_lpc *uart = (struct rt_uart_lpc*)dev;
  162. RT_ASSERT(dev != RT_NULL);
  163. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  164. {
  165. /* Enable the UART Interrupt */
  166. NVIC_EnableIRQ( uart->UART_IRQn );
  167. }
  168. return RT_EOK;
  169. }
  170. static rt_err_t rt_uart_close(rt_device_t dev)
  171. {
  172. struct rt_uart_lpc *uart = (struct rt_uart_lpc*)dev;
  173. RT_ASSERT(dev != RT_NULL);
  174. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  175. {
  176. /* Disable the UART Interrupt */
  177. NVIC_DisableIRQ( uart->UART_IRQn );
  178. }
  179. return RT_EOK;
  180. }
  181. static rt_size_t rt_uart_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  182. {
  183. rt_uint8_t* ptr;
  184. struct rt_uart_lpc *uart = (struct rt_uart_lpc*)dev;
  185. RT_ASSERT(uart != RT_NULL);
  186. /* point to buffer */
  187. ptr = (rt_uint8_t*) buffer;
  188. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  189. {
  190. while (size)
  191. {
  192. /* interrupt receive */
  193. rt_base_t level;
  194. /* disable interrupt */
  195. level = rt_hw_interrupt_disable();
  196. if (uart->read_index != uart->save_index)
  197. {
  198. *ptr = uart->rx_buffer[uart->read_index];
  199. uart->read_index ++;
  200. if (uart->read_index >= RT_UART_RX_BUFFER_SIZE)
  201. uart->read_index = 0;
  202. }
  203. else
  204. {
  205. /* no data in rx buffer */
  206. /* enable interrupt */
  207. rt_hw_interrupt_enable(level);
  208. break;
  209. }
  210. /* enable interrupt */
  211. rt_hw_interrupt_enable(level);
  212. ptr ++;
  213. size --;
  214. }
  215. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  216. }
  217. return 0;
  218. }
  219. static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  220. {
  221. struct rt_uart_lpc *uart = (struct rt_uart_lpc*)dev;
  222. char *ptr;
  223. ptr = (char*)buffer;
  224. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  225. {
  226. /* stream mode */
  227. while (size)
  228. {
  229. if (*ptr == '\n')
  230. {
  231. while (!(uart->UART->LSR & UART_LSR_THRE));
  232. UART_SendByte( uart->UART,'\r');
  233. }
  234. while (!(uart->UART->LSR & UART_LSR_THRE));
  235. UART_SendByte( uart->UART,*ptr);
  236. ptr ++;
  237. size --;
  238. }
  239. }
  240. else
  241. {
  242. UART_Send( uart->UART, (uint8_t *)buffer, size, BLOCKING);
  243. }
  244. return (rt_size_t) ptr - (rt_size_t) buffer;
  245. }
  246. void rt_hw_uart_init(void)
  247. {
  248. struct rt_uart_lpc* uart;
  249. #ifdef RT_USING_UART0
  250. /* get uart device */
  251. uart = &uart0_device;
  252. uart0_device.UART = LPC_UART0;
  253. uart0_device.UART_IRQn = UART0_IRQn;
  254. /* device initialization */
  255. uart->parent.type = RT_Device_Class_Char;
  256. rt_memset(uart->rx_buffer, 0, sizeof(uart->rx_buffer));
  257. uart->read_index = uart->save_index = 0;
  258. /* device interface */
  259. uart->parent.init = rt_uart_init;
  260. uart->parent.open = rt_uart_open;
  261. uart->parent.close = rt_uart_close;
  262. uart->parent.read = rt_uart_read;
  263. uart->parent.write = rt_uart_write;
  264. uart->parent.control = RT_NULL;
  265. uart->parent.user_data = RT_NULL;
  266. rt_device_register(&uart->parent,
  267. "uart0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX);
  268. #endif
  269. #ifdef RT_USING_UART1
  270. /* get uart device */
  271. uart = &uart1_device;
  272. uart1_device.UART = (LPC_UART_TypeDef *)LPC_UART1;
  273. uart1_device.UART_IRQn = UART1_IRQn;
  274. /* device initialization */
  275. uart->parent.type = RT_Device_Class_Char;
  276. rt_memset(uart->rx_buffer, 0, sizeof(uart->rx_buffer));
  277. uart->read_index = uart->save_index = 0;
  278. /* device interface */
  279. uart->parent.init = rt_uart_init;
  280. uart->parent.open = rt_uart_open;
  281. uart->parent.close = rt_uart_close;
  282. uart->parent.read = rt_uart_read;
  283. uart->parent.write = rt_uart_write;
  284. uart->parent.control = RT_NULL;
  285. uart->parent.user_data = RT_NULL;
  286. rt_device_register(&uart->parent,
  287. "uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX);
  288. #endif
  289. }
  290. /*@}*/