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