1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2020, 2022 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8 #ifndef FSL_FLEXIO_SPI_EDMA_H_
9 #define FSL_FLEXIO_SPI_EDMA_H_
10
11 #include "fsl_flexio_spi.h"
12 #include "fsl_edma.h"
13
14 /*!
15 * @addtogroup flexio_edma_spi
16 * @{
17 */
18
19 /*******************************************************************************
20 * Definitions
21 ******************************************************************************/
22
23 /*! @name Driver version */
24 /*! @{ */
25 /*! @brief FlexIO SPI EDMA driver version. */
26 #define FSL_FLEXIO_SPI_EDMA_DRIVER_VERSION (MAKE_VERSION(2, 3, 0))
27 /*! @} */
28
29 /*! @brief typedef for flexio_spi_master_edma_handle_t in advance. */
30 typedef struct _flexio_spi_master_edma_handle flexio_spi_master_edma_handle_t;
31
32 /*! @brief Slave handle is the same with master handle. */
33 typedef flexio_spi_master_edma_handle_t flexio_spi_slave_edma_handle_t;
34
35 /*! @brief FlexIO SPI master callback for finished transmit */
36 typedef void (*flexio_spi_master_edma_transfer_callback_t)(FLEXIO_SPI_Type *base,
37 flexio_spi_master_edma_handle_t *handle,
38 status_t status,
39 void *userData);
40
41 /*! @brief FlexIO SPI slave callback for finished transmit */
42 typedef void (*flexio_spi_slave_edma_transfer_callback_t)(FLEXIO_SPI_Type *base,
43 flexio_spi_slave_edma_handle_t *handle,
44 status_t status,
45 void *userData);
46
47 /*! @brief FlexIO SPI eDMA transfer handle, users should not touch the content of the handle.*/
48 struct _flexio_spi_master_edma_handle
49 {
50 size_t transferSize; /*!< Total bytes to be transferred. */
51 uint8_t nbytes; /*!< eDMA minor byte transfer count initially configured. */
52 bool txInProgress; /*!< Send transfer in progress */
53 bool rxInProgress; /*!< Receive transfer in progress */
54 edma_handle_t *txHandle; /*!< DMA handler for SPI send */
55 edma_handle_t *rxHandle; /*!< DMA handler for SPI receive */
56 flexio_spi_master_edma_transfer_callback_t callback; /*!< Callback for SPI DMA transfer */
57 void *userData; /*!< User Data for SPI DMA callback */
58 };
59
60 /*******************************************************************************
61 * APIs
62 ******************************************************************************/
63 #if defined(__cplusplus)
64 extern "C" {
65 #endif
66
67 /*!
68 * @name eDMA Transactional
69 * @{
70 */
71
72 /*!
73 * @brief Initializes the FlexIO SPI master eDMA handle.
74 *
75 * This function initializes the FlexIO SPI master eDMA handle which can be used for other FlexIO SPI master
76 * transactional
77 * APIs.
78 * For a specified FlexIO SPI instance, call this API once to get the initialized handle.
79 *
80 * @param base Pointer to FLEXIO_SPI_Type structure.
81 * @param handle Pointer to flexio_spi_master_edma_handle_t structure to store the transfer state.
82 * @param callback SPI callback, NULL means no callback.
83 * @param userData callback function parameter.
84 * @param txHandle User requested eDMA handle for FlexIO SPI RX eDMA transfer.
85 * @param rxHandle User requested eDMA handle for FlexIO SPI TX eDMA transfer.
86 * @retval kStatus_Success Successfully create the handle.
87 * @retval kStatus_OutOfRange The FlexIO SPI eDMA type/handle table out of range.
88 */
89 status_t FLEXIO_SPI_MasterTransferCreateHandleEDMA(FLEXIO_SPI_Type *base,
90 flexio_spi_master_edma_handle_t *handle,
91 flexio_spi_master_edma_transfer_callback_t callback,
92 void *userData,
93 edma_handle_t *txHandle,
94 edma_handle_t *rxHandle);
95
96 /*!
97 * @brief Performs a non-blocking FlexIO SPI transfer using eDMA.
98 *
99 * @note This interface returns immediately after transfer initiates. Call
100 * FLEXIO_SPI_MasterGetTransferCountEDMA to poll the transfer status and check
101 * whether the FlexIO SPI transfer is finished.
102 *
103 * @param base Pointer to FLEXIO_SPI_Type structure.
104 * @param handle Pointer to flexio_spi_master_edma_handle_t structure to store the transfer state.
105 * @param xfer Pointer to FlexIO SPI transfer structure.
106 * @retval kStatus_Success Successfully start a transfer.
107 * @retval kStatus_InvalidArgument Input argument is invalid.
108 * @retval kStatus_FLEXIO_SPI_Busy FlexIO SPI is not idle, is running another transfer.
109 */
110 status_t FLEXIO_SPI_MasterTransferEDMA(FLEXIO_SPI_Type *base,
111 flexio_spi_master_edma_handle_t *handle,
112 flexio_spi_transfer_t *xfer);
113
114 /*!
115 * @brief Aborts a FlexIO SPI transfer using eDMA.
116 *
117 * @param base Pointer to FLEXIO_SPI_Type structure.
118 * @param handle FlexIO SPI eDMA handle pointer.
119 */
120 void FLEXIO_SPI_MasterTransferAbortEDMA(FLEXIO_SPI_Type *base, flexio_spi_master_edma_handle_t *handle);
121
122 /*!
123 * @brief Gets the number of bytes transferred so far using FlexIO SPI master eDMA.
124 *
125 * @param base Pointer to FLEXIO_SPI_Type structure.
126 * @param handle FlexIO SPI eDMA handle pointer.
127 * @param count Number of bytes transferred so far by the non-blocking transaction.
128 */
129 status_t FLEXIO_SPI_MasterTransferGetCountEDMA(FLEXIO_SPI_Type *base,
130 flexio_spi_master_edma_handle_t *handle,
131 size_t *count);
132
133 /*!
134 * @brief Initializes the FlexIO SPI slave eDMA handle.
135 *
136 * This function initializes the FlexIO SPI slave eDMA handle.
137 *
138 * @param base Pointer to FLEXIO_SPI_Type structure.
139 * @param handle Pointer to flexio_spi_slave_edma_handle_t structure to store the transfer state.
140 * @param callback SPI callback, NULL means no callback.
141 * @param userData callback function parameter.
142 * @param txHandle User requested eDMA handle for FlexIO SPI TX eDMA transfer.
143 * @param rxHandle User requested eDMA handle for FlexIO SPI RX eDMA transfer.
144 */
FLEXIO_SPI_SlaveTransferCreateHandleEDMA(FLEXIO_SPI_Type * base,flexio_spi_slave_edma_handle_t * handle,flexio_spi_slave_edma_transfer_callback_t callback,void * userData,edma_handle_t * txHandle,edma_handle_t * rxHandle)145 static inline void FLEXIO_SPI_SlaveTransferCreateHandleEDMA(FLEXIO_SPI_Type *base,
146 flexio_spi_slave_edma_handle_t *handle,
147 flexio_spi_slave_edma_transfer_callback_t callback,
148 void *userData,
149 edma_handle_t *txHandle,
150 edma_handle_t *rxHandle)
151 {
152 (void)FLEXIO_SPI_MasterTransferCreateHandleEDMA(base, handle, callback, userData, txHandle, rxHandle);
153 }
154
155 /*!
156 * @brief Performs a non-blocking FlexIO SPI transfer using eDMA.
157 *
158 * @note This interface returns immediately after transfer initiates. Call
159 * FLEXIO_SPI_SlaveGetTransferCountEDMA to poll the transfer status and
160 * check whether the FlexIO SPI transfer is finished.
161 *
162 * @param base Pointer to FLEXIO_SPI_Type structure.
163 * @param handle Pointer to flexio_spi_slave_edma_handle_t structure to store the transfer state.
164 * @param xfer Pointer to FlexIO SPI transfer structure.
165 * @retval kStatus_Success Successfully start a transfer.
166 * @retval kStatus_InvalidArgument Input argument is invalid.
167 * @retval kStatus_FLEXIO_SPI_Busy FlexIO SPI is not idle, is running another transfer.
168 */
169 status_t FLEXIO_SPI_SlaveTransferEDMA(FLEXIO_SPI_Type *base,
170 flexio_spi_slave_edma_handle_t *handle,
171 flexio_spi_transfer_t *xfer);
172
173 /*!
174 * @brief Aborts a FlexIO SPI transfer using eDMA.
175 *
176 * @param base Pointer to FLEXIO_SPI_Type structure.
177 * @param handle Pointer to flexio_spi_slave_edma_handle_t structure to store the transfer state.
178 */
FLEXIO_SPI_SlaveTransferAbortEDMA(FLEXIO_SPI_Type * base,flexio_spi_slave_edma_handle_t * handle)179 static inline void FLEXIO_SPI_SlaveTransferAbortEDMA(FLEXIO_SPI_Type *base, flexio_spi_slave_edma_handle_t *handle)
180 {
181 FLEXIO_SPI_MasterTransferAbortEDMA(base, handle);
182 }
183
184 /*!
185 * @brief Gets the number of bytes transferred so far using FlexIO SPI slave eDMA.
186 *
187 * @param base Pointer to FLEXIO_SPI_Type structure.
188 * @param handle FlexIO SPI eDMA handle pointer.
189 * @param count Number of bytes transferred so far by the non-blocking transaction.
190 */
FLEXIO_SPI_SlaveTransferGetCountEDMA(FLEXIO_SPI_Type * base,flexio_spi_slave_edma_handle_t * handle,size_t * count)191 static inline status_t FLEXIO_SPI_SlaveTransferGetCountEDMA(FLEXIO_SPI_Type *base,
192 flexio_spi_slave_edma_handle_t *handle,
193 size_t *count)
194 {
195 return FLEXIO_SPI_MasterTransferGetCountEDMA(base, handle, count);
196 }
197
198 /*! @} */
199
200 #if defined(__cplusplus)
201 }
202 #endif
203
204 /*!
205 * @}
206 */
207 #endif
208