drv_uart.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-04-05 bigmagic Initial version
  9. * 2020-10-28 ma Buadrate & Multi-Port support
  10. */
  11. /**
  12. * @addtogroup ls2k
  13. */
  14. /*@{*/
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #include <rthw.h>
  18. #include "drv_uart.h"
  19. #define TRUE 1
  20. #define FALSE 0
  21. const struct serial_configure config_uart0 = {
  22. BAUD_RATE_115200, /* 921600 bits/s */
  23. DATA_BITS_8, /* 8 databits */
  24. STOP_BITS_1, /* 1 stopbit */
  25. PARITY_NONE, /* No parity */
  26. BIT_ORDER_LSB, /* LSB first sent */
  27. NRZ_NORMAL, /* Normal mode */
  28. RT_SERIAL_RB_BUFSZ, /* Buffer size */
  29. 0
  30. };
  31. struct rt_uart_ls2k
  32. {
  33. void *base;
  34. rt_uint32_t IRQ;
  35. };
  36. static rt_err_t ls2k_uart_set_buad(struct rt_serial_device *serial,struct serial_configure *cfg)
  37. {
  38. struct rt_uart_ls2k *uart_dev = RT_NULL;
  39. rt_err_t ret=RT_EOK;
  40. RT_ASSERT(serial != RT_NULL);
  41. RT_ASSERT(cfg != RT_NULL);
  42. uart_dev = (struct rt_uart_ls2k *)serial->parent.user_data;
  43. uint64_t brtc = (125000000U) / (16*(cfg->baud_rate));
  44. UART_LCR(uart_dev->base)=0x80; // Activate buadcfg
  45. UART_LSB(uart_dev->base)= brtc & 0xff;
  46. UART_MSB(uart_dev->base)= brtc >> 8;
  47. if(((((short)UART_MSB(uart_dev->base))<<8) | UART_LSB(uart_dev->base)) != brtc)
  48. ret=RT_ERROR;
  49. UART_LCR(uart_dev->base)= CFCR_8BITS;// Back to normal
  50. UART_MCR(uart_dev->base)= MCR_IENABLE/* | MCR_DTR | MCR_RTS*/;
  51. UART_IER(uart_dev->base) = 0;
  52. }
  53. static rt_err_t ls2k_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  54. {
  55. struct rt_uart_ls2k *uart_dev = RT_NULL;
  56. RT_ASSERT(serial != RT_NULL);
  57. RT_ASSERT(cfg != RT_NULL);
  58. ls2k_uart_set_buad(serial,cfg);
  59. uart_dev = (struct rt_uart_ls2k *)serial->parent.user_data;
  60. HWREG8(0xffffffffbfe10428)=0x1f;// Enable Multi-Port Support, by default it's 0x11 ,which means UART0 & UART4 Controller is in single port mode.
  61. UART_IER(uart_dev->base) = 0; /* clear interrupt */
  62. UART_FCR(uart_dev->base) = 0xc1; /* reset UART Rx/Tx */
  63. /* set databits, stopbits and parity. (8-bit data, 1 stopbit, no parity) */
  64. UART_LCR(uart_dev->base) = 0x3;
  65. UART_MCR(uart_dev->base) = 0x3;
  66. UART_LSR(uart_dev->base) = 0x60;
  67. UART_MSR(uart_dev->base) = 0xb0;
  68. return RT_EOK;
  69. }
  70. static rt_err_t ls2k_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  71. {
  72. struct rt_uart_ls2k *uart_dev = RT_NULL;
  73. RT_ASSERT(serial != RT_NULL);
  74. uart_dev = (struct rt_uart_ls2k *)serial->parent.user_data;
  75. switch (cmd)
  76. {
  77. case RT_DEVICE_CTRL_CLR_INT: /* Disable RX IRQ */
  78. rt_hw_interrupt_mask(uart_dev->IRQ);
  79. break;
  80. case RT_DEVICE_CTRL_SET_INT: /* Enable RX IRQ */
  81. rt_hw_interrupt_umask(uart_dev->IRQ);
  82. UART_IER(uart_dev->base) |= (IER_IRxE|IER_ILE);
  83. break;
  84. default:
  85. break;
  86. }
  87. return RT_EOK;
  88. }
  89. static rt_bool_t uart_is_transmit_empty(struct rt_uart_ls2k *uart_dev)
  90. {
  91. unsigned char status = UART_LSR(uart_dev->base);
  92. if (status & (UARTLSR_TE | UARTLSR_TFE))
  93. {
  94. return TRUE;
  95. }
  96. else
  97. {
  98. return FALSE;
  99. }
  100. }
  101. static int ls2k_uart_putc(struct rt_serial_device *serial, char c)
  102. {
  103. struct rt_uart_ls2k *uart_dev = RT_NULL;
  104. RT_ASSERT(serial != RT_NULL);
  105. uart_dev = (struct rt_uart_ls2k *)serial->parent.user_data;
  106. while (FALSE == uart_is_transmit_empty(uart_dev))
  107. ;
  108. UART_DAT(uart_dev->base) = c;
  109. return 1;
  110. }
  111. static int ls2k_uart_getc(struct rt_serial_device *serial)
  112. {
  113. struct rt_uart_ls2k *uart_dev = RT_NULL;
  114. RT_ASSERT(serial != RT_NULL);
  115. uart_dev = (struct rt_uart_ls2k *)serial->parent.user_data;
  116. if (LSR_RXRDY & UART_LSR(uart_dev->base))
  117. {
  118. return UART_DAT(uart_dev->base);
  119. }
  120. return -1;
  121. }
  122. /* UART interrupt handler */
  123. static void uart_irq_handler(int vector, void *param)
  124. {
  125. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  126. struct rt_uart_ls2k *uart_dev = RT_NULL;
  127. RT_ASSERT(serial != RT_NULL);
  128. uart_dev = (struct rt_uart_ls2k *)serial->parent.user_data;
  129. unsigned char iir = UART_IIR(uart_dev->base);
  130. /* Find out interrupt reason */
  131. if ((IIR_RXTOUT & iir) || (IIR_RXRDY & iir))
  132. {
  133. rt_interrupt_enter();
  134. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  135. rt_interrupt_leave();
  136. }
  137. }
  138. static const struct rt_uart_ops ls2k_uart_ops =
  139. {
  140. ls2k_uart_configure,
  141. ls2k_uart_control,
  142. ls2k_uart_putc,
  143. ls2k_uart_getc,
  144. };
  145. struct rt_uart_ls2k uart_dev0 =
  146. {
  147. (void *)UARTx_BASE(0),
  148. LS2K_UART_0_1_2_3_IRQ,
  149. };
  150. struct rt_uart_ls2k uart_dev4 =
  151. {
  152. (void *)UARTx_BASE(4),
  153. LS2K_UART_4_5_6_7_IRQ ,
  154. };
  155. struct rt_serial_device serial,serial4;
  156. void rt_hw_uart_init(void)
  157. {
  158. //UART0_1_ENABLE=0xff;
  159. struct rt_uart_ls2k *uart,*uart4;
  160. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  161. uart = &uart_dev0;
  162. uart4=&uart_dev4;
  163. serial.ops = &ls2k_uart_ops;
  164. serial.config = config_uart0;
  165. serial4.ops= &ls2k_uart_ops;
  166. serial4.config=config;
  167. rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial, "UART0");
  168. rt_hw_interrupt_install(uart4->IRQ, uart_irq_handler, &serial4, "UART4");
  169. /* register UART device */
  170. rt_hw_serial_register(&serial,
  171. "uart0",
  172. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  173. uart);
  174. rt_hw_serial_register(&serial4,
  175. "uart4",
  176. RT_DEVICE_FLAG_RDWR| RT_DEVICE_FLAG_INT_RX,
  177. &uart_dev4);
  178. }
  179. /*@}*/