1 /*
2  * Copyright 2022 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
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, 1))
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     const 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     const 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 Tx 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 edmaTxDataToTxRegHandle.
170  * (2) For a shared DMA request source, enable and set the Rx/Tx 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 config transfer parameter while using eDMA.
207  *
208  * This function is preparing to transfer data using eDMA, work with LPSPI_MasterTransferEDMALite.
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  * @param configFlags transfer configuration flags. @ref _lpspi_transfer_config_flag_for_master.
213  * @return Indicates whether LPSPI master transfer was successful or not.
214  * @retval kStatus_Success          Execution successfully.
215  * @retval kStatus_LPSPI_Busy       The LPSPI device is busy.
216  */
217 status_t LPSPI_MasterTransferPrepareEDMALite(LPSPI_Type *base,
218                                              lpspi_master_edma_handle_t *handle,
219                                              uint32_t configFlags);
220 
221 /*!
222  * @brief LPSPI master transfer data using eDMA without configs.
223  *
224  * This function transfers data using eDMA. This is a non-blocking function, which returns right away. When all data
225  * is transferred, the callback function is called.
226  *
227  * Note:
228  * This API is only for transfer through DMA without configuration.
229  * Before calling this API, you must call LPSPI_MasterTransferPrepareEDMALite to configure it once.
230  * The transfer data size should be an integer multiple of bytesPerFrame if bytesPerFrame is less than or equal to 4.
231  * For bytesPerFrame greater than 4:
232  * The transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not an integer multiple of 4.
233  * Otherwise, the transfer data size can be an integer multiple of bytesPerFrame.
234  *
235  * @param base LPSPI peripheral base address.
236  * @param handle pointer to lpspi_master_edma_handle_t structure which stores the transfer state.
237  * @param transfer pointer to lpspi_transfer_t structure, config field is not uesed.
238  * @return Indicates whether LPSPI master transfer was successful or not.
239  * @retval kStatus_Success          Execution successfully.
240  * @retval kStatus_LPSPI_Busy       The LPSPI device is busy.
241  * @retval kStatus_InvalidArgument  The transfer structure is invalid.
242  */
243 status_t LPSPI_MasterTransferEDMALite(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, lpspi_transfer_t *transfer);
244 
245 /*!
246  * @brief LPSPI master aborts a transfer which is using eDMA.
247  *
248  * This function aborts a transfer which is using eDMA.
249  *
250  * @param base LPSPI peripheral base address.
251  * @param handle pointer to lpspi_master_edma_handle_t structure which stores the transfer state.
252  */
253 void LPSPI_MasterTransferAbortEDMA(LPSPI_Type *base, lpspi_master_edma_handle_t *handle);
254 
255 /*!
256  * @brief Gets the master eDMA transfer remaining bytes.
257  *
258  * This function gets the master eDMA transfer remaining bytes.
259  *
260  * @param base LPSPI peripheral base address.
261  * @param handle pointer to lpspi_master_edma_handle_t structure which stores the transfer state.
262  * @param count Number of bytes transferred so far by the EDMA transaction.
263  * @return status of status_t.
264  */
265 status_t LPSPI_MasterTransferGetCountEDMA(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, size_t *count);
266 
267 /*!
268  * @brief Initializes the LPSPI slave eDMA handle.
269  *
270  * This function initializes the LPSPI eDMA handle which can be used for other LPSPI transactional APIs.  Usually, for a
271  * specified LPSPI instance, call this API once to get the initialized handle.
272  *
273  * Note that LPSPI eDMA has a separated (Rx and Tx as two sources) or shared (Rx  and Tx as the same source) DMA request
274  * source.
275  *
276  * (1) For a separated DMA request source, enable and set the Rx DMAMUX source for edmaRxRegToRxDataHandle and
277  * Tx DMAMUX source for edmaTxDataToTxRegHandle.
278  * (2) For a shared DMA request source, enable and set the Rx/Rx DMAMUX source for edmaRxRegToRxDataHandle .
279  *
280  * @param base LPSPI peripheral base address.
281  * @param handle LPSPI handle pointer to lpspi_slave_edma_handle_t.
282  * @param callback LPSPI callback.
283  * @param userData callback function parameter.
284  * @param edmaRxRegToRxDataHandle edmaRxRegToRxDataHandle pointer to edma_handle_t.
285  * @param edmaTxDataToTxRegHandle edmaTxDataToTxRegHandle pointer to edma_handle_t.
286  */
287 void LPSPI_SlaveTransferCreateHandleEDMA(LPSPI_Type *base,
288                                          lpspi_slave_edma_handle_t *handle,
289                                          lpspi_slave_edma_transfer_callback_t callback,
290                                          void *userData,
291                                          edma_handle_t *edmaRxRegToRxDataHandle,
292                                          edma_handle_t *edmaTxDataToTxRegHandle);
293 
294 /*!
295  * @brief LPSPI slave transfers data using eDMA.
296  *
297  * This function transfers data using eDMA. This is a non-blocking function, which return right away. When all data
298  * is transferred, the callback function is called.
299  *
300  * Note:
301  * The transfer data size should be an integer multiple of bytesPerFrame if bytesPerFrame is less than or equal to 4.
302  * For bytesPerFrame greater than 4:
303  * The transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not an integer multiple of 4.
304  * Otherwise, the transfer data size can be an integer multiple of bytesPerFrame.
305  *
306  * @param base LPSPI peripheral base address.
307  * @param handle pointer to lpspi_slave_edma_handle_t structure which stores the transfer state.
308  * @param transfer pointer to lpspi_transfer_t structure.
309  * @return status of status_t.
310  */
311 status_t LPSPI_SlaveTransferEDMA(LPSPI_Type *base, lpspi_slave_edma_handle_t *handle, lpspi_transfer_t *transfer);
312 
313 /*!
314  * @brief LPSPI slave aborts a transfer which is using eDMA.
315  *
316  * This function aborts a transfer which is using eDMA.
317  *
318  * @param base LPSPI peripheral base address.
319  * @param handle pointer to lpspi_slave_edma_handle_t structure which stores the transfer state.
320  */
321 void LPSPI_SlaveTransferAbortEDMA(LPSPI_Type *base, lpspi_slave_edma_handle_t *handle);
322 
323 /*!
324  * @brief Gets the slave eDMA transfer remaining bytes.
325  *
326  * This function gets the slave eDMA transfer remaining bytes.
327  *
328  * @param base LPSPI peripheral base address.
329  * @param handle pointer to lpspi_slave_edma_handle_t structure which stores the transfer state.
330  * @param count Number of bytes transferred so far by the eDMA transaction.
331  * @return status of status_t.
332  */
333 status_t LPSPI_SlaveTransferGetCountEDMA(LPSPI_Type *base, lpspi_slave_edma_handle_t *handle, size_t *count);
334 
335 #if defined(__cplusplus)
336 }
337 #endif
338 
339 /*! @}*/
340 
341 #endif /*FSL_LPSPI_EDMA_H_*/
342