fsl_flexio_spi.h 27 KB

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