fsl_flexio_spi.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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_FLEXIO_SPI_H_
  35. #define _FSL_FLEXIO_SPI_H_
  36. #include "fsl_common.h"
  37. #include "fsl_flexio.h"
  38. /*!
  39. * @addtogroup flexio_spi
  40. * @{
  41. */
  42. /*******************************************************************************
  43. * Definitions
  44. ******************************************************************************/
  45. /*! @name Driver version */
  46. /*@{*/
  47. /*! @brief FlexIO SPI driver version 2.1.3. */
  48. #define FSL_FLEXIO_SPI_DRIVER_VERSION (MAKE_VERSION(2, 1, 3))
  49. /*@}*/
  50. #ifndef FLEXIO_SPI_DUMMYDATA
  51. /*! @brief FlexIO SPI dummy transfer data, the data is sent while txData is NULL. */
  52. #define FLEXIO_SPI_DUMMYDATA (0xFFFFU)
  53. #endif
  54. /*! @brief Error codes for the FlexIO SPI driver. */
  55. enum _flexio_spi_status
  56. {
  57. kStatus_FLEXIO_SPI_Busy = MAKE_STATUS(kStatusGroup_FLEXIO_SPI, 1), /*!< FlexIO SPI is busy. */
  58. kStatus_FLEXIO_SPI_Idle = MAKE_STATUS(kStatusGroup_FLEXIO_SPI, 2), /*!< SPI is idle */
  59. kStatus_FLEXIO_SPI_Error = MAKE_STATUS(kStatusGroup_FLEXIO_SPI, 3), /*!< FlexIO SPI error. */
  60. };
  61. /*! @brief FlexIO SPI clock phase configuration. */
  62. typedef enum _flexio_spi_clock_phase
  63. {
  64. kFLEXIO_SPI_ClockPhaseFirstEdge = 0x0U, /*!< First edge on SPSCK occurs at the middle of the first
  65. * cycle of a data transfer. */
  66. kFLEXIO_SPI_ClockPhaseSecondEdge = 0x1U, /*!< First edge on SPSCK occurs at the start of the
  67. * first cycle of a data transfer. */
  68. } flexio_spi_clock_phase_t;
  69. /*! @brief FlexIO SPI data shifter direction options. */
  70. typedef enum _flexio_spi_shift_direction
  71. {
  72. kFLEXIO_SPI_MsbFirst = 0, /*!< Data transfers start with most significant bit. */
  73. kFLEXIO_SPI_LsbFirst = 1, /*!< Data transfers start with least significant bit. */
  74. } flexio_spi_shift_direction_t;
  75. /*! @brief FlexIO SPI data length mode options. */
  76. typedef enum _flexio_spi_data_bitcount_mode
  77. {
  78. kFLEXIO_SPI_8BitMode = 0x08U, /*!< 8-bit data transmission mode. */
  79. kFLEXIO_SPI_16BitMode = 0x10U, /*!< 16-bit data transmission mode. */
  80. } flexio_spi_data_bitcount_mode_t;
  81. /*! @brief Define FlexIO SPI interrupt mask. */
  82. enum _flexio_spi_interrupt_enable
  83. {
  84. kFLEXIO_SPI_TxEmptyInterruptEnable = 0x1U, /*!< Transmit buffer empty interrupt enable. */
  85. kFLEXIO_SPI_RxFullInterruptEnable = 0x2U, /*!< Receive buffer full interrupt enable. */
  86. };
  87. /*! @brief Define FlexIO SPI status mask. */
  88. enum _flexio_spi_status_flags
  89. {
  90. kFLEXIO_SPI_TxBufferEmptyFlag = 0x1U, /*!< Transmit buffer empty flag. */
  91. kFLEXIO_SPI_RxBufferFullFlag = 0x2U, /*!< Receive buffer full flag. */
  92. };
  93. /*! @brief Define FlexIO SPI DMA mask. */
  94. enum _flexio_spi_dma_enable
  95. {
  96. kFLEXIO_SPI_TxDmaEnable = 0x1U, /*!< Tx DMA request source */
  97. kFLEXIO_SPI_RxDmaEnable = 0x2U, /*!< Rx DMA request source */
  98. kFLEXIO_SPI_DmaAllEnable = 0x3U, /*!< All DMA request source*/
  99. };
  100. /*! @brief Define FlexIO SPI transfer flags. */
  101. enum _flexio_spi_transfer_flags
  102. {
  103. kFLEXIO_SPI_8bitMsb = 0x1U, /*!< FlexIO SPI 8-bit MSB first */
  104. kFLEXIO_SPI_8bitLsb = 0x2U, /*!< FlexIO SPI 8-bit LSB first */
  105. kFLEXIO_SPI_16bitMsb = 0x9U, /*!< FlexIO SPI 16-bit MSB first */
  106. kFLEXIO_SPI_16bitLsb = 0xaU, /*!< FlexIO SPI 16-bit LSB first */
  107. };
  108. /*! @brief Define FlexIO SPI access structure typedef. */
  109. typedef struct _flexio_spi_type
  110. {
  111. FLEXIO_Type *flexioBase; /*!< FlexIO base pointer. */
  112. uint8_t SDOPinIndex; /*!< Pin select for data output. */
  113. uint8_t SDIPinIndex; /*!< Pin select for data input. */
  114. uint8_t SCKPinIndex; /*!< Pin select for clock. */
  115. uint8_t CSnPinIndex; /*!< Pin select for enable. */
  116. uint8_t shifterIndex[2]; /*!< Shifter index used in FlexIO SPI. */
  117. uint8_t timerIndex[2]; /*!< Timer index used in FlexIO SPI. */
  118. } FLEXIO_SPI_Type;
  119. /*! @brief Define FlexIO SPI master configuration structure. */
  120. typedef struct _flexio_spi_master_config
  121. {
  122. bool enableMaster; /*!< Enable/disable FlexIO SPI master after configuration. */
  123. bool enableInDoze; /*!< Enable/disable FlexIO operation in doze mode. */
  124. bool enableInDebug; /*!< Enable/disable FlexIO operation in debug mode. */
  125. bool enableFastAccess; /*!< Enable/disable fast access to FlexIO registers,
  126. fast access requires the FlexIO clock to be at least
  127. twice the frequency of the bus clock. */
  128. uint32_t baudRate_Bps; /*!< Baud rate in Bps. */
  129. flexio_spi_clock_phase_t phase; /*!< Clock phase. */
  130. flexio_spi_data_bitcount_mode_t dataMode; /*!< 8bit or 16bit mode. */
  131. } flexio_spi_master_config_t;
  132. /*! @brief Define FlexIO SPI slave configuration structure. */
  133. typedef struct _flexio_spi_slave_config
  134. {
  135. bool enableSlave; /*!< Enable/disable FlexIO SPI slave after configuration. */
  136. bool enableInDoze; /*!< Enable/disable FlexIO operation in doze mode. */
  137. bool enableInDebug; /*!< Enable/disable FlexIO operation in debug mode. */
  138. bool enableFastAccess; /*!< Enable/disable fast access to FlexIO registers,
  139. fast access requires the FlexIO clock to be at least
  140. twice the frequency of the bus clock. */
  141. flexio_spi_clock_phase_t phase; /*!< Clock phase. */
  142. flexio_spi_data_bitcount_mode_t dataMode; /*!< 8bit or 16bit mode. */
  143. } flexio_spi_slave_config_t;
  144. /*! @brief Define FlexIO SPI transfer structure. */
  145. typedef struct _flexio_spi_transfer
  146. {
  147. uint8_t *txData; /*!< Send buffer. */
  148. uint8_t *rxData; /*!< Receive buffer. */
  149. size_t dataSize; /*!< Transfer bytes. */
  150. uint8_t flags; /*!< FlexIO SPI control flag, MSB first or LSB first. */
  151. } flexio_spi_transfer_t;
  152. /*! @brief typedef for flexio_spi_master_handle_t in advance. */
  153. typedef struct _flexio_spi_master_handle flexio_spi_master_handle_t;
  154. /*! @brief Slave handle is the same with master handle. */
  155. typedef flexio_spi_master_handle_t flexio_spi_slave_handle_t;
  156. /*! @brief FlexIO SPI master callback for finished transmit */
  157. typedef void (*flexio_spi_master_transfer_callback_t)(FLEXIO_SPI_Type *base,
  158. flexio_spi_master_handle_t *handle,
  159. status_t status,
  160. void *userData);
  161. /*! @brief FlexIO SPI slave callback for finished transmit */
  162. typedef void (*flexio_spi_slave_transfer_callback_t)(FLEXIO_SPI_Type *base,
  163. flexio_spi_slave_handle_t *handle,
  164. status_t status,
  165. void *userData);
  166. /*! @brief Define FlexIO SPI handle structure. */
  167. struct _flexio_spi_master_handle
  168. {
  169. uint8_t *txData; /*!< Transfer buffer. */
  170. uint8_t *rxData; /*!< Receive buffer. */
  171. size_t transferSize; /*!< Total bytes to be transferred. */
  172. volatile size_t txRemainingBytes; /*!< Send data remaining in bytes. */
  173. volatile size_t rxRemainingBytes; /*!< Receive data remaining in bytes. */
  174. volatile uint32_t state; /*!< FlexIO SPI internal state. */
  175. uint8_t bytePerFrame; /*!< SPI mode, 2bytes or 1byte in a frame */
  176. flexio_spi_shift_direction_t direction; /*!< Shift direction. */
  177. flexio_spi_master_transfer_callback_t callback; /*!< FlexIO SPI callback. */
  178. void *userData; /*!< Callback parameter. */
  179. };
  180. /*******************************************************************************
  181. * API
  182. ******************************************************************************/
  183. #if defined(__cplusplus)
  184. extern "C" {
  185. #endif /*_cplusplus*/
  186. /*!
  187. * @name FlexIO SPI Configuration
  188. * @{
  189. */
  190. /*!
  191. * @brief Ungates the FlexIO clock, resets the FlexIO module, configures the FlexIO SPI master hardware,
  192. * and configures the FlexIO SPI with FlexIO SPI master configuration. The
  193. * configuration structure can be filled by the user, or be set with default values
  194. * by the FLEXIO_SPI_MasterGetDefaultConfig().
  195. *
  196. * @note FlexIO SPI master only support CPOL = 0, which means clock inactive low.
  197. *
  198. * Example
  199. @code
  200. FLEXIO_SPI_Type spiDev = {
  201. .flexioBase = FLEXIO,
  202. .SDOPinIndex = 0,
  203. .SDIPinIndex = 1,
  204. .SCKPinIndex = 2,
  205. .CSnPinIndex = 3,
  206. .shifterIndex = {0,1},
  207. .timerIndex = {0,1}
  208. };
  209. flexio_spi_master_config_t config = {
  210. .enableMaster = true,
  211. .enableInDoze = false,
  212. .enableInDebug = true,
  213. .enableFastAccess = false,
  214. .baudRate_Bps = 500000,
  215. .phase = kFLEXIO_SPI_ClockPhaseFirstEdge,
  216. .direction = kFLEXIO_SPI_MsbFirst,
  217. .dataMode = kFLEXIO_SPI_8BitMode
  218. };
  219. FLEXIO_SPI_MasterInit(&spiDev, &config, srcClock_Hz);
  220. @endcode
  221. *
  222. * @param base Pointer to the FLEXIO_SPI_Type structure.
  223. * @param masterConfig Pointer to the flexio_spi_master_config_t structure.
  224. * @param srcClock_Hz FlexIO source clock in Hz.
  225. */
  226. void FLEXIO_SPI_MasterInit(FLEXIO_SPI_Type *base, flexio_spi_master_config_t *masterConfig, uint32_t srcClock_Hz);
  227. /*!
  228. * @brief Resets the FlexIO SPI timer and shifter config.
  229. *
  230. * @param base Pointer to the FLEXIO_SPI_Type.
  231. */
  232. void FLEXIO_SPI_MasterDeinit(FLEXIO_SPI_Type *base);
  233. /*!
  234. * @brief Gets the default configuration to configure the FlexIO SPI master. The configuration
  235. * can be used directly by calling the FLEXIO_SPI_MasterConfigure().
  236. * Example:
  237. @code
  238. flexio_spi_master_config_t masterConfig;
  239. FLEXIO_SPI_MasterGetDefaultConfig(&masterConfig);
  240. @endcode
  241. * @param masterConfig Pointer to the flexio_spi_master_config_t structure.
  242. */
  243. void FLEXIO_SPI_MasterGetDefaultConfig(flexio_spi_master_config_t *masterConfig);
  244. /*!
  245. * @brief Ungates the FlexIO clock, resets the FlexIO module, configures the FlexIO SPI slave hardware
  246. * configuration, and configures the FlexIO SPI with FlexIO SPI slave configuration. The
  247. * configuration structure can be filled by the user, or be set with default values
  248. * by the FLEXIO_SPI_SlaveGetDefaultConfig().
  249. *
  250. * @note Only one timer is needed in the FlexIO SPI slave. As a result, the second timer index is ignored.
  251. * FlexIO SPI slave only support CPOL = 0, which means clock inactive low.
  252. * Example
  253. @code
  254. FLEXIO_SPI_Type spiDev = {
  255. .flexioBase = FLEXIO,
  256. .SDOPinIndex = 0,
  257. .SDIPinIndex = 1,
  258. .SCKPinIndex = 2,
  259. .CSnPinIndex = 3,
  260. .shifterIndex = {0,1},
  261. .timerIndex = {0}
  262. };
  263. flexio_spi_slave_config_t config = {
  264. .enableSlave = true,
  265. .enableInDoze = false,
  266. .enableInDebug = true,
  267. .enableFastAccess = false,
  268. .phase = kFLEXIO_SPI_ClockPhaseFirstEdge,
  269. .direction = kFLEXIO_SPI_MsbFirst,
  270. .dataMode = kFLEXIO_SPI_8BitMode
  271. };
  272. FLEXIO_SPI_SlaveInit(&spiDev, &config);
  273. @endcode
  274. * @param base Pointer to the FLEXIO_SPI_Type structure.
  275. * @param slaveConfig Pointer to the flexio_spi_slave_config_t structure.
  276. */
  277. void FLEXIO_SPI_SlaveInit(FLEXIO_SPI_Type *base, flexio_spi_slave_config_t *slaveConfig);
  278. /*!
  279. * @brief Gates the FlexIO clock.
  280. *
  281. * @param base Pointer to the FLEXIO_SPI_Type.
  282. */
  283. void FLEXIO_SPI_SlaveDeinit(FLEXIO_SPI_Type *base);
  284. /*!
  285. * @brief Gets the default configuration to configure the FlexIO SPI slave. The configuration
  286. * can be used directly for calling the FLEXIO_SPI_SlaveConfigure().
  287. * Example:
  288. @code
  289. flexio_spi_slave_config_t slaveConfig;
  290. FLEXIO_SPI_SlaveGetDefaultConfig(&slaveConfig);
  291. @endcode
  292. * @param slaveConfig Pointer to the flexio_spi_slave_config_t structure.
  293. */
  294. void FLEXIO_SPI_SlaveGetDefaultConfig(flexio_spi_slave_config_t *slaveConfig);
  295. /*@}*/
  296. /*!
  297. * @name Status
  298. * @{
  299. */
  300. /*!
  301. * @brief Gets FlexIO SPI status flags.
  302. *
  303. * @param base Pointer to the FLEXIO_SPI_Type structure.
  304. * @return status flag; Use the status flag to AND the following flag mask and get the status.
  305. * @arg kFLEXIO_SPI_TxEmptyFlag
  306. * @arg kFLEXIO_SPI_RxEmptyFlag
  307. */
  308. uint32_t FLEXIO_SPI_GetStatusFlags(FLEXIO_SPI_Type *base);
  309. /*!
  310. * @brief Clears FlexIO SPI status flags.
  311. *
  312. * @param base Pointer to the FLEXIO_SPI_Type structure.
  313. * @param mask status flag
  314. * The parameter can be any combination of the following values:
  315. * @arg kFLEXIO_SPI_TxEmptyFlag
  316. * @arg kFLEXIO_SPI_RxEmptyFlag
  317. */
  318. void FLEXIO_SPI_ClearStatusFlags(FLEXIO_SPI_Type *base, uint32_t mask);
  319. /*@}*/
  320. /*!
  321. * @name Interrupts
  322. * @{
  323. */
  324. /*!
  325. * @brief Enables the FlexIO SPI interrupt.
  326. *
  327. * This function enables the FlexIO SPI interrupt.
  328. *
  329. * @param base Pointer to the FLEXIO_SPI_Type structure.
  330. * @param mask interrupt source. The parameter can be any combination of the following values:
  331. * @arg kFLEXIO_SPI_RxFullInterruptEnable
  332. * @arg kFLEXIO_SPI_TxEmptyInterruptEnable
  333. */
  334. void FLEXIO_SPI_EnableInterrupts(FLEXIO_SPI_Type *base, uint32_t mask);
  335. /*!
  336. * @brief Disables the FlexIO SPI interrupt.
  337. *
  338. * This function disables the FlexIO SPI interrupt.
  339. *
  340. * @param base Pointer to the FLEXIO_SPI_Type structure.
  341. * @param mask interrupt source The parameter can be any combination of the following values:
  342. * @arg kFLEXIO_SPI_RxFullInterruptEnable
  343. * @arg kFLEXIO_SPI_TxEmptyInterruptEnable
  344. */
  345. void FLEXIO_SPI_DisableInterrupts(FLEXIO_SPI_Type *base, uint32_t mask);
  346. /*@}*/
  347. /*!
  348. * @name DMA Control
  349. * @{
  350. */
  351. /*!
  352. * @brief Enables/disables the FlexIO SPI transmit DMA. This function enables/disables the FlexIO SPI Tx DMA,
  353. * which means that asserting the kFLEXIO_SPI_TxEmptyFlag does/doesn't trigger the DMA request.
  354. *
  355. * @param base Pointer to the FLEXIO_SPI_Type structure.
  356. * @param mask SPI DMA source.
  357. * @param enable True means enable DMA, false means disable DMA.
  358. */
  359. void FLEXIO_SPI_EnableDMA(FLEXIO_SPI_Type *base, uint32_t mask, bool enable);
  360. /*!
  361. * @brief Gets the FlexIO SPI transmit data register address for MSB first transfer.
  362. *
  363. * This function returns the SPI data register address, which is mainly used by DMA/eDMA.
  364. *
  365. * @param base Pointer to the FLEXIO_SPI_Type structure.
  366. * @param direction Shift direction of MSB first or LSB first.
  367. * @return FlexIO SPI transmit data register address.
  368. */
  369. static inline uint32_t FLEXIO_SPI_GetTxDataRegisterAddress(FLEXIO_SPI_Type *base,
  370. flexio_spi_shift_direction_t direction)
  371. {
  372. if (direction == kFLEXIO_SPI_MsbFirst)
  373. {
  374. return FLEXIO_GetShifterBufferAddress(base->flexioBase, kFLEXIO_ShifterBufferBitSwapped,
  375. base->shifterIndex[0]) +
  376. 3U;
  377. }
  378. else
  379. {
  380. return FLEXIO_GetShifterBufferAddress(base->flexioBase, kFLEXIO_ShifterBuffer, base->shifterIndex[0]);
  381. }
  382. }
  383. /*!
  384. * @brief Gets the FlexIO SPI receive data register address for the MSB first transfer.
  385. *
  386. * This function returns the SPI data register address, which is mainly used by DMA/eDMA.
  387. *
  388. * @param base Pointer to the FLEXIO_SPI_Type structure.
  389. * @param direction Shift direction of MSB first or LSB first.
  390. * @return FlexIO SPI receive data register address.
  391. */
  392. static inline uint32_t FLEXIO_SPI_GetRxDataRegisterAddress(FLEXIO_SPI_Type *base,
  393. flexio_spi_shift_direction_t direction)
  394. {
  395. if (direction == kFLEXIO_SPI_MsbFirst)
  396. {
  397. return FLEXIO_GetShifterBufferAddress(base->flexioBase, kFLEXIO_ShifterBufferBitSwapped, base->shifterIndex[1]);
  398. }
  399. else
  400. {
  401. return FLEXIO_GetShifterBufferAddress(base->flexioBase, kFLEXIO_ShifterBuffer, base->shifterIndex[1]) + 3U;
  402. }
  403. }
  404. /*@}*/
  405. /*!
  406. * @name Bus Operations
  407. * @{
  408. */
  409. /*!
  410. * @brief Enables/disables the FlexIO SPI module operation.
  411. *
  412. * @param base Pointer to the FLEXIO_SPI_Type.
  413. * @param enable True to enable, false does not have any effect.
  414. */
  415. static inline void FLEXIO_SPI_Enable(FLEXIO_SPI_Type *base, bool enable)
  416. {
  417. if (enable)
  418. {
  419. base->flexioBase->CTRL |= FLEXIO_CTRL_FLEXEN_MASK;
  420. }
  421. }
  422. /*!
  423. * @brief Sets baud rate for the FlexIO SPI transfer, which is only used for the master.
  424. *
  425. * @param base Pointer to the FLEXIO_SPI_Type structure.
  426. * @param baudRate_Bps Baud Rate needed in Hz.
  427. * @param srcClockHz SPI source clock frequency in Hz.
  428. */
  429. void FLEXIO_SPI_MasterSetBaudRate(FLEXIO_SPI_Type *base, uint32_t baudRate_Bps, uint32_t srcClockHz);
  430. /*!
  431. * @brief Writes one byte of data, which is sent using the MSB method.
  432. *
  433. * @note This is a non-blocking API, which returns directly after the data is put into the
  434. * data register but the data transfer is not finished on the bus. Ensure that
  435. * the TxEmptyFlag is asserted before calling this API.
  436. *
  437. * @param base Pointer to the FLEXIO_SPI_Type structure.
  438. * @param direction Shift direction of MSB first or LSB first.
  439. * @param data 8 bit/16 bit data.
  440. */
  441. static inline void FLEXIO_SPI_WriteData(FLEXIO_SPI_Type *base, flexio_spi_shift_direction_t direction, uint16_t data)
  442. {
  443. if (direction == kFLEXIO_SPI_MsbFirst)
  444. {
  445. base->flexioBase->SHIFTBUFBBS[base->shifterIndex[0]] = data;
  446. }
  447. else
  448. {
  449. base->flexioBase->SHIFTBUF[base->shifterIndex[0]] = data;
  450. }
  451. }
  452. /*!
  453. * @brief Reads 8 bit/16 bit data.
  454. *
  455. * @note This is a non-blocking API, which returns directly after the data is read from the
  456. * data register. Ensure that the RxFullFlag is asserted before calling this API.
  457. *
  458. * @param base Pointer to the FLEXIO_SPI_Type structure.
  459. * @param direction Shift direction of MSB first or LSB first.
  460. * @return 8 bit/16 bit data received.
  461. */
  462. static inline uint16_t FLEXIO_SPI_ReadData(FLEXIO_SPI_Type *base, flexio_spi_shift_direction_t direction)
  463. {
  464. if (direction == kFLEXIO_SPI_MsbFirst)
  465. {
  466. return base->flexioBase->SHIFTBUFBIS[base->shifterIndex[1]];
  467. }
  468. else
  469. {
  470. return base->flexioBase->SHIFTBUFBYS[base->shifterIndex[1]];
  471. }
  472. }
  473. /*!
  474. * @brief Sends a buffer of data bytes.
  475. *
  476. * @note This function blocks using the polling method until all bytes have been sent.
  477. *
  478. * @param base Pointer to the FLEXIO_SPI_Type structure.
  479. * @param direction Shift direction of MSB first or LSB first.
  480. * @param buffer The data bytes to send.
  481. * @param size The number of data bytes to send.
  482. */
  483. void FLEXIO_SPI_WriteBlocking(FLEXIO_SPI_Type *base,
  484. flexio_spi_shift_direction_t direction,
  485. const uint8_t *buffer,
  486. size_t size);
  487. /*!
  488. * @brief Receives a buffer of bytes.
  489. *
  490. * @note This function blocks using the polling method until all bytes have been received.
  491. *
  492. * @param base Pointer to the FLEXIO_SPI_Type structure.
  493. * @param direction Shift direction of MSB first or LSB first.
  494. * @param buffer The buffer to store the received bytes.
  495. * @param size The number of data bytes to be received.
  496. * @param direction Shift direction of MSB first or LSB first.
  497. */
  498. void FLEXIO_SPI_ReadBlocking(FLEXIO_SPI_Type *base,
  499. flexio_spi_shift_direction_t direction,
  500. uint8_t *buffer,
  501. size_t size);
  502. /*!
  503. * @brief Receives a buffer of bytes.
  504. *
  505. * @note This function blocks via polling until all bytes have been received.
  506. *
  507. * @param base pointer to FLEXIO_SPI_Type structure
  508. * @param xfer FlexIO SPI transfer structure, see #flexio_spi_transfer_t.
  509. */
  510. void FLEXIO_SPI_MasterTransferBlocking(FLEXIO_SPI_Type *base, flexio_spi_transfer_t *xfer);
  511. /*Transactional APIs*/
  512. /*!
  513. * @name Transactional
  514. * @{
  515. */
  516. /*!
  517. * @brief Initializes the FlexIO SPI Master handle, which is used in transactional functions.
  518. *
  519. * @param base Pointer to the FLEXIO_SPI_Type structure.
  520. * @param handle Pointer to the flexio_spi_master_handle_t structure to store the transfer state.
  521. * @param callback The callback function.
  522. * @param userData The parameter of the callback function.
  523. * @retval kStatus_Success Successfully create the handle.
  524. * @retval kStatus_OutOfRange The FlexIO type/handle/ISR table out of range.
  525. */
  526. status_t FLEXIO_SPI_MasterTransferCreateHandle(FLEXIO_SPI_Type *base,
  527. flexio_spi_master_handle_t *handle,
  528. flexio_spi_master_transfer_callback_t callback,
  529. void *userData);
  530. /*!
  531. * @brief Master transfer data using IRQ.
  532. *
  533. * This function sends data using IRQ. This is a non-blocking function, which returns
  534. * right away. When all data is sent out/received, the callback function is called.
  535. *
  536. * @param base Pointer to the FLEXIO_SPI_Type structure.
  537. * @param handle Pointer to the flexio_spi_master_handle_t structure to store the transfer state.
  538. * @param xfer FlexIO SPI transfer structure. See #flexio_spi_transfer_t.
  539. * @retval kStatus_Success Successfully start a transfer.
  540. * @retval kStatus_InvalidArgument Input argument is invalid.
  541. * @retval kStatus_FLEXIO_SPI_Busy SPI is not idle, is running another transfer.
  542. */
  543. status_t FLEXIO_SPI_MasterTransferNonBlocking(FLEXIO_SPI_Type *base,
  544. flexio_spi_master_handle_t *handle,
  545. flexio_spi_transfer_t *xfer);
  546. /*!
  547. * @brief Aborts the master data transfer, which used IRQ.
  548. *
  549. * @param base Pointer to the FLEXIO_SPI_Type structure.
  550. * @param handle Pointer to the flexio_spi_master_handle_t structure to store the transfer state.
  551. */
  552. void FLEXIO_SPI_MasterTransferAbort(FLEXIO_SPI_Type *base, flexio_spi_master_handle_t *handle);
  553. /*!
  554. * @brief Gets the data transfer status which used IRQ.
  555. *
  556. * @param base Pointer to the FLEXIO_SPI_Type structure.
  557. * @param handle Pointer to the flexio_spi_master_handle_t structure to store the transfer state.
  558. * @param count Number of bytes transferred so far by the non-blocking transaction.
  559. * @retval kStatus_InvalidArgument count is Invalid.
  560. * @retval kStatus_Success Successfully return the count.
  561. */
  562. status_t FLEXIO_SPI_MasterTransferGetCount(FLEXIO_SPI_Type *base, flexio_spi_master_handle_t *handle, size_t *count);
  563. /*!
  564. * @brief FlexIO SPI master IRQ handler function.
  565. *
  566. * @param spiType Pointer to the FLEXIO_SPI_Type structure.
  567. * @param spiHandle Pointer to the flexio_spi_master_handle_t structure to store the transfer state.
  568. */
  569. void FLEXIO_SPI_MasterTransferHandleIRQ(void *spiType, void *spiHandle);
  570. /*!
  571. * @brief Initializes the FlexIO SPI Slave handle, which is used in transactional functions.
  572. *
  573. * @param base Pointer to the FLEXIO_SPI_Type structure.
  574. * @param handle Pointer to the flexio_spi_slave_handle_t structure to store the transfer state.
  575. * @param callback The callback function.
  576. * @param userData The parameter of the callback function.
  577. * @retval kStatus_Success Successfully create the handle.
  578. * @retval kStatus_OutOfRange The FlexIO type/handle/ISR table out of range.
  579. */
  580. status_t FLEXIO_SPI_SlaveTransferCreateHandle(FLEXIO_SPI_Type *base,
  581. flexio_spi_slave_handle_t *handle,
  582. flexio_spi_slave_transfer_callback_t callback,
  583. void *userData);
  584. /*!
  585. * @brief Slave transfer data using IRQ.
  586. *
  587. * This function sends data using IRQ. This is a non-blocking function, which returns
  588. * right away. When all data is sent out/received, the callback function is called.
  589. * @param handle Pointer to the flexio_spi_slave_handle_t structure to store the transfer state.
  590. *
  591. * @param base Pointer to the FLEXIO_SPI_Type structure.
  592. * @param xfer FlexIO SPI transfer structure. See #flexio_spi_transfer_t.
  593. * @retval kStatus_Success Successfully start a transfer.
  594. * @retval kStatus_InvalidArgument Input argument is invalid.
  595. * @retval kStatus_FLEXIO_SPI_Busy SPI is not idle; it is running another transfer.
  596. */
  597. status_t FLEXIO_SPI_SlaveTransferNonBlocking(FLEXIO_SPI_Type *base,
  598. flexio_spi_slave_handle_t *handle,
  599. flexio_spi_transfer_t *xfer);
  600. /*!
  601. * @brief Aborts the slave data transfer which used IRQ, share same API with master.
  602. *
  603. * @param base Pointer to the FLEXIO_SPI_Type structure.
  604. * @param handle Pointer to the flexio_spi_slave_handle_t structure to store the transfer state.
  605. */
  606. static inline void FLEXIO_SPI_SlaveTransferAbort(FLEXIO_SPI_Type *base, flexio_spi_slave_handle_t *handle)
  607. {
  608. FLEXIO_SPI_MasterTransferAbort(base, handle);
  609. }
  610. /*!
  611. * @brief Gets the data transfer status which used IRQ, share same API with master.
  612. *
  613. * @param base Pointer to the FLEXIO_SPI_Type structure.
  614. * @param handle Pointer to the flexio_spi_slave_handle_t structure to store the transfer state.
  615. * @param count Number of bytes transferred so far by the non-blocking transaction.
  616. * @retval kStatus_InvalidArgument count is Invalid.
  617. * @retval kStatus_Success Successfully return the count.
  618. */
  619. static inline status_t FLEXIO_SPI_SlaveTransferGetCount(FLEXIO_SPI_Type *base,
  620. flexio_spi_slave_handle_t *handle,
  621. size_t *count)
  622. {
  623. return FLEXIO_SPI_MasterTransferGetCount(base, handle, count);
  624. }
  625. /*!
  626. * @brief FlexIO SPI slave IRQ handler function.
  627. *
  628. * @param spiType Pointer to the FLEXIO_SPI_Type structure.
  629. * @param spiHandle Pointer to the flexio_spi_slave_handle_t structure to store the transfer state.
  630. */
  631. void FLEXIO_SPI_SlaveTransferHandleIRQ(void *spiType, void *spiHandle);
  632. /*@}*/
  633. #if defined(__cplusplus)
  634. }
  635. #endif /*_cplusplus*/
  636. /*@}*/
  637. #endif /*_FSL_FLEXIO_SPI_H_*/