serial.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 "LPC24xx.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. 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)
  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)
  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, rt_hw_uart_isr_1, RT_NULL);
  130. #endif
  131. }
  132. else
  133. {
  134. #ifdef RT_USING_UART2
  135. rt_hw_interrupt_install(lpc_serial->irqno, rt_hw_uart_isr_2, RT_NULL);
  136. #endif
  137. }
  138. rt_hw_interrupt_umask(lpc_serial->irqno);
  139. }
  140. return RT_EOK;
  141. }
  142. static rt_err_t rt_serial_close(rt_device_t dev)
  143. {
  144. struct rt_lpcserial* lpc_serial;
  145. lpc_serial = (struct rt_lpcserial*) dev;
  146. RT_ASSERT(lpc_serial != RT_NULL);
  147. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  148. {
  149. /* disable UART rx interrupt */
  150. UART_IER(lpc_serial->hw_base) = 0x00;
  151. }
  152. return RT_EOK;
  153. }
  154. static rt_err_t rt_serial_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  155. {
  156. return RT_EOK;
  157. }
  158. static rt_size_t rt_serial_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  159. {
  160. rt_uint8_t* ptr;
  161. struct rt_lpcserial *lpc_serial = (struct rt_lpcserial*)dev;
  162. RT_ASSERT(lpc_serial != RT_NULL);
  163. /* point to buffer */
  164. ptr = (rt_uint8_t*) buffer;
  165. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  166. {
  167. while (size)
  168. {
  169. /* interrupt receive */
  170. rt_base_t level;
  171. /* disable interrupt */
  172. level = rt_hw_interrupt_disable();
  173. if (lpc_serial->read_index != lpc_serial->save_index)
  174. {
  175. *ptr = lpc_serial->rx_buffer[lpc_serial->read_index];
  176. lpc_serial->read_index ++;
  177. if (lpc_serial->read_index >= RT_UART_RX_BUFFER_SIZE)
  178. lpc_serial->read_index = 0;
  179. }
  180. else
  181. {
  182. /* no data in rx buffer */
  183. /* enable interrupt */
  184. rt_hw_interrupt_enable(level);
  185. break;
  186. }
  187. /* enable interrupt */
  188. rt_hw_interrupt_enable(level);
  189. ptr ++; size --;
  190. }
  191. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  192. }
  193. else if (dev->flag & RT_DEVICE_FLAG_DMA_RX)
  194. {
  195. /* not support right now */
  196. RT_ASSERT(0);
  197. }
  198. /* polling mode */
  199. while (size && (UART_LSR(lpc_serial->hw_base) & 0x01))
  200. {
  201. /* Read Character */
  202. *ptr = UART_RBR(lpc_serial->hw_base);
  203. ptr ++;
  204. size --;
  205. }
  206. return (rt_size_t)ptr - (rt_size_t)buffer;
  207. }
  208. static rt_size_t rt_serial_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  209. {
  210. struct rt_lpcserial* lpc_serial;
  211. char *ptr;
  212. lpc_serial = (struct rt_lpcserial*) dev;
  213. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  214. {
  215. /* not support */
  216. RT_ASSERT(0);
  217. }
  218. else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  219. {
  220. /* not support */
  221. RT_ASSERT(0);
  222. }
  223. /* polling write */
  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. while (!(UART_LSR(lpc_serial->hw_base) & 0x20));
  233. UART_THR(lpc_serial->hw_base) = '\r';
  234. }
  235. while (!(UART_LSR(lpc_serial->hw_base) & 0x20));
  236. UART_THR(lpc_serial->hw_base) = *ptr;
  237. ptr ++;
  238. size --;
  239. }
  240. }
  241. else
  242. {
  243. while (size)
  244. {
  245. while (!(UART_LSR(lpc_serial->hw_base) & 0x20));
  246. UART_THR(lpc_serial->hw_base) = *ptr;
  247. ptr ++;
  248. size --;
  249. }
  250. }
  251. return (rt_size_t) ptr - (rt_size_t) buffer;
  252. }
  253. void rt_hw_serial_init(void)
  254. {
  255. struct rt_lpcserial* lpc_serial;
  256. #ifdef RT_USING_UART1
  257. lpc_serial = &serial1;
  258. lpc_serial->parent.type = RT_Device_Class_Char;
  259. lpc_serial->hw_base = 0xE000C000;
  260. lpc_serial->baudrate = 115200;
  261. lpc_serial->irqno = UART0_INT;
  262. rt_memset(lpc_serial->rx_buffer, 0, sizeof(lpc_serial->rx_buffer));
  263. lpc_serial->read_index = lpc_serial->save_index = 0;
  264. /* Enable UART0 RxD and TxD pins */
  265. PINSEL0 |= 0x05;
  266. /* 8 bits, no Parity, 1 Stop bit */
  267. UART_LCR(lpc_serial->hw_base) = 0x83;
  268. /* Setup Baudrate */
  269. UART_DLL(lpc_serial->hw_base) = (PCLK/16/lpc_serial->baudrate) & 0xFF;
  270. UART_DLM(lpc_serial->hw_base) = ((PCLK/16/lpc_serial->baudrate) >> 8) & 0xFF;
  271. /* DLAB = 0 */
  272. UART_LCR(lpc_serial->hw_base) = 0x03;
  273. lpc_serial->parent.init = rt_serial_init;
  274. lpc_serial->parent.open = rt_serial_open;
  275. lpc_serial->parent.close = rt_serial_close;
  276. lpc_serial->parent.read = rt_serial_read;
  277. lpc_serial->parent.write = rt_serial_write;
  278. lpc_serial->parent.control = rt_serial_control;
  279. lpc_serial->parent.private = RT_NULL;
  280. rt_device_register(&lpc_serial->parent,
  281. "uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  282. #endif
  283. #ifdef RT_USING_UART2
  284. lpc_serial = &serial2;
  285. lpc_serial->parent.type = RT_Device_Class_Char;
  286. lpc_serial->hw_base = 0xE0010000;
  287. lpc_serial->baudrate = 115200;
  288. lpc_serial->irqno = UART1_INT;
  289. rt_memset(lpc_serial->rx_buffer, 0, sizeof(lpc_serial->rx_buffer));
  290. lpc_serial->read_index = lpc_serial->save_index = 0;
  291. /* Enable UART1 RxD and TxD pins */
  292. PINSEL0 |= 0x05 << 16;
  293. /* 8 bits, no Parity, 1 Stop bit */
  294. UART_LCR(lpc_serial->hw_base) = 0x83;
  295. /* Setup Baudrate */
  296. UART_DLL(lpc_serial->hw_base) = (PCLK/16/lpc_serial->baudrate) & 0xFF;
  297. UART_DLM(lpc_serial->hw_base) = ((PCLK/16/lpc_serial->baudrate) >> 8) & 0xFF;
  298. /* DLAB = 0 */
  299. UART_LCR(lpc_serial->hw_base) = 0x03;
  300. lpc_serial->parent.init = rt_serial_init;
  301. lpc_serial->parent.open = rt_serial_open;
  302. lpc_serial->parent.close = rt_serial_close;
  303. lpc_serial->parent.read = rt_serial_read;
  304. lpc_serial->parent.write = rt_serial_write;
  305. lpc_serial->parent.control = rt_serial_control;
  306. lpc_serial->parent.private = RT_NULL;
  307. rt_device_register(&lpc_serial->parent,
  308. "uart2", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  309. #endif
  310. }
  311. /*@}*/