uart.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * File : uart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009 - 2012, 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://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. */
  13. #include <rthw.h>
  14. #include <rtthread.h>
  15. #include <soc3210.h>
  16. /**
  17. * @addtogroup Loongson SoC3210
  18. */
  19. /*@{*/
  20. #if defined(RT_USING_UART) && defined(RT_USING_DEVICE)
  21. /* UART interrupt enable register value */
  22. #define UARTIER_IME (1 << 3)
  23. #define UARTIER_ILE (1 << 2)
  24. #define UARTIER_ITXE (1 << 1)
  25. #define UARTIER_IRXE (1 << 0)
  26. /* UART line control register value */
  27. #define UARTLCR_DLAB (1 << 7)
  28. #define UARTLCR_BCB (1 << 6)
  29. #define UARTLCR_SPB (1 << 5)
  30. #define UARTLCR_EPS (1 << 4)
  31. #define UARTLCR_PE (1 << 3)
  32. #define UARTLCR_SB (1 << 2)
  33. /* UART line status register value */
  34. #define UARTLSR_ERROR (1 << 7)
  35. #define UARTLSR_TE (1 << 6)
  36. #define UARTLSR_TFE (1 << 5)
  37. #define UARTLSR_BI (1 << 4)
  38. #define UARTLSR_FE (1 << 3)
  39. #define UARTLSR_PE (1 << 2)
  40. #define UARTLSR_OE (1 << 1)
  41. #define UARTLSR_DR (1 << 0)
  42. struct rt_uart_soc3210
  43. {
  44. struct rt_device parent;
  45. rt_uint32_t hw_base;
  46. rt_uint32_t irq;
  47. /* buffer for reception */
  48. rt_uint8_t read_index, save_index;
  49. rt_uint8_t rx_buffer[RT_UART_RX_BUFFER_SIZE];
  50. }uart_device;
  51. static void rt_uart_irqhandler(int irqno)
  52. {
  53. rt_ubase_t level;
  54. rt_uint8_t isr;
  55. struct rt_uart_soc3210* uart = &uart_device;
  56. /* read interrupt status and clear it */
  57. isr = UART_IIR(uart->hw_base);
  58. isr = (isr >> 1) & 0x3;
  59. if (isr & 0x02) /* receive data available */
  60. {
  61. /* Receive Data Available */
  62. while (UART_LSR(uart->hw_base) & UARTLSR_DR)
  63. {
  64. uart->rx_buffer[uart->save_index] = UART_DAT(uart->hw_base);
  65. level = rt_hw_interrupt_disable();
  66. uart->save_index ++;
  67. if (uart->save_index >= RT_UART_RX_BUFFER_SIZE)
  68. uart->save_index = 0;
  69. rt_hw_interrupt_enable(level);
  70. }
  71. /* invoke callback */
  72. if (uart->parent.rx_indicate != RT_NULL)
  73. {
  74. rt_size_t length;
  75. if (uart->read_index > uart->save_index)
  76. length = RT_UART_RX_BUFFER_SIZE - uart->read_index + uart->save_index;
  77. else
  78. length = uart->save_index - uart->read_index;
  79. uart->parent.rx_indicate(&uart->parent, length);
  80. }
  81. }
  82. return;
  83. }
  84. static rt_err_t rt_uart_init (rt_device_t dev)
  85. {
  86. rt_uint32_t baud_div;
  87. struct rt_uart_soc3210 *uart = (struct rt_uart_soc3210*)dev;
  88. RT_ASSERT(uart != RT_NULL);
  89. #if 0
  90. /* init UART Hardware */
  91. UART_IER(uart->hw_base) = 0; /* clear interrupt */
  92. UART_FCR(uart->hw_base) = 0x60; /* reset UART Rx/Tx */
  93. /* enable UART clock */
  94. /* set databits, stopbits and parity. (8-bit data, 1 stopbit, no parity) */
  95. UART_LCR(uart->hw_base) = 0x3;
  96. /* set baudrate */
  97. baud_div = DEV_CLK / 16 / UART_BAUDRATE;
  98. UART_LCR(uart->hw_base) |= UARTLCR_DLAB;
  99. UART_MSB(uart->hw_base) = (baud_div >> 8) & 0xff;
  100. UART_LSB(uart->hw_base) = baud_div & 0xff;
  101. UART_LCR(uart->hw_base) &= ~UARTLCR_DLAB;
  102. /* Enable UART unit, enable and clear FIFO */
  103. UART_FCR(uart->hw_base) = UARTFCR_UUE | UARTFCR_FE | UARTFCR_TFLS | UARTFCR_RFLS;
  104. #endif
  105. return RT_EOK;
  106. }
  107. static rt_err_t rt_uart_open(rt_device_t dev, rt_uint16_t oflag)
  108. {
  109. struct rt_uart_soc3210 *uart = (struct rt_uart_soc3210*)dev;
  110. RT_ASSERT(uart != RT_NULL);
  111. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  112. {
  113. /* Enable the UART Interrupt */
  114. UART_IER(uart->hw_base) |= UARTIER_IRXE;
  115. /* install interrupt */
  116. rt_hw_interrupt_install(uart->irq, rt_uart_irqhandler, RT_NULL);
  117. rt_hw_interrupt_umask(uart->irq);
  118. }
  119. return RT_EOK;
  120. }
  121. static rt_err_t rt_uart_close(rt_device_t dev)
  122. {
  123. struct rt_uart_soc3210 *uart = (struct rt_uart_soc3210*)dev;
  124. RT_ASSERT(uart != RT_NULL);
  125. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  126. {
  127. /* Disable the UART Interrupt */
  128. UART_IER(uart->hw_base) &= ~(UARTIER_IRXE);
  129. }
  130. return RT_EOK;
  131. }
  132. static rt_size_t rt_uart_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  133. {
  134. rt_uint8_t *ptr;
  135. struct rt_uart_soc3210 *uart = (struct rt_uart_soc3210 *)dev;
  136. RT_ASSERT(uart != RT_NULL);
  137. /* point to buffer */
  138. ptr = (rt_uint8_t*) buffer;
  139. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  140. {
  141. while (size)
  142. {
  143. /* interrupt receive */
  144. rt_base_t level;
  145. /* disable interrupt */
  146. level = rt_hw_interrupt_disable();
  147. if (uart->read_index != uart->save_index)
  148. {
  149. *ptr = uart->rx_buffer[uart->read_index];
  150. uart->read_index ++;
  151. if (uart->read_index >= RT_UART_RX_BUFFER_SIZE)
  152. uart->read_index = 0;
  153. }
  154. else
  155. {
  156. /* no data in rx buffer */
  157. /* enable interrupt */
  158. rt_hw_interrupt_enable(level);
  159. break;
  160. }
  161. /* enable interrupt */
  162. rt_hw_interrupt_enable(level);
  163. ptr ++;
  164. size --;
  165. }
  166. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  167. }
  168. return 0;
  169. }
  170. static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  171. {
  172. char *ptr;
  173. struct rt_uart_soc3210 *uart = (struct rt_uart_soc3210 *)dev;
  174. RT_ASSERT(uart != RT_NULL);
  175. ptr = (char *)buffer;
  176. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  177. {
  178. /* stream mode */
  179. while (size)
  180. {
  181. if (*ptr == '\n')
  182. {
  183. /* FIFO status, contain valid data */
  184. while (!(UART_LSR(uart->hw_base) & (UARTLSR_TE | UARTLSR_TFE)));
  185. /* write data */
  186. UART_DAT(uart->hw_base) = '\r';
  187. }
  188. /* FIFO status, contain valid data */
  189. while (!(UART_LSR(uart->hw_base) & (UARTLSR_TE | UARTLSR_TFE)));
  190. /* write data */
  191. UART_DAT(uart->hw_base) = *ptr;
  192. ptr ++;
  193. size --;
  194. }
  195. }
  196. else
  197. {
  198. while (size != 0)
  199. {
  200. /* FIFO status, contain valid data */
  201. while (!(UART_LSR(uart->hw_base) & (UARTLSR_TE | UARTLSR_TFE)));
  202. /* write data */
  203. UART_DAT(uart->hw_base) = *ptr;
  204. ptr++;
  205. size--;
  206. }
  207. }
  208. return (rt_size_t) ptr - (rt_size_t) buffer;
  209. }
  210. void rt_hw_uart_init(void)
  211. {
  212. struct rt_uart_soc3210 *uart;
  213. /* get uart device */
  214. uart = &uart_device;
  215. /* device initialization */
  216. uart->parent.type = RT_Device_Class_Char;
  217. rt_memset(uart->rx_buffer, 0, sizeof(uart->rx_buffer));
  218. uart->read_index = uart->save_index = 0;
  219. #if defined(RT_USING_UART1)
  220. uart->hw_base = UART0_BASE;
  221. uart->irq = IRQ_UART0;
  222. #elif defined(RT_USING_UART2)
  223. uart->hw_base = UART1_BASE;
  224. uart->irq = IRQ_UART1;
  225. #endif
  226. /* device interface */
  227. uart->parent.init = rt_uart_init;
  228. uart->parent.open = rt_uart_open;
  229. uart->parent.close = rt_uart_close;
  230. uart->parent.read = rt_uart_read;
  231. uart->parent.write = rt_uart_write;
  232. uart->parent.control = RT_NULL;
  233. uart->parent.user_data = RT_NULL;
  234. rt_device_register(&uart->parent,
  235. "uart", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX);
  236. }
  237. #endif /* end of UART */
  238. /*@}*/