uart.c 6.9 KB

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