fsl_flexio_spi.h 27 KB

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