drv_usart.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * File : drv_usart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. */
  23. #include <rtdevice.h>
  24. #include <encoding.h>
  25. #include <platform.h>
  26. #include <interrupt.h>
  27. static void usart_handler(int vector, void *param)
  28. {
  29. rt_hw_serial_isr((struct rt_serial_device *)param, RT_SERIAL_EVENT_RX_IND);
  30. }
  31. static rt_err_t usart_configure(struct rt_serial_device *serial,
  32. struct serial_configure *cfg)
  33. {
  34. RT_ASSERT(serial != RT_NULL);
  35. RT_ASSERT(cfg != RT_NULL);
  36. GPIO_REG(GPIO_IOF_SEL) &= ~IOF0_UART0_MASK;
  37. GPIO_REG(GPIO_IOF_EN) |= IOF0_UART0_MASK;
  38. UART0_REG(UART_REG_DIV) = get_cpu_freq() / cfg->baud_rate - 1;
  39. UART0_REG(UART_REG_TXCTRL) |= UART_TXEN;
  40. UART0_REG(UART_REG_RXCTRL) |= UART_RXEN;
  41. UART0_REG(UART_REG_IE) = UART_IP_RXWM;
  42. return RT_EOK;
  43. }
  44. static rt_err_t usart_control(struct rt_serial_device *serial,
  45. int cmd, void *arg)
  46. {
  47. RT_ASSERT(serial != RT_NULL);
  48. switch (cmd)
  49. {
  50. case RT_DEVICE_CTRL_CLR_INT:
  51. break;
  52. case RT_DEVICE_CTRL_SET_INT:
  53. break;
  54. }
  55. return RT_EOK;
  56. }
  57. static int usart_putc(struct rt_serial_device *serial, char c)
  58. {
  59. while (UART0_REG(UART_REG_TXFIFO) & 0x80000000) ;
  60. UART0_REG(UART_REG_TXFIFO) = c;
  61. return 0;
  62. }
  63. static int usart_getc(struct rt_serial_device *serial)
  64. {
  65. rt_int32_t val = UART0_REG(UART_REG_RXFIFO);
  66. if (val > 0)
  67. return (rt_uint8_t)val;
  68. else
  69. return -1;
  70. }
  71. static struct rt_uart_ops ops =
  72. {
  73. usart_configure,
  74. usart_control,
  75. usart_putc,
  76. usart_getc,
  77. };
  78. static struct rt_serial_device serial =
  79. {
  80. .ops = &ops,
  81. .config.baud_rate = BAUD_RATE_115200,
  82. .config.bit_order = BIT_ORDER_LSB,
  83. .config.data_bits = DATA_BITS_8,
  84. .config.parity = PARITY_NONE,
  85. .config.stop_bits = STOP_BITS_1,
  86. .config.invert = NRZ_NORMAL,
  87. .config.bufsz = RT_SERIAL_RB_BUFSZ,
  88. };
  89. int rt_hw_uart_init(void)
  90. {
  91. rt_hw_serial_register(
  92. &serial,
  93. "dusart",
  94. RT_DEVICE_FLAG_STREAM
  95. | RT_DEVICE_FLAG_RDWR
  96. | RT_DEVICE_FLAG_INT_RX, RT_NULL);
  97. rt_hw_interrupt_install(
  98. INT_UART0_BASE,
  99. usart_handler,
  100. (void *) & (serial.parent),
  101. "uart interrupt");
  102. rt_hw_interrupt_unmask(INT_UART0_BASE);
  103. return 0;
  104. }
  105. INIT_BOARD_EXPORT(rt_hw_uart_init);