The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Feb 20 20:53:29 2019 +0000
Revision:
172:65be27845400
Parent:
171:3a7713b1edbc
mbed library release version 165

Who changed what in which revision?

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