fsl_lpspi_edma.h 15 KB

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