fsl_lpspi_edma.h 13 KB

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