fsl_flexio_spi_edma.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #include "fsl_flexio_spi_edma.h"
  31. /*******************************************************************************
  32. * Definitons
  33. ******************************************************************************/
  34. /*<! Structure definition for spi_edma_private_handle_t. The structure is private. */
  35. typedef struct _flexio_spi_master_edma_private_handle
  36. {
  37. FLEXIO_SPI_Type *base;
  38. flexio_spi_master_edma_handle_t *handle;
  39. } flexio_spi_master_edma_private_handle_t;
  40. /*******************************************************************************
  41. * Prototypes
  42. ******************************************************************************/
  43. /*!
  44. * @brief EDMA callback function for FLEXIO SPI send transfer.
  45. *
  46. * @param handle EDMA handle pointer.
  47. * @param param Callback function parameter.
  48. */
  49. static void FLEXIO_SPI_TxEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds);
  50. /*!
  51. * @brief EDMA callback function for FLEXIO SPI receive transfer.
  52. *
  53. * @param handle EDMA handle pointer.
  54. * @param param Callback function parameter.
  55. */
  56. static void FLEXIO_SPI_RxEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds);
  57. /*!
  58. * @brief EDMA config for FLEXIO SPI transfer.
  59. *
  60. * @param base pointer to FLEXIO_SPI_Type structure.
  61. * @param handle pointer to flexio_spi_master_edma_handle_t structure to store the transfer state.
  62. * @param xfer Pointer to flexio spi transfer structure.
  63. */
  64. static void FLEXIO_SPI_EDMAConfig(FLEXIO_SPI_Type *base,
  65. flexio_spi_master_edma_handle_t *handle,
  66. flexio_spi_transfer_t *xfer);
  67. /*******************************************************************************
  68. * Variables
  69. ******************************************************************************/
  70. /* Dummy data used to send */
  71. static const uint16_t s_dummyData = FLEXIO_SPI_DUMMYDATA;
  72. /*< @brief user configurable flexio spi handle count. */
  73. #define FLEXIO_SPI_HANDLE_COUNT 2
  74. /*<! Private handle only used for internally. */
  75. static flexio_spi_master_edma_private_handle_t s_edmaPrivateHandle[FLEXIO_SPI_HANDLE_COUNT];
  76. /*******************************************************************************
  77. * Code
  78. ******************************************************************************/
  79. static void FLEXIO_SPI_TxEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  80. {
  81. tcds = tcds;
  82. flexio_spi_master_edma_private_handle_t *spiPrivateHandle = (flexio_spi_master_edma_private_handle_t *)param;
  83. /* Disable Tx DMA */
  84. if (transferDone)
  85. {
  86. FLEXIO_SPI_EnableDMA(spiPrivateHandle->base, kFLEXIO_SPI_TxDmaEnable, false);
  87. /* change the state */
  88. spiPrivateHandle->handle->txInProgress = false;
  89. /* All finished, call the callback */
  90. if ((spiPrivateHandle->handle->txInProgress == false) && (spiPrivateHandle->handle->rxInProgress == false))
  91. {
  92. if (spiPrivateHandle->handle->callback)
  93. {
  94. (spiPrivateHandle->handle->callback)(spiPrivateHandle->base, spiPrivateHandle->handle, kStatus_Success,
  95. spiPrivateHandle->handle->userData);
  96. }
  97. }
  98. }
  99. }
  100. static void FLEXIO_SPI_RxEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  101. {
  102. tcds = tcds;
  103. flexio_spi_master_edma_private_handle_t *spiPrivateHandle = (flexio_spi_master_edma_private_handle_t *)param;
  104. if (transferDone)
  105. {
  106. /* Disable Rx dma */
  107. FLEXIO_SPI_EnableDMA(spiPrivateHandle->base, kFLEXIO_SPI_RxDmaEnable, false);
  108. /* change the state */
  109. spiPrivateHandle->handle->rxInProgress = false;
  110. /* All finished, call the callback */
  111. if ((spiPrivateHandle->handle->txInProgress == false) && (spiPrivateHandle->handle->rxInProgress == false))
  112. {
  113. if (spiPrivateHandle->handle->callback)
  114. {
  115. (spiPrivateHandle->handle->callback)(spiPrivateHandle->base, spiPrivateHandle->handle, kStatus_Success,
  116. spiPrivateHandle->handle->userData);
  117. }
  118. }
  119. }
  120. }
  121. static void FLEXIO_SPI_EDMAConfig(FLEXIO_SPI_Type *base,
  122. flexio_spi_master_edma_handle_t *handle,
  123. flexio_spi_transfer_t *xfer)
  124. {
  125. edma_transfer_config_t xferConfig;
  126. flexio_spi_shift_direction_t direction;
  127. uint8_t bytesPerFrame;
  128. /* Configure the values in handle. */
  129. switch (xfer->flags)
  130. {
  131. case kFLEXIO_SPI_8bitMsb:
  132. bytesPerFrame = 1;
  133. direction = kFLEXIO_SPI_MsbFirst;
  134. break;
  135. case kFLEXIO_SPI_8bitLsb:
  136. bytesPerFrame = 1;
  137. direction = kFLEXIO_SPI_LsbFirst;
  138. break;
  139. case kFLEXIO_SPI_16bitMsb:
  140. bytesPerFrame = 2;
  141. direction = kFLEXIO_SPI_MsbFirst;
  142. break;
  143. case kFLEXIO_SPI_16bitLsb:
  144. bytesPerFrame = 2;
  145. direction = kFLEXIO_SPI_LsbFirst;
  146. break;
  147. default:
  148. bytesPerFrame = 1U;
  149. direction = kFLEXIO_SPI_MsbFirst;
  150. assert(true);
  151. break;
  152. }
  153. /* Save total transfer size. */
  154. handle->transferSize = xfer->dataSize;
  155. /* Configure tx transfer EDMA. */
  156. xferConfig.destAddr = FLEXIO_SPI_GetTxDataRegisterAddress(base, direction);
  157. xferConfig.destOffset = 0;
  158. if (bytesPerFrame == 1U)
  159. {
  160. xferConfig.srcTransferSize = kEDMA_TransferSize1Bytes;
  161. xferConfig.destTransferSize = kEDMA_TransferSize1Bytes;
  162. xferConfig.minorLoopBytes = 1;
  163. }
  164. else
  165. {
  166. if (direction == kFLEXIO_SPI_MsbFirst)
  167. {
  168. xferConfig.destAddr -= 1U;
  169. }
  170. xferConfig.srcTransferSize = kEDMA_TransferSize2Bytes;
  171. xferConfig.destTransferSize = kEDMA_TransferSize2Bytes;
  172. xferConfig.minorLoopBytes = 2;
  173. }
  174. /* Configure DMA channel. */
  175. if (xfer->txData)
  176. {
  177. xferConfig.srcOffset = bytesPerFrame;
  178. xferConfig.srcAddr = (uint32_t)(xfer->txData);
  179. }
  180. else
  181. {
  182. /* Disable the source increasement and source set to dummyData. */
  183. xferConfig.srcOffset = 0;
  184. xferConfig.srcAddr = (uint32_t)(&s_dummyData);
  185. }
  186. xferConfig.majorLoopCounts = (xfer->dataSize / xferConfig.minorLoopBytes);
  187. /* Store the initially configured eDMA minor byte transfer count into the FLEXIO SPI handle */
  188. handle->nbytes = xferConfig.minorLoopBytes;
  189. if (handle->txHandle)
  190. {
  191. EDMA_SubmitTransfer(handle->txHandle, &xferConfig);
  192. }
  193. /* Configure tx transfer EDMA. */
  194. if (xfer->rxData)
  195. {
  196. xferConfig.srcAddr = FLEXIO_SPI_GetRxDataRegisterAddress(base, direction);
  197. if (bytesPerFrame == 2U)
  198. {
  199. if (direction == kFLEXIO_SPI_LsbFirst)
  200. {
  201. xferConfig.srcAddr -= 1U;
  202. }
  203. }
  204. xferConfig.srcOffset = 0;
  205. xferConfig.destAddr = (uint32_t)(xfer->rxData);
  206. xferConfig.destOffset = bytesPerFrame;
  207. EDMA_SubmitTransfer(handle->rxHandle, &xferConfig);
  208. handle->rxInProgress = true;
  209. FLEXIO_SPI_EnableDMA(base, kFLEXIO_SPI_RxDmaEnable, true);
  210. EDMA_StartTransfer(handle->rxHandle);
  211. }
  212. /* Always start Tx transfer. */
  213. if (handle->txHandle)
  214. {
  215. handle->txInProgress = true;
  216. FLEXIO_SPI_EnableDMA(base, kFLEXIO_SPI_TxDmaEnable, true);
  217. EDMA_StartTransfer(handle->txHandle);
  218. }
  219. }
  220. status_t FLEXIO_SPI_MasterTransferCreateHandleEDMA(FLEXIO_SPI_Type *base,
  221. flexio_spi_master_edma_handle_t *handle,
  222. flexio_spi_master_edma_transfer_callback_t callback,
  223. void *userData,
  224. edma_handle_t *txHandle,
  225. edma_handle_t *rxHandle)
  226. {
  227. assert(handle);
  228. uint8_t index = 0;
  229. /* Find the an empty handle pointer to store the handle. */
  230. for (index = 0; index < FLEXIO_SPI_HANDLE_COUNT; index++)
  231. {
  232. if (s_edmaPrivateHandle[index].base == NULL)
  233. {
  234. s_edmaPrivateHandle[index].base = base;
  235. s_edmaPrivateHandle[index].handle = handle;
  236. break;
  237. }
  238. }
  239. if (index == FLEXIO_SPI_HANDLE_COUNT)
  240. {
  241. return kStatus_OutOfRange;
  242. }
  243. /* Set spi base to handle. */
  244. handle->txHandle = txHandle;
  245. handle->rxHandle = rxHandle;
  246. /* Register callback and userData. */
  247. handle->callback = callback;
  248. handle->userData = userData;
  249. /* Set SPI state to idle. */
  250. handle->txInProgress = false;
  251. handle->rxInProgress = false;
  252. /* Install callback for Tx/Rx dma channel. */
  253. if (handle->txHandle)
  254. {
  255. EDMA_SetCallback(handle->txHandle, FLEXIO_SPI_TxEDMACallback, &s_edmaPrivateHandle[index]);
  256. }
  257. if (handle->rxHandle)
  258. {
  259. EDMA_SetCallback(handle->rxHandle, FLEXIO_SPI_RxEDMACallback, &s_edmaPrivateHandle[index]);
  260. }
  261. return kStatus_Success;
  262. }
  263. status_t FLEXIO_SPI_MasterTransferEDMA(FLEXIO_SPI_Type *base,
  264. flexio_spi_master_edma_handle_t *handle,
  265. flexio_spi_transfer_t *xfer)
  266. {
  267. assert(handle);
  268. assert(xfer);
  269. uint32_t dataMode = 0;
  270. uint16_t timerCmp = base->flexioBase->TIMCMP[base->timerIndex[0]];
  271. timerCmp &= 0x00FFU;
  272. /* Check if the device is busy. */
  273. if ((handle->txInProgress) || (handle->rxInProgress))
  274. {
  275. return kStatus_FLEXIO_SPI_Busy;
  276. }
  277. /* Check if input parameter invalid. */
  278. if (((xfer->txData == NULL) && (xfer->rxData == NULL)) || (xfer->dataSize == 0U))
  279. {
  280. return kStatus_InvalidArgument;
  281. }
  282. /* configure data mode. */
  283. if ((xfer->flags == kFLEXIO_SPI_8bitMsb) || (xfer->flags == kFLEXIO_SPI_8bitLsb))
  284. {
  285. dataMode = (8 * 2 - 1U) << 8U;
  286. }
  287. else if ((xfer->flags == kFLEXIO_SPI_16bitMsb) || (xfer->flags == kFLEXIO_SPI_16bitLsb))
  288. {
  289. dataMode = (16 * 2 - 1U) << 8U;
  290. }
  291. else
  292. {
  293. dataMode = 8 * 2 - 1U;
  294. }
  295. dataMode |= timerCmp;
  296. base->flexioBase->TIMCMP[base->timerIndex[0]] = dataMode;
  297. FLEXIO_SPI_EDMAConfig(base, handle, xfer);
  298. return kStatus_Success;
  299. }
  300. status_t FLEXIO_SPI_MasterTransferGetCountEDMA(FLEXIO_SPI_Type *base,
  301. flexio_spi_master_edma_handle_t *handle,
  302. size_t *count)
  303. {
  304. assert(handle);
  305. if (!count)
  306. {
  307. return kStatus_InvalidArgument;
  308. }
  309. if (handle->rxInProgress)
  310. {
  311. *count = (handle->transferSize -
  312. (uint32_t)handle->nbytes *
  313. EDMA_GetRemainingMajorLoopCount(handle->rxHandle->base, handle->rxHandle->channel));
  314. }
  315. else
  316. {
  317. *count = (handle->transferSize -
  318. (uint32_t)handle->nbytes *
  319. EDMA_GetRemainingMajorLoopCount(handle->txHandle->base, handle->txHandle->channel));
  320. }
  321. return kStatus_Success;
  322. }
  323. void FLEXIO_SPI_MasterTransferAbortEDMA(FLEXIO_SPI_Type *base, flexio_spi_master_edma_handle_t *handle)
  324. {
  325. assert(handle);
  326. /* Disable dma. */
  327. EDMA_StopTransfer(handle->txHandle);
  328. EDMA_StopTransfer(handle->rxHandle);
  329. /* Disable DMA enable bit. */
  330. FLEXIO_SPI_EnableDMA(base, kFLEXIO_SPI_DmaAllEnable, false);
  331. /* Set the handle state. */
  332. handle->txInProgress = false;
  333. handle->rxInProgress = false;
  334. }
  335. status_t FLEXIO_SPI_SlaveTransferEDMA(FLEXIO_SPI_Type *base,
  336. flexio_spi_slave_edma_handle_t *handle,
  337. flexio_spi_transfer_t *xfer)
  338. {
  339. assert(handle);
  340. assert(xfer);
  341. uint32_t dataMode = 0;
  342. /* Check if the device is busy. */
  343. if ((handle->txInProgress) || (handle->rxInProgress))
  344. {
  345. return kStatus_FLEXIO_SPI_Busy;
  346. }
  347. /* Check if input parameter invalid. */
  348. if (((xfer->txData == NULL) && (xfer->rxData == NULL)) || (xfer->dataSize == 0U))
  349. {
  350. return kStatus_InvalidArgument;
  351. }
  352. /* configure data mode. */
  353. if ((xfer->flags == kFLEXIO_SPI_8bitMsb) || (xfer->flags == kFLEXIO_SPI_8bitLsb))
  354. {
  355. dataMode = 8 * 2 - 1U;
  356. }
  357. else if ((xfer->flags == kFLEXIO_SPI_16bitMsb) || (xfer->flags == kFLEXIO_SPI_16bitLsb))
  358. {
  359. dataMode = 16 * 2 - 1U;
  360. }
  361. else
  362. {
  363. dataMode = 8 * 2 - 1U;
  364. }
  365. base->flexioBase->TIMCMP[base->timerIndex[0]] = dataMode;
  366. FLEXIO_SPI_EDMAConfig(base, handle, xfer);
  367. return kStatus_Success;
  368. }