1 /*
2 * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
3 * Copyright 2016-2024 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8 #ifndef FSL_LPUART_H_
9 #define FSL_LPUART_H_
10
11 #include "fsl_common.h"
12
13 /*!
14 * @addtogroup lpuart_driver
15 * @{
16 */
17
18 /*******************************************************************************
19 * Definitions
20 ******************************************************************************/
21
22 /*! @name Driver version */
23 /*! @{ */
24 /*! @brief LPUART driver version. */
25 #define FSL_LPUART_DRIVER_VERSION (MAKE_VERSION(2, 8, 2))
26 /*! @} */
27
28 /*! @brief Retry times for waiting flag. */
29 #ifndef UART_RETRY_TIMES
30 #define UART_RETRY_TIMES 0U /* Defining to zero means to keep waiting for the flag until it is assert/deassert. */
31 #endif
32
33 /*! @brief Error codes for the LPUART driver. */
34 enum
35 {
36 kStatus_LPUART_TxBusy = MAKE_STATUS(kStatusGroup_LPUART, 0), /*!< TX busy */
37 kStatus_LPUART_RxBusy = MAKE_STATUS(kStatusGroup_LPUART, 1), /*!< RX busy */
38 kStatus_LPUART_TxIdle = MAKE_STATUS(kStatusGroup_LPUART, 2), /*!< LPUART transmitter is idle. */
39 kStatus_LPUART_RxIdle = MAKE_STATUS(kStatusGroup_LPUART, 3), /*!< LPUART receiver is idle. */
40 kStatus_LPUART_TxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_LPUART, 4), /*!< TX FIFO watermark too large */
41 kStatus_LPUART_RxWatermarkTooLarge = MAKE_STATUS(kStatusGroup_LPUART, 5), /*!< RX FIFO watermark too large */
42 kStatus_LPUART_FlagCannotClearManually = MAKE_STATUS(kStatusGroup_LPUART, 6), /*!< Some flag can't manually clear */
43 kStatus_LPUART_Error = MAKE_STATUS(kStatusGroup_LPUART, 7), /*!< Error happens on LPUART. */
44 kStatus_LPUART_RxRingBufferOverrun =
45 MAKE_STATUS(kStatusGroup_LPUART, 8), /*!< LPUART RX software ring buffer overrun. */
46 kStatus_LPUART_RxHardwareOverrun = MAKE_STATUS(kStatusGroup_LPUART, 9), /*!< LPUART RX receiver overrun. */
47 kStatus_LPUART_NoiseError = MAKE_STATUS(kStatusGroup_LPUART, 10), /*!< LPUART noise error. */
48 kStatus_LPUART_FramingError = MAKE_STATUS(kStatusGroup_LPUART, 11), /*!< LPUART framing error. */
49 kStatus_LPUART_ParityError = MAKE_STATUS(kStatusGroup_LPUART, 12), /*!< LPUART parity error. */
50 kStatus_LPUART_BaudrateNotSupport =
51 MAKE_STATUS(kStatusGroup_LPUART, 13), /*!< Baudrate is not support in current clock source */
52 kStatus_LPUART_IdleLineDetected = MAKE_STATUS(kStatusGroup_LPUART, 14), /*!< IDLE flag. */
53 kStatus_LPUART_Timeout = MAKE_STATUS(kStatusGroup_LPUART, 15), /*!< LPUART times out. */
54 };
55
56 /*! @brief LPUART parity mode. */
57 typedef enum _lpuart_parity_mode
58 {
59 kLPUART_ParityDisabled = 0x0U, /*!< Parity disabled */
60 kLPUART_ParityEven = 0x2U, /*!< Parity enabled, type even, bit setting: PE|PT = 10 */
61 kLPUART_ParityOdd = 0x3U, /*!< Parity enabled, type odd, bit setting: PE|PT = 11 */
62 } lpuart_parity_mode_t;
63
64 /*! @brief LPUART data bits count. */
65 typedef enum _lpuart_data_bits
66 {
67 kLPUART_EightDataBits = 0x0U, /*!< Eight data bit */
68 #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
69 kLPUART_SevenDataBits = 0x1U, /*!< Seven data bit */
70 #endif
71 } lpuart_data_bits_t;
72
73 /*! @brief LPUART stop bit count. */
74 typedef enum _lpuart_stop_bit_count
75 {
76 kLPUART_OneStopBit = 0U, /*!< One stop bit */
77 kLPUART_TwoStopBit = 1U, /*!< Two stop bits */
78 } lpuart_stop_bit_count_t;
79
80 #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
81 /*! @brief LPUART transmit CTS source. */
82 typedef enum _lpuart_transmit_cts_source
83 {
84 kLPUART_CtsSourcePin = 0U, /*!< CTS resource is the LPUART_CTS pin. */
85 kLPUART_CtsSourceMatchResult = 1U, /*!< CTS resource is the match result. */
86 } lpuart_transmit_cts_source_t;
87
88 /*! @brief LPUART transmit CTS configure. */
89 typedef enum _lpuart_transmit_cts_config
90 {
91 kLPUART_CtsSampleAtStart = 0U, /*!< CTS input is sampled at the start of each character. */
92 kLPUART_CtsSampleAtIdle = 1U, /*!< CTS input is sampled when the transmitter is idle */
93 } lpuart_transmit_cts_config_t;
94 #endif
95
96 /*! @brief LPUART idle flag type defines when the receiver starts counting. */
97 typedef enum _lpuart_idle_type_select
98 {
99 kLPUART_IdleTypeStartBit = 0U, /*!< Start counting after a valid start bit. */
100 kLPUART_IdleTypeStopBit = 1U, /*!< Start counting after a stop bit. */
101 } lpuart_idle_type_select_t;
102
103 /*! @brief LPUART idle detected configuration.
104 * This structure defines the number of idle characters that must be received before
105 * the IDLE flag is set.
106 */
107 typedef enum _lpuart_idle_config
108 {
109 kLPUART_IdleCharacter1 = 0U, /*!< the number of idle characters. */
110 kLPUART_IdleCharacter2 = 1U, /*!< the number of idle characters. */
111 kLPUART_IdleCharacter4 = 2U, /*!< the number of idle characters. */
112 kLPUART_IdleCharacter8 = 3U, /*!< the number of idle characters. */
113 kLPUART_IdleCharacter16 = 4U, /*!< the number of idle characters. */
114 kLPUART_IdleCharacter32 = 5U, /*!< the number of idle characters. */
115 kLPUART_IdleCharacter64 = 6U, /*!< the number of idle characters. */
116 kLPUART_IdleCharacter128 = 7U, /*!< the number of idle characters. */
117 } lpuart_idle_config_t;
118
119 /*!
120 * @brief LPUART interrupt configuration structure, default settings all disabled.
121 *
122 * This structure contains the settings for all LPUART interrupt configurations.
123 */
124 enum _lpuart_interrupt_enable
125 {
126 #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
127 kLPUART_LinBreakInterruptEnable = (LPUART_BAUD_LBKDIE_MASK >> 8U), /*!< LIN break detect. bit 7 */
128 #endif
129 kLPUART_RxActiveEdgeInterruptEnable = (LPUART_BAUD_RXEDGIE_MASK >> 8U), /*!< Receive Active Edge. bit 6 */
130 kLPUART_TxDataRegEmptyInterruptEnable = (LPUART_CTRL_TIE_MASK), /*!< Transmit data register empty. bit 23 */
131 kLPUART_TransmissionCompleteInterruptEnable = (LPUART_CTRL_TCIE_MASK), /*!< Transmission complete. bit 22 */
132 kLPUART_RxDataRegFullInterruptEnable = (LPUART_CTRL_RIE_MASK), /*!< Receiver data register full. bit 21 */
133 kLPUART_IdleLineInterruptEnable = (LPUART_CTRL_ILIE_MASK), /*!< Idle line. bit 20 */
134 kLPUART_RxOverrunInterruptEnable = (LPUART_CTRL_ORIE_MASK), /*!< Receiver Overrun. bit 27 */
135 kLPUART_NoiseErrorInterruptEnable = (LPUART_CTRL_NEIE_MASK), /*!< Noise error flag. bit 26 */
136 kLPUART_FramingErrorInterruptEnable = (LPUART_CTRL_FEIE_MASK), /*!< Framing error flag. bit 25 */
137 kLPUART_ParityErrorInterruptEnable = (LPUART_CTRL_PEIE_MASK), /*!< Parity error flag. bit 24 */
138 #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
139 kLPUART_Match1InterruptEnable = (LPUART_CTRL_MA1IE_MASK), /*!< Parity error flag. bit 15 */
140 kLPUART_Match2InterruptEnable = (LPUART_CTRL_MA2IE_MASK), /*!< Parity error flag. bit 14 */
141 #endif
142 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
143 kLPUART_TxFifoOverflowInterruptEnable = (LPUART_FIFO_TXOFE_MASK), /*!< Transmit FIFO Overflow. bit 9 */
144 kLPUART_RxFifoUnderflowInterruptEnable = (LPUART_FIFO_RXUFE_MASK), /*!< Receive FIFO Underflow. bit 8 */
145 #endif
146
147 kLPUART_AllInterruptEnable = kLPUART_RxActiveEdgeInterruptEnable | kLPUART_TxDataRegEmptyInterruptEnable |
148 kLPUART_TransmissionCompleteInterruptEnable | kLPUART_RxDataRegFullInterruptEnable |
149 kLPUART_IdleLineInterruptEnable | kLPUART_RxOverrunInterruptEnable |
150 kLPUART_NoiseErrorInterruptEnable | kLPUART_FramingErrorInterruptEnable |
151 kLPUART_ParityErrorInterruptEnable
152 #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
153 | kLPUART_LinBreakInterruptEnable
154 #endif
155 #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
156 | kLPUART_Match1InterruptEnable | kLPUART_Match2InterruptEnable
157 #endif
158 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
159 | kLPUART_TxFifoOverflowInterruptEnable | kLPUART_RxFifoUnderflowInterruptEnable
160 #endif
161 ,
162 };
163
164 /*!
165 * @brief LPUART status flags.
166 *
167 * This provides constants for the LPUART status flags for use in the LPUART functions.
168 */
169 enum _lpuart_flags
170 {
171 kLPUART_TxDataRegEmptyFlag =
172 (LPUART_STAT_TDRE_MASK), /*!< Transmit data register empty flag, sets when transmit buffer is empty. bit 23 */
173 kLPUART_TransmissionCompleteFlag =
174 (LPUART_STAT_TC_MASK), /*!< Transmission complete flag, sets when transmission activity complete. bit 22 */
175 kLPUART_RxDataRegFullFlag = (LPUART_STAT_RDRF_MASK), /*!< Receive data register full flag, sets when the receive
176 data buffer is full. bit 21 */
177 kLPUART_IdleLineFlag = (LPUART_STAT_IDLE_MASK), /*!< Idle line detect flag, sets when idle line detected. bit 20 */
178 kLPUART_RxOverrunFlag = (LPUART_STAT_OR_MASK), /*!< Receive Overrun, sets when new data is received before data is
179 read from receive register. bit 19 */
180 kLPUART_NoiseErrorFlag = (LPUART_STAT_NF_MASK), /*!< Receive takes 3 samples of each received bit. If any of these
181 samples differ, noise flag sets. bit 18 */
182 kLPUART_FramingErrorFlag =
183 (LPUART_STAT_FE_MASK), /*!< Frame error flag, sets if logic 0 was detected where stop bit expected. bit 17 */
184 kLPUART_ParityErrorFlag = (LPUART_STAT_PF_MASK), /*!< If parity enabled, sets upon parity error detection. bit 16 */
185 #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
186 kLPUART_LinBreakFlag = (LPUART_STAT_LBKDIF_MASK), /*!< LIN break detect interrupt flag, sets when LIN break
187 char detected and LIN circuit enabled. bit 31 */
188 #endif
189 kLPUART_RxActiveEdgeFlag = (LPUART_STAT_RXEDGIF_MASK), /*!< Receive pin active edge interrupt flag, sets when active
190 edge detected. bit 30 */
191 kLPUART_RxActiveFlag =
192 (LPUART_STAT_RAF_MASK), /*!< Receiver Active Flag (RAF), sets at beginning of valid start. bit 24 */
193 #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
194 kLPUART_DataMatch1Flag =
195 LPUART_STAT_MA1F_MASK, /*!< The next character to be read from LPUART_DATA matches MA1. bit 15 */
196 kLPUART_DataMatch2Flag =
197 LPUART_STAT_MA2F_MASK, /*!< The next character to be read from LPUART_DATA matches MA2. bit 14 */
198 #endif
199 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
200 kLPUART_TxFifoEmptyFlag =
201 (LPUART_FIFO_TXEMPT_MASK >> 16), /*!< TXEMPT bit, sets if transmit buffer is empty. bit 7 */
202 kLPUART_RxFifoEmptyFlag =
203 (LPUART_FIFO_RXEMPT_MASK >> 16), /*!< RXEMPT bit, sets if receive buffer is empty. bit 6 */
204 kLPUART_TxFifoOverflowFlag =
205 (LPUART_FIFO_TXOF_MASK >> 16), /*!< TXOF bit, sets if transmit buffer overflow occurred. bit 1 */
206 kLPUART_RxFifoUnderflowFlag =
207 (LPUART_FIFO_RXUF_MASK >> 16), /*!< RXUF bit, sets if receive buffer underflow occurred. bit 0 */
208 #endif
209
210 kLPUART_AllClearFlags = kLPUART_RxActiveEdgeFlag | kLPUART_IdleLineFlag | kLPUART_RxOverrunFlag |
211 kLPUART_NoiseErrorFlag | kLPUART_FramingErrorFlag | kLPUART_ParityErrorFlag
212 #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
213 | kLPUART_DataMatch1Flag | kLPUART_DataMatch2Flag
214 #endif
215 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
216 | kLPUART_TxFifoOverflowFlag | kLPUART_RxFifoUnderflowFlag
217 #endif
218 #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
219 | kLPUART_LinBreakFlag
220 #endif
221 ,
222
223 kLPUART_AllFlags =
224 kLPUART_RxActiveEdgeFlag | kLPUART_IdleLineFlag | kLPUART_RxOverrunFlag | kLPUART_TxDataRegEmptyFlag |
225 kLPUART_TransmissionCompleteFlag | kLPUART_RxDataRegFullFlag | kLPUART_RxActiveFlag | kLPUART_NoiseErrorFlag |
226 kLPUART_FramingErrorFlag | kLPUART_ParityErrorFlag
227 #if defined(FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING) && FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
228 | kLPUART_DataMatch1Flag | kLPUART_DataMatch2Flag
229 #endif
230 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
231 | kLPUART_TxFifoOverflowFlag | kLPUART_RxFifoUnderflowFlag | kLPUART_TxFifoEmptyFlag | kLPUART_RxFifoEmptyFlag
232 #endif
233 #if defined(FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_LPUART_HAS_LIN_BREAK_DETECT
234 | kLPUART_LinBreakFlag
235 #endif
236 ,
237 };
238
239 /*! @brief LPUART configuration structure. */
240 typedef struct _lpuart_config
241 {
242 uint32_t baudRate_Bps; /*!< LPUART baud rate */
243 lpuart_parity_mode_t parityMode; /*!< Parity mode, disabled (default), even, odd */
244 lpuart_data_bits_t dataBitsCount; /*!< Data bits count, eight (default), seven */
245 bool isMsb; /*!< Data bits order, LSB (default), MSB */
246 #if defined(FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT
247 lpuart_stop_bit_count_t stopBitCount; /*!< Number of stop bits, 1 stop bit (default) or 2 stop bits */
248 #endif
249 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
250 uint8_t txFifoWatermark; /*!< TX FIFO watermark */
251 uint8_t rxFifoWatermark; /*!< RX FIFO watermark */
252 #endif
253 #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
254 bool enableRxRTS; /*!< RX RTS enable */
255 bool enableTxCTS; /*!< TX CTS enable */
256 lpuart_transmit_cts_source_t txCtsSource; /*!< TX CTS source */
257 lpuart_transmit_cts_config_t txCtsConfig; /*!< TX CTS configure */
258 #endif
259 lpuart_idle_type_select_t rxIdleType; /*!< RX IDLE type. */
260 lpuart_idle_config_t rxIdleConfig; /*!< RX IDLE configuration. */
261 bool enableTx; /*!< Enable TX */
262 bool enableRx; /*!< Enable RX */
263 } lpuart_config_t;
264
265 /*! @brief LPUART transfer structure. */
266 typedef struct _lpuart_transfer
267 {
268 /*
269 * Use separate TX and RX data pointer, because TX data is const data.
270 * The member data is kept for backward compatibility.
271 */
272 union
273 {
274 uint8_t *data; /*!< The buffer of data to be transfer.*/
275 uint8_t *rxData; /*!< The buffer to receive data. */
276 uint16_t *rxData16; /*!< The buffer to receive data. */
277 const uint8_t *txData; /*!< The buffer of data to be sent. */
278 const uint16_t *txData16; /*!< The buffer of data to be sent. */
279 };
280 size_t dataSize; /*!< The byte count to be transfer. */
281 } lpuart_transfer_t;
282
283 /* Forward declaration of the handle typedef. */
284 typedef struct _lpuart_handle lpuart_handle_t;
285
286 /*! @brief LPUART transfer callback function. */
287 typedef void (*lpuart_transfer_callback_t)(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData);
288
289 /*! @brief LPUART handle structure. */
290 struct _lpuart_handle
291 {
292 union
293 {
294 const uint8_t *volatile txData; /*!< Address of remaining data to send. */
295 const uint16_t *volatile txData16; /*!< Address of remaining data to send. */
296 };
297 volatile size_t txDataSize; /*!< Size of the remaining data to send. */
298 size_t txDataSizeAll; /*!< Size of the data to send out. */
299 union
300 {
301 uint8_t *volatile rxData; /*!< Address of remaining data to receive. */
302 uint16_t *volatile rxData16; /*!< Address of remaining data to receive. */
303 };
304 volatile size_t rxDataSize; /*!< Size of the remaining data to receive. */
305 size_t rxDataSizeAll; /*!< Size of the data to receive. */
306
307 union
308 {
309 uint8_t *rxRingBuffer; /*!< Start address of the receiver ring buffer. */
310 uint16_t *rxRingBuffer16; /*!< Start address of the receiver ring buffer. */
311 };
312 size_t rxRingBufferSize; /*!< Size of the ring buffer. */
313 volatile uint16_t rxRingBufferHead; /*!< Index for the driver to store received data into ring buffer. */
314 volatile uint16_t rxRingBufferTail; /*!< Index for the user to get data from the ring buffer. */
315
316 lpuart_transfer_callback_t callback; /*!< Callback function. */
317 void *userData; /*!< LPUART callback function parameter.*/
318
319 volatile uint8_t txState; /*!< TX transfer state. */
320 volatile uint8_t rxState; /*!< RX transfer state. */
321
322 #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
323 bool isSevenDataBits; /*!< Seven data bits flag. */
324 #endif
325 bool is16bitData; /*!< 16bit data bits flag, only used for 9bit or 10bit data */
326 };
327
328 /* Typedef for interrupt handler. */
329 typedef void (*lpuart_isr_t)(LPUART_Type *base, void *handle);
330
331 /*******************************************************************************
332 * Variables
333 ******************************************************************************/
334 /* Array of LPUART handle. */
335 extern void *s_lpuartHandle[];
336
337 /* Array of LPUART IRQ number. */
338 #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
339 extern const IRQn_Type s_lpuartTxIRQ[];
340 #else
341 extern const IRQn_Type s_lpuartIRQ[];
342 #endif
343
344 /* LPUART ISR for transactional APIs. */
345 extern lpuart_isr_t s_lpuartIsr[];
346
347 /*******************************************************************************
348 * API
349 ******************************************************************************/
350
351 #if defined(__cplusplus)
352 extern "C" {
353 #endif /* _cplusplus */
354
355 #if defined(FSL_FEATURE_LPUART_HAS_GLOBAL) && FSL_FEATURE_LPUART_HAS_GLOBAL
356
357 /*!
358 * @name Software Reset
359 * @{
360 */
361
362 /*!
363 * @brief Resets the LPUART using software.
364 *
365 * This function resets all internal logic and registers except the Global Register.
366 * Remains set until cleared by software.
367 *
368 * @param base LPUART peripheral base address.
369 */
LPUART_SoftwareReset(LPUART_Type * base)370 static inline void LPUART_SoftwareReset(LPUART_Type *base)
371 {
372 base->GLOBAL |= LPUART_GLOBAL_RST_MASK;
373 base->GLOBAL &= ~LPUART_GLOBAL_RST_MASK;
374 }
375 /*! @} */
376 #endif /*FSL_FEATURE_LPUART_HAS_GLOBAL*/
377
378 /*!
379 * @name Initialization and deinitialization
380 * @{
381 */
382
383 /*!
384 * @brief Initializes an LPUART instance with the user configuration structure and the peripheral clock.
385 *
386 * This function configures the LPUART module with user-defined settings. Call the LPUART_GetDefaultConfig() function
387 * to configure the configuration structure and get the default configuration.
388 * The example below shows how to use this API to configure the LPUART.
389 * @code
390 * lpuart_config_t lpuartConfig;
391 * lpuartConfig.baudRate_Bps = 115200U;
392 * lpuartConfig.parityMode = kLPUART_ParityDisabled;
393 * lpuartConfig.dataBitsCount = kLPUART_EightDataBits;
394 * lpuartConfig.isMsb = false;
395 * lpuartConfig.stopBitCount = kLPUART_OneStopBit;
396 * lpuartConfig.txFifoWatermark = 0;
397 * lpuartConfig.rxFifoWatermark = 1;
398 * LPUART_Init(LPUART1, &lpuartConfig, 20000000U);
399 * @endcode
400 *
401 * @param base LPUART peripheral base address.
402 * @param config Pointer to a user-defined configuration structure.
403 * @param srcClock_Hz LPUART clock source frequency in HZ.
404 * @retval kStatus_LPUART_BaudrateNotSupport Baudrate is not support in current clock source.
405 * @retval kStatus_Success LPUART initialize succeed
406 */
407 status_t LPUART_Init(LPUART_Type *base, const lpuart_config_t *config, uint32_t srcClock_Hz);
408
409 /*!
410 * @brief Deinitializes a LPUART instance.
411 *
412 * This function waits for transmit to complete, disables TX and RX, and disables the LPUART clock.
413 *
414 * @param base LPUART peripheral base address.
415 */
416 void LPUART_Deinit(LPUART_Type *base);
417
418 /*!
419 * @brief Gets the default configuration structure.
420 *
421 * This function initializes the LPUART configuration structure to a default value. The default
422 * values are:
423 * lpuartConfig->baudRate_Bps = 115200U;
424 * lpuartConfig->parityMode = kLPUART_ParityDisabled;
425 * lpuartConfig->dataBitsCount = kLPUART_EightDataBits;
426 * lpuartConfig->isMsb = false;
427 * lpuartConfig->stopBitCount = kLPUART_OneStopBit;
428 * lpuartConfig->txFifoWatermark = 0;
429 * lpuartConfig->rxFifoWatermark = 1;
430 * lpuartConfig->rxIdleType = kLPUART_IdleTypeStartBit;
431 * lpuartConfig->rxIdleConfig = kLPUART_IdleCharacter1;
432 * lpuartConfig->enableTx = false;
433 * lpuartConfig->enableRx = false;
434 *
435 * @param config Pointer to a configuration structure.
436 */
437 void LPUART_GetDefaultConfig(lpuart_config_t *config);
438 /*! @} */
439
440 /*!
441 * @name Module configuration
442 * @{
443 */
444 /*!
445 * @brief Sets the LPUART instance baudrate.
446 *
447 * This function configures the LPUART module baudrate. This function is used to update
448 * the LPUART module baudrate after the LPUART module is initialized by the LPUART_Init.
449 * @code
450 * LPUART_SetBaudRate(LPUART1, 115200U, 20000000U);
451 * @endcode
452 *
453 * @param base LPUART peripheral base address.
454 * @param baudRate_Bps LPUART baudrate to be set.
455 * @param srcClock_Hz LPUART clock source frequency in HZ.
456 * @retval kStatus_LPUART_BaudrateNotSupport Baudrate is not supported in the current clock source.
457 * @retval kStatus_Success Set baudrate succeeded.
458 */
459 status_t LPUART_SetBaudRate(LPUART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
460
461 /*!
462 * @brief Enable 9-bit data mode for LPUART.
463 *
464 * This function set the 9-bit mode for LPUART module. The 9th bit is not used for parity thus can be modified by user.
465 *
466 * @param base LPUART peripheral base address.
467 * @param enable true to enable, flase to disable.
468 */
469 void LPUART_Enable9bitMode(LPUART_Type *base, bool enable);
470
471 /*!
472 * @brief Set the LPUART address.
473 *
474 * This function configures the address for LPUART module that works as slave in 9-bit data mode. One or two address
475 * fields can be configured. When the address field's match enable bit is set, the frame it receices with MSB being
476 * 1 is considered as an address frame, otherwise it is considered as data frame. Once the address frame matches one
477 * of slave's own addresses, this slave is addressed. This address frame and its following data frames are stored in
478 * the receive buffer, otherwise the frames will be discarded. To un-address a slave, just send an address frame with
479 * unmatched address.
480 *
481 * @note Any LPUART instance joined in the multi-slave system can work as slave. The position of the address mark is the
482 * same as the parity bit when parity is enabled for 8 bit and 9 bit data formats.
483 *
484 * @param base LPUART peripheral base address.
485 * @param address1 LPUART slave address1.
486 * @param address2 LPUART slave address2.
487 */
LPUART_SetMatchAddress(LPUART_Type * base,uint16_t address1,uint16_t address2)488 static inline void LPUART_SetMatchAddress(LPUART_Type *base, uint16_t address1, uint16_t address2)
489 {
490 /* Configure match address. */
491 uint32_t address = ((uint32_t)address2 << 16U) | (uint32_t)address1 | 0x1000100UL;
492 base->MATCH = address;
493 }
494
495 /*!
496 * @brief Enable the LPUART match address feature.
497 *
498 * @param base LPUART peripheral base address.
499 * @param match1 true to enable match address1, false to disable.
500 * @param match2 true to enable match address2, false to disable.
501 */
LPUART_EnableMatchAddress(LPUART_Type * base,bool match1,bool match2)502 static inline void LPUART_EnableMatchAddress(LPUART_Type *base, bool match1, bool match2)
503 {
504 /* Configure match address1 enable bit. */
505 if (match1)
506 {
507 base->BAUD |= (uint32_t)LPUART_BAUD_MAEN1_MASK;
508 }
509 else
510 {
511 base->BAUD &= ~(uint32_t)LPUART_BAUD_MAEN1_MASK;
512 }
513 /* Configure match address2 enable bit. */
514 if (match2)
515 {
516 base->BAUD |= (uint32_t)LPUART_BAUD_MAEN2_MASK;
517 }
518 else
519 {
520 base->BAUD &= ~(uint32_t)LPUART_BAUD_MAEN2_MASK;
521 }
522 }
523
524 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
525 /*!
526 * @brief Sets the rx FIFO watermark.
527 *
528 * @param base LPUART peripheral base address.
529 * @param water Rx FIFO watermark.
530 */
LPUART_SetRxFifoWatermark(LPUART_Type * base,uint8_t water)531 static inline void LPUART_SetRxFifoWatermark(LPUART_Type *base, uint8_t water)
532 {
533 assert((uint8_t)FSL_FEATURE_LPUART_FIFO_SIZEn(base) > water);
534 base->WATER = (base->WATER & ~LPUART_WATER_RXWATER_MASK) | LPUART_WATER_RXWATER(water);
535 }
536
537 /*!
538 * @brief Sets the tx FIFO watermark.
539 *
540 * @param base LPUART peripheral base address.
541 * @param water Tx FIFO watermark.
542 */
LPUART_SetTxFifoWatermark(LPUART_Type * base,uint8_t water)543 static inline void LPUART_SetTxFifoWatermark(LPUART_Type *base, uint8_t water)
544 {
545 assert((uint8_t)FSL_FEATURE_LPUART_FIFO_SIZEn(base) > water);
546 base->WATER = (base->WATER & ~LPUART_WATER_TXWATER_MASK) | LPUART_WATER_TXWATER(water);
547 }
548 #endif
549
550 /*!
551 * @brief Sets the LPUART using 16bit transmit, only for 9bit or 10bit mode.
552 *
553 * This function Enable 16bit Data transmit in lpuart_handle_t.
554 *
555 * @param handle LPUART handle pointer.
556 * @param enable true to enable, false to disable.
557 */
LPUART_TransferEnable16Bit(lpuart_handle_t * handle,bool enable)558 static inline void LPUART_TransferEnable16Bit(lpuart_handle_t *handle, bool enable)
559 {
560 handle->is16bitData = enable;
561 }
562 /*! @} */
563
564 /*!
565 * @name Status
566 * @{
567 */
568
569 /*!
570 * @brief Gets LPUART status flags.
571 *
572 * This function gets all LPUART status flags. The flags are returned as the logical
573 * OR value of the enumerators @ref _lpuart_flags. To check for a specific status,
574 * compare the return value with enumerators in the @ref _lpuart_flags.
575 * For example, to check whether the TX is empty:
576 * @code
577 * if (kLPUART_TxDataRegEmptyFlag & LPUART_GetStatusFlags(LPUART1))
578 * {
579 * ...
580 * }
581 * @endcode
582 *
583 * @param base LPUART peripheral base address.
584 * @return LPUART status flags which are ORed by the enumerators in the _lpuart_flags.
585 */
586 uint32_t LPUART_GetStatusFlags(LPUART_Type *base);
587
588 /*!
589 * @brief Clears status flags with a provided mask.
590 *
591 * This function clears LPUART status flags with a provided mask. Automatically cleared flags
592 * can't be cleared by this function.
593 * Flags that can only cleared or set by hardware are:
594 * kLPUART_TxDataRegEmptyFlag, kLPUART_TransmissionCompleteFlag, kLPUART_RxDataRegFullFlag,
595 * kLPUART_RxActiveFlag, kLPUART_NoiseErrorFlag, kLPUART_ParityErrorFlag,
596 * kLPUART_TxFifoEmptyFlag,kLPUART_RxFifoEmptyFlag
597 * Note: This API should be called when the Tx/Rx is idle, otherwise it takes no effects.
598 *
599 * @param base LPUART peripheral base address.
600 * @param mask the status flags to be cleared. The user can use the enumerators in the
601 * _lpuart_status_flag_t to do the OR operation and get the mask.
602 * @return 0 succeed, others failed.
603 * @retval kStatus_LPUART_FlagCannotClearManually The flag can't be cleared by this function but
604 * it is cleared automatically by hardware.
605 * @retval kStatus_Success Status in the mask are cleared.
606 */
607 status_t LPUART_ClearStatusFlags(LPUART_Type *base, uint32_t mask);
608 /*! @} */
609
610 /*!
611 * @name Interrupts
612 * @{
613 */
614
615 /*!
616 * @brief Enables LPUART interrupts according to a provided mask.
617 *
618 * This function enables the LPUART interrupts according to a provided mask. The mask
619 * is a logical OR of enumeration members. See the @ref _lpuart_interrupt_enable.
620 * This examples shows how to enable TX empty interrupt and RX full interrupt:
621 * @code
622 * LPUART_EnableInterrupts(LPUART1,kLPUART_TxDataRegEmptyInterruptEnable | kLPUART_RxDataRegFullInterruptEnable);
623 * @endcode
624 *
625 * @param base LPUART peripheral base address.
626 * @param mask The interrupts to enable. Logical OR of @ref _lpuart_interrupt_enable.
627 */
628 void LPUART_EnableInterrupts(LPUART_Type *base, uint32_t mask);
629
630 /*!
631 * @brief Disables LPUART interrupts according to a provided mask.
632 *
633 * This function disables the LPUART interrupts according to a provided mask. The mask
634 * is a logical OR of enumeration members. See @ref _lpuart_interrupt_enable.
635 * This example shows how to disable the TX empty interrupt and RX full interrupt:
636 * @code
637 * LPUART_DisableInterrupts(LPUART1,kLPUART_TxDataRegEmptyInterruptEnable | kLPUART_RxDataRegFullInterruptEnable);
638 * @endcode
639 *
640 * @param base LPUART peripheral base address.
641 * @param mask The interrupts to disable. Logical OR of @ref _lpuart_interrupt_enable.
642 */
643 void LPUART_DisableInterrupts(LPUART_Type *base, uint32_t mask);
644
645 /*!
646 * @brief Gets enabled LPUART interrupts.
647 *
648 * This function gets the enabled LPUART interrupts. The enabled interrupts are returned
649 * as the logical OR value of the enumerators @ref _lpuart_interrupt_enable. To check
650 * a specific interrupt enable status, compare the return value with enumerators
651 * in @ref _lpuart_interrupt_enable.
652 * For example, to check whether the TX empty interrupt is enabled:
653 * @code
654 * uint32_t enabledInterrupts = LPUART_GetEnabledInterrupts(LPUART1);
655 *
656 * if (kLPUART_TxDataRegEmptyInterruptEnable & enabledInterrupts)
657 * {
658 * ...
659 * }
660 * @endcode
661 *
662 * @param base LPUART peripheral base address.
663 * @return LPUART interrupt flags which are logical OR of the enumerators in @ref _lpuart_interrupt_enable.
664 */
665 uint32_t LPUART_GetEnabledInterrupts(LPUART_Type *base);
666 /*! @} */
667
668 #if defined(FSL_FEATURE_LPUART_HAS_DMA_ENABLE) && FSL_FEATURE_LPUART_HAS_DMA_ENABLE
669 /*!
670 * @name DMA Configuration
671 * @{
672 */
673 /*!
674 * @brief Gets the LPUART data register address.
675 *
676 * This function returns the LPUART data register address, which is mainly used by the DMA/eDMA.
677 *
678 * @param base LPUART peripheral base address.
679 * @return LPUART data register addresses which are used both by the transmitter and receiver.
680 */
LPUART_GetDataRegisterAddress(LPUART_Type * base)681 static inline uintptr_t LPUART_GetDataRegisterAddress(LPUART_Type *base)
682 {
683 return (uintptr_t) & (base->DATA);
684 }
685
686 /*!
687 * @brief Enables or disables the LPUART transmitter DMA request.
688 *
689 * This function enables or disables the transmit data register empty flag, STAT[TDRE], to generate DMA requests.
690 *
691 * @param base LPUART peripheral base address.
692 * @param enable True to enable, false to disable.
693 */
LPUART_EnableTxDMA(LPUART_Type * base,bool enable)694 static inline void LPUART_EnableTxDMA(LPUART_Type *base, bool enable)
695 {
696 if (enable)
697 {
698 base->BAUD |= LPUART_BAUD_TDMAE_MASK;
699 }
700 else
701 {
702 base->BAUD &= ~LPUART_BAUD_TDMAE_MASK;
703 }
704 }
705
706 /*!
707 * @brief Enables or disables the LPUART receiver DMA.
708 *
709 * This function enables or disables the receiver data register full flag, STAT[RDRF], to generate DMA requests.
710 *
711 * @param base LPUART peripheral base address.
712 * @param enable True to enable, false to disable.
713 */
LPUART_EnableRxDMA(LPUART_Type * base,bool enable)714 static inline void LPUART_EnableRxDMA(LPUART_Type *base, bool enable)
715 {
716 if (enable)
717 {
718 base->BAUD |= LPUART_BAUD_RDMAE_MASK;
719 }
720 else
721 {
722 base->BAUD &= ~LPUART_BAUD_RDMAE_MASK;
723 }
724 }
725 /*! @} */
726 #endif /* FSL_FEATURE_LPUART_HAS_DMA_ENABLE */
727
728 /*!
729 * @name Bus Operations
730 * @{
731 */
732
733 /*!
734 * @brief Get the LPUART instance from peripheral base address.
735 *
736 * @param base LPUART peripheral base address.
737 * @return LPUART instance.
738 */
739 uint32_t LPUART_GetInstance(LPUART_Type *base);
740
741 /*!
742 * @brief Enables or disables the LPUART transmitter.
743 *
744 * This function enables or disables the LPUART transmitter.
745 *
746 * @param base LPUART peripheral base address.
747 * @param enable True to enable, false to disable.
748 */
LPUART_EnableTx(LPUART_Type * base,bool enable)749 static inline void LPUART_EnableTx(LPUART_Type *base, bool enable)
750 {
751 if (enable)
752 {
753 base->CTRL |= LPUART_CTRL_TE_MASK;
754 }
755 else
756 {
757 base->CTRL &= ~LPUART_CTRL_TE_MASK;
758 }
759 }
760
761 /*!
762 * @brief Enables or disables the LPUART receiver.
763 *
764 * This function enables or disables the LPUART receiver.
765 *
766 * @param base LPUART peripheral base address.
767 * @param enable True to enable, false to disable.
768 */
LPUART_EnableRx(LPUART_Type * base,bool enable)769 static inline void LPUART_EnableRx(LPUART_Type *base, bool enable)
770 {
771 if (enable)
772 {
773 base->CTRL |= LPUART_CTRL_RE_MASK;
774 }
775 else
776 {
777 base->CTRL &= ~LPUART_CTRL_RE_MASK;
778 }
779 }
780
781 /*!
782 * @brief Writes to the transmitter register.
783 *
784 * This function writes data to the transmitter register directly. The upper layer must
785 * ensure that the TX register is empty or that the TX FIFO has room before calling this function.
786 *
787 * @param base LPUART peripheral base address.
788 * @param data Data write to the TX register.
789 */
LPUART_WriteByte(LPUART_Type * base,uint8_t data)790 static inline void LPUART_WriteByte(LPUART_Type *base, uint8_t data)
791 {
792 base->DATA = data;
793 }
794
795 /*!
796 * @brief Reads the receiver register.
797 *
798 * This function reads data from the receiver register directly. The upper layer must
799 * ensure that the receiver register is full or that the RX FIFO has data before calling this function.
800 *
801 * @param base LPUART peripheral base address.
802 * @return Data read from data register.
803 */
LPUART_ReadByte(LPUART_Type * base)804 static inline uint8_t LPUART_ReadByte(LPUART_Type *base)
805 {
806 #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
807 uint32_t ctrl = base->CTRL;
808 uint8_t result;
809 /*
810 * $Branch Coverage Justification$
811 * (ctrl & LPUART_CTRL_M7_MASK) == 0U) false is not covered.
812 * If ctrl & LPUART_CTRL_M7_MASK is 0, it can't be !0 in next judge.
813 */
814 bool isSevenDataBits = (((ctrl & LPUART_CTRL_M7_MASK) != 0U) ||
815 (((ctrl & LPUART_CTRL_M7_MASK) == 0U) && ((ctrl & LPUART_CTRL_M_MASK) == 0U) &&
816 ((ctrl & LPUART_CTRL_PE_MASK) != 0U)));
817
818 if (isSevenDataBits)
819 {
820 result = (uint8_t)(base->DATA & 0x7FU);
821 }
822 else
823 {
824 result = (uint8_t)base->DATA;
825 }
826
827 return result;
828 #else
829 return (uint8_t)(base->DATA);
830 #endif
831 }
832
833 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
834 /*!
835 * @brief Gets the rx FIFO data count.
836 *
837 * @param base LPUART peripheral base address.
838 * @return rx FIFO data count.
839 */
LPUART_GetRxFifoCount(LPUART_Type * base)840 static inline uint8_t LPUART_GetRxFifoCount(LPUART_Type *base)
841 {
842 return (uint8_t)((base->WATER & LPUART_WATER_RXCOUNT_MASK) >> LPUART_WATER_RXCOUNT_SHIFT);
843 }
844
845 /*!
846 * @brief Gets the tx FIFO data count.
847 *
848 * @param base LPUART peripheral base address.
849 * @return tx FIFO data count.
850 */
LPUART_GetTxFifoCount(LPUART_Type * base)851 static inline uint8_t LPUART_GetTxFifoCount(LPUART_Type *base)
852 {
853 return (uint8_t)((base->WATER & LPUART_WATER_TXCOUNT_MASK) >> LPUART_WATER_TXCOUNT_SHIFT);
854 }
855 #endif
856
857 /*!
858 * @brief Transmit an address frame in 9-bit data mode.
859 *
860 * @param base LPUART peripheral base address.
861 * @param address LPUART slave address.
862 */
863 void LPUART_SendAddress(LPUART_Type *base, uint8_t address);
864
865 /*!
866 * @brief Writes to the transmitter register using a blocking method.
867 *
868 * This function polls the transmitter register, first waits for the register to be empty or TX FIFO to have room,
869 * and writes data to the transmitter buffer, then waits for the dat to be sent out to the bus.
870 *
871 * @param base LPUART peripheral base address.
872 * @param data Start address of the data to write.
873 * @param length Size of the data to write.
874 * @retval kStatus_LPUART_Timeout Transmission timed out and was aborted.
875 * @retval kStatus_Success Successfully wrote all data.
876 */
877 status_t LPUART_WriteBlocking(LPUART_Type *base, const uint8_t *data, size_t length);
878
879 /*!
880 * @brief Writes to the transmitter register using a blocking method in 9bit or 10bit mode.
881 *
882 * @note This function only support 9bit or 10bit transfer.
883 * Please make sure only 10bit of data is valid and other bits are 0.
884 *
885 * @param base LPUART peripheral base address.
886 * @param data Start address of the data to write.
887 * @param length Size of the data to write.
888 * @retval kStatus_LPUART_Timeout Transmission timed out and was aborted.
889 * @retval kStatus_Success Successfully wrote all data.
890 */
891 status_t LPUART_WriteBlocking16bit(LPUART_Type *base, const uint16_t *data, size_t length);
892
893 /*!
894 * @brief Reads the receiver data register using a blocking method.
895 *
896 * This function polls the receiver register, waits for the receiver register full or receiver FIFO
897 * has data, and reads data from the TX register.
898 *
899 * @param base LPUART peripheral base address.
900 * @param data Start address of the buffer to store the received data.
901 * @param length Size of the buffer.
902 * @retval kStatus_LPUART_RxHardwareOverrun Receiver overrun happened while receiving data.
903 * @retval kStatus_LPUART_NoiseError Noise error happened while receiving data.
904 * @retval kStatus_LPUART_FramingError Framing error happened while receiving data.
905 * @retval kStatus_LPUART_ParityError Parity error happened while receiving data.
906 * @retval kStatus_LPUART_Timeout Transmission timed out and was aborted.
907 * @retval kStatus_Success Successfully received all data.
908 */
909 status_t LPUART_ReadBlocking(LPUART_Type *base, uint8_t *data, size_t length);
910
911 /*!
912 * @brief Reads the receiver data register in 9bit or 10bit mode.
913 *
914 * @note This function only support 9bit or 10bit transfer.
915 *
916 * @param base LPUART peripheral base address.
917 * @param data Start address of the buffer to store the received data by 16bit, only 10bit is valid.
918 * @param length Size of the buffer.
919 * @retval kStatus_LPUART_RxHardwareOverrun Receiver overrun happened while receiving data.
920 * @retval kStatus_LPUART_NoiseError Noise error happened while receiving data.
921 * @retval kStatus_LPUART_FramingError Framing error happened while receiving data.
922 * @retval kStatus_LPUART_ParityError Parity error happened while receiving data.
923 * @retval kStatus_LPUART_Timeout Transmission timed out and was aborted.
924 * @retval kStatus_Success Successfully received all data.
925 */
926 status_t LPUART_ReadBlocking16bit(LPUART_Type *base, uint16_t *data, size_t length);
927
928 /*! @} */
929
930 /*!
931 * @name Transactional
932 * @{
933 */
934
935 /*!
936 * @brief Initializes the LPUART handle.
937 *
938 * This function initializes the LPUART handle, which can be used for other LPUART
939 * transactional APIs. Usually, for a specified LPUART instance,
940 * call this API once to get the initialized handle.
941 *
942 * The LPUART driver supports the "background" receiving, which means that user can set up
943 * an RX ring buffer optionally. Data received is stored into the ring buffer even when the
944 * user doesn't call the LPUART_TransferReceiveNonBlocking() API. If there is already data received
945 * in the ring buffer, the user can get the received data from the ring buffer directly.
946 * The ring buffer is disabled if passing NULL as @p ringBuffer.
947 *
948 * @param base LPUART peripheral base address.
949 * @param handle LPUART handle pointer.
950 * @param callback Callback function.
951 * @param userData User data.
952 */
953 void LPUART_TransferCreateHandle(LPUART_Type *base,
954 lpuart_handle_t *handle,
955 lpuart_transfer_callback_t callback,
956 void *userData);
957 /*!
958 * @brief Transmits a buffer of data using the interrupt method.
959 *
960 * This function send data using an interrupt method. This is a non-blocking function, which
961 * returns directly without waiting for all data written to the transmitter register. When
962 * all data is written to the TX register in the ISR, the LPUART driver calls the callback
963 * function and passes the @ref kStatus_LPUART_TxIdle as status parameter.
964 *
965 * @note The kStatus_LPUART_TxIdle is passed to the upper layer when all data are written
966 * to the TX register. However, there is no check to ensure that all the data sent out. Before disabling the TX,
967 * check the kLPUART_TransmissionCompleteFlag to ensure that the transmit is finished.
968 *
969 * @param base LPUART peripheral base address.
970 * @param handle LPUART handle pointer.
971 * @param xfer LPUART transfer structure, see #lpuart_transfer_t.
972 * @retval kStatus_Success Successfully start the data transmission.
973 * @retval kStatus_LPUART_TxBusy Previous transmission still not finished, data not all written to the TX register.
974 * @retval kStatus_InvalidArgument Invalid argument.
975 */
976 status_t LPUART_TransferSendNonBlocking(LPUART_Type *base, lpuart_handle_t *handle, lpuart_transfer_t *xfer);
977
978 /*!
979 * @brief Sets up the RX ring buffer.
980 *
981 * This function sets up the RX ring buffer to a specific UART handle.
982 *
983 * When the RX ring buffer is used, data received is stored into the ring buffer even when
984 * the user doesn't call the UART_TransferReceiveNonBlocking() API. If there is already data received
985 * in the ring buffer, the user can get the received data from the ring buffer directly.
986 *
987 * @note When using RX ring buffer, one byte is reserved for internal use. In other
988 * words, if @p ringBufferSize is 32, then only 31 bytes are used for saving data.
989 *
990 * @param base LPUART peripheral base address.
991 * @param handle LPUART handle pointer.
992 * @param ringBuffer Start address of ring buffer for background receiving. Pass NULL to disable the ring buffer.
993 * @param ringBufferSize size of the ring buffer.
994 */
995 void LPUART_TransferStartRingBuffer(LPUART_Type *base,
996 lpuart_handle_t *handle,
997 uint8_t *ringBuffer,
998 size_t ringBufferSize);
999
1000 /*!
1001 * @brief Aborts the background transfer and uninstalls the ring buffer.
1002 *
1003 * This function aborts the background transfer and uninstalls the ring buffer.
1004 *
1005 * @param base LPUART peripheral base address.
1006 * @param handle LPUART handle pointer.
1007 */
1008 void LPUART_TransferStopRingBuffer(LPUART_Type *base, lpuart_handle_t *handle);
1009
1010 /*!
1011 * @brief Get the length of received data in RX ring buffer.
1012 *
1013 * @param base LPUART peripheral base address.
1014 * @param handle LPUART handle pointer.
1015 * @return Length of received data in RX ring buffer.
1016 */
1017 size_t LPUART_TransferGetRxRingBufferLength(LPUART_Type *base, lpuart_handle_t *handle);
1018
1019 /*!
1020 * @brief Aborts the interrupt-driven data transmit.
1021 *
1022 * This function aborts the interrupt driven data sending. The user can get the remainBtyes to find out
1023 * how many bytes are not sent out.
1024 *
1025 * @param base LPUART peripheral base address.
1026 * @param handle LPUART handle pointer.
1027 */
1028 void LPUART_TransferAbortSend(LPUART_Type *base, lpuart_handle_t *handle);
1029
1030 /*!
1031 * @brief Gets the number of bytes that have been sent out to bus.
1032 *
1033 * This function gets the number of bytes that have been sent out to bus by an interrupt method.
1034 *
1035 * @param base LPUART peripheral base address.
1036 * @param handle LPUART handle pointer.
1037 * @param count Send bytes count.
1038 * @retval kStatus_NoTransferInProgress No send in progress.
1039 * @retval kStatus_InvalidArgument Parameter is invalid.
1040 * @retval kStatus_Success Get successfully through the parameter \p count;
1041 */
1042 status_t LPUART_TransferGetSendCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count);
1043
1044 /*!
1045 * @brief Receives a buffer of data using the interrupt method.
1046 *
1047 * This function receives data using an interrupt method. This is a non-blocking function
1048 * which returns without waiting to ensure that all data are received.
1049 * If the RX ring buffer is used and not empty, the data in the ring buffer is copied and
1050 * the parameter @p receivedBytes shows how many bytes are copied from the ring buffer.
1051 * After copying, if the data in the ring buffer is not enough for read, the receive
1052 * request is saved by the LPUART driver. When the new data arrives, the receive request
1053 * is serviced first. When all data is received, the LPUART driver notifies the upper layer
1054 * through a callback function and passes a status parameter kStatus_UART_RxIdle.
1055 * For example, the upper layer needs 10 bytes but there are only 5 bytes in ring buffer.
1056 * The 5 bytes are copied to xfer->data, which returns with the
1057 * parameter @p receivedBytes set to 5. For the remaining 5 bytes, the newly arrived data is
1058 * saved from xfer->data[5]. When 5 bytes are received, the LPUART driver notifies the upper layer.
1059 * If the RX ring buffer is not enabled, this function enables the RX and RX interrupt
1060 * to receive data to xfer->data. When all data is received, the upper layer is notified.
1061 *
1062 * @param base LPUART peripheral base address.
1063 * @param handle LPUART handle pointer.
1064 * @param xfer LPUART transfer structure, see uart_transfer_t.
1065 * @param receivedBytes Bytes received from the ring buffer directly.
1066 * @retval kStatus_Success Successfully queue the transfer into the transmit queue.
1067 * @retval kStatus_LPUART_RxBusy Previous receive request is not finished.
1068 * @retval kStatus_InvalidArgument Invalid argument.
1069 */
1070 status_t LPUART_TransferReceiveNonBlocking(LPUART_Type *base,
1071 lpuart_handle_t *handle,
1072 lpuart_transfer_t *xfer,
1073 size_t *receivedBytes);
1074
1075 /*!
1076 * @brief Aborts the interrupt-driven data receiving.
1077 *
1078 * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to find out
1079 * how many bytes not received yet.
1080 *
1081 * @param base LPUART peripheral base address.
1082 * @param handle LPUART handle pointer.
1083 */
1084 void LPUART_TransferAbortReceive(LPUART_Type *base, lpuart_handle_t *handle);
1085
1086 /*!
1087 * @brief Gets the number of bytes that have been received.
1088 *
1089 * This function gets the number of bytes that have been received.
1090 *
1091 * @param base LPUART peripheral base address.
1092 * @param handle LPUART handle pointer.
1093 * @param count Receive bytes count.
1094 * @retval kStatus_NoTransferInProgress No receive in progress.
1095 * @retval kStatus_InvalidArgument Parameter is invalid.
1096 * @retval kStatus_Success Get successfully through the parameter \p count;
1097 */
1098 status_t LPUART_TransferGetReceiveCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count);
1099
1100 /*!
1101 * @brief LPUART IRQ handle function.
1102 *
1103 * This function handles the LPUART transmit and receive IRQ request.
1104 *
1105 * @param base LPUART peripheral base address.
1106 * @param irqHandle LPUART handle pointer.
1107 */
1108 void LPUART_TransferHandleIRQ(LPUART_Type *base, void *irqHandle);
1109
1110 /*!
1111 * @brief LPUART Error IRQ handle function.
1112 *
1113 * This function handles the LPUART error IRQ request.
1114 *
1115 * @param base LPUART peripheral base address.
1116 * @param irqHandle LPUART handle pointer.
1117 */
1118 void LPUART_TransferHandleErrorIRQ(LPUART_Type *base, void *irqHandle);
1119
1120 /*! @} */
1121
1122 #if defined(__cplusplus)
1123 }
1124 #endif
1125
1126 /*! @}*/
1127
1128 #endif /* FSL_LPUART_H_ */
1129