serial.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * serial.c UART driver
  3. *
  4. * COPYRIGHT (C) 2013, Shanghai Real-Thread Technology Co., Ltd
  5. *
  6. * This file is part of RT-Thread (http://www.rt-thread.org)
  7. * Maintainer: bernard.xiong <bernard.xiong at gmail.com>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. *
  25. * Change Logs:
  26. * Date Author Notes
  27. * 2013-03-30 Bernard the first verion
  28. */
  29. #include <rthw.h>
  30. #include <registers/regsuart.h>
  31. #include <uart/imx_uart.h>
  32. #include <rtdevice.h>
  33. #include "serial.h"
  34. struct hw_uart_device
  35. {
  36. uint32_t instance;
  37. int irqno;
  38. };
  39. static void rt_hw_uart_isr(int irqno, void *param)
  40. {
  41. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  42. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  43. }
  44. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  45. {
  46. struct hw_uart_device *uart;
  47. uint32_t baudrate;
  48. uint8_t parity, stopbits, datasize, flowcontrol;
  49. RT_ASSERT(serial != RT_NULL);
  50. uart = (struct hw_uart_device *)serial->parent.user_data;
  51. baudrate = cfg->baud_rate;
  52. switch (cfg->data_bits)
  53. {
  54. case DATA_BITS_8:
  55. datasize = EIGHTBITS;
  56. break;
  57. case DATA_BITS_7:
  58. datasize = SEVENBITS;
  59. break;
  60. }
  61. if (cfg->stop_bits == STOP_BITS_1) stopbits = STOPBITS_ONE;
  62. else if (cfg->stop_bits == STOP_BITS_2) stopbits = STOPBITS_TWO;
  63. parity = PARITY_NONE;
  64. flowcontrol = FLOWCTRL_OFF;
  65. /* initailize UART */
  66. // uart_init(uart->instance, baudrate, parity, stopbits, datasize, flowcontrol);
  67. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, serial, "uart");
  68. rt_hw_interrupt_mask(uart->irqno);
  69. /* Set the IRQ mode for the Rx FIFO */
  70. uart_set_FIFO_mode(uart->instance, RX_FIFO, 1, IRQ_MODE);
  71. return RT_EOK;
  72. }
  73. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  74. {
  75. struct hw_uart_device *uart;
  76. RT_ASSERT(serial != RT_NULL);
  77. uart = (struct hw_uart_device *)serial->parent.user_data;
  78. switch (cmd)
  79. {
  80. case RT_DEVICE_CTRL_CLR_INT:
  81. /* disable rx irq */
  82. rt_hw_interrupt_mask(uart->irqno);
  83. break;
  84. case RT_DEVICE_CTRL_SET_INT:
  85. /* enable rx irq */
  86. rt_hw_interrupt_umask(uart->irqno);
  87. break;
  88. }
  89. return RT_EOK;
  90. }
  91. static int uart_putc(struct rt_serial_device *serial, char c)
  92. {
  93. struct hw_uart_device *uart;
  94. RT_ASSERT(serial != RT_NULL);
  95. uart = (struct hw_uart_device *)serial->parent.user_data;
  96. uart_putchar(uart->instance, (uint8_t*)&c);
  97. return 1;
  98. }
  99. static int uart_getc(struct rt_serial_device *serial)
  100. {
  101. int ch;
  102. struct hw_uart_device *uart;
  103. RT_ASSERT(serial != RT_NULL);
  104. uart = (struct hw_uart_device *)serial->parent.user_data;
  105. ch = uart_getchar(uart->instance);
  106. if (ch == NONE_CHAR) ch = -1;
  107. return ch;
  108. }
  109. static const struct rt_uart_ops _uart_ops =
  110. {
  111. uart_configure,
  112. uart_control,
  113. uart_putc,
  114. uart_getc,
  115. };
  116. #ifdef RT_USING_UART0
  117. /* UART device driver structure */
  118. static struct hw_uart_device _uart0_device =
  119. {
  120. HW_UART0,
  121. IMX_INT_UART0
  122. };
  123. static struct rt_serial_device _serial0;
  124. #endif
  125. #ifdef RT_USING_UART1
  126. /* UART1 device driver structure */
  127. static struct hw_uart_device _uart1_device =
  128. {
  129. HW_UART1,
  130. IMX_INT_UART1
  131. };
  132. static struct rt_serial_device _serial1;
  133. #endif
  134. int rt_hw_uart_init(void)
  135. {
  136. struct hw_uart_device *uart;
  137. struct serial_configure config;
  138. config.baud_rate = BAUD_RATE_115200;
  139. config.bit_order = BIT_ORDER_LSB;
  140. config.data_bits = DATA_BITS_8;
  141. config.parity = PARITY_NONE;
  142. config.stop_bits = STOP_BITS_1;
  143. config.invert = NRZ_NORMAL;
  144. config.bufsz = RT_SERIAL_RB_BUFSZ;
  145. #ifdef RT_USING_UART0
  146. uart = &_uart0_device;
  147. _serial0.ops = &_uart_ops;
  148. _serial0.config = config;
  149. /* register UART1 device */
  150. rt_hw_serial_register(&_serial0, "uart0",
  151. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  152. uart);
  153. #endif
  154. #ifdef RT_USING_UART1
  155. uart = &_uart1_device;
  156. _serial1.ops = &_uart_ops;
  157. _serial1.config = config;
  158. /* register UART1 device */
  159. rt_hw_serial_register(&_serial1, "uart1",
  160. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
  161. #endif
  162. return 0;
  163. }
  164. INIT_BOARD_EXPORT(rt_hw_uart_init);