1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef _FSL_USART_DMA_H_
9 #define _FSL_USART_DMA_H_
10 
11 #include "fsl_common.h"
12 #include "fsl_dma.h"
13 #include "fsl_usart.h"
14 
15 /*!
16  * @addtogroup usart_dma_driver
17  * @{
18  */
19 
20 /*! @file */
21 
22 /*******************************************************************************
23  * Definitions
24  ******************************************************************************/
25 
26 /*! @name Driver version */
27 /*@{*/
28 /*! @brief USART dma driver version. */
29 #define FSL_USART_DMA_DRIVER_VERSION (MAKE_VERSION(2, 6, 0))
30 /*@}*/
31 
32 /* Forward declaration of the handle typedef. */
33 typedef struct _usart_dma_handle usart_dma_handle_t;
34 
35 /*! @brief UART transfer callback function. */
36 typedef void (*usart_dma_transfer_callback_t)(USART_Type *base,
37                                               usart_dma_handle_t *handle,
38                                               status_t status,
39                                               void *userData);
40 
41 /*!
42  * @brief UART DMA handle
43  */
44 struct _usart_dma_handle
45 {
46     USART_Type *base; /*!< UART peripheral base address. */
47 
48     usart_dma_transfer_callback_t callback; /*!< Callback function. */
49     void *userData;                         /*!< UART callback function parameter.*/
50     size_t rxDataSizeAll;                   /*!< Size of the data to receive. */
51     size_t txDataSizeAll;                   /*!< Size of the data to send out. */
52 
53     dma_handle_t *txDmaHandle; /*!< The DMA TX channel used. */
54     dma_handle_t *rxDmaHandle; /*!< The DMA RX channel used. */
55 
56     volatile uint8_t txState; /*!< TX transfer state. */
57     volatile uint8_t rxState; /*!< RX transfer state */
58 };
59 
60 /*******************************************************************************
61  * API
62  ******************************************************************************/
63 
64 #if defined(__cplusplus)
65 extern "C" {
66 #endif /* _cplusplus */
67 
68 /*!
69  * @name DMA transactional
70  * @{
71  */
72 
73 /*!
74  * @brief Initializes the USART handle which is used in transactional functions.
75  * @param base USART peripheral base address.
76  * @param handle Pointer to usart_dma_handle_t structure.
77  * @param callback Callback function.
78  * @param userData User data.
79  * @param txDmaHandle User-requested DMA handle for TX DMA transfer.
80  * @param rxDmaHandle User-requested DMA handle for RX DMA transfer.
81  */
82 status_t USART_TransferCreateHandleDMA(USART_Type *base,
83                                        usart_dma_handle_t *handle,
84                                        usart_dma_transfer_callback_t callback,
85                                        void *userData,
86                                        dma_handle_t *txDmaHandle,
87                                        dma_handle_t *rxDmaHandle);
88 
89 /*!
90  * @brief Sends data using DMA.
91  *
92  * This function sends data using DMA. This is a non-blocking function, which returns
93  * right away. When all data is sent, the send callback function is called.
94  *
95  * @param base USART peripheral base address.
96  * @param handle USART handle pointer.
97  * @param xfer USART DMA transfer structure. See #usart_transfer_t.
98  * @retval kStatus_Success if succeed, others failed.
99  * @retval kStatus_USART_TxBusy Previous transfer on going.
100  * @retval kStatus_InvalidArgument Invalid argument.
101  */
102 status_t USART_TransferSendDMA(USART_Type *base, usart_dma_handle_t *handle, usart_transfer_t *xfer);
103 
104 /*!
105  * @brief Receives data using DMA.
106  *
107  * This function receives data using DMA. This is a non-blocking function, which returns
108  * right away. When all data is received, the receive callback function is called.
109  *
110  * @param base USART peripheral base address.
111  * @param handle Pointer to usart_dma_handle_t structure.
112  * @param xfer USART DMA transfer structure. See #usart_transfer_t.
113  * @retval kStatus_Success if succeed, others failed.
114  * @retval kStatus_USART_RxBusy Previous transfer on going.
115  * @retval kStatus_InvalidArgument Invalid argument.
116  */
117 status_t USART_TransferReceiveDMA(USART_Type *base, usart_dma_handle_t *handle, usart_transfer_t *xfer);
118 
119 /*!
120  * @brief Aborts the sent data using DMA.
121  *
122  * This function aborts send data using DMA.
123  *
124  * @param base USART peripheral base address
125  * @param handle Pointer to usart_dma_handle_t structure
126  */
127 void USART_TransferAbortSendDMA(USART_Type *base, usart_dma_handle_t *handle);
128 
129 /*!
130  * @brief Aborts the received data using DMA.
131  *
132  * This function aborts the received data using DMA.
133  *
134  * @param base USART peripheral base address
135  * @param handle Pointer to usart_dma_handle_t structure
136  */
137 void USART_TransferAbortReceiveDMA(USART_Type *base, usart_dma_handle_t *handle);
138 
139 /*!
140  * @brief Get the number of bytes that have been received.
141  *
142  * This function gets the number of bytes that have been received.
143  *
144  * @param base USART peripheral base address.
145  * @param handle USART handle pointer.
146  * @param count Receive bytes count.
147  * @retval kStatus_NoTransferInProgress No receive in progress.
148  * @retval kStatus_InvalidArgument Parameter is invalid.
149  * @retval kStatus_Success Get successfully through the parameter \p count;
150  */
151 status_t USART_TransferGetReceiveCountDMA(USART_Type *base, usart_dma_handle_t *handle, uint32_t *count);
152 
153 /*!
154  * @brief Get the number of bytes that have been sent.
155  *
156  * This function gets the number of bytes that have been sent.
157  *
158  * @param base USART peripheral base address.
159  * @param handle USART handle pointer.
160  * @param count Sent bytes count.
161  * @retval kStatus_NoTransferInProgress No receive in progress.
162  * @retval kStatus_InvalidArgument Parameter is invalid.
163  * @retval kStatus_Success Get successfully through the parameter \p count;
164  */
165 status_t USART_TransferGetSendCountDMA(USART_Type *base, usart_dma_handle_t *handle, uint32_t *count);
166 
167 /* @} */
168 
169 #if defined(__cplusplus)
170 }
171 #endif
172 
173 /*! @}*/
174 
175 #endif /* _FSL_USART_DMA_H_ */
176