drv_uart.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * File : drv_uart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009-2014 RT-Thread Develop 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. * 2013-05-18 Bernard The first version for LPC40xx
  13. * 2014-12-16 RT_learning The first version for LPC5410x
  14. * 2017-08-01 XiaoYang The first version for LPC546xx
  15. */
  16. #include <rthw.h>
  17. #include <rtthread.h>
  18. #include <rtdevice.h>
  19. #include "fsl_usart.h"
  20. #include "fsl_common.h"
  21. #include "fsl_iocon.h"
  22. struct lpc_uart
  23. {
  24. USART_Type *UART;
  25. IRQn_Type UART_IRQn;
  26. };
  27. static rt_err_t lpc_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  28. {
  29. struct lpc_uart *uart;
  30. usart_config_t u0_config;
  31. RT_ASSERT(serial != RT_NULL);
  32. uart = (struct lpc_uart *)serial->parent.user_data;
  33. /*
  34. * config.baudRate_Bps = 115200U;
  35. * config.parityMode = kUSART_ParityDisabled;
  36. * config.stopBitCount = kUSART_OneStopBit;
  37. * config.loopback = false;
  38. * config.enableTx = false;
  39. * config.enableRx = false;
  40. */
  41. USART_GetDefaultConfig(&u0_config);
  42. u0_config.baudRate_Bps = cfg->baud_rate;
  43. u0_config.enableTx = true;
  44. u0_config.enableRx = true;
  45. USART_Init(uart->UART, &u0_config, CLOCK_GetFreq(kCLOCK_Flexcomm0));
  46. return RT_EOK;
  47. }
  48. static rt_err_t lpc_control(struct rt_serial_device *serial, int cmd, void *arg)
  49. {
  50. struct lpc_uart *uart;
  51. RT_ASSERT(serial != RT_NULL);
  52. uart = (struct lpc_uart *)serial->parent.user_data;
  53. switch (cmd)
  54. {
  55. case RT_DEVICE_CTRL_CLR_INT:
  56. /* disable rx irq */
  57. USART_DisableInterrupts(uart->UART, kUSART_RxLevelInterruptEnable);
  58. break;
  59. case RT_DEVICE_CTRL_SET_INT:
  60. /* enable rx irq */
  61. USART_EnableInterrupts(uart->UART, kUSART_RxLevelInterruptEnable);
  62. break;
  63. }
  64. return RT_EOK;
  65. }
  66. static int lpc_putc(struct rt_serial_device *serial, char c)
  67. {
  68. struct lpc_uart *uart;
  69. uart = (struct lpc_uart *)serial->parent.user_data;
  70. while (!(kUSART_TxFifoNotFullFlag & USART_GetStatusFlags(uart->UART)));
  71. USART_WriteByte(uart->UART, c);
  72. return 1;
  73. }
  74. static int lpc_getc(struct rt_serial_device *serial)
  75. {
  76. struct lpc_uart *uart;
  77. uart = (struct lpc_uart *)serial->parent.user_data;
  78. if (kUSART_RxFifoNotEmptyFlag & USART_GetStatusFlags(uart->UART))
  79. {
  80. return USART_ReadByte(uart->UART);
  81. }
  82. else
  83. return -1;
  84. }
  85. static const struct rt_uart_ops lpc_uart_ops =
  86. {
  87. lpc_configure,
  88. lpc_control,
  89. lpc_putc,
  90. lpc_getc,
  91. };
  92. #define IOCON_PIO_DIGITAL_EN 0x0100u /*!< Enables digital function */
  93. #define IOCON_PIO_FUNC1 0x01u /*!< Selects pin function 1 */
  94. #define IOCON_PIO_INPFILT_OFF 0x0200u /*!< Input filter disabled */
  95. #define IOCON_PIO_INV_DI 0x00u /*!< Input function is not inverted */
  96. #define IOCON_PIO_MODE_INACT 0x00u /*!< No addition pin function */
  97. #define IOCON_PIO_OPENDRAIN_DI 0x00u /*!< Open drain is disabled */
  98. #define IOCON_PIO_SLEW_STANDARD 0x00u /*!< Standard mode, output slew rate control is enabled */
  99. #define PIN29_IDX 29u /*!< Pin number for pin 29 in a port 0 */
  100. #define PIN30_IDX 30u /*!< Pin number for pin 30 in a port 0 */
  101. #define PORT0_IDX 0u /*!< Port index */
  102. /* UART0 device driver structure */
  103. struct lpc_uart uart0 =
  104. {
  105. USART0,
  106. FLEXCOMM0_IRQn,
  107. };
  108. struct rt_serial_device serial0;
  109. void FLEXCOMM0_IRQHandler(void)
  110. {
  111. /* enter interrupt */
  112. rt_interrupt_enter();
  113. rt_hw_serial_isr(&serial0, RT_SERIAL_EVENT_RX_IND);
  114. /* leave interrupt */
  115. rt_interrupt_leave();
  116. }
  117. void rt_hw_uart_init(void)
  118. {
  119. struct lpc_uart *uart;
  120. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  121. #ifdef RT_USING_UART0
  122. uart = &uart0;
  123. serial0.ops = &lpc_uart_ops;
  124. serial0.config = config;
  125. serial0.parent.user_data = uart;
  126. /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
  127. CLOCK_AttachClk(kFRO12M_to_FLEXCOMM0);
  128. /* Enables the clock for the IOCON block. 0 = Disable; 1 = Enable.: 0x01u */
  129. // CLOCK_EnableClock(kCLOCK_Iocon);
  130. {
  131. const uint32_t port0_pin29_config = (
  132. IOCON_PIO_FUNC1 | /* Pin is configured as FC0_RXD_SDA_MOSI */
  133. IOCON_PIO_MODE_INACT | /* No addition pin function */
  134. IOCON_PIO_INV_DI | /* Input function is not inverted */
  135. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  136. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  137. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  138. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  139. );
  140. IOCON_PinMuxSet(IOCON, PORT0_IDX, PIN29_IDX, port0_pin29_config); /* PORT0 PIN29 (coords: B13) is configured as FC0_RXD_SDA_MOSI */
  141. const uint32_t port0_pin30_config = (
  142. IOCON_PIO_FUNC1 | /* Pin is configured as FC0_TXD_SCL_MISO */
  143. IOCON_PIO_MODE_INACT | /* No addition pin function */
  144. IOCON_PIO_INV_DI | /* Input function is not inverted */
  145. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  146. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  147. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  148. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  149. );
  150. IOCON_PinMuxSet(IOCON, PORT0_IDX, PIN30_IDX, port0_pin30_config); /* PORT0 PIN30 (coords: A2) is configured as FC0_TXD_SCL_MISO */
  151. }
  152. /* Enable RX interrupt. */
  153. USART_EnableInterrupts(uart->UART, kUSART_RxLevelInterruptEnable);
  154. EnableIRQ(uart->UART_IRQn);
  155. /* register UART0 device */
  156. rt_hw_serial_register(&serial0, "uart0",
  157. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  158. uart);
  159. #endif
  160. }