You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

813 lines
32 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. #ifndef _FSL_UART_H_
  9. #define _FSL_UART_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup uart_driver
  13. * @{
  14. */
  15. /*******************************************************************************
  16. * Definitions
  17. ******************************************************************************/
  18. /*! @name Driver version */
  19. /*@{*/
  20. /*! @brief UART driver version 2.3.0. */
  21. #define FSL_UART_DRIVER_VERSION (MAKE_VERSION(2, 3, 0))
  22. /*@}*/
  23. /*! @brief Retry times for waiting flag. */
  24. #ifndef UART_RETRY_TIMES
  25. #define UART_RETRY_TIMES 0U /* Defining to zero means to keep waiting for the flag until it is assert/deassert. */
  26. #endif
  27. /*! @brief Error codes for the UART driver. */
  28. enum
  29. {
  30. kStatus_UART_TxBusy = MAKE_STATUS(kStatusGroup_UART, 0), /*!< Transmitter is busy. */
  31. kStatus_UART_RxBusy = MAKE_STATUS(kStatusGroup_UART, 1), /*!< Receiver is busy. */
  32. kStatus_UART_TxIdle = MAKE_STATUS(kStatusGroup_UART, 2), /*!< UART transmitter is idle. */
  33. kStatus_UART_RxIdle = MAKE_STATUS(kStatusGroup_UART, 3), /*!< UART receiver is idle. */
  34. kStatus_UART_TxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_UART, 4), /*!< TX FIFO watermark too large */
  35. kStatus_UART_RxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_UART, 5), /*!< RX FIFO watermark too large */
  36. kStatus_UART_FlagCannotClearManually =
  37. MAKE_STATUS(kStatusGroup_UART, 6), /*!< UART flag can't be manually cleared. */
  38. kStatus_UART_Error = MAKE_STATUS(kStatusGroup_UART, 7), /*!< Error happens on UART. */
  39. kStatus_UART_RxRingBufferOverrun = MAKE_STATUS(kStatusGroup_UART, 8), /*!< UART RX software ring buffer overrun. */
  40. kStatus_UART_RxHardwareOverrun = MAKE_STATUS(kStatusGroup_UART, 9), /*!< UART RX receiver overrun. */
  41. kStatus_UART_NoiseError = MAKE_STATUS(kStatusGroup_UART, 10), /*!< UART noise error. */
  42. kStatus_UART_FramingError = MAKE_STATUS(kStatusGroup_UART, 11), /*!< UART framing error. */
  43. kStatus_UART_ParityError = MAKE_STATUS(kStatusGroup_UART, 12), /*!< UART parity error. */
  44. kStatus_UART_BaudrateNotSupport =
  45. MAKE_STATUS(kStatusGroup_UART, 13), /*!< Baudrate is not support in current clock source */
  46. kStatus_UART_IdleLineDetected = MAKE_STATUS(kStatusGroup_UART, 14), /*!< UART IDLE line detected. */
  47. kStatus_UART_Timeout = MAKE_STATUS(kStatusGroup_UART, 15), /*!< UART times out. */
  48. };
  49. /*! @brief UART parity mode. */
  50. typedef enum _uart_parity_mode
  51. {
  52. kUART_ParityDisabled = 0x0U, /*!< Parity disabled */
  53. kUART_ParityEven = 0x2U, /*!< Parity enabled, type even, bit setting: PE|PT = 10 */
  54. kUART_ParityOdd = 0x3U, /*!< Parity enabled, type odd, bit setting: PE|PT = 11 */
  55. } uart_parity_mode_t;
  56. /*! @brief UART stop bit count. */
  57. typedef enum _uart_stop_bit_count
  58. {
  59. kUART_OneStopBit = 0U, /*!< One stop bit */
  60. kUART_TwoStopBit = 1U, /*!< Two stop bits */
  61. } uart_stop_bit_count_t;
  62. /*! @brief UART idle type select. */
  63. typedef enum _uart_idle_type_select
  64. {
  65. kUART_IdleTypeStartBit = 0U, /*!< Start counting after a valid start bit. */
  66. kUART_IdleTypeStopBit = 1U, /*!< Start counting after a stop bit. */
  67. } uart_idle_type_select_t;
  68. /*!
  69. * @brief UART interrupt configuration structure, default settings all disabled.
  70. *
  71. * This structure contains the settings for all of the UART interrupt configurations.
  72. */
  73. enum _uart_interrupt_enable
  74. {
  75. #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT
  76. kUART_LinBreakInterruptEnable = (UART_BDH_LBKDIE_MASK), /*!< LIN break detect interrupt. */
  77. #endif
  78. kUART_RxActiveEdgeInterruptEnable = (UART_BDH_RXEDGIE_MASK), /*!< RX active edge interrupt. */
  79. kUART_TxDataRegEmptyInterruptEnable = (UART_C2_TIE_MASK << 8), /*!< Transmit data register empty interrupt. */
  80. kUART_TransmissionCompleteInterruptEnable = (UART_C2_TCIE_MASK << 8), /*!< Transmission complete interrupt. */
  81. kUART_RxDataRegFullInterruptEnable = (UART_C2_RIE_MASK << 8), /*!< Receiver data register full interrupt. */
  82. kUART_IdleLineInterruptEnable = (UART_C2_ILIE_MASK << 8), /*!< Idle line interrupt. */
  83. kUART_RxOverrunInterruptEnable = (UART_C3_ORIE_MASK << 16), /*!< Receiver overrun interrupt. */
  84. kUART_NoiseErrorInterruptEnable = (UART_C3_NEIE_MASK << 16), /*!< Noise error flag interrupt. */
  85. kUART_FramingErrorInterruptEnable = (UART_C3_FEIE_MASK << 16), /*!< Framing error flag interrupt. */
  86. kUART_ParityErrorInterruptEnable = (UART_C3_PEIE_MASK << 16), /*!< Parity error flag interrupt. */
  87. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  88. kUART_RxFifoOverflowInterruptEnable = (UART_CFIFO_RXOFE_MASK << 24), /*!< RX FIFO overflow interrupt. */
  89. kUART_TxFifoOverflowInterruptEnable = (UART_CFIFO_TXOFE_MASK << 24), /*!< TX FIFO overflow interrupt. */
  90. kUART_RxFifoUnderflowInterruptEnable = (UART_CFIFO_RXUFE_MASK << 24), /*!< RX FIFO underflow interrupt. */
  91. #endif
  92. kUART_AllInterruptsEnable =
  93. #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT
  94. kUART_LinBreakInterruptEnable |
  95. #endif
  96. kUART_RxActiveEdgeInterruptEnable | kUART_TxDataRegEmptyInterruptEnable |
  97. kUART_TransmissionCompleteInterruptEnable | kUART_RxDataRegFullInterruptEnable | kUART_IdleLineInterruptEnable |
  98. kUART_RxOverrunInterruptEnable | kUART_NoiseErrorInterruptEnable | kUART_FramingErrorInterruptEnable |
  99. kUART_ParityErrorInterruptEnable
  100. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  101. | kUART_RxFifoOverflowInterruptEnable | kUART_TxFifoOverflowInterruptEnable |
  102. kUART_RxFifoUnderflowInterruptEnable
  103. #endif
  104. ,
  105. };
  106. /*!
  107. * @brief UART status flags.
  108. *
  109. * This provides constants for the UART status flags for use in the UART functions.
  110. */
  111. enum _uart_flags
  112. {
  113. kUART_TxDataRegEmptyFlag = (UART_S1_TDRE_MASK), /*!< TX data register empty flag. */
  114. kUART_TransmissionCompleteFlag = (UART_S1_TC_MASK), /*!< Transmission complete flag. */
  115. kUART_RxDataRegFullFlag = (UART_S1_RDRF_MASK), /*!< RX data register full flag. */
  116. kUART_IdleLineFlag = (UART_S1_IDLE_MASK), /*!< Idle line detect flag. */
  117. kUART_RxOverrunFlag = (UART_S1_OR_MASK), /*!< RX overrun flag. */
  118. kUART_NoiseErrorFlag = (UART_S1_NF_MASK), /*!< RX takes 3 samples of each received bit.
  119. If any of these samples differ, noise flag sets */
  120. kUART_FramingErrorFlag = (UART_S1_FE_MASK), /*!< Frame error flag, sets if logic 0 was detected
  121. where stop bit expected */
  122. kUART_ParityErrorFlag = (UART_S1_PF_MASK), /*!< If parity enabled, sets upon parity error detection */
  123. #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT
  124. kUART_LinBreakFlag =
  125. (UART_S2_LBKDIF_MASK
  126. << 8), /*!< LIN break detect interrupt flag, sets when LIN break char detected and LIN circuit enabled */
  127. #endif
  128. kUART_RxActiveEdgeFlag =
  129. (UART_S2_RXEDGIF_MASK << 8), /*!< RX pin active edge interrupt flag,sets when active edge detected */
  130. kUART_RxActiveFlag =
  131. (UART_S2_RAF_MASK << 8), /*!< Receiver Active Flag (RAF), sets at beginning of valid start bit */
  132. #if defined(FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS) && FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS
  133. kUART_NoiseErrorInRxDataRegFlag = (UART_ED_NOISY_MASK << 16), /*!< Noisy bit, sets if noise detected. */
  134. kUART_ParityErrorInRxDataRegFlag = (UART_ED_PARITYE_MASK << 16), /*!< Parity bit, sets if parity error detected. */
  135. #endif
  136. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  137. kUART_TxFifoEmptyFlag = (int)(UART_SFIFO_TXEMPT_MASK << 24), /*!< TXEMPT bit, sets if TX buffer is empty */
  138. kUART_RxFifoEmptyFlag = (UART_SFIFO_RXEMPT_MASK << 24), /*!< RXEMPT bit, sets if RX buffer is empty */
  139. kUART_TxFifoOverflowFlag = (UART_SFIFO_TXOF_MASK << 24), /*!< TXOF bit, sets if TX buffer overflow occurred */
  140. kUART_RxFifoOverflowFlag = (UART_SFIFO_RXOF_MASK << 24), /*!< RXOF bit, sets if receive buffer overflow */
  141. kUART_RxFifoUnderflowFlag = (UART_SFIFO_RXUF_MASK << 24), /*!< RXUF bit, sets if receive buffer underflow */
  142. #endif
  143. };
  144. /*! @brief UART configuration structure. */
  145. typedef struct _uart_config
  146. {
  147. uint32_t baudRate_Bps; /*!< UART baud rate */
  148. uart_parity_mode_t parityMode; /*!< Parity mode, disabled (default), even, odd */
  149. #if defined(FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
  150. uart_stop_bit_count_t stopBitCount; /*!< Number of stop bits, 1 stop bit (default) or 2 stop bits */
  151. #endif
  152. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  153. uint8_t txFifoWatermark; /*!< TX FIFO watermark */
  154. uint8_t rxFifoWatermark; /*!< RX FIFO watermark */
  155. #endif
  156. #if defined(FSL_FEATURE_UART_HAS_MODEM_SUPPORT) && FSL_FEATURE_UART_HAS_MODEM_SUPPORT
  157. bool enableRxRTS; /*!< RX RTS enable */
  158. bool enableTxCTS; /*!< TX CTS enable */
  159. #endif
  160. uart_idle_type_select_t idleType; /*!< IDLE type select. */
  161. bool enableTx; /*!< Enable TX */
  162. bool enableRx; /*!< Enable RX */
  163. } uart_config_t;
  164. /*! @brief UART transfer structure. */
  165. typedef struct _uart_transfer
  166. {
  167. uint8_t *data; /*!< The buffer of data to be transfer.*/
  168. size_t dataSize; /*!< The byte count to be transfer. */
  169. } uart_transfer_t;
  170. /* Forward declaration of the handle typedef. */
  171. typedef struct _uart_handle uart_handle_t;
  172. /*! @brief UART transfer callback function. */
  173. typedef void (*uart_transfer_callback_t)(UART_Type *base, uart_handle_t *handle, status_t status, void *userData);
  174. /*! @brief UART handle structure. */
  175. struct _uart_handle
  176. {
  177. uint8_t *volatile txData; /*!< Address of remaining data to send. */
  178. volatile size_t txDataSize; /*!< Size of the remaining data to send. */
  179. size_t txDataSizeAll; /*!< Size of the data to send out. */
  180. uint8_t *volatile rxData; /*!< Address of remaining data to receive. */
  181. volatile size_t rxDataSize; /*!< Size of the remaining data to receive. */
  182. size_t rxDataSizeAll; /*!< Size of the data to receive. */
  183. uint8_t *rxRingBuffer; /*!< Start address of the receiver ring buffer. */
  184. size_t rxRingBufferSize; /*!< Size of the ring buffer. */
  185. volatile uint16_t rxRingBufferHead; /*!< Index for the driver to store received data into ring buffer. */
  186. volatile uint16_t rxRingBufferTail; /*!< Index for the user to get data from the ring buffer. */
  187. uart_transfer_callback_t callback; /*!< Callback function. */
  188. void *userData; /*!< UART callback function parameter.*/
  189. volatile uint8_t txState; /*!< TX transfer state. */
  190. volatile uint8_t rxState; /*!< RX transfer state */
  191. };
  192. /*******************************************************************************
  193. * API
  194. ******************************************************************************/
  195. #if defined(__cplusplus)
  196. extern "C" {
  197. #endif /* _cplusplus */
  198. /*!
  199. * @brief Get the UART instance from peripheral base address.
  200. *
  201. * @param base UART peripheral base address.
  202. * @return UART instance.
  203. */
  204. uint32_t UART_GetInstance(UART_Type *base);
  205. /*!
  206. * @name Initialization and deinitialization
  207. * @{
  208. */
  209. /*!
  210. * @brief Initializes a UART instance with a user configuration structure and peripheral clock.
  211. *
  212. * This function configures the UART module with the user-defined settings. The user can configure the configuration
  213. * structure and also get the default configuration by using the UART_GetDefaultConfig() function.
  214. * The example below shows how to use this API to configure UART.
  215. * @code
  216. * uart_config_t uartConfig;
  217. * uartConfig.baudRate_Bps = 115200U;
  218. * uartConfig.parityMode = kUART_ParityDisabled;
  219. * uartConfig.stopBitCount = kUART_OneStopBit;
  220. * uartConfig.txFifoWatermark = 0;
  221. * uartConfig.rxFifoWatermark = 1;
  222. * UART_Init(UART1, &uartConfig, 20000000U);
  223. * @endcode
  224. *
  225. * @param base UART peripheral base address.
  226. * @param config Pointer to the user-defined configuration structure.
  227. * @param srcClock_Hz UART clock source frequency in HZ.
  228. * @retval kStatus_UART_BaudrateNotSupport Baudrate is not support in current clock source.
  229. * @retval kStatus_Success Status UART initialize succeed
  230. */
  231. status_t UART_Init(UART_Type *base, const uart_config_t *config, uint32_t srcClock_Hz);
  232. /*!
  233. * @brief Deinitializes a UART instance.
  234. *
  235. * This function waits for TX complete, disables TX and RX, and disables the UART clock.
  236. *
  237. * @param base UART peripheral base address.
  238. */
  239. void UART_Deinit(UART_Type *base);
  240. /*!
  241. * @brief Gets the default configuration structure.
  242. *
  243. * This function initializes the UART configuration structure to a default value. The default
  244. * values are as follows.
  245. * uartConfig->baudRate_Bps = 115200U;
  246. * uartConfig->bitCountPerChar = kUART_8BitsPerChar;
  247. * uartConfig->parityMode = kUART_ParityDisabled;
  248. * uartConfig->stopBitCount = kUART_OneStopBit;
  249. * uartConfig->txFifoWatermark = 0;
  250. * uartConfig->rxFifoWatermark = 1;
  251. * uartConfig->idleType = kUART_IdleTypeStartBit;
  252. * uartConfig->enableTx = false;
  253. * uartConfig->enableRx = false;
  254. *
  255. * @param config Pointer to configuration structure.
  256. */
  257. void UART_GetDefaultConfig(uart_config_t *config);
  258. /*!
  259. * @brief Sets the UART instance baud rate.
  260. *
  261. * This function configures the UART module baud rate. This function is used to update
  262. * the UART module baud rate after the UART module is initialized by the UART_Init.
  263. * @code
  264. * UART_SetBaudRate(UART1, 115200U, 20000000U);
  265. * @endcode
  266. *
  267. * @param base UART peripheral base address.
  268. * @param baudRate_Bps UART baudrate to be set.
  269. * @param srcClock_Hz UART clock source frequency in Hz.
  270. * @retval kStatus_UART_BaudrateNotSupport Baudrate is not support in the current clock source.
  271. * @retval kStatus_Success Set baudrate succeeded.
  272. */
  273. status_t UART_SetBaudRate(UART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
  274. /* @} */
  275. /*!
  276. * @name Status
  277. * @{
  278. */
  279. /*!
  280. * @brief Gets UART status flags.
  281. *
  282. * This function gets all UART status flags. The flags are returned as the logical
  283. * OR value of the enumerators @ref _uart_flags. To check a specific status,
  284. * compare the return value with enumerators in @ref _uart_flags.
  285. * For example, to check whether the TX is empty, do the following.
  286. * @code
  287. * if (kUART_TxDataRegEmptyFlag & UART_GetStatusFlags(UART1))
  288. * {
  289. * ...
  290. * }
  291. * @endcode
  292. *
  293. * @param base UART peripheral base address.
  294. * @return UART status flags which are ORed by the enumerators in the _uart_flags.
  295. */
  296. uint32_t UART_GetStatusFlags(UART_Type *base);
  297. /*!
  298. * @brief Clears status flags with the provided mask.
  299. *
  300. * This function clears UART status flags with a provided mask. An automatically cleared flag
  301. * can't be cleared by this function.
  302. * These flags can only be cleared or set by hardware.
  303. * kUART_TxDataRegEmptyFlag, kUART_TransmissionCompleteFlag, kUART_RxDataRegFullFlag,
  304. * kUART_RxActiveFlag, kUART_NoiseErrorInRxDataRegFlag, kUART_ParityErrorInRxDataRegFlag,
  305. * kUART_TxFifoEmptyFlag,kUART_RxFifoEmptyFlag
  306. * Note that this API should be called when the Tx/Rx is idle. Otherwise it has no effect.
  307. *
  308. * @param base UART peripheral base address.
  309. * @param mask The status flags to be cleared; it is logical OR value of @ref _uart_flags.
  310. * @retval kStatus_UART_FlagCannotClearManually The flag can't be cleared by this function but
  311. * it is cleared automatically by hardware.
  312. * @retval kStatus_Success Status in the mask is cleared.
  313. */
  314. status_t UART_ClearStatusFlags(UART_Type *base, uint32_t mask);
  315. /* @} */
  316. /*!
  317. * @name Interrupts
  318. * @{
  319. */
  320. /*!
  321. * @brief Enables UART interrupts according to the provided mask.
  322. *
  323. * This function enables the UART interrupts according to the provided mask. The mask
  324. * is a logical OR of enumeration members. See @ref _uart_interrupt_enable.
  325. * For example, to enable TX empty interrupt and RX full interrupt, do the following.
  326. * @code
  327. * UART_EnableInterrupts(UART1,kUART_TxDataRegEmptyInterruptEnable | kUART_RxDataRegFullInterruptEnable);
  328. * @endcode
  329. *
  330. * @param base UART peripheral base address.
  331. * @param mask The interrupts to enable. Logical OR of @ref _uart_interrupt_enable.
  332. */
  333. void UART_EnableInterrupts(UART_Type *base, uint32_t mask);
  334. /*!
  335. * @brief Disables the UART interrupts according to the provided mask.
  336. *
  337. * This function disables the UART interrupts according to the provided mask. The mask
  338. * is a logical OR of enumeration members. See @ref _uart_interrupt_enable.
  339. * For example, to disable TX empty interrupt and RX full interrupt do the following.
  340. * @code
  341. * UART_DisableInterrupts(UART1,kUART_TxDataRegEmptyInterruptEnable | kUART_RxDataRegFullInterruptEnable);
  342. * @endcode
  343. *
  344. * @param base UART peripheral base address.
  345. * @param mask The interrupts to disable. Logical OR of @ref _uart_interrupt_enable.
  346. */
  347. void UART_DisableInterrupts(UART_Type *base, uint32_t mask);
  348. /*!
  349. * @brief Gets the enabled UART interrupts.
  350. *
  351. * This function gets the enabled UART interrupts. The enabled interrupts are returned
  352. * as the logical OR value of the enumerators @ref _uart_interrupt_enable. To check
  353. * a specific interrupts enable status, compare the return value with enumerators
  354. * in @ref _uart_interrupt_enable.
  355. * For example, to check whether TX empty interrupt is enabled, do the following.
  356. * @code
  357. * uint32_t enabledInterrupts = UART_GetEnabledInterrupts(UART1);
  358. *
  359. * if (kUART_TxDataRegEmptyInterruptEnable & enabledInterrupts)
  360. * {
  361. * ...
  362. * }
  363. * @endcode
  364. *
  365. * @param base UART peripheral base address.
  366. * @return UART interrupt flags which are logical OR of the enumerators in @ref _uart_interrupt_enable.
  367. */
  368. uint32_t UART_GetEnabledInterrupts(UART_Type *base);
  369. /* @} */
  370. #if defined(FSL_FEATURE_UART_HAS_DMA_SELECT) && FSL_FEATURE_UART_HAS_DMA_SELECT
  371. /*!
  372. * @name DMA Control
  373. * @{
  374. */
  375. /*!
  376. * @brief Gets the UART data register address.
  377. *
  378. * This function returns the UART data register address, which is mainly used by DMA/eDMA.
  379. *
  380. * @param base UART peripheral base address.
  381. * @return UART data register addresses which are used both by the transmitter and the receiver.
  382. */
  383. static inline uint32_t UART_GetDataRegisterAddress(UART_Type *base)
  384. {
  385. return (uint32_t) & (base->D);
  386. }
  387. /*!
  388. * @brief Enables or disables the UART transmitter DMA request.
  389. *
  390. * This function enables or disables the transmit data register empty flag, S1[TDRE], to generate the DMA requests.
  391. *
  392. * @param base UART peripheral base address.
  393. * @param enable True to enable, false to disable.
  394. */
  395. static inline void UART_EnableTxDMA(UART_Type *base, bool enable)
  396. {
  397. if (enable)
  398. {
  399. #if (defined(FSL_FEATURE_UART_IS_SCI) && FSL_FEATURE_UART_IS_SCI)
  400. base->C4 |= (uint8_t)UART_C4_TDMAS_MASK;
  401. #else
  402. base->C5 |= (uint8_t)UART_C5_TDMAS_MASK;
  403. #endif
  404. base->C2 |= (uint8_t)UART_C2_TIE_MASK;
  405. }
  406. else
  407. {
  408. #if (defined(FSL_FEATURE_UART_IS_SCI) && FSL_FEATURE_UART_IS_SCI)
  409. base->C4 &= ~(uint8_t)UART_C4_TDMAS_MASK;
  410. #else
  411. base->C5 &= ~(uint8_t)UART_C5_TDMAS_MASK;
  412. #endif
  413. base->C2 &= ~(uint8_t)UART_C2_TIE_MASK;
  414. }
  415. }
  416. /*!
  417. * @brief Enables or disables the UART receiver DMA.
  418. *
  419. * This function enables or disables the receiver data register full flag, S1[RDRF], to generate DMA requests.
  420. *
  421. * @param base UART peripheral base address.
  422. * @param enable True to enable, false to disable.
  423. */
  424. static inline void UART_EnableRxDMA(UART_Type *base, bool enable)
  425. {
  426. if (enable)
  427. {
  428. #if (defined(FSL_FEATURE_UART_IS_SCI) && FSL_FEATURE_UART_IS_SCI)
  429. base->C4 |= (uint8_t)UART_C4_RDMAS_MASK;
  430. #else
  431. base->C5 |= (uint8_t)UART_C5_RDMAS_MASK;
  432. #endif
  433. base->C2 |= (uint8_t)UART_C2_RIE_MASK;
  434. }
  435. else
  436. {
  437. #if (defined(FSL_FEATURE_UART_IS_SCI) && FSL_FEATURE_UART_IS_SCI)
  438. base->C4 &= ~(uint8_t)UART_C4_RDMAS_MASK;
  439. #else
  440. base->C5 &= ~(uint8_t)UART_C5_RDMAS_MASK;
  441. #endif
  442. base->C2 &= ~(uint8_t)UART_C2_RIE_MASK;
  443. }
  444. }
  445. /* @} */
  446. #endif /* FSL_FEATURE_UART_HAS_DMA_SELECT */
  447. /*!
  448. * @name Bus Operations
  449. * @{
  450. */
  451. /*!
  452. * @brief Enables or disables the UART transmitter.
  453. *
  454. * This function enables or disables the UART transmitter.
  455. *
  456. * @param base UART peripheral base address.
  457. * @param enable True to enable, false to disable.
  458. */
  459. static inline void UART_EnableTx(UART_Type *base, bool enable)
  460. {
  461. if (enable)
  462. {
  463. base->C2 |= (uint8_t)UART_C2_TE_MASK;
  464. }
  465. else
  466. {
  467. base->C2 &= ~(uint8_t)UART_C2_TE_MASK;
  468. }
  469. }
  470. /*!
  471. * @brief Enables or disables the UART receiver.
  472. *
  473. * This function enables or disables the UART receiver.
  474. *
  475. * @param base UART peripheral base address.
  476. * @param enable True to enable, false to disable.
  477. */
  478. static inline void UART_EnableRx(UART_Type *base, bool enable)
  479. {
  480. if (enable)
  481. {
  482. base->C2 |= (uint8_t)UART_C2_RE_MASK;
  483. }
  484. else
  485. {
  486. base->C2 &= ~(uint8_t)UART_C2_RE_MASK;
  487. }
  488. }
  489. /*!
  490. * @brief Writes to the TX register.
  491. *
  492. * This function writes data to the TX register directly. The upper layer must ensure
  493. * that the TX register is empty or TX FIFO has empty room before calling this function.
  494. *
  495. * @param base UART peripheral base address.
  496. * @param data The byte to write.
  497. */
  498. static inline void UART_WriteByte(UART_Type *base, uint8_t data)
  499. {
  500. base->D = data;
  501. }
  502. /*!
  503. * @brief Reads the RX register directly.
  504. *
  505. * This function reads data from the RX register directly. The upper layer must
  506. * ensure that the RX register is full or that the TX FIFO has data before calling this function.
  507. *
  508. * @param base UART peripheral base address.
  509. * @return The byte read from UART data register.
  510. */
  511. static inline uint8_t UART_ReadByte(UART_Type *base)
  512. {
  513. return base->D;
  514. }
  515. /*!
  516. * @brief Writes to the TX register using a blocking method.
  517. *
  518. * This function polls the TX register, waits for the TX register to be empty or for the TX FIFO
  519. * to have room and writes data to the TX buffer.
  520. *
  521. * @param base UART peripheral base address.
  522. * @param data Start address of the data to write.
  523. * @param length Size of the data to write.
  524. * @retval kStatus_UART_Timeout Transmission timed out and was aborted.
  525. * @retval kStatus_Success Successfully wrote all data.
  526. */
  527. status_t UART_WriteBlocking(UART_Type *base, const uint8_t *data, size_t length);
  528. /*!
  529. * @brief Read RX data register using a blocking method.
  530. *
  531. * This function polls the RX register, waits for the RX register to be full or for RX FIFO to
  532. * have data, and reads data from the TX register.
  533. *
  534. * @param base UART peripheral base address.
  535. * @param data Start address of the buffer to store the received data.
  536. * @param length Size of the buffer.
  537. * @retval kStatus_UART_RxHardwareOverrun Receiver overrun occurred while receiving data.
  538. * @retval kStatus_UART_NoiseError A noise error occurred while receiving data.
  539. * @retval kStatus_UART_FramingError A framing error occurred while receiving data.
  540. * @retval kStatus_UART_ParityError A parity error occurred while receiving data.
  541. * @retval kStatus_UART_Timeout Transmission timed out and was aborted.
  542. * @retval kStatus_Success Successfully received all data.
  543. */
  544. status_t UART_ReadBlocking(UART_Type *base, uint8_t *data, size_t length);
  545. /* @} */
  546. /*!
  547. * @name Transactional
  548. * @{
  549. */
  550. /*!
  551. * @brief Initializes the UART handle.
  552. *
  553. * This function initializes the UART handle which can be used for other UART
  554. * transactional APIs. Usually, for a specified UART instance,
  555. * call this API once to get the initialized handle.
  556. *
  557. * @param base UART peripheral base address.
  558. * @param handle UART handle pointer.
  559. * @param callback The callback function.
  560. * @param userData The parameter of the callback function.
  561. */
  562. void UART_TransferCreateHandle(UART_Type *base,
  563. uart_handle_t *handle,
  564. uart_transfer_callback_t callback,
  565. void *userData);
  566. /*!
  567. * @brief Sets up the RX ring buffer.
  568. *
  569. * This function sets up the RX ring buffer to a specific UART handle.
  570. *
  571. * When the RX ring buffer is used, data received are stored into the ring buffer even when the
  572. * user doesn't call the UART_TransferReceiveNonBlocking() API. If data is already received
  573. * in the ring buffer, the user can get the received data from the ring buffer directly.
  574. *
  575. * @note When using the RX ring buffer, one byte is reserved for internal use. In other
  576. * words, if @p ringBufferSize is 32, only 31 bytes are used for saving data.
  577. *
  578. * @param base UART peripheral base address.
  579. * @param handle UART handle pointer.
  580. * @param ringBuffer Start address of the ring buffer for background receiving. Pass NULL to disable the ring buffer.
  581. * @param ringBufferSize Size of the ring buffer.
  582. */
  583. void UART_TransferStartRingBuffer(UART_Type *base, uart_handle_t *handle, uint8_t *ringBuffer, size_t ringBufferSize);
  584. /*!
  585. * @brief Aborts the background transfer and uninstalls the ring buffer.
  586. *
  587. * This function aborts the background transfer and uninstalls the ring buffer.
  588. *
  589. * @param base UART peripheral base address.
  590. * @param handle UART handle pointer.
  591. */
  592. void UART_TransferStopRingBuffer(UART_Type *base, uart_handle_t *handle);
  593. /*!
  594. * @brief Get the length of received data in RX ring buffer.
  595. *
  596. * @param handle UART handle pointer.
  597. * @return Length of received data in RX ring buffer.
  598. */
  599. size_t UART_TransferGetRxRingBufferLength(uart_handle_t *handle);
  600. /*!
  601. * @brief Transmits a buffer of data using the interrupt method.
  602. *
  603. * This function sends data using an interrupt method. This is a non-blocking function, which
  604. * returns directly without waiting for all data to be written to the TX register. When
  605. * all data is written to the TX register in the ISR, the UART driver calls the callback
  606. * function and passes the @ref kStatus_UART_TxIdle as status parameter.
  607. *
  608. * @note The kStatus_UART_TxIdle is passed to the upper layer when all data is written
  609. * to the TX register. However, it does not ensure that all data is sent out. Before disabling the TX,
  610. * check the kUART_TransmissionCompleteFlag to ensure that the TX is finished.
  611. *
  612. * @param base UART peripheral base address.
  613. * @param handle UART handle pointer.
  614. * @param xfer UART transfer structure. See #uart_transfer_t.
  615. * @retval kStatus_Success Successfully start the data transmission.
  616. * @retval kStatus_UART_TxBusy Previous transmission still not finished; data not all written to TX register yet.
  617. * @retval kStatus_InvalidArgument Invalid argument.
  618. */
  619. status_t UART_TransferSendNonBlocking(UART_Type *base, uart_handle_t *handle, uart_transfer_t *xfer);
  620. /*!
  621. * @brief Aborts the interrupt-driven data transmit.
  622. *
  623. * This function aborts the interrupt-driven data sending. The user can get the remainBytes to find out
  624. * how many bytes are not sent out.
  625. *
  626. * @param base UART peripheral base address.
  627. * @param handle UART handle pointer.
  628. */
  629. void UART_TransferAbortSend(UART_Type *base, uart_handle_t *handle);
  630. /*!
  631. * @brief Gets the number of bytes sent out to bus.
  632. *
  633. * This function gets the number of bytes sent out to bus by using the interrupt method.
  634. *
  635. * @param base UART peripheral base address.
  636. * @param handle UART handle pointer.
  637. * @param count Send bytes count.
  638. * @retval kStatus_NoTransferInProgress No send in progress.
  639. * @retval kStatus_InvalidArgument The parameter is invalid.
  640. * @retval kStatus_Success Get successfully through the parameter \p count;
  641. */
  642. status_t UART_TransferGetSendCount(UART_Type *base, uart_handle_t *handle, uint32_t *count);
  643. /*!
  644. * @brief Receives a buffer of data using an interrupt method.
  645. *
  646. * This function receives data using an interrupt method. This is a non-blocking function, which
  647. * returns without waiting for all data to be received.
  648. * If the RX ring buffer is used and not empty, the data in the ring buffer is copied and
  649. * the parameter @p receivedBytes shows how many bytes are copied from the ring buffer.
  650. * After copying, if the data in the ring buffer is not enough to read, the receive
  651. * request is saved by the UART driver. When the new data arrives, the receive request
  652. * is serviced first. When all data is received, the UART driver notifies the upper layer
  653. * through a callback function and passes the status parameter @ref kStatus_UART_RxIdle.
  654. * For example, the upper layer needs 10 bytes but there are only 5 bytes in the ring buffer.
  655. * The 5 bytes are copied to the xfer->data and this function returns with the
  656. * parameter @p receivedBytes set to 5. For the left 5 bytes, newly arrived data is
  657. * saved from the xfer->data[5]. When 5 bytes are received, the UART driver notifies the upper layer.
  658. * If the RX ring buffer is not enabled, this function enables the RX and RX interrupt
  659. * to receive data to the xfer->data. When all data is received, the upper layer is notified.
  660. *
  661. * @param base UART peripheral base address.
  662. * @param handle UART handle pointer.
  663. * @param xfer UART transfer structure, see #uart_transfer_t.
  664. * @param receivedBytes Bytes received from the ring buffer directly.
  665. * @retval kStatus_Success Successfully queue the transfer into transmit queue.
  666. * @retval kStatus_UART_RxBusy Previous receive request is not finished.
  667. * @retval kStatus_InvalidArgument Invalid argument.
  668. */
  669. status_t UART_TransferReceiveNonBlocking(UART_Type *base,
  670. uart_handle_t *handle,
  671. uart_transfer_t *xfer,
  672. size_t *receivedBytes);
  673. /*!
  674. * @brief Aborts the interrupt-driven data receiving.
  675. *
  676. * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to know
  677. * how many bytes are not received yet.
  678. *
  679. * @param base UART peripheral base address.
  680. * @param handle UART handle pointer.
  681. */
  682. void UART_TransferAbortReceive(UART_Type *base, uart_handle_t *handle);
  683. /*!
  684. * @brief Gets the number of bytes that have been received.
  685. *
  686. * This function gets the number of bytes that have been received.
  687. *
  688. * @param base UART peripheral base address.
  689. * @param handle UART handle pointer.
  690. * @param count Receive bytes count.
  691. * @retval kStatus_NoTransferInProgress No receive in progress.
  692. * @retval kStatus_InvalidArgument Parameter is invalid.
  693. * @retval kStatus_Success Get successfully through the parameter \p count;
  694. */
  695. status_t UART_TransferGetReceiveCount(UART_Type *base, uart_handle_t *handle, uint32_t *count);
  696. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  697. /*!
  698. * @brief Enables or disables the UART Tx FIFO.
  699. *
  700. * This function enables or disables the UART Tx FIFO.
  701. *
  702. * param base UART peripheral base address.
  703. * param enable true to enable, false to disable.
  704. * retval kStatus_Success Successfully turn on or turn off Tx FIFO.
  705. * retval kStatus_Fail Fail to turn on or turn off Tx FIFO.
  706. */
  707. status_t UART_EnableTxFIFO(UART_Type *base, bool enable);
  708. /*!
  709. * @brief Enables or disables the UART Rx FIFO.
  710. *
  711. * This function enables or disables the UART Rx FIFO.
  712. *
  713. * param base UART peripheral base address.
  714. * param enable true to enable, false to disable.
  715. * retval kStatus_Success Successfully turn on or turn off Rx FIFO.
  716. * retval kStatus_Fail Fail to turn on or turn off Rx FIFO.
  717. */
  718. status_t UART_EnableRxFIFO(UART_Type *base, bool enable);
  719. #endif /* FSL_FEATURE_UART_HAS_FIFO */
  720. /*!
  721. * @brief UART IRQ handle function.
  722. *
  723. * This function handles the UART transmit and receive IRQ request.
  724. *
  725. * @param base UART peripheral base address.
  726. * @param handle UART handle pointer.
  727. */
  728. void UART_TransferHandleIRQ(UART_Type *base, uart_handle_t *handle);
  729. /*!
  730. * @brief UART Error IRQ handle function.
  731. *
  732. * This function handles the UART error IRQ request.
  733. *
  734. * @param base UART peripheral base address.
  735. * @param handle UART handle pointer.
  736. */
  737. void UART_TransferHandleErrorIRQ(UART_Type *base, uart_handle_t *handle);
  738. /* @} */
  739. #if defined(__cplusplus)
  740. }
  741. #endif
  742. /*! @}*/
  743. #endif /* _FSL_UART_H_ */