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