uart.c 6.5 KB

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