serial.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * COPYRIGHT (C) 2018, Real-Thread Information Technology Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2013-03-30 Bernard the first verion
  9. */
  10. #include <rthw.h>
  11. #include <registers/regsuart.h>
  12. #include <uart/imx_uart.h>
  13. #include <rtdevice.h>
  14. #include "serial.h"
  15. struct hw_uart_device
  16. {
  17. uint32_t instance;
  18. int irqno;
  19. };
  20. static void rt_hw_uart_isr(int irqno, void *param)
  21. {
  22. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  23. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  24. }
  25. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  26. {
  27. struct hw_uart_device *uart;
  28. uint32_t baudrate;
  29. uint8_t parity, stopbits, datasize, flowcontrol;
  30. RT_ASSERT(serial != RT_NULL);
  31. uart = (struct hw_uart_device *)serial->parent.user_data;
  32. baudrate = cfg->baud_rate;
  33. switch (cfg->data_bits)
  34. {
  35. case DATA_BITS_8:
  36. datasize = EIGHTBITS;
  37. break;
  38. case DATA_BITS_7:
  39. datasize = SEVENBITS;
  40. break;
  41. }
  42. if (cfg->stop_bits == STOP_BITS_1) stopbits = STOPBITS_ONE;
  43. else if (cfg->stop_bits == STOP_BITS_2) stopbits = STOPBITS_TWO;
  44. parity = PARITY_NONE;
  45. flowcontrol = FLOWCTRL_OFF;
  46. /* initailize UART */
  47. // uart_init(uart->instance, baudrate, parity, stopbits, datasize, flowcontrol);
  48. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, serial, "uart");
  49. rt_hw_interrupt_mask(uart->irqno);
  50. /* Set the IRQ mode for the Rx FIFO */
  51. uart_set_FIFO_mode(uart->instance, RX_FIFO, 1, IRQ_MODE);
  52. return RT_EOK;
  53. }
  54. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  55. {
  56. struct hw_uart_device *uart;
  57. RT_ASSERT(serial != RT_NULL);
  58. uart = (struct hw_uart_device *)serial->parent.user_data;
  59. switch (cmd)
  60. {
  61. case RT_DEVICE_CTRL_CLR_INT:
  62. /* disable rx irq */
  63. rt_hw_interrupt_mask(uart->irqno);
  64. break;
  65. case RT_DEVICE_CTRL_SET_INT:
  66. /* enable rx irq */
  67. rt_hw_interrupt_umask(uart->irqno);
  68. break;
  69. }
  70. return RT_EOK;
  71. }
  72. static int uart_putc(struct rt_serial_device *serial, char c)
  73. {
  74. struct hw_uart_device *uart;
  75. RT_ASSERT(serial != RT_NULL);
  76. uart = (struct hw_uart_device *)serial->parent.user_data;
  77. uart_putchar(uart->instance, (uint8_t*)&c);
  78. return 1;
  79. }
  80. static int uart_getc(struct rt_serial_device *serial)
  81. {
  82. int ch;
  83. struct hw_uart_device *uart;
  84. RT_ASSERT(serial != RT_NULL);
  85. uart = (struct hw_uart_device *)serial->parent.user_data;
  86. ch = uart_getchar(uart->instance);
  87. if (ch == NONE_CHAR) ch = -1;
  88. return ch;
  89. }
  90. static const struct rt_uart_ops _uart_ops =
  91. {
  92. uart_configure,
  93. uart_control,
  94. uart_putc,
  95. uart_getc,
  96. };
  97. #ifdef RT_USING_UART0
  98. /* UART device driver structure */
  99. static struct hw_uart_device _uart0_device =
  100. {
  101. HW_UART0,
  102. IMX_INT_UART0
  103. };
  104. static struct rt_serial_device _serial0;
  105. #endif
  106. #ifdef RT_USING_UART1
  107. /* UART1 device driver structure */
  108. static struct hw_uart_device _uart1_device =
  109. {
  110. HW_UART1,
  111. IMX_INT_UART1
  112. };
  113. static struct rt_serial_device _serial1;
  114. #endif
  115. int rt_hw_uart_init(void)
  116. {
  117. struct hw_uart_device *uart;
  118. struct serial_configure config;
  119. config.baud_rate = BAUD_RATE_115200;
  120. config.bit_order = BIT_ORDER_LSB;
  121. config.data_bits = DATA_BITS_8;
  122. config.parity = PARITY_NONE;
  123. config.stop_bits = STOP_BITS_1;
  124. config.invert = NRZ_NORMAL;
  125. config.bufsz = RT_SERIAL_RB_BUFSZ;
  126. #ifdef RT_USING_UART0
  127. uart = &_uart0_device;
  128. _serial0.ops = &_uart_ops;
  129. _serial0.config = config;
  130. /* register UART1 device */
  131. rt_hw_serial_register(&_serial0, "uart0",
  132. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  133. uart);
  134. #endif
  135. #ifdef RT_USING_UART1
  136. uart = &_uart1_device;
  137. _serial1.ops = &_uart_ops;
  138. _serial1.config = config;
  139. /* register UART1 device */
  140. rt_hw_serial_register(&_serial1, "uart1",
  141. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
  142. #endif
  143. return 0;
  144. }
  145. INIT_BOARD_EXPORT(rt_hw_uart_init);