uart_dev.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * File : uart_dev.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2013, 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. * 2009-01-05 Bernard the first version
  13. * 2010-03-29 Bernard remove interrupt Tx and DMA Rx mode
  14. * 2013-05-13 aozima update for kehong-lingtai.
  15. */
  16. #include "uart/uart.h"
  17. #include "uart_dev.h"
  18. #include "ae210p.h"
  19. #include "board.h"
  20. #include "bsp_hal.h"
  21. #include "rtdevice.h"
  22. #include "serial.h"
  23. #define UART_ENABLE_IRQ(n) hal_intc_irq_enable(n)
  24. #define UART_DISABLE_IRQ(n) hal_intc_irq_disable(n)
  25. struct uart_device
  26. {
  27. uint32_t uart_base;
  28. uint32_t irq;
  29. };
  30. static rt_err_t __uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  31. {
  32. struct uart_device *uartDev = RT_NULL;
  33. RT_ASSERT(serial != RT_NULL);
  34. RT_ASSERT(cfg != RT_NULL);
  35. uartDev = (struct uart_device *)serial->parent.user_data;
  36. __drv_uart_init(uartDev->uart_base, cfg->baud_rate);
  37. // todo : enable FIFO threshold, enable rx & rx timeout(threshold) interrupt
  38. return RT_EOK;
  39. }
  40. static rt_err_t __uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  41. {
  42. struct uart_device *uartDev = RT_NULL;
  43. RT_ASSERT(serial != RT_NULL);
  44. uartDev = (struct uart_device *)serial->parent.user_data;
  45. switch (cmd)
  46. {
  47. case RT_DEVICE_CTRL_CLR_INT: /* disable rx irq */
  48. UART_DISABLE_IRQ(uartDev->irq);
  49. break;
  50. case RT_DEVICE_CTRL_SET_INT: /* enable rx irq */
  51. UART_ENABLE_IRQ(uartDev->irq);
  52. break;
  53. default:
  54. break;
  55. }
  56. return RT_EOK;
  57. }
  58. static int __uart_putc(struct rt_serial_device *serial, char c)
  59. {
  60. struct uart_device *uartDev = RT_NULL;
  61. RT_ASSERT(serial != RT_NULL);
  62. uartDev = (struct uart_device *)serial->parent.user_data;
  63. __drv_uart_put_char(uartDev->uart_base, c); // Transmit Data
  64. return 1;
  65. }
  66. static int __uart_getc(struct rt_serial_device *serial)
  67. {
  68. int ch = -1;
  69. struct uart_device *uartDev = RT_NULL;
  70. RT_ASSERT(serial != RT_NULL);
  71. uartDev = (struct uart_device *)serial->parent.user_data;
  72. ch = -1;
  73. if (__drv_uart_is_kbd_hit(uartDev->uart_base))
  74. {
  75. ch = __drv_uart_get_char(uartDev->uart_base) & 0x00FF;
  76. }
  77. return ch;
  78. }
  79. static const struct rt_uart_ops __uart_ops =
  80. {
  81. __uart_configure,
  82. __uart_control,
  83. __uart_putc,
  84. __uart_getc,
  85. RT_NULL
  86. };
  87. #if RT_USING_UART01
  88. struct uart_device uartDev01 =
  89. { // UART01 device driver structure
  90. UART1_BASE,
  91. IRQ_UART1_VECTOR
  92. };
  93. struct rt_serial_device serial01;
  94. void URT01_IRQHandler(void)
  95. {
  96. struct uart_device *uartDev = RT_NULL;
  97. uartDev = &uartDev01;
  98. rt_interrupt_enter(); /* enter interrupt */
  99. // if (uart->uart_device->Interrupt & ((1 << bsUART_TIMEOUT_INTENAB) | (1 << bsUART_RECEIVE_INTENAB))) // RX
  100. // {
  101. // rt_hw_serial_isr(&serial01, RT_SERIAL_EVENT_RX_IND);
  102. // }
  103. //
  104. // if (uart->uart_device->Interrupt & (1 << bsUART_TRANSMIT_INTENAB)) // TX
  105. // {
  106. // ;
  107. // }
  108. //
  109. // /* clear all interrupt */
  110. // uart->uart_device->IntClear = (1 << bsUART_RECEIVE_INTENAB)
  111. // | (1 << bsUART_TRANSMIT_INTENAB)
  112. // | (1 << bsUART_TIMEOUT_INTENAB);
  113. rt_interrupt_leave(); /* leave interrupt */
  114. }
  115. #endif /* RT_USING_UART01 */
  116. #if RT_USING_UART02
  117. struct uart_device uartDev02 =
  118. { // UART02 device driver structure
  119. UART2_BASE,
  120. IRQ_UATR2_VECTOR
  121. };
  122. struct rt_serial_device serial02;
  123. void URT02_IRQHandler(void)
  124. {
  125. struct uart_device *uartDev = RT_NULL;
  126. uartDev = &uartDev02;
  127. rt_interrupt_enter(); /* enter interrupt */
  128. uartDev = uartDev;
  129. rt_interrupt_leave(); /* leave interrupt */
  130. }
  131. #endif /* RT_USING_UART02 */
  132. void rt_hw_usart_init(void)
  133. {
  134. struct uart_device *uartDev = RT_NULL;
  135. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  136. #if RT_USING_UART01
  137. uart = &uartDev01;
  138. config.baud_rate = BAUD_RATE_38400;
  139. serial01.ops = &__uart_ops;
  140. serial01.config = config;
  141. // set interrupt priority level
  142. // disable interrupt
  143. // register UART01 device
  144. rt_hw_serial_register(&serial01, "uart01",
  145. RT_DEVICE_FLAG_RDWR /*| RT_DEVICE_FLAG_INT_RX*/,
  146. uartDev);
  147. #endif /* RT_USING_UART01 */
  148. #if RT_USING_UART02
  149. uartDev = &uartDev02;
  150. config.baud_rate = BAUD_RATE_38400;
  151. serial02.ops = &__uart_ops;
  152. serial02.config = config;
  153. // set interrupt priority level
  154. // disable interrupt
  155. /* register UART02 device */
  156. rt_hw_serial_register(&serial02, "uart02",
  157. RT_DEVICE_FLAG_RDWR /*| RT_DEVICE_FLAG_INT_RX*/,
  158. uartDev);
  159. #endif /* RT_USING_UART02 */
  160. }