fsl_spi.h 26 KB

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