Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

1812 righe
61 KiB

  1. /*
  2. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2020 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_uart.h"
  9. /*******************************************************************************
  10. * Definitions
  11. ******************************************************************************/
  12. /* Component ID definition, used by tools. */
  13. #ifndef FSL_COMPONENT_ID
  14. #define FSL_COMPONENT_ID "platform.drivers.uart"
  15. #endif
  16. /* UART transfer state. */
  17. enum
  18. {
  19. kUART_TxIdle, /* TX idle. */
  20. kUART_TxBusy, /* TX busy. */
  21. kUART_RxIdle, /* RX idle. */
  22. kUART_RxBusy, /* RX busy. */
  23. kUART_RxFramingError, /* Rx framing error */
  24. kUART_RxParityError /* Rx parity error */
  25. };
  26. /* Typedef for interrupt handler. */
  27. typedef void (*uart_isr_t)(UART_Type *base, uart_handle_t *handle);
  28. /*******************************************************************************
  29. * Prototypes
  30. ******************************************************************************/
  31. /*!
  32. * @brief Check whether the RX ring buffer is full.
  33. *
  34. * @param handle UART handle pointer.
  35. * @retval true RX ring buffer is full.
  36. * @retval false RX ring buffer is not full.
  37. */
  38. static bool UART_TransferIsRxRingBufferFull(uart_handle_t *handle);
  39. /*!
  40. * @brief Read RX register using non-blocking method.
  41. *
  42. * This function reads data from the TX register directly, upper layer must make
  43. * sure the RX register is full or TX FIFO has data before calling this function.
  44. *
  45. * @param base UART peripheral base address.
  46. * @param data Start address of the buffer to store the received data.
  47. * @param length Size of the buffer.
  48. */
  49. static void UART_ReadNonBlocking(UART_Type *base, uint8_t *data, size_t length);
  50. /*!
  51. * @brief Write to TX register using non-blocking method.
  52. *
  53. * This function writes data to the TX register directly, upper layer must make
  54. * sure the TX register is empty or TX FIFO has empty room before calling this function.
  55. *
  56. * @note This function does not check whether all the data has been sent out to bus,
  57. * so before disable TX, check kUART_TransmissionCompleteFlag to ensure the TX is
  58. * finished.
  59. *
  60. * @param base UART peripheral base address.
  61. * @param data Start address of the data to write.
  62. * @param length Size of the buffer to be sent.
  63. */
  64. static void UART_WriteNonBlocking(UART_Type *base, const uint8_t *data, size_t length);
  65. /*******************************************************************************
  66. * Variables
  67. ******************************************************************************/
  68. /* Array of UART handle. */
  69. #if (defined(UART5))
  70. #define UART_HANDLE_ARRAY_SIZE 6
  71. #else /* UART5 */
  72. #if (defined(UART4))
  73. #define UART_HANDLE_ARRAY_SIZE 5
  74. #else /* UART4 */
  75. #if (defined(UART3))
  76. #define UART_HANDLE_ARRAY_SIZE 4
  77. #else /* UART3 */
  78. #if (defined(UART2))
  79. #define UART_HANDLE_ARRAY_SIZE 3
  80. #else /* UART2 */
  81. #if (defined(UART1))
  82. #define UART_HANDLE_ARRAY_SIZE 2
  83. #else /* UART1 */
  84. #if (defined(UART0))
  85. #define UART_HANDLE_ARRAY_SIZE 1
  86. #else /* UART0 */
  87. #error No UART instance.
  88. #endif /* UART 0 */
  89. #endif /* UART 1 */
  90. #endif /* UART 2 */
  91. #endif /* UART 3 */
  92. #endif /* UART 4 */
  93. #endif /* UART 5 */
  94. static uart_handle_t *s_uartHandle[UART_HANDLE_ARRAY_SIZE];
  95. /* Array of UART peripheral base address. */
  96. static UART_Type *const s_uartBases[] = UART_BASE_PTRS;
  97. /* Array of UART IRQ number. */
  98. static const IRQn_Type s_uartIRQ[] = UART_RX_TX_IRQS;
  99. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  100. /* Array of UART clock name. */
  101. static const clock_ip_name_t s_uartClock[] = UART_CLOCKS;
  102. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  103. /* UART ISR for transactional APIs. */
  104. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  105. static uart_isr_t s_uartIsr = (uart_isr_t)DefaultISR;
  106. #else
  107. static uart_isr_t s_uartIsr;
  108. #endif
  109. /*******************************************************************************
  110. * Code
  111. ******************************************************************************/
  112. /*!
  113. * brief Get the UART instance from peripheral base address.
  114. *
  115. * param base UART peripheral base address.
  116. * return UART instance.
  117. */
  118. uint32_t UART_GetInstance(UART_Type *base)
  119. {
  120. uint32_t instance;
  121. uint32_t uartArrayCount = (sizeof(s_uartBases) / sizeof(s_uartBases[0]));
  122. /* Find the instance index from base address mappings. */
  123. for (instance = 0; instance < uartArrayCount; instance++)
  124. {
  125. if (s_uartBases[instance] == base)
  126. {
  127. break;
  128. }
  129. }
  130. assert(instance < uartArrayCount);
  131. return instance;
  132. }
  133. /*!
  134. * brief Get the length of received data in RX ring buffer.
  135. *
  136. * param handle UART handle pointer.
  137. * return Length of received data in RX ring buffer.
  138. */
  139. size_t UART_TransferGetRxRingBufferLength(uart_handle_t *handle)
  140. {
  141. assert(handle);
  142. size_t size;
  143. uint16_t rxRingBufferHead = handle->rxRingBufferHead;
  144. uint16_t rxRingBufferTail = handle->rxRingBufferTail;
  145. if (rxRingBufferTail > rxRingBufferHead)
  146. {
  147. size = (size_t)rxRingBufferHead + handle->rxRingBufferSize - (size_t)rxRingBufferTail;
  148. }
  149. else
  150. {
  151. size = (size_t)rxRingBufferHead - (size_t)rxRingBufferTail;
  152. }
  153. return size;
  154. }
  155. static bool UART_TransferIsRxRingBufferFull(uart_handle_t *handle)
  156. {
  157. assert(handle);
  158. bool full;
  159. if (UART_TransferGetRxRingBufferLength(handle) == (handle->rxRingBufferSize - 1U))
  160. {
  161. full = true;
  162. }
  163. else
  164. {
  165. full = false;
  166. }
  167. return full;
  168. }
  169. /*!
  170. * brief Initializes a UART instance with a user configuration structure and peripheral clock.
  171. *
  172. * This function configures the UART module with the user-defined settings. The user can configure the configuration
  173. * structure and also get the default configuration by using the UART_GetDefaultConfig() function.
  174. * The example below shows how to use this API to configure UART.
  175. * code
  176. * uart_config_t uartConfig;
  177. * uartConfig.baudRate_Bps = 115200U;
  178. * uartConfig.parityMode = kUART_ParityDisabled;
  179. * uartConfig.stopBitCount = kUART_OneStopBit;
  180. * uartConfig.txFifoWatermark = 0;
  181. * uartConfig.rxFifoWatermark = 1;
  182. * UART_Init(UART1, &uartConfig, 20000000U);
  183. * endcode
  184. *
  185. * param base UART peripheral base address.
  186. * param config Pointer to the user-defined configuration structure.
  187. * param srcClock_Hz UART clock source frequency in HZ.
  188. * retval kStatus_UART_BaudrateNotSupport Baudrate is not support in current clock source.
  189. * retval kStatus_Success Status UART initialize succeed
  190. */
  191. status_t UART_Init(UART_Type *base, const uart_config_t *config, uint32_t srcClock_Hz)
  192. {
  193. assert(config);
  194. assert(config->baudRate_Bps);
  195. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  196. assert((uint8_t)FSL_FEATURE_UART_FIFO_SIZEn(base) >= config->txFifoWatermark);
  197. assert((uint8_t)FSL_FEATURE_UART_FIFO_SIZEn(base) >= config->rxFifoWatermark);
  198. #endif
  199. uint32_t sbr = 0U;
  200. uint8_t temp = 0U;
  201. uint32_t baudDiff = 0U;
  202. /* Calculate the baud rate modulo divisor, sbr*/
  203. sbr = srcClock_Hz / (config->baudRate_Bps * 16U);
  204. /* set sbrTemp to 1 if the sourceClockInHz can not satisfy the desired baud rate */
  205. if (sbr == 0U)
  206. {
  207. sbr = 1U;
  208. }
  209. #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
  210. /* Determine if a fractional divider is needed to fine tune closer to the
  211. * desired baud, each value of brfa is in 1/32 increments,
  212. * hence the multiply-by-32. */
  213. uint32_t tempBaud = 0U;
  214. uint32_t brfa = (2U * srcClock_Hz / (config->baudRate_Bps)) - 32U * sbr;
  215. /* Calculate the baud rate based on the temporary SBR values and BRFA */
  216. tempBaud = srcClock_Hz * 2U / (sbr * 32U + brfa);
  217. baudDiff =
  218. (tempBaud > config->baudRate_Bps) ? (tempBaud - config->baudRate_Bps) : (config->baudRate_Bps - tempBaud);
  219. #else
  220. /* Calculate the baud rate based on the temporary SBR values */
  221. baudDiff = (srcClock_Hz / (sbr * 16U)) - config->baudRate_Bps;
  222. /* Select the better value between sbr and (sbr + 1) */
  223. if (baudDiff > (config->baudRate_Bps - (srcClock_Hz / (16U * ((uint32_t)sbr + 1U)))))
  224. {
  225. baudDiff = config->baudRate_Bps - (srcClock_Hz / (16U * ((uint32_t)sbr + 1U)));
  226. sbr++;
  227. }
  228. #endif
  229. /* next, check to see if actual baud rate is within 3% of desired baud rate
  230. * based on the calculate SBR value */
  231. if (baudDiff > ((config->baudRate_Bps / 100U) * 3U))
  232. {
  233. /* Unacceptable baud rate difference of more than 3%*/
  234. return kStatus_UART_BaudrateNotSupport;
  235. }
  236. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  237. /* Enable uart clock */
  238. CLOCK_EnableClock(s_uartClock[UART_GetInstance(base)]);
  239. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  240. /* Disable UART TX RX before setting. */
  241. base->C2 &= ~((uint8_t)UART_C2_TE_MASK | (uint8_t)UART_C2_RE_MASK);
  242. /* Write the sbr value to the BDH and BDL registers*/
  243. base->BDH = (base->BDH & ~(uint8_t)UART_BDH_SBR_MASK) | (uint8_t)(sbr >> 8);
  244. base->BDL = (uint8_t)sbr;
  245. #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
  246. /* Write the brfa value to the register*/
  247. base->C4 = (base->C4 & ~(uint8_t)UART_C4_BRFA_MASK) | ((uint8_t)brfa & UART_C4_BRFA_MASK);
  248. #endif
  249. /* Set bit count/parity mode/idle type. */
  250. temp = base->C1 &
  251. ~((uint8_t)UART_C1_PE_MASK | (uint8_t)UART_C1_PT_MASK | (uint8_t)UART_C1_M_MASK | (uint8_t)UART_C1_ILT_MASK);
  252. temp |= UART_C1_ILT(config->idleType);
  253. if (kUART_ParityDisabled != config->parityMode)
  254. {
  255. temp |= (UART_C1_M_MASK | (uint8_t)config->parityMode);
  256. }
  257. base->C1 = temp;
  258. #if defined(FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
  259. /* Set stop bit per char */
  260. base->BDH = (base->BDH & ~(uint8_t)UART_BDH_SBNS_MASK) | (uint8_t)UART_BDH_SBNS((uint8_t)config->stopBitCount);
  261. #endif
  262. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  263. /* Set tx/rx FIFO watermark
  264. Note:
  265. Take care of the RX FIFO, RX interrupt request only assert when received bytes
  266. equal or more than RX water mark, there is potential issue if RX water
  267. mark larger than 1.
  268. For example, if RX FIFO water mark is 2, upper layer needs 5 bytes and
  269. 5 bytes are received. the last byte will be saved in FIFO but not trigger
  270. RX interrupt because the water mark is 2.
  271. */
  272. base->TWFIFO = config->txFifoWatermark;
  273. base->RWFIFO = config->rxFifoWatermark;
  274. /* Enable tx/rx FIFO */
  275. base->PFIFO |= (UART_PFIFO_TXFE_MASK | UART_PFIFO_RXFE_MASK);
  276. /* Flush FIFO */
  277. base->CFIFO |= (UART_CFIFO_TXFLUSH_MASK | UART_CFIFO_RXFLUSH_MASK);
  278. #endif
  279. #if defined(FSL_FEATURE_UART_HAS_MODEM_SUPPORT) && FSL_FEATURE_UART_HAS_MODEM_SUPPORT
  280. if (config->enableRxRTS)
  281. {
  282. /* Enable receiver RTS(request-to-send) function. */
  283. base->MODEM |= UART_MODEM_RXRTSE_MASK;
  284. }
  285. if (config->enableTxCTS)
  286. {
  287. /* Enable transmitter CTS(clear-to-send) function. */
  288. base->MODEM |= UART_MODEM_TXCTSE_MASK;
  289. }
  290. #endif
  291. /* Enable TX/RX base on configure structure. */
  292. temp = base->C2;
  293. if (config->enableTx)
  294. {
  295. temp |= UART_C2_TE_MASK;
  296. }
  297. if (config->enableRx)
  298. {
  299. temp |= UART_C2_RE_MASK;
  300. }
  301. base->C2 = temp;
  302. return kStatus_Success;
  303. }
  304. /*!
  305. * brief Deinitializes a UART instance.
  306. *
  307. * This function waits for TX complete, disables TX and RX, and disables the UART clock.
  308. *
  309. * param base UART peripheral base address.
  310. */
  311. void UART_Deinit(UART_Type *base)
  312. {
  313. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  314. /* Wait tx FIFO send out*/
  315. while (0U != base->TCFIFO)
  316. {
  317. }
  318. #endif
  319. /* Wait last char shoft out */
  320. while (0U == (base->S1 & UART_S1_TC_MASK))
  321. {
  322. }
  323. /* Disable the module. */
  324. base->C2 = 0;
  325. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  326. /* Disable uart clock */
  327. CLOCK_DisableClock(s_uartClock[UART_GetInstance(base)]);
  328. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  329. }
  330. /*!
  331. * brief Gets the default configuration structure.
  332. *
  333. * This function initializes the UART configuration structure to a default value. The default
  334. * values are as follows.
  335. * uartConfig->baudRate_Bps = 115200U;
  336. * uartConfig->bitCountPerChar = kUART_8BitsPerChar;
  337. * uartConfig->parityMode = kUART_ParityDisabled;
  338. * uartConfig->stopBitCount = kUART_OneStopBit;
  339. * uartConfig->txFifoWatermark = 0;
  340. * uartConfig->rxFifoWatermark = 1;
  341. * uartConfig->idleType = kUART_IdleTypeStartBit;
  342. * uartConfig->enableTx = false;
  343. * uartConfig->enableRx = false;
  344. *
  345. * param config Pointer to configuration structure.
  346. */
  347. void UART_GetDefaultConfig(uart_config_t *config)
  348. {
  349. assert(config);
  350. /* Initializes the configure structure to zero. */
  351. (void)memset(config, 0, sizeof(*config));
  352. config->baudRate_Bps = 115200U;
  353. config->parityMode = kUART_ParityDisabled;
  354. #if defined(FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
  355. config->stopBitCount = kUART_OneStopBit;
  356. #endif
  357. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  358. config->txFifoWatermark = 0;
  359. config->rxFifoWatermark = 1;
  360. #endif
  361. #if defined(FSL_FEATURE_UART_HAS_MODEM_SUPPORT) && FSL_FEATURE_UART_HAS_MODEM_SUPPORT
  362. config->enableRxRTS = false;
  363. config->enableTxCTS = false;
  364. #endif
  365. config->idleType = kUART_IdleTypeStartBit;
  366. config->enableTx = false;
  367. config->enableRx = false;
  368. }
  369. /*!
  370. * brief Sets the UART instance baud rate.
  371. *
  372. * This function configures the UART module baud rate. This function is used to update
  373. * the UART module baud rate after the UART module is initialized by the UART_Init.
  374. * code
  375. * UART_SetBaudRate(UART1, 115200U, 20000000U);
  376. * endcode
  377. *
  378. * param base UART peripheral base address.
  379. * param baudRate_Bps UART baudrate to be set.
  380. * param srcClock_Hz UART clock source frequency in Hz.
  381. * retval kStatus_UART_BaudrateNotSupport Baudrate is not support in the current clock source.
  382. * retval kStatus_Success Set baudrate succeeded.
  383. */
  384. status_t UART_SetBaudRate(UART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
  385. {
  386. assert(baudRate_Bps);
  387. uint32_t sbr = 0;
  388. uint32_t baudDiff = 0;
  389. uint8_t oldCtrl;
  390. /* Calculate the baud rate modulo divisor, sbr*/
  391. sbr = srcClock_Hz / (baudRate_Bps * 16U);
  392. /* set sbrTemp to 1 if the sourceClockInHz can not satisfy the desired baud rate */
  393. if (sbr == 0U)
  394. {
  395. sbr = 1U;
  396. }
  397. #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
  398. /* Determine if a fractional divider is needed to fine tune closer to the
  399. * desired baud, each value of brfa is in 1/32 increments,
  400. * hence the multiply-by-32. */
  401. uint32_t tempBaud = 0U;
  402. uint32_t brfa = (2U * srcClock_Hz / (baudRate_Bps)) - 32U * sbr;
  403. /* Calculate the baud rate based on the temporary SBR values and BRFA */
  404. tempBaud = (srcClock_Hz * 2U / ((sbr * 32U + brfa)));
  405. baudDiff = (tempBaud > baudRate_Bps) ? (tempBaud - baudRate_Bps) : (baudRate_Bps - tempBaud);
  406. #else
  407. /* Calculate the baud rate based on the temporary SBR values */
  408. baudDiff = (srcClock_Hz / (sbr * 16U)) - baudRate_Bps;
  409. /* Select the better value between sbr and (sbr + 1) */
  410. if (baudDiff > (baudRate_Bps - (srcClock_Hz / (16U * (sbr + 1U)))))
  411. {
  412. baudDiff = baudRate_Bps - (srcClock_Hz / (16U * (sbr + 1U)));
  413. sbr++;
  414. }
  415. #endif
  416. /* next, check to see if actual baud rate is within 3% of desired baud rate
  417. * based on the calculate SBR value */
  418. if (baudDiff < ((baudRate_Bps / 100U) * 3U))
  419. {
  420. /* Store C2 before disable Tx and Rx */
  421. oldCtrl = base->C2;
  422. /* Disable UART TX RX before setting. */
  423. base->C2 &= ~((uint8_t)UART_C2_TE_MASK | (uint8_t)UART_C2_RE_MASK);
  424. /* Write the sbr value to the BDH and BDL registers*/
  425. base->BDH = (base->BDH & ~(uint8_t)UART_BDH_SBR_MASK) | (uint8_t)(sbr >> 8);
  426. base->BDL = (uint8_t)sbr;
  427. #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
  428. /* Write the brfa value to the register*/
  429. base->C4 = (base->C4 & ~(uint8_t)UART_C4_BRFA_MASK) | ((uint8_t)brfa & (uint8_t)UART_C4_BRFA_MASK);
  430. #endif
  431. /* Restore C2. */
  432. base->C2 = oldCtrl;
  433. return kStatus_Success;
  434. }
  435. else
  436. {
  437. /* Unacceptable baud rate difference of more than 3%*/
  438. return kStatus_UART_BaudrateNotSupport;
  439. }
  440. }
  441. /*!
  442. * brief Enables UART interrupts according to the provided mask.
  443. *
  444. * This function enables the UART interrupts according to the provided mask. The mask
  445. * is a logical OR of enumeration members. See ref _uart_interrupt_enable.
  446. * For example, to enable TX empty interrupt and RX full interrupt, do the following.
  447. * code
  448. * UART_EnableInterrupts(UART1,kUART_TxDataRegEmptyInterruptEnable | kUART_RxDataRegFullInterruptEnable);
  449. * endcode
  450. *
  451. * param base UART peripheral base address.
  452. * param mask The interrupts to enable. Logical OR of ref _uart_interrupt_enable.
  453. */
  454. void UART_EnableInterrupts(UART_Type *base, uint32_t mask)
  455. {
  456. mask &= (uint32_t)kUART_AllInterruptsEnable;
  457. /* The interrupt mask is combined by control bits from several register: ((CFIFO<<24) | (C3<<16) | (C2<<8) |(BDH))
  458. */
  459. base->BDH |= (uint8_t)mask;
  460. base->C2 |= (uint8_t)(mask >> 8);
  461. base->C3 |= (uint8_t)(mask >> 16);
  462. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  463. base->CFIFO |= (uint8_t)(mask >> 24);
  464. #endif
  465. }
  466. /*!
  467. * brief Disables the UART interrupts according to the provided mask.
  468. *
  469. * This function disables the UART interrupts according to the provided mask. The mask
  470. * is a logical OR of enumeration members. See ref _uart_interrupt_enable.
  471. * For example, to disable TX empty interrupt and RX full interrupt do the following.
  472. * code
  473. * UART_DisableInterrupts(UART1,kUART_TxDataRegEmptyInterruptEnable | kUART_RxDataRegFullInterruptEnable);
  474. * endcode
  475. *
  476. * param base UART peripheral base address.
  477. * param mask The interrupts to disable. Logical OR of ref _uart_interrupt_enable.
  478. */
  479. void UART_DisableInterrupts(UART_Type *base, uint32_t mask)
  480. {
  481. mask &= (uint32_t)kUART_AllInterruptsEnable;
  482. /* The interrupt mask is combined by control bits from several register: ((CFIFO<<24) | (C3<<16) | (C2<<8) |(BDH))
  483. */
  484. base->BDH &= ~(uint8_t)mask;
  485. base->C2 &= ~(uint8_t)(mask >> 8);
  486. base->C3 &= ~(uint8_t)(mask >> 16);
  487. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  488. base->CFIFO &= ~(uint8_t)(mask >> 24);
  489. #endif
  490. }
  491. /*!
  492. * brief Gets the enabled UART interrupts.
  493. *
  494. * This function gets the enabled UART interrupts. The enabled interrupts are returned
  495. * as the logical OR value of the enumerators ref _uart_interrupt_enable. To check
  496. * a specific interrupts enable status, compare the return value with enumerators
  497. * in ref _uart_interrupt_enable.
  498. * For example, to check whether TX empty interrupt is enabled, do the following.
  499. * code
  500. * uint32_t enabledInterrupts = UART_GetEnabledInterrupts(UART1);
  501. *
  502. * if (kUART_TxDataRegEmptyInterruptEnable & enabledInterrupts)
  503. * {
  504. * ...
  505. * }
  506. * endcode
  507. *
  508. * param base UART peripheral base address.
  509. * return UART interrupt flags which are logical OR of the enumerators in ref _uart_interrupt_enable.
  510. */
  511. uint32_t UART_GetEnabledInterrupts(UART_Type *base)
  512. {
  513. uint32_t temp;
  514. temp = (uint32_t)base->BDH;
  515. temp |= ((uint32_t)(base->C2) << 8);
  516. temp |= ((uint32_t)(base->C3) << 16);
  517. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  518. temp |= ((uint32_t)(base->CFIFO) << 24);
  519. #endif
  520. return temp & (uint32_t)kUART_AllInterruptsEnable;
  521. }
  522. /*!
  523. * brief Gets UART status flags.
  524. *
  525. * This function gets all UART status flags. The flags are returned as the logical
  526. * OR value of the enumerators ref _uart_flags. To check a specific status,
  527. * compare the return value with enumerators in ref _uart_flags.
  528. * For example, to check whether the TX is empty, do the following.
  529. * code
  530. * if (kUART_TxDataRegEmptyFlag & UART_GetStatusFlags(UART1))
  531. * {
  532. * ...
  533. * }
  534. * endcode
  535. *
  536. * param base UART peripheral base address.
  537. * return UART status flags which are ORed by the enumerators in the _uart_flags.
  538. */
  539. uint32_t UART_GetStatusFlags(UART_Type *base)
  540. {
  541. uint32_t status_flag;
  542. status_flag = (uint32_t)base->S1;
  543. status_flag |= ((uint32_t)(base->S2) << 8);
  544. #if defined(FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS) && FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS
  545. status_flag |= ((uint32_t)(base->ED) << 16);
  546. #endif
  547. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  548. status_flag |= ((uint32_t)(base->SFIFO) << 24);
  549. #endif
  550. return status_flag;
  551. }
  552. /*!
  553. * brief Clears status flags with the provided mask.
  554. *
  555. * This function clears UART status flags with a provided mask. An automatically cleared flag
  556. * can't be cleared by this function.
  557. * These flags can only be cleared or set by hardware.
  558. * kUART_TxDataRegEmptyFlag, kUART_TransmissionCompleteFlag, kUART_RxDataRegFullFlag,
  559. * kUART_RxActiveFlag, kUART_NoiseErrorInRxDataRegFlag, kUART_ParityErrorInRxDataRegFlag,
  560. * kUART_TxFifoEmptyFlag,kUART_RxFifoEmptyFlag
  561. * Note that this API should be called when the Tx/Rx is idle. Otherwise it has no effect.
  562. *
  563. * param base UART peripheral base address.
  564. * param mask The status flags to be cleared; it is logical OR value of ref _uart_flags.
  565. * retval kStatus_UART_FlagCannotClearManually The flag can't be cleared by this function but
  566. * it is cleared automatically by hardware.
  567. * retval kStatus_Success Status in the mask is cleared.
  568. */
  569. status_t UART_ClearStatusFlags(UART_Type *base, uint32_t mask)
  570. {
  571. uint8_t reg = base->S2;
  572. status_t status;
  573. #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT
  574. reg &= ~((uint8_t)UART_S2_RXEDGIF_MASK | (uint8_t)UART_S2_LBKDIF_MASK);
  575. #else
  576. reg &= ~(uint8_t)UART_S2_RXEDGIF_MASK;
  577. #endif
  578. base->S2 = reg | (uint8_t)(mask >> 8);
  579. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  580. base->SFIFO = (uint8_t)(mask >> 24);
  581. #endif
  582. if ((mask & ((uint32_t)kUART_IdleLineFlag | (uint32_t)kUART_NoiseErrorFlag | (uint32_t)kUART_FramingErrorFlag |
  583. (uint32_t)kUART_ParityErrorFlag | (uint32_t)kUART_RxOverrunFlag)) != 0u)
  584. {
  585. /* Read base->S1 and base->D to clear the flags. */
  586. (void)base->S1;
  587. (void)base->D;
  588. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  589. /* Read base->D may cause receiver underflow when there are no valid data.
  590. Clear receiver underflow flag */
  591. base->SFIFO = UART_SFIFO_RXUF_MASK;
  592. /* Flush FIFO data. Otherwise FIFO pointer will be in unknown state. */
  593. base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
  594. #endif
  595. }
  596. /* If some flags still pending. */
  597. if ((mask & UART_GetStatusFlags(base)) != 0U)
  598. {
  599. /* Some flags can only clear or set by the hardware itself, these flags are: kUART_TxDataRegEmptyFlag,
  600. kUART_TransmissionCompleteFlag, kUART_RxDataRegFullFlag, kUART_RxActiveFlag, kUART_NoiseErrorInRxDataRegFlag,
  601. kUART_ParityErrorInRxDataRegFlag, kUART_TxFifoEmptyFlag, kUART_RxFifoEmptyFlag. */
  602. status = kStatus_UART_FlagCannotClearManually;
  603. }
  604. else
  605. {
  606. status = kStatus_Success;
  607. }
  608. return status;
  609. }
  610. /*!
  611. * brief Writes to the TX register using a blocking method.
  612. *
  613. * This function polls the TX register, waits for the TX register to be empty or for the TX FIFO
  614. * to have room and writes data to the TX buffer.
  615. *
  616. * param base UART peripheral base address.
  617. * param data Start address of the data to write.
  618. * param length Size of the data to write.
  619. * retval kStatus_UART_Timeout Transmission timed out and was aborted.
  620. * retval kStatus_Success Successfully wrote all data.
  621. */
  622. status_t UART_WriteBlocking(UART_Type *base, const uint8_t *data, size_t length)
  623. {
  624. #if UART_RETRY_TIMES
  625. uint32_t waitTimes;
  626. #endif
  627. while (0U != length--)
  628. {
  629. #if UART_RETRY_TIMES
  630. waitTimes = UART_RETRY_TIMES;
  631. while ((0U == (base->S1 & UART_S1_TDRE_MASK)) && (0U != --waitTimes))
  632. #else
  633. while (0U == (base->S1 & UART_S1_TDRE_MASK))
  634. #endif
  635. {
  636. }
  637. #if UART_RETRY_TIMES
  638. if (waitTimes == 0U)
  639. {
  640. return kStatus_LPUART_Timeout;
  641. }
  642. #endif
  643. base->D = *(data++);
  644. }
  645. /* Ensure all the data in the transmit buffer are sent out to bus. */
  646. #if UART_RETRY_TIMES
  647. waitTimes = UART_RETRY_TIMES;
  648. while ((0U == (base->S1 & UART_S1_TC_MASK)) && (0U != --waitTimes))
  649. #else
  650. while (0U == (base->S1 & UART_S1_TC_MASK))
  651. #endif
  652. {
  653. }
  654. #if UART_RETRY_TIMES
  655. if (waitTimes == 0U)
  656. {
  657. return kStatus_LPUART_Timeout;
  658. }
  659. #endif
  660. return kStatus_Success;
  661. }
  662. static void UART_WriteNonBlocking(UART_Type *base, const uint8_t *data, size_t length)
  663. {
  664. assert(data);
  665. size_t i;
  666. /* The Non Blocking write data API assume user have ensured there is enough space in
  667. peripheral to write. */
  668. for (i = 0; i < length; i++)
  669. {
  670. base->D = data[i];
  671. }
  672. }
  673. /*!
  674. * brief Read RX data register using a blocking method.
  675. *
  676. * This function polls the RX register, waits for the RX register to be full or for RX FIFO to
  677. * have data, and reads data from the TX register.
  678. *
  679. * param base UART peripheral base address.
  680. * param data Start address of the buffer to store the received data.
  681. * param length Size of the buffer.
  682. * retval kStatus_UART_RxHardwareOverrun Receiver overrun occurred while receiving data.
  683. * retval kStatus_UART_NoiseError A noise error occurred while receiving data.
  684. * retval kStatus_UART_FramingError A framing error occurred while receiving data.
  685. * retval kStatus_UART_ParityError A parity error occurred while receiving data.
  686. * retval kStatus_UART_Timeout Transmission timed out and was aborted.
  687. * retval kStatus_Success Successfully received all data.
  688. */
  689. status_t UART_ReadBlocking(UART_Type *base, uint8_t *data, size_t length)
  690. {
  691. assert(data != NULL);
  692. status_t status = kStatus_Success;
  693. uint32_t statusFlag;
  694. #if UART_RETRY_TIMES
  695. uint32_t waitTimes;
  696. #endif
  697. while (length-- != 0U)
  698. {
  699. #if UART_RETRY_TIMES
  700. waitTimes = UART_RETRY_TIMES;
  701. #endif
  702. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  703. while (base->RCFIFO == 0U)
  704. #else
  705. while ((base->S1 & UART_S1_RDRF_MASK) == 0U)
  706. #endif
  707. {
  708. #if UART_RETRY_TIMES
  709. if (0U == --waitTimes)
  710. {
  711. status = kStatus_LPUART_Timeout;
  712. break;
  713. }
  714. #endif
  715. statusFlag = UART_GetStatusFlags(base);
  716. if (0U != (statusFlag & (uint32_t)kUART_RxOverrunFlag))
  717. {
  718. status = ((kStatus_Success == UART_ClearStatusFlags(base, (uint32_t)kUART_RxOverrunFlag)) ?
  719. (status_t)(kStatus_UART_RxHardwareOverrun) :
  720. (status_t)(kStatus_UART_FlagCannotClearManually));
  721. /* If the OR bit is set all the other error flags are prevented from setting,
  722. no need to check other status flags. */
  723. break;
  724. }
  725. if (0U != (statusFlag & (uint32_t)kUART_ParityErrorFlag))
  726. {
  727. status = ((kStatus_Success == UART_ClearStatusFlags(base, (uint32_t)kUART_ParityErrorFlag)) ?
  728. (status_t)(kStatus_UART_ParityError) :
  729. (status_t)(kStatus_UART_FlagCannotClearManually));
  730. }
  731. if (0U != (statusFlag & (uint32_t)kUART_FramingErrorFlag))
  732. {
  733. status = ((kStatus_Success == UART_ClearStatusFlags(base, (uint32_t)kUART_FramingErrorFlag)) ?
  734. (status_t)(kStatus_UART_FramingError) :
  735. (status_t)(kStatus_UART_FlagCannotClearManually));
  736. }
  737. if (0U != (statusFlag & (uint32_t)kUART_NoiseErrorFlag))
  738. {
  739. status = ((kStatus_Success == UART_ClearStatusFlags(base, (uint32_t)kUART_NoiseErrorFlag)) ?
  740. (status_t)(kStatus_UART_NoiseError) :
  741. (status_t)(kStatus_UART_FlagCannotClearManually));
  742. }
  743. if (kStatus_Success != status)
  744. {
  745. break;
  746. }
  747. }
  748. if (kStatus_Success == status)
  749. {
  750. *(data++) = base->D;
  751. }
  752. else
  753. {
  754. break;
  755. }
  756. }
  757. return status;
  758. }
  759. static void UART_ReadNonBlocking(UART_Type *base, uint8_t *data, size_t length)
  760. {
  761. assert(data);
  762. size_t i;
  763. /* The Non Blocking read data API assume user have ensured there is enough space in
  764. peripheral to write. */
  765. for (i = 0; i < length; i++)
  766. {
  767. data[i] = base->D;
  768. }
  769. }
  770. /*!
  771. * brief Initializes the UART handle.
  772. *
  773. * This function initializes the UART handle which can be used for other UART
  774. * transactional APIs. Usually, for a specified UART instance,
  775. * call this API once to get the initialized handle.
  776. *
  777. * param base UART peripheral base address.
  778. * param handle UART handle pointer.
  779. * param callback The callback function.
  780. * param userData The parameter of the callback function.
  781. */
  782. void UART_TransferCreateHandle(UART_Type *base,
  783. uart_handle_t *handle,
  784. uart_transfer_callback_t callback,
  785. void *userData)
  786. {
  787. assert(handle);
  788. uint32_t instance;
  789. /* Zero the handle. */
  790. (void)memset(handle, 0, sizeof(*handle));
  791. /* Set the TX/RX state. */
  792. handle->rxState = (uint8_t)kUART_RxIdle;
  793. handle->txState = (uint8_t)kUART_TxIdle;
  794. /* Set the callback and user data. */
  795. handle->callback = callback;
  796. handle->userData = userData;
  797. /* Get instance from peripheral base address. */
  798. instance = UART_GetInstance(base);
  799. /* Save the handle in global variables to support the double weak mechanism. */
  800. s_uartHandle[instance] = handle;
  801. s_uartIsr = UART_TransferHandleIRQ;
  802. /* Enable interrupt in NVIC. */
  803. (void)EnableIRQ(s_uartIRQ[instance]);
  804. }
  805. /*!
  806. * brief Sets up the RX ring buffer.
  807. *
  808. * This function sets up the RX ring buffer to a specific UART handle.
  809. *
  810. * When the RX ring buffer is used, data received are stored into the ring buffer even when the
  811. * user doesn't call the UART_TransferReceiveNonBlocking() API. If data is already received
  812. * in the ring buffer, the user can get the received data from the ring buffer directly.
  813. *
  814. * note When using the RX ring buffer, one byte is reserved for internal use. In other
  815. * words, if p ringBufferSize is 32, only 31 bytes are used for saving data.
  816. *
  817. * param base UART peripheral base address.
  818. * param handle UART handle pointer.
  819. * param ringBuffer Start address of the ring buffer for background receiving. Pass NULL to disable the ring buffer.
  820. * param ringBufferSize Size of the ring buffer.
  821. */
  822. void UART_TransferStartRingBuffer(UART_Type *base, uart_handle_t *handle, uint8_t *ringBuffer, size_t ringBufferSize)
  823. {
  824. assert(handle);
  825. assert(ringBuffer);
  826. /* Setup the ringbuffer address */
  827. handle->rxRingBuffer = ringBuffer;
  828. handle->rxRingBufferSize = ringBufferSize;
  829. handle->rxRingBufferHead = 0U;
  830. handle->rxRingBufferTail = 0U;
  831. /* Enable the interrupt to accept the data when user need the ring buffer. */
  832. UART_EnableInterrupts(base, (uint32_t)kUART_RxDataRegFullInterruptEnable |
  833. (uint32_t)kUART_RxOverrunInterruptEnable |
  834. (uint32_t)kUART_FramingErrorInterruptEnable);
  835. /* Enable parity error interrupt when parity mode is enable*/
  836. if ((UART_C1_PE_MASK & base->C1) != 0U)
  837. {
  838. UART_EnableInterrupts(base, (uint32_t)kUART_ParityErrorInterruptEnable);
  839. }
  840. }
  841. /*!
  842. * brief Aborts the background transfer and uninstalls the ring buffer.
  843. *
  844. * This function aborts the background transfer and uninstalls the ring buffer.
  845. *
  846. * param base UART peripheral base address.
  847. * param handle UART handle pointer.
  848. */
  849. void UART_TransferStopRingBuffer(UART_Type *base, uart_handle_t *handle)
  850. {
  851. assert(handle);
  852. if (handle->rxState == (uint8_t)kUART_RxIdle)
  853. {
  854. UART_DisableInterrupts(base, (uint32_t)kUART_RxDataRegFullInterruptEnable |
  855. (uint32_t)kUART_RxOverrunInterruptEnable |
  856. (uint32_t)kUART_FramingErrorInterruptEnable);
  857. /* Disable parity error interrupt when parity mode is enable*/
  858. if ((UART_C1_PE_MASK & base->C1) != 0U)
  859. {
  860. UART_DisableInterrupts(base, (uint32_t)kUART_ParityErrorInterruptEnable);
  861. }
  862. }
  863. handle->rxRingBuffer = NULL;
  864. handle->rxRingBufferSize = 0U;
  865. handle->rxRingBufferHead = 0U;
  866. handle->rxRingBufferTail = 0U;
  867. }
  868. /*!
  869. * brief Transmits a buffer of data using the interrupt method.
  870. *
  871. * This function sends data using an interrupt method. This is a non-blocking function, which
  872. * returns directly without waiting for all data to be written to the TX register. When
  873. * all data is written to the TX register in the ISR, the UART driver calls the callback
  874. * function and passes the ref kStatus_UART_TxIdle as status parameter.
  875. *
  876. * note The kStatus_UART_TxIdle is passed to the upper layer when all data is written
  877. * to the TX register. However, it does not ensure that all data is sent out. Before disabling the TX,
  878. * check the kUART_TransmissionCompleteFlag to ensure that the TX is finished.
  879. *
  880. * param base UART peripheral base address.
  881. * param handle UART handle pointer.
  882. * param xfer UART transfer structure. See #uart_transfer_t.
  883. * retval kStatus_Success Successfully start the data transmission.
  884. * retval kStatus_UART_TxBusy Previous transmission still not finished; data not all written to TX register yet.
  885. * retval kStatus_InvalidArgument Invalid argument.
  886. */
  887. status_t UART_TransferSendNonBlocking(UART_Type *base, uart_handle_t *handle, uart_transfer_t *xfer)
  888. {
  889. assert(handle);
  890. assert(xfer);
  891. assert(xfer->dataSize);
  892. assert(xfer->data);
  893. status_t status;
  894. /* Return error if current TX busy. */
  895. if ((uint8_t)kUART_TxBusy == handle->txState)
  896. {
  897. status = kStatus_UART_TxBusy;
  898. }
  899. else
  900. {
  901. handle->txData = xfer->data;
  902. handle->txDataSize = xfer->dataSize;
  903. handle->txDataSizeAll = xfer->dataSize;
  904. handle->txState = (uint8_t)kUART_TxBusy;
  905. /* Enable transmitter interrupt. */
  906. UART_EnableInterrupts(base, (uint32_t)kUART_TxDataRegEmptyInterruptEnable);
  907. status = kStatus_Success;
  908. }
  909. return status;
  910. }
  911. /*!
  912. * brief Aborts the interrupt-driven data transmit.
  913. *
  914. * This function aborts the interrupt-driven data sending. The user can get the remainBytes to find out
  915. * how many bytes are not sent out.
  916. *
  917. * param base UART peripheral base address.
  918. * param handle UART handle pointer.
  919. */
  920. void UART_TransferAbortSend(UART_Type *base, uart_handle_t *handle)
  921. {
  922. assert(handle);
  923. UART_DisableInterrupts(
  924. base, (uint32_t)kUART_TxDataRegEmptyInterruptEnable | (uint32_t)kUART_TransmissionCompleteInterruptEnable);
  925. handle->txDataSize = 0;
  926. handle->txState = (uint8_t)kUART_TxIdle;
  927. }
  928. /*!
  929. * brief Gets the number of bytes sent out to bus.
  930. *
  931. * This function gets the number of bytes sent out to bus by using the interrupt method.
  932. *
  933. * param base UART peripheral base address.
  934. * param handle UART handle pointer.
  935. * param count Send bytes count.
  936. * retval kStatus_NoTransferInProgress No send in progress.
  937. * retval kStatus_InvalidArgument The parameter is invalid.
  938. * retval kStatus_Success Get successfully through the parameter \p count;
  939. */
  940. status_t UART_TransferGetSendCount(UART_Type *base, uart_handle_t *handle, uint32_t *count)
  941. {
  942. assert(handle);
  943. assert(count);
  944. if ((uint8_t)kUART_TxIdle == handle->txState)
  945. {
  946. return kStatus_NoTransferInProgress;
  947. }
  948. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  949. *count = handle->txDataSizeAll - handle->txDataSize - base->TCFIFO;
  950. #else
  951. if ((base->S1 & (uint8_t)kUART_TxDataRegEmptyFlag) != 0U)
  952. {
  953. *count = handle->txDataSizeAll - handle->txDataSize;
  954. }
  955. else
  956. {
  957. *count = handle->txDataSizeAll - handle->txDataSize - 1U;
  958. }
  959. #endif /* FSL_FEATURE_UART_HAS_FIFO */
  960. return kStatus_Success;
  961. }
  962. /*!
  963. * brief Receives a buffer of data using an interrupt method.
  964. *
  965. * This function receives data using an interrupt method. This is a non-blocking function, which
  966. * returns without waiting for all data to be received.
  967. * If the RX ring buffer is used and not empty, the data in the ring buffer is copied and
  968. * the parameter p receivedBytes shows how many bytes are copied from the ring buffer.
  969. * After copying, if the data in the ring buffer is not enough to read, the receive
  970. * request is saved by the UART driver. When the new data arrives, the receive request
  971. * is serviced first. When all data is received, the UART driver notifies the upper layer
  972. * through a callback function and passes the status parameter ref kStatus_UART_RxIdle.
  973. * For example, the upper layer needs 10 bytes but there are only 5 bytes in the ring buffer.
  974. * The 5 bytes are copied to the xfer->data and this function returns with the
  975. * parameter p receivedBytes set to 5. For the left 5 bytes, newly arrived data is
  976. * saved from the xfer->data[5]. When 5 bytes are received, the UART driver notifies the upper layer.
  977. * If the RX ring buffer is not enabled, this function enables the RX and RX interrupt
  978. * to receive data to the xfer->data. When all data is received, the upper layer is notified.
  979. *
  980. * param base UART peripheral base address.
  981. * param handle UART handle pointer.
  982. * param xfer UART transfer structure, see #uart_transfer_t.
  983. * param receivedBytes Bytes received from the ring buffer directly.
  984. * retval kStatus_Success Successfully queue the transfer into transmit queue.
  985. * retval kStatus_UART_RxBusy Previous receive request is not finished.
  986. * retval kStatus_InvalidArgument Invalid argument.
  987. */
  988. status_t UART_TransferReceiveNonBlocking(UART_Type *base,
  989. uart_handle_t *handle,
  990. uart_transfer_t *xfer,
  991. size_t *receivedBytes)
  992. {
  993. assert(handle);
  994. assert(xfer);
  995. assert(xfer->data);
  996. assert(xfer->dataSize);
  997. uint32_t i;
  998. status_t status;
  999. /* How many bytes to copy from ring buffer to user memory. */
  1000. size_t bytesToCopy = 0U;
  1001. /* How many bytes to receive. */
  1002. size_t bytesToReceive;
  1003. /* How many bytes currently have received. */
  1004. size_t bytesCurrentReceived;
  1005. /* How to get data:
  1006. 1. If RX ring buffer is not enabled, then save xfer->data and xfer->dataSize
  1007. to uart handle, enable interrupt to store received data to xfer->data. When
  1008. all data received, trigger callback.
  1009. 2. If RX ring buffer is enabled and not empty, get data from ring buffer first.
  1010. If there are enough data in ring buffer, copy them to xfer->data and return.
  1011. If there are not enough data in ring buffer, copy all of them to xfer->data,
  1012. save the xfer->data remained empty space to uart handle, receive data
  1013. to this empty space and trigger callback when finished. */
  1014. if ((uint8_t)kUART_RxBusy == handle->rxState)
  1015. {
  1016. status = kStatus_UART_RxBusy;
  1017. }
  1018. else
  1019. {
  1020. bytesToReceive = xfer->dataSize;
  1021. bytesCurrentReceived = 0U;
  1022. /* If RX ring buffer is used. */
  1023. if (handle->rxRingBuffer != NULL)
  1024. {
  1025. /* Disable UART RX IRQ, protect ring buffer. */
  1026. UART_DisableInterrupts(base, (uint32_t)kUART_RxDataRegFullInterruptEnable);
  1027. /* How many bytes in RX ring buffer currently. */
  1028. bytesToCopy = UART_TransferGetRxRingBufferLength(handle);
  1029. if (bytesToCopy != 0U)
  1030. {
  1031. bytesToCopy = MIN(bytesToReceive, bytesToCopy);
  1032. bytesToReceive -= bytesToCopy;
  1033. /* Copy data from ring buffer to user memory. */
  1034. for (i = 0U; i < bytesToCopy; i++)
  1035. {
  1036. xfer->data[bytesCurrentReceived++] = handle->rxRingBuffer[handle->rxRingBufferTail];
  1037. /* Wrap to 0. Not use modulo (%) because it might be large and slow. */
  1038. if ((size_t)handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
  1039. {
  1040. handle->rxRingBufferTail = 0U;
  1041. }
  1042. else
  1043. {
  1044. handle->rxRingBufferTail++;
  1045. }
  1046. }
  1047. }
  1048. /* If ring buffer does not have enough data, still need to read more data. */
  1049. if (bytesToReceive != 0U)
  1050. {
  1051. /* No data in ring buffer, save the request to UART handle. */
  1052. handle->rxData = xfer->data + bytesCurrentReceived;
  1053. handle->rxDataSize = bytesToReceive;
  1054. handle->rxDataSizeAll = bytesToReceive;
  1055. handle->rxState = (uint8_t)kUART_RxBusy;
  1056. }
  1057. /* Enable UART RX IRQ if previously enabled. */
  1058. UART_EnableInterrupts(base, (uint32_t)kUART_RxDataRegFullInterruptEnable);
  1059. /* Call user callback since all data are received. */
  1060. if (0U == bytesToReceive)
  1061. {
  1062. if (handle->callback != NULL)
  1063. {
  1064. handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData);
  1065. }
  1066. }
  1067. }
  1068. /* Ring buffer not used. */
  1069. else
  1070. {
  1071. handle->rxData = xfer->data + bytesCurrentReceived;
  1072. handle->rxDataSize = bytesToReceive;
  1073. handle->rxDataSizeAll = bytesToReceive;
  1074. handle->rxState = (uint8_t)kUART_RxBusy;
  1075. /* Enable RX/Rx overrun/framing error/idle line interrupt. */
  1076. UART_EnableInterrupts(
  1077. base, (uint32_t)kUART_RxDataRegFullInterruptEnable | (uint32_t)kUART_RxOverrunInterruptEnable |
  1078. (uint32_t)kUART_FramingErrorInterruptEnable | (uint32_t)kUART_IdleLineInterruptEnable);
  1079. /* Enable parity error interrupt when parity mode is enable*/
  1080. if ((UART_C1_PE_MASK & base->C1) != 0U)
  1081. {
  1082. UART_EnableInterrupts(base, (uint32_t)kUART_ParityErrorInterruptEnable);
  1083. }
  1084. }
  1085. /* Return the how many bytes have read. */
  1086. if (receivedBytes != NULL)
  1087. {
  1088. *receivedBytes = bytesCurrentReceived;
  1089. }
  1090. status = kStatus_Success;
  1091. }
  1092. return status;
  1093. }
  1094. /*!
  1095. * brief Aborts the interrupt-driven data receiving.
  1096. *
  1097. * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to know
  1098. * how many bytes are not received yet.
  1099. *
  1100. * param base UART peripheral base address.
  1101. * param handle UART handle pointer.
  1102. */
  1103. void UART_TransferAbortReceive(UART_Type *base, uart_handle_t *handle)
  1104. {
  1105. assert(handle);
  1106. /* Only abort the receive to handle->rxData, the RX ring buffer is still working. */
  1107. if (NULL == handle->rxRingBuffer)
  1108. {
  1109. /* Disable RX interrupt. */
  1110. UART_DisableInterrupts(
  1111. base, (uint32_t)kUART_RxDataRegFullInterruptEnable | (uint32_t)kUART_RxOverrunInterruptEnable |
  1112. (uint32_t)kUART_FramingErrorInterruptEnable | (uint32_t)kUART_IdleLineInterruptEnable);
  1113. /* Disable parity error interrupt when parity mode is enable*/
  1114. if ((UART_C1_PE_MASK & base->C1) != 0U)
  1115. {
  1116. UART_DisableInterrupts(base, (uint32_t)kUART_ParityErrorInterruptEnable);
  1117. }
  1118. }
  1119. handle->rxDataSize = 0U;
  1120. handle->rxState = (uint8_t)kUART_RxIdle;
  1121. }
  1122. /*!
  1123. * brief Gets the number of bytes that have been received.
  1124. *
  1125. * This function gets the number of bytes that have been received.
  1126. *
  1127. * param base UART peripheral base address.
  1128. * param handle UART handle pointer.
  1129. * param count Receive bytes count.
  1130. * retval kStatus_NoTransferInProgress No receive in progress.
  1131. * retval kStatus_InvalidArgument Parameter is invalid.
  1132. * retval kStatus_Success Get successfully through the parameter \p count;
  1133. */
  1134. status_t UART_TransferGetReceiveCount(UART_Type *base, uart_handle_t *handle, uint32_t *count)
  1135. {
  1136. assert(handle);
  1137. assert(count);
  1138. if ((uint8_t)kUART_RxIdle == handle->rxState)
  1139. {
  1140. return kStatus_NoTransferInProgress;
  1141. }
  1142. if (NULL == count)
  1143. {
  1144. return kStatus_InvalidArgument;
  1145. }
  1146. *count = handle->rxDataSizeAll - handle->rxDataSize;
  1147. return kStatus_Success;
  1148. }
  1149. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  1150. /*!
  1151. * @brief Enables or disables the UART Tx FIFO.
  1152. *
  1153. * This function enables or disables the UART Tx FIFO.
  1154. *
  1155. * param base UART peripheral base address.
  1156. * param enable true to enable, false to disable.
  1157. * retval kStatus_Success Successfully turn on or turn off Tx FIFO.
  1158. * retval kStatus_Fail Fail to turn on or turn off Tx FIFO.
  1159. */
  1160. status_t UART_EnableTxFIFO(UART_Type *base, bool enable)
  1161. {
  1162. uint8_t sfifo = 0;
  1163. uint8_t temp = 0;
  1164. sfifo = base->SFIFO;
  1165. temp = base->C2 & (UART_C2_RE_MASK | UART_C2_TE_MASK);
  1166. /* The Tx FIFO must be empty */
  1167. if ((sfifo & UART_SFIFO_TXEMPT_MASK) == UART_SFIFO_TXEMPT_MASK)
  1168. {
  1169. /* Disable UART TX RX before setting */
  1170. base->C2 &= ~((uint8_t)UART_C2_TE_MASK | (uint8_t)UART_C2_RE_MASK);
  1171. /* Flush FIFO */
  1172. base->CFIFO |= (UART_CFIFO_TXFLUSH_MASK | UART_CFIFO_RXFLUSH_MASK);
  1173. if (enable)
  1174. {
  1175. base->PFIFO |= (uint8_t)UART_PFIFO_TXFE_MASK;
  1176. }
  1177. else
  1178. {
  1179. base->PFIFO &= ~(uint8_t)UART_PFIFO_TXFE_MASK;
  1180. }
  1181. /* Flush FIFO */
  1182. base->CFIFO |= (UART_CFIFO_TXFLUSH_MASK | UART_CFIFO_RXFLUSH_MASK);
  1183. base->C2 |= temp;
  1184. return kStatus_Success;
  1185. }
  1186. else
  1187. {
  1188. return kStatus_Fail;
  1189. }
  1190. }
  1191. /*!
  1192. * @brief Enables or disables the UART Rx FIFO.
  1193. *
  1194. * This function enables or disables the UART Rx FIFO.
  1195. *
  1196. * param base UART peripheral base address.
  1197. * param enable true to enable, false to disable.
  1198. * retval kStatus_Success Successfully turn on or turn off Rx FIFO.
  1199. * retval kStatus_Fail Fail to turn on or turn off Rx FIFO.
  1200. */
  1201. status_t UART_EnableRxFIFO(UART_Type *base, bool enable)
  1202. {
  1203. uint8_t sfifo = 0;
  1204. uint8_t temp = 0;
  1205. sfifo = base->SFIFO;
  1206. temp = base->C2 & ((uint8_t)UART_C2_RE_MASK | (uint8_t)UART_C2_TE_MASK);
  1207. /* The Rx FIFO must be empty */
  1208. if ((sfifo & UART_SFIFO_RXEMPT_MASK) == UART_SFIFO_RXEMPT_MASK)
  1209. {
  1210. /* Disable UART TX RX before setting */
  1211. base->C2 &= ~((uint8_t)UART_C2_TE_MASK | (uint8_t)UART_C2_RE_MASK);
  1212. /* Flush FIFO */
  1213. base->CFIFO |= (UART_CFIFO_TXFLUSH_MASK | UART_CFIFO_RXFLUSH_MASK);
  1214. if (enable)
  1215. {
  1216. base->PFIFO |= (uint8_t)UART_PFIFO_RXFE_MASK;
  1217. }
  1218. else
  1219. {
  1220. base->PFIFO &= ~(uint8_t)UART_PFIFO_RXFE_MASK;
  1221. }
  1222. /* Flush FIFO */
  1223. base->CFIFO |= (UART_CFIFO_TXFLUSH_MASK | UART_CFIFO_RXFLUSH_MASK);
  1224. base->C2 |= temp;
  1225. return kStatus_Success;
  1226. }
  1227. else
  1228. {
  1229. return kStatus_Fail;
  1230. }
  1231. }
  1232. #endif /* FSL_FEATURE_UART_HAS_FIFO */
  1233. /*!
  1234. * brief UART IRQ handle function.
  1235. *
  1236. * This function handles the UART transmit and receive IRQ request.
  1237. *
  1238. * param base UART peripheral base address.
  1239. * param handle UART handle pointer.
  1240. */
  1241. void UART_TransferHandleIRQ(UART_Type *base, uart_handle_t *handle)
  1242. {
  1243. assert(handle);
  1244. uint8_t count;
  1245. uint8_t tempCount;
  1246. uint32_t status = UART_GetStatusFlags(base);
  1247. uint8_t tmpdata;
  1248. /* If RX framing error */
  1249. if (((uint32_t)kUART_FramingErrorFlag & status) != 0U)
  1250. {
  1251. /* Read base->D to clear framing error flag, otherwise the RX does not work. */
  1252. while ((base->S1 & UART_S1_RDRF_MASK) != 0U)
  1253. {
  1254. (void)base->D;
  1255. }
  1256. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  1257. /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */
  1258. base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
  1259. #endif
  1260. handle->rxState = (uint8_t)kUART_RxFramingError;
  1261. handle->rxDataSize = 0U;
  1262. /* Trigger callback. */
  1263. if (handle->callback != NULL)
  1264. {
  1265. handle->callback(base, handle, kStatus_UART_FramingError, handle->userData);
  1266. }
  1267. }
  1268. /* If RX parity error */
  1269. if (((uint32_t)kUART_ParityErrorFlag & status) != 0U)
  1270. {
  1271. /* Read base->D to clear parity error flag, otherwise the RX does not work. */
  1272. while ((base->S1 & UART_S1_RDRF_MASK) != 0U)
  1273. {
  1274. (void)base->D;
  1275. }
  1276. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  1277. /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */
  1278. base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
  1279. #endif
  1280. handle->rxState = (uint8_t)kUART_RxParityError;
  1281. handle->rxDataSize = 0U;
  1282. /* Trigger callback. */
  1283. if (handle->callback != NULL)
  1284. {
  1285. handle->callback(base, handle, kStatus_UART_ParityError, handle->userData);
  1286. }
  1287. }
  1288. /* If RX overrun. */
  1289. if (((uint32_t)kUART_RxOverrunFlag & status) != 0U)
  1290. {
  1291. /* Read base->D to clear overrun flag, otherwise the RX does not work. */
  1292. while ((base->S1 & UART_S1_RDRF_MASK) != 0U)
  1293. {
  1294. (void)base->D;
  1295. }
  1296. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  1297. /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */
  1298. base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
  1299. #endif
  1300. /* Trigger callback. */
  1301. if (handle->callback != NULL)
  1302. {
  1303. handle->callback(base, handle, kStatus_UART_RxHardwareOverrun, handle->userData);
  1304. }
  1305. }
  1306. /* If IDLE line was detected. */
  1307. if ((((uint32_t)kUART_IdleLineFlag & status) != 0U) && ((UART_C2_ILIE_MASK & base->C2) != 0U))
  1308. {
  1309. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  1310. /* If still some data in the FIFO, read out these data to user data buffer. */
  1311. count = base->RCFIFO;
  1312. /* If handle->rxDataSize is not 0, first save data to handle->rxData. */
  1313. while ((count != 0U) && (handle->rxDataSize != 0U))
  1314. {
  1315. tempCount = (uint8_t)MIN(handle->rxDataSize, (uint32_t)count);
  1316. /* Using non block API to read the data from the registers. */
  1317. UART_ReadNonBlocking(base, handle->rxData, tempCount);
  1318. handle->rxData += tempCount;
  1319. handle->rxDataSize -= tempCount;
  1320. count -= tempCount;
  1321. /* If all the data required for upper layer is ready, trigger callback. */
  1322. if (0U == handle->rxDataSize)
  1323. {
  1324. handle->rxState = (uint8_t)kUART_RxIdle;
  1325. /* Disable RX interrupt/overrun interrupt/fram error/idle line detected interrupt */
  1326. UART_DisableInterrupts(base, (uint32_t)kUART_RxDataRegFullInterruptEnable |
  1327. (uint32_t)kUART_RxOverrunInterruptEnable |
  1328. (uint32_t)kUART_FramingErrorInterruptEnable);
  1329. /* Disable parity error interrupt when parity mode is enable*/
  1330. if ((UART_C1_PE_MASK & base->C1) != 0U)
  1331. {
  1332. UART_DisableInterrupts(base, (uint32_t)kUART_ParityErrorInterruptEnable);
  1333. }
  1334. if (handle->callback != NULL)
  1335. {
  1336. handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData);
  1337. }
  1338. }
  1339. }
  1340. #endif
  1341. /* To clear IDLE, read UART status S1 with IDLE set and then read D.*/
  1342. while ((UART_S1_IDLE_MASK & base->S1) != 0U)
  1343. {
  1344. (void)base->D;
  1345. }
  1346. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  1347. /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */
  1348. base->CFIFO |= UART_CFIFO_RXFLUSH_MASK;
  1349. #endif
  1350. /* If rxDataSize is 0, disable idle line interrupt.*/
  1351. if (0U == (handle->rxDataSize))
  1352. {
  1353. UART_DisableInterrupts(base, (uint32_t)kUART_IdleLineInterruptEnable);
  1354. }
  1355. /* If callback is not NULL and rxDataSize is not 0. */
  1356. if ((handle->callback != NULL) && (handle->rxDataSize != 0U))
  1357. {
  1358. handle->callback(base, handle, kStatus_UART_IdleLineDetected, handle->userData);
  1359. }
  1360. }
  1361. /* Receive data register full */
  1362. if ((((uint32_t)kUART_RxDataRegFullFlag & status) != 0U) && ((UART_C2_RIE_MASK & base->C2) != 0U))
  1363. {
  1364. /* Get the size that can be stored into buffer for this interrupt. */
  1365. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  1366. count = base->RCFIFO;
  1367. #else
  1368. count = 1;
  1369. #endif
  1370. /* If handle->rxDataSize is not 0, first save data to handle->rxData. */
  1371. while ((count != 0U) && (handle->rxDataSize != 0U))
  1372. {
  1373. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  1374. tempCount = (uint8_t)MIN(handle->rxDataSize, (uint32_t)count);
  1375. #else
  1376. tempCount = 1;
  1377. #endif
  1378. /* Using non block API to read the data from the registers. */
  1379. UART_ReadNonBlocking(base, handle->rxData, tempCount);
  1380. handle->rxData += tempCount;
  1381. handle->rxDataSize -= tempCount;
  1382. count -= tempCount;
  1383. /* If all the data required for upper layer is ready, trigger callback. */
  1384. if (0U == handle->rxDataSize)
  1385. {
  1386. handle->rxState = (uint8_t)kUART_RxIdle;
  1387. if (handle->callback != NULL)
  1388. {
  1389. handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData);
  1390. }
  1391. }
  1392. }
  1393. /* If use RX ring buffer, receive data to ring buffer. */
  1394. if (handle->rxRingBuffer != NULL)
  1395. {
  1396. while (0U != count--)
  1397. {
  1398. /* If RX ring buffer is full, trigger callback to notify over run. */
  1399. if (UART_TransferIsRxRingBufferFull(handle))
  1400. {
  1401. if (handle->callback != NULL)
  1402. {
  1403. handle->callback(base, handle, kStatus_UART_RxRingBufferOverrun, handle->userData);
  1404. }
  1405. }
  1406. /* If ring buffer is still full after callback function, the oldest data is overridden. */
  1407. if (UART_TransferIsRxRingBufferFull(handle))
  1408. {
  1409. /* Increase handle->rxRingBufferTail to make room for new data. */
  1410. if ((size_t)handle->rxRingBufferTail + 1U == handle->rxRingBufferSize)
  1411. {
  1412. handle->rxRingBufferTail = 0U;
  1413. }
  1414. else
  1415. {
  1416. handle->rxRingBufferTail++;
  1417. }
  1418. }
  1419. /* Read data. */
  1420. tmpdata = base->D;
  1421. handle->rxRingBuffer[handle->rxRingBufferHead] = tmpdata;
  1422. /* Increase handle->rxRingBufferHead. */
  1423. if ((size_t)handle->rxRingBufferHead + 1U == handle->rxRingBufferSize)
  1424. {
  1425. handle->rxRingBufferHead = 0U;
  1426. }
  1427. else
  1428. {
  1429. handle->rxRingBufferHead++;
  1430. }
  1431. }
  1432. }
  1433. else if (0U == handle->rxDataSize)
  1434. {
  1435. /* Disable RX interrupt/overrun interrupt/fram error/idle line detected interrupt */
  1436. UART_DisableInterrupts(base, (uint32_t)kUART_RxDataRegFullInterruptEnable |
  1437. (uint32_t)kUART_RxOverrunInterruptEnable |
  1438. (uint32_t)kUART_FramingErrorInterruptEnable);
  1439. /* Disable parity error interrupt when parity mode is enable*/
  1440. if ((UART_C1_PE_MASK & base->C1) != 0U)
  1441. {
  1442. UART_DisableInterrupts(base, (uint32_t)kUART_ParityErrorInterruptEnable);
  1443. }
  1444. }
  1445. else
  1446. {
  1447. }
  1448. }
  1449. /* If framing error or parity error happened, stop the RX interrupt when use no ring buffer */
  1450. if (((handle->rxState == (uint8_t)kUART_RxFramingError) || (handle->rxState == (uint8_t)kUART_RxParityError)) &&
  1451. (NULL == handle->rxRingBuffer))
  1452. {
  1453. UART_DisableInterrupts(
  1454. base, (uint32_t)kUART_RxDataRegFullInterruptEnable | (uint32_t)kUART_RxOverrunInterruptEnable |
  1455. (uint32_t)kUART_FramingErrorInterruptEnable | (uint32_t)kUART_IdleLineInterruptEnable);
  1456. /* Disable parity error interrupt when parity mode is enable*/
  1457. if ((UART_C1_PE_MASK & base->C1) != 0U)
  1458. {
  1459. UART_DisableInterrupts(base, (uint32_t)kUART_ParityErrorInterruptEnable);
  1460. }
  1461. }
  1462. /* Send data register empty and the interrupt is enabled. */
  1463. if ((((uint32_t)kUART_TxDataRegEmptyFlag & status) != 0U) && ((base->C2 & UART_C2_TIE_MASK) != 0U))
  1464. {
  1465. /* Get the bytes that available at this moment. */
  1466. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  1467. count = (uint8_t)FSL_FEATURE_UART_FIFO_SIZEn(base) - base->TCFIFO;
  1468. #else
  1469. count = 1;
  1470. #endif
  1471. while ((count != 0U) && (handle->txDataSize != 0U))
  1472. {
  1473. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  1474. tempCount = (uint8_t)MIN(handle->txDataSize, (uint32_t)count);
  1475. #else
  1476. tempCount = 1;
  1477. #endif
  1478. /* Using non block API to write the data to the registers. */
  1479. UART_WriteNonBlocking(base, handle->txData, tempCount);
  1480. handle->txData += tempCount;
  1481. handle->txDataSize -= tempCount;
  1482. count -= tempCount;
  1483. /* If all the data are written to data register, TX finished. */
  1484. if (0U == handle->txDataSize)
  1485. {
  1486. /* Disable TX register empty interrupt. */
  1487. base->C2 = (base->C2 & ~(uint8_t)UART_C2_TIE_MASK);
  1488. /* Enable transmission complete interrupt. */
  1489. base->C2 = (base->C2 | (uint8_t)UART_C2_TCIE_MASK);
  1490. }
  1491. }
  1492. }
  1493. /* Transmission complete and the interrupt is enabled. */
  1494. if ((0U != ((uint32_t)kUART_TransmissionCompleteFlag & status)) && (0U != (base->C2 & UART_C2_TCIE_MASK)))
  1495. {
  1496. /* Set txState to idle only when all data has been sent out to bus. */
  1497. handle->txState = (uint8_t)kUART_TxIdle;
  1498. /* Disable transmission complete interrupt. */
  1499. base->C2 = (base->C2 & ~(uint8_t)UART_C2_TCIE_MASK);
  1500. /* Trigger callback. */
  1501. if (handle->callback != NULL)
  1502. {
  1503. handle->callback(base, handle, kStatus_UART_TxIdle, handle->userData);
  1504. }
  1505. }
  1506. }
  1507. /*!
  1508. * brief UART Error IRQ handle function.
  1509. *
  1510. * This function handles the UART error IRQ request.
  1511. *
  1512. * param base UART peripheral base address.
  1513. * param handle UART handle pointer.
  1514. */
  1515. void UART_TransferHandleErrorIRQ(UART_Type *base, uart_handle_t *handle)
  1516. {
  1517. /* To be implemented by User. */
  1518. }
  1519. #if defined(FSL_FEATURE_UART_HAS_SHARED_IRQ0_IRQ1_IRQ2_IRQ3) && FSL_FEATURE_UART_HAS_SHARED_IRQ0_IRQ1_IRQ2_IRQ3
  1520. void UART0_UART1_UART2_UART3_DriverIRQHandler(void)
  1521. {
  1522. for (uint32_t instance = 0U; instance < 4U; instance++)
  1523. {
  1524. if (s_uartHandle[instance] != NULL)
  1525. {
  1526. s_uartIsr(s_uartBases[instance], s_uartHandle[instance]);
  1527. }
  1528. }
  1529. }
  1530. #else
  1531. #if defined(UART0)
  1532. #if ((!(defined(FSL_FEATURE_SOC_LPSCI_COUNT))) || \
  1533. ((defined(FSL_FEATURE_SOC_LPSCI_COUNT)) && (FSL_FEATURE_SOC_LPSCI_COUNT == 0)))
  1534. void UART0_DriverIRQHandler(void)
  1535. {
  1536. s_uartIsr(UART0, s_uartHandle[0]);
  1537. SDK_ISR_EXIT_BARRIER;
  1538. }
  1539. void UART0_RX_TX_DriverIRQHandler(void)
  1540. {
  1541. UART0_DriverIRQHandler();
  1542. SDK_ISR_EXIT_BARRIER;
  1543. }
  1544. #endif
  1545. #endif
  1546. #if defined(UART1)
  1547. void UART1_DriverIRQHandler(void)
  1548. {
  1549. s_uartIsr(UART1, s_uartHandle[1]);
  1550. SDK_ISR_EXIT_BARRIER;
  1551. }
  1552. void UART1_RX_TX_DriverIRQHandler(void)
  1553. {
  1554. UART1_DriverIRQHandler();
  1555. SDK_ISR_EXIT_BARRIER;
  1556. }
  1557. #endif
  1558. #if defined(UART2)
  1559. void UART2_DriverIRQHandler(void)
  1560. {
  1561. s_uartIsr(UART2, s_uartHandle[2]);
  1562. SDK_ISR_EXIT_BARRIER;
  1563. }
  1564. void UART2_RX_TX_DriverIRQHandler(void)
  1565. {
  1566. UART2_DriverIRQHandler();
  1567. SDK_ISR_EXIT_BARRIER;
  1568. }
  1569. #endif
  1570. #if defined(UART3)
  1571. void UART3_DriverIRQHandler(void)
  1572. {
  1573. s_uartIsr(UART3, s_uartHandle[3]);
  1574. SDK_ISR_EXIT_BARRIER;
  1575. }
  1576. void UART3_RX_TX_DriverIRQHandler(void)
  1577. {
  1578. UART3_DriverIRQHandler();
  1579. SDK_ISR_EXIT_BARRIER;
  1580. }
  1581. #endif
  1582. #endif
  1583. #if defined(UART4)
  1584. void UART4_DriverIRQHandler(void)
  1585. {
  1586. s_uartIsr(UART4, s_uartHandle[4]);
  1587. SDK_ISR_EXIT_BARRIER;
  1588. }
  1589. void UART4_RX_TX_DriverIRQHandler(void)
  1590. {
  1591. UART4_DriverIRQHandler();
  1592. SDK_ISR_EXIT_BARRIER;
  1593. }
  1594. #endif
  1595. #if defined(UART5)
  1596. void UART5_DriverIRQHandler(void)
  1597. {
  1598. s_uartIsr(UART5, s_uartHandle[5]);
  1599. SDK_ISR_EXIT_BARRIER;
  1600. }
  1601. void UART5_RX_TX_DriverIRQHandler(void)
  1602. {
  1603. UART5_DriverIRQHandler();
  1604. SDK_ISR_EXIT_BARRIER;
  1605. }
  1606. #endif