1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef _FSL_LPSPI_EDMA_H_
9 #define _FSL_LPSPI_EDMA_H_
10 
11 #include "fsl_lpspi.h"
12 #include "fsl_edma.h"
13 
14 /*!
15  * @addtogroup lpspi_edma_driver
16  * @{
17  */
18 
19 /***********************************************************************************************************************
20  * Definitions
21  **********************************************************************************************************************/
22 /*! @name Driver version */
23 /*@{*/
24 /*! @brief LPSPI EDMA driver version. */
25 #define FSL_LPSPI_EDMA_DRIVER_VERSION (MAKE_VERSION(2, 1, 0))
26 /*@}*/
27 
28 /*!
29  * @brief Forward declaration of the _lpspi_master_edma_handle typedefs.
30  */
31 typedef struct _lpspi_master_edma_handle lpspi_master_edma_handle_t;
32 
33 /*!
34  * @brief Forward declaration of the _lpspi_slave_edma_handle typedefs.
35  */
36 typedef struct _lpspi_slave_edma_handle lpspi_slave_edma_handle_t;
37 
38 /*!
39  * @brief Completion callback function pointer type.
40  *
41  * @param base LPSPI peripheral base address.
42  * @param handle Pointer to the handle for the LPSPI master.
43  * @param status Success or error code describing whether the transfer completed.
44  * @param userData Arbitrary pointer-dataSized value passed from the application.
45  */
46 typedef void (*lpspi_master_edma_transfer_callback_t)(LPSPI_Type *base,
47                                                       lpspi_master_edma_handle_t *handle,
48                                                       status_t status,
49                                                       void *userData);
50 /*!
51  * @brief Completion callback function pointer type.
52  *
53  * @param base LPSPI peripheral base address.
54  * @param handle Pointer to the handle for the LPSPI slave.
55  * @param status Success or error code describing whether the transfer completed.
56  * @param userData Arbitrary pointer-dataSized value passed from the application.
57  */
58 typedef void (*lpspi_slave_edma_transfer_callback_t)(LPSPI_Type *base,
59                                                      lpspi_slave_edma_handle_t *handle,
60                                                      status_t status,
61                                                      void *userData);
62 
63 /*! @brief LPSPI master eDMA transfer handle structure used for transactional API. */
64 struct _lpspi_master_edma_handle
65 {
66     volatile bool isPcsContinuous; /*!< Is PCS continuous in transfer. */
67 
68     volatile bool isByteSwap; /*!< A flag that whether should byte swap. */
69 
70     volatile uint8_t fifoSize; /*!< FIFO dataSize. */
71 
72     volatile uint8_t rxWatermark; /*!< Rx watermark. */
73 
74     volatile uint8_t bytesEachWrite; /*!< Bytes for each write TDR. */
75     volatile uint8_t bytesEachRead;  /*!< Bytes for each read RDR. */
76 
77     volatile uint8_t bytesLastRead;    /*!< Bytes for last read RDR. */
78     volatile bool isThereExtraRxBytes; /*!< Is there extra RX byte. */
79 
80     uint8_t *volatile txData;             /*!< Send buffer. */
81     uint8_t *volatile rxData;             /*!< Receive buffer. */
82     volatile size_t txRemainingByteCount; /*!< Number of bytes remaining to send.*/
83     volatile size_t rxRemainingByteCount; /*!< Number of bytes remaining to receive.*/
84 
85     volatile uint32_t writeRegRemainingTimes; /*!< Write TDR register remaining times. */
86     volatile uint32_t readRegRemainingTimes;  /*!< Read RDR register remaining times. */
87 
88     uint32_t totalByteCount; /*!< Number of transfer bytes*/
89 
90     uint32_t txBuffIfNull; /*!< Used if there is not txData for DMA purpose.*/
91     uint32_t rxBuffIfNull; /*!< Used if there is not rxData for DMA purpose.*/
92 
93     uint32_t transmitCommand; /*!< Used to write TCR for DMA purpose.*/
94 
95     volatile uint8_t state; /*!< LPSPI transfer state , _lpspi_transfer_state.*/
96 
97     uint8_t nbytes; /*!< eDMA minor byte transfer count initially configured. */
98 
99     lpspi_master_edma_transfer_callback_t callback; /*!< Completion callback. */
100     void *userData;                                 /*!< Callback user data. */
101 
102     edma_handle_t *edmaRxRegToRxDataHandle; /*!<edma_handle_t handle point used for RxReg to RxData buff*/
103     edma_handle_t *edmaTxDataToTxRegHandle; /*!<edma_handle_t handle point used for TxData to TxReg buff*/
104 
105     edma_tcd_t lpspiSoftwareTCD[3]; /*!<SoftwareTCD, internal used*/
106 };
107 
108 /*! @brief LPSPI slave eDMA transfer handle structure used for transactional API.*/
109 struct _lpspi_slave_edma_handle
110 {
111     volatile bool isByteSwap; /*!< A flag that whether should byte swap. */
112 
113     volatile uint8_t fifoSize; /*!< FIFO dataSize. */
114 
115     volatile uint8_t rxWatermark; /*!< Rx watermark. */
116 
117     volatile uint8_t bytesEachWrite; /*!< Bytes for each write TDR. */
118     volatile uint8_t bytesEachRead;  /*!< Bytes for each read RDR. */
119 
120     volatile uint8_t bytesLastRead;    /*!< Bytes for last read RDR. */
121     volatile bool isThereExtraRxBytes; /*!< Is there extra RX byte. */
122 
123     uint8_t nbytes; /*!< eDMA minor byte transfer count initially configured. */
124 
125     uint8_t *volatile txData;             /*!< Send buffer. */
126     uint8_t *volatile rxData;             /*!< Receive buffer. */
127     volatile size_t txRemainingByteCount; /*!< Number of bytes remaining to send.*/
128     volatile size_t rxRemainingByteCount; /*!< Number of bytes remaining to receive.*/
129 
130     volatile uint32_t writeRegRemainingTimes; /*!< Write TDR register remaining times. */
131     volatile uint32_t readRegRemainingTimes;  /*!< Read RDR register remaining times. */
132 
133     uint32_t totalByteCount; /*!< Number of transfer bytes*/
134 
135     uint32_t txBuffIfNull; /*!< Used if there is not txData for DMA purpose.*/
136     uint32_t rxBuffIfNull; /*!< Used if there is not rxData for DMA purpose.*/
137 
138     volatile uint8_t state; /*!< LPSPI transfer state.*/
139 
140     uint32_t errorCount; /*!< Error count for slave transfer.*/
141 
142     lpspi_slave_edma_transfer_callback_t callback; /*!< Completion callback. */
143     void *userData;                                /*!< Callback user data. */
144 
145     edma_handle_t *edmaRxRegToRxDataHandle; /*!<edma_handle_t handle point used for RxReg to RxData buff*/
146     edma_handle_t *edmaTxDataToTxRegHandle; /*!<edma_handle_t handle point used for TxData to TxReg*/
147 
148     edma_tcd_t lpspiSoftwareTCD[2]; /*!<SoftwareTCD, internal used*/
149 };
150 
151 /***********************************************************************************************************************
152  * API
153  **********************************************************************************************************************/
154 #if defined(__cplusplus)
155 extern "C" {
156 #endif /*_cplusplus*/
157 
158 /*Transactional APIs*/
159 
160 /*!
161  * @brief Initializes the LPSPI master eDMA handle.
162  *
163  * This function initializes the LPSPI eDMA handle which can be used for other LPSPI transactional APIs.  Usually, for a
164  * specified LPSPI instance, call this API once to get the initialized handle.
165  *
166  * Note that the LPSPI eDMA has a separated (Rx and Rx as two sources) or shared (Rx  and Tx are the same source) DMA
167  * request source.
168  * (1) For a separated DMA request source, enable and set the Rx DMAMUX source for edmaRxRegToRxDataHandle and
169  * Tx DMAMUX source for edmaIntermediaryToTxRegHandle.
170  * (2) For a shared DMA request source, enable and set the Rx/Rx DMAMUX source for edmaRxRegToRxDataHandle.
171  *
172  * @param base LPSPI peripheral base address.
173  * @param handle LPSPI handle pointer to lpspi_master_edma_handle_t.
174  * @param callback LPSPI callback.
175  * @param userData callback function parameter.
176  * @param edmaRxRegToRxDataHandle edmaRxRegToRxDataHandle pointer to edma_handle_t.
177  * @param edmaTxDataToTxRegHandle edmaTxDataToTxRegHandle pointer to edma_handle_t.
178  */
179 void LPSPI_MasterTransferCreateHandleEDMA(LPSPI_Type *base,
180                                           lpspi_master_edma_handle_t *handle,
181                                           lpspi_master_edma_transfer_callback_t callback,
182                                           void *userData,
183                                           edma_handle_t *edmaRxRegToRxDataHandle,
184                                           edma_handle_t *edmaTxDataToTxRegHandle);
185 
186 /*!
187  * @brief LPSPI master transfer data using eDMA.
188  *
189  * This function transfers data using eDMA. This is a non-blocking function, which returns right away. When all data
190  * is transferred, the callback function is called.
191  *
192  * Note:
193  * The transfer data size should be an integer multiple of bytesPerFrame if bytesPerFrame is less than or equal to 4.
194  * For bytesPerFrame greater than 4:
195  * The transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not an integer multiple of 4.
196  * Otherwise, the transfer data size can be an integer multiple of bytesPerFrame.
197  *
198  * @param base LPSPI peripheral base address.
199  * @param handle pointer to lpspi_master_edma_handle_t structure which stores the transfer state.
200  * @param transfer pointer to lpspi_transfer_t structure.
201  * @return status of status_t.
202  */
203 status_t LPSPI_MasterTransferEDMA(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, lpspi_transfer_t *transfer);
204 
205 /*!
206  * @brief LPSPI master aborts a transfer which is using eDMA.
207  *
208  * This function aborts a transfer which is using eDMA.
209  *
210  * @param base LPSPI peripheral base address.
211  * @param handle pointer to lpspi_master_edma_handle_t structure which stores the transfer state.
212  */
213 void LPSPI_MasterTransferAbortEDMA(LPSPI_Type *base, lpspi_master_edma_handle_t *handle);
214 
215 /*!
216  * @brief Gets the master eDMA transfer remaining bytes.
217  *
218  * This function gets the master eDMA transfer remaining bytes.
219  *
220  * @param base LPSPI peripheral base address.
221  * @param handle pointer to lpspi_master_edma_handle_t structure which stores the transfer state.
222  * @param count Number of bytes transferred so far by the EDMA transaction.
223  * @return status of status_t.
224  */
225 status_t LPSPI_MasterTransferGetCountEDMA(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, size_t *count);
226 
227 /*!
228  * @brief Initializes the LPSPI slave eDMA handle.
229  *
230  * This function initializes the LPSPI eDMA handle which can be used for other LPSPI transactional APIs.  Usually, for a
231  * specified LPSPI instance, call this API once to get the initialized handle.
232  *
233  * Note that LPSPI eDMA has a separated (Rx and Tx as two sources) or shared (Rx  and Tx as the same source) DMA request
234  * source.
235  *
236  * (1) For a separated DMA request source, enable and set the Rx DMAMUX source for edmaRxRegToRxDataHandle and
237  * Tx DMAMUX source for edmaTxDataToTxRegHandle.
238  * (2) For a shared DMA request source, enable and set the Rx/Rx DMAMUX source for edmaRxRegToRxDataHandle .
239  *
240  * @param base LPSPI peripheral base address.
241  * @param handle LPSPI handle pointer to lpspi_slave_edma_handle_t.
242  * @param callback LPSPI callback.
243  * @param userData callback function parameter.
244  * @param edmaRxRegToRxDataHandle edmaRxRegToRxDataHandle pointer to edma_handle_t.
245  * @param edmaTxDataToTxRegHandle edmaTxDataToTxRegHandle pointer to edma_handle_t.
246  */
247 void LPSPI_SlaveTransferCreateHandleEDMA(LPSPI_Type *base,
248                                          lpspi_slave_edma_handle_t *handle,
249                                          lpspi_slave_edma_transfer_callback_t callback,
250                                          void *userData,
251                                          edma_handle_t *edmaRxRegToRxDataHandle,
252                                          edma_handle_t *edmaTxDataToTxRegHandle);
253 
254 /*!
255  * @brief LPSPI slave transfers data using eDMA.
256  *
257  * This function transfers data using eDMA. This is a non-blocking function, which return right away. When all data
258  * is transferred, the callback function is called.
259  *
260  * Note:
261  * The transfer data size should be an integer multiple of bytesPerFrame if bytesPerFrame is less than or equal to 4.
262  * For bytesPerFrame greater than 4:
263  * The transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not an integer multiple of 4.
264  * Otherwise, the transfer data size can be an integer multiple of bytesPerFrame.
265  *
266  * @param base LPSPI peripheral base address.
267  * @param handle pointer to lpspi_slave_edma_handle_t structure which stores the transfer state.
268  * @param transfer pointer to lpspi_transfer_t structure.
269  * @return status of status_t.
270  */
271 status_t LPSPI_SlaveTransferEDMA(LPSPI_Type *base, lpspi_slave_edma_handle_t *handle, lpspi_transfer_t *transfer);
272 
273 /*!
274  * @brief LPSPI slave aborts a transfer which is using eDMA.
275  *
276  * This function aborts a transfer which is using eDMA.
277  *
278  * @param base LPSPI peripheral base address.
279  * @param handle pointer to lpspi_slave_edma_handle_t structure which stores the transfer state.
280  */
281 void LPSPI_SlaveTransferAbortEDMA(LPSPI_Type *base, lpspi_slave_edma_handle_t *handle);
282 
283 /*!
284  * @brief Gets the slave eDMA transfer remaining bytes.
285  *
286  * This function gets the slave eDMA transfer remaining bytes.
287  *
288  * @param base LPSPI peripheral base address.
289  * @param handle pointer to lpspi_slave_edma_handle_t structure which stores the transfer state.
290  * @param count Number of bytes transferred so far by the eDMA transaction.
291  * @return status of status_t.
292  */
293 status_t LPSPI_SlaveTransferGetCountEDMA(LPSPI_Type *base, lpspi_slave_edma_handle_t *handle, size_t *count);
294 
295 #if defined(__cplusplus)
296 }
297 #endif
298 
299 /*! @}*/
300 
301 #endif /*_FSL_LPSPI_EDMA_H_*/
302