fsl_spi_dma.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, 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_SPI_DMA_H_
  35. #define _FSL_SPI_DMA_H_
  36. #include "fsl_dma.h"
  37. #include "fsl_spi.h"
  38. /*!
  39. * @addtogroup spi_dma_driver
  40. * @{
  41. */
  42. /*! @file */
  43. /*******************************************************************************
  44. * Definitions
  45. ******************************************************************************/
  46. /*! @name Driver version */
  47. /*@{*/
  48. /*! @brief SPI DMA driver version 2.0.2. */
  49. #define FSL_SPI_DMA_DRIVER_VERSION (MAKE_VERSION(2, 0, 2))
  50. /*@}*/
  51. typedef struct _spi_dma_handle spi_dma_handle_t;
  52. /*! @brief SPI DMA callback called at the end of transfer. */
  53. typedef void (*spi_dma_callback_t)(SPI_Type *base, spi_dma_handle_t *handle, status_t status, void *userData);
  54. /*! @brief SPI DMA transfer handle, users should not touch the content of the handle.*/
  55. struct _spi_dma_handle
  56. {
  57. volatile bool txInProgress; /*!< Send transfer finished */
  58. volatile bool rxInProgress; /*!< Receive transfer finished */
  59. dma_handle_t *txHandle; /*!< DMA handler for SPI send */
  60. dma_handle_t *rxHandle; /*!< DMA handler for SPI receive */
  61. uint8_t bytesPerFrame; /*!< Bytes in a frame for SPI tranfer */
  62. spi_dma_callback_t callback; /*!< Callback for SPI DMA transfer */
  63. void *userData; /*!< User Data for SPI DMA callback */
  64. uint32_t state; /*!< Internal state of SPI DMA transfer */
  65. size_t transferSize; /*!< Bytes need to be transfer */
  66. };
  67. /*******************************************************************************
  68. * APIs
  69. ******************************************************************************/
  70. #if defined(__cplusplus)
  71. extern "C" {
  72. #endif
  73. /*!
  74. * @name DMA Transactional
  75. * @{
  76. */
  77. /*!
  78. * @brief Initialize the SPI master DMA handle.
  79. *
  80. * This function initializes the SPI master DMA handle which can be used for other SPI master transactional APIs.
  81. * Usually, for a specified SPI instance, user need only call this API once to get the initialized handle.
  82. *
  83. * @param base SPI peripheral base address.
  84. * @param handle SPI handle pointer.
  85. * @param callback User callback function called at the end of a transfer.
  86. * @param userData User data for callback.
  87. * @param txHandle DMA handle pointer for SPI Tx, the handle shall be static allocated by users.
  88. * @param rxHandle DMA handle pointer for SPI Rx, the handle shall be static allocated by users.
  89. */
  90. status_t SPI_MasterTransferCreateHandleDMA(SPI_Type *base,
  91. spi_dma_handle_t *handle,
  92. spi_dma_callback_t callback,
  93. void *userData,
  94. dma_handle_t *txHandle,
  95. dma_handle_t *rxHandle);
  96. /*!
  97. * @brief Perform a non-blocking SPI transfer using DMA.
  98. *
  99. * @note This interface returned immediately after transfer initiates, users should call
  100. * SPI_GetTransferStatus to poll the transfer status to check whether SPI transfer finished.
  101. *
  102. * @param base SPI peripheral base address.
  103. * @param handle SPI DMA handle pointer.
  104. * @param xfer Pointer to dma transfer structure.
  105. * @retval kStatus_Success Successfully start a transfer.
  106. * @retval kStatus_InvalidArgument Input argument is invalid.
  107. * @retval kStatus_SPI_Busy SPI is not idle, is running another transfer.
  108. */
  109. status_t SPI_MasterTransferDMA(SPI_Type *base, spi_dma_handle_t *handle, spi_transfer_t *xfer);
  110. /*!
  111. * @brief Transfers a block of data using a DMA method.
  112. *
  113. * This function using polling way to do the first half transimission and using DMA way to
  114. * do the srcond half transimission, the transfer mechanism is half-duplex.
  115. * When do the second half transimission, code will return right away. When all data is transferred,
  116. * the callback function is called.
  117. *
  118. * @param base SPI base pointer
  119. * @param handle A pointer to the spi_master_dma_handle_t structure which stores the transfer state.
  120. * @param transfer A pointer to the spi_half_duplex_transfer_t structure.
  121. * @return status of status_t.
  122. */
  123. status_t SPI_MasterHalfDuplexTransferDMA(SPI_Type *base, spi_dma_handle_t *handle, spi_half_duplex_transfer_t *xfer);
  124. /*!
  125. * @brief Initialize the SPI slave DMA handle.
  126. *
  127. * This function initializes the SPI slave DMA handle which can be used for other SPI master transactional APIs.
  128. * Usually, for a specified SPI instance, user need only call this API once to get the initialized handle.
  129. *
  130. * @param base SPI peripheral base address.
  131. * @param handle SPI handle pointer.
  132. * @param callback User callback function called at the end of a transfer.
  133. * @param userData User data for callback.
  134. * @param txHandle DMA handle pointer for SPI Tx, the handle shall be static allocated by users.
  135. * @param rxHandle DMA handle pointer for SPI Rx, the handle shall be static allocated by users.
  136. */
  137. static inline status_t SPI_SlaveTransferCreateHandleDMA(SPI_Type *base,
  138. spi_dma_handle_t *handle,
  139. spi_dma_callback_t callback,
  140. void *userData,
  141. dma_handle_t *txHandle,
  142. dma_handle_t *rxHandle)
  143. {
  144. return SPI_MasterTransferCreateHandleDMA(base, handle, callback, userData, txHandle, rxHandle);
  145. }
  146. /*!
  147. * @brief Perform a non-blocking SPI transfer using DMA.
  148. *
  149. * @note This interface returned immediately after transfer initiates, users should call
  150. * SPI_GetTransferStatus to poll the transfer status to check whether SPI transfer finished.
  151. *
  152. * @param base SPI peripheral base address.
  153. * @param handle SPI DMA handle pointer.
  154. * @param xfer Pointer to dma transfer structure.
  155. * @retval kStatus_Success Successfully start a transfer.
  156. * @retval kStatus_InvalidArgument Input argument is invalid.
  157. * @retval kStatus_SPI_Busy SPI is not idle, is running another transfer.
  158. */
  159. static inline status_t SPI_SlaveTransferDMA(SPI_Type *base, spi_dma_handle_t *handle, spi_transfer_t *xfer)
  160. {
  161. return SPI_MasterTransferDMA(base, handle, xfer);
  162. }
  163. /*!
  164. * @brief Abort a SPI transfer using DMA.
  165. *
  166. * @param base SPI peripheral base address.
  167. * @param handle SPI DMA handle pointer.
  168. */
  169. void SPI_MasterTransferAbortDMA(SPI_Type *base, spi_dma_handle_t *handle);
  170. /*!
  171. * @brief Gets the master DMA transfer remaining bytes.
  172. *
  173. * This function gets the master DMA transfer remaining bytes.
  174. *
  175. * @param base SPI peripheral base address.
  176. * @param handle A pointer to the spi_dma_handle_t structure which stores the transfer state.
  177. * @param count A number of bytes transferred by the non-blocking transaction.
  178. * @return status of status_t.
  179. */
  180. status_t SPI_MasterTransferGetCountDMA(SPI_Type *base, spi_dma_handle_t *handle, size_t *count);
  181. /*!
  182. * @brief Abort a SPI transfer using DMA.
  183. *
  184. * @param base SPI peripheral base address.
  185. * @param handle SPI DMA handle pointer.
  186. */
  187. static inline void SPI_SlaveTransferAbortDMA(SPI_Type *base, spi_dma_handle_t *handle)
  188. {
  189. SPI_MasterTransferAbortDMA(base, handle);
  190. }
  191. /*!
  192. * @brief Gets the slave DMA transfer remaining bytes.
  193. *
  194. * This function gets the slave DMA transfer remaining bytes.
  195. *
  196. * @param base SPI peripheral base address.
  197. * @param handle A pointer to the spi_dma_handle_t structure which stores the transfer state.
  198. * @param count A number of bytes transferred by the non-blocking transaction.
  199. * @return status of status_t.
  200. */
  201. static inline status_t SPI_SlaveTransferGetCountDMA(SPI_Type *base, spi_dma_handle_t *handle, size_t *count)
  202. {
  203. return SPI_MasterTransferGetCountDMA(base, handle, count);
  204. }
  205. /*! @} */
  206. #if defined(__cplusplus)
  207. }
  208. #endif
  209. /*! @} */
  210. #endif /* _FSL_SPI_DMA_H_*/