fsl_spi.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * Copyright (c) 2016, 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_SPI_H_
  31. #define _FSL_SPI_H_
  32. #include "fsl_common.h"
  33. #include "fsl_flexcomm.h"
  34. /*!
  35. * @addtogroup spi_driver
  36. * @{
  37. */
  38. /*! @file */
  39. /*******************************************************************************
  40. * Definitions
  41. ******************************************************************************/
  42. /*! @name Driver version */
  43. /*@{*/
  44. /*! @brief USART driver version 2.0.0. */
  45. #define FSL_SPI_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  46. /*@}*/
  47. #define SPI_DUMMYDATA (0xFFFF)
  48. #define SPI_DATA(n) (((uint32_t)(n)) & 0xFFFF)
  49. #define SPI_CTRLMASK (0xFFFF0000)
  50. #define SPI_ASSERTNUM_SSEL(n) ((~(1U << ((n) + 16))) & 0xF0000)
  51. #define SPI_DEASSERTNUM_SSEL(n) (1U << ((n) + 16))
  52. #define SPI_DEASSERT_ALL (0xF0000)
  53. #define SPI_FIFOWR_FLAGS_MASK (~(SPI_DEASSERT_ALL | SPI_FIFOWR_TXDATA_MASK | SPI_FIFOWR_LEN_MASK))
  54. #define SPI_FIFOTRIG_TXLVL_GET(base) (((base)->FIFOTRIG & SPI_FIFOTRIG_TXLVL_MASK) >> SPI_FIFOTRIG_TXLVL_SHIFT)
  55. #define SPI_FIFOTRIG_RXLVL_GET(base) (((base)->FIFOTRIG & SPI_FIFOTRIG_RXLVL_MASK) >> SPI_FIFOTRIG_RXLVL_SHIFT)
  56. /*! @brief SPI transfer option.*/
  57. typedef enum _spi_xfer_option {
  58. kSPI_FrameDelay = (SPI_FIFOWR_EOF_MASK), /*!< Delay chip select */
  59. kSPI_FrameAssert = (SPI_FIFOWR_EOT_MASK), /*!< When transfer ends, assert chip select */
  60. } spi_xfer_option_t;
  61. /*! @brief SPI data shifter direction options.*/
  62. typedef enum _spi_shift_direction {
  63. kSPI_MsbFirst = 0U, /*!< Data transfers start with most significant bit. */
  64. kSPI_LsbFirst = 1U /*!< Data transfers start with least significant bit. */
  65. } spi_shift_direction_t;
  66. /*! @brief SPI clock polarity configuration.*/
  67. typedef enum _spi_clock_polarity {
  68. kSPI_ClockPolarityActiveHigh = 0x0U, /*!< Active-high SPI clock (idles low). */
  69. kSPI_ClockPolarityActiveLow /*!< Active-low SPI clock (idles high). */
  70. } spi_clock_polarity_t;
  71. /*! @brief SPI clock phase configuration.*/
  72. typedef enum _spi_clock_phase {
  73. kSPI_ClockPhaseFirstEdge = 0x0U, /*!< First edge on SCK occurs at the middle of the first
  74. * cycle of a data transfer. */
  75. kSPI_ClockPhaseSecondEdge /*!< First edge on SCK occurs at the start of the
  76. * first cycle of a data transfer. */
  77. } spi_clock_phase_t;
  78. /*! @brief txFIFO watermark values */
  79. typedef enum _spi_txfifo_watermark {
  80. kSPI_TxFifo0 = 0, /*!< SPI tx watermark is empty */
  81. kSPI_TxFifo1 = 1, /*!< SPI tx watermark at 1 item */
  82. kSPI_TxFifo2 = 2, /*!< SPI tx watermark at 2 items */
  83. kSPI_TxFifo3 = 3, /*!< SPI tx watermark at 3 items */
  84. kSPI_TxFifo4 = 4, /*!< SPI tx watermark at 4 items */
  85. kSPI_TxFifo5 = 5, /*!< SPI tx watermark at 5 items */
  86. kSPI_TxFifo6 = 6, /*!< SPI tx watermark at 6 items */
  87. kSPI_TxFifo7 = 7, /*!< SPI tx watermark at 7 items */
  88. } spi_txfifo_watermark_t;
  89. /*! @brief rxFIFO watermark values */
  90. typedef enum _spi_rxfifo_watermark {
  91. kSPI_RxFifo1 = 0, /*!< SPI rx watermark at 1 item */
  92. kSPI_RxFifo2 = 1, /*!< SPI rx watermark at 2 items */
  93. kSPI_RxFifo3 = 2, /*!< SPI rx watermark at 3 items */
  94. kSPI_RxFifo4 = 3, /*!< SPI rx watermark at 4 items */
  95. kSPI_RxFifo5 = 4, /*!< SPI rx watermark at 5 items */
  96. kSPI_RxFifo6 = 5, /*!< SPI rx watermark at 6 items */
  97. kSPI_RxFifo7 = 6, /*!< SPI rx watermark at 7 items */
  98. kSPI_RxFifo8 = 7, /*!< SPI rx watermark at 8 items */
  99. } spi_rxfifo_watermark_t;
  100. /*! @brief Transfer data width */
  101. typedef enum _spi_data_width {
  102. kSPI_Data4Bits = 3, /*!< 4 bits data width */
  103. kSPI_Data5Bits = 4, /*!< 5 bits data width */
  104. kSPI_Data6Bits = 5, /*!< 6 bits data width */
  105. kSPI_Data7Bits = 6, /*!< 7 bits data width */
  106. kSPI_Data8Bits = 7, /*!< 8 bits data width */
  107. kSPI_Data9Bits = 8, /*!< 9 bits data width */
  108. kSPI_Data10Bits = 9, /*!< 10 bits data width */
  109. kSPI_Data11Bits = 10, /*!< 11 bits data width */
  110. kSPI_Data12Bits = 11, /*!< 12 bits data width */
  111. kSPI_Data13Bits = 12, /*!< 13 bits data width */
  112. kSPI_Data14Bits = 13, /*!< 14 bits data width */
  113. kSPI_Data15Bits = 14, /*!< 15 bits data width */
  114. kSPI_Data16Bits = 15, /*!< 16 bits data width */
  115. } spi_data_width_t;
  116. /*! @brief Slave select */
  117. typedef enum _spi_ssel {
  118. kSPI_Ssel0 = 0, /*!< Slave select 0 */
  119. kSPI_Ssel1 = 1, /*!< Slave select 1 */
  120. kSPI_Ssel2 = 2, /*!< Slave select 2 */
  121. kSPI_Ssel3 = 3, /*!< Slave select 3 */
  122. } spi_ssel_t;
  123. /*! @brief SPI master user configure structure.*/
  124. typedef struct _spi_master_config
  125. {
  126. bool enableLoopback; /*!< Enable loopback for test purpose */
  127. bool enableMaster; /*!< Enable SPI at initialization time */
  128. spi_clock_polarity_t polarity; /*!< Clock polarity */
  129. spi_clock_phase_t phase; /*!< Clock phase */
  130. spi_shift_direction_t direction; /*!< MSB or LSB */
  131. uint32_t baudRate_Bps; /*!< Baud Rate for SPI in Hz */
  132. spi_data_width_t dataWidth; /*!< Width of the data */
  133. spi_ssel_t sselNum; /*!< Slave select number */
  134. spi_txfifo_watermark_t txWatermark; /*!< txFIFO watermark */
  135. spi_rxfifo_watermark_t rxWatermark; /*!< rxFIFO watermark */
  136. } spi_master_config_t;
  137. /*! @brief SPI slave user configure structure.*/
  138. typedef struct _spi_slave_config
  139. {
  140. bool enableSlave; /*!< Enable SPI at initialization time */
  141. spi_clock_polarity_t polarity; /*!< Clock polarity */
  142. spi_clock_phase_t phase; /*!< Clock phase */
  143. spi_shift_direction_t direction; /*!< MSB or LSB */
  144. spi_data_width_t dataWidth; /*!< Width of the data */
  145. spi_txfifo_watermark_t txWatermark; /*!< txFIFO watermark */
  146. spi_rxfifo_watermark_t rxWatermark; /*!< rxFIFO watermark */
  147. } spi_slave_config_t;
  148. /*! @brief SPI transfer status.*/
  149. enum _spi_status
  150. {
  151. kStatus_SPI_Busy = MAKE_STATUS(kStatusGroup_LPC_SPI, 0), /*!< SPI bus is busy */
  152. kStatus_SPI_Idle = MAKE_STATUS(kStatusGroup_LPC_SPI, 1), /*!< SPI is idle */
  153. kStatus_SPI_Error = MAKE_STATUS(kStatusGroup_LPC_SPI, 2), /*!< SPI error */
  154. kStatus_SPI_BaudrateNotSupport =
  155. MAKE_STATUS(kStatusGroup_LPC_SPI, 3) /*!< Baudrate is not support in current clock source */
  156. };
  157. /*! @brief SPI interrupt sources.*/
  158. enum _spi_interrupt_enable
  159. {
  160. kSPI_RxLvlIrq = SPI_FIFOINTENSET_RXLVL_MASK, /*!< Rx level interrupt */
  161. kSPI_TxLvlIrq = SPI_FIFOINTENSET_TXLVL_MASK, /*!< Tx level interrupt */
  162. };
  163. /*! @brief SPI status flags.*/
  164. enum _spi_statusflags
  165. {
  166. kSPI_TxEmptyFlag = SPI_FIFOSTAT_TXEMPTY_MASK, /*!< txFifo is empty */
  167. kSPI_TxNotFullFlag = SPI_FIFOSTAT_TXNOTFULL_MASK, /*!< txFifo is not full */
  168. kSPI_RxNotEmptyFlag = SPI_FIFOSTAT_RXNOTEMPTY_MASK, /*!< rxFIFO is not empty */
  169. kSPI_RxFullFlag = SPI_FIFOSTAT_RXFULL_MASK, /*!< rxFIFO is full */
  170. };
  171. /*! @brief SPI transfer structure */
  172. typedef struct _spi_transfer
  173. {
  174. uint8_t *txData; /*!< Send buffer */
  175. uint8_t *rxData; /*!< Receive buffer */
  176. uint32_t configFlags; /*!< Additional option to control transfer */
  177. size_t dataSize; /*!< Transfer bytes */
  178. } spi_transfer_t;
  179. /*! @brief Internal configuration structure used in 'spi' and 'spi_dma' driver */
  180. typedef struct _spi_config
  181. {
  182. spi_data_width_t dataWidth;
  183. spi_ssel_t sselNum;
  184. } spi_config_t;
  185. /*! @brief Master handle type */
  186. typedef struct _spi_master_handle spi_master_handle_t;
  187. /*! @brief Slave handle type */
  188. typedef spi_master_handle_t spi_slave_handle_t;
  189. /*! @brief SPI master callback for finished transmit */
  190. typedef void (*spi_master_callback_t)(SPI_Type *base, spi_master_handle_t *handle, status_t status, void *userData);
  191. /*! @brief SPI slave callback for finished transmit */
  192. typedef void (*spi_slave_callback_t)(SPI_Type *base, spi_slave_handle_t *handle, status_t status, void *userData);
  193. /*! @brief SPI transfer handle structure */
  194. struct _spi_master_handle
  195. {
  196. uint8_t *volatile txData; /*!< Transfer buffer */
  197. uint8_t *volatile rxData; /*!< Receive buffer */
  198. volatile size_t txRemainingBytes; /*!< Number of data to be transmitted [in bytes] */
  199. volatile size_t rxRemainingBytes; /*!< Number of data to be received [in bytes] */
  200. volatile size_t toReceiveCount; /*!< Receive data remaining in bytes */
  201. size_t totalByteCount; /*!< A number of transfer bytes */
  202. volatile uint32_t state; /*!< SPI internal state */
  203. spi_master_callback_t callback; /*!< SPI callback */
  204. void *userData; /*!< Callback parameter */
  205. uint8_t dataWidth; /*!< Width of the data [Valid values: 1 to 16] */
  206. uint8_t sselNum; /*!< Slave select number to be asserted when transferring data [Valid values: 0 to 3] */
  207. uint32_t configFlags; /*!< Additional option to control transfer */
  208. spi_txfifo_watermark_t txWatermark; /*!< txFIFO watermark */
  209. spi_rxfifo_watermark_t rxWatermark; /*!< rxFIFO watermark */
  210. };
  211. #if defined(__cplusplus)
  212. extern "C" {
  213. #endif
  214. /*******************************************************************************
  215. * API
  216. ******************************************************************************/
  217. /*! @brief Returns instance number for SPI peripheral base address. */
  218. uint32_t SPI_GetInstance(SPI_Type *base);
  219. /*!
  220. * @name Initialization and deinitialization
  221. * @{
  222. */
  223. /*!
  224. * @brief Sets the SPI master configuration structure to default values.
  225. *
  226. * The purpose of this API is to get the configuration structure initialized for use in SPI_MasterInit().
  227. * User may use the initialized structure unchanged in SPI_MasterInit(), or modify
  228. * some fields of the structure before calling SPI_MasterInit(). After calling this API,
  229. * the master is ready to transfer.
  230. * Example:
  231. @code
  232. spi_master_config_t config;
  233. SPI_MasterGetDefaultConfig(&config);
  234. @endcode
  235. *
  236. * @param config pointer to master config structure
  237. */
  238. void SPI_MasterGetDefaultConfig(spi_master_config_t *config);
  239. /*!
  240. * @brief Initializes the SPI with master configuration.
  241. *
  242. * The configuration structure can be filled by user from scratch, or be set with default
  243. * values by SPI_MasterGetDefaultConfig(). After calling this API, the slave is ready to transfer.
  244. * Example
  245. @code
  246. spi_master_config_t config = {
  247. .baudRate_Bps = 400000,
  248. ...
  249. };
  250. SPI_MasterInit(SPI0, &config);
  251. @endcode
  252. *
  253. * @param base SPI base pointer
  254. * @param config pointer to master configuration structure
  255. * @param srcClock_Hz Source clock frequency.
  256. */
  257. status_t SPI_MasterInit(SPI_Type *base, const spi_master_config_t *config, uint32_t srcClock_Hz);
  258. /*!
  259. * @brief Sets the SPI slave configuration structure to default values.
  260. *
  261. * The purpose of this API is to get the configuration structure initialized for use in SPI_SlaveInit().
  262. * Modify some fields of the structure before calling SPI_SlaveInit().
  263. * Example:
  264. @code
  265. spi_slave_config_t config;
  266. SPI_SlaveGetDefaultConfig(&config);
  267. @endcode
  268. *
  269. * @param config pointer to slave configuration structure
  270. */
  271. void SPI_SlaveGetDefaultConfig(spi_slave_config_t *config);
  272. /*!
  273. * @brief Initializes the SPI with slave configuration.
  274. *
  275. * The configuration structure can be filled by user from scratch or be set with
  276. * default values by SPI_SlaveGetDefaultConfig().
  277. * After calling this API, the slave is ready to transfer.
  278. * Example
  279. @code
  280. spi_slave_config_t config = {
  281. .polarity = flexSPIClockPolarity_ActiveHigh;
  282. .phase = flexSPIClockPhase_FirstEdge;
  283. .direction = flexSPIMsbFirst;
  284. ...
  285. };
  286. SPI_SlaveInit(SPI0, &config);
  287. @endcode
  288. *
  289. * @param base SPI base pointer
  290. * @param config pointer to slave configuration structure
  291. */
  292. status_t SPI_SlaveInit(SPI_Type *base, const spi_slave_config_t *config);
  293. /*!
  294. * @brief De-initializes the SPI.
  295. *
  296. * Calling this API resets the SPI module, gates the SPI clock.
  297. * The SPI module can't work unless calling the SPI_MasterInit/SPI_SlaveInit to initialize module.
  298. *
  299. * @param base SPI base pointer
  300. */
  301. void SPI_Deinit(SPI_Type *base);
  302. /*!
  303. * @brief Enable or disable the SPI Master or Slave
  304. * @param base SPI base pointer
  305. * @param enable or disable ( true = enable, false = disable)
  306. */
  307. static inline void SPI_Enable(SPI_Type *base, bool enable)
  308. {
  309. if (enable)
  310. {
  311. base->CFG |= SPI_CFG_ENABLE_MASK;
  312. }
  313. else
  314. {
  315. base->CFG &= ~SPI_CFG_ENABLE_MASK;
  316. }
  317. }
  318. /*! @} */
  319. /*!
  320. * @name Status
  321. * @{
  322. */
  323. /*!
  324. * @brief Gets the status flag.
  325. *
  326. * @param base SPI base pointer
  327. * @return SPI Status, use status flag to AND @ref _spi_statusflags could get the related status.
  328. */
  329. static inline uint32_t SPI_GetStatusFlags(SPI_Type *base)
  330. {
  331. assert(NULL != base);
  332. return base->FIFOSTAT;
  333. }
  334. /*! @} */
  335. /*!
  336. * @name Interrupts
  337. * @{
  338. */
  339. /*!
  340. * @brief Enables the interrupt for the SPI.
  341. *
  342. * @param base SPI base pointer
  343. * @param irqs SPI interrupt source. The parameter can be any combination of the following values:
  344. * @arg kSPI_RxLvlIrq
  345. * @arg kSPI_TxLvlIrq
  346. */
  347. static inline void SPI_EnableInterrupts(SPI_Type *base, uint32_t irqs)
  348. {
  349. assert(NULL != base);
  350. base->FIFOINTENSET = irqs;
  351. }
  352. /*!
  353. * @brief Disables the interrupt for the SPI.
  354. *
  355. * @param base SPI base pointer
  356. * @param irqs SPI interrupt source. The parameter can be any combination of the following values:
  357. * @arg kSPI_RxLvlIrq
  358. * @arg kSPI_TxLvlIrq
  359. */
  360. static inline void SPI_DisableInterrupts(SPI_Type *base, uint32_t irqs)
  361. {
  362. assert(NULL != base);
  363. base->FIFOINTENCLR = irqs;
  364. }
  365. /*! @} */
  366. /*!
  367. * @name DMA Control
  368. * @{
  369. */
  370. /*!
  371. * @brief Enables the DMA request from SPI txFIFO.
  372. *
  373. * @param base SPI base pointer
  374. * @param enable True means enable DMA, false means disable DMA
  375. */
  376. void SPI_EnableTxDMA(SPI_Type *base, bool enable);
  377. /*!
  378. * @brief Enables the DMA request from SPI rxFIFO.
  379. *
  380. * @param base SPI base pointer
  381. * @param enable True means enable DMA, false means disable DMA
  382. */
  383. void SPI_EnableRxDMA(SPI_Type *base, bool enable);
  384. /*! @} */
  385. /*!
  386. * @name Bus Operations
  387. * @{
  388. */
  389. /*!
  390. * @brief Sets the baud rate for SPI transfer. This is only used in master.
  391. *
  392. * @param base SPI base pointer
  393. * @param baudrate_Bps baud rate needed in Hz.
  394. * @param srcClock_Hz SPI source clock frequency in Hz.
  395. */
  396. status_t SPI_MasterSetBaud(SPI_Type *base, uint32_t baudrate_Bps, uint32_t srcClock_Hz);
  397. /*!
  398. * @brief Writes a data into the SPI data register.
  399. *
  400. * @param base SPI base pointer
  401. * @param data needs to be write.
  402. * @param configFlags transfer configuration options @ref spi_xfer_option_t
  403. */
  404. void SPI_WriteData(SPI_Type *base, uint16_t data, uint32_t configFlags);
  405. /*!
  406. * @brief Gets a data from the SPI data register.
  407. *
  408. * @param base SPI base pointer
  409. * @return Data in the register.
  410. */
  411. static inline uint32_t SPI_ReadData(SPI_Type *base)
  412. {
  413. assert(NULL != base);
  414. return base->FIFORD;
  415. }
  416. /*! @} */
  417. /*!
  418. * @name Transactional
  419. * @{
  420. */
  421. /*!
  422. * @brief Initializes the SPI master handle.
  423. *
  424. * This function initializes the SPI master handle which can be used for other SPI master transactional APIs. Usually,
  425. * for a specified SPI instance, call this API once to get the initialized handle.
  426. *
  427. * @param base SPI peripheral base address.
  428. * @param handle SPI handle pointer.
  429. * @param callback Callback function.
  430. * @param userData User data.
  431. */
  432. status_t SPI_MasterTransferCreateHandle(SPI_Type *base,
  433. spi_master_handle_t *handle,
  434. spi_master_callback_t callback,
  435. void *userData);
  436. /*!
  437. * @brief Transfers a block of data using a polling method.
  438. *
  439. * @param base SPI base pointer
  440. * @param xfer pointer to spi_xfer_config_t structure
  441. * @retval kStatus_Success Successfully start a transfer.
  442. * @retval kStatus_InvalidArgument Input argument is invalid.
  443. */
  444. status_t SPI_MasterTransferBlocking(SPI_Type *base, spi_transfer_t *xfer);
  445. /*!
  446. * @brief Performs a non-blocking SPI interrupt transfer.
  447. *
  448. * @param base SPI peripheral base address.
  449. * @param handle pointer to spi_master_handle_t structure which stores the transfer state
  450. * @param xfer pointer to spi_xfer_config_t structure
  451. * @retval kStatus_Success Successfully start a transfer.
  452. * @retval kStatus_InvalidArgument Input argument is invalid.
  453. * @retval kStatus_SPI_Busy SPI is not idle, is running another transfer.
  454. */
  455. status_t SPI_MasterTransferNonBlocking(SPI_Type *base, spi_master_handle_t *handle, spi_transfer_t *xfer);
  456. /*!
  457. * @brief Gets the master transfer count.
  458. *
  459. * This function gets the master transfer count.
  460. *
  461. * @param base SPI peripheral base address.
  462. * @param handle Pointer to the spi_master_handle_t structure which stores the transfer state.
  463. * @param count The number of bytes transferred by using the non-blocking transaction.
  464. * @return status of status_t.
  465. */
  466. status_t SPI_MasterTransferGetCount(SPI_Type *base, spi_master_handle_t *handle, size_t *count);
  467. /*!
  468. * @brief SPI master aborts a transfer using an interrupt.
  469. *
  470. * This function aborts a transfer using an interrupt.
  471. *
  472. * @param base SPI peripheral base address.
  473. * @param handle Pointer to the spi_master_handle_t structure which stores the transfer state.
  474. */
  475. void SPI_MasterTransferAbort(SPI_Type *base, spi_master_handle_t *handle);
  476. /*!
  477. * @brief Interrupts the handler for the SPI.
  478. *
  479. * @param base SPI peripheral base address.
  480. * @param handle pointer to spi_master_handle_t structure which stores the transfer state.
  481. */
  482. void SPI_MasterTransferHandleIRQ(SPI_Type *base, spi_master_handle_t *handle);
  483. /*!
  484. * @brief Initializes the SPI slave handle.
  485. *
  486. * This function initializes the SPI slave handle which can be used for other SPI slave transactional APIs. Usually,
  487. * for a specified SPI instance, call this API once to get the initialized handle.
  488. *
  489. * @param base SPI peripheral base address.
  490. * @param handle SPI handle pointer.
  491. * @param callback Callback function.
  492. * @param userData User data.
  493. */
  494. static inline status_t SPI_SlaveTransferCreateHandle(SPI_Type *base,
  495. spi_slave_handle_t *handle,
  496. spi_slave_callback_t callback,
  497. void *userData)
  498. {
  499. return SPI_MasterTransferCreateHandle(base, handle, callback, userData);
  500. }
  501. /*!
  502. * @brief Performs a non-blocking SPI slave interrupt transfer.
  503. *
  504. * @note The API returns immediately after the transfer initialization is finished.
  505. *
  506. * @param base SPI peripheral base address.
  507. * @param handle pointer to spi_master_handle_t structure which stores the transfer state
  508. * @param xfer pointer to spi_xfer_config_t structure
  509. * @retval kStatus_Success Successfully start a transfer.
  510. * @retval kStatus_InvalidArgument Input argument is invalid.
  511. * @retval kStatus_SPI_Busy SPI is not idle, is running another transfer.
  512. */
  513. static inline status_t SPI_SlaveTransferNonBlocking(SPI_Type *base, spi_slave_handle_t *handle, spi_transfer_t *xfer)
  514. {
  515. return SPI_MasterTransferNonBlocking(base, handle, xfer);
  516. }
  517. /*!
  518. * @brief Gets the slave transfer count.
  519. *
  520. * This function gets the slave transfer count.
  521. *
  522. * @param base SPI peripheral base address.
  523. * @param handle Pointer to the spi_master_handle_t structure which stores the transfer state.
  524. * @param count The number of bytes transferred by using the non-blocking transaction.
  525. * @return status of status_t.
  526. */
  527. static inline status_t SPI_SlaveTransferGetCount(SPI_Type *base, spi_slave_handle_t *handle, size_t *count)
  528. {
  529. return SPI_MasterTransferGetCount(base, (spi_master_handle_t*)handle, count);
  530. }
  531. /*!
  532. * @brief SPI slave aborts a transfer using an interrupt.
  533. *
  534. * This function aborts a transfer using an interrupt.
  535. *
  536. * @param base SPI peripheral base address.
  537. * @param handle Pointer to the spi_slave_handle_t structure which stores the transfer state.
  538. */
  539. static inline void SPI_SlaveTransferAbort(SPI_Type *base, spi_slave_handle_t *handle)
  540. {
  541. SPI_MasterTransferAbort(base, (spi_master_handle_t*)handle);
  542. }
  543. /*!
  544. * @brief Interrupts a handler for the SPI slave.
  545. *
  546. * @param base SPI peripheral base address.
  547. * @param handle pointer to spi_slave_handle_t structure which stores the transfer state
  548. */
  549. static inline void SPI_SlaveTransferHandleIRQ(SPI_Type *base, spi_slave_handle_t *handle)
  550. {
  551. SPI_MasterTransferHandleIRQ(base, handle);
  552. }
  553. /*! @} */
  554. #if defined(__cplusplus)
  555. }
  556. #endif
  557. /*! @} */
  558. #endif /* _FSL_SPI_H_*/