2 * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
3 * Copyright 2016-2017 NXP
6 * SPDX-License-Identifier: BSD-3-Clause
11 #include "fsl_common.h"
14 * @addtogroup lpuart_driver
18 /*******************************************************************************
20 ******************************************************************************/
22 /*! @name Driver version */
24 /*! @brief LPUART driver version 2.2.3. */
25 #define FSL_LPUART_DRIVER_VERSION (MAKE_VERSION(2, 2, 4))
28 /*! @brief Error codes for the LPUART driver. */
31 kStatus_LPUART_TxBusy = MAKE_STATUS(kStatusGroup_LPUART, 0), /*!< TX busy */
32 kStatus_LPUART_RxBusy = MAKE_STATUS(kStatusGroup_LPUART, 1), /*!< RX busy */
33 kStatus_LPUART_TxIdle = MAKE_STATUS(kStatusGroup_LPUART, 2), /*!< LPUART transmitter is idle. */
34 kStatus_LPUART_RxIdle = MAKE_STATUS(kStatusGroup_LPUART, 3), /*!< LPUART receiver is idle. */
35 kStatus_LPUART_TxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_LPUART, 4), /*!< TX FIFO watermark too large */
36 kStatus_LPUART_RxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_LPUART, 5), /*!< RX FIFO watermark too large */
37 kStatus_LPUART_FlagCannotClearManually = MAKE_STATUS(kStatusGroup_LPUART, 6), /*!< Some flag can't manually clear */
38 kStatus_LPUART_Error = MAKE_STATUS(kStatusGroup_LPUART, 7), /*!< Error happens on LPUART. */
39 kStatus_LPUART_RxRingBufferOverrun =
40 MAKE_STATUS(kStatusGroup_LPUART, 8), /*!< LPUART RX software ring buffer overrun. */
41 kStatus_LPUART_RxHardwareOverrun = MAKE_STATUS(kStatusGroup_LPUART, 9), /*!< LPUART RX receiver overrun. */
42 kStatus_LPUART_NoiseError = MAKE_STATUS(kStatusGroup_LPUART, 10), /*!< LPUART noise error. */
43 kStatus_LPUART_FramingError = MAKE_STATUS(kStatusGroup_LPUART, 11), /*!< LPUART framing error. */
44 kStatus_LPUART_ParityError = MAKE_STATUS(kStatusGroup_LPUART, 12), /*!< LPUART parity error. */
45 kStatus_LPUART_BaudrateNotSupport =
46 MAKE_STATUS(kStatusGroup_LPUART, 13), /*!< Baudrate is not support in current clock source */
47 kStatus_LPUART_IdleLineDetected = MAKE_STATUS(kStatusGroup_LPUART, 14), /*!< IDLE flag. */
50 /*! @brief LPUART parity mode. */
51 typedef enum _lpuart_parity_mode
53 kLPUART_ParityDisabled = 0x0U, /*!< Parity disabled */
54 kLPUART_ParityEven = 0x2U, /*!< Parity enabled, type even, bit setting: PE|PT = 10 */
55 kLPUART_ParityOdd = 0x3U, /*!< Parity enabled, type odd, bit setting: PE|PT = 11 */
56 } lpuart_parity_mode_t;
58 /*! @brief LPUART data bits count. */
59 typedef enum _lpuart_data_bits
61 kLPUART_EightDataBits = 0x0U, /*!< Eight data bit */
62 #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
63 kLPUART_SevenDataBits = 0x1U, /*!< Seven data bit */
67 /*! @brief LPUART stop bit count. */
68 typedef enum _lpuart_stop_bit_count
70 kLPUART_OneStopBit = 0U, /*!< One stop bit */
71 kLPUART_TwoStopBit = 1U, /*!< Two stop bits */
72 } lpuart_stop_bit_count_t;
74 #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
75 /*! @brief LPUART transmit CTS source. */
76 typedef enum _lpuart_transmit_cts_source
78 kLPUART_CtsSourcePin = 0U, /*!< CTS resource is the LPUART_CTS pin. */
79 kLPUART_CtsSourceMatchResult = 1U, /*!< CTS resource is the match result. */
80 } lpuart_transmit_cts_source_t;
82 /*! @brief LPUART transmit CTS configure. */
83 typedef enum _lpuart_transmit_cts_config
85 kLPUART_CtsSampleAtStart = 0U, /*!< CTS input is sampled at the start of each character. */
86 kLPUART_CtsSampleAtIdle = 1U, /*!< CTS input is sampled when the transmitter is idle */
87 } lpuart_transmit_cts_config_t;
90 /*! @brief LPUART idle flag type defines when the receiver starts counting. */
91 typedef enum _lpuart_idle_type_select
93 kLPUART_IdleTypeStartBit = 0U, /*!< Start counting after a valid start bit. */
94 kLPUART_IdleTypeStopBit = 1U, /*!< Start conuting after a stop bit. */
95 } lpuart_idle_type_select_t;
97 /*! @brief LPUART idle detected configuration.
98 * This structure defines the number of idle characters that must be received before
99 * the IDLE flag is set.
101 typedef enum _lpuart_idle_config
103 kLPUART_IdleCharacter1 = 0U, /*!< the number of idle characters. */
104 kLPUART_IdleCharacter2 = 1U, /*!< the number of idle characters. */
105 kLPUART_IdleCharacter4 = 2U, /*!< the number of idle characters. */
106 kLPUART_IdleCharacter8 = 3U, /*!< the number of idle characters. */
107 kLPUART_IdleCharacter16 = 4U, /*!< the number of idle characters. */
108 kLPUART_IdleCharacter32 = 5U, /*!< the number of idle characters. */
109 kLPUART_IdleCharacter64 = 6U, /*!< the number of idle characters. */
110 kLPUART_IdleCharacter128 = 7U, /*!< the number of idle characters. */
111 } lpuart_idle_config_t;
114 * @brief LPUART interrupt configuration structure, default settings all disabled.
116 * This structure contains the settings for all LPUART interrupt configurations.
118 enum _lpuart_interrupt_enable
120 #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
121 kLPUART_LinBreakInterruptEnable = (LPUART_BAUD_LBKDIE_MASK >> 8), /*!< LIN break detect. */
123 kLPUART_RxActiveEdgeInterruptEnable = (LPUART_BAUD_RXEDGIE_MASK >> 8), /*!< Receive Active Edge. */
124 kLPUART_TxDataRegEmptyInterruptEnable = (LPUART_CTRL_TIE_MASK), /*!< Transmit data register empty. */
125 kLPUART_TransmissionCompleteInterruptEnable = (LPUART_CTRL_TCIE_MASK), /*!< Transmission complete. */
126 kLPUART_RxDataRegFullInterruptEnable = (LPUART_CTRL_RIE_MASK), /*!< Receiver data register full. */
127 kLPUART_IdleLineInterruptEnable = (LPUART_CTRL_ILIE_MASK), /*!< Idle line. */
128 kLPUART_RxOverrunInterruptEnable = (LPUART_CTRL_ORIE_MASK), /*!< Receiver Overrun. */
129 kLPUART_NoiseErrorInterruptEnable = (LPUART_CTRL_NEIE_MASK), /*!< Noise error flag. */
130 kLPUART_FramingErrorInterruptEnable = (LPUART_CTRL_FEIE_MASK), /*!< Framing error flag. */
131 kLPUART_ParityErrorInterruptEnable = (LPUART_CTRL_PEIE_MASK), /*!< Parity error flag. */
132 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
133 kLPUART_TxFifoOverflowInterruptEnable = (LPUART_FIFO_TXOFE_MASK >> 8), /*!< Transmit FIFO Overflow. */
134 kLPUART_RxFifoUnderflowInterruptEnable = (LPUART_FIFO_RXUFE_MASK >> 8), /*!< Receive FIFO Underflow. */
139 * @brief LPUART status flags.
141 * This provides constants for the LPUART status flags for use in the LPUART functions.
145 kLPUART_TxDataRegEmptyFlag =
146 (LPUART_STAT_TDRE_MASK), /*!< Transmit data register empty flag, sets when transmit buffer is empty */
147 kLPUART_TransmissionCompleteFlag =
148 (LPUART_STAT_TC_MASK), /*!< Transmission complete flag, sets when transmission activity complete */
149 kLPUART_RxDataRegFullFlag =
150 (LPUART_STAT_RDRF_MASK), /*!< Receive data register full flag, sets when the receive data buffer is full */
151 kLPUART_IdleLineFlag = (LPUART_STAT_IDLE_MASK), /*!< Idle line detect flag, sets when idle line detected */
152 kLPUART_RxOverrunFlag = (LPUART_STAT_OR_MASK), /*!< Receive Overrun, sets when new data is received before data is
153 read from receive register */
154 kLPUART_NoiseErrorFlag = (LPUART_STAT_NF_MASK), /*!< Receive takes 3 samples of each received bit. If any of these
155 samples differ, noise flag sets */
156 kLPUART_FramingErrorFlag =
157 (LPUART_STAT_FE_MASK), /*!< Frame error flag, sets if logic 0 was detected where stop bit expected */
158 kLPUART_ParityErrorFlag = (LPUART_STAT_PF_MASK), /*!< If parity enabled, sets upon parity error detection */
159 #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
160 kLPUART_LinBreakFlag = (LPUART_STAT_LBKDIF_MASK), /*!< LIN break detect interrupt flag, sets when LIN break char
161 detected and LIN circuit enabled */
163 kLPUART_RxActiveEdgeFlag =
164 (LPUART_STAT_RXEDGIF_MASK), /*!< Receive pin active edge interrupt flag, sets when active edge detected */
165 kLPUART_RxActiveFlag =
166 (LPUART_STAT_RAF_MASK), /*!< Receiver Active Flag (RAF), sets at beginning of valid start bit */
167 #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
168 kLPUART_DataMatch1Flag = LPUART_STAT_MA1F_MASK, /*!< The next character to be read from LPUART_DATA matches MA1*/
169 kLPUART_DataMatch2Flag = LPUART_STAT_MA2F_MASK, /*!< The next character to be read from LPUART_DATA matches MA2*/
171 #if defined(FSL_FEATURE_LPUART_HAS_EXTENDED_DATA_REGISTER_FLAGS) && FSL_FEATURE_LPUART_HAS_EXTENDED_DATA_REGISTER_FLAGS
172 kLPUART_NoiseErrorInRxDataRegFlag =
173 (LPUART_DATA_NOISY_MASK >> 10), /*!< NOISY bit, sets if noise detected in current data word */
174 kLPUART_ParityErrorInRxDataRegFlag =
175 (LPUART_DATA_PARITYE_MASK >> 10), /*!< PARITYE bit, sets if noise detected in current data word */
177 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
178 kLPUART_TxFifoEmptyFlag = (LPUART_FIFO_TXEMPT_MASK >> 16), /*!< TXEMPT bit, sets if transmit buffer is empty */
179 kLPUART_RxFifoEmptyFlag = (LPUART_FIFO_RXEMPT_MASK >> 16), /*!< RXEMPT bit, sets if receive buffer is empty */
180 kLPUART_TxFifoOverflowFlag =
181 (LPUART_FIFO_TXOF_MASK >> 16), /*!< TXOF bit, sets if transmit buffer overflow occurred */
182 kLPUART_RxFifoUnderflowFlag =
183 (LPUART_FIFO_RXUF_MASK >> 16), /*!< RXUF bit, sets if receive buffer underflow occurred */
187 /*! @brief LPUART configuration structure. */
188 typedef struct _lpuart_config
190 uint32_t baudRate_Bps; /*!< LPUART baud rate */
191 lpuart_parity_mode_t parityMode; /*!< Parity mode, disabled (default), even, odd */
192 lpuart_data_bits_t dataBitsCount; /*!< Data bits count, eight (default), seven */
193 bool isMsb; /*!< Data bits order, LSB (default), MSB */
194 #if defined(FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT
195 lpuart_stop_bit_count_t stopBitCount; /*!< Number of stop bits, 1 stop bit (default) or 2 stop bits */
197 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
198 uint8_t txFifoWatermark; /*!< TX FIFO watermark */
199 uint8_t rxFifoWatermark; /*!< RX FIFO watermark */
201 #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
202 bool enableRxRTS; /*!< RX RTS enable */
203 bool enableTxCTS; /*!< TX CTS enable */
204 lpuart_transmit_cts_source_t txCtsSource; /*!< TX CTS source */
205 lpuart_transmit_cts_config_t txCtsConfig; /*!< TX CTS configure */
207 lpuart_idle_type_select_t rxIdleType; /*!< RX IDLE type. */
208 lpuart_idle_config_t rxIdleConfig; /*!< RX IDLE configuration. */
209 bool enableTx; /*!< Enable TX */
210 bool enableRx; /*!< Enable RX */
213 /*! @brief LPUART transfer structure. */
214 typedef struct _lpuart_transfer
216 uint8_t *data; /*!< The buffer of data to be transfer.*/
217 size_t dataSize; /*!< The byte count to be transfer. */
220 /* Forward declaration of the handle typedef. */
221 typedef struct _lpuart_handle lpuart_handle_t;
223 /*! @brief LPUART transfer callback function. */
224 typedef void (*lpuart_transfer_callback_t)(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData);
226 /*! @brief LPUART handle structure. */
227 struct _lpuart_handle
229 uint8_t *volatile txData; /*!< Address of remaining data to send. */
230 volatile size_t txDataSize; /*!< Size of the remaining data to send. */
231 size_t txDataSizeAll; /*!< Size of the data to send out. */
232 uint8_t *volatile rxData; /*!< Address of remaining data to receive. */
233 volatile size_t rxDataSize; /*!< Size of the remaining data to receive. */
234 size_t rxDataSizeAll; /*!< Size of the data to receive. */
236 uint8_t *rxRingBuffer; /*!< Start address of the receiver ring buffer. */
237 size_t rxRingBufferSize; /*!< Size of the ring buffer. */
238 volatile uint16_t rxRingBufferHead; /*!< Index for the driver to store received data into ring buffer. */
239 volatile uint16_t rxRingBufferTail; /*!< Index for the user to get data from the ring buffer. */
241 lpuart_transfer_callback_t callback; /*!< Callback function. */
242 void *userData; /*!< LPUART callback function parameter.*/
244 volatile uint8_t txState; /*!< TX transfer state. */
245 volatile uint8_t rxState; /*!< RX transfer state. */
247 #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
248 bool isSevenDataBits; /*!< Seven data bits flag. */
252 /*******************************************************************************
254 ******************************************************************************/
256 #if defined(__cplusplus)
258 #endif /* _cplusplus */
260 #if defined(FSL_FEATURE_LPUART_HAS_GLOBAL) && FSL_FEATURE_LPUART_HAS_GLOBAL
263 * @name Software Reset
268 * @brief Resets the LPUART using software.
270 * This function resets all internal logic and registers except the Global Register.
271 * Remains set until cleared by software.
273 * @param base LPUART peripheral base address.
275 static inline void LPUART_SoftwareReset(LPUART_Type *base)
277 base->GLOBAL |= LPUART_GLOBAL_RST_MASK;
278 base->GLOBAL &= ~LPUART_GLOBAL_RST_MASK;
281 #endif /*FSL_FEATURE_LPUART_HAS_GLOBAL*/
284 * @name Initialization and deinitialization
289 * @brief Initializes an LPUART instance with the user configuration structure and the peripheral clock.
291 * This function configures the LPUART module with user-defined settings. Call the LPUART_GetDefaultConfig() function
292 * to configure the configuration structure and get the default configuration.
293 * The example below shows how to use this API to configure the LPUART.
295 * lpuart_config_t lpuartConfig;
296 * lpuartConfig.baudRate_Bps = 115200U;
297 * lpuartConfig.parityMode = kLPUART_ParityDisabled;
298 * lpuartConfig.dataBitsCount = kLPUART_EightDataBits;
299 * lpuartConfig.isMsb = false;
300 * lpuartConfig.stopBitCount = kLPUART_OneStopBit;
301 * lpuartConfig.txFifoWatermark = 0;
302 * lpuartConfig.rxFifoWatermark = 1;
303 * LPUART_Init(LPUART1, &lpuartConfig, 20000000U);
306 * @param base LPUART peripheral base address.
307 * @param config Pointer to a user-defined configuration structure.
308 * @param srcClock_Hz LPUART clock source frequency in HZ.
309 * @retval kStatus_LPUART_BaudrateNotSupport Baudrate is not support in current clock source.
310 * @retval kStatus_Success LPUART initialize succeed
312 status_t LPUART_Init(LPUART_Type *base, const lpuart_config_t *config, uint32_t srcClock_Hz);
315 * @brief Deinitializes a LPUART instance.
317 * This function waits for transmit to complete, disables TX and RX, and disables the LPUART clock.
319 * @param base LPUART peripheral base address.
321 void LPUART_Deinit(LPUART_Type *base);
324 * @brief Gets the default configuration structure.
326 * This function initializes the LPUART configuration structure to a default value. The default
328 * lpuartConfig->baudRate_Bps = 115200U;
329 * lpuartConfig->parityMode = kLPUART_ParityDisabled;
330 * lpuartConfig->dataBitsCount = kLPUART_EightDataBits;
331 * lpuartConfig->isMsb = false;
332 * lpuartConfig->stopBitCount = kLPUART_OneStopBit;
333 * lpuartConfig->txFifoWatermark = 0;
334 * lpuartConfig->rxFifoWatermark = 1;
335 * lpuartConfig->rxIdleType = kLPUART_IdleTypeStartBit;
336 * lpuartConfig->rxIdleConfig = kLPUART_IdleCharacter1;
337 * lpuartConfig->enableTx = false;
338 * lpuartConfig->enableRx = false;
340 * @param config Pointer to a configuration structure.
342 void LPUART_GetDefaultConfig(lpuart_config_t *config);
345 * @brief Sets the LPUART instance baudrate.
347 * This function configures the LPUART module baudrate. This function is used to update
348 * the LPUART module baudrate after the LPUART module is initialized by the LPUART_Init.
350 * LPUART_SetBaudRate(LPUART1, 115200U, 20000000U);
353 * @param base LPUART peripheral base address.
354 * @param baudRate_Bps LPUART baudrate to be set.
355 * @param srcClock_Hz LPUART clock source frequency in HZ.
356 * @retval kStatus_LPUART_BaudrateNotSupport Baudrate is not supported in the current clock source.
357 * @retval kStatus_Success Set baudrate succeeded.
359 status_t LPUART_SetBaudRate(LPUART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
369 * @brief Gets LPUART status flags.
371 * This function gets all LPUART status flags. The flags are returned as the logical
372 * OR value of the enumerators @ref _lpuart_flags. To check for a specific status,
373 * compare the return value with enumerators in the @ref _lpuart_flags.
374 * For example, to check whether the TX is empty:
376 * if (kLPUART_TxDataRegEmptyFlag & LPUART_GetStatusFlags(LPUART1))
382 * @param base LPUART peripheral base address.
383 * @return LPUART status flags which are ORed by the enumerators in the _lpuart_flags.
385 uint32_t LPUART_GetStatusFlags(LPUART_Type *base);
388 * @brief Clears status flags with a provided mask.
390 * This function clears LPUART status flags with a provided mask. Automatically cleared flags
391 * can't be cleared by this function.
392 * Flags that can only cleared or set by hardware are:
393 * kLPUART_TxDataRegEmptyFlag, kLPUART_TransmissionCompleteFlag, kLPUART_RxDataRegFullFlag,
394 * kLPUART_RxActiveFlag, kLPUART_NoiseErrorInRxDataRegFlag, kLPUART_ParityErrorInRxDataRegFlag,
395 * kLPUART_TxFifoEmptyFlag,kLPUART_RxFifoEmptyFlag
396 * Note: This API should be called when the Tx/Rx is idle, otherwise it takes no effects.
398 * @param base LPUART peripheral base address.
399 * @param mask the status flags to be cleared. The user can use the enumerators in the
400 * _lpuart_status_flag_t to do the OR operation and get the mask.
401 * @return 0 succeed, others failed.
402 * @retval kStatus_LPUART_FlagCannotClearManually The flag can't be cleared by this function but
403 * it is cleared automatically by hardware.
404 * @retval kStatus_Success Status in the mask are cleared.
406 status_t LPUART_ClearStatusFlags(LPUART_Type *base, uint32_t mask);
416 * @brief Enables LPUART interrupts according to a provided mask.
418 * This function enables the LPUART interrupts according to a provided mask. The mask
419 * is a logical OR of enumeration members. See the @ref _lpuart_interrupt_enable.
420 * This examples shows how to enable TX empty interrupt and RX full interrupt:
422 * LPUART_EnableInterrupts(LPUART1,kLPUART_TxDataRegEmptyInterruptEnable | kLPUART_RxDataRegFullInterruptEnable);
425 * @param base LPUART peripheral base address.
426 * @param mask The interrupts to enable. Logical OR of @ref _uart_interrupt_enable.
428 void LPUART_EnableInterrupts(LPUART_Type *base, uint32_t mask);
431 * @brief Disables LPUART interrupts according to a provided mask.
433 * This function disables the LPUART interrupts according to a provided mask. The mask
434 * is a logical OR of enumeration members. See @ref _lpuart_interrupt_enable.
435 * This example shows how to disable the TX empty interrupt and RX full interrupt:
437 * LPUART_DisableInterrupts(LPUART1,kLPUART_TxDataRegEmptyInterruptEnable | kLPUART_RxDataRegFullInterruptEnable);
440 * @param base LPUART peripheral base address.
441 * @param mask The interrupts to disable. Logical OR of @ref _lpuart_interrupt_enable.
443 void LPUART_DisableInterrupts(LPUART_Type *base, uint32_t mask);
446 * @brief Gets enabled LPUART interrupts.
448 * This function gets the enabled LPUART interrupts. The enabled interrupts are returned
449 * as the logical OR value of the enumerators @ref _lpuart_interrupt_enable. To check
450 * a specific interrupt enable status, compare the return value with enumerators
451 * in @ref _lpuart_interrupt_enable.
452 * For example, to check whether the TX empty interrupt is enabled:
454 * uint32_t enabledInterrupts = LPUART_GetEnabledInterrupts(LPUART1);
456 * if (kLPUART_TxDataRegEmptyInterruptEnable & enabledInterrupts)
462 * @param base LPUART peripheral base address.
463 * @return LPUART interrupt flags which are logical OR of the enumerators in @ref _lpuart_interrupt_enable.
465 uint32_t LPUART_GetEnabledInterrupts(LPUART_Type *base);
467 #if defined(FSL_FEATURE_LPUART_HAS_DMA_ENABLE) && FSL_FEATURE_LPUART_HAS_DMA_ENABLE
469 * @brief Gets the LPUART data register address.
471 * This function returns the LPUART data register address, which is mainly used by the DMA/eDMA.
473 * @param base LPUART peripheral base address.
474 * @return LPUART data register addresses which are used both by the transmitter and receiver.
476 static inline uint32_t LPUART_GetDataRegisterAddress(LPUART_Type *base)
478 return (uint32_t) & (base->DATA);
482 * @brief Enables or disables the LPUART transmitter DMA request.
484 * This function enables or disables the transmit data register empty flag, STAT[TDRE], to generate DMA requests.
486 * @param base LPUART peripheral base address.
487 * @param enable True to enable, false to disable.
489 static inline void LPUART_EnableTxDMA(LPUART_Type *base, bool enable)
493 base->BAUD |= LPUART_BAUD_TDMAE_MASK;
494 base->CTRL |= LPUART_CTRL_TIE_MASK;
498 base->BAUD &= ~LPUART_BAUD_TDMAE_MASK;
499 base->CTRL &= ~LPUART_CTRL_TIE_MASK;
504 * @brief Enables or disables the LPUART receiver DMA.
506 * This function enables or disables the receiver data register full flag, STAT[RDRF], to generate DMA requests.
508 * @param base LPUART peripheral base address.
509 * @param enable True to enable, false to disable.
511 static inline void LPUART_EnableRxDMA(LPUART_Type *base, bool enable)
515 base->BAUD |= LPUART_BAUD_RDMAE_MASK;
516 base->CTRL |= LPUART_CTRL_RIE_MASK;
520 base->BAUD &= ~LPUART_BAUD_RDMAE_MASK;
521 base->CTRL &= ~LPUART_CTRL_RIE_MASK;
526 #endif /* FSL_FEATURE_LPUART_HAS_DMA_ENABLE */
529 * @name Bus Operations
534 * @brief Enables or disables the LPUART transmitter.
536 * This function enables or disables the LPUART transmitter.
538 * @param base LPUART peripheral base address.
539 * @param enable True to enable, false to disable.
541 static inline void LPUART_EnableTx(LPUART_Type *base, bool enable)
545 base->CTRL |= LPUART_CTRL_TE_MASK;
549 base->CTRL &= ~LPUART_CTRL_TE_MASK;
554 * @brief Enables or disables the LPUART receiver.
556 * This function enables or disables the LPUART receiver.
558 * @param base LPUART peripheral base address.
559 * @param enable True to enable, false to disable.
561 static inline void LPUART_EnableRx(LPUART_Type *base, bool enable)
565 base->CTRL |= LPUART_CTRL_RE_MASK;
569 base->CTRL &= ~LPUART_CTRL_RE_MASK;
574 * @brief Writes to the transmitter register.
576 * This function writes data to the transmitter register directly. The upper layer must
577 * ensure that the TX register is empty or that the TX FIFO has room before calling this function.
579 * @param base LPUART peripheral base address.
580 * @param data Data write to the TX register.
582 static inline void LPUART_WriteByte(LPUART_Type *base, uint8_t data)
588 * @brief Reads the receiver register.
590 * This function reads data from the receiver register directly. The upper layer must
591 * ensure that the receiver register is full or that the RX FIFO has data before calling this function.
593 * @param base LPUART peripheral base address.
594 * @return Data read from data register.
596 static inline uint8_t LPUART_ReadByte(LPUART_Type *base)
598 #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
599 uint32_t ctrl = base->CTRL;
600 bool isSevenDataBits =
601 ((ctrl & LPUART_CTRL_M7_MASK) ||
602 ((!(ctrl & LPUART_CTRL_M7_MASK)) && (!(ctrl & LPUART_CTRL_M_MASK)) && (ctrl & LPUART_CTRL_PE_MASK)));
606 return (base->DATA & 0x7F);
618 * @brief Writes to the transmitter register using a blocking method.
620 * This function polls the transmitter register, waits for the register to be empty or for TX FIFO to have
621 * room, and writes data to the transmitter buffer.
623 * @note This function does not check whether all data has been sent out to the bus.
624 * Before disabling the transmitter, check the kLPUART_TransmissionCompleteFlag to ensure that the transmit is
627 * @param base LPUART peripheral base address.
628 * @param data Start address of the data to write.
629 * @param length Size of the data to write.
631 void LPUART_WriteBlocking(LPUART_Type *base, const uint8_t *data, size_t length);
634 * @brief Reads the receiver data register using a blocking method.
636 * This function polls the receiver register, waits for the receiver register full or receiver FIFO
637 * has data, and reads data from the TX register.
639 * @param base LPUART peripheral base address.
640 * @param data Start address of the buffer to store the received data.
641 * @param length Size of the buffer.
642 * @retval kStatus_LPUART_RxHardwareOverrun Receiver overrun happened while receiving data.
643 * @retval kStatus_LPUART_NoiseError Noise error happened while receiving data.
644 * @retval kStatus_LPUART_FramingError Framing error happened while receiving data.
645 * @retval kStatus_LPUART_ParityError Parity error happened while receiving data.
646 * @retval kStatus_Success Successfully received all data.
648 status_t LPUART_ReadBlocking(LPUART_Type *base, uint8_t *data, size_t length);
653 * @name Transactional
658 * @brief Initializes the LPUART handle.
660 * This function initializes the LPUART handle, which can be used for other LPUART
661 * transactional APIs. Usually, for a specified LPUART instance,
662 * call this API once to get the initialized handle.
664 * The LPUART driver supports the "background" receiving, which means that user can set up
665 * an RX ring buffer optionally. Data received is stored into the ring buffer even when the
666 * user doesn't call the LPUART_TransferReceiveNonBlocking() API. If there is already data received
667 * in the ring buffer, the user can get the received data from the ring buffer directly.
668 * The ring buffer is disabled if passing NULL as @p ringBuffer.
670 * @param base LPUART peripheral base address.
671 * @param handle LPUART handle pointer.
672 * @param callback Callback function.
673 * @param userData User data.
675 void LPUART_TransferCreateHandle(LPUART_Type *base,
676 lpuart_handle_t *handle,
677 lpuart_transfer_callback_t callback,
680 * @brief Transmits a buffer of data using the interrupt method.
682 * This function send data using an interrupt method. This is a non-blocking function, which
683 * returns directly without waiting for all data written to the transmitter register. When
684 * all data is written to the TX register in the ISR, the LPUART driver calls the callback
685 * function and passes the @ref kStatus_LPUART_TxIdle as status parameter.
687 * @note The kStatus_LPUART_TxIdle is passed to the upper layer when all data are written
688 * to the TX register. However, there is no check to ensure that all the data sent out. Before disabling the TX,
689 * check the kLPUART_TransmissionCompleteFlag to ensure that the transmit is finished.
691 * @param base LPUART peripheral base address.
692 * @param handle LPUART handle pointer.
693 * @param xfer LPUART transfer structure, see #lpuart_transfer_t.
694 * @retval kStatus_Success Successfully start the data transmission.
695 * @retval kStatus_LPUART_TxBusy Previous transmission still not finished, data not all written to the TX register.
696 * @retval kStatus_InvalidArgument Invalid argument.
698 status_t LPUART_TransferSendNonBlocking(LPUART_Type *base, lpuart_handle_t *handle, lpuart_transfer_t *xfer);
701 * @brief Sets up the RX ring buffer.
703 * This function sets up the RX ring buffer to a specific UART handle.
705 * When the RX ring buffer is used, data received is stored into the ring buffer even when
706 * the user doesn't call the UART_TransferReceiveNonBlocking() API. If there is already data received
707 * in the ring buffer, the user can get the received data from the ring buffer directly.
709 * @note When using RX ring buffer, one byte is reserved for internal use. In other
710 * words, if @p ringBufferSize is 32, then only 31 bytes are used for saving data.
712 * @param base LPUART peripheral base address.
713 * @param handle LPUART handle pointer.
714 * @param ringBuffer Start address of ring buffer for background receiving. Pass NULL to disable the ring buffer.
715 * @param ringBufferSize size of the ring buffer.
717 void LPUART_TransferStartRingBuffer(LPUART_Type *base,
718 lpuart_handle_t *handle,
720 size_t ringBufferSize);
723 * @brief Aborts the background transfer and uninstalls the ring buffer.
725 * This function aborts the background transfer and uninstalls the ring buffer.
727 * @param base LPUART peripheral base address.
728 * @param handle LPUART handle pointer.
730 void LPUART_TransferStopRingBuffer(LPUART_Type *base, lpuart_handle_t *handle);
733 * @brief Aborts the interrupt-driven data transmit.
735 * This function aborts the interrupt driven data sending. The user can get the remainBtyes to find out
736 * how many bytes are not sent out.
738 * @param base LPUART peripheral base address.
739 * @param handle LPUART handle pointer.
741 void LPUART_TransferAbortSend(LPUART_Type *base, lpuart_handle_t *handle);
744 * @brief Gets the number of bytes that have been written to the LPUART transmitter register.
746 * This function gets the number of bytes that have been written to LPUART TX
747 * register by an interrupt method.
749 * @param base LPUART peripheral base address.
750 * @param handle LPUART handle pointer.
751 * @param count Send bytes count.
752 * @retval kStatus_NoTransferInProgress No send in progress.
753 * @retval kStatus_InvalidArgument Parameter is invalid.
754 * @retval kStatus_Success Get successfully through the parameter \p count;
756 status_t LPUART_TransferGetSendCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count);
759 * @brief Receives a buffer of data using the interrupt method.
761 * This function receives data using an interrupt method. This is a non-blocking function
762 * which returns without waiting to ensure that all data are received.
763 * If the RX ring buffer is used and not empty, the data in the ring buffer is copied and
764 * the parameter @p receivedBytes shows how many bytes are copied from the ring buffer.
765 * After copying, if the data in the ring buffer is not enough for read, the receive
766 * request is saved by the LPUART driver. When the new data arrives, the receive request
767 * is serviced first. When all data is received, the LPUART driver notifies the upper layer
768 * through a callback function and passes a status parameter @ref kStatus_UART_RxIdle.
769 * For example, the upper layer needs 10 bytes but there are only 5 bytes in ring buffer.
770 * The 5 bytes are copied to xfer->data, which returns with the
771 * parameter @p receivedBytes set to 5. For the remaining 5 bytes, the newly arrived data is
772 * saved from xfer->data[5]. When 5 bytes are received, the LPUART driver notifies the upper layer.
773 * If the RX ring buffer is not enabled, this function enables the RX and RX interrupt
774 * to receive data to xfer->data. When all data is received, the upper layer is notified.
776 * @param base LPUART peripheral base address.
777 * @param handle LPUART handle pointer.
778 * @param xfer LPUART transfer structure, see #uart_transfer_t.
779 * @param receivedBytes Bytes received from the ring buffer directly.
780 * @retval kStatus_Success Successfully queue the transfer into the transmit queue.
781 * @retval kStatus_LPUART_RxBusy Previous receive request is not finished.
782 * @retval kStatus_InvalidArgument Invalid argument.
784 status_t LPUART_TransferReceiveNonBlocking(LPUART_Type *base,
785 lpuart_handle_t *handle,
786 lpuart_transfer_t *xfer,
787 size_t *receivedBytes);
790 * @brief Aborts the interrupt-driven data receiving.
792 * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to find out
793 * how many bytes not received yet.
795 * @param base LPUART peripheral base address.
796 * @param handle LPUART handle pointer.
798 void LPUART_TransferAbortReceive(LPUART_Type *base, lpuart_handle_t *handle);
801 * @brief Gets the number of bytes that have been received.
803 * This function gets the number of bytes that have been received.
805 * @param base LPUART peripheral base address.
806 * @param handle LPUART handle pointer.
807 * @param count Receive bytes count.
808 * @retval kStatus_NoTransferInProgress No receive in progress.
809 * @retval kStatus_InvalidArgument Parameter is invalid.
810 * @retval kStatus_Success Get successfully through the parameter \p count;
812 status_t LPUART_TransferGetReceiveCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count);
815 * @brief LPUART IRQ handle function.
817 * This function handles the LPUART transmit and receive IRQ request.
819 * @param base LPUART peripheral base address.
820 * @param handle LPUART handle pointer.
822 void LPUART_TransferHandleIRQ(LPUART_Type *base, lpuart_handle_t *handle);
825 * @brief LPUART Error IRQ handle function.
827 * This function handles the LPUART error IRQ request.
829 * @param base LPUART peripheral base address.
830 * @param handle LPUART handle pointer.
832 void LPUART_TransferHandleErrorIRQ(LPUART_Type *base, lpuart_handle_t *handle);
836 #if defined(__cplusplus)
842 #endif /* _FSL_LPUART_H_ */