fsl_dspi_edma.h 14 KB

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