1 /* 2 * Copyright (c) 2015, Freescale Semiconductor, Inc. 3 * Copyright 2016-2022 NXP 4 * All rights reserved. 5 * 6 * SPDX-License-Identifier: BSD-3-Clause 7 */ 8 #ifndef FSL_LPSPI_EDMA_H_ 9 #define FSL_LPSPI_EDMA_H_ 10 11 #include "fsl_lpspi.h" 12 #include "fsl_edma.h" 13 14 /*! 15 * @addtogroup lpspi_edma_driver 16 * @{ 17 */ 18 19 /*********************************************************************************************************************** 20 * Definitions 21 **********************************************************************************************************************/ 22 /*! @name Driver version */ 23 /*! @{ */ 24 /*! @brief LPSPI EDMA driver version. */ 25 #define FSL_LPSPI_EDMA_DRIVER_VERSION (MAKE_VERSION(2, 4, 4)) 26 27 /*! @brief DMA max transfer size */ 28 #define DMA_MAX_TRANSFER_COUNT 0x7FFFU 29 /*! @} */ 30 31 /*! 32 * @brief Forward declaration of the _lpspi_master_edma_handle typedefs. 33 */ 34 typedef struct _lpspi_master_edma_handle lpspi_master_edma_handle_t; 35 36 /*! 37 * @brief Forward declaration of the _lpspi_slave_edma_handle typedefs. 38 */ 39 typedef struct _lpspi_slave_edma_handle lpspi_slave_edma_handle_t; 40 41 /*! 42 * @brief Completion callback function pointer type. 43 * 44 * @param base LPSPI peripheral base address. 45 * @param handle Pointer to the handle for the LPSPI master. 46 * @param status Success or error code describing whether the transfer completed. 47 * @param userData Arbitrary pointer-dataSized value passed from the application. 48 */ 49 typedef void (*lpspi_master_edma_transfer_callback_t)(LPSPI_Type *base, 50 lpspi_master_edma_handle_t *handle, 51 status_t status, 52 void *userData); 53 /*! 54 * @brief Completion callback function pointer type. 55 * 56 * @param base LPSPI peripheral base address. 57 * @param handle Pointer to the handle for the LPSPI slave. 58 * @param status Success or error code describing whether the transfer completed. 59 * @param userData Arbitrary pointer-dataSized value passed from the application. 60 */ 61 typedef void (*lpspi_slave_edma_transfer_callback_t)(LPSPI_Type *base, 62 lpspi_slave_edma_handle_t *handle, 63 status_t status, 64 void *userData); 65 66 /*! @brief LPSPI master eDMA transfer handle structure used for transactional API. */ 67 struct _lpspi_master_edma_handle 68 { 69 volatile bool isPcsContinuous; /*!< Is PCS continuous in transfer. */ 70 71 volatile bool isByteSwap; /*!< A flag that whether should byte swap. */ 72 73 volatile uint8_t fifoSize; /*!< FIFO dataSize. */ 74 75 volatile uint8_t rxWatermark; /*!< Rx watermark. */ 76 77 volatile uint8_t bytesEachWrite; /*!< Bytes for each write TDR. */ 78 volatile uint8_t bytesEachRead; /*!< Bytes for each read RDR. */ 79 80 volatile uint8_t bytesLastRead; /*!< Bytes for last read RDR. */ 81 volatile bool isThereExtraRxBytes; /*!< Is there extra RX byte. */ 82 83 const uint8_t *volatile txData; /*!< Send buffer. */ 84 uint8_t *volatile rxData; /*!< Receive buffer. */ 85 volatile size_t txRemainingByteCount; /*!< Number of bytes remaining to send.*/ 86 volatile size_t rxRemainingByteCount; /*!< Number of bytes remaining to receive.*/ 87 88 volatile uint32_t writeRegRemainingTimes; /*!< Write TDR register remaining times. */ 89 volatile uint32_t readRegRemainingTimes; /*!< Read RDR register remaining times. */ 90 91 uint32_t totalByteCount; /*!< Number of transfer bytes*/ 92 93 edma_tcd_t *lastTimeTCD; /*!< Pointer to the lastTime TCD*/ 94 bool isMultiDMATransmit; /*!< Is there multi DMA transmit*/ 95 volatile uint8_t dmaTransmitTime; /*!< DMA Transfer times. */ 96 uint32_t lastTimeDataBytes; /*!< DMA transmit last Time data Bytes */ 97 98 uint32_t dataBytesEveryTime; /*!< Bytes in a time for DMA transfer, default is DMA_MAX_TRANSFER_COUNT */ 99 100 edma_transfer_config_t transferConfigRx; /*!< Config of DMA rx channel.*/ 101 edma_transfer_config_t transferConfigTx; /*!< Config of DMA tx channel.*/ 102 uint32_t txBuffIfNull; /*!< Used if there is not txData for DMA purpose.*/ 103 uint32_t rxBuffIfNull; /*!< Used if there is not rxData for DMA purpose.*/ 104 105 uint32_t transmitCommand; /*!< Used to write TCR for DMA purpose.*/ 106 107 volatile uint8_t state; /*!< LPSPI transfer state , _lpspi_transfer_state.*/ 108 109 uint8_t nbytes; /*!< eDMA minor byte transfer count initially configured. */ 110 111 lpspi_master_edma_transfer_callback_t callback; /*!< Completion callback. */ 112 void *userData; /*!< Callback user data. */ 113 114 edma_handle_t *edmaRxRegToRxDataHandle; /*!<edma_handle_t handle point used for RxReg to RxData buff*/ 115 edma_handle_t *edmaTxDataToTxRegHandle; /*!<edma_handle_t handle point used for TxData to TxReg buff*/ 116 117 edma_tcd_t lpspiSoftwareTCD[3]; /*!<SoftwareTCD, internal used*/ 118 }; 119 120 /*! @brief LPSPI slave eDMA transfer handle structure used for transactional API.*/ 121 struct _lpspi_slave_edma_handle 122 { 123 volatile bool isByteSwap; /*!< A flag that whether should byte swap. */ 124 125 volatile uint8_t fifoSize; /*!< FIFO dataSize. */ 126 127 volatile uint8_t rxWatermark; /*!< Rx watermark. */ 128 129 volatile uint8_t bytesEachWrite; /*!< Bytes for each write TDR. */ 130 volatile uint8_t bytesEachRead; /*!< Bytes for each read RDR. */ 131 132 volatile uint8_t bytesLastRead; /*!< Bytes for last read RDR. */ 133 volatile bool isThereExtraRxBytes; /*!< Is there extra RX byte. */ 134 135 uint8_t nbytes; /*!< eDMA minor byte transfer count initially configured. */ 136 137 const uint8_t *volatile txData; /*!< Send buffer. */ 138 uint8_t *volatile rxData; /*!< Receive buffer. */ 139 volatile size_t txRemainingByteCount; /*!< Number of bytes remaining to send.*/ 140 volatile size_t rxRemainingByteCount; /*!< Number of bytes remaining to receive.*/ 141 142 volatile uint32_t writeRegRemainingTimes; /*!< Write TDR register remaining times. */ 143 volatile uint32_t readRegRemainingTimes; /*!< Read RDR register remaining times. */ 144 145 uint32_t totalByteCount; /*!< Number of transfer bytes*/ 146 147 uint32_t txBuffIfNull; /*!< Used if there is not txData for DMA purpose.*/ 148 uint32_t rxBuffIfNull; /*!< Used if there is not rxData for DMA purpose.*/ 149 150 volatile uint8_t state; /*!< LPSPI transfer state.*/ 151 152 uint32_t errorCount; /*!< Error count for slave transfer.*/ 153 154 lpspi_slave_edma_transfer_callback_t callback; /*!< Completion callback. */ 155 void *userData; /*!< Callback user data. */ 156 157 edma_handle_t *edmaRxRegToRxDataHandle; /*!<edma_handle_t handle point used for RxReg to RxData buff*/ 158 edma_handle_t *edmaTxDataToTxRegHandle; /*!<edma_handle_t handle point used for TxData to TxReg*/ 159 160 edma_tcd_t lpspiSoftwareTCD[2]; /*!<SoftwareTCD, internal used*/ 161 }; 162 163 /*********************************************************************************************************************** 164 * API 165 **********************************************************************************************************************/ 166 #if defined(__cplusplus) 167 extern "C" { 168 #endif /*_cplusplus*/ 169 170 /*Transactional APIs*/ 171 172 /*! 173 * @brief Initializes the LPSPI master eDMA handle. 174 * 175 * This function initializes the LPSPI eDMA handle which can be used for other LPSPI transactional APIs. Usually, for a 176 * specified LPSPI instance, call this API once to get the initialized handle. 177 * 178 * Note that the LPSPI eDMA has a separated (Rx and Tx as two sources) or shared (Rx and Tx are the same source) DMA 179 * request source. 180 * (1) For a separated DMA request source, enable and set the Rx DMAMUX source for edmaRxRegToRxDataHandle and 181 * Tx DMAMUX source for edmaTxDataToTxRegHandle. 182 * (2) For a shared DMA request source, enable and set the Rx/Tx DMAMUX source for edmaRxRegToRxDataHandle. 183 * 184 * @param base LPSPI peripheral base address. 185 * @param handle LPSPI handle pointer to lpspi_master_edma_handle_t. 186 * @param callback LPSPI callback. 187 * @param userData callback function parameter. 188 * @param edmaRxRegToRxDataHandle edmaRxRegToRxDataHandle pointer to edma_handle_t. 189 * @param edmaTxDataToTxRegHandle edmaTxDataToTxRegHandle pointer to edma_handle_t. 190 */ 191 void LPSPI_MasterTransferCreateHandleEDMA(LPSPI_Type *base, 192 lpspi_master_edma_handle_t *handle, 193 lpspi_master_edma_transfer_callback_t callback, 194 void *userData, 195 edma_handle_t *edmaRxRegToRxDataHandle, 196 edma_handle_t *edmaTxDataToTxRegHandle); 197 198 /*! 199 * @brief LPSPI master transfer data using eDMA. 200 * 201 * This function transfers data using eDMA. This is a non-blocking function, which returns right away. When all data 202 * is transferred, the callback function is called. 203 * 204 * Note: 205 * The transfer data size should be an integer multiple of bytesPerFrame if bytesPerFrame is less than or equal to 4. 206 * For bytesPerFrame greater than 4: 207 * The transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not an integer multiple of 4. 208 * Otherwise, the transfer data size can be an integer multiple of bytesPerFrame. 209 * 210 * @param base LPSPI peripheral base address. 211 * @param handle pointer to lpspi_master_edma_handle_t structure which stores the transfer state. 212 * @param transfer pointer to lpspi_transfer_t structure. 213 * @return status of status_t. 214 */ 215 status_t LPSPI_MasterTransferEDMA(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, lpspi_transfer_t *transfer); 216 217 /*! 218 * @brief LPSPI master config transfer parameter while using eDMA. 219 * 220 * This function is preparing to transfer data using eDMA, work with LPSPI_MasterTransferEDMALite. 221 * 222 * @param base LPSPI peripheral base address. 223 * @param handle pointer to lpspi_master_edma_handle_t structure which stores the transfer state. 224 * @param configFlags transfer configuration flags. @ref _lpspi_transfer_config_flag_for_master. 225 * @return Indicates whether LPSPI master transfer was successful or not. 226 * @retval kStatus_Success Execution successfully. 227 * @retval kStatus_LPSPI_Busy The LPSPI device is busy. 228 */ 229 status_t LPSPI_MasterTransferPrepareEDMALite(LPSPI_Type *base, 230 lpspi_master_edma_handle_t *handle, 231 uint32_t configFlags); 232 233 /*! 234 * @brief LPSPI master transfer data using eDMA without configs. 235 * 236 * This function transfers data using eDMA. This is a non-blocking function, which returns right away. When all data 237 * is transferred, the callback function is called. 238 * 239 * Note: 240 * This API is only for transfer through DMA without configuration. 241 * Before calling this API, you must call LPSPI_MasterTransferPrepareEDMALite to configure it once. 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_master_edma_handle_t structure which stores the transfer state. 249 * @param transfer pointer to lpspi_transfer_t structure, config field is not uesed. 250 * @return Indicates whether LPSPI master transfer was successful or not. 251 * @retval kStatus_Success Execution successfully. 252 * @retval kStatus_LPSPI_Busy The LPSPI device is busy. 253 * @retval kStatus_InvalidArgument The transfer structure is invalid. 254 */ 255 status_t LPSPI_MasterTransferEDMALite(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, lpspi_transfer_t *transfer); 256 257 /*! 258 * @brief LPSPI master aborts a transfer which is using eDMA. 259 * 260 * This function aborts a transfer which is using eDMA. 261 * 262 * @param base LPSPI peripheral base address. 263 * @param handle pointer to lpspi_master_edma_handle_t structure which stores the transfer state. 264 */ 265 void LPSPI_MasterTransferAbortEDMA(LPSPI_Type *base, lpspi_master_edma_handle_t *handle); 266 267 /*! 268 * @brief Gets the master eDMA transfer remaining bytes. 269 * 270 * This function gets the master eDMA transfer remaining bytes. 271 * 272 * @param base LPSPI peripheral base address. 273 * @param handle pointer to lpspi_master_edma_handle_t structure which stores the transfer state. 274 * @param count Number of bytes transferred so far by the EDMA transaction. 275 * @return status of status_t. 276 */ 277 status_t LPSPI_MasterTransferGetCountEDMA(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, size_t *count); 278 279 /*! 280 * @brief Initializes the LPSPI slave eDMA handle. 281 * 282 * This function initializes the LPSPI eDMA handle which can be used for other LPSPI transactional APIs. Usually, for a 283 * specified LPSPI instance, call this API once to get the initialized handle. 284 * 285 * Note that LPSPI eDMA has a separated (Rx and Tx as two sources) or shared (Rx and Tx as the same source) DMA request 286 * source. 287 * 288 * (1) For a separated DMA request source, enable and set the Rx DMAMUX source for edmaRxRegToRxDataHandle and 289 * Tx DMAMUX source for edmaTxDataToTxRegHandle. 290 * (2) For a shared DMA request source, enable and set the Rx/Rx DMAMUX source for edmaRxRegToRxDataHandle . 291 * 292 * @param base LPSPI peripheral base address. 293 * @param handle LPSPI handle pointer to lpspi_slave_edma_handle_t. 294 * @param callback LPSPI callback. 295 * @param userData callback function parameter. 296 * @param edmaRxRegToRxDataHandle edmaRxRegToRxDataHandle pointer to edma_handle_t. 297 * @param edmaTxDataToTxRegHandle edmaTxDataToTxRegHandle pointer to edma_handle_t. 298 */ 299 void LPSPI_SlaveTransferCreateHandleEDMA(LPSPI_Type *base, 300 lpspi_slave_edma_handle_t *handle, 301 lpspi_slave_edma_transfer_callback_t callback, 302 void *userData, 303 edma_handle_t *edmaRxRegToRxDataHandle, 304 edma_handle_t *edmaTxDataToTxRegHandle); 305 306 /*! 307 * @brief LPSPI slave transfers data using eDMA. 308 * 309 * This function transfers data using eDMA. This is a non-blocking function, which return right away. When all data 310 * is transferred, the callback function is called. 311 * 312 * Note: 313 * The transfer data size should be an integer multiple of bytesPerFrame if bytesPerFrame is less than or equal to 4. 314 * For bytesPerFrame greater than 4: 315 * The transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not an integer multiple of 4. 316 * Otherwise, the transfer data size can be an integer multiple of bytesPerFrame. 317 * 318 * @param base LPSPI peripheral base address. 319 * @param handle pointer to lpspi_slave_edma_handle_t structure which stores the transfer state. 320 * @param transfer pointer to lpspi_transfer_t structure. 321 * @return status of status_t. 322 */ 323 status_t LPSPI_SlaveTransferEDMA(LPSPI_Type *base, lpspi_slave_edma_handle_t *handle, lpspi_transfer_t *transfer); 324 325 /*! 326 * @brief LPSPI slave aborts a transfer which is using eDMA. 327 * 328 * This function aborts a transfer which is using eDMA. 329 * 330 * @param base LPSPI peripheral base address. 331 * @param handle pointer to lpspi_slave_edma_handle_t structure which stores the transfer state. 332 */ 333 void LPSPI_SlaveTransferAbortEDMA(LPSPI_Type *base, lpspi_slave_edma_handle_t *handle); 334 335 /*! 336 * @brief Gets the slave eDMA transfer remaining bytes. 337 * 338 * This function gets the slave eDMA transfer remaining bytes. 339 * 340 * @param base LPSPI peripheral base address. 341 * @param handle pointer to lpspi_slave_edma_handle_t structure which stores the transfer state. 342 * @param count Number of bytes transferred so far by the eDMA transaction. 343 * @return status of status_t. 344 */ 345 status_t LPSPI_SlaveTransferGetCountEDMA(LPSPI_Type *base, lpspi_slave_edma_handle_t *handle, size_t *count); 346 347 #if defined(__cplusplus) 348 } 349 #endif 350 351 /*! @}*/ 352 353 #endif /*FSL_LPSPI_EDMA_H_*/ 354