fsl_dspi_edma.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_DSPI_EDMA_H_
  31. #define _FSL_DSPI_EDMA_H_
  32. #include "fsl_dspi.h"
  33. #include "fsl_edma.h"
  34. /*!
  35. * @addtogroup dspi_edma_driver
  36. * @{
  37. */
  38. /***********************************************************************************************************************
  39. * Definitions
  40. **********************************************************************************************************************/
  41. /*!
  42. * @brief Forward declaration of the DSPI eDMA master handle typedefs.
  43. */
  44. typedef struct _dspi_master_edma_handle dspi_master_edma_handle_t;
  45. /*!
  46. * @brief Forward declaration of the DSPI eDMA slave handle typedefs.
  47. */
  48. typedef struct _dspi_slave_edma_handle dspi_slave_edma_handle_t;
  49. /*!
  50. * @brief Completion callback function pointer type.
  51. *
  52. * @param base DSPI peripheral base address.
  53. * @param handle A pointer to the handle for the DSPI master.
  54. * @param status Success or error code describing whether the transfer completed.
  55. * @param userData An arbitrary pointer-dataSized value passed from the application.
  56. */
  57. typedef void (*dspi_master_edma_transfer_callback_t)(SPI_Type *base,
  58. dspi_master_edma_handle_t *handle,
  59. status_t status,
  60. void *userData);
  61. /*!
  62. * @brief Completion callback function pointer type.
  63. *
  64. * @param base DSPI peripheral base address.
  65. * @param handle A pointer to the handle for the DSPI slave.
  66. * @param status Success or error code describing whether the transfer completed.
  67. * @param userData An arbitrary pointer-dataSized value passed from the application.
  68. */
  69. typedef void (*dspi_slave_edma_transfer_callback_t)(SPI_Type *base,
  70. dspi_slave_edma_handle_t *handle,
  71. status_t status,
  72. void *userData);
  73. /*! @brief DSPI master eDMA transfer handle structure used for the transactional API. */
  74. struct _dspi_master_edma_handle
  75. {
  76. uint32_t bitsPerFrame; /*!< The desired number of bits per frame. */
  77. volatile uint32_t command; /*!< The desired data command. */
  78. volatile uint32_t lastCommand; /*!< The desired last data command. */
  79. uint8_t fifoSize; /*!< FIFO dataSize. */
  80. volatile bool
  81. isPcsActiveAfterTransfer; /*!< Indicates whether the PCS signal keeps active after the last frame transfer.*/
  82. uint8_t nbytes; /*!< eDMA minor byte transfer count initially configured. */
  83. volatile uint8_t state; /*!< DSPI transfer state , _dspi_transfer_state.*/
  84. uint8_t *volatile txData; /*!< Send buffer. */
  85. uint8_t *volatile rxData; /*!< Receive buffer. */
  86. volatile size_t remainingSendByteCount; /*!< A number of bytes remaining to send.*/
  87. volatile size_t remainingReceiveByteCount; /*!< A number of bytes remaining to receive.*/
  88. size_t totalByteCount; /*!< A number of transfer bytes*/
  89. uint32_t rxBuffIfNull; /*!< Used if there is not rxData for DMA purpose.*/
  90. uint32_t txBuffIfNull; /*!< Used if there is not txData for DMA purpose.*/
  91. dspi_master_edma_transfer_callback_t callback; /*!< Completion callback. */
  92. void *userData; /*!< Callback user data. */
  93. edma_handle_t *edmaRxRegToRxDataHandle; /*!<edma_handle_t handle point used for RxReg to RxData buff*/
  94. edma_handle_t *edmaTxDataToIntermediaryHandle; /*!<edma_handle_t handle point used for TxData to Intermediary*/
  95. edma_handle_t *edmaIntermediaryToTxRegHandle; /*!<edma_handle_t handle point used for Intermediary to TxReg*/
  96. edma_tcd_t dspiSoftwareTCD[2]; /*!<SoftwareTCD , internal used*/
  97. };
  98. /*! @brief DSPI slave eDMA transfer handle structure used for the transactional API.*/
  99. struct _dspi_slave_edma_handle
  100. {
  101. uint32_t bitsPerFrame; /*!< The desired number of bits per frame. */
  102. uint8_t *volatile txData; /*!< Send buffer. */
  103. uint8_t *volatile rxData; /*!< Receive buffer. */
  104. volatile size_t remainingSendByteCount; /*!< A number of bytes remaining to send.*/
  105. volatile size_t remainingReceiveByteCount; /*!< A number of bytes remaining to receive.*/
  106. size_t totalByteCount; /*!< A number of transfer bytes*/
  107. uint32_t rxBuffIfNull; /*!< Used if there is not rxData for DMA purpose.*/
  108. uint32_t txBuffIfNull; /*!< Used if there is not txData for DMA purpose.*/
  109. uint32_t txLastData; /*!< Used if there is an extra byte when 16bits per frame for DMA purpose.*/
  110. uint8_t nbytes; /*!< eDMA minor byte transfer count initially configured. */
  111. volatile uint8_t state; /*!< DSPI transfer state.*/
  112. dspi_slave_edma_transfer_callback_t callback; /*!< Completion callback. */
  113. void *userData; /*!< Callback user data. */
  114. edma_handle_t *edmaRxRegToRxDataHandle; /*!<edma_handle_t handle point used for RxReg to RxData buff*/
  115. edma_handle_t *edmaTxDataToTxRegHandle; /*!<edma_handle_t handle point used for TxData to TxReg*/
  116. };
  117. /***********************************************************************************************************************
  118. * API
  119. **********************************************************************************************************************/
  120. #if defined(__cplusplus)
  121. extern "C" {
  122. #endif /*_cplusplus*/
  123. /*Transactional APIs*/
  124. /*!
  125. * @brief Initializes the DSPI master eDMA handle.
  126. *
  127. * This function initializes the DSPI eDMA handle which can be used for other DSPI transactional APIs. Usually, for a
  128. * specified DSPI instance, call this API once to get the initialized handle.
  129. *
  130. * Note that DSPI eDMA has separated (RX and TX as two sources) or shared (RX and TX are the same source) DMA request
  131. * source.
  132. * (1) For the separated DMA request source, enable and set the RX DMAMUX source for edmaRxRegToRxDataHandle and
  133. * TX DMAMUX source for edmaIntermediaryToTxRegHandle.
  134. * (2) For the shared DMA request source, enable and set the RX/RX DMAMUX source for the edmaRxRegToRxDataHandle.
  135. *
  136. * @param base DSPI peripheral base address.
  137. * @param handle DSPI handle pointer to dspi_master_edma_handle_t.
  138. * @param callback DSPI callback.
  139. * @param userData A callback function parameter.
  140. * @param edmaRxRegToRxDataHandle edmaRxRegToRxDataHandle pointer to edma_handle_t.
  141. * @param edmaTxDataToIntermediaryHandle edmaTxDataToIntermediaryHandle pointer to edma_handle_t.
  142. * @param edmaIntermediaryToTxRegHandle edmaIntermediaryToTxRegHandle pointer to edma_handle_t.
  143. */
  144. void DSPI_MasterTransferCreateHandleEDMA(SPI_Type *base,
  145. dspi_master_edma_handle_t *handle,
  146. dspi_master_edma_transfer_callback_t callback,
  147. void *userData,
  148. edma_handle_t *edmaRxRegToRxDataHandle,
  149. edma_handle_t *edmaTxDataToIntermediaryHandle,
  150. edma_handle_t *edmaIntermediaryToTxRegHandle);
  151. /*!
  152. * @brief DSPI master transfer data using eDMA.
  153. *
  154. * This function transfers data using eDMA. This is a non-blocking function, which returns right away. When all data
  155. * is transferred, the callback function is called.
  156. *
  157. * @param base DSPI peripheral base address.
  158. * @param handle A pointer to the dspi_master_edma_handle_t structure which stores the transfer state.
  159. * @param transfer A pointer to the dspi_transfer_t structure.
  160. * @return status of status_t.
  161. */
  162. status_t DSPI_MasterTransferEDMA(SPI_Type *base, dspi_master_edma_handle_t *handle, dspi_transfer_t *transfer);
  163. /*!
  164. * @brief DSPI master aborts a transfer which is using eDMA.
  165. *
  166. * This function aborts a transfer which is using eDMA.
  167. *
  168. * @param base DSPI peripheral base address.
  169. * @param handle A pointer to the dspi_master_edma_handle_t structure which stores the transfer state.
  170. */
  171. void DSPI_MasterTransferAbortEDMA(SPI_Type *base, dspi_master_edma_handle_t *handle);
  172. /*!
  173. * @brief Gets the master eDMA transfer count.
  174. *
  175. * This function gets the master eDMA transfer count.
  176. *
  177. * @param base DSPI peripheral base address.
  178. * @param handle A pointer to the dspi_master_edma_handle_t structure which stores the transfer state.
  179. * @param count A number of bytes transferred by the non-blocking transaction.
  180. * @return status of status_t.
  181. */
  182. status_t DSPI_MasterTransferGetCountEDMA(SPI_Type *base, dspi_master_edma_handle_t *handle, size_t *count);
  183. /*!
  184. * @brief Initializes the DSPI slave eDMA handle.
  185. *
  186. * This function initializes the DSPI eDMA handle which can be used for other DSPI transactional APIs. Usually, for a
  187. * specified DSPI instance, call this API once to get the initialized handle.
  188. *
  189. * Note that DSPI eDMA has separated (RN and TX in 2 sources) or shared (RX and TX are the same source) DMA request
  190. * source.
  191. * (1)For the separated DMA request source, enable and set the RX DMAMUX source for edmaRxRegToRxDataHandle and
  192. * TX DMAMUX source for edmaTxDataToTxRegHandle.
  193. * (2)For the shared DMA request source, enable and set the RX/RX DMAMUX source for the edmaRxRegToRxDataHandle.
  194. *
  195. * @param base DSPI peripheral base address.
  196. * @param handle DSPI handle pointer to dspi_slave_edma_handle_t.
  197. * @param callback DSPI callback.
  198. * @param userData A callback function parameter.
  199. * @param edmaRxRegToRxDataHandle edmaRxRegToRxDataHandle pointer to edma_handle_t.
  200. * @param edmaTxDataToTxRegHandle edmaTxDataToTxRegHandle pointer to edma_handle_t.
  201. */
  202. void DSPI_SlaveTransferCreateHandleEDMA(SPI_Type *base,
  203. dspi_slave_edma_handle_t *handle,
  204. dspi_slave_edma_transfer_callback_t callback,
  205. void *userData,
  206. edma_handle_t *edmaRxRegToRxDataHandle,
  207. edma_handle_t *edmaTxDataToTxRegHandle);
  208. /*!
  209. * @brief DSPI slave transfer data using eDMA.
  210. *
  211. * This function transfers data using eDMA. This is a non-blocking function, which returns right away. When all data
  212. * is transferred, the callback function is called.
  213. * Note that the slave eDMA transfer doesn't support transfer_size is 1 when the bitsPerFrame is greater
  214. * than eight.
  215. * @param base DSPI peripheral base address.
  216. * @param handle A pointer to the dspi_slave_edma_handle_t structure which stores the transfer state.
  217. * @param transfer A pointer to the dspi_transfer_t structure.
  218. * @return status of status_t.
  219. */
  220. status_t DSPI_SlaveTransferEDMA(SPI_Type *base, dspi_slave_edma_handle_t *handle, dspi_transfer_t *transfer);
  221. /*!
  222. * @brief DSPI slave aborts a transfer which is using eDMA.
  223. *
  224. * This function aborts a transfer which is using eDMA.
  225. *
  226. * @param base DSPI peripheral base address.
  227. * @param handle A pointer to the dspi_slave_edma_handle_t structure which stores the transfer state.
  228. */
  229. void DSPI_SlaveTransferAbortEDMA(SPI_Type *base, dspi_slave_edma_handle_t *handle);
  230. /*!
  231. * @brief Gets the slave eDMA transfer count.
  232. *
  233. * This function gets the slave eDMA transfer count.
  234. *
  235. * @param base DSPI peripheral base address.
  236. * @param handle A pointer to the dspi_slave_edma_handle_t structure which stores the transfer state.
  237. * @param count A number of bytes transferred so far by the non-blocking transaction.
  238. * @return status of status_t.
  239. */
  240. status_t DSPI_SlaveTransferGetCountEDMA(SPI_Type *base, dspi_slave_edma_handle_t *handle, size_t *count);
  241. #if defined(__cplusplus)
  242. }
  243. #endif /*_cplusplus*/
  244. /*!
  245. *@}
  246. */
  247. #endif /*_FSL_DSPI_EDMA_H_*/