Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fsl_uart.h Source File

fsl_uart.h

00001 /*
00002  * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
00003  * Copyright 2016-2017 NXP
00004  * All rights reserved.
00005  *
00006  * SPDX-License-Identifier: BSD-3-Clause
00007  */
00008 #ifndef _FSL_UART_H_
00009 #define _FSL_UART_H_
00010 
00011 #include "fsl_common.h"
00012 
00013 /*!
00014  * @addtogroup uart_driver
00015  * @{
00016  */
00017 
00018 /*******************************************************************************
00019  * Definitions
00020  ******************************************************************************/
00021 
00022 /*! @name Driver version */
00023 /*@{*/
00024 /*! @brief UART driver version 2.1.6. */
00025 #define FSL_UART_DRIVER_VERSION (MAKE_VERSION(2, 1, 6))
00026 /*@}*/
00027 
00028 /*! @brief Error codes for the UART driver. */
00029 enum _uart_status
00030 {
00031     kStatus_UART_TxBusy  = MAKE_STATUS(kStatusGroup_UART , 0),              /*!< Transmitter is busy. */
00032     kStatus_UART_RxBusy  = MAKE_STATUS(kStatusGroup_UART , 1),              /*!< Receiver is busy. */
00033     kStatus_UART_TxIdle  = MAKE_STATUS(kStatusGroup_UART , 2),              /*!< UART transmitter is idle. */
00034     kStatus_UART_RxIdle  = MAKE_STATUS(kStatusGroup_UART , 3),              /*!< UART receiver is idle. */
00035     kStatus_UART_TxWatermarkTooLarge  = MAKE_STATUS(kStatusGroup_UART , 4), /*!< TX FIFO watermark too large  */
00036     kStatus_UART_RxWatermarkTooLarge  = MAKE_STATUS(kStatusGroup_UART , 5), /*!< RX FIFO watermark too large  */
00037     kStatus_UART_FlagCannotClearManually  =
00038         MAKE_STATUS(kStatusGroup_UART , 6),                                /*!< UART flag can't be manually cleared. */
00039     kStatus_UART_Error  = MAKE_STATUS(kStatusGroup_UART , 7),               /*!< Error happens on UART. */
00040     kStatus_UART_RxRingBufferOverrun  = MAKE_STATUS(kStatusGroup_UART , 8), /*!< UART RX software ring buffer overrun. */
00041     kStatus_UART_RxHardwareOverrun  = MAKE_STATUS(kStatusGroup_UART , 9),   /*!< UART RX receiver overrun. */
00042     kStatus_UART_NoiseError  = MAKE_STATUS(kStatusGroup_UART , 10),         /*!< UART noise error. */
00043     kStatus_UART_FramingError  = MAKE_STATUS(kStatusGroup_UART , 11),       /*!< UART framing error. */
00044     kStatus_UART_ParityError  = MAKE_STATUS(kStatusGroup_UART , 12),        /*!< UART parity error. */
00045     kStatus_UART_BaudrateNotSupport  =
00046         MAKE_STATUS(kStatusGroup_UART , 13), /*!< Baudrate is not support in current clock source */
00047     kStatus_UART_IdleLineDetected  = MAKE_STATUS(kStatusGroup_UART , 14), /*!< UART IDLE line detected. */
00048 };
00049 
00050 /*! @brief UART parity mode. */
00051 typedef enum _uart_parity_mode
00052 {
00053     kUART_ParityDisabled  = 0x0U, /*!< Parity disabled */
00054     kUART_ParityEven  = 0x2U,     /*!< Parity enabled, type even, bit setting: PE|PT = 10 */
00055     kUART_ParityOdd  = 0x3U,      /*!< Parity enabled, type odd,  bit setting: PE|PT = 11 */
00056 } uart_parity_mode_t;
00057 
00058 /*! @brief UART stop bit count. */
00059 typedef enum _uart_stop_bit_count
00060 {
00061     kUART_OneStopBit  = 0U, /*!< One stop bit */
00062     kUART_TwoStopBit  = 1U, /*!< Two stop bits */
00063 } uart_stop_bit_count_t;
00064 
00065 /*! @brief UART idle type select. */
00066 typedef enum _uart_idle_type_select
00067 {
00068     kUART_IdleTypeStartBit  = 0U, /*!< Start counting after a valid start bit. */
00069     kUART_IdleTypeStopBit  = 1U,  /*!< Start counting after a stop bit. */
00070 } uart_idle_type_select_t;
00071 
00072 /*!
00073  * @brief UART interrupt configuration structure, default settings all disabled.
00074  *
00075  * This structure contains the settings for all of the UART interrupt configurations.
00076  */
00077 enum _uart_interrupt_enable
00078 {
00079 #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT
00080     kUART_LinBreakInterruptEnable  = (UART_BDH_LBKDIE_MASK), /*!< LIN break detect interrupt. */
00081 #endif
00082     kUART_RxActiveEdgeInterruptEnable  = (UART_BDH_RXEDGIE_MASK),   /*!< RX active edge interrupt. */
00083     kUART_TxDataRegEmptyInterruptEnable  = (UART_C2_TIE_MASK << 8), /*!< Transmit data register empty interrupt. */
00084     kUART_TransmissionCompleteInterruptEnable  = (UART_C2_TCIE_MASK << 8), /*!< Transmission complete interrupt. */
00085     kUART_RxDataRegFullInterruptEnable  = (UART_C2_RIE_MASK << 8),         /*!< Receiver data register full interrupt. */
00086     kUART_IdleLineInterruptEnable  = (UART_C2_ILIE_MASK << 8),             /*!< Idle line interrupt. */
00087     kUART_RxOverrunInterruptEnable  = (UART_C3_ORIE_MASK << 16),           /*!< Receiver overrun interrupt. */
00088     kUART_NoiseErrorInterruptEnable  = (UART_C3_NEIE_MASK << 16),          /*!< Noise error flag interrupt. */
00089     kUART_FramingErrorInterruptEnable  = (UART_C3_FEIE_MASK << 16),        /*!< Framing error flag interrupt. */
00090     kUART_ParityErrorInterruptEnable  = (UART_C3_PEIE_MASK << 16),         /*!< Parity error flag interrupt. */
00091 #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
00092     kUART_RxFifoOverflowInterruptEnable  = (UART_CFIFO_RXOFE_MASK << 24),  /*!< RX FIFO overflow interrupt. */
00093     kUART_TxFifoOverflowInterruptEnable  = (UART_CFIFO_TXOFE_MASK << 24),  /*!< TX FIFO overflow interrupt. */
00094     kUART_RxFifoUnderflowInterruptEnable  = (UART_CFIFO_RXUFE_MASK << 24), /*!< RX FIFO underflow interrupt. */
00095 #endif
00096     kUART_AllInterruptsEnable =
00097 #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT
00098         kUART_LinBreakInterruptEnable  |
00099 #endif
00100         kUART_RxActiveEdgeInterruptEnable  | kUART_TxDataRegEmptyInterruptEnable  |
00101         kUART_TransmissionCompleteInterruptEnable  | kUART_RxDataRegFullInterruptEnable  | kUART_IdleLineInterruptEnable  |
00102         kUART_RxOverrunInterruptEnable  | kUART_NoiseErrorInterruptEnable  | kUART_FramingErrorInterruptEnable  |
00103         kUART_ParityErrorInterruptEnable 
00104 #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
00105         |
00106         kUART_RxFifoOverflowInterruptEnable  | kUART_TxFifoOverflowInterruptEnable  | kUART_RxFifoUnderflowInterruptEnable 
00107 #endif
00108     ,
00109 };
00110 
00111 /*!
00112  * @brief UART status flags.
00113  *
00114  * This provides constants for the UART status flags for use in the UART functions.
00115  */
00116 enum _uart_flags
00117 {
00118     kUART_TxDataRegEmptyFlag  = (UART_S1_TDRE_MASK),     /*!< TX data register empty flag. */
00119     kUART_TransmissionCompleteFlag  = (UART_S1_TC_MASK), /*!< Transmission complete flag. */
00120     kUART_RxDataRegFullFlag  = (UART_S1_RDRF_MASK),      /*!< RX data register full flag. */
00121     kUART_IdleLineFlag  = (UART_S1_IDLE_MASK),           /*!< Idle line detect flag. */
00122     kUART_RxOverrunFlag  = (UART_S1_OR_MASK),            /*!< RX overrun flag. */
00123     kUART_NoiseErrorFlag  = (UART_S1_NF_MASK),           /*!< RX takes 3 samples of each received bit.
00124                                                              If any of these samples differ, noise flag sets */
00125     kUART_FramingErrorFlag  = (UART_S1_FE_MASK),         /*!< Frame error flag, sets if logic 0 was detected
00126                                                              where stop bit expected */
00127     kUART_ParityErrorFlag  = (UART_S1_PF_MASK),          /*!< If parity enabled, sets upon parity error detection */
00128 #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT
00129     kUART_LinBreakFlag  =
00130         (UART_S2_LBKDIF_MASK
00131          << 8), /*!< LIN break detect interrupt flag, sets when LIN break char detected and LIN circuit enabled */
00132 #endif
00133     kUART_RxActiveEdgeFlag  =
00134         (UART_S2_RXEDGIF_MASK << 8), /*!< RX pin active edge interrupt flag,sets when active edge detected */
00135     kUART_RxActiveFlag  =
00136         (UART_S2_RAF_MASK << 8), /*!< Receiver Active Flag (RAF), sets at beginning of valid start bit */
00137 #if defined(FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS) && FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS
00138     kUART_NoiseErrorInRxDataRegFlag  = (UART_ED_NOISY_MASK << 16),    /*!< Noisy bit, sets if noise detected. */
00139     kUART_ParityErrorInRxDataRegFlag  = (UART_ED_PARITYE_MASK << 16), /*!< Parity bit, sets if parity error detected. */
00140 #endif
00141 #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
00142     kUART_TxFifoEmptyFlag  = (int)(UART_SFIFO_TXEMPT_MASK << 24), /*!< TXEMPT bit, sets if TX buffer is empty */
00143     kUART_RxFifoEmptyFlag  = (UART_SFIFO_RXEMPT_MASK << 24),      /*!< RXEMPT bit, sets if RX buffer is empty */
00144     kUART_TxFifoOverflowFlag  = (UART_SFIFO_TXOF_MASK << 24),     /*!< TXOF bit, sets if TX buffer overflow occurred */
00145     kUART_RxFifoOverflowFlag  = (UART_SFIFO_RXOF_MASK << 24),     /*!< RXOF bit, sets if receive buffer overflow */
00146     kUART_RxFifoUnderflowFlag  = (UART_SFIFO_RXUF_MASK << 24),    /*!< RXUF bit, sets if receive buffer underflow */
00147 #endif
00148 };
00149 
00150 /*! @brief UART configuration structure. */
00151 typedef struct _uart_config
00152 {
00153     uint32_t baudRate_Bps ;         /*!< UART baud rate  */
00154     uart_parity_mode_t parityMode ; /*!< Parity mode, disabled (default), even, odd */
00155 #if defined(FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
00156     uart_stop_bit_count_t stopBitCount ; /*!< Number of stop bits, 1 stop bit (default) or 2 stop bits  */
00157 #endif
00158 #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
00159     uint8_t txFifoWatermark ; /*!< TX FIFO watermark */
00160     uint8_t rxFifoWatermark ; /*!< RX FIFO watermark */
00161 #endif
00162 #if defined(FSL_FEATURE_UART_HAS_MODEM_SUPPORT) && FSL_FEATURE_UART_HAS_MODEM_SUPPORT
00163     bool enableRxRTS ; /*!< RX RTS enable */
00164     bool enableTxCTS ; /*!< TX CTS enable */
00165 #endif
00166     uart_idle_type_select_t idleType ; /*!< IDLE type select. */
00167     bool enableTx ;                    /*!< Enable TX */
00168     bool enableRx ;                    /*!< Enable RX */
00169 } uart_config_t;
00170 
00171 /*! @brief UART transfer structure. */
00172 typedef struct _uart_transfer
00173 {
00174     uint8_t *data ;   /*!< The buffer of data to be transfer.*/
00175     size_t dataSize ; /*!< The byte count to be transfer. */
00176 } uart_transfer_t;
00177 
00178 /* Forward declaration of the handle typedef. */
00179 typedef struct _uart_handle uart_handle_t;
00180 
00181 /*! @brief UART transfer callback function. */
00182 typedef void (*uart_transfer_callback_t)(UART_Type *base, uart_handle_t *handle, status_t status, void *userData );
00183 
00184 /*! @brief UART handle structure. */
00185 struct _uart_handle
00186 {
00187     uint8_t *volatile txData ;   /*!< Address of remaining data to send. */
00188     volatile size_t txDataSize ; /*!< Size of the remaining data to send. */
00189     size_t txDataSizeAll ;       /*!< Size of the data to send out. */
00190     uint8_t *volatile rxData ;   /*!< Address of remaining data to receive. */
00191     volatile size_t rxDataSize ; /*!< Size of the remaining data to receive. */
00192     size_t rxDataSizeAll ;       /*!< Size of the data to receive. */
00193 
00194     uint8_t *rxRingBuffer ;              /*!< Start address of the receiver ring buffer. */
00195     size_t rxRingBufferSize ;            /*!< Size of the ring buffer. */
00196     volatile uint16_t rxRingBufferHead ; /*!< Index for the driver to store received data into ring buffer. */
00197     volatile uint16_t rxRingBufferTail ; /*!< Index for the user to get data from the ring buffer. */
00198 
00199     uart_transfer_callback_t callback ; /*!< Callback function. */
00200     void *userData ;                    /*!< UART callback function parameter.*/
00201 
00202     volatile uint8_t txState ; /*!< TX transfer state. */
00203     volatile uint8_t rxState ; /*!< RX transfer state */
00204 };
00205 
00206 /*******************************************************************************
00207  * API
00208  ******************************************************************************/
00209 
00210 #if defined(__cplusplus)
00211 extern "C" {
00212 #endif /* _cplusplus */
00213 
00214 /*!
00215  * @brief Get the UART instance from peripheral base address.
00216  *
00217  * @param base UART peripheral base address.
00218  * @return UART instance.
00219  */
00220 uint32_t UART_GetInstance(UART_Type *base);
00221 
00222 /*!
00223  * @name Initialization and deinitialization
00224  * @{
00225  */
00226 
00227 /*!
00228  * @brief Initializes a UART instance with a user configuration structure and peripheral clock.
00229  *
00230  * This function configures the UART module with the user-defined settings. The user can configure the configuration
00231  * structure and also get the default configuration by using the UART_GetDefaultConfig() function.
00232  * The example below shows how to use this API to configure UART.
00233  * @code
00234  *  uart_config_t uartConfig;
00235  *  uartConfig.baudRate_Bps = 115200U;
00236  *  uartConfig.parityMode = kUART_ParityDisabled;
00237  *  uartConfig.stopBitCount = kUART_OneStopBit;
00238  *  uartConfig.txFifoWatermark = 0;
00239  *  uartConfig.rxFifoWatermark = 1;
00240  *  UART_Init(UART1, &uartConfig, 20000000U);
00241  * @endcode
00242  *
00243  * @param base UART peripheral base address.
00244  * @param config Pointer to the user-defined configuration structure.
00245  * @param srcClock_Hz UART clock source frequency in HZ.
00246  * @retval kStatus_UART_BaudrateNotSupport Baudrate is not support in current clock source.
00247  * @retval kStatus_Success Status UART initialize succeed
00248  */
00249 status_t UART_Init(UART_Type *base, const uart_config_t *config, uint32_t srcClock_Hz);
00250 
00251 /*!
00252  * @brief Deinitializes a UART instance.
00253  *
00254  * This function waits for TX complete, disables TX and RX, and disables the UART clock.
00255  *
00256  * @param base UART peripheral base address.
00257  */
00258 void UART_Deinit(UART_Type *base);
00259 
00260 /*!
00261  * @brief Gets the default configuration structure.
00262  *
00263  * This function initializes the UART configuration structure to a default value. The default
00264  * values are as follows.
00265  *   uartConfig->baudRate_Bps = 115200U;
00266  *   uartConfig->bitCountPerChar = kUART_8BitsPerChar;
00267  *   uartConfig->parityMode = kUART_ParityDisabled;
00268  *   uartConfig->stopBitCount = kUART_OneStopBit;
00269  *   uartConfig->txFifoWatermark = 0;
00270  *   uartConfig->rxFifoWatermark = 1;
00271  *   uartConfig->idleType = kUART_IdleTypeStartBit;
00272  *   uartConfig->enableTx = false;
00273  *   uartConfig->enableRx = false;
00274  *
00275  * @param config Pointer to configuration structure.
00276  */
00277 void UART_GetDefaultConfig(uart_config_t *config);
00278 
00279 /*!
00280  * @brief Sets the UART instance baud rate.
00281  *
00282  * This function configures the UART module baud rate. This function is used to update
00283  * the UART module baud rate after the UART module is initialized by the UART_Init.
00284  * @code
00285  *  UART_SetBaudRate(UART1, 115200U, 20000000U);
00286  * @endcode
00287  *
00288  * @param base UART peripheral base address.
00289  * @param baudRate_Bps UART baudrate to be set.
00290  * @param srcClock_Hz UART clock source freqency in Hz.
00291  * @retval kStatus_UART_BaudrateNotSupport Baudrate is not support in the current clock source.
00292  * @retval kStatus_Success Set baudrate succeeded.
00293  */
00294 status_t UART_SetBaudRate(UART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
00295 
00296 /* @} */
00297 
00298 /*!
00299  * @name Status
00300  * @{
00301  */
00302 
00303 /*!
00304  * @brief Gets UART status flags.
00305  *
00306  * This function gets all UART status flags. The flags are returned as the logical
00307  * OR value of the enumerators @ref _uart_flags. To check a specific status,
00308  * compare the return value with enumerators in @ref _uart_flags.
00309  * For example, to check whether the TX is empty, do the following.
00310  * @code
00311  *     if (kUART_TxDataRegEmptyFlag & UART_GetStatusFlags(UART1))
00312  *     {
00313  *         ...
00314  *     }
00315  * @endcode
00316  *
00317  * @param base UART peripheral base address.
00318  * @return UART status flags which are ORed by the enumerators in the _uart_flags.
00319  */
00320 uint32_t UART_GetStatusFlags(UART_Type *base);
00321 
00322 /*!
00323  * @brief Clears status flags with the provided mask.
00324  *
00325  * This function clears UART status flags with a provided mask. An automatically cleared flag
00326  * can't be cleared by this function.
00327  * These flags can only be cleared or set by hardware.
00328  *    kUART_TxDataRegEmptyFlag, kUART_TransmissionCompleteFlag, kUART_RxDataRegFullFlag,
00329  *    kUART_RxActiveFlag, kUART_NoiseErrorInRxDataRegFlag, kUART_ParityErrorInRxDataRegFlag,
00330  *    kUART_TxFifoEmptyFlag,kUART_RxFifoEmptyFlag
00331  * Note that this API should be called when the Tx/Rx is idle. Otherwise it has no effect.
00332  *
00333  * @param base UART peripheral base address.
00334  * @param mask The status flags to be cleared; it is logical OR value of @ref _uart_flags.
00335  * @retval kStatus_UART_FlagCannotClearManually The flag can't be cleared by this function but
00336  *         it is cleared automatically by hardware.
00337  * @retval kStatus_Success Status in the mask is cleared.
00338  */
00339 status_t UART_ClearStatusFlags(UART_Type *base, uint32_t mask);
00340 
00341 /* @} */
00342 
00343 /*!
00344  * @name Interrupts
00345  * @{
00346  */
00347 
00348 /*!
00349  * @brief Enables UART interrupts according to the provided mask.
00350  *
00351  * This function enables the UART interrupts according to the provided mask. The mask
00352  * is a logical OR of enumeration members. See @ref _uart_interrupt_enable.
00353  * For example, to enable TX empty interrupt and RX full interrupt, do the following.
00354  * @code
00355  *     UART_EnableInterrupts(UART1,kUART_TxDataRegEmptyInterruptEnable | kUART_RxDataRegFullInterruptEnable);
00356  * @endcode
00357  *
00358  * @param base UART peripheral base address.
00359  * @param mask The interrupts to enable. Logical OR of @ref _uart_interrupt_enable.
00360  */
00361 void UART_EnableInterrupts(UART_Type *base, uint32_t mask);
00362 
00363 /*!
00364  * @brief Disables the UART interrupts according to the provided mask.
00365  *
00366  * This function disables the UART interrupts according to the provided mask. The mask
00367  * is a logical OR of enumeration members. See @ref _uart_interrupt_enable.
00368  * For example, to disable TX empty interrupt and RX full interrupt do the following.
00369  * @code
00370  *     UART_DisableInterrupts(UART1,kUART_TxDataRegEmptyInterruptEnable | kUART_RxDataRegFullInterruptEnable);
00371  * @endcode
00372  *
00373  * @param base UART peripheral base address.
00374  * @param mask The interrupts to disable. Logical OR of @ref _uart_interrupt_enable.
00375  */
00376 void UART_DisableInterrupts(UART_Type *base, uint32_t mask);
00377 
00378 /*!
00379  * @brief Gets the enabled UART interrupts.
00380  *
00381  * This function gets the enabled UART interrupts. The enabled interrupts are returned
00382  * as the logical OR value of the enumerators @ref _uart_interrupt_enable. To check
00383  * a specific interrupts enable status, compare the return value with enumerators
00384  * in @ref _uart_interrupt_enable.
00385  * For example, to check whether TX empty interrupt is enabled, do the following.
00386  * @code
00387  *     uint32_t enabledInterrupts = UART_GetEnabledInterrupts(UART1);
00388  *
00389  *     if (kUART_TxDataRegEmptyInterruptEnable & enabledInterrupts)
00390  *     {
00391  *         ...
00392  *     }
00393  * @endcode
00394  *
00395  * @param base UART peripheral base address.
00396  * @return UART interrupt flags which are logical OR of the enumerators in @ref _uart_interrupt_enable.
00397  */
00398 uint32_t UART_GetEnabledInterrupts(UART_Type *base);
00399 
00400 /* @} */
00401 
00402 #if defined(FSL_FEATURE_UART_HAS_DMA_SELECT) && FSL_FEATURE_UART_HAS_DMA_SELECT
00403 /*!
00404  * @name DMA Control
00405  * @{
00406  */
00407 
00408 /*!
00409  * @brief Gets the UART data register address.
00410  *
00411  * This function returns the UART data register address, which is mainly used by DMA/eDMA.
00412  *
00413  * @param base UART peripheral base address.
00414  * @return UART data register addresses which are used both by the transmitter and the receiver.
00415  */
00416 static inline uint32_t UART_GetDataRegisterAddress(UART_Type *base)
00417 {
00418     return (uint32_t) & (base->D);
00419 }
00420 
00421 /*!
00422  * @brief Enables or disables the UART transmitter DMA request.
00423  *
00424  * This function enables or disables the transmit data register empty flag, S1[TDRE], to generate the DMA requests.
00425  *
00426  * @param base UART peripheral base address.
00427  * @param enable True to enable, false to disable.
00428  */
00429 static inline void UART_EnableTxDMA(UART_Type *base, bool enable)
00430 {
00431     if (enable)
00432     {
00433 #if (defined(FSL_FEATURE_UART_IS_SCI) && FSL_FEATURE_UART_IS_SCI)
00434         base->C4 |= UART_C4_TDMAS_MASK;
00435 #else
00436         base->C5 |= UART_C5_TDMAS_MASK;
00437 #endif
00438         base->C2 |= UART_C2_TIE_MASK;
00439     }
00440     else
00441     {
00442 #if (defined(FSL_FEATURE_UART_IS_SCI) && FSL_FEATURE_UART_IS_SCI)
00443         base->C4 &= ~UART_C4_TDMAS_MASK;
00444 #else
00445         base->C5 &= ~UART_C5_TDMAS_MASK;
00446 #endif
00447         base->C2 &= ~UART_C2_TIE_MASK;
00448     }
00449 }
00450 
00451 /*!
00452  * @brief Enables or disables the UART receiver DMA.
00453  *
00454  * This function enables or disables the receiver data register full flag, S1[RDRF], to generate DMA requests.
00455  *
00456  * @param base UART peripheral base address.
00457  * @param enable True to enable, false to disable.
00458  */
00459 static inline void UART_EnableRxDMA(UART_Type *base, bool enable)
00460 {
00461     if (enable)
00462     {
00463 #if (defined(FSL_FEATURE_UART_IS_SCI) && FSL_FEATURE_UART_IS_SCI)
00464         base->C4 |= UART_C4_RDMAS_MASK;
00465 #else
00466         base->C5 |= UART_C5_RDMAS_MASK;
00467 #endif
00468         base->C2 |= UART_C2_RIE_MASK;
00469     }
00470     else
00471     {
00472 #if (defined(FSL_FEATURE_UART_IS_SCI) && FSL_FEATURE_UART_IS_SCI)
00473         base->C4 &= ~UART_C4_RDMAS_MASK;
00474 #else
00475         base->C5 &= ~UART_C5_RDMAS_MASK;
00476 #endif
00477         base->C2 &= ~UART_C2_RIE_MASK;
00478     }
00479 }
00480 
00481 /* @} */
00482 #endif /* FSL_FEATURE_UART_HAS_DMA_SELECT */
00483 
00484 /*!
00485  * @name Bus Operations
00486  * @{
00487  */
00488 
00489 /*!
00490  * @brief Enables or disables the UART transmitter.
00491  *
00492  * This function enables or disables the UART transmitter.
00493  *
00494  * @param base UART peripheral base address.
00495  * @param enable True to enable, false to disable.
00496  */
00497 static inline void UART_EnableTx(UART_Type *base, bool enable)
00498 {
00499     if (enable)
00500     {
00501         base->C2 |= UART_C2_TE_MASK;
00502     }
00503     else
00504     {
00505         base->C2 &= ~UART_C2_TE_MASK;
00506     }
00507 }
00508 
00509 /*!
00510  * @brief Enables or disables the UART receiver.
00511  *
00512  * This function enables or disables the UART receiver.
00513  *
00514  * @param base UART peripheral base address.
00515  * @param enable True to enable, false to disable.
00516  */
00517 static inline void UART_EnableRx(UART_Type *base, bool enable)
00518 {
00519     if (enable)
00520     {
00521         base->C2 |= UART_C2_RE_MASK;
00522     }
00523     else
00524     {
00525         base->C2 &= ~UART_C2_RE_MASK;
00526     }
00527 }
00528 
00529 /*!
00530  * @brief Writes to the TX register.
00531  *
00532  * This function writes data to the TX register directly. The upper layer must ensure
00533  * that the TX register is empty or TX FIFO has empty room before calling this function.
00534  *
00535  * @param base UART peripheral base address.
00536  * @param data The byte to write.
00537  */
00538 static inline void UART_WriteByte(UART_Type *base, uint8_t data)
00539 {
00540     base->D = data;
00541 }
00542 
00543 /*!
00544  * @brief Reads the RX register directly.
00545  *
00546  * This function reads data from the RX register directly. The upper layer must
00547  * ensure that the RX register is full or that the TX FIFO has data before calling this function.
00548  *
00549  * @param base UART peripheral base address.
00550  * @return The byte read from UART data register.
00551  */
00552 static inline uint8_t UART_ReadByte(UART_Type *base)
00553 {
00554     return base->D;
00555 }
00556 
00557 /*!
00558  * @brief Writes to the TX register using a blocking method.
00559  *
00560  * This function polls the TX register, waits for the TX register to be empty or for the TX FIFO
00561  * to have room and writes data to the TX buffer.
00562  *
00563  * @note This function does not check whether all data is sent out to the bus.
00564  * Before disabling the TX, check kUART_TransmissionCompleteFlag to ensure that the TX is
00565  * finished.
00566  *
00567  * @param base UART peripheral base address.
00568  * @param data Start address of the data to write.
00569  * @param length Size of the data to write.
00570  */
00571 void UART_WriteBlocking(UART_Type *base, const uint8_t *data, size_t length);
00572 
00573 /*!
00574  * @brief Read RX data register using a blocking method.
00575  *
00576  * This function polls the RX register, waits for the RX register to be full or for RX FIFO to
00577  * have data, and reads data from the TX register.
00578  *
00579  * @param base UART peripheral base address.
00580  * @param data Start address of the buffer to store the received data.
00581  * @param length Size of the buffer.
00582  * @retval kStatus_UART_RxHardwareOverrun Receiver overrun occurred while receiving data.
00583  * @retval kStatus_UART_NoiseError A noise error occurred while receiving data.
00584  * @retval kStatus_UART_FramingError A framing error occurred while receiving data.
00585  * @retval kStatus_UART_ParityError A parity error occurred while receiving data.
00586  * @retval kStatus_Success Successfully received all data.
00587  */
00588 status_t UART_ReadBlocking(UART_Type *base, uint8_t *data, size_t length);
00589 
00590 /* @} */
00591 
00592 /*!
00593  * @name Transactional
00594  * @{
00595  */
00596 
00597 /*!
00598  * @brief Initializes the UART handle.
00599  *
00600  * This function initializes the UART handle which can be used for other UART
00601  * transactional APIs. Usually, for a specified UART instance,
00602  * call this API once to get the initialized handle.
00603  *
00604  * @param base UART peripheral base address.
00605  * @param handle UART handle pointer.
00606  * @param callback The callback function.
00607  * @param userData The parameter of the callback function.
00608  */
00609 void UART_TransferCreateHandle(UART_Type *base,
00610                                uart_handle_t *handle,
00611                                uart_transfer_callback_t callback,
00612                                void *userData);
00613 
00614 /*!
00615  * @brief Sets up the RX ring buffer.
00616  *
00617  * This function sets up the RX ring buffer to a specific UART handle.
00618  *
00619  * When the RX ring buffer is used, data received are stored into the ring buffer even when the
00620  * user doesn't call the UART_TransferReceiveNonBlocking() API. If data is already received
00621  * in the ring buffer, the user can get the received data from the ring buffer directly.
00622  *
00623  * @note When using the RX ring buffer, one byte is reserved for internal use. In other
00624  * words, if @p ringBufferSize is 32, only 31 bytes are used for saving data.
00625  *
00626  * @param base UART peripheral base address.
00627  * @param handle UART handle pointer.
00628  * @param ringBuffer Start address of the ring buffer for background receiving. Pass NULL to disable the ring buffer.
00629  * @param ringBufferSize Size of the ring buffer.
00630  */
00631 void UART_TransferStartRingBuffer(UART_Type *base, uart_handle_t *handle, uint8_t *ringBuffer, size_t ringBufferSize);
00632 
00633 /*!
00634  * @brief Aborts the background transfer and uninstalls the ring buffer.
00635  *
00636  * This function aborts the background transfer and uninstalls the ring buffer.
00637  *
00638  * @param base UART peripheral base address.
00639  * @param handle UART handle pointer.
00640  */
00641 void UART_TransferStopRingBuffer(UART_Type *base, uart_handle_t *handle);
00642 
00643 /*!
00644  * @brief Get the length of received data in RX ring buffer.
00645  *
00646  * @param handle UART handle pointer.
00647  * @return Length of received data in RX ring buffer.
00648  */
00649 size_t UART_TransferGetRxRingBufferLength(uart_handle_t *handle);
00650 
00651 /*!
00652  * @brief Transmits a buffer of data using the interrupt method.
00653  *
00654  * This function sends data using an interrupt method. This is a non-blocking function, which
00655  * returns directly without waiting for all data to be written to the TX register. When
00656  * all data is written to the TX register in the ISR, the UART driver calls the callback
00657  * function and passes the @ref kStatus_UART_TxIdle as status parameter.
00658  *
00659  * @note The kStatus_UART_TxIdle is passed to the upper layer when all data is written
00660  * to the TX register. However, it does not ensure that all data is sent out. Before disabling the TX,
00661  * check the kUART_TransmissionCompleteFlag to ensure that the TX is finished.
00662  *
00663  * @param base UART peripheral base address.
00664  * @param handle UART handle pointer.
00665  * @param xfer UART transfer structure. See  #uart_transfer_t.
00666  * @retval kStatus_Success Successfully start the data transmission.
00667  * @retval kStatus_UART_TxBusy Previous transmission still not finished; data not all written to TX register yet.
00668  * @retval kStatus_InvalidArgument Invalid argument.
00669  */
00670 status_t UART_TransferSendNonBlocking(UART_Type *base, uart_handle_t *handle, uart_transfer_t *xfer);
00671 
00672 /*!
00673  * @brief Aborts the interrupt-driven data transmit.
00674  *
00675  * This function aborts the interrupt-driven data sending. The user can get the remainBytes to find out
00676  * how many bytes are not sent out.
00677  *
00678  * @param base UART peripheral base address.
00679  * @param handle UART handle pointer.
00680  */
00681 void UART_TransferAbortSend(UART_Type *base, uart_handle_t *handle);
00682 
00683 /*!
00684  * @brief Gets the number of bytes written to the UART TX register.
00685  *
00686  * This function gets the number of bytes written to the UART TX
00687  * register by using the interrupt method.
00688  *
00689  * @param base UART peripheral base address.
00690  * @param handle UART handle pointer.
00691  * @param count Send bytes count.
00692  * @retval kStatus_NoTransferInProgress No send in progress.
00693  * @retval kStatus_InvalidArgument The parameter is invalid.
00694  * @retval kStatus_Success Get successfully through the parameter \p count;
00695  */
00696 status_t UART_TransferGetSendCount(UART_Type *base, uart_handle_t *handle, uint32_t *count);
00697 
00698 /*!
00699  * @brief Receives a buffer of data using an interrupt method.
00700  *
00701  * This function receives data using an interrupt method. This is a non-blocking function, which
00702  *  returns without waiting for all data to be received.
00703  * If the RX ring buffer is used and not empty, the data in the ring buffer is copied and
00704  * the parameter @p receivedBytes shows how many bytes are copied from the ring buffer.
00705  * After copying, if the data in the ring buffer is not enough to read, the receive
00706  * request is saved by the UART driver. When the new data arrives, the receive request
00707  * is serviced first. When all data is received, the UART driver notifies the upper layer
00708  * through a callback function and passes the status parameter @ref kStatus_UART_RxIdle.
00709  * For example, the upper layer needs 10 bytes but there are only 5 bytes in the ring buffer.
00710  * The 5 bytes are copied to the xfer->data and this function returns with the
00711  * parameter @p receivedBytes set to 5. For the left 5 bytes, newly arrived data is
00712  * saved from the xfer->data[5]. When 5 bytes are received, the UART driver notifies the upper layer.
00713  * If the RX ring buffer is not enabled, this function enables the RX and RX interrupt
00714  * to receive data to the xfer->data. When all data is received, the upper layer is notified.
00715  *
00716  * @param base UART peripheral base address.
00717  * @param handle UART handle pointer.
00718  * @param xfer UART transfer structure, see #uart_transfer_t.
00719  * @param receivedBytes Bytes received from the ring buffer directly.
00720  * @retval kStatus_Success Successfully queue the transfer into transmit queue.
00721  * @retval kStatus_UART_RxBusy Previous receive request is not finished.
00722  * @retval kStatus_InvalidArgument Invalid argument.
00723  */
00724 status_t UART_TransferReceiveNonBlocking(UART_Type *base,
00725                                          uart_handle_t *handle,
00726                                          uart_transfer_t *xfer,
00727                                          size_t *receivedBytes);
00728 
00729 /*!
00730  * @brief Aborts the interrupt-driven data receiving.
00731  *
00732  * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to know
00733  * how many bytes are not received yet.
00734  *
00735  * @param base UART peripheral base address.
00736  * @param handle UART handle pointer.
00737  */
00738 void UART_TransferAbortReceive(UART_Type *base, uart_handle_t *handle);
00739 
00740 /*!
00741  * @brief Gets the number of bytes that have been received.
00742  *
00743  * This function gets the number of bytes that have been received.
00744  *
00745  * @param base UART peripheral base address.
00746  * @param handle UART handle pointer.
00747  * @param count Receive bytes count.
00748  * @retval kStatus_NoTransferInProgress No receive in progress.
00749  * @retval kStatus_InvalidArgument Parameter is invalid.
00750  * @retval kStatus_Success Get successfully through the parameter \p count;
00751  */
00752 status_t UART_TransferGetReceiveCount(UART_Type *base, uart_handle_t *handle, uint32_t *count);
00753 
00754 /*!
00755  * @brief UART IRQ handle function.
00756  *
00757  * This function handles the UART transmit and receive IRQ request.
00758  *
00759  * @param base UART peripheral base address.
00760  * @param handle UART handle pointer.
00761  */
00762 void UART_TransferHandleIRQ(UART_Type *base, uart_handle_t *handle);
00763 
00764 /*!
00765  * @brief UART Error IRQ handle function.
00766  *
00767  * This function handles the UART error IRQ request.
00768  *
00769  * @param base UART peripheral base address.
00770  * @param handle UART handle pointer.
00771  */
00772 void UART_TransferHandleErrorIRQ(UART_Type *base, uart_handle_t *handle);
00773 
00774 /* @} */
00775 
00776 #if defined(__cplusplus)
00777 }
00778 #endif
00779 
00780 /*! @}*/
00781 
00782 #endif /* _FSL_UART_H_ */