]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32M1_Vega_GCC_Eclipse/common/rv32m1_sdk_riscv/devices/RV32M1/drivers/fsl_lpuart.h
Move the 'generic' version of freertos_risc_v_chip_specific_extensions.h back to...
[freertos] / FreeRTOS / Demo / RISC-V_RV32M1_Vega_GCC_Eclipse / common / rv32m1_sdk_riscv / devices / RV32M1 / drivers / fsl_lpuart.h
1 /*
2  * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 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 2.2.3. */
25 #define FSL_LPUART_DRIVER_VERSION (MAKE_VERSION(2, 2, 4))
26 /*@}*/
27
28 /*! @brief Error codes for the LPUART driver. */
29 enum _lpuart_status
30 {
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. */
48 };
49
50 /*! @brief LPUART parity mode. */
51 typedef enum _lpuart_parity_mode
52 {
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;
57
58 /*! @brief LPUART data bits count. */
59 typedef enum _lpuart_data_bits
60 {
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 */
64 #endif
65 } lpuart_data_bits_t;
66
67 /*! @brief LPUART stop bit count. */
68 typedef enum _lpuart_stop_bit_count
69 {
70     kLPUART_OneStopBit = 0U, /*!< One stop bit */
71     kLPUART_TwoStopBit = 1U, /*!< Two stop bits */
72 } lpuart_stop_bit_count_t;
73
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
77 {
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;
81
82 /*! @brief LPUART transmit CTS configure. */
83 typedef enum _lpuart_transmit_cts_config
84 {
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;
88 #endif
89
90 /*! @brief LPUART idle flag type defines when the receiver starts counting. */
91 typedef enum _lpuart_idle_type_select
92 {
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;
96
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.
100  */
101 typedef enum _lpuart_idle_config
102 {
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;
112
113 /*!
114  * @brief LPUART interrupt configuration structure, default settings all disabled.
115  *
116  * This structure contains the settings for all LPUART interrupt configurations.
117  */
118 enum _lpuart_interrupt_enable
119 {
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. */
122 #endif
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. */
135 #endif
136 };
137
138 /*!
139  * @brief LPUART status flags.
140  *
141  * This provides constants for the LPUART status flags for use in the LPUART functions.
142  */
143 enum _lpuart_flags
144 {
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 */
162 #endif
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*/
170 #endif
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 */
176 #endif
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 */
184 #endif
185 };
186
187 /*! @brief LPUART configuration structure. */
188 typedef struct _lpuart_config
189 {
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  */
196 #endif
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 */
200 #endif
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 */
206 #endif
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 */
211 } lpuart_config_t;
212
213 /*! @brief LPUART transfer structure. */
214 typedef struct _lpuart_transfer
215 {
216     uint8_t *data;   /*!< The buffer of data to be transfer.*/
217     size_t dataSize; /*!< The byte count to be transfer. */
218 } lpuart_transfer_t;
219
220 /* Forward declaration of the handle typedef. */
221 typedef struct _lpuart_handle lpuart_handle_t;
222
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);
225
226 /*! @brief LPUART handle structure. */
227 struct _lpuart_handle
228 {
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. */
235
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. */
240
241     lpuart_transfer_callback_t callback; /*!< Callback function. */
242     void *userData;                      /*!< LPUART callback function parameter.*/
243
244     volatile uint8_t txState; /*!< TX transfer state. */
245     volatile uint8_t rxState; /*!< RX transfer state. */
246
247 #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
248     bool isSevenDataBits; /*!< Seven data bits flag. */
249 #endif
250 };
251
252 /*******************************************************************************
253  * API
254  ******************************************************************************/
255
256 #if defined(__cplusplus)
257 extern "C" {
258 #endif /* _cplusplus */
259
260 #if defined(FSL_FEATURE_LPUART_HAS_GLOBAL) && FSL_FEATURE_LPUART_HAS_GLOBAL
261
262 /*!
263  * @name Software Reset
264  * @{
265  */
266
267 /*!
268  * @brief Resets the LPUART using software.
269  *
270  * This function resets all internal logic and registers except the Global Register.
271  * Remains set until cleared by software.
272  *
273  * @param base LPUART peripheral base address.
274  */
275 static inline void LPUART_SoftwareReset(LPUART_Type *base)
276 {
277     base->GLOBAL |= LPUART_GLOBAL_RST_MASK;
278     base->GLOBAL &= ~LPUART_GLOBAL_RST_MASK;
279 }
280 /* @} */
281 #endif /*FSL_FEATURE_LPUART_HAS_GLOBAL*/
282
283 /*!
284  * @name Initialization and deinitialization
285  * @{
286  */
287
288 /*!
289  * @brief Initializes an LPUART instance with the user configuration structure and the peripheral clock.
290  *
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.
294  * @code
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);
304  * @endcode
305  *
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
311  */
312 status_t LPUART_Init(LPUART_Type *base, const lpuart_config_t *config, uint32_t srcClock_Hz);
313
314 /*!
315  * @brief Deinitializes a LPUART instance.
316  *
317  * This function waits for transmit to complete, disables TX and RX, and disables the LPUART clock.
318  *
319  * @param base LPUART peripheral base address.
320  */
321 void LPUART_Deinit(LPUART_Type *base);
322
323 /*!
324  * @brief Gets the default configuration structure.
325  *
326  * This function initializes the LPUART configuration structure to a default value. The default
327  * values are:
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;
339  *
340  * @param config Pointer to a configuration structure.
341  */
342 void LPUART_GetDefaultConfig(lpuart_config_t *config);
343
344 /*!
345  * @brief Sets the LPUART instance baudrate.
346  *
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.
349  * @code
350  *  LPUART_SetBaudRate(LPUART1, 115200U, 20000000U);
351  * @endcode
352  *
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.
358  */
359 status_t LPUART_SetBaudRate(LPUART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz);
360
361 /* @} */
362
363 /*!
364  * @name Status
365  * @{
366  */
367
368 /*!
369  * @brief Gets LPUART status flags.
370  *
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:
375  * @code
376  *     if (kLPUART_TxDataRegEmptyFlag & LPUART_GetStatusFlags(LPUART1))
377  *     {
378  *         ...
379  *     }
380  * @endcode
381  *
382  * @param base LPUART peripheral base address.
383  * @return LPUART status flags which are ORed by the enumerators in the _lpuart_flags.
384  */
385 uint32_t LPUART_GetStatusFlags(LPUART_Type *base);
386
387 /*!
388  * @brief Clears status flags with a provided mask.
389  *
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.
397  *
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.
405  */
406 status_t LPUART_ClearStatusFlags(LPUART_Type *base, uint32_t mask);
407
408 /* @} */
409
410 /*!
411  * @name Interrupts
412  * @{
413  */
414
415 /*!
416  * @brief Enables LPUART interrupts according to a provided mask.
417  *
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:
421  * @code
422  *     LPUART_EnableInterrupts(LPUART1,kLPUART_TxDataRegEmptyInterruptEnable | kLPUART_RxDataRegFullInterruptEnable);
423  * @endcode
424  *
425  * @param base LPUART peripheral base address.
426  * @param mask The interrupts to enable. Logical OR of @ref _uart_interrupt_enable.
427  */
428 void LPUART_EnableInterrupts(LPUART_Type *base, uint32_t mask);
429
430 /*!
431  * @brief Disables  LPUART interrupts according to a provided mask.
432  *
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:
436  * @code
437  *     LPUART_DisableInterrupts(LPUART1,kLPUART_TxDataRegEmptyInterruptEnable | kLPUART_RxDataRegFullInterruptEnable);
438  * @endcode
439  *
440  * @param base LPUART peripheral base address.
441  * @param mask The interrupts to disable. Logical OR of @ref _lpuart_interrupt_enable.
442  */
443 void LPUART_DisableInterrupts(LPUART_Type *base, uint32_t mask);
444
445 /*!
446  * @brief Gets enabled LPUART interrupts.
447  *
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:
453  * @code
454  *     uint32_t enabledInterrupts = LPUART_GetEnabledInterrupts(LPUART1);
455  *
456  *     if (kLPUART_TxDataRegEmptyInterruptEnable & enabledInterrupts)
457  *     {
458  *         ...
459  *     }
460  * @endcode
461  *
462  * @param base LPUART peripheral base address.
463  * @return LPUART interrupt flags which are logical OR of the enumerators in @ref _lpuart_interrupt_enable.
464  */
465 uint32_t LPUART_GetEnabledInterrupts(LPUART_Type *base);
466
467 #if defined(FSL_FEATURE_LPUART_HAS_DMA_ENABLE) && FSL_FEATURE_LPUART_HAS_DMA_ENABLE
468 /*!
469  * @brief Gets the LPUART data register address.
470  *
471  * This function returns the LPUART data register address, which is mainly used by the DMA/eDMA.
472  *
473  * @param base LPUART peripheral base address.
474  * @return LPUART data register addresses which are used both by the transmitter and receiver.
475  */
476 static inline uint32_t LPUART_GetDataRegisterAddress(LPUART_Type *base)
477 {
478     return (uint32_t) & (base->DATA);
479 }
480
481 /*!
482  * @brief Enables or disables the LPUART transmitter DMA request.
483  *
484  * This function enables or disables the transmit data register empty flag, STAT[TDRE], to generate DMA requests.
485  *
486  * @param base LPUART peripheral base address.
487  * @param enable True to enable, false to disable.
488  */
489 static inline void LPUART_EnableTxDMA(LPUART_Type *base, bool enable)
490 {
491     if (enable)
492     {
493         base->BAUD |= LPUART_BAUD_TDMAE_MASK;
494         base->CTRL |= LPUART_CTRL_TIE_MASK;
495     }
496     else
497     {
498         base->BAUD &= ~LPUART_BAUD_TDMAE_MASK;
499         base->CTRL &= ~LPUART_CTRL_TIE_MASK;
500     }
501 }
502
503 /*!
504  * @brief Enables or disables the LPUART receiver DMA.
505  *
506  * This function enables or disables the receiver data register full flag, STAT[RDRF], to generate DMA requests.
507  *
508  * @param base LPUART peripheral base address.
509  * @param enable True to enable, false to disable.
510  */
511 static inline void LPUART_EnableRxDMA(LPUART_Type *base, bool enable)
512 {
513     if (enable)
514     {
515         base->BAUD |= LPUART_BAUD_RDMAE_MASK;
516         base->CTRL |= LPUART_CTRL_RIE_MASK;
517     }
518     else
519     {
520         base->BAUD &= ~LPUART_BAUD_RDMAE_MASK;
521         base->CTRL &= ~LPUART_CTRL_RIE_MASK;
522     }
523 }
524
525 /* @} */
526 #endif /* FSL_FEATURE_LPUART_HAS_DMA_ENABLE */
527
528 /*!
529  * @name Bus Operations
530  * @{
531  */
532
533 /*!
534  * @brief Enables or disables the LPUART transmitter.
535  *
536  * This function enables or disables the LPUART transmitter.
537  *
538  * @param base LPUART peripheral base address.
539  * @param enable True to enable, false to disable.
540  */
541 static inline void LPUART_EnableTx(LPUART_Type *base, bool enable)
542 {
543     if (enable)
544     {
545         base->CTRL |= LPUART_CTRL_TE_MASK;
546     }
547     else
548     {
549         base->CTRL &= ~LPUART_CTRL_TE_MASK;
550     }
551 }
552
553 /*!
554  * @brief Enables or disables the LPUART receiver.
555  *
556  * This function enables or disables the LPUART receiver.
557  *
558  * @param base LPUART peripheral base address.
559  * @param enable True to enable, false to disable.
560  */
561 static inline void LPUART_EnableRx(LPUART_Type *base, bool enable)
562 {
563     if (enable)
564     {
565         base->CTRL |= LPUART_CTRL_RE_MASK;
566     }
567     else
568     {
569         base->CTRL &= ~LPUART_CTRL_RE_MASK;
570     }
571 }
572
573 /*!
574  * @brief Writes to the transmitter register.
575  *
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.
578  *
579  * @param base LPUART peripheral base address.
580  * @param data Data write to the TX register.
581  */
582 static inline void LPUART_WriteByte(LPUART_Type *base, uint8_t data)
583 {
584     base->DATA = data;
585 }
586
587 /*!
588  * @brief Reads the receiver register.
589  *
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.
592  *
593  * @param base LPUART peripheral base address.
594  * @return Data read from data register.
595  */
596 static inline uint8_t LPUART_ReadByte(LPUART_Type *base)
597 {
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)));
603
604     if (isSevenDataBits)
605     {
606         return (base->DATA & 0x7F);
607     }
608     else
609     {
610         return base->DATA;
611     }
612 #else
613     return base->DATA;
614 #endif
615 }
616
617 /*!
618  * @brief Writes to the transmitter register using a blocking method.
619  *
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.
622  *
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
625  * finished.
626  *
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.
630  */
631 void LPUART_WriteBlocking(LPUART_Type *base, const uint8_t *data, size_t length);
632
633 /*!
634 * @brief Reads the receiver data register using a blocking method.
635  *
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.
638  *
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.
647  */
648 status_t LPUART_ReadBlocking(LPUART_Type *base, uint8_t *data, size_t length);
649
650 /* @} */
651
652 /*!
653  * @name Transactional
654  * @{
655  */
656
657 /*!
658  * @brief Initializes the LPUART handle.
659  *
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.
663  *
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.
669  *
670  * @param base LPUART peripheral base address.
671  * @param handle LPUART handle pointer.
672  * @param callback Callback function.
673  * @param userData User data.
674  */
675 void LPUART_TransferCreateHandle(LPUART_Type *base,
676                                  lpuart_handle_t *handle,
677                                  lpuart_transfer_callback_t callback,
678                                  void *userData);
679 /*!
680  * @brief Transmits a buffer of data using the interrupt method.
681  *
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.
686  *
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.
690  *
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.
697  */
698 status_t LPUART_TransferSendNonBlocking(LPUART_Type *base, lpuart_handle_t *handle, lpuart_transfer_t *xfer);
699
700 /*!
701  * @brief Sets up the RX ring buffer.
702  *
703  * This function sets up the RX ring buffer to a specific UART handle.
704  *
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.
708  *
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.
711  *
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.
716  */
717 void LPUART_TransferStartRingBuffer(LPUART_Type *base,
718                                     lpuart_handle_t *handle,
719                                     uint8_t *ringBuffer,
720                                     size_t ringBufferSize);
721
722 /*!
723  * @brief Aborts the background transfer and uninstalls the ring buffer.
724  *
725  * This function aborts the background transfer and uninstalls the ring buffer.
726  *
727  * @param base LPUART peripheral base address.
728  * @param handle LPUART handle pointer.
729  */
730 void LPUART_TransferStopRingBuffer(LPUART_Type *base, lpuart_handle_t *handle);
731
732 /*!
733  * @brief Aborts the interrupt-driven data transmit.
734  *
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.
737  *
738  * @param base LPUART peripheral base address.
739  * @param handle LPUART handle pointer.
740  */
741 void LPUART_TransferAbortSend(LPUART_Type *base, lpuart_handle_t *handle);
742
743 /*!
744  * @brief Gets the number of bytes that have been written to the LPUART transmitter register.
745  *
746  * This function gets the number of bytes that have been written to LPUART TX
747  * register by an interrupt method.
748  *
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;
755  */
756 status_t LPUART_TransferGetSendCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count);
757
758 /*!
759  * @brief Receives a buffer of data using the interrupt method.
760  *
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.
775  *
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.
783  */
784 status_t LPUART_TransferReceiveNonBlocking(LPUART_Type *base,
785                                            lpuart_handle_t *handle,
786                                            lpuart_transfer_t *xfer,
787                                            size_t *receivedBytes);
788
789 /*!
790  * @brief Aborts the interrupt-driven data receiving.
791  *
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.
794  *
795  * @param base LPUART peripheral base address.
796  * @param handle LPUART handle pointer.
797  */
798 void LPUART_TransferAbortReceive(LPUART_Type *base, lpuart_handle_t *handle);
799
800 /*!
801  * @brief Gets the number of bytes that have been received.
802  *
803  * This function gets the number of bytes that have been received.
804  *
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;
811  */
812 status_t LPUART_TransferGetReceiveCount(LPUART_Type *base, lpuart_handle_t *handle, uint32_t *count);
813
814 /*!
815  * @brief LPUART IRQ handle function.
816  *
817  * This function handles the LPUART transmit and receive IRQ request.
818  *
819  * @param base LPUART peripheral base address.
820  * @param handle LPUART handle pointer.
821  */
822 void LPUART_TransferHandleIRQ(LPUART_Type *base, lpuart_handle_t *handle);
823
824 /*!
825  * @brief LPUART Error IRQ handle function.
826  *
827  * This function handles the LPUART error IRQ request.
828  *
829  * @param base LPUART peripheral base address.
830  * @param handle LPUART handle pointer.
831  */
832 void LPUART_TransferHandleErrorIRQ(LPUART_Type *base, lpuart_handle_t *handle);
833
834 /* @} */
835
836 #if defined(__cplusplus)
837 }
838 #endif
839
840 /*! @}*/
841
842 #endif /* _FSL_LPUART_H_ */