drv_uart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * File : drv_uart.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. * 2018-03-17 laiyiketang Add other uart.
  14. */
  15. #include <rtthread.h>
  16. #include "drv_uart.h"
  17. #include "fsl_common.h"
  18. #include "fsl_lpuart.h"
  19. #include "fsl_iomuxc.h"
  20. #ifdef RT_USING_SERIAL
  21. #if defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL
  22. #error "Please don't define 'FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL'!"
  23. #endif
  24. #if !defined(RT_USING_UART1) && !defined(RT_USING_UART2) && \
  25. !defined(RT_USING_UART3) && !defined(RT_USING_UART4) && \
  26. !defined(RT_USING_UART5) && !defined(RT_USING_UART6) && \
  27. !defined(RT_USING_UART7) && !defined(RT_USING_UART8)
  28. #error "Please define at least one UARTx"
  29. #endif
  30. #include <rtdevice.h>
  31. /* imxrt uart driver */
  32. struct imxrt_uart
  33. {
  34. LPUART_Type *uart_base;
  35. IRQn_Type irqn;
  36. struct rt_serial_device *serial;
  37. char *device_name;
  38. };
  39. static void uart_isr(struct rt_serial_device *serial);
  40. #if defined(RT_USING_UART1)
  41. struct rt_serial_device serial1;
  42. void LPUART1_IRQHandler(void)
  43. {
  44. uart_isr(&serial1);
  45. }
  46. #endif /* RT_USING_UART1 */
  47. #if defined(RT_USING_UART2)
  48. struct rt_serial_device serial2;
  49. void LPUART2_IRQHandler(void)
  50. {
  51. uart_isr(&serial2);
  52. }
  53. #endif /* RT_USING_UART2 */
  54. #if defined(RT_USING_UART3)
  55. struct rt_serial_device serial3;
  56. void LPUART3_IRQHandler(void)
  57. {
  58. uart_isr(&serial3);
  59. }
  60. #endif /* RT_USING_UART3 */
  61. #if defined(RT_USING_UART4)
  62. struct rt_serial_device serial4;
  63. void LPUART4_IRQHandler(void)
  64. {
  65. uart_isr(&serial4);
  66. }
  67. #endif /* RT_USING_UART4 */
  68. #if defined(RT_USING_UART5)
  69. struct rt_serial_device serial5;
  70. void LPUART5_IRQHandler(void)
  71. {
  72. uart_isr(&serial5);
  73. }
  74. #endif /* RT_USING_UART5 */
  75. #if defined(RT_USING_UART6)
  76. struct rt_serial_device serial6;
  77. void LPUART6_IRQHandler(void)
  78. {
  79. uart_isr(&serial6);
  80. }
  81. #endif /* RT_USING_UART6 */
  82. #if defined(RT_USING_UART7)
  83. struct rt_serial_device serial7;
  84. void LPUART7_IRQHandler(void)
  85. {
  86. uart_isr(&serial7);
  87. }
  88. #endif /* RT_USING_UART7 */
  89. #if defined(RT_USING_UART8)
  90. struct rt_serial_device serial8;
  91. void LPUART8_IRQHandler(void)
  92. {
  93. uart_isr(&serial8);
  94. }
  95. #endif /* RT_USING_UART8 */
  96. static const struct imxrt_uart uarts[] =
  97. {
  98. #ifdef RT_USING_UART1
  99. {
  100. LPUART1,
  101. LPUART1_IRQn,
  102. &serial1,
  103. "uart1",
  104. },
  105. #endif
  106. #ifdef RT_USING_UART2
  107. {
  108. LPUART2,
  109. LPUART2_IRQn,
  110. &serial2,
  111. "uart2",
  112. },
  113. #endif
  114. #ifdef RT_USING_UART3
  115. {
  116. LPUART3,
  117. LPUART3_IRQn,
  118. &serial3,
  119. "uart3",
  120. },
  121. #endif
  122. #ifdef RT_USING_UART4
  123. {
  124. LPUART4,
  125. LPUART4_IRQn,
  126. &serial4,
  127. "uart4",
  128. },
  129. #endif
  130. #ifdef RT_USING_UART5
  131. {
  132. LPUART5,
  133. LPUART5_IRQn,
  134. &serial5,
  135. "uart5",
  136. },
  137. #endif
  138. #ifdef RT_USING_UART6
  139. {
  140. LPUART6,
  141. LPUART6_IRQn,
  142. &serial6,
  143. "uart6",
  144. },
  145. #endif
  146. #ifdef RT_USING_UART7
  147. {
  148. LPUART7,
  149. LPUART7_IRQn,
  150. &serial7,
  151. "uart7",
  152. },
  153. #endif
  154. #ifdef RT_USING_UART8
  155. {
  156. LPUART8,
  157. LPUART8_IRQn,
  158. &serial8,
  159. "uart8",
  160. },
  161. #endif
  162. };
  163. /* Get debug console frequency. */
  164. uint32_t GetUartSrcFreq(void)
  165. {
  166. uint32_t freq;
  167. /* To make it simple, we assume default PLL and divider settings, and the only variable
  168. from application is use PLL3 source or OSC source */
  169. if (CLOCK_GetMux(kCLOCK_UartMux) == 0) /* PLL3 div6 80M */
  170. {
  171. freq = (CLOCK_GetPllFreq(kCLOCK_PllUsb1) / 6U) / (CLOCK_GetDiv(kCLOCK_UartDiv) + 1U);
  172. }
  173. else
  174. {
  175. freq = CLOCK_GetOscFreq() / (CLOCK_GetDiv(kCLOCK_UartDiv) + 1U);
  176. }
  177. return freq;
  178. }
  179. /**
  180. * @brief UART MSP Initialization
  181. * This function configures the hardware resources used in this example:
  182. * - Peripheral's clock enable
  183. * - Peripheral's GPIO Configuration
  184. * - NVIC configuration for UART interrupt request enable
  185. * @param huart: UART handle pointer
  186. * @retval None
  187. */
  188. void imxrt_uart_gpio_init(struct imxrt_uart *uart)
  189. {
  190. if (uart->uart_base != RT_NULL)
  191. {
  192. #ifdef RT_USING_UART1
  193. IOMUXC_SetPinMux(
  194. IOMUXC_GPIO_AD_B0_12_LPUART1_TX, /* GPIO_AD_B0_12 is configured as LPUART1_TX */
  195. 0U); /* Software Input On Field: Input Path is determined by functionality */
  196. IOMUXC_SetPinMux(
  197. IOMUXC_GPIO_AD_B0_13_LPUART1_RX, /* GPIO_AD_B0_13 is configured as LPUART1_RX */
  198. 0U); /* Software Input On Field: Input Path is determined by functionality */
  199. IOMUXC_SetPinConfig(
  200. IOMUXC_GPIO_AD_B0_12_LPUART1_TX, /* GPIO_AD_B0_12 PAD functional properties : */
  201. 0x10B0u); /* Slew Rate Field: Slow Slew Rate
  202. Drive Strength Field: R0/6
  203. Speed Field: medium(100MHz)
  204. Open Drain Enable Field: Open Drain Disabled
  205. Pull / Keep Enable Field: Pull/Keeper Enabled
  206. Pull / Keep Select Field: Keeper
  207. Pull Up / Down Config. Field: 100K Ohm Pull Down
  208. Hyst. Enable Field: Hysteresis Disabled */
  209. IOMUXC_SetPinConfig(
  210. IOMUXC_GPIO_AD_B0_13_LPUART1_RX, /* GPIO_AD_B0_13 PAD functional properties : */
  211. 0x10B0u); /* Slew Rate Field: Slow Slew Rate
  212. Drive Strength Field: R0/6
  213. Speed Field: medium(100MHz)
  214. Open Drain Enable Field: Open Drain Disabled
  215. Pull / Keep Enable Field: Pull/Keeper Enabled
  216. Pull / Keep Select Field: Keeper
  217. Pull Up / Down Config. Field: 100K Ohm Pull Down
  218. Hyst. Enable Field: Hysteresis Disabled */
  219. #endif
  220. #ifdef RT_USING_UART2
  221. IOMUXC_SetPinMux(
  222. IOMUXC_GPIO_AD_B1_02_LPUART2_TX,
  223. 0U);
  224. IOMUXC_SetPinMux(
  225. IOMUXC_GPIO_AD_B1_03_LPUART2_RX,
  226. 0U);
  227. IOMUXC_SetPinConfig(
  228. IOMUXC_GPIO_AD_B1_02_LPUART2_TX,
  229. 0x10B0u);
  230. IOMUXC_SetPinConfig(
  231. IOMUXC_GPIO_AD_B1_03_LPUART2_RX,
  232. 0x10B0u);
  233. #endif
  234. #ifdef RT_USING_UART3
  235. IOMUXC_SetPinMux(
  236. IOMUXC_GPIO_AD_B1_06_LPUART3_TX,
  237. 0U);
  238. IOMUXC_SetPinMux(
  239. IOMUXC_GPIO_AD_B1_07_LPUART3_RX,
  240. 0U);
  241. IOMUXC_SetPinConfig(
  242. IOMUXC_GPIO_AD_B1_06_LPUART3_TX,
  243. 0x10B0u);
  244. IOMUXC_SetPinConfig(
  245. IOMUXC_GPIO_AD_B1_07_LPUART3_RX,
  246. 0x10B0u);
  247. #endif
  248. #ifdef RT_USING_UART4
  249. #ifdef BOARD_RT1050_ATK
  250. IOMUXC_SetPinMux(
  251. IOMUXC_GPIO_SD_B1_00_LPUART4_TX,
  252. 0U);
  253. IOMUXC_SetPinMux(
  254. IOMUXC_GPIO_SD_B1_01_LPUART4_RX,
  255. 0U);
  256. IOMUXC_SetPinConfig(
  257. IOMUXC_GPIO_SD_B1_00_LPUART4_TX,
  258. 0x10B0u);
  259. IOMUXC_SetPinConfig(
  260. IOMUXC_GPIO_SD_B1_01_LPUART4_RX,
  261. 0x10B0u);
  262. #else
  263. IOMUXC_SetPinMux(
  264. IOMUXC_GPIO_B1_00_LPUART4_TX,
  265. 0U);
  266. IOMUXC_SetPinMux(
  267. IOMUXC_GPIO_B1_01_LPUART4_RX,
  268. 0U);
  269. IOMUXC_SetPinConfig(
  270. IOMUXC_GPIO_B1_00_LPUART4_TX,
  271. 0x10B0u);
  272. IOMUXC_SetPinConfig(
  273. IOMUXC_GPIO_B1_01_LPUART4_RX,
  274. 0x10B0u);
  275. #endif
  276. #endif
  277. #ifdef RT_USING_UART5
  278. IOMUXC_SetPinMux(
  279. IOMUXC_GPIO_B1_12_LPUART5_TX,
  280. 0U);
  281. IOMUXC_SetPinMux(
  282. IOMUXC_GPIO_B1_13_LPUART5_RX,
  283. 0U);
  284. IOMUXC_SetPinConfig(
  285. IOMUXC_GPIO_B1_12_LPUART5_TX,
  286. 0x10B0u);
  287. IOMUXC_SetPinConfig(
  288. IOMUXC_GPIO_B1_13_LPUART5_RX,
  289. 0x10B0u);
  290. #endif
  291. #ifdef RT_USING_UART6
  292. IOMUXC_SetPinMux(
  293. IOMUXC_GPIO_AD_B0_02_LPUART6_TX,
  294. 0U);
  295. IOMUXC_SetPinMux(
  296. IOMUXC_GPIO_AD_B0_03_LPUART6_RX,
  297. 0U);
  298. IOMUXC_SetPinConfig(
  299. IOMUXC_GPIO_AD_B0_02_LPUART6_TX,
  300. 0x10B0u);
  301. IOMUXC_SetPinConfig(
  302. IOMUXC_GPIO_AD_B0_03_LPUART6_RX,
  303. 0x10B0u);
  304. #endif
  305. #ifdef RT_USING_UART7
  306. IOMUXC_SetPinMux(
  307. IOMUXC_GPIO_EMC_31_LPUART7_TX,
  308. 0U);
  309. IOMUXC_SetPinMux(
  310. IOMUXC_GPIO_EMC_32_LPUART7_RX,
  311. 0U);
  312. IOMUXC_SetPinConfig(
  313. IOMUXC_GPIO_EMC_31_LPUART7_TX,
  314. 0x10B0u);
  315. IOMUXC_SetPinConfig(
  316. IOMUXC_GPIO_EMC_32_LPUART7_RX,
  317. 0x10B0u);
  318. #endif
  319. #ifdef RT_USING_UART8
  320. IOMUXC_SetPinMux(
  321. IOMUXC_GPIO_AD_B1_10_LPUART8_TX,
  322. 0U);
  323. IOMUXC_SetPinMux(
  324. IOMUXC_GPIO_AD_B1_11_LPUART8_RX,
  325. 0U);
  326. IOMUXC_SetPinConfig(
  327. IOMUXC_GPIO_AD_B1_10_LPUART8_TX,
  328. 0x10B0u);
  329. IOMUXC_SetPinConfig(
  330. IOMUXC_GPIO_AD_B1_11_LPUART8_RX,
  331. 0x10B0u);
  332. #endif
  333. }
  334. else
  335. {
  336. RT_ASSERT(RT_NULL);
  337. }
  338. }
  339. static rt_err_t imxrt_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  340. {
  341. struct imxrt_uart *uart;
  342. lpuart_config_t config;
  343. RT_ASSERT(serial != RT_NULL);
  344. RT_ASSERT(cfg != RT_NULL);
  345. uart = (struct imxrt_uart *)serial->parent.user_data;
  346. imxrt_uart_gpio_init(uart);
  347. LPUART_GetDefaultConfig(&config);
  348. config.baudRate_Bps = cfg->baud_rate;
  349. switch (cfg->data_bits)
  350. {
  351. case DATA_BITS_7:
  352. config.dataBitsCount = kLPUART_SevenDataBits;
  353. break;
  354. default:
  355. config.dataBitsCount = kLPUART_EightDataBits;
  356. break;
  357. }
  358. switch (cfg->stop_bits)
  359. {
  360. case STOP_BITS_2:
  361. config.stopBitCount = kLPUART_TwoStopBit;
  362. break;
  363. default:
  364. config.stopBitCount = kLPUART_OneStopBit;
  365. break;
  366. }
  367. switch (cfg->parity)
  368. {
  369. case PARITY_ODD:
  370. config.parityMode = kLPUART_ParityOdd;
  371. break;
  372. case PARITY_EVEN:
  373. config.parityMode = kLPUART_ParityEven;
  374. break;
  375. default:
  376. config.parityMode = kLPUART_ParityDisabled;
  377. break;
  378. }
  379. config.enableTx = true;
  380. config.enableRx = true;
  381. LPUART_Init(uart->uart_base, &config, GetUartSrcFreq());
  382. LPUART_EnableInterrupts(uart->uart_base, kLPUART_RxDataRegFullInterruptEnable);
  383. return RT_EOK;
  384. }
  385. static rt_err_t imxrt_control(struct rt_serial_device *serial, int cmd, void *arg)
  386. {
  387. struct imxrt_uart *uart;
  388. RT_ASSERT(serial != RT_NULL);
  389. uart = (struct imxrt_uart *)serial->parent.user_data;
  390. switch (cmd)
  391. {
  392. case RT_DEVICE_CTRL_CLR_INT:
  393. /* disable rx irq */
  394. DisableIRQ(uart->irqn);
  395. break;
  396. case RT_DEVICE_CTRL_SET_INT:
  397. /* enable rx irq */
  398. EnableIRQ(uart->irqn);
  399. break;
  400. }
  401. return RT_EOK;
  402. }
  403. static int imxrt_putc(struct rt_serial_device *serial, char ch)
  404. {
  405. struct imxrt_uart *uart;
  406. RT_ASSERT(serial != RT_NULL);
  407. uart = (struct imxrt_uart *)serial->parent.user_data;
  408. LPUART_WriteByte(uart->uart_base, ch);
  409. while (!(LPUART_GetStatusFlags(uart->uart_base) & kLPUART_TxDataRegEmptyFlag));
  410. return 1;
  411. }
  412. static int imxrt_getc(struct rt_serial_device *serial)
  413. {
  414. int ch;
  415. struct imxrt_uart *uart;
  416. RT_ASSERT(serial != RT_NULL);
  417. uart = (struct imxrt_uart *)serial->parent.user_data;
  418. ch = -1;
  419. if (LPUART_GetStatusFlags(uart->uart_base) & kLPUART_RxDataRegFullFlag)
  420. ch = LPUART_ReadByte(uart->uart_base);
  421. return ch;
  422. }
  423. /**
  424. * Uart common interrupt process. This need add to uart ISR.
  425. *
  426. * @param serial serial device
  427. */
  428. static void uart_isr(struct rt_serial_device *serial)
  429. {
  430. struct imxrt_uart *uart;
  431. LPUART_Type *base;
  432. RT_ASSERT(serial != RT_NULL);
  433. uart = (struct imxrt_uart *) serial->parent.user_data;
  434. RT_ASSERT(uart != RT_NULL);
  435. base = uart->uart_base;
  436. RT_ASSERT(base != RT_NULL);
  437. /* enter interrupt */
  438. rt_interrupt_enter();
  439. /* UART in mode Receiver -------------------------------------------------*/
  440. if (LPUART_GetStatusFlags(base) & kLPUART_RxDataRegFullFlag)
  441. {
  442. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  443. }
  444. /* If RX overrun. */
  445. if (LPUART_STAT_OR_MASK & base->STAT)
  446. {
  447. /* Clear overrun flag, otherwise the RX does not work. */
  448. base->STAT = ((base->STAT & 0x3FE00000U) | LPUART_STAT_OR_MASK);
  449. }
  450. /* leave interrupt */
  451. rt_interrupt_leave();
  452. }
  453. static const struct rt_uart_ops imxrt_uart_ops =
  454. {
  455. imxrt_configure,
  456. imxrt_control,
  457. imxrt_putc,
  458. imxrt_getc,
  459. };
  460. int imxrt_hw_uart_init(void)
  461. {
  462. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  463. int i;
  464. /* Configure UART divider to default */
  465. CLOCK_SetMux(kCLOCK_UartMux, 0); /* Set UART source to PLL3 80M */
  466. CLOCK_SetDiv(kCLOCK_UartDiv, 0); /* Set UART divider to 1 */
  467. for (i = 0; i < sizeof(uarts) / sizeof(uarts[0]); i++)
  468. {
  469. uarts[i].serial->ops = &imxrt_uart_ops;
  470. uarts[i].serial->config = config;
  471. /* register UART device */
  472. rt_hw_serial_register(uarts[i].serial,
  473. uarts[i].device_name,
  474. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  475. (void *)&uarts[i]);
  476. }
  477. return 0;
  478. }
  479. INIT_BOARD_EXPORT(imxrt_hw_uart_init);
  480. #endif /*RT_USING_SERIAL */