fsl_lpspi_edma.h 14 KB

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