serial.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * File : serial.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development 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://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-08-23 Bernard first version
  13. */
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include "lpc214x.h"
  17. #include "board.h"
  18. /* serial hardware register */
  19. #define REG8(d) (*((volatile unsigned char *)(d)))
  20. #define REG32(d) (*((volatile unsigned long *)(d)))
  21. #define UART_RBR(base) REG8(base + 0x00)
  22. #define UART_THR(base) REG8(base + 0x00)
  23. #define UART_IER(base) REG32(base + 0x04)
  24. #define UART_IIR(base) REG32(base + 0x08)
  25. #define UART_FCR(base) REG8(base + 0x08)
  26. #define UART_LCR(base) REG8(base + 0x0C)
  27. #define UART_MCR(base) REG8(base + 0x10)
  28. #define UART_LSR(base) REG8(base + 0x14)
  29. #define UART_MSR(base) REG8(base + 0x18)
  30. #define UART_SCR(base) REG8(base + 0x1C)
  31. #define UART_DLL(base) REG8(base + 0x00)
  32. #define UART_DLM(base) REG8(base + 0x04)
  33. #define UART_ACR(base) REG32(base + 0x20)
  34. #define UART_FDR(base) REG32(base + 0x28)
  35. #define UART_TER(base) REG8(base + 0x30)
  36. /* LPC serial device */
  37. struct rt_lpcserial
  38. {
  39. /* inherit from device */
  40. struct rt_device parent;
  41. rt_uint32_t hw_base;
  42. rt_uint32_t irqno;
  43. rt_uint32_t baudrate;
  44. /* reception field */
  45. rt_uint16_t save_index, read_index;
  46. rt_uint8_t rx_buffer[RT_UART_RX_BUFFER_SIZE];
  47. };
  48. #ifdef RT_USING_UART1
  49. struct rt_lpcserial serial1;
  50. #endif
  51. #ifdef RT_USING_UART2
  52. struct rt_lpcserial serial2;
  53. #endif
  54. void rt_hw_serial_init(void);
  55. #define U0PINS 0x00000005
  56. void rt_hw_uart_isr(struct rt_lpcserial* lpc_serial)
  57. {
  58. RT_UNUSED rt_uint32_t iir;
  59. RT_ASSERT(lpc_serial != RT_NULL)
  60. if (UART_LSR(lpc_serial->hw_base) & 0x01)
  61. {
  62. rt_base_t level;
  63. while (UART_LSR(lpc_serial->hw_base) & 0x01)
  64. {
  65. /* disable interrupt */
  66. level = rt_hw_interrupt_disable();
  67. /* read character */
  68. lpc_serial->rx_buffer[lpc_serial->save_index] =
  69. UART_RBR(lpc_serial->hw_base);
  70. lpc_serial->save_index ++;
  71. if (lpc_serial->save_index >= RT_UART_RX_BUFFER_SIZE)
  72. lpc_serial->save_index = 0;
  73. /* if the next position is read index, discard this 'read char' */
  74. if (lpc_serial->save_index == lpc_serial->read_index)
  75. {
  76. lpc_serial->read_index ++;
  77. if (lpc_serial->read_index >= RT_UART_RX_BUFFER_SIZE)
  78. lpc_serial->read_index = 0;
  79. }
  80. /* enable interrupt */
  81. rt_hw_interrupt_enable(level);
  82. }
  83. /* invoke callback */
  84. if(lpc_serial->parent.rx_indicate != RT_NULL)
  85. {
  86. lpc_serial->parent.rx_indicate(&lpc_serial->parent, 1);
  87. }
  88. }
  89. /* clear interrupt source */
  90. iir = UART_IIR(lpc_serial->hw_base);
  91. /* acknowledge Interrupt */
  92. VICVectAddr = 0;
  93. }
  94. #ifdef RT_USING_UART1
  95. void rt_hw_uart_isr_1(int irqno, void *param)
  96. {
  97. /* get lpc serial device */
  98. rt_hw_uart_isr(&serial1);
  99. }
  100. #endif
  101. #ifdef RT_USING_UART2
  102. void rt_hw_uart_isr_2(int irqno, void *param)
  103. {
  104. /* get lpc serial device */
  105. rt_hw_uart_isr(&serial2);
  106. }
  107. #endif
  108. /**
  109. * @addtogroup LPC214x
  110. */
  111. /*@{*/
  112. static rt_err_t rt_serial_init (rt_device_t dev)
  113. {
  114. return RT_EOK;
  115. }
  116. static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag)
  117. {
  118. struct rt_lpcserial* lpc_serial;
  119. lpc_serial = (struct rt_lpcserial*) dev;
  120. RT_ASSERT(lpc_serial != RT_NULL);
  121. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  122. {
  123. /* init UART rx interrupt */
  124. UART_IER(lpc_serial->hw_base) = 0x01;
  125. /* install ISR */
  126. if (lpc_serial->irqno == UART0_INT)
  127. {
  128. #ifdef RT_USING_UART1
  129. rt_hw_interrupt_install(lpc_serial->irqno,
  130. rt_hw_uart_isr_1, &serial1, "UART1");
  131. #endif
  132. }
  133. else
  134. {
  135. #ifdef RT_USING_UART2
  136. rt_hw_interrupt_install(lpc_serial->irqno,
  137. rt_hw_uart_isr_2, &serial2, "UART2");
  138. #endif
  139. }
  140. rt_hw_interrupt_umask(lpc_serial->irqno);
  141. }
  142. return RT_EOK;
  143. }
  144. static rt_err_t rt_serial_close(rt_device_t dev)
  145. {
  146. struct rt_lpcserial* lpc_serial;
  147. lpc_serial = (struct rt_lpcserial*) dev;
  148. RT_ASSERT(lpc_serial != RT_NULL);
  149. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  150. {
  151. /* disable UART rx interrupt */
  152. UART_IER(lpc_serial->hw_base) = 0x00;
  153. }
  154. return RT_EOK;
  155. }
  156. static rt_err_t rt_serial_control(rt_device_t dev, int cmd, void *args)
  157. {
  158. return RT_EOK;
  159. }
  160. static rt_size_t rt_serial_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  161. {
  162. rt_uint8_t* ptr;
  163. struct rt_lpcserial *lpc_serial = (struct rt_lpcserial*)dev;
  164. RT_ASSERT(lpc_serial != RT_NULL);
  165. /* point to buffer */
  166. ptr = (rt_uint8_t*) buffer;
  167. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  168. {
  169. while (size)
  170. {
  171. /* interrupt receive */
  172. rt_base_t level;
  173. /* disable interrupt */
  174. level = rt_hw_interrupt_disable();
  175. if (lpc_serial->read_index != lpc_serial->save_index)
  176. {
  177. *ptr = lpc_serial->rx_buffer[lpc_serial->read_index];
  178. lpc_serial->read_index ++;
  179. if (lpc_serial->read_index >= RT_UART_RX_BUFFER_SIZE)
  180. lpc_serial->read_index = 0;
  181. }
  182. else
  183. {
  184. /* no data in rx buffer */
  185. /* enable interrupt */
  186. rt_hw_interrupt_enable(level);
  187. break;
  188. }
  189. /* enable interrupt */
  190. rt_hw_interrupt_enable(level);
  191. ptr ++; size --;
  192. }
  193. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  194. }
  195. else if (dev->flag & RT_DEVICE_FLAG_DMA_RX)
  196. {
  197. /* not support right now */
  198. RT_ASSERT(0);
  199. }
  200. /* polling mode */
  201. while (size && (UART_LSR(lpc_serial->hw_base) & 0x01))
  202. {
  203. /* Read Character */
  204. *ptr = UART_RBR(lpc_serial->hw_base);
  205. ptr ++;
  206. size --;
  207. }
  208. return (rt_size_t)ptr - (rt_size_t)buffer;
  209. }
  210. static rt_size_t rt_serial_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  211. {
  212. struct rt_lpcserial* lpc_serial;
  213. char *ptr;
  214. lpc_serial = (struct rt_lpcserial*) dev;
  215. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  216. {
  217. /* not support */
  218. RT_ASSERT(0);
  219. }
  220. else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  221. {
  222. /* not support */
  223. RT_ASSERT(0);
  224. }
  225. /* polling write */
  226. ptr = (char *)buffer;
  227. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  228. {
  229. /* stream mode */
  230. while (size)
  231. {
  232. if (*ptr == '\n')
  233. {
  234. while (!(UART_LSR(lpc_serial->hw_base) & 0x20));
  235. UART_THR(lpc_serial->hw_base) = '\r';
  236. }
  237. while (!(UART_LSR(lpc_serial->hw_base) & 0x20));
  238. UART_THR(lpc_serial->hw_base) = *ptr;
  239. ptr ++;
  240. size --;
  241. }
  242. }
  243. else
  244. {
  245. while (size)
  246. {
  247. while (!(UART_LSR(lpc_serial->hw_base) & 0x20));
  248. UART_THR(lpc_serial->hw_base) = *ptr;
  249. ptr ++;
  250. size --;
  251. }
  252. }
  253. return (rt_size_t) ptr - (rt_size_t) buffer;
  254. }
  255. void rt_hw_serial_init(void)
  256. {
  257. struct rt_lpcserial* lpc_serial;
  258. #ifdef RT_USING_UART1
  259. lpc_serial = &serial1;
  260. lpc_serial->parent.type = RT_Device_Class_Char;
  261. lpc_serial->hw_base = 0xE000C000;
  262. lpc_serial->baudrate = 115200;
  263. lpc_serial->irqno = UART0_INT;
  264. rt_memset(lpc_serial->rx_buffer, 0, sizeof(lpc_serial->rx_buffer));
  265. lpc_serial->read_index = lpc_serial->save_index = 0;
  266. /* Enable UART0 RxD and TxD pins */
  267. PINSEL0 |= 0x05;
  268. /* 8 bits, no Parity, 1 Stop bit */
  269. UART_LCR(lpc_serial->hw_base) = 0x83;
  270. /* Setup Baudrate */
  271. UART_DLL(lpc_serial->hw_base) = (PCLK/16/lpc_serial->baudrate) & 0xFF;
  272. UART_DLM(lpc_serial->hw_base) = ((PCLK/16/lpc_serial->baudrate) >> 8) & 0xFF;
  273. /* DLAB = 0 */
  274. UART_LCR(lpc_serial->hw_base) = 0x03;
  275. lpc_serial->parent.type = RT_Device_Class_Char;
  276. lpc_serial->parent.init = rt_serial_init;
  277. lpc_serial->parent.open = rt_serial_open;
  278. lpc_serial->parent.close = rt_serial_close;
  279. lpc_serial->parent.read = rt_serial_read;
  280. lpc_serial->parent.write = rt_serial_write;
  281. lpc_serial->parent.control = rt_serial_control;
  282. lpc_serial->parent.user_data = RT_NULL;
  283. rt_device_register(&lpc_serial->parent,
  284. "uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  285. #endif
  286. #ifdef RT_USING_UART2
  287. lpc_serial = &serial2;
  288. lpc_serial->parent.type = RT_Device_Class_Char;
  289. lpc_serial->hw_base = 0xE0010000;
  290. lpc_serial->baudrate = 115200;
  291. lpc_serial->irqno = UART1_INT;
  292. rt_memset(lpc_serial->rx_buffer, 0, sizeof(lpc_serial->rx_buffer));
  293. lpc_serial->read_index = lpc_serial->save_index = 0;
  294. /* Enable UART1 RxD and TxD pins */
  295. PINSEL0 |= 0x05 << 16;
  296. /* 8 bits, no Parity, 1 Stop bit */
  297. UART_LCR(lpc_serial->hw_base) = 0x83;
  298. /* Setup Baudrate */
  299. UART_DLL(lpc_serial->hw_base) = (PCLK/16/lpc_serial->baudrate) & 0xFF;
  300. UART_DLM(lpc_serial->hw_base) = ((PCLK/16/lpc_serial->baudrate) >> 8) & 0xFF;
  301. /* DLAB = 0 */
  302. UART_LCR(lpc_serial->hw_base) = 0x03;
  303. lpc_serial->parent.type = RT_Device_Class_Char;
  304. lpc_serial->parent.init = rt_serial_init;
  305. lpc_serial->parent.open = rt_serial_open;
  306. lpc_serial->parent.close = rt_serial_close;
  307. lpc_serial->parent.read = rt_serial_read;
  308. lpc_serial->parent.write = rt_serial_write;
  309. lpc_serial->parent.control = rt_serial_control;
  310. lpc_serial->parent.user_data = RT_NULL;
  311. rt_device_register(&lpc_serial->parent,
  312. "uart2", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  313. #endif
  314. }
  315. /*@}*/