usart.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * File : usart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2013, RT-Thread Development 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. * 2017-10-10 Tanek the first version
  13. */
  14. #include <rtthread.h>
  15. #include "usart.h"
  16. #include "fsl_common.h"
  17. #include "fsl_lpuart.h"
  18. #include "fsl_iomuxc.h"
  19. #ifdef RT_USING_SERIAL
  20. #if !defined(RT_USING_UART0) && !defined(RT_USING_UART1) && \
  21. !defined(RT_USING_UART2) && !defined(RT_USING_UART3) && \
  22. !defined(RT_USING_UART4) && !defined(RT_USING_UART5) && \
  23. !defined(RT_USING_UART6) && !defined(RT_USING_UART7)
  24. #error "Please define at least one UARTx"
  25. #endif
  26. #include <rtdevice.h>
  27. /* imxrt uart driver */
  28. struct imxrt_uart
  29. {
  30. LPUART_Type * uart_base;
  31. IRQn_Type irqn;
  32. struct rt_serial_device * serial;
  33. char *device_name;
  34. };
  35. static void uart_isr(struct rt_serial_device *serial);
  36. #if defined(RT_USING_UART1)
  37. struct rt_serial_device serial1;
  38. void LPUART1_IRQHandler(void)
  39. {
  40. uart_isr(&serial1);
  41. }
  42. #endif /* RT_USING_UART1 */
  43. #if defined(RT_USING_UART2)
  44. struct rt_serial_device serial2;
  45. void USART2_IRQHandler(void)
  46. {
  47. uart_isr(&serial2);
  48. }
  49. #endif /* RT_USING_UART2 */
  50. #if defined(RT_USING_UART3)
  51. struct rt_serial_device serial3;
  52. void UART3_IRQHandler(void)
  53. {
  54. uart_isr(&serial3);
  55. }
  56. #endif /* RT_USING_UART3 */
  57. #if defined(RT_USING_UART4)
  58. struct rt_serial_device serial4;
  59. void UART4_IRQHandler(void)
  60. {
  61. uart_isr(&serial4);
  62. }
  63. #endif /* RT_USING_UART4 */
  64. #if defined(RT_USING_UART5)
  65. struct rt_serial_device serial5;
  66. void USART5_IRQHandler(void)
  67. {
  68. uart_isr(&serial5);
  69. }
  70. #endif /* RT_USING_UART5 */
  71. #if defined(RT_USING_UART6)
  72. struct rt_serial_device serial6;
  73. void UART6_IRQHandler(void)
  74. {
  75. uart_isr(&serial6);
  76. }
  77. #endif /* RT_USING_UART6 */
  78. #if defined(RT_USING_UART7)
  79. struct rt_serial_device serial7;
  80. void UART7_IRQHandler(void)
  81. {
  82. uart_isr(&serial7);
  83. }
  84. #endif /* RT_USING_UART7 */
  85. #if defined(RT_USING_UART8)
  86. struct rt_serial_device serial8;
  87. void UART8_IRQHandler(void)
  88. {
  89. uart_isr(&serial8);
  90. }
  91. #endif /* RT_USING_UART8 */
  92. static const struct imxrt_uart uarts[] = {
  93. #ifdef RT_USING_UART1
  94. {
  95. LPUART1,
  96. LPUART1_IRQn,
  97. &serial1,
  98. "uart1",
  99. },
  100. #endif
  101. };
  102. /* Get debug console frequency. */
  103. uint32_t BOARD_DebugConsoleSrcFreq(void)
  104. {
  105. uint32_t freq;
  106. /* To make it simple, we assume default PLL and divider settings, and the only variable
  107. from application is use PLL3 source or OSC source */
  108. if (CLOCK_GetMux(kCLOCK_UartMux) == 0) /* PLL3 div6 80M */
  109. {
  110. freq = (CLOCK_GetPllFreq(kCLOCK_PllUsb1) / 6U) / (CLOCK_GetDiv(kCLOCK_UartDiv) + 1U);
  111. }
  112. else
  113. {
  114. freq = CLOCK_GetOscFreq() / (CLOCK_GetDiv(kCLOCK_UartDiv) + 1U);
  115. }
  116. return freq;
  117. }
  118. /**
  119. * @brief UART MSP Initialization
  120. * This function configures the hardware resources used in this example:
  121. * - Peripheral's clock enable
  122. * - Peripheral's GPIO Configuration
  123. * - NVIC configuration for UART interrupt request enable
  124. * @param huart: UART handle pointer
  125. * @retval None
  126. */
  127. void imxrt_uart_gpio_init(struct imxrt_uart *uart)
  128. {
  129. if (uart->uart_base == LPUART1)
  130. {
  131. CLOCK_EnableClock(kCLOCK_Iomuxc); /* iomuxc clock (iomuxc_clk_enable): 0x03u */
  132. IOMUXC_SetPinMux(
  133. IOMUXC_GPIO_AD_B0_12_LPUART1_TX, /* GPIO_AD_B0_12 is configured as LPUART1_TX */
  134. 0U); /* Software Input On Field: Input Path is determined by functionality */
  135. IOMUXC_SetPinMux(
  136. IOMUXC_GPIO_AD_B0_13_LPUART1_RX, /* GPIO_AD_B0_13 is configured as LPUART1_RX */
  137. 0U); /* Software Input On Field: Input Path is determined by functionality */
  138. IOMUXC_SetPinConfig(
  139. IOMUXC_GPIO_AD_B0_12_LPUART1_TX, /* GPIO_AD_B0_12 PAD functional properties : */
  140. 0x10B0u); /* Slew Rate Field: Slow Slew Rate
  141. Drive Strength Field: R0/6
  142. Speed Field: medium(100MHz)
  143. Open Drain Enable Field: Open Drain Disabled
  144. Pull / Keep Enable Field: Pull/Keeper Enabled
  145. Pull / Keep Select Field: Keeper
  146. Pull Up / Down Config. Field: 100K Ohm Pull Down
  147. Hyst. Enable Field: Hysteresis Disabled */
  148. IOMUXC_SetPinConfig(
  149. IOMUXC_GPIO_AD_B0_13_LPUART1_RX, /* GPIO_AD_B0_13 PAD functional properties : */
  150. 0x10B0u); /* Slew Rate Field: Slow Slew Rate
  151. Drive Strength Field: R0/6
  152. Speed Field: medium(100MHz)
  153. Open Drain Enable Field: Open Drain Disabled
  154. Pull / Keep Enable Field: Pull/Keeper Enabled
  155. Pull / Keep Select Field: Keeper
  156. Pull Up / Down Config. Field: 100K Ohm Pull Down
  157. Hyst. Enable Field: Hysteresis Disabled */
  158. }
  159. else
  160. {
  161. RT_ASSERT(RT_NULL);
  162. }
  163. }
  164. static rt_err_t imxrt_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  165. {
  166. struct imxrt_uart *uart;
  167. lpuart_config_t config;
  168. RT_ASSERT(serial != RT_NULL);
  169. RT_ASSERT(cfg != RT_NULL);
  170. uart = (struct imxrt_uart *)serial->parent.user_data;
  171. imxrt_uart_gpio_init(uart);
  172. LPUART_GetDefaultConfig(&config);
  173. config.baudRate_Bps = cfg->baud_rate;
  174. switch (cfg->data_bits)
  175. {
  176. case DATA_BITS_7:
  177. config.dataBitsCount = kLPUART_SevenDataBits;
  178. break;
  179. default:
  180. config.dataBitsCount = kLPUART_EightDataBits;
  181. break;
  182. }
  183. switch (cfg->stop_bits)
  184. {
  185. case STOP_BITS_2:
  186. config.stopBitCount = kLPUART_TwoStopBit;
  187. break;
  188. default:
  189. config.stopBitCount = kLPUART_OneStopBit;
  190. break;
  191. }
  192. switch (cfg->parity)
  193. {
  194. case PARITY_ODD:
  195. config.parityMode = kLPUART_ParityOdd;
  196. break;
  197. case PARITY_EVEN:
  198. config.parityMode = kLPUART_ParityEven;
  199. break;
  200. default:
  201. config.parityMode = kLPUART_ParityDisabled;
  202. break;
  203. }
  204. config.enableTx = true;
  205. config.enableRx = true;
  206. LPUART_Init(uart->uart_base, &config, BOARD_DebugConsoleSrcFreq());
  207. return RT_EOK;
  208. }
  209. static rt_err_t imxrt_control(struct rt_serial_device *serial, int cmd, void *arg)
  210. {
  211. struct imxrt_uart *uart;
  212. RT_ASSERT(serial != RT_NULL);
  213. uart = (struct imxrt_uart *)serial->parent.user_data;
  214. switch (cmd)
  215. {
  216. case RT_DEVICE_CTRL_CLR_INT:
  217. /* disable interrupt */
  218. LPUART_DisableInterrupts(uart->uart_base, kLPUART_RxDataRegFullInterruptEnable);
  219. /* disable rx irq */
  220. DisableIRQ(uart->irqn);
  221. break;
  222. case RT_DEVICE_CTRL_SET_INT:
  223. /* enable interrupt */
  224. LPUART_EnableInterrupts(uart->uart_base, kLPUART_RxDataRegFullInterruptEnable);
  225. /* enable rx irq */
  226. EnableIRQ(uart->irqn);
  227. break;
  228. }
  229. return RT_EOK;
  230. }
  231. static int imxrt_putc(struct rt_serial_device *serial, char ch)
  232. {
  233. struct imxrt_uart *uart;
  234. RT_ASSERT(serial != RT_NULL);
  235. uart = (struct imxrt_uart *)serial->parent.user_data;
  236. LPUART_WriteByte(uart->uart_base, ch);
  237. while(!(LPUART_GetStatusFlags(uart->uart_base) & kLPUART_TxDataRegEmptyFlag));
  238. return 1;
  239. }
  240. static int imxrt_getc(struct rt_serial_device *serial)
  241. {
  242. int ch;
  243. struct imxrt_uart *uart;
  244. RT_ASSERT(serial != RT_NULL);
  245. uart = (struct imxrt_uart *)serial->parent.user_data;
  246. ch = -1;
  247. if (LPUART_GetStatusFlags(uart->uart_base) & kLPUART_RxDataRegFullFlag)
  248. ch = LPUART_ReadByte(uart->uart_base);
  249. return ch;
  250. }
  251. /**
  252. * Uart common interrupt process. This need add to uart ISR.
  253. *
  254. * @param serial serial device
  255. */
  256. static void uart_isr(struct rt_serial_device *serial)
  257. {
  258. struct imxrt_uart *uart;
  259. LPUART_Type *base;
  260. RT_ASSERT(serial != RT_NULL);
  261. uart = (struct imxrt_uart *) serial->parent.user_data;
  262. RT_ASSERT(uart != RT_NULL);
  263. base = uart->uart_base;
  264. RT_ASSERT(base != RT_NULL);
  265. /* enter interrupt */
  266. rt_interrupt_enter();
  267. /* UART in mode Receiver -------------------------------------------------*/
  268. if (LPUART_GetStatusFlags(base) & kLPUART_RxDataRegFullFlag)
  269. {
  270. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  271. }
  272. /* If RX overrun. */
  273. if (LPUART_STAT_OR_MASK & base->STAT)
  274. {
  275. /* Clear overrun flag, otherwise the RX does not work. */
  276. base->STAT = ((base->STAT & 0x3FE00000U) | LPUART_STAT_OR_MASK);
  277. }
  278. /* leave interrupt */
  279. rt_interrupt_leave();
  280. }
  281. static const struct rt_uart_ops imxrt_uart_ops =
  282. {
  283. imxrt_configure,
  284. imxrt_control,
  285. imxrt_putc,
  286. imxrt_getc,
  287. };
  288. int imxrt_hw_usart_init(void)
  289. {
  290. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  291. int i;
  292. for (i = 0; i < sizeof(uarts) / sizeof(uarts[0]); i++)
  293. {
  294. uarts[i].serial->ops = &imxrt_uart_ops;
  295. uarts[i].serial->config = config;
  296. /* register UART1 device */
  297. rt_hw_serial_register(uarts[i].serial,
  298. uarts[i].device_name,
  299. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  300. (void *)&uarts[i]);
  301. }
  302. return 0;
  303. }
  304. INIT_BOARD_EXPORT(imxrt_hw_usart_init);
  305. #endif /*RT_USING_SERIAL */