1
0

drv_uart.c 6.8 KB

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